From 8311203f73bf6eff8bcac8d1fb72f9cf8b65c45b Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Thu, 8 Jun 2017 12:14:35 +0200 Subject: Refactor isValidSignature --- src/0x.js.ts | 14 +------------- src/contract_wrappers/exchange_wrapper.ts | 4 ++-- src/utils/utils.ts | 16 ++++++++++++++++ 3 files changed, 19 insertions(+), 15 deletions(-) (limited to 'src') diff --git a/src/0x.js.ts b/src/0x.js.ts index 8f1178b2a..f275d9fbd 100644 --- a/src/0x.js.ts +++ b/src/0x.js.ts @@ -40,19 +40,7 @@ export class ZeroEx { assert.doesConformToSchema('signature', signature, ecSignatureSchema); assert.isETHAddressHex('signerAddressHex', signerAddressHex); - const dataBuff = ethUtil.toBuffer(dataHex); - const msgHashBuff = ethUtil.hashPersonalMessage(dataBuff); - try { - const pubKey = ethUtil.ecrecover( - msgHashBuff, - signature.v, - ethUtil.toBuffer(signature.r), - ethUtil.toBuffer(signature.s)); - const retrievedAddress = ethUtil.bufferToHex(ethUtil.pubToAddress(pubKey)); - return retrievedAddress === signerAddressHex; - } catch (err) { - return false; - } + return utils.isValidSignature(dataHex, signature, signerAddressHex); } /** * Generates pseudo-random 256 bit salt. diff --git a/src/contract_wrappers/exchange_wrapper.ts b/src/contract_wrappers/exchange_wrapper.ts index d144d8aad..5b5d1e914 100644 --- a/src/contract_wrappers/exchange_wrapper.ts +++ b/src/contract_wrappers/exchange_wrapper.ts @@ -70,8 +70,8 @@ export class ExchangeWrapper extends ContractWrapper { await this.stopWatchingExchangeLogEventsAsync(); delete this.exchangeContractIfExists; } - public async isValidSignatureAsync(dataHex: string, ecSignature: ECSignature, - signerAddressHex: string): Promise { + private async isValidSignatureUsingContractCallAsync(dataHex: string, ecSignature: ECSignature, + signerAddressHex: string): Promise { assert.isHexString('dataHex', dataHex); assert.doesConformToSchema('ecSignature', ecSignature, ecSignatureSchema); assert.isETHAddressHex('signerAddressHex', signerAddressHex); diff --git a/src/utils/utils.ts b/src/utils/utils.ts index 5786bab07..ea3d8c7e1 100644 --- a/src/utils/utils.ts +++ b/src/utils/utils.ts @@ -4,6 +4,7 @@ import * as ethABI from 'ethereumjs-abi'; import * as ethUtil from 'ethereumjs-util'; import {Order, SignedOrder, SolidityTypes} from '../types'; import * as BigNumber from 'bignumber.js'; +import {ECSignature} from '../types'; export const utils = { /** @@ -50,6 +51,21 @@ export const utils = { const hashHex = ethUtil.bufferToHex(hashBuff); return hashHex; }, + isValidSignature(dataHex: string, signature: ECSignature, signerAddressHex: string): boolean { + const dataBuff = ethUtil.toBuffer(dataHex); + const msgHashBuff = ethUtil.hashPersonalMessage(dataBuff); + try { + const pubKey = ethUtil.ecrecover( + msgHashBuff, + signature.v, + ethUtil.toBuffer(signature.r), + ethUtil.toBuffer(signature.s)); + const retrievedAddress = ethUtil.bufferToHex(ethUtil.pubToAddress(pubKey)); + return retrievedAddress === signerAddressHex; + } catch (err) { + return false; + } + }, getCurrentUnixTimestamp(): BigNumber.BigNumber { return new BigNumber(Date.now() / 1000); }, -- cgit From a328f0c8053173fd8e1f75b808228c3edb617bcc Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Thu, 8 Jun 2017 12:19:02 +0200 Subject: Move isValidSignature back from utils --- src/0x.js.ts | 14 +++++++++++++- src/utils/utils.ts | 15 --------------- 2 files changed, 13 insertions(+), 16 deletions(-) (limited to 'src') diff --git a/src/0x.js.ts b/src/0x.js.ts index f275d9fbd..8f1178b2a 100644 --- a/src/0x.js.ts +++ b/src/0x.js.ts @@ -40,7 +40,19 @@ export class ZeroEx { assert.doesConformToSchema('signature', signature, ecSignatureSchema); assert.isETHAddressHex('signerAddressHex', signerAddressHex); - return utils.isValidSignature(dataHex, signature, signerAddressHex); + const dataBuff = ethUtil.toBuffer(dataHex); + const msgHashBuff = ethUtil.hashPersonalMessage(dataBuff); + try { + const pubKey = ethUtil.ecrecover( + msgHashBuff, + signature.v, + ethUtil.toBuffer(signature.r), + ethUtil.toBuffer(signature.s)); + const retrievedAddress = ethUtil.bufferToHex(ethUtil.pubToAddress(pubKey)); + return retrievedAddress === signerAddressHex; + } catch (err) { + return false; + } } /** * Generates pseudo-random 256 bit salt. diff --git a/src/utils/utils.ts b/src/utils/utils.ts index ea3d8c7e1..ded2d31fd 100644 --- a/src/utils/utils.ts +++ b/src/utils/utils.ts @@ -51,21 +51,6 @@ export const utils = { const hashHex = ethUtil.bufferToHex(hashBuff); return hashHex; }, - isValidSignature(dataHex: string, signature: ECSignature, signerAddressHex: string): boolean { - const dataBuff = ethUtil.toBuffer(dataHex); - const msgHashBuff = ethUtil.hashPersonalMessage(dataBuff); - try { - const pubKey = ethUtil.ecrecover( - msgHashBuff, - signature.v, - ethUtil.toBuffer(signature.r), - ethUtil.toBuffer(signature.s)); - const retrievedAddress = ethUtil.bufferToHex(ethUtil.pubToAddress(pubKey)); - return retrievedAddress === signerAddressHex; - } catch (err) { - return false; - } - }, getCurrentUnixTimestamp(): BigNumber.BigNumber { return new BigNumber(Date.now() / 1000); }, -- cgit From ea4cf16eae4bab31c5da522bb582a5c83af53705 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Thu, 8 Jun 2017 17:02:18 +0200 Subject: Remove unused import --- src/utils/utils.ts | 1 - 1 file changed, 1 deletion(-) (limited to 'src') diff --git a/src/utils/utils.ts b/src/utils/utils.ts index ded2d31fd..5786bab07 100644 --- a/src/utils/utils.ts +++ b/src/utils/utils.ts @@ -4,7 +4,6 @@ import * as ethABI from 'ethereumjs-abi'; import * as ethUtil from 'ethereumjs-util'; import {Order, SignedOrder, SolidityTypes} from '../types'; import * as BigNumber from 'bignumber.js'; -import {ECSignature} from '../types'; export const utils = { /** -- cgit