Merge pull request #146 from dcousens/magictests

MagicHash Tests
This commit is contained in:
Kyle Drake 2014-04-21 09:39:50 -07:00
commit 1b66976d92
3 changed files with 36 additions and 13 deletions

View file

@ -7,17 +7,17 @@ var ecdsa = require('./ecdsa')
var ECPubKey = require('./eckey').ECPubKey var ECPubKey = require('./eckey').ECPubKey
// FIXME: magicHash is incompatible with other magic messages // FIXME: magicHash is incompatible with other magic messages
var magicBytes = convert.stringToBytes('Bitcoin Signed Message:\n') var magicBytes = new Buffer('Bitcoin Signed Message:\n')
function magicHash(message) { function magicHash(message) {
var messageBytes = convert.stringToBytes(message) var messageBytes = new Buffer(message)
var buffer = [].concat( var buffer = Buffer.concat([
convert.numToVarInt(magicBytes.length), new Buffer(convert.numToVarInt(magicBytes.length)),
magicBytes, magicBytes,
convert.numToVarInt(messageBytes.length), new Buffer(convert.numToVarInt(messageBytes.length)),
messageBytes messageBytes
) ])
return crypto.hash256(buffer) return crypto.hash256(buffer)
} }

12
test/fixtures/message.js vendored Normal file
View file

@ -0,0 +1,12 @@
module.exports = {
magicHash: [
{
message: '',
hash256: '80e795d4a4caadd7047af389d9f7f220562feb6196032e2131e10563352c4bcc'
},
{
message: 'Vires is Numeris',
hash256: 'f8a5affbef4a3241b19067aa694562f64f513310817297089a8929a930f4f933'
}
]
}

View file

@ -1,7 +1,9 @@
var assert = require('assert') var assert = require('assert')
var convert = require('../').convert
var ECKey = require('../src/eckey').ECKey var ECKey = require('../src/eckey').ECKey
var Message = require('../').Message var Message = require('../').Message
var network = require('../').network
var fixtures = require('./fixtures/message')
describe('Message', function() { describe('Message', function() {
var msg var msg
@ -10,6 +12,17 @@ describe('Message', function() {
msg = 'vires is numeris' msg = 'vires is numeris'
}) })
describe('magicHash', function() {
it('matches the test vectors', function() {
fixtures.magicHash.forEach(function(f) {
var actual = Message.magicHash(f.message)
var expected = f.hash256
assert.equal(actual.toString('hex'), expected)
})
})
})
describe('verify', function() { describe('verify', function() {
var addr, sig, caddr, csig var addr, sig, caddr, csig
@ -17,8 +30,8 @@ describe('Message', function() {
addr = '16UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM' // uncompressed addr = '16UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM' // uncompressed
caddr = '1PMycacnJaSqwwJqjawXBErnLsZ7RkXUAs' // compressed caddr = '1PMycacnJaSqwwJqjawXBErnLsZ7RkXUAs' // compressed
sig = convert.hexToBytes('1bc25ac0fb503abc9bad23f558742740fafaec1f52deaaf106b9759a5ce84c93921c4a669c5ec3dfeb7e2d7d177a2f49db407900874f6de2f701a4c16783776d8d') sig = new Buffer('1bc25ac0fb503abc9bad23f558742740fafaec1f52deaaf106b9759a5ce84c93921c4a669c5ec3dfeb7e2d7d177a2f49db407900874f6de2f701a4c16783776d8d', 'hex')
csig = convert.hexToBytes('1fc25ac0fb503abc9bad23f558742740fafaec1f52deaaf106b9759a5ce84c93921c4a669c5ec3dfeb7e2d7d177a2f49db407900874f6de2f701a4c16783776d8d') csig = new Buffer('1fc25ac0fb503abc9bad23f558742740fafaec1f52deaaf106b9759a5ce84c93921c4a669c5ec3dfeb7e2d7d177a2f49db407900874f6de2f701a4c16783776d8d', 'hex')
}) })
it('can verify a signed message', function() { it('can verify a signed message', function() {
@ -38,7 +51,7 @@ describe('Message', function() {
it('supports alternate network addresses', function() { it('supports alternate network addresses', function() {
var taddr = 'mxnQZKxSKjzaMgrdXzk35rif3u62TLDrg9' var taddr = 'mxnQZKxSKjzaMgrdXzk35rif3u62TLDrg9'
var tsig = convert.base64ToBytes('IGucnrTku3KLCCHUMwq9anawfrlN8RK1HWMN+10LhsHJeysBdWfj5ohJcS/+oqrlVFNvEgbgEeAQUL6r3sZwnj8=') var tsig = new Buffer('IGucnrTku3KLCCHUMwq9anawfrlN8RK1HWMN+10LhsHJeysBdWfj5ohJcS/+oqrlVFNvEgbgEeAQUL6r3sZwnj8=', 'base64')
assert.ok(Message.verify(taddr, tsig, msg)) assert.ok(Message.verify(taddr, tsig, msg))
assert.ok(!Message.verify(taddr, tsig, 'foobar')) assert.ok(!Message.verify(taddr, tsig, 'foobar'))
@ -70,12 +83,10 @@ describe('Message', function() {
describe('testnet address', function(){ describe('testnet address', function(){
it('works', function(){ it('works', function(){
var testnet = require('../').network.testnet
var key = ECKey.makeRandom() var key = ECKey.makeRandom()
var sig = Message.sign(key, msg) var sig = Message.sign(key, msg)
var addr = key.pub.getAddress(testnet.pubKeyHash) var addr = key.pub.getAddress(network.testnet.pubKeyHash)
assert(Message.verify(addr, sig, msg)) assert(Message.verify(addr, sig, msg))
}) })
}) })