secp256k1 Sign & Verify signature

node v10.20.1
version: 1.0.0
endpointsharetweet
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)) } }
Using the lib:
const sampleLib = module.exports 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} `)
Loading…

no comments

    sign in to comment