diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/ts/0x.js.ts | 12 | ||||
-rw-r--r-- | src/ts/globals.d.ts | 2 | ||||
-rw-r--r-- | src/ts/utils/assert.ts | 6 |
3 files changed, 16 insertions, 4 deletions
diff --git a/src/ts/0x.js.ts b/src/ts/0x.js.ts index 1cc2d8b44..d189b48b1 100644 --- a/src/ts/0x.js.ts +++ b/src/ts/0x.js.ts @@ -13,18 +13,19 @@ 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 { - assert.isString('data', data); + public static isValidSignature(dataHex: string, signature: ECSignature, signer: ETHAddressHex): boolean { + assert.isHexString('dataHex', dataHex); assert.doesConformToSchema('signature', signature, ECSignatureSchema); 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, @@ -50,4 +51,9 @@ export class ZeroEx { const salt = randomNumber.times(factor).round(); return salt; } + /** Checks if order hash is valid */ + 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 659638a1e..58efe136a 100644 --- a/src/ts/globals.d.ts +++ b/src/ts/globals.d.ts @@ -7,7 +7,7 @@ declare interface Schema { } declare module 'ethereumjs-util' { - const toBuffer: (data: string) => Buffer; + 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; diff --git a/src/ts/utils/assert.ts b/src/ts/utils/assert.ts index 972047118..58182dac0 100644 --- a/src/ts/utils/assert.ts +++ b/src/ts/utils/assert.ts @@ -3,6 +3,8 @@ import * as BigNumber from 'bignumber.js'; import Web3 = require('web3'); import {SchemaValidator} from './schema_validator'; +const HEX_REGEX = /^0x([0-9A-F]{2})*$/i; + export const assert = { isBigNumber(variableName: string, value: BigNumber.BigNumber) { const isBigNumber = _.isObject(value) && value.isBigNumber; @@ -11,6 +13,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)); |