flocore/test/test.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

58 lines
1.8 KiB
JavaScript

'use strict';
var chai = chai || require('chai');
var should = chai.should();
var bitcore = bitcore || require('../bitcore');
var Message = bitcore.Message;
var coinUtil = bitcore.util;
describe('Message', function() {
describe('sign', function() {
it('should return a signature', function() {
var key = bitcore.Key.generateSync();
var sig = Message.sign('my message', key);
sig.length.should.be.greaterThan(0);
});
});
describe('verifyWithPubKey', function() {
it('should verify a signed message', function() {
var message = 'my message';
var key = bitcore.Key.generateSync();
var sig = Message.sign(message, key);
Message.verifyWithPubKey(key.public, message, sig).should.equal(true);
});
});
describe('magicBytes', function() {
it('should be "Bitcoin Signed Message:\\n"', function() {
Message.magicBytes.toString().should.equal('Bitcoin Signed Message:\n');
});
});
describe('magicHash', function() {
it('should hash the message with the magic bytes', function() {
var str = 'my message';
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);
var hash2 = Message.magicHash(str);
hash.toString().should.equal(hash2.toString());
});
it('should hash this message the same way as bitcoinjs-lib', function() {
var hashHex = '74eacdc6c04724869380907bf4aab561a1494a4a800fba266b29b8158c2c4cec';
var str = 'this is a test message';
hashHex.should.equal(Message.magicHash(str).toString('hex'));
});
});
});