-
-
Save josejaguirre/39bea9daeec250c528f316548f120e62 to your computer and use it in GitHub Desktop.
ECDSA in JavaScript: secp256k1-based sign / verify / recoverPubKey
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(); | |
let keyPair = ec.keyFromPrivate("97ddae0f3a25b92268175400149d65d6887b9cefaf28ea2c078e05cdc15a3c0a"); | |
let privKey = keyPair.getPrivate("hex"); | |
let pubKey = keyPair.getPublic(); | |
console.log(`Private key: ${privKey}`); | |
console.log("Public key :", pubKey.encode("hex").substr(2)); | |
console.log("Public key (compressed):", | |
pubKey.encodeCompressed("hex")); | |
console.log(); | |
let msg = 'Message for signing'; | |
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); | |
console.log(); | |
let hexToDecimal = (x) => ec.keyFromPrivate(x, "hex").getPrivate().toString(10); | |
let pubKeyRecovered = ec.recoverPubKey( | |
hexToDecimal(msgHash), signature, signature.recoveryParam, "hex"); | |
console.log("Recovered pubKey:", pubKeyRecovered.encodeCompressed("hex")); | |
let validSig = ec.verify(msgHash, signature, pubKeyRecovered); | |
console.log("Signature valid?", validSig); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment