blob: 4b9680a18d5686173c7ee1e368a52e07a1bac3b5 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
import * as BigNumber from 'bignumber.js';
import * as ethUtil from 'ethereumjs-util';
import {assert} from './utils/assert';
/**
* Elliptic Curve signature
*/
export interface ECSignature {
v: number;
r: string;
s: string;
}
const MAX_DIGITS_IN_UNSIGNED_256_INT = 78;
export class ZeroEx {
/**
* Verifies that the elliptic curve signature `signature` was generated
* by signing `data` with the private key corresponding to the `signer` address.
*/
public static isValidSignature(data: string, signature: ECSignature, signer: ETHAddressHex): boolean {
assert.isString('data', data);
assert.isObject('signature', signature);
assert.isETHAddressHex('signer', signer);
const dataBuff = ethUtil.toBuffer(data);
const msgHashBuff = ethUtil.hashPersonalMessage(dataBuff);
try {
const pubKey = ethUtil.ecrecover(msgHashBuff,
signature.v,
ethUtil.toBuffer(signature.r),
ethUtil.toBuffer(signature.s));
const retrievedAddress = ethUtil.bufferToHex(ethUtil.pubToAddress(pubKey));
return retrievedAddress === signer;
} catch (err) {
return false;
}
}
/**
* Generates pseudo-random 256 bit salt.
* The salt is used to ensure that the 0x order generated has a unique orderHash that does
* not collide with any other outstanding orders.
*/
public static generatePseudoRandomSalt(): BigNumber.BigNumber {
// BigNumber.random returns a pseudo-random number between 0 & 1 with a passed in number of decimal places.
// Source: https://mikemcl.github.io/bignumber.js/#random
const randomNumber = BigNumber.random(MAX_DIGITS_IN_UNSIGNED_256_INT);
const factor = new BigNumber(10).pow(MAX_DIGITS_IN_UNSIGNED_256_INT - 1);
const salt = randomNumber.times(factor).round();
return salt;
}
}
|