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-10-10 19:23:40 +11:00
|
|
|
var BigInteger = require('bigi')
|
2015-03-02 16:48:36 +11:00
|
|
|
var ECPair = require('./ecpair')
|
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
|
|
|
|
2015-02-23 10:36:57 +11:00
|
|
|
function magicHash (message, network) {
|
2015-03-16 10:57:39 +11:00
|
|
|
var messagePrefix = new Buffer(network.messagePrefix)
|
2014-05-05 07:44:51 +10:00
|
|
|
var messageBuffer = new Buffer(message)
|
2014-10-17 13:07:14 +11:00
|
|
|
var lengthBuffer = bufferutils.varIntBuffer(messageBuffer.length)
|
2012-08-16 00:25:06 +02:00
|
|
|
|
2015-03-16 10:57:39 +11:00
|
|
|
var buffer = Buffer.concat([messagePrefix, 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
|
|
|
|
2015-03-02 16:48:36 +11:00
|
|
|
function sign (keyPair, message, network) {
|
2014-05-16 15:39:17 +10:00
|
|
|
network = network || networks.bitcoin
|
|
|
|
|
|
|
|
var hash = magicHash(message, network)
|
2015-03-02 16:48:36 +11:00
|
|
|
var signature = keyPair.sign(hash)
|
2014-05-10 22:30:29 +10:00
|
|
|
var e = BigInteger.fromBuffer(hash)
|
2015-03-02 16:48:36 +11:00
|
|
|
var i = ecdsa.calcPubKeyRecoveryParam(ecparams, e, signature, keyPair.Q)
|
2012-08-16 00:25:06 +02:00
|
|
|
|
2015-03-02 16:48:36 +11:00
|
|
|
return signature.toCompact(i, keyPair.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
|
2015-02-23 10:36:57 +11:00
|
|
|
function verify (address, signature, message, network) {
|
2014-08-20 09:14:04 +10:00
|
|
|
if (!Buffer.isBuffer(signature)) {
|
|
|
|
signature = new Buffer(signature, 'base64')
|
|
|
|
}
|
|
|
|
|
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)
|
2015-03-02 16:48:36 +11:00
|
|
|
var keyPair = new ECPair(null, Q, {
|
|
|
|
compressed: parsed.compressed,
|
|
|
|
network: network
|
|
|
|
})
|
2012-08-16 00:25:06 +02:00
|
|
|
|
2015-07-08 15:45:59 +10:00
|
|
|
return keyPair.getAddress() === 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
|
|
|
|
}
|