diff options
author | Fabio Berger <me@fabioberger.com> | 2017-05-25 19:57:40 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-05-25 19:57:40 +0800 |
commit | f7b8378a6eb4b4c6c3461ff677723869c67a4753 (patch) | |
tree | acd0984b53d3e47c094784e39cd24cbdd75b2b04 /src | |
parent | dfb7b039f15d572ddf2e417f71ecb069114100a8 (diff) | |
parent | f3cfd3e708608cc88f61fcb9c6c8b56fcfa9d030 (diff) | |
download | dexon-0x-contracts-f7b8378a6eb4b4c6c3461ff677723869c67a4753.tar.gz dexon-0x-contracts-f7b8378a6eb4b4c6c3461ff677723869c67a4753.tar.zst dexon-0x-contracts-f7b8378a6eb4b4c6c3461ff677723869c67a4753.zip |
Merge pull request #11 from 0xProject/isValidOrderHash
Port isValidOrderHash and tests
Diffstat (limited to 'src')
-rw-r--r-- | src/ts/0x.js.ts | 19 | ||||
-rw-r--r-- | src/ts/globals.d.ts | 11 | ||||
-rw-r--r-- | src/ts/utils/assert.ts | 8 |
3 files changed, 24 insertions, 14 deletions
diff --git a/src/ts/0x.js.ts b/src/ts/0x.js.ts index b71c691d5..ead1f56df 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'; @@ -17,14 +18,14 @@ 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 `signerAddressHex` address. */ - public static isValidSignature(data: string, signature: ECSignature, signer: ETHAddressHex): boolean { - assert.isString('data', data); + public static isValidSignature(dataHex: string, signature: ECSignature, signerAddressHex: string): boolean { + assert.isHexString('dataHex', dataHex); 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); try { const pubKey = ethUtil.ecrecover(msgHashBuff, @@ -32,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 === signerAddressHex; } catch (err) { return false; } @@ -50,6 +51,12 @@ export class ZeroEx { const salt = randomNumber.times(factor).round(); 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). * E.g: If a currency has 18 decimal places, 1e18 or one quintillion of the currency is equivalent diff --git a/src/ts/globals.d.ts b/src/ts/globals.d.ts index 974b4b26d..796812c87 100644 --- a/src/ts/globals.d.ts +++ b/src/ts/globals.d.ts @@ -1,9 +1,5 @@ declare module 'chai-bignumber'; -declare type ETHPublicKey = string; -declare type ETHAddressHex = string; -declare type ETHAddressBuff = Buffer; - declare interface Schema { id: string; } @@ -20,9 +16,10 @@ declare namespace Chai { /* tslint:enable */ 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; - const pubToAddress: (pubKey: ETHPublicKey) => ETHAddressBuff; + const ecrecover: (msgHashBuff: Buffer, v: number, r: Buffer, s: Buffer) => string; + const pubToAddress: (pubKey: string) => Buffer; + const isValidAddress: (address: string) => boolean; } diff --git a/src/ts/utils/assert.ts b/src/ts/utils/assert.ts index 972047118..2f52c6a3b 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]*$/i; + export const assert = { isBigNumber(variableName: string, value: BigNumber.BigNumber) { const isBigNumber = _.isObject(value) && value.isBigNumber; @@ -11,7 +13,11 @@ export const assert = { isString(variableName: string, value: string) { 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(); this.assert(web3.isAddress(value), this.typeAssertionMessage(variableName, 'ETHAddressHex', value)); }, |