diff options
author | Fabio Berger <me@fabioberger.com> | 2017-11-10 06:18:03 +0800 |
---|---|---|
committer | Fabio Berger <me@fabioberger.com> | 2017-11-10 06:18:03 +0800 |
commit | 62861d1e1315a14e955b30a45db05e7907b0d22d (patch) | |
tree | 86077be09b3a9ce41923c44fd57ecede1a94cc1d /src | |
parent | 0c8886ad0c0e1cffa7da0495a648028d1fe54476 (diff) | |
download | dexon-sol-tools-62861d1e1315a14e955b30a45db05e7907b0d22d.tar dexon-sol-tools-62861d1e1315a14e955b30a45db05e7907b0d22d.tar.gz dexon-sol-tools-62861d1e1315a14e955b30a45db05e7907b0d22d.tar.bz2 dexon-sol-tools-62861d1e1315a14e955b30a45db05e7907b0d22d.tar.lz dexon-sol-tools-62861d1e1315a14e955b30a45db05e7907b0d22d.tar.xz dexon-sol-tools-62861d1e1315a14e955b30a45db05e7907b0d22d.tar.zst dexon-sol-tools-62861d1e1315a14e955b30a45db05e7907b0d22d.zip |
Move isValidSignature implementation into signatureUtils
Diffstat (limited to 'src')
-rw-r--r-- | src/0x.ts | 15 | ||||
-rw-r--r-- | src/utils/signature_utils.ts | 15 |
2 files changed, 17 insertions, 13 deletions
@@ -88,19 +88,8 @@ export class ZeroEx { assert.doesConformToSchema('signature', signature, schemas.ecSignatureSchema); assert.isETHAddressHex('signerAddress', signerAddress); - const dataBuff = ethUtil.toBuffer(data); - 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 === signerAddress; - } catch (err) { - return false; - } + const isValidSignature = signatureUtils.isValidSignature(data, signature, signerAddress); + return isValidSignature; } /** * Generates a pseudo-random 256-bit salt. diff --git a/src/utils/signature_utils.ts b/src/utils/signature_utils.ts index b312b5554..d066f8bf0 100644 --- a/src/utils/signature_utils.ts +++ b/src/utils/signature_utils.ts @@ -2,6 +2,21 @@ import * as ethUtil from 'ethereumjs-util'; import {ECSignature} from '../types'; export const signatureUtils = { + isValidSignature(data: string, signature: ECSignature, signerAddress: string): boolean { + const dataBuff = ethUtil.toBuffer(data); + 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 === signerAddress; + } catch (err) { + return false; + } + }, parseSignatureHexAsVRS(signatureHex: string): ECSignature { const signatureBuffer = ethUtil.toBuffer(signatureHex); let v = signatureBuffer[0]; |