flocore/lib/Message.js
Ryan X. Charles 659dc10f96 add support for signing/verifying messages
This adds a new Message class with static methods for signing and verifying a
message the same way as bitcoind. (In a nutshell, messages a prepended with
"Bitcoin Signed Message:" before being hashed and signed).

There is one important piece missing ... verifying a signature with an address,
and not a public key. I have not yet implemented this because the cryptography
interface of bitcore does not allow me to derive the public key from a
signature. This will need to be added before verifying from an address is
possible.
2014-04-19 11:28:19 -03:00

43 lines
1.0 KiB
JavaScript

'use strict';
var imports = require('soop').imports();
var coinUtil = imports.coinUtil || require('../util');
var Key = imports.Key || require('./Key');
var Message = function() {
};
Message.sign = function(str, key) {
var hash = Message.magicHash(str);
var sig = key.signSync(hash);
return sig;
};
Message.verifyWithPubKey = function(pubkey, message, sig) {
var hash = Message.magicHash(message);
var key = new Key();
if (pubkey.length == 65)
key.compressed = false;
key.public = pubkey;
return key.verifySignatureSync(hash, sig);
};
//TODO: Message.verify ... with address, not pubkey
Message.magicBytes = new Buffer('Bitcoin Signed Message:\n');
Message.magicHash = function(str) {
var magicBytes = Message.magicBytes;
var prefix1 = coinUtil.varIntBuf(magicBytes.length);
var message = new Buffer(str);
var prefix2 = coinUtil.varIntBuf(message.length);
var buf = Buffer.concat([prefix1, magicBytes, prefix2, message]);
var hash = coinUtil.twoSha256(buf);
return hash;
};
module.exports = require('soop')(Message);