bitcoinjs-lib/test/message.js

87 lines
3 KiB
JavaScript
Raw Normal View History

var assert = require('assert')
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')
var ECKey = require('../src/eckey')
var Message = require('../src/message')
2014-04-21 13:24:58 +02:00
var fixtures = require('./fixtures/message')
describe('Message', function() {
var message
beforeEach(function() {
message = 'vires is numeris'
})
2014-04-21 13:24:58 +02:00
describe('magicHash', function() {
it('matches the test vectors', function() {
fixtures.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
})
})
})
describe('verify', function() {
var addr, sig, caddr, csig
beforeEach(function() {
addr = '16UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM' // uncompressed
caddr = '1PMycacnJaSqwwJqjawXBErnLsZ7RkXUAs' // compressed
2014-05-16 08:41:57 +02:00
sig = new Buffer('G8JawPtQOrybrSP1WHQnQPr67B9S3qrxBrl1mlzoTJOSHEpmnF7D3+t+LX0Xei9J20B5AIdPbeL3AaTBZ4N3bY0=', 'base64')
csig = new Buffer('H8JawPtQOrybrSP1WHQnQPr67B9S3qrxBrl1mlzoTJOSHEpmnF7D3+t+LX0Xei9J20B5AIdPbeL3AaTBZ4N3bY0=', 'base64')
})
it('can verify a signed message', function() {
assert.ok(Message.verify(addr, sig, message))
assert.ok(Message.verify(caddr, csig, message))
})
it('will fail for the wrong message', function() {
assert.ok(!Message.verify(addr, sig, 'foobar'))
assert.ok(!Message.verify(caddr, csig, 'foobar'))
})
it('will fail for the wrong public key', function() {
assert.ok(!Message.verify('1MsHWS1BnwMc3tLE8G35UXsS58fKipzB7a', sig, message))
assert.ok(!Message.verify('1Q1pE5vPGEEMqRcVRMbtBK842Y6Pzo6nK9', csig, message))
})
it('does not cross verify (compressed/uncompressed)', function() {
assert.ok(!Message.verify(addr, csig, message))
assert.ok(!Message.verify(caddr, sig, message))
})
2014-05-16 07:39:17 +02:00
it('supports alternate networks', function() {
var dogeaddr = 'DFpN6QqFfUm3gKNaxN6tNcab1FArL9cZLE'
var dogesig = new Buffer('H6k+dZwJ8oOei3PCSpdj603fDvhlhQ+sqaFNIDvo/bI+Xh6zyIKGzZpyud6YhZ1a5mcrwMVtTWL+VXq/hC5Zj7s=', 'base64')
assert.ok(Message.verify(dogeaddr, dogesig, message, networks.dogecoin))
})
})
describe('signing', function() {
it('gives matching signatures irrespective of point compression', function() {
2014-05-16 07:39:17 +02:00
var privKey = new ECKey(BigInteger.ONE, false)
var compressedKey = new ECKey(privKey.D, true)
var sig = Message.sign(privKey, message)
var csig = Message.sign(compressedKey, message)
assert.notDeepEqual(sig.slice(0, 2), csig.slice(0, 2)) // unequal compression flags
assert.deepEqual(sig.slice(2), csig.slice(2)) // equal signatures
})
2014-05-16 07:39:17 +02:00
it('supports alternate networks', function() {
var privKey = new ECKey(BigInteger.ONE)
var signature = Message.sign(privKey, message, networks.dogecoin)
assert.equal(signature.toString('base64'), 'H6k+dZwJ8oOei3PCSpdj603fDvhlhQ+sqaFNIDvo/bI+Xh6zyIKGzZpyud6YhZ1a5mcrwMVtTWL+VXq/hC5Zj7s=')
})
})
})