Skip to content

Commit

Permalink
Merge pull request #141 from AElfProject/feature/test-chain-util-verify
Browse files Browse the repository at this point in the history
feat: pubKey deprecatedParam
  • Loading branch information
hzz780 authored Feb 26, 2024
2 parents eb1f599 + 22abcf4 commit d3457c1
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 20 additions & 1 deletion src/wallet/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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 {
Expand Down
3 changes: 3 additions & 0 deletions test/unit/wallet/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});

0 comments on commit d3457c1

Please sign in to comment.