Port isValidOrderHash and tests
This commit is contained in:
parent
334d2f175f
commit
5be5debdf1
@ -12,13 +12,14 @@ export interface ECSignature {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const MAX_DIGITS_IN_UNSIGNED_256_INT = 78;
|
const MAX_DIGITS_IN_UNSIGNED_256_INT = 78;
|
||||||
|
const ORDER_HASH_LENGTH = 66;
|
||||||
|
|
||||||
export class ZeroEx {
|
export class ZeroEx {
|
||||||
/**
|
/**
|
||||||
* Verifies that the elliptic curve signature `signature` was generated
|
* 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 `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.isString('data', data);
|
||||||
assert.isObject('signature', signature);
|
assert.isObject('signature', signature);
|
||||||
assert.isETHAddressHex('signer', signer);
|
assert.isETHAddressHex('signer', signer);
|
||||||
@ -49,4 +50,9 @@ export class ZeroEx {
|
|||||||
const salt = randomNumber.times(factor).round();
|
const salt = randomNumber.times(factor).round();
|
||||||
return salt;
|
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
5
src/ts/globals.d.ts
vendored
@ -1,11 +1,12 @@
|
|||||||
declare type ETHPublicKey = string;
|
declare type ETHPublicKey = string;
|
||||||
declare type ETHAddressHex = string;
|
declare type ETHAddressHex = string;
|
||||||
|
declare type HexString = string;
|
||||||
declare type ETHAddressBuff = Buffer;
|
declare type ETHAddressBuff = Buffer;
|
||||||
|
|
||||||
declare module 'ethereumjs-util' {
|
declare module 'ethereumjs-util' {
|
||||||
const toBuffer: (data: string) => Buffer;
|
const toBuffer: (data: HexString) => Buffer;
|
||||||
const hashPersonalMessage: (msg: Buffer) => 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 ecrecover: (msgHashBuff: Buffer, v: number, r: Buffer, s: Buffer) => ETHPublicKey;
|
||||||
const pubToAddress: (pubKey: ETHPublicKey) => ETHAddressBuff;
|
const pubToAddress: (pubKey: ETHPublicKey) => ETHAddressBuff;
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,8 @@ import * as _ from 'lodash';
|
|||||||
import * as BigNumber from 'bignumber.js';
|
import * as BigNumber from 'bignumber.js';
|
||||||
import Web3 = require('web3');
|
import Web3 = require('web3');
|
||||||
|
|
||||||
|
const HEX_REGEX = /^0x([0-9A-F]{2})*$/i;
|
||||||
|
|
||||||
export const assert = {
|
export const assert = {
|
||||||
isBigNumber(variableName: string, value: BigNumber.BigNumber) {
|
isBigNumber(variableName: string, value: BigNumber.BigNumber) {
|
||||||
const isBigNumber = _.isObject(value) && value.isBigNumber;
|
const isBigNumber = _.isObject(value) && value.isBigNumber;
|
||||||
@ -10,6 +12,10 @@ export const assert = {
|
|||||||
isString(variableName: string, value: string) {
|
isString(variableName: string, value: string) {
|
||||||
this.assert(_.isString(value), this.typeAssertionMessage(variableName, 'string', value));
|
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) {
|
isETHAddressHex(variableName: string, value: ETHAddressHex) {
|
||||||
const web3 = new Web3();
|
const web3 = new Web3();
|
||||||
this.assert(web3.isAddress(value), this.typeAssertionMessage(variableName, 'ETHAddressHex', value));
|
this.assert(web3.isAddress(value), this.typeAssertionMessage(variableName, 'ETHAddressHex', value));
|
||||||
|
@ -86,4 +86,14 @@ describe('ZeroEx library', () => {
|
|||||||
expect(salt.lessThan(twoPow256)).to.be.true;
|
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;
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user