2013-03-02 18:28:13 +01:00
|
|
|
/// Implements Bitcoin's feature for signing arbitrary messages.
|
|
|
|
|
2014-03-28 06:20:46 +01:00
|
|
|
var Address = require('./address')
|
2014-03-27 02:00:08 +01:00
|
|
|
var convert = require('./convert')
|
2014-04-08 14:13:03 +02:00
|
|
|
var crypto = require('./crypto')
|
2014-03-28 06:20:46 +01:00
|
|
|
var ecdsa = require('./ecdsa')
|
2014-03-29 07:44:03 +01:00
|
|
|
var ECPubKey = require('./eckey').ECPubKey
|
2013-03-02 18:28:13 +01:00
|
|
|
|
2014-03-28 20:03:43 +01:00
|
|
|
// FIXME: magicHash is incompatible with other magic messages
|
|
|
|
var magicBytes = convert.stringToBytes('Bitcoin Signed Message:\n')
|
2012-08-16 00:25:06 +02:00
|
|
|
|
2014-03-28 20:03:43 +01:00
|
|
|
function magicHash(message) {
|
2014-03-27 02:00:08 +01:00
|
|
|
var messageBytes = convert.stringToBytes(message)
|
2012-08-16 00:25:06 +02:00
|
|
|
|
2014-03-28 20:03:43 +01:00
|
|
|
var buffer = [].concat(
|
2014-03-27 02:00:08 +01:00
|
|
|
convert.numToVarInt(magicBytes.length),
|
|
|
|
magicBytes,
|
|
|
|
convert.numToVarInt(messageBytes.length),
|
|
|
|
messageBytes
|
|
|
|
)
|
2012-08-16 00:25:06 +02:00
|
|
|
|
2014-04-08 14:13:03 +02:00
|
|
|
return crypto.hash256(buffer)
|
2014-03-27 02:00:08 +01:00
|
|
|
}
|
2012-08-16 00:25:06 +02:00
|
|
|
|
2014-03-28 20:03:43 +01:00
|
|
|
// TODO: parameterize compression instead of using ECKey.compressed
|
|
|
|
function sign(key, message) {
|
|
|
|
var hash = magicHash(message)
|
2014-03-27 02:00:08 +01:00
|
|
|
var sig = key.sign(hash)
|
|
|
|
var obj = ecdsa.parseSig(sig)
|
2014-04-17 11:08:16 +02:00
|
|
|
var i = ecdsa.calcPubKeyRecoveryParam(key.pub.Q, obj.r, obj.s, hash)
|
2012-08-16 00:25:06 +02:00
|
|
|
|
2014-03-27 02:00:08 +01:00
|
|
|
i += 27
|
2014-04-17 11:08:16 +02:00
|
|
|
if (key.pub.compressed) {
|
2014-03-27 02:00:08 +01:00
|
|
|
i += 4
|
|
|
|
}
|
2012-08-16 00:25:06 +02:00
|
|
|
|
2014-03-27 02:00:08 +01:00
|
|
|
var rBa = obj.r.toByteArrayUnsigned()
|
|
|
|
var sBa = obj.s.toByteArrayUnsigned()
|
2012-08-16 00:25:06 +02:00
|
|
|
|
2013-02-17 06:39:15 +01:00
|
|
|
// Pad to 32 bytes per value
|
2014-03-31 05:47:47 +02:00
|
|
|
while (rBa.length < 32) rBa.unshift(0);
|
|
|
|
while (sBa.length < 32) sBa.unshift(0);
|
2012-08-16 00:25:06 +02:00
|
|
|
|
2014-03-27 02:00:08 +01:00
|
|
|
sig = [i].concat(rBa, sBa)
|
2012-08-16 00:25:06 +02:00
|
|
|
|
2014-03-28 20:05:07 +01:00
|
|
|
return sig
|
2014-03-27 02:00:08 +01:00
|
|
|
}
|
2012-08-16 00:25:06 +02:00
|
|
|
|
2014-03-28 20:03:43 +01:00
|
|
|
function verify(address, sig, message) {
|
2014-03-28 20:05:07 +01:00
|
|
|
sig = ecdsa.parseSigCompact(sig)
|
2012-08-16 00:25:06 +02:00
|
|
|
|
2014-03-31 04:08:25 +02:00
|
|
|
var pubKey = new ECPubKey(ecdsa.recoverPubKey(sig.r, sig.s, magicHash(message), sig.i))
|
2014-04-17 11:08:16 +02:00
|
|
|
pubKey.compressed = !!(sig.i & 4)
|
2012-08-16 00:25:06 +02:00
|
|
|
|
2014-03-28 06:20:46 +01:00
|
|
|
address = new Address(address)
|
2014-03-28 20:03:43 +01:00
|
|
|
return pubKey.getAddress(address.version).toString() === address.toString()
|
2014-03-27 02:00:08 +01:00
|
|
|
}
|
2012-08-16 00:25:06 +02:00
|
|
|
|
2014-03-28 20:03:43 +01:00
|
|
|
module.exports = {
|
|
|
|
magicHash: magicHash,
|
|
|
|
sign: sign,
|
|
|
|
verify: verify
|
|
|
|
}
|