Last active
August 29, 2015 14:05
-
-
Save glorat/2179505f380b9984cb4f to your computer and use it in GitHub Desktop.
Brainwallet bitcoinsig with bitcoinjs-lib 1.0.3 - And a bug
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Created by Kevin Tam on 08/08/2014. | |
*/ | |
var Base58 = require('bs58'); | |
var Crypto = require('crypto'); | |
var BigInteger = require('bigi'); | |
var assert = require('assert'); | |
var Bitcoin = require('../../') | |
function msg_numToVarInt(i) { | |
if (i < 0xfd) { | |
return [i]; | |
} else if (i <= 0xffff) { | |
// can't use numToVarInt from bitcoinjs, BitcoinQT wants big endian here (!) | |
return [0xfd, i & 255, i >>> 8]; | |
} else { | |
throw ("message too large"); | |
} | |
} | |
function sha256(b) { | |
return Bitcoin.crypto.sha256(b); | |
} | |
function msg_bytes(message) { | |
var b = new Buffer(message, 'UTF8'); | |
return Buffer.concat([new Buffer(msg_numToVarInt(b.length)), b]); | |
} | |
function msg_digest(message) { | |
//var b = msg_bytes("Bitcoin Signed Message:\n").concat(msg_bytes(message)); | |
var b = Buffer.concat([msg_bytes("Bitcoin Signed Message:\n"), msg_bytes(message)]); | |
// console.log("To hash:" + new Buffer(b).toString("base64")); | |
return sha256(sha256(new Buffer(b))); | |
} | |
function bitcoinsig_test() { | |
var k = '5JeWZ1z6sRcLTJXdQEDdB986E6XfLAkj9CgNE4EHzr5GmjrVFpf'; | |
var a = '17mDAmveV5wBwxajBsY7g1trbMW1DVWcgL'; | |
var s = 'HDiv4Oe9SjM1FFVbKk4m3N34efYiRgkQGGoEm564ldYt44jHVTuX23+WnihNMi4vujvpUs1M529P3kftjDezn9E='; | |
var m = 'test message'; | |
// Beware passing this into other libs! http://www.mattesch.info/blog/the-instanceof-trap-in-node-js/ | |
var Ecurve = require("ecurve"); | |
var secp256k1 = Ecurve.getCurveByName('secp256k1'); | |
// When dcousen fixes it, replace ECurve with this | |
//var secp256k1 = Bitcoin.ECKey.curve; | |
// Part un - Verify pre-signed message | |
var siginfo = new Bitcoin.ECSignature.parseCompact(new Buffer(s,"base64")); | |
var hash = msg_digest(m); | |
assert.equal(hash.toString("base64"), "EiYXnd9jg/vPUQLJSSU4tyBsc5rnnrBkQIwqvWfTm+0="); | |
// hash.toString("base64") 9YdKhI6dqPsHun4O6UIhoLHRyx8VOaEBpJPd/7z4TQU= | |
var e = BigInteger.fromBuffer(hash); | |
var pubkeyQ = Bitcoin.ecdsa.recoverPubKey(secp256k1, e, siginfo.signature, siginfo.i); | |
// FIXME: bitcoinjs-lib 1.0.3 throws a instanceof exception on next line due to external Ecurve | |
var pubkey = new Bitcoin.ECPubKey(pubkeyQ, siginfo.compressed); | |
assert.equal(pubkey.getAddress().toBase58Check(), a, "Extract pub address from signature should match pub addr"); | |
var v1 = Bitcoin.ecdsa.verify(secp256k1, hash, siginfo.signature, pubkeyQ); | |
assert(v1, "Signature should pass"); | |
// Part deux - do signing and reverify | |
var payload = Base58.decode(k); | |
var priv = Bitcoin.ECKey.fromWIF(k) | |
var sig = priv.sign(hash); | |
var v2 = Bitcoin.ecdsa.verify(secp256k1, hash, siginfo.signature, pubkeyQ); | |
assert(v2, "Signature should pass after re-signing") | |
} | |
bitcoinsig_test(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment