From b25b6c598692586cc620cafa9cf76b4d955efa9d Mon Sep 17 00:00:00 2001 From: AbigailDeng Date: Fri, 29 Dec 2023 10:17:57 +0800 Subject: [PATCH] feat: add verify --- README.md | 2 +- package.json | 23 ++++++++++++----------- src/wallet/index.js | 14 ++++++++++++++ test/unit/wallet/index.test.js | 11 +++++++++++ yarn.lock | 5 +++++ 5 files changed, 43 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 116ad437..ffaf465e 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/package.json b/package.json index e2942a1e..d755e5e9 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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" @@ -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" } } diff --git a/src/wallet/index.js b/src/wallet/index.js index 510746a6..2bfc5f46 100644 --- a/src/wallet/index.js +++ b/src/wallet/index.js @@ -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 { @@ -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, diff --git a/test/unit/wallet/index.test.js b/test/unit/wallet/index.test.js index 7de9239a..68d48fd5 100644 --- a/test/unit/wallet/index.test.js +++ b/test/unit/wallet/index.test.js @@ -90,4 +90,15 @@ describe('test wallet', () => { const _getWallet = Wallet.__GetDependency__('_getWallet'); expect(() => _getWallet()).toThrow('not a valid method'); }); + + test('test verify', () => { + const signature = + 'f6c8eb063cb44037d42e39803d6cd2f348895f6a27a0cc353ef6ea3c9275f5ca36b77981b8c7b25b093c5a46e5f7575f1afa11018db0a0dba2eaaa00779f581601'; + const msgHash = + 'aa3ec16e6acc809d8b2818662276256abfd2f1b441cb51574933f3d4bd115d11'; + const pubKey = + '0409fdbe79db858522b456327f9bb0035ca5084097efce46ca363583776bbe86c8542f77cf3a3814f536de743b7d5eb30738b9c8df05c791a2faee749945d4550d'; + const isValid = Wallet.verify(signature.toString('hex'), msgHash, pubKey); + expect(isValid).toBe(true); + }); }); diff --git a/yarn.lock b/yarn.lock index 064a2425..f5bc6f7b 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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== + body-parser@1.20.1: version "1.20.1" resolved "https://registry.npmjs.org/body-parser/-/body-parser-1.20.1.tgz#b1812a8912c195cd371a3ee5e66faa2338a5c668"