2013-03-02 18:28:13 +01:00
|
|
|
/// Implements Bitcoin's feature for signing arbitrary messages.
|
|
|
|
|
|
|
|
var Crypto = require('./crypto-js/crypto');
|
|
|
|
var ecdsa = require('./ecdsa');
|
|
|
|
var conv = require('./convert');
|
|
|
|
var util = require('./util');
|
|
|
|
|
2013-02-17 06:39:15 +01:00
|
|
|
var Message = {};
|
2012-08-16 00:25:06 +02:00
|
|
|
|
2013-02-17 06:39:15 +01:00
|
|
|
Message.magicPrefix = "Bitcoin Signed Message:\n";
|
2012-08-16 00:25:06 +02:00
|
|
|
|
2013-02-17 06:39:15 +01:00
|
|
|
Message.makeMagicMessage = function (message) {
|
|
|
|
var magicBytes = Crypto.charenc.UTF8.stringToBytes(Message.magicPrefix);
|
|
|
|
var messageBytes = Crypto.charenc.UTF8.stringToBytes(message);
|
2012-08-16 00:25:06 +02:00
|
|
|
|
2013-02-17 06:39:15 +01:00
|
|
|
var buffer = [];
|
2013-03-02 18:28:13 +01:00
|
|
|
buffer = buffer.concat(util.numToVarInt(magicBytes.length));
|
2013-02-17 06:39:15 +01:00
|
|
|
buffer = buffer.concat(magicBytes);
|
2013-03-02 18:28:13 +01:00
|
|
|
buffer = buffer.concat(util.numToVarInt(messageBytes.length));
|
2013-02-17 06:39:15 +01:00
|
|
|
buffer = buffer.concat(messageBytes);
|
2012-08-16 00:25:06 +02:00
|
|
|
|
2013-02-17 06:39:15 +01:00
|
|
|
return buffer;
|
|
|
|
};
|
2012-08-16 00:25:06 +02:00
|
|
|
|
2013-02-17 06:39:15 +01:00
|
|
|
Message.getHash = function (message) {
|
|
|
|
var buffer = Message.makeMagicMessage(message);
|
|
|
|
return Crypto.SHA256(Crypto.SHA256(buffer, {asBytes: true}), {asBytes: true});
|
|
|
|
};
|
2012-08-16 00:25:06 +02:00
|
|
|
|
2013-02-17 06:39:15 +01:00
|
|
|
Message.signMessage = function (key, message, compressed) {
|
|
|
|
var hash = Message.getHash(message);
|
2012-08-16 00:25:06 +02:00
|
|
|
|
2013-02-17 06:39:15 +01:00
|
|
|
var sig = key.sign(hash);
|
2012-08-16 00:25:06 +02:00
|
|
|
|
2013-03-02 18:28:13 +01:00
|
|
|
var obj = ecdsa.parseSig(sig);
|
2012-08-16 00:25:06 +02:00
|
|
|
|
2013-02-17 06:39:15 +01:00
|
|
|
var address = key.getBitcoinAddress().toString();
|
2013-03-02 18:28:13 +01:00
|
|
|
var i = ecdsa.calcPubkeyRecoveryParam(address, obj.r, obj.s, hash);
|
2012-08-16 00:25:06 +02:00
|
|
|
|
2013-02-17 06:39:15 +01:00
|
|
|
i += 27;
|
|
|
|
if (compressed) i += 4;
|
2012-08-16 00:25:06 +02:00
|
|
|
|
2013-02-17 06:39:15 +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
|
|
|
|
while (rBa.length < 32) rBa.unshift(0);
|
|
|
|
while (sBa.length < 32) sBa.unshift(0);
|
2012-08-16 00:25:06 +02:00
|
|
|
|
2013-02-17 06:39:15 +01:00
|
|
|
sig = [i].concat(rBa).concat(sBa);
|
2012-08-16 00:25:06 +02:00
|
|
|
|
2013-03-02 18:28:13 +01:00
|
|
|
return conv.bytesToBase64(sig);
|
2013-02-17 06:39:15 +01:00
|
|
|
};
|
2012-08-16 00:25:06 +02:00
|
|
|
|
2013-02-17 06:39:15 +01:00
|
|
|
Message.verifyMessage = function (address, sig, message) {
|
2013-03-02 18:28:13 +01:00
|
|
|
sig = conv.base64ToBytes(sig);
|
|
|
|
sig = ecdsa.parseSigCompact(sig);
|
2012-08-16 00:25:06 +02:00
|
|
|
|
2013-02-17 06:39:15 +01:00
|
|
|
var hash = Message.getHash(message);
|
2012-08-16 00:25:06 +02:00
|
|
|
|
2013-02-17 06:39:15 +01:00
|
|
|
var isCompressed = !!(sig.i & 4);
|
2013-03-02 18:28:13 +01:00
|
|
|
var pubKey = ecdsa.recoverPubKey(sig.r, sig.s, hash, sig.i);
|
2012-08-16 00:25:06 +02:00
|
|
|
|
2013-02-17 06:39:15 +01:00
|
|
|
pubKey.setCompressed(isCompressed);
|
2012-08-16 00:25:06 +02:00
|
|
|
|
2013-02-17 06:39:15 +01:00
|
|
|
var expectedAddress = pubKey.getBitcoinAddress().toString();
|
2012-08-16 00:25:06 +02:00
|
|
|
|
2013-02-17 06:39:15 +01:00
|
|
|
return (address === expectedAddress);
|
|
|
|
};
|
2012-08-16 00:25:06 +02:00
|
|
|
|
2013-02-17 06:39:15 +01:00
|
|
|
module.exports = Message;
|