2015-02-23 00:36:57 +01:00
|
|
|
/* global describe, it */
|
|
|
|
|
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-06-14 02:21:38 +02:00
|
|
|
var Address = require('../src/address')
|
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
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
describe('Message', function () {
|
|
|
|
describe('magicHash', function () {
|
|
|
|
fixtures.valid.magicHash.forEach(function (f) {
|
|
|
|
it('produces the correct magicHash for "' + f.message + '" (' + f.network + ')', function () {
|
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
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
describe('verify', function () {
|
|
|
|
it('accepts an Address object', function () {
|
2014-06-14 02:21:38 +02:00
|
|
|
var f = fixtures.valid.verify[0]
|
|
|
|
var network = networks[f.network]
|
|
|
|
|
|
|
|
var address = Address.fromBase58Check(f.address)
|
2014-08-20 01:17:55 +02:00
|
|
|
assert(Message.verify(address, f.signature, f.message, network))
|
2014-06-14 02:21:38 +02:00
|
|
|
})
|
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
fixtures.valid.verify.forEach(function (f) {
|
|
|
|
it('verifies a valid signature for "' + f.message + '" (' + f.network + ')', function () {
|
2014-05-22 05:22:59 +02:00
|
|
|
var network = networks[f.network]
|
2013-03-02 18:28:13 +01:00
|
|
|
|
2014-08-20 01:17:55 +02:00
|
|
|
assert(Message.verify(f.address, f.signature, f.message, network))
|
2014-01-11 07:57:43 +01:00
|
|
|
|
2014-05-22 05:22:59 +02:00
|
|
|
if (f.compressed) {
|
2014-08-20 01:17:55 +02:00
|
|
|
assert(Message.verify(f.compressed.address, f.compressed.signature, f.message, network))
|
2014-05-22 05:22:59 +02:00
|
|
|
}
|
|
|
|
})
|
2014-03-28 19:52:15 +01:00
|
|
|
})
|
2014-05-16 07:39:17 +02:00
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
fixtures.invalid.verify.forEach(function (f) {
|
|
|
|
it(f.description, function () {
|
2014-08-20 01:17:55 +02:00
|
|
|
assert(!Message.verify(f.address, f.signature, f.message))
|
2014-05-22 05:22:59 +02:00
|
|
|
})
|
2014-05-16 07:39:17 +02:00
|
|
|
})
|
2014-03-28 09:32:51 +01:00
|
|
|
})
|
2014-03-11 18:25:36 +01:00
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
describe('signing', function () {
|
|
|
|
fixtures.valid.signing.forEach(function (f) {
|
|
|
|
it(f.description, function () {
|
2014-05-22 05:22:59 +02:00
|
|
|
var network = networks[f.network]
|
2014-03-28 06:42:39 +01:00
|
|
|
|
2014-06-07 10:24:16 +02:00
|
|
|
var privKey = new ECKey(new BigInteger(f.d), false)
|
2014-05-24 05:54:46 +02:00
|
|
|
var signature = Message.sign(privKey, f.message, network)
|
|
|
|
assert.equal(signature.toString('base64'), f.signature)
|
2014-05-16 07:39:17 +02:00
|
|
|
|
2014-05-22 05:22:59 +02:00
|
|
|
if (f.compressed) {
|
2014-06-07 10:24:16 +02:00
|
|
|
var compressedPrivKey = new ECKey(new BigInteger(f.d))
|
2014-05-24 05:54:46 +02:00
|
|
|
var compressedSignature = Message.sign(compressedPrivKey, f.message)
|
2014-05-16 07:39:17 +02:00
|
|
|
|
2014-05-24 05:54:46 +02:00
|
|
|
assert.equal(compressedSignature.toString('base64'), f.compressed.signature)
|
2014-05-22 05:22:59 +02:00
|
|
|
}
|
|
|
|
})
|
2014-05-16 07:39:17 +02:00
|
|
|
})
|
2014-03-28 09:32:51 +01:00
|
|
|
})
|
2014-01-11 07:57:43 +01:00
|
|
|
})
|