Created
April 15, 2023 15:02
-
-
Save changwu-tw/5b969db5494d32eaaabada93465a3f14 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let elliptic = require('elliptic'); | |
let sha3 = require('js-sha3'); | |
let ec = new elliptic.ec('secp256k1'); | |
// let keyPair = ec.genKeyPair(); | |
// privkey: 0xbe2d433d23c65543f3841dd402d4af1ef97674cc656c7c3cf35b06e17bd22da9 | |
// pubKey: 0x0331632fb5e68cd4c81b9656babb420d630da2a629f97aaf3fa07f30df22866bac | |
// 0x4f3edf983ac636a65a842ce7c78d9aa706d3b113bce9c46f30d7d21715b23b1d | |
// 0x04e68acfc0253a10620dff706b0a1b1f1f5833ea3beb3bde2250d5f271f3563606672ebc45e0b7ea2e816ecb70ca03137b1c9476eec63d4632e990020b7b6fba39 | |
// 0x90F8bf6A479f320ead074411a4B0e7944Ea8c9C1 | |
let keyPair = ec.keyFromPrivate("4f3edf983ac636a65a842ce7c78d9aa706d3b113bce9c46f30d7d21715b23b1d"); | |
let privKey = keyPair.getPrivate("hex"); | |
let pubKey = keyPair.getPublic(); | |
console.log(`Private key: 0x${privKey}`); | |
console.log("Public key:", "0x"+pubKey.encodeCompressed("hex")); | |
console.log("Public key :", pubKey.encode("hex").substr(2)); | |
let msg = 'ETHTokyo'; | |
let msgHash = sha3.keccak256(msg); | |
let signature = ec.sign(msgHash, privKey, "hex", {canonical: true}); | |
console.log(`Msg: ${msg}`); | |
console.log(`Msg hash: ${msgHash}`); | |
console.log("signature: ", signature.r.toString() + signature.s.toString()); | |
let new_keypair = ec.keyFromPrivate(signature.r.toString() + signature.s.toString()); | |
let new_privKey = new_keypair.getPrivate("hex"); | |
console.log("new_prikey: ", new_privKey); | |
let new_pubKey = new_keypair.getPublic(); | |
console.log("Public key :", new_pubKey.encode("hex").substr(2)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment