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 ++++++ 3 files changed, 16 insertions(+), 3 deletions(-) (limited to 'src') 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)); -- cgit 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(-) (limited to 'src') 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 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(-) (limited to 'src') 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 From eee06e0cc97333891a84aff22196849105846eb4 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Thu, 25 May 2017 13:42:53 +0200 Subject: Address feedback --- src/ts/0x.js.ts | 13 +++++++------ src/ts/utils/assert.ts | 2 +- 2 files changed, 8 insertions(+), 7 deletions(-) (limited to 'src') 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) { -- cgit 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 + 1 file changed, 1 insertion(+) (limited to 'src') 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 -- cgit 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(-) (limited to 'src') 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 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(-) (limited to 'src') 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 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(-) (limited to 'src') 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