2014-01-11 07:57:43 +01:00
|
|
|
/* global describe, it */
|
2013-03-02 18:28:13 +01:00
|
|
|
var assert = require('assert');
|
2014-01-11 07:57:43 +01:00
|
|
|
var Message = require('../src/message.js');
|
|
|
|
var ECKey = require('../src/eckey.js').ECKey;
|
|
|
|
var hexToBytes = require('../src/convert.js').hexToBytes;
|
2013-03-02 18:28:13 +01:00
|
|
|
|
|
|
|
var priv = '18e14a7b6a307f426a94f8114701e7c8e774e7f9a47e2c2035db29a206321725';
|
|
|
|
var addr = '16UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM';
|
|
|
|
var msg = 'foobar';
|
|
|
|
|
2014-01-11 07:57:43 +01:00
|
|
|
describe('Message', function() {
|
|
|
|
describe('verify', function() {
|
|
|
|
it('passes case 1', function() {
|
|
|
|
var key = new ECKey(hexToBytes(priv));
|
|
|
|
assert.equal(key.getBitcoinAddress().toString(), addr);
|
2013-03-02 18:28:13 +01:00
|
|
|
|
2014-01-11 07:57:43 +01:00
|
|
|
var sig = Message.signMessage(key, msg);
|
|
|
|
assert.ok(Message.verifyMessage(addr, sig, msg));
|
2013-03-02 18:28:13 +01:00
|
|
|
|
2014-01-11 07:57:43 +01:00
|
|
|
// wrong message
|
|
|
|
assert.ok(!Message.verifyMessage(addr, sig, 'not foobar'));
|
|
|
|
|
|
|
|
// wrong address
|
|
|
|
assert.ok(!Message.verifyMessage('1MsHWS1BnwMc3tLE8G35UXsS58fKipzB7a', sig, msg));
|
|
|
|
})
|
|
|
|
|
|
|
|
it('passes case 2', function() {
|
|
|
|
var priv = '5HwoXVkHoRM8sL2KmNRS217n1g8mPPBomrY7yehCuXC1115WWsh';
|
|
|
|
var key = new ECKey(hexToBytes(priv));
|
|
|
|
var sig = Message.signMessage(key, msg);
|
|
|
|
assert.ok(!Message.verifyMessage(addr, sig, msg));
|
|
|
|
})
|
2014-03-11 18:25:36 +01:00
|
|
|
|
|
|
|
it('handles compressed keys', function() {
|
|
|
|
var key = new ECKey(hexToBytes(priv));
|
|
|
|
key.compressed = true
|
|
|
|
|
|
|
|
var addr = key.getBitcoinAddress().toString()
|
|
|
|
|
|
|
|
var sig = Message.signMessage(key, msg);
|
|
|
|
assert.ok(Message.verifyMessage(addr, sig, msg));
|
|
|
|
|
|
|
|
// wrong message
|
|
|
|
assert.ok(!Message.verifyMessage(addr, sig, 'not foobar'));
|
|
|
|
|
|
|
|
// wrong address
|
|
|
|
assert.ok(!Message.verifyMessage('1MsHWS1BnwMc3tLE8G35UXsS58fKipzB7a', sig, msg));
|
|
|
|
})
|
2014-01-11 07:57:43 +01:00
|
|
|
})
|
|
|
|
})
|