diff --git a/README.md b/README.md index e9025a31..31acbc87 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ | Statements | Branches | Functions | Lines | | --------------------------- | ----------------------- | ------------------------- | ----------------- | -| ![Statements](https://img.shields.io/badge/statements-96.21%25-brightgreen.svg?style=flat) | ![Branches](https://img.shields.io/badge/branches-93.59%25-brightgreen.svg?style=flat) | ![Functions](https://img.shields.io/badge/functions-96.65%25-brightgreen.svg?style=flat) | ![Lines](https://img.shields.io/badge/lines-96.37%25-brightgreen.svg?style=flat) | +| ![Statements](https://img.shields.io/badge/statements-96.23%25-brightgreen.svg?style=flat) | ![Branches](https://img.shields.io/badge/branches-93.62%25-brightgreen.svg?style=flat) | ![Functions](https://img.shields.io/badge/functions-96.66%25-brightgreen.svg?style=flat) | ![Lines](https://img.shields.io/badge/lines-96.39%25-brightgreen.svg?style=flat) | ## 1. Introduction diff --git a/src/wallet/index.js b/src/wallet/index.js index 2bfc5f46..8ce22064 100644 --- a/src/wallet/index.js +++ b/src/wallet/index.js @@ -253,6 +253,13 @@ const sign = (hexString, keyPair) => { return getSignature(bytesToBeSign, keyPair); }; +const hexToDecimal = x => ellipticEc.keyFromPrivate(x, 'hex').getPrivate().toString(10); + +/** + * @param {string} signature Signature + * @param {string} msgHash Message for signing + * @param {string} pubKey deprecatedParam - This parameter is deprecated. + */ const verify = (signature, msgHash, pubKey) => { const rHex = signature.substring(0, 64); const sHex = signature.substring(64, 128); @@ -262,7 +269,19 @@ const verify = (signature, msgHash, pubKey) => { s: new BN(sHex, 16), recoveryParam: recoveryParamHex.slice(1), }; - return ellipticEc.verify(msgHash, sigObj, Buffer.from(pubKey, 'hex')); + let publicKey; + if (!pubKey) { + const key = ellipticEc.recoverPubKey( + hexToDecimal(msgHash), + sigObj, + +sigObj.recoveryParam, + 'hex' + ); + publicKey = ellipticEc.keyFromPublic(key).getPublic('hex'); + } else { + publicKey = pubKey; + } + return ellipticEc.verify(msgHash, sigObj, Buffer.from(publicKey, 'hex')); }; export default { diff --git a/test/unit/wallet/index.test.js b/test/unit/wallet/index.test.js index 51062fa7..f47a8d9c 100644 --- a/test/unit/wallet/index.test.js +++ b/test/unit/wallet/index.test.js @@ -102,5 +102,8 @@ describe('test wallet', () => { 'b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9'; const isValid = Wallet.verify(signature.toString('hex'), msgHash, pubKey); expect(isValid).toBe(true); + + const isValidNoPubKey = Wallet.verify(signature.toString('hex'), msgHash); + expect(isValidNoPubKey).toBe(true); }); });