2013-03-02 12:28:13 -05:00
|
|
|
/// Implements Bitcoin's feature for signing arbitrary messages.
|
2014-03-28 16:20:46 +11:00
|
|
|
var Address = require('./address')
|
2014-05-10 22:30:29 +10:00
|
|
|
var BigInteger = require('bigi')
|
2014-05-13 16:38:13 +10:00
|
|
|
var bufferutils = require('./bufferutils')
|
2014-04-08 22:13:03 +10:00
|
|
|
var crypto = require('./crypto')
|
2014-03-28 16:20:46 +11:00
|
|
|
var ecdsa = require('./ecdsa')
|
2014-05-16 15:39:17 +10:00
|
|
|
var networks = require('./networks')
|
2013-03-02 12:28:13 -05:00
|
|
|
|
2014-05-16 15:39:17 +10:00
|
|
|
var Address = require('./address')
|
|
|
|
var ECPubKey = require('./ecpubkey')
|
2014-06-15 16:44:52 +10:00
|
|
|
var ECSignature = require('./ecsignature')
|
2012-08-16 00:25:06 +02:00
|
|
|
|
2014-06-07 16:24:27 +10:00
|
|
|
var ecurve = require('ecurve')
|
|
|
|
var ecparams = ecurve.getCurveByName('secp256k1')
|
2014-05-17 15:06:52 +10:00
|
|
|
|
2014-05-16 15:39:17 +10:00
|
|
|
function magicHash(message, network) {
|
2014-06-17 21:05:18 +10:00
|
|
|
var magicPrefix = new Buffer(network.magicPrefix)
|
2014-05-05 07:44:51 +10:00
|
|
|
var messageBuffer = new Buffer(message)
|
2014-05-13 16:38:13 +10:00
|
|
|
var lengthBuffer = new Buffer(bufferutils.varIntSize(messageBuffer.length))
|
|
|
|
bufferutils.writeVarInt(lengthBuffer, messageBuffer.length, 0)
|
2012-08-16 00:25:06 +02:00
|
|
|
|
2014-06-15 16:44:52 +10:00
|
|
|
var buffer = Buffer.concat([magicPrefix, lengthBuffer, messageBuffer])
|
2014-04-08 22:13:03 +10:00
|
|
|
return crypto.hash256(buffer)
|
2014-03-26 21:00:08 -04:00
|
|
|
}
|
2012-08-16 00:25:06 +02:00
|
|
|
|
2014-06-15 16:44:52 +10:00
|
|
|
function sign(privKey, message, network) {
|
2014-05-16 15:39:17 +10:00
|
|
|
network = network || networks.bitcoin
|
|
|
|
|
|
|
|
var hash = magicHash(message, network)
|
2014-06-15 16:44:52 +10:00
|
|
|
var signature = privKey.sign(hash)
|
2014-05-10 22:30:29 +10:00
|
|
|
var e = BigInteger.fromBuffer(hash)
|
2014-06-15 16:44:52 +10:00
|
|
|
var i = ecdsa.calcPubKeyRecoveryParam(ecparams, e, signature, privKey.pub.Q)
|
2012-08-16 00:25:06 +02:00
|
|
|
|
2014-06-15 16:44:52 +10:00
|
|
|
return signature.toCompact(i, privKey.pub.compressed)
|
2014-03-26 21:00:08 -04:00
|
|
|
}
|
2012-08-16 00:25:06 +02:00
|
|
|
|
2014-05-16 15:39:17 +10:00
|
|
|
// TODO: network could be implied from address
|
2014-08-20 09:14:04 +10:00
|
|
|
function verify(address, signature, message, network) {
|
|
|
|
if (!Buffer.isBuffer(signature)) {
|
|
|
|
signature = new Buffer(signature, 'base64')
|
|
|
|
}
|
|
|
|
|
2014-06-03 21:43:10 +10:00
|
|
|
if (address instanceof Address) {
|
|
|
|
address = address.toString()
|
2014-04-17 23:31:45 +10:00
|
|
|
}
|
2014-08-20 09:14:04 +10:00
|
|
|
|
2014-05-16 15:39:17 +10:00
|
|
|
network = network || networks.bitcoin
|
2012-08-16 00:25:06 +02:00
|
|
|
|
2014-05-16 15:39:17 +10:00
|
|
|
var hash = magicHash(message, network)
|
2014-08-20 09:14:04 +10:00
|
|
|
var parsed = ECSignature.parseCompact(signature)
|
2014-05-10 22:30:29 +10:00
|
|
|
var e = BigInteger.fromBuffer(hash)
|
2014-05-24 16:25:38 +10:00
|
|
|
var Q = ecdsa.recoverPubKey(ecparams, e, parsed.signature, parsed.i)
|
2012-08-16 00:25:06 +02:00
|
|
|
|
2014-05-24 16:25:38 +10:00
|
|
|
var pubKey = new ECPubKey(Q, parsed.compressed)
|
2014-06-03 21:43:10 +10:00
|
|
|
return pubKey.getAddress(network).toString() === address
|
2014-03-26 21:00:08 -04:00
|
|
|
}
|
2012-08-16 00:25:06 +02:00
|
|
|
|
2014-03-29 06:03:43 +11:00
|
|
|
module.exports = {
|
|
|
|
magicHash: magicHash,
|
|
|
|
sign: sign,
|
|
|
|
verify: verify
|
|
|
|
}
|