Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add verify #136

Merged
merged 1 commit into from
Jan 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.18%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.63%25-brightgreen.svg?style=flat) | ![Lines](https://img.shields.io/badge/lines-96.35%25-brightgreen.svg?style=flat) |
| ![Statements](https://img.shields.io/badge/statements-96.96%25-brightgreen.svg?style=flat) | ![Branches](https://img.shields.io/badge/branches-94.27%25-brightgreen.svg?style=flat) | ![Functions](https://img.shields.io/badge/functions-97.12%25-brightgreen.svg?style=flat) | ![Lines](https://img.shields.io/badge/lines-97.16%25-brightgreen.svg?style=flat) |


## 1. Introduction
Expand Down
23 changes: 12 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,15 @@
"engineStrict": true,
"dependencies": {
"@aelfqueen/protobufjs": "^6.8.9",
"@aws-crypto/sha256-js": "^5.0.0",
"@babel/plugin-proposal-optional-chaining": "^7.21.0",
"@babel/runtime": "^7.4.5",
"@typescript-eslint/parser": "^5.47.1",
"assert": "^2.0.0",
"babel-plugin-rewire": "^1.2.0",
"bignumber.js": "^9.0.0",
"bip39": "3.0.4",
"bn.js": "^5.2.1",
"browserify-cipher": "^1.0.1",
"bs58": "^5.0.0",
"buffer": "^6.0.3",
Expand All @@ -73,9 +76,7 @@
"scryptsy": "^2.1.0",
"stream-browserify": "^3.0.0",
"typescript": "^5.0.4",
"xmlhttprequest": "^1.8.0",
"@aws-crypto/sha256-js": "^5.0.0",
"@babel/plugin-proposal-optional-chaining": "^7.21.0"
"xmlhttprequest": "^1.8.0"
},
"lint-staged": {
"*.js": "eslint"
Expand All @@ -99,36 +100,36 @@
"@babel/preset-env": "7.21.4",
"@types/elliptic": "^6.4.14",
"@types/hdkey": "^2.0.1",
"babel-eslint": "^10.0.2",
"babel-loader": "^8.0.6",
"bundle-analyzer": "^0.0.6",
"chalk": "^4.1.2",
"commitizen": "^4.3.0",
"core-js": "^3.30.1",
"cross-env": "^7.0.3",
"cz-conventional-changelog": "^3.0.2",
"eslint": "^8.39.0",
"eslint-config-airbnb-base": "^14.0.0",
"eslint-plugin-babel": "^5.3.1",
"eslint-plugin-import": "^2.27.5",
"file-loader": "^4.0.0",
"git-cz": "^4.9.0",
"glob": "^10.2.2",
"husky": "^8.0.0",
"istanbul-badges-readme": "^1.8.5",
"jest": "^29.5.0",
"jest-environment-jsdom": "^29.5.0",
"jest-environment-jsdom-fifteen": "^1.0.2",
"jest-github-reporter": "^1.1.1",
"lint-staged": "^13.2.1",
"rimraf": "^5.0.0",
"size-limit": "^8.1.2",
"standard-version": "^9.5.0",
"unused-files-webpack-plugin": "^3.4.0",
"webpack": "^5.80.0",
"webpack-bundle-analyzer": "^4.8.0",
"webpack-cli": "^5.0.2",
"webpack-deadcode-plugin": "^0.1.17",
"webpack-merge": "^5.8.0",
"babel-eslint": "^10.0.2",
"bundle-analyzer": "^0.0.6",
"cz-conventional-changelog": "^3.0.2",
"file-loader": "^4.0.0",
"jest-environment-jsdom-fifteen": "^1.0.2",
"size-limit": "^8.1.2",
"unused-files-webpack-plugin": "^3.4.0"
"webpack-merge": "^5.8.0"
}
}
14 changes: 14 additions & 0 deletions src/wallet/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import * as bip39 from 'bip39';
import hdkey from 'hdkey';
import AES from 'crypto-js/aes';
import encUTF8 from 'crypto-js/enc-utf8';
import BN from 'bn.js';
import sha256 from '../util/sha256';
import * as keyStore from '../util/keyStore';
import {
Expand Down Expand Up @@ -252,10 +253,23 @@ const sign = (hexString, keyPair) => {
return getSignature(bytesToBeSign, keyPair);
};

const verify = (signature, msgHash, pubKey) => {
const rHex = signature.substring(0, 64);
const sHex = signature.substring(64, 128);
const recoveryParamHex = signature.substring(128, 130);
const sigObj = {
r: new BN(rHex, 16),
s: new BN(sHex, 16),
recoveryParam: recoveryParamHex.slice(1),
};
return ellipticEc.verify(msgHash, sigObj, Buffer.from(pubKey, 'hex'));
};

export default {
hdkey,
bip39,
sign,
verify,
signTransaction,
createNewWallet,
getWalletByMnemonic,
Expand Down
11 changes: 11 additions & 0 deletions test/unit/wallet/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,15 @@ describe('test wallet', () => {
const _getWallet = Wallet.__GetDependency__('_getWallet');
expect(() => _getWallet()).toThrow('not a valid method');
});

test('test verify', () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sign and verify can use the same privatekey, pubkey and msg.

const signature =
'f6c8eb063cb44037d42e39803d6cd2f348895f6a27a0cc353ef6ea3c9275f5ca36b77981b8c7b25b093c5a46e5f7575f1afa11018db0a0dba2eaaa00779f581601';
const msgHash =
'aa3ec16e6acc809d8b2818662276256abfd2f1b441cb51574933f3d4bd115d11';
const pubKey =
'0409fdbe79db858522b456327f9bb0035ca5084097efce46ca363583776bbe86c8542f77cf3a3814f536de743b7d5eb30738b9c8df05c791a2faee749945d4550d';
const isValid = Wallet.verify(signature.toString('hex'), msgHash, pubKey);
expect(isValid).toBe(true);
});
});
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3130,6 +3130,11 @@ bn.js@^5.0.0, bn.js@^5.1.1:
resolved "https://registry.yarnpkg.com/bn.js/-/bn.js-5.2.1.tgz#0bc527a6a0d18d0aa8d5b0538ce4a77dccfa7b70"
integrity sha512-eXRvHzWyYPBuB4NBy0cmYQjGitUrtqwbvlzP3G6VFnNRbsZQIxQ10PbKKHt8gZ/HW/D/747aDl+QkDqg3KQLMQ==

bn.js@^5.2.1:
version "5.2.1"
resolved "https://registry.npmjs.org/bn.js/-/bn.js-5.2.1.tgz#0bc527a6a0d18d0aa8d5b0538ce4a77dccfa7b70"
integrity sha512-eXRvHzWyYPBuB4NBy0cmYQjGitUrtqwbvlzP3G6VFnNRbsZQIxQ10PbKKHt8gZ/HW/D/747aDl+QkDqg3KQLMQ==

[email protected]:
version "1.20.1"
resolved "https://registry.npmjs.org/body-parser/-/body-parser-1.20.1.tgz#b1812a8912c195cd371a3ee5e66faa2338a5c668"
Expand Down