From 03291f9f8919943335b6dc1211423dc0d8fe5d6d Mon Sep 17 00:00:00 2001 From: "Ryan X. Charles" Date: Tue, 16 Sep 2014 11:54:38 -0700 Subject: [PATCH] add ECDSA example --- examples/ecdsa.js | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 examples/ecdsa.js diff --git a/examples/ecdsa.js b/examples/ecdsa.js new file mode 100644 index 0000000..c0bc155 --- /dev/null +++ b/examples/ecdsa.js @@ -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)); +