bitcoinjs-lib/test/message.js

74 lines
2.2 KiB
JavaScript
Raw Normal View History

2015-02-23 10:36:57 +11:00
/* global describe, it */
var assert = require('assert')
2014-10-18 21:54:13 +11:00
var message = require('../src/message')
var networks = require('../src/networks')
2014-05-13 16:35:07 +10:00
2014-06-14 10:21:38 +10:00
var Address = require('../src/address')
2014-05-16 15:39:17 +10:00
var BigInteger = require('bigi')
2015-03-02 16:48:36 +11:00
var ECPair = require('../src/ecpair')
2014-05-18 19:47:39 +10:00
var fixtures = require('./fixtures/message.json')
2014-04-21 21:24:58 +10:00
2014-10-18 21:54:13 +11:00
describe('message', function () {
2015-02-23 10:36:57 +11:00
describe('magicHash', function () {
fixtures.valid.magicHash.forEach(function (f) {
2014-09-15 15:00:13 +10:00
it('produces the correct magicHash for "' + f.message + '" (' + f.network + ')', function () {
2014-05-16 15:39:17 +10:00
var network = networks[f.network]
2014-10-18 21:54:13 +11:00
var actual = message.magicHash(f.message, network)
2014-04-21 21:24:58 +10:00
2014-05-16 15:39:17 +10:00
assert.equal(actual.toString('hex'), f.magicHash)
2014-04-21 21:24:58 +10:00
})
})
})
2015-02-23 10:36:57 +11:00
describe('verify', function () {
it('accepts an Address object', function () {
2014-06-14 10:21:38 +10:00
var f = fixtures.valid.verify[0]
var network = networks[f.network]
var address = Address.fromBase58Check(f.address)
2014-10-18 21:54:13 +11:00
assert(message.verify(address, f.signature, f.message, network))
2014-06-14 10:21:38 +10:00
})
2015-02-23 10:36:57 +11:00
fixtures.valid.verify.forEach(function (f) {
it('verifies a valid signature for "' + f.message + '" (' + f.network + ')', function () {
2014-05-22 13:22:59 +10:00
var network = networks[f.network]
2014-10-18 21:54:13 +11:00
assert(message.verify(f.address, f.signature, f.message, network))
2014-05-22 13:22:59 +10:00
if (f.compressed) {
2014-10-18 21:54:13 +11:00
assert(message.verify(f.compressed.address, f.compressed.signature, f.message, network))
2014-05-22 13:22:59 +10:00
}
})
})
2014-05-16 15:39:17 +10:00
2015-02-23 10:36:57 +11:00
fixtures.invalid.verify.forEach(function (f) {
it(f.description, function () {
2014-10-18 21:54:13 +11:00
assert(!message.verify(f.address, f.signature, f.message))
2014-05-22 13:22:59 +10:00
})
2014-05-16 15:39:17 +10:00
})
})
2015-02-23 10:36:57 +11:00
describe('signing', function () {
fixtures.valid.signing.forEach(function (f) {
it(f.description, function () {
2014-05-22 13:22:59 +10:00
var network = networks[f.network]
2015-03-02 16:48:36 +11:00
var keyPair = new ECPair(new BigInteger(f.d), null, {
compressed: false
})
var signature = message.sign(keyPair, f.message, network)
2014-05-24 13:54:46 +10:00
assert.equal(signature.toString('base64'), f.signature)
2014-05-16 15:39:17 +10:00
2014-05-22 13:22:59 +10:00
if (f.compressed) {
2015-03-02 16:48:36 +11:00
var compressedPrivKey = new ECPair(new BigInteger(f.d))
2014-10-18 21:54:13 +11:00
var compressedSignature = message.sign(compressedPrivKey, f.message)
2014-05-16 15:39:17 +10:00
2014-05-24 13:54:46 +10:00
assert.equal(compressedSignature.toString('base64'), f.compressed.signature)
2014-05-22 13:22:59 +10:00
}
})
2014-05-16 15:39:17 +10:00
})
})
})