Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@WietseWind
Created February 18, 2019 14:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save WietseWind/c774fc0b14a78dd323f761162134ef1d to your computer and use it in GitHub Desktop.
Save WietseWind/c774fc0b14a78dd323f761162134ef1d to your computer and use it in GitHub Desktop.
secp256k1 Sign & Verify (Sample: https://runkit.com/wietsewind/5c6abbb0549f4e0012a665d5)
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))
}
}
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}
`)
@WietseWind
Copy link
Author

Sample output:

Account:                 rUwHCiUJJwxYU9Bjc8EmHCRnvh4rJgHN2A
Message Hash:            D65CC5390FAD02D43CD64F931EA47E06D7EFA1A5FD0DB32CB719FAE67CBBD525
Signature:               B1366626C0FED1A5169D96A4C9975A4E416B3B1CB236E972CD2BCCA033F38E76BB0882E02BF041015AC36C2A2C1B570A728BDB251528864544FF68395667D8760
Account from Signature:  rUwHCiUJJwxYU9Bjc8EmHCRnvh4rJgHN2A
Signature Valid for Msg: true

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment