2014-03-28 19:52:15 +01:00
|
|
|
var assert = require('assert')
|
2014-05-13 09:55:53 +02:00
|
|
|
var networks = require('../src/networks')
|
2014-05-13 08:35:07 +02:00
|
|
|
|
2014-05-16 07:39:17 +02:00
|
|
|
var BigInteger = require('bigi')
|
2014-05-13 09:55:53 +02:00
|
|
|
var ECKey = require('../src/eckey')
|
|
|
|
var Message = require('../src/message')
|
2013-03-02 18:28:13 +01:00
|
|
|
|
2014-05-18 11:47:39 +02:00
|
|
|
var fixtures = require('./fixtures/message.json')
|
2014-04-21 13:24:58 +02:00
|
|
|
|
2014-01-11 07:57:43 +01:00
|
|
|
describe('Message', function() {
|
2014-04-21 13:24:58 +02:00
|
|
|
describe('magicHash', function() {
|
|
|
|
it('matches the test vectors', function() {
|
2014-05-22 05:22:59 +02:00
|
|
|
fixtures.valid.magicHash.forEach(function(f) {
|
2014-05-16 07:39:17 +02:00
|
|
|
var network = networks[f.network]
|
|
|
|
var actual = Message.magicHash(f.message, network)
|
2014-04-21 13:24:58 +02:00
|
|
|
|
2014-05-16 07:39:17 +02:00
|
|
|
assert.equal(actual.toString('hex'), f.magicHash)
|
2014-04-21 13:24:58 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2014-03-28 19:52:15 +01:00
|
|
|
describe('verify', function() {
|
2014-05-22 05:22:59 +02:00
|
|
|
it('verifies a valid signature', function() {
|
|
|
|
fixtures.valid.verify.forEach(function(f) {
|
|
|
|
var network = networks[f.network]
|
2013-03-02 18:28:13 +01:00
|
|
|
|
2014-05-22 05:22:59 +02:00
|
|
|
var s1 = new Buffer(f.signature, 'base64')
|
|
|
|
assert.ok(Message.verify(f.address, s1, f.message, network))
|
2014-01-11 07:57:43 +01:00
|
|
|
|
2014-05-22 05:22:59 +02:00
|
|
|
if (f.compressed) {
|
|
|
|
var s2 = new Buffer(f.compressed.signature, 'base64')
|
2014-01-11 07:57:43 +01:00
|
|
|
|
2014-05-22 05:22:59 +02:00
|
|
|
assert.ok(Message.verify(f.compressed.address, s2, f.message, network))
|
|
|
|
}
|
|
|
|
})
|
2014-03-28 19:52:15 +01:00
|
|
|
})
|
2014-05-16 07:39:17 +02:00
|
|
|
|
2014-05-22 05:22:59 +02:00
|
|
|
fixtures.invalid.verify.forEach(function(f) {
|
|
|
|
it(f.description, function() {
|
|
|
|
var signature = new Buffer(f.signature, 'base64')
|
|
|
|
assert.ok(!Message.verify(f.address, signature, f.message))
|
|
|
|
})
|
2014-05-16 07:39:17 +02:00
|
|
|
})
|
2014-03-28 09:32:51 +01:00
|
|
|
})
|
2014-03-11 18:25:36 +01:00
|
|
|
|
2014-03-28 19:52:15 +01:00
|
|
|
describe('signing', function() {
|
2014-05-22 05:22:59 +02:00
|
|
|
fixtures.valid.signing.forEach(function(f) {
|
|
|
|
it(f.description, function() {
|
|
|
|
var network = networks[f.network]
|
2014-03-28 06:42:39 +01:00
|
|
|
|
2014-05-22 05:22:59 +02:00
|
|
|
var k1 = new ECKey(new BigInteger(f.D), false)
|
|
|
|
var s1 = Message.sign(k1, f.message, network)
|
|
|
|
assert.equal(s1.toString('base64'), f.signature)
|
2014-05-16 07:39:17 +02:00
|
|
|
|
2014-05-22 05:22:59 +02:00
|
|
|
if (f.compressed) {
|
|
|
|
var k2 = new ECKey(new BigInteger(f.D))
|
|
|
|
var s2 = Message.sign(k2, f.message)
|
2014-05-16 07:39:17 +02:00
|
|
|
|
2014-05-22 05:22:59 +02:00
|
|
|
assert.equal(s2.toString('base64'), f.compressed.signature)
|
|
|
|
}
|
|
|
|
})
|
2014-05-16 07:39:17 +02:00
|
|
|
})
|
2014-03-28 09:32:51 +01:00
|
|
|
})
|
2014-01-11 07:57:43 +01:00
|
|
|
})
|