aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--package.json2
-rw-r--r--src/ts/0x.js.ts16
-rw-r--r--src/ts/globals.d.ts11
-rw-r--r--src/ts/utils/assert.ts8
-rw-r--r--test/0x.js.ts14
5 files changed, 36 insertions, 15 deletions
diff --git a/package.json b/package.json
index 63ad8d61d..557d6eeff 100644
--- a/package.json
+++ b/package.json
@@ -33,9 +33,9 @@
"devDependencies": {
"@types/bignumber.js": "^4.0.2",
"@types/chai": "^3.5.2",
- "@types/jsonschema": "^1.1.1",
"@types/mocha": "^2.2.41",
"@types/node": "^7.0.22",
+ "@types/lodash": "^4.14.64",
"awesome-typescript-loader": "^3.1.3",
"bignumber.js": "^4.0.2",
"chai": "^3.5.0",
diff --git a/src/ts/0x.js.ts b/src/ts/0x.js.ts
index b71c691d5..cc2adca33 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';
@@ -19,12 +20,12 @@ 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, signerETHAddressHex: string): boolean {
+ assert.isHexString('dataHex', dataHex);
assert.doesConformToSchema('signature', signature, ECSignatureSchema);
- assert.isETHAddressHex('signer', signer);
+ assert.isETHAddressHex('signerAddress', signerETHAddressHex);
- 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 === signerETHAddressHex;
} catch (err) {
return false;
}
@@ -50,6 +51,11 @@ 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]{66}$/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));
},
diff --git a/test/0x.js.ts b/test/0x.js.ts
index 46e5c4da5..01a5554ff 100644
--- a/test/0x.js.ts
+++ b/test/0x.js.ts
@@ -57,7 +57,7 @@ describe('ZeroEx library', () => {
});
});
it('should return false if the data doesn\'t pertain to the signature & address', () => {
- const isValid = ZeroEx.isValidSignature('wrong data', signature, address);
+ const isValid = ZeroEx.isValidSignature('0x0', signature, address);
expect(isValid).to.be.false;
});
it('should return false if the address doesn\'t pertain to the signature & data', () => {
@@ -87,6 +87,18 @@ describe('ZeroEx library', () => {
expect(salt.lessThan(twoPow256)).to.be.true;
});
});
+ describe('#isValidOrderHash', () => {
+ it('returns false if the value is not a hex string', () => {
+ const isValid = ZeroEx.isValidOrderHash('not a hex');
+ expect(isValid).to.be.false;
+ });
+ 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;
describe('#toUnitAmount', () => {
it('Should return the expected unit amount for the decimals passed in', () => {
const baseUnitAmount = new BigNumber(1000000000);