Created
February 18, 2019 14:09
-
-
Save WietseWind/c774fc0b14a78dd323f761162134ef1d to your computer and use it in GitHub Desktop.
secp256k1 Sign & Verify (Sample: https://runkit.com/wietsewind/5c6abbb0549f4e0012a665d5)
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
const elliptic = require('elliptic') | |
const secp256k1 = elliptic.ec('secp256k1') | |
const hash = require('hash.js') | |
const keypairs = require('ripple-keypairs') | |
const hexToDecimal = (x) => secp256k1.keyFromPrivate(x, 'hex').getPrivate().toString(10) | |
module.exports = { | |
sign (message, familySeed) { | |
const digest = hash.sha256().update(message).digest('hex').toUpperCase() | |
const keypair = keypairs.deriveKeypair(familySeed) | |
const signature = secp256k1.sign(digest, keypair.privateKey.substr(2)) | |
return { | |
hash: digest, | |
signature: (signature.r.toJSON() + signature.s.toJSON() + signature.recoveryParam.toString(16)).toUpperCase() | |
} | |
}, | |
unserializeSignature (signature) { | |
return { | |
r: signature.slice(0, 64).toLowerCase(), | |
s: signature.slice(64, 128).toLowerCase(), | |
recoveryParam: parseInt(signature.slice(128), 16) | |
} | |
}, | |
deriveAddress (familySeed) { | |
const keypair = keypairs.deriveKeypair(familySeed) | |
return keypairs.deriveAddress(keypair.publicKey) | |
}, | |
recoverPubKey (hash, signature) { | |
const unserializedSignature = this.unserializeSignature(signature) | |
const pubKeyRecovered = secp256k1.recoverPubKey(hexToDecimal(hash), unserializedSignature, unserializedSignature.recoveryParam, 'hex') | |
return pubKeyRecovered | |
}, | |
recoverAddress (hash, signature) { | |
return keypairs.deriveAddress(this.recoverPubKey(hash, signature).encodeCompressed('hex')) | |
}, | |
verify (message, signature) { | |
const digest = hash.sha256().update(message).digest('hex') | |
const unserializedSignature = this.unserializeSignature(signature) | |
return secp256k1.verify(digest, unserializedSignature, this.recoverPubKey(digest, signature)) | |
} | |
} |
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
const sampleLib = require('./') | |
const familySeed = 'spyWUG5bB6UPURokVDQsj2jceSwsD' | |
const message = 'This is me testing :)' | |
const account = sampleLib.deriveAddress(familySeed) | |
const signed = sampleLib.sign(message, familySeed) | |
const accountFromSignature = sampleLib.recoverAddress(signed.hash, signed.signature) | |
const verified = sampleLib.verify(message, signed.signature) | |
console.log(` | |
Account: ${account} | |
Message Hash: ${signed.hash} | |
Signature: ${signed.signature} | |
Account from Signature: ${accountFromSignature} | |
Signature Valid for Msg: ${verified} | |
`) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sample output: