From 5be5debdf1122d7f9767fa7e5cd23a23d91bf93c Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Thu, 25 May 2017 12:08:54 +0200 Subject: Port isValidOrderHash and tests --- src/ts/0x.js.ts | 8 +++++++- src/ts/globals.d.ts | 5 +++-- src/ts/utils/assert.ts | 6 ++++++ test/0x.js.ts | 10 ++++++++++ 4 files changed, 26 insertions(+), 3 deletions(-) diff --git a/src/ts/0x.js.ts b/src/ts/0x.js.ts index 4b9680a18..1f9ea5f9b 100644 --- a/src/ts/0x.js.ts +++ b/src/ts/0x.js.ts @@ -12,13 +12,14 @@ export interface ECSignature { } const MAX_DIGITS_IN_UNSIGNED_256_INT = 78; +const ORDER_HASH_LENGTH = 66; 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 { + public static isValidSignature(data: HexString, signature: ECSignature, signer: ETHAddressHex): boolean { assert.isString('data', data); assert.isObject('signature', signature); assert.isETHAddressHex('signer', signer); @@ -49,4 +50,9 @@ export class ZeroEx { const salt = randomNumber.times(factor).round(); return salt; } + /** Checks if order hash is valid */ + public static isValidOrderHash(orderHash: HexString): boolean { + assert.isHexString('orderHash', orderHash); + return orderHash.length === ORDER_HASH_LENGTH; + } } diff --git a/src/ts/globals.d.ts b/src/ts/globals.d.ts index 0f7391b39..9b81e15bd 100644 --- a/src/ts/globals.d.ts +++ b/src/ts/globals.d.ts @@ -1,11 +1,12 @@ declare type ETHPublicKey = string; declare type ETHAddressHex = string; +declare type HexString = string; declare type ETHAddressBuff = Buffer; declare module 'ethereumjs-util' { - const toBuffer: (data: string) => Buffer; + const toBuffer: (data: HexString) => Buffer; const hashPersonalMessage: (msg: Buffer) => Buffer; - const bufferToHex: (buff: Buffer) => string; + const bufferToHex: (buff: Buffer) => HexString; const ecrecover: (msgHashBuff: Buffer, v: number, r: Buffer, s: Buffer) => ETHPublicKey; const pubToAddress: (pubKey: ETHPublicKey) => ETHAddressBuff; } diff --git a/src/ts/utils/assert.ts b/src/ts/utils/assert.ts index a29ae922d..602361233 100644 --- a/src/ts/utils/assert.ts +++ b/src/ts/utils/assert.ts @@ -2,6 +2,8 @@ import * as _ from 'lodash'; import * as BigNumber from 'bignumber.js'; import Web3 = require('web3'); +const HEX_REGEX = /^0x([0-9A-F]{2})*$/i; + export const assert = { isBigNumber(variableName: string, value: BigNumber.BigNumber) { const isBigNumber = _.isObject(value) && value.isBigNumber; @@ -10,6 +12,10 @@ export const assert = { isString(variableName: string, value: string) { this.assert(_.isString(value), this.typeAssertionMessage(variableName, 'string', value)); }, + isHexString(variableName: string, value: string) { + this.assert(_.isString(value) && HEX_REGEX.test(value), + this.typeAssertionMessage(variableName, 'HexString', value)); + }, isETHAddressHex(variableName: string, value: ETHAddressHex) { const web3 = new Web3(); this.assert(web3.isAddress(value), this.typeAssertionMessage(variableName, 'ETHAddressHex', value)); diff --git a/test/0x.js.ts b/test/0x.js.ts index e4e3cc0a7..26e56a18b 100644 --- a/test/0x.js.ts +++ b/test/0x.js.ts @@ -86,4 +86,14 @@ describe('ZeroEx library', () => { expect(salt.lessThan(twoPow256)).to.be.true; }); }); + describe('#isValidOrderHash', () => { + 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; + }); + }); }); -- cgit v1.2.3 From 00a16b37e0bd3a394285cd887e8678fb7eb19149 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Thu, 25 May 2017 12:28:28 +0200 Subject: Remove HexString type --- src/ts/0x.js.ts | 8 ++++---- src/ts/globals.d.ts | 5 ++--- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/src/ts/0x.js.ts b/src/ts/0x.js.ts index 1f9ea5f9b..89d049bec 100644 --- a/src/ts/0x.js.ts +++ b/src/ts/0x.js.ts @@ -19,12 +19,12 @@ 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: HexString, signature: ECSignature, signer: ETHAddressHex): boolean { - assert.isString('data', data); + public static isValidSignature(dataHex: string, signature: ECSignature, signer: ETHAddressHex): boolean { + assert.isHexString('dataHex', dataHex); assert.isObject('signature', signature); assert.isETHAddressHex('signer', signer); - const dataBuff = ethUtil.toBuffer(data); + const dataBuff = ethUtil.toBuffer(dataHex); const msgHashBuff = ethUtil.hashPersonalMessage(dataBuff); try { const pubKey = ethUtil.ecrecover(msgHashBuff, @@ -51,7 +51,7 @@ export class ZeroEx { return salt; } /** Checks if order hash is valid */ - public static isValidOrderHash(orderHash: HexString): boolean { + public static isValidOrderHash(orderHash: string): boolean { assert.isHexString('orderHash', orderHash); return orderHash.length === ORDER_HASH_LENGTH; } diff --git a/src/ts/globals.d.ts b/src/ts/globals.d.ts index 9b81e15bd..c9cc0d481 100644 --- a/src/ts/globals.d.ts +++ b/src/ts/globals.d.ts @@ -1,12 +1,11 @@ declare type ETHPublicKey = string; declare type ETHAddressHex = string; -declare type HexString = string; declare type ETHAddressBuff = Buffer; declare module 'ethereumjs-util' { - const toBuffer: (data: HexString) => Buffer; + const toBuffer: (dataHex: string) => Buffer; const hashPersonalMessage: (msg: Buffer) => Buffer; - const bufferToHex: (buff: Buffer) => HexString; + const bufferToHex: (buff: Buffer) => string; const ecrecover: (msgHashBuff: Buffer, v: number, r: Buffer, s: Buffer) => ETHPublicKey; const pubToAddress: (pubKey: ETHPublicKey) => ETHAddressBuff; } -- cgit v1.2.3 From 5d64b562c60710e9b9b6a6286d4278718f043d0d Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Thu, 25 May 2017 12:52:31 +0200 Subject: Fix tests --- test/0x.js.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/0x.js.ts b/test/0x.js.ts index 26e56a18b..2da026694 100644 --- a/test/0x.js.ts +++ b/test/0x.js.ts @@ -56,7 +56,7 @@ describe('ZeroEx library', () => { }); }); 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('0x00', signature, address); expect(isValid).to.be.false; }); it('should return false if the address doesn\'t pertain to the signature & data', () => { -- cgit v1.2.3 From 74a80c28d348da6e4050e43e713113c4c0df87df Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Thu, 25 May 2017 13:00:56 +0200 Subject: Remove type aliases --- src/ts/0x.js.ts | 6 +++--- src/ts/globals.d.ts | 7 +++---- src/ts/utils/assert.ts | 2 +- 3 files changed, 7 insertions(+), 8 deletions(-) diff --git a/src/ts/0x.js.ts b/src/ts/0x.js.ts index d189b48b1..a3a83670d 100644 --- a/src/ts/0x.js.ts +++ b/src/ts/0x.js.ts @@ -20,10 +20,10 @@ 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(dataHex: string, signature: ECSignature, signer: ETHAddressHex): boolean { + public static isValidSignature(dataHex: string, signature: ECSignature, signerAddress: string): boolean { assert.isHexString('dataHex', dataHex); assert.doesConformToSchema('signature', signature, ECSignatureSchema); - assert.isETHAddressHex('signer', signer); + assert.isETHAddressHex('signerAddress', signerAddress); const dataBuff = ethUtil.toBuffer(dataHex); const msgHashBuff = ethUtil.hashPersonalMessage(dataBuff); @@ -33,7 +33,7 @@ export class ZeroEx { ethUtil.toBuffer(signature.r), ethUtil.toBuffer(signature.s)); const retrievedAddress = ethUtil.bufferToHex(ethUtil.pubToAddress(pubKey)); - return retrievedAddress === signer; + return retrievedAddress === signerAddress; } catch (err) { return false; } diff --git a/src/ts/globals.d.ts b/src/ts/globals.d.ts index 58efe136a..1e502b061 100644 --- a/src/ts/globals.d.ts +++ b/src/ts/globals.d.ts @@ -1,5 +1,3 @@ -declare type ETHPublicKey = string; -declare type ETHAddressHex = string; declare type ETHAddressBuff = Buffer; declare interface Schema { @@ -10,6 +8,7 @@ declare module 'ethereumjs-util' { const toBuffer: (dataHex: string) => Buffer; const hashPersonalMessage: (msg: Buffer) => Buffer; const bufferToHex: (buff: Buffer) => string; - const ecrecover: (msgHashBuff: Buffer, v: number, r: Buffer, s: Buffer) => ETHPublicKey; - const pubToAddress: (pubKey: ETHPublicKey) => ETHAddressBuff; + const ecrecover: (msgHashBuff: Buffer, v: number, r: Buffer, s: Buffer) => string; + const pubToAddress: (pubKey: string) => ETHAddressBuff; + const isValidAddress: (address: string) => boolean; } diff --git a/src/ts/utils/assert.ts b/src/ts/utils/assert.ts index 58182dac0..509590ee6 100644 --- a/src/ts/utils/assert.ts +++ b/src/ts/utils/assert.ts @@ -17,7 +17,7 @@ export const assert = { this.assert(_.isString(value) && HEX_REGEX.test(value), this.typeAssertionMessage(variableName, 'HexString', value)); }, - isETHAddressHex(variableName: string, value: ETHAddressHex) { + isETHAddressHex(variableName: string, value: string) { const web3 = new Web3(); this.assert(web3.isAddress(value), this.typeAssertionMessage(variableName, 'ETHAddressHex', value)); }, -- cgit v1.2.3 From eee06e0cc97333891a84aff22196849105846eb4 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Thu, 25 May 2017 13:42:53 +0200 Subject: Address feedback --- package.json | 2 +- src/ts/0x.js.ts | 13 +++++++------ src/ts/utils/assert.ts | 2 +- test/0x.js.ts | 6 +++++- 4 files changed, 14 insertions(+), 9 deletions(-) diff --git a/package.json b/package.json index 950ad0d38..a46ffcaf1 100644 --- a/package.json +++ b/package.json @@ -33,9 +33,9 @@ "devDependencies": { "@types/bignumber.js": "^4.0.2", "@types/chai": "^3.5.2", - "@types/jsonschema": "^1.1.1", "@types/mocha": "^2.2.41", "@types/node": "^7.0.22", + "@types/lodash": "^4.14.64", "awesome-typescript-loader": "^3.1.3", "bignumber.js": "^4.0.2", "chai": "^3.5.0", diff --git a/src/ts/0x.js.ts b/src/ts/0x.js.ts index a3a83670d..bd0ce80e2 100644 --- a/src/ts/0x.js.ts +++ b/src/ts/0x.js.ts @@ -1,5 +1,6 @@ import * as BigNumber from 'bignumber.js'; import * as ethUtil from 'ethereumjs-util'; +import * as _ from 'lodash'; import {assert} from './utils/assert'; import {ECSignatureSchema} from './schemas/ec_signature_schema'; @@ -13,17 +14,16 @@ export interface ECSignature { } const MAX_DIGITS_IN_UNSIGNED_256_INT = 78; -const ORDER_HASH_LENGTH = 66; 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(dataHex: string, signature: ECSignature, signerAddress: string): boolean { + public static isValidSignature(dataHex: string, signature: ECSignature, signerETHAddressHex: string): boolean { assert.isHexString('dataHex', dataHex); assert.doesConformToSchema('signature', signature, ECSignatureSchema); - assert.isETHAddressHex('signerAddress', signerAddress); + assert.isETHAddressHex('signerAddress', signerETHAddressHex); const dataBuff = ethUtil.toBuffer(dataHex); const msgHashBuff = ethUtil.hashPersonalMessage(dataBuff); @@ -33,7 +33,7 @@ export class ZeroEx { ethUtil.toBuffer(signature.r), ethUtil.toBuffer(signature.s)); const retrievedAddress = ethUtil.bufferToHex(ethUtil.pubToAddress(pubKey)); - return retrievedAddress === signerAddress; + return retrievedAddress === signerETHAddressHex; } catch (err) { return false; } @@ -53,7 +53,8 @@ export class ZeroEx { } /** Checks if order hash is valid */ public static isValidOrderHash(orderHash: string): boolean { - assert.isHexString('orderHash', orderHash); - return orderHash.length === ORDER_HASH_LENGTH; + assert.isString('orderHash', orderHash); + const isValid = /^0x[0-9A-F]{66}$/i.test(orderHash); + return isValid; } } diff --git a/src/ts/utils/assert.ts b/src/ts/utils/assert.ts index 509590ee6..2f52c6a3b 100644 --- a/src/ts/utils/assert.ts +++ b/src/ts/utils/assert.ts @@ -3,7 +3,7 @@ import * as BigNumber from 'bignumber.js'; import Web3 = require('web3'); import {SchemaValidator} from './schema_validator'; -const HEX_REGEX = /^0x([0-9A-F]{2})*$/i; +const HEX_REGEX = /^0x[0-9A-F]*$/i; export const assert = { isBigNumber(variableName: string, value: BigNumber.BigNumber) { diff --git a/test/0x.js.ts b/test/0x.js.ts index 2ac6a2bea..38c36d248 100644 --- a/test/0x.js.ts +++ b/test/0x.js.ts @@ -52,7 +52,7 @@ describe('ZeroEx library', () => { }); }); it('should return false if the data doesn\'t pertain to the signature & address', () => { - const isValid = ZeroEx.isValidSignature('0x00', signature, address); + const isValid = ZeroEx.isValidSignature('0x0', signature, address); expect(isValid).to.be.false; }); it('should return false if the address doesn\'t pertain to the signature & data', () => { @@ -83,6 +83,10 @@ describe('ZeroEx library', () => { }); }); 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; -- cgit v1.2.3 From 00b64014525b4aabd82a61c1330a3f126554016a Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Thu, 25 May 2017 13:47:04 +0200 Subject: Fix braces --- src/ts/0x.js.ts | 1 + test/0x.js.ts | 2 ++ 2 files changed, 3 insertions(+) diff --git a/src/ts/0x.js.ts b/src/ts/0x.js.ts index cc2adca33..5e500b481 100644 --- a/src/ts/0x.js.ts +++ b/src/ts/0x.js.ts @@ -56,6 +56,7 @@ export class ZeroEx { assert.isString('orderHash', orderHash); const isValid = /^0x[0-9A-F]{66}$/i.test(orderHash); return isValid; + } /* * 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 diff --git a/test/0x.js.ts b/test/0x.js.ts index 01a5554ff..a913fd6b5 100644 --- a/test/0x.js.ts +++ b/test/0x.js.ts @@ -99,6 +99,8 @@ describe('ZeroEx library', () => { it('returns true if order hash is correct', () => { const isValid = ZeroEx.isValidOrderHash('0x' + Array(65).join('0')); expect(isValid).to.be.true; + }); + }); describe('#toUnitAmount', () => { it('Should return the expected unit amount for the decimals passed in', () => { const baseUnitAmount = new BigNumber(1000000000); -- cgit v1.2.3 From 3fe582d94c587cf3c38b6c42988890dcd6d54659 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Thu, 25 May 2017 13:50:05 +0200 Subject: Fix tests --- src/ts/0x.js.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ts/0x.js.ts b/src/ts/0x.js.ts index 5e500b481..f52750eb6 100644 --- a/src/ts/0x.js.ts +++ b/src/ts/0x.js.ts @@ -54,7 +54,7 @@ export class ZeroEx { /** Checks if order hash is valid */ public static isValidOrderHash(orderHash: string): boolean { assert.isString('orderHash', orderHash); - const isValid = /^0x[0-9A-F]{66}$/i.test(orderHash); + const isValid = /^0x[0-9A-F]{64}$/i.test(orderHash); return isValid; } /* -- cgit v1.2.3 From 612019f5e7c75210ecdb3d81f4337a5bf16b45e5 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Thu, 25 May 2017 13:53:54 +0200 Subject: Fix comment --- src/ts/0x.js.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ts/0x.js.ts b/src/ts/0x.js.ts index f52750eb6..c0aaa06fa 100644 --- a/src/ts/0x.js.ts +++ b/src/ts/0x.js.ts @@ -18,7 +18,7 @@ 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. + * by signing `data` with the private key corresponding to the `signerETHAddressHex` address. */ public static isValidSignature(dataHex: string, signature: ECSignature, signerETHAddressHex: string): boolean { assert.isHexString('dataHex', dataHex); -- cgit v1.2.3 From f3cfd3e708608cc88f61fcb9c6c8b56fcfa9d030 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Thu, 25 May 2017 13:54:56 +0200 Subject: Get rid of ETH --- src/ts/0x.js.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/ts/0x.js.ts b/src/ts/0x.js.ts index c0aaa06fa..ead1f56df 100644 --- a/src/ts/0x.js.ts +++ b/src/ts/0x.js.ts @@ -18,12 +18,12 @@ 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 `signerETHAddressHex` address. + * by signing `data` with the private key corresponding to the `signerAddressHex` address. */ - public static isValidSignature(dataHex: string, signature: ECSignature, signerETHAddressHex: string): boolean { + public static isValidSignature(dataHex: string, signature: ECSignature, signerAddressHex: string): boolean { assert.isHexString('dataHex', dataHex); assert.doesConformToSchema('signature', signature, ECSignatureSchema); - assert.isETHAddressHex('signerAddress', signerETHAddressHex); + assert.isETHAddressHex('signerAddressHex', signerAddressHex); const dataBuff = ethUtil.toBuffer(dataHex); const msgHashBuff = ethUtil.hashPersonalMessage(dataBuff); @@ -33,7 +33,7 @@ export class ZeroEx { ethUtil.toBuffer(signature.r), ethUtil.toBuffer(signature.s)); const retrievedAddress = ethUtil.bufferToHex(ethUtil.pubToAddress(pubKey)); - return retrievedAddress === signerETHAddressHex; + return retrievedAddress === signerAddressHex; } catch (err) { return false; } -- cgit v1.2.3