ECDSA Message Signature

The following is an example of generating a keypair, signing a message and verifying the signature.

The message is first hashed and hex encoded. The signature is then generated from the hash and the keypair.

A new keypair is built using only the public key from the previous signing key. The public key and the hash of the message are used to verify the signature.

var CryptoJS = require('crypto-js')  
var elliptic = require('elliptic')

const ec = new'secp256k1')

const msg = "Super secret message"

// generate a key
const keypair = ec.genKeyPair()

// hash the message
var hash = CryptoJS.SHA3(msg, {outputLength: 256}).toString(CryptoJS.enc.Hex)

// generate a signature from the keypair and the hash of the messsage
var sig = keypair.sign(hash)


// generate a keypair from only the public portion of the above key
var pub = keypair.getPublic().encode('hex')  
var key = ec.keyFromPublic(pub, 'hex')

// verify the message signature
console.log('verification: ' + key.verify(hash, sig))  
Super secret message  
Signature {  
  r: <BN: a64acac0b33ee6e84f3413439c76f4873d9d64e2c594ba302585de484e9db59b>,
  s: <BN: 83493330fcdddfb801f22e228e0fd8a90966fa8e53004d0b0cd1ffce68d3deb>,
  recoveryParam: 0 }
verification: true