Address feedback
This commit is contained in:
parent
74a80c28d3
commit
eee06e0cc9
@ -33,9 +33,9 @@
|
|||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/bignumber.js": "^4.0.2",
|
"@types/bignumber.js": "^4.0.2",
|
||||||
"@types/chai": "^3.5.2",
|
"@types/chai": "^3.5.2",
|
||||||
"@types/jsonschema": "^1.1.1",
|
|
||||||
"@types/mocha": "^2.2.41",
|
"@types/mocha": "^2.2.41",
|
||||||
"@types/node": "^7.0.22",
|
"@types/node": "^7.0.22",
|
||||||
|
"@types/lodash": "^4.14.64",
|
||||||
"awesome-typescript-loader": "^3.1.3",
|
"awesome-typescript-loader": "^3.1.3",
|
||||||
"bignumber.js": "^4.0.2",
|
"bignumber.js": "^4.0.2",
|
||||||
"chai": "^3.5.0",
|
"chai": "^3.5.0",
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
import * as BigNumber from 'bignumber.js';
|
import * as BigNumber from 'bignumber.js';
|
||||||
import * as ethUtil from 'ethereumjs-util';
|
import * as ethUtil from 'ethereumjs-util';
|
||||||
|
import * as _ from 'lodash';
|
||||||
import {assert} from './utils/assert';
|
import {assert} from './utils/assert';
|
||||||
import {ECSignatureSchema} from './schemas/ec_signature_schema';
|
import {ECSignatureSchema} from './schemas/ec_signature_schema';
|
||||||
|
|
||||||
@ -13,17 +14,16 @@ 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(dataHex: string, signature: ECSignature, signerAddress: string): boolean {
|
public static isValidSignature(dataHex: string, signature: ECSignature, signerETHAddressHex: string): boolean {
|
||||||
assert.isHexString('dataHex', dataHex);
|
assert.isHexString('dataHex', dataHex);
|
||||||
assert.doesConformToSchema('signature', signature, ECSignatureSchema);
|
assert.doesConformToSchema('signature', signature, ECSignatureSchema);
|
||||||
assert.isETHAddressHex('signerAddress', signerAddress);
|
assert.isETHAddressHex('signerAddress', signerETHAddressHex);
|
||||||
|
|
||||||
const dataBuff = ethUtil.toBuffer(dataHex);
|
const dataBuff = ethUtil.toBuffer(dataHex);
|
||||||
const msgHashBuff = ethUtil.hashPersonalMessage(dataBuff);
|
const msgHashBuff = ethUtil.hashPersonalMessage(dataBuff);
|
||||||
@ -33,7 +33,7 @@ export class ZeroEx {
|
|||||||
ethUtil.toBuffer(signature.r),
|
ethUtil.toBuffer(signature.r),
|
||||||
ethUtil.toBuffer(signature.s));
|
ethUtil.toBuffer(signature.s));
|
||||||
const retrievedAddress = ethUtil.bufferToHex(ethUtil.pubToAddress(pubKey));
|
const retrievedAddress = ethUtil.bufferToHex(ethUtil.pubToAddress(pubKey));
|
||||||
return retrievedAddress === signerAddress;
|
return retrievedAddress === signerETHAddressHex;
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -53,7 +53,8 @@ export class ZeroEx {
|
|||||||
}
|
}
|
||||||
/** Checks if order hash is valid */
|
/** Checks if order hash is valid */
|
||||||
public static isValidOrderHash(orderHash: string): boolean {
|
public static isValidOrderHash(orderHash: string): boolean {
|
||||||
assert.isHexString('orderHash', orderHash);
|
assert.isString('orderHash', orderHash);
|
||||||
return orderHash.length === ORDER_HASH_LENGTH;
|
const isValid = /^0x[0-9A-F]{66}$/i.test(orderHash);
|
||||||
|
return isValid;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,7 +3,7 @@ import * as BigNumber from 'bignumber.js';
|
|||||||
import Web3 = require('web3');
|
import Web3 = require('web3');
|
||||||
import {SchemaValidator} from './schema_validator';
|
import {SchemaValidator} from './schema_validator';
|
||||||
|
|
||||||
const HEX_REGEX = /^0x([0-9A-F]{2})*$/i;
|
const HEX_REGEX = /^0x[0-9A-F]*$/i;
|
||||||
|
|
||||||
export const assert = {
|
export const assert = {
|
||||||
isBigNumber(variableName: string, value: BigNumber.BigNumber) {
|
isBigNumber(variableName: string, value: BigNumber.BigNumber) {
|
||||||
|
@ -52,7 +52,7 @@ describe('ZeroEx library', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
it('should return false if the data doesn\'t pertain to the signature & address', () => {
|
it('should return false if the data doesn\'t pertain to the signature & address', () => {
|
||||||
const isValid = ZeroEx.isValidSignature('0x00', signature, address);
|
const isValid = ZeroEx.isValidSignature('0x0', signature, address);
|
||||||
expect(isValid).to.be.false;
|
expect(isValid).to.be.false;
|
||||||
});
|
});
|
||||||
it('should return false if the address doesn\'t pertain to the signature & data', () => {
|
it('should return false if the address doesn\'t pertain to the signature & data', () => {
|
||||||
@ -83,6 +83,10 @@ describe('ZeroEx library', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
describe('#isValidOrderHash', () => {
|
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', () => {
|
it('returns false if the length is wrong', () => {
|
||||||
const isValid = ZeroEx.isValidOrderHash('0xdeadbeef');
|
const isValid = ZeroEx.isValidOrderHash('0xdeadbeef');
|
||||||
expect(isValid).to.be.false;
|
expect(isValid).to.be.false;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user