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-05-10 14:30:29 +02:00
|
|
|
var BigInteger = require('bigi')
|
2014-05-13 08:38:13 +02:00
|
|
|
var bufferutils = require('./bufferutils')
|
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-05-16 07:39:17 +02:00
|
|
|
var networks = require('./networks')
|
2013-03-02 18:28:13 +01:00
|
|
|
|
2014-05-16 07:39:17 +02:00
|
|
|
var Address = require('./address')
|
|
|
|
var ECPubKey = require('./ecpubkey')
|
2012-08-16 00:25:06 +02:00
|
|
|
|
2014-05-17 07:06:52 +02:00
|
|
|
var sec = require('./sec')
|
|
|
|
var ecparams = sec('secp256k1')
|
|
|
|
|
2014-05-16 07:39:17 +02:00
|
|
|
function magicHash(message, network) {
|
|
|
|
var magicPrefix = new Buffer(network.magicPrefix)
|
2014-05-04 23:44:51 +02:00
|
|
|
var messageBuffer = new Buffer(message)
|
2014-05-13 08:38:13 +02:00
|
|
|
var lengthBuffer = new Buffer(bufferutils.varIntSize(messageBuffer.length))
|
|
|
|
bufferutils.writeVarInt(lengthBuffer, messageBuffer.length, 0)
|
2012-08-16 00:25:06 +02:00
|
|
|
|
2014-05-04 23:44:51 +02:00
|
|
|
var buffer = Buffer.concat([
|
2014-05-16 07:39:17 +02:00
|
|
|
magicPrefix, lengthBuffer, messageBuffer
|
2014-05-04 23:44:51 +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-05-16 07:39:17 +02:00
|
|
|
function sign(key, message, network) {
|
|
|
|
network = network || networks.bitcoin
|
|
|
|
|
|
|
|
var hash = magicHash(message, network)
|
2014-05-24 08:25:38 +02:00
|
|
|
var signature = key.sign(hash)
|
2014-05-10 14:30:29 +02:00
|
|
|
var e = BigInteger.fromBuffer(hash)
|
2014-05-24 08:25:38 +02:00
|
|
|
var i = ecdsa.calcPubKeyRecoveryParam(ecparams, e, signature, key.pub.Q)
|
2012-08-16 00:25:06 +02:00
|
|
|
|
2014-05-24 08:25:38 +02:00
|
|
|
return ecdsa.serializeSigCompact(signature, i, key.pub.compressed)
|
2014-03-27 02:00:08 +01:00
|
|
|
}
|
2012-08-16 00:25:06 +02:00
|
|
|
|
2014-05-16 07:39:17 +02:00
|
|
|
// TODO: network could be implied from address
|
|
|
|
function verify(address, compactSig, message, network) {
|
2014-06-03 13:43:10 +02:00
|
|
|
if (address instanceof Address) {
|
|
|
|
address = address.toString()
|
2014-04-17 15:31:45 +02:00
|
|
|
}
|
2014-05-16 07:39:17 +02:00
|
|
|
network = network || networks.bitcoin
|
2012-08-16 00:25:06 +02:00
|
|
|
|
2014-05-16 07:39:17 +02:00
|
|
|
var hash = magicHash(message, network)
|
2014-05-24 08:25:38 +02:00
|
|
|
var parsed = ecdsa.parseSigCompact(compactSig)
|
2014-05-10 14:30:29 +02:00
|
|
|
var e = BigInteger.fromBuffer(hash)
|
2014-05-24 08:25:38 +02:00
|
|
|
var Q = ecdsa.recoverPubKey(ecparams, e, parsed.signature, parsed.i)
|
2012-08-16 00:25:06 +02:00
|
|
|
|
2014-05-24 08:25:38 +02:00
|
|
|
var pubKey = new ECPubKey(Q, parsed.compressed)
|
2014-06-03 13:43:10 +02:00
|
|
|
return pubKey.getAddress(network).toString() === address
|
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
|
|
|
|
}
|