Merge pull request #11 from 0xProject/isValidOrderHash
Port isValidOrderHash and tests
This commit is contained in:
commit
f7b8378a6e
@ -33,9 +33,9 @@
|
|||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/bignumber.js": "^4.0.2",
|
"@types/bignumber.js": "^4.0.2",
|
||||||
"@types/chai": "^3.5.2",
|
"@types/chai": "^3.5.2",
|
||||||
"@types/jsonschema": "^1.1.1",
|
|
||||||
"@types/mocha": "^2.2.41",
|
"@types/mocha": "^2.2.41",
|
||||||
"@types/node": "^7.0.22",
|
"@types/node": "^7.0.22",
|
||||||
|
"@types/lodash": "^4.14.64",
|
||||||
"awesome-typescript-loader": "^3.1.3",
|
"awesome-typescript-loader": "^3.1.3",
|
||||||
"bignumber.js": "^4.0.2",
|
"bignumber.js": "^4.0.2",
|
||||||
"chai": "^3.5.0",
|
"chai": "^3.5.0",
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
import * as BigNumber from 'bignumber.js';
|
import * as BigNumber from 'bignumber.js';
|
||||||
import * as ethUtil from 'ethereumjs-util';
|
import * as ethUtil from 'ethereumjs-util';
|
||||||
|
import * as _ from 'lodash';
|
||||||
import {assert} from './utils/assert';
|
import {assert} from './utils/assert';
|
||||||
import {ECSignatureSchema} from './schemas/ec_signature_schema';
|
import {ECSignatureSchema} from './schemas/ec_signature_schema';
|
||||||
|
|
||||||
@ -17,14 +18,14 @@ const MAX_DIGITS_IN_UNSIGNED_256_INT = 78;
|
|||||||
export class ZeroEx {
|
export class ZeroEx {
|
||||||
/**
|
/**
|
||||||
* Verifies that the elliptic curve signature `signature` was generated
|
* Verifies that the elliptic curve signature `signature` was generated
|
||||||
* by signing `data` with the private key corresponding to the `signer` address.
|
* by signing `data` with the private key corresponding to the `signerAddressHex` address.
|
||||||
*/
|
*/
|
||||||
public static isValidSignature(data: string, signature: ECSignature, signer: ETHAddressHex): boolean {
|
public static isValidSignature(dataHex: string, signature: ECSignature, signerAddressHex: string): boolean {
|
||||||
assert.isString('data', data);
|
assert.isHexString('dataHex', dataHex);
|
||||||
assert.doesConformToSchema('signature', signature, ECSignatureSchema);
|
assert.doesConformToSchema('signature', signature, ECSignatureSchema);
|
||||||
assert.isETHAddressHex('signer', signer);
|
assert.isETHAddressHex('signerAddressHex', signerAddressHex);
|
||||||
|
|
||||||
const dataBuff = ethUtil.toBuffer(data);
|
const dataBuff = ethUtil.toBuffer(dataHex);
|
||||||
const msgHashBuff = ethUtil.hashPersonalMessage(dataBuff);
|
const msgHashBuff = ethUtil.hashPersonalMessage(dataBuff);
|
||||||
try {
|
try {
|
||||||
const pubKey = ethUtil.ecrecover(msgHashBuff,
|
const pubKey = ethUtil.ecrecover(msgHashBuff,
|
||||||
@ -32,7 +33,7 @@ export class ZeroEx {
|
|||||||
ethUtil.toBuffer(signature.r),
|
ethUtil.toBuffer(signature.r),
|
||||||
ethUtil.toBuffer(signature.s));
|
ethUtil.toBuffer(signature.s));
|
||||||
const retrievedAddress = ethUtil.bufferToHex(ethUtil.pubToAddress(pubKey));
|
const retrievedAddress = ethUtil.bufferToHex(ethUtil.pubToAddress(pubKey));
|
||||||
return retrievedAddress === signer;
|
return retrievedAddress === signerAddressHex;
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -50,6 +51,12 @@ export class ZeroEx {
|
|||||||
const salt = randomNumber.times(factor).round();
|
const salt = randomNumber.times(factor).round();
|
||||||
return salt;
|
return salt;
|
||||||
}
|
}
|
||||||
|
/** Checks if order hash is valid */
|
||||||
|
public static isValidOrderHash(orderHash: string): boolean {
|
||||||
|
assert.isString('orderHash', orderHash);
|
||||||
|
const isValid = /^0x[0-9A-F]{64}$/i.test(orderHash);
|
||||||
|
return isValid;
|
||||||
|
}
|
||||||
/*
|
/*
|
||||||
* A unit amount is defined as the amount of a token above the specified decimal places (integer part).
|
* A unit amount is defined as the amount of a token above the specified decimal places (integer part).
|
||||||
* E.g: If a currency has 18 decimal places, 1e18 or one quintillion of the currency is equivalent
|
* E.g: If a currency has 18 decimal places, 1e18 or one quintillion of the currency is equivalent
|
||||||
|
11
src/ts/globals.d.ts
vendored
11
src/ts/globals.d.ts
vendored
@ -1,9 +1,5 @@
|
|||||||
declare module 'chai-bignumber';
|
declare module 'chai-bignumber';
|
||||||
|
|
||||||
declare type ETHPublicKey = string;
|
|
||||||
declare type ETHAddressHex = string;
|
|
||||||
declare type ETHAddressBuff = Buffer;
|
|
||||||
|
|
||||||
declare interface Schema {
|
declare interface Schema {
|
||||||
id: string;
|
id: string;
|
||||||
}
|
}
|
||||||
@ -20,9 +16,10 @@ declare namespace Chai {
|
|||||||
/* tslint:enable */
|
/* tslint:enable */
|
||||||
|
|
||||||
declare module 'ethereumjs-util' {
|
declare module 'ethereumjs-util' {
|
||||||
const toBuffer: (data: string) => Buffer;
|
const toBuffer: (dataHex: string) => Buffer;
|
||||||
const hashPersonalMessage: (msg: Buffer) => Buffer;
|
const hashPersonalMessage: (msg: Buffer) => Buffer;
|
||||||
const bufferToHex: (buff: Buffer) => string;
|
const bufferToHex: (buff: Buffer) => string;
|
||||||
const ecrecover: (msgHashBuff: Buffer, v: number, r: Buffer, s: Buffer) => ETHPublicKey;
|
const ecrecover: (msgHashBuff: Buffer, v: number, r: Buffer, s: Buffer) => string;
|
||||||
const pubToAddress: (pubKey: ETHPublicKey) => ETHAddressBuff;
|
const pubToAddress: (pubKey: string) => Buffer;
|
||||||
|
const isValidAddress: (address: string) => boolean;
|
||||||
}
|
}
|
||||||
|
@ -3,6 +3,8 @@ import * as BigNumber from 'bignumber.js';
|
|||||||
import Web3 = require('web3');
|
import Web3 = require('web3');
|
||||||
import {SchemaValidator} from './schema_validator';
|
import {SchemaValidator} from './schema_validator';
|
||||||
|
|
||||||
|
const HEX_REGEX = /^0x[0-9A-F]*$/i;
|
||||||
|
|
||||||
export const assert = {
|
export const assert = {
|
||||||
isBigNumber(variableName: string, value: BigNumber.BigNumber) {
|
isBigNumber(variableName: string, value: BigNumber.BigNumber) {
|
||||||
const isBigNumber = _.isObject(value) && value.isBigNumber;
|
const isBigNumber = _.isObject(value) && value.isBigNumber;
|
||||||
@ -11,7 +13,11 @@ export const assert = {
|
|||||||
isString(variableName: string, value: string) {
|
isString(variableName: string, value: string) {
|
||||||
this.assert(_.isString(value), this.typeAssertionMessage(variableName, 'string', value));
|
this.assert(_.isString(value), this.typeAssertionMessage(variableName, 'string', value));
|
||||||
},
|
},
|
||||||
isETHAddressHex(variableName: string, value: ETHAddressHex) {
|
isHexString(variableName: string, value: string) {
|
||||||
|
this.assert(_.isString(value) && HEX_REGEX.test(value),
|
||||||
|
this.typeAssertionMessage(variableName, 'HexString', value));
|
||||||
|
},
|
||||||
|
isETHAddressHex(variableName: string, value: string) {
|
||||||
const web3 = new Web3();
|
const web3 = new Web3();
|
||||||
this.assert(web3.isAddress(value), this.typeAssertionMessage(variableName, 'ETHAddressHex', value));
|
this.assert(web3.isAddress(value), this.typeAssertionMessage(variableName, 'ETHAddressHex', value));
|
||||||
},
|
},
|
||||||
|
@ -57,7 +57,7 @@ describe('ZeroEx library', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
it('should return false if the data doesn\'t pertain to the signature & address', () => {
|
it('should return false if the data doesn\'t pertain to the signature & address', () => {
|
||||||
const isValid = ZeroEx.isValidSignature('wrong data', signature, address);
|
const isValid = ZeroEx.isValidSignature('0x0', signature, address);
|
||||||
expect(isValid).to.be.false;
|
expect(isValid).to.be.false;
|
||||||
});
|
});
|
||||||
it('should return false if the address doesn\'t pertain to the signature & data', () => {
|
it('should return false if the address doesn\'t pertain to the signature & data', () => {
|
||||||
@ -87,6 +87,20 @@ describe('ZeroEx library', () => {
|
|||||||
expect(salt.lessThan(twoPow256)).to.be.true;
|
expect(salt.lessThan(twoPow256)).to.be.true;
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
describe('#isValidOrderHash', () => {
|
||||||
|
it('returns false if the value is not a hex string', () => {
|
||||||
|
const isValid = ZeroEx.isValidOrderHash('not a hex');
|
||||||
|
expect(isValid).to.be.false;
|
||||||
|
});
|
||||||
|
it('returns false if the length is wrong', () => {
|
||||||
|
const isValid = ZeroEx.isValidOrderHash('0xdeadbeef');
|
||||||
|
expect(isValid).to.be.false;
|
||||||
|
});
|
||||||
|
it('returns true if order hash is correct', () => {
|
||||||
|
const isValid = ZeroEx.isValidOrderHash('0x' + Array(65).join('0'));
|
||||||
|
expect(isValid).to.be.true;
|
||||||
|
});
|
||||||
|
});
|
||||||
describe('#toUnitAmount', () => {
|
describe('#toUnitAmount', () => {
|
||||||
it('Should return the expected unit amount for the decimals passed in', () => {
|
it('Should return the expected unit amount for the decimals passed in', () => {
|
||||||
const baseUnitAmount = new BigNumber(1000000000);
|
const baseUnitAmount = new BigNumber(1000000000);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user