Port isValidOrderHash and tests

This commit is contained in:
Leonid Logvinov 2017-05-25 12:08:54 +02:00
parent 334d2f175f
commit 5be5debdf1
No known key found for this signature in database
GPG Key ID: 0DD294BFDE8C95D4
4 changed files with 26 additions and 3 deletions

View File

@ -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;
}
}

5
src/ts/globals.d.ts vendored
View File

@ -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;
}

View File

@ -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));

View File

@ -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;
});
});
});