add ECDSA example

This commit is contained in:
Ryan X. Charles 2014-09-16 11:54:38 -07:00
parent 6c42969d01
commit 03291f9f89
1 changed files with 21 additions and 0 deletions

21
examples/ecdsa.js Normal file
View File

@ -0,0 +1,21 @@
var ECDSA = require('../lib/ecdsa');
var Keypair = require('../lib/keypair');
var Hash = require('../lib/hash');
//ECDSA is the signature algorithm used in bitcoin
//start with a keypair that you will use for signing
var keypair = Keypair().fromRandom();
//a message to be signed (normally you would have the hash of a transaction)
var messagebuf = new Buffer('This is a message I would like to sign');
//calculate a 32 byte hash for use in ECDSA. one way to do that is sha256.
var hashbuf = Hash.sha256(messagebuf);
var sig = ECDSA.sign(hashbuf, keypair);
//Anyone with the public key can verify
var pubkey = keypair.pubkey;
console.log('Valid signature? ' + ECDSA.verify(hashbuf, sig, pubkey));