diff options
author | Leonid Logvinov <logvinov.leon@gmail.com> | 2017-05-25 18:08:54 +0800 |
---|---|---|
committer | Leonid Logvinov <logvinov.leon@gmail.com> | 2017-05-25 18:08:54 +0800 |
commit | 5be5debdf1122d7f9767fa7e5cd23a23d91bf93c (patch) | |
tree | 66c7a8c97350b198d3cc12dbd2a968e9a5953d4e | |
parent | 334d2f175fd9dcb3f9e336250d21c7916a2671ae (diff) | |
download | dexon-sol-tools-5be5debdf1122d7f9767fa7e5cd23a23d91bf93c.tar dexon-sol-tools-5be5debdf1122d7f9767fa7e5cd23a23d91bf93c.tar.gz dexon-sol-tools-5be5debdf1122d7f9767fa7e5cd23a23d91bf93c.tar.bz2 dexon-sol-tools-5be5debdf1122d7f9767fa7e5cd23a23d91bf93c.tar.lz dexon-sol-tools-5be5debdf1122d7f9767fa7e5cd23a23d91bf93c.tar.xz dexon-sol-tools-5be5debdf1122d7f9767fa7e5cd23a23d91bf93c.tar.zst dexon-sol-tools-5be5debdf1122d7f9767fa7e5cd23a23d91bf93c.zip |
Port isValidOrderHash and tests
-rw-r--r-- | src/ts/0x.js.ts | 8 | ||||
-rw-r--r-- | src/ts/globals.d.ts | 5 | ||||
-rw-r--r-- | src/ts/utils/assert.ts | 6 | ||||
-rw-r--r-- | 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; + }); + }); }); |