Browse Source

message: data-driven tests

hk-custom-address
Daniel Cousens 11 years ago
parent
commit
2114deba95
  1. 101
      test/fixtures/message.json
  2. 75
      test/message.js

101
test/fixtures/message.json

@ -1,19 +1,86 @@
{
"magicHash": [
{
"network": "bitcoin",
"message": "",
"magicHash": "80e795d4a4caadd7047af389d9f7f220562feb6196032e2131e10563352c4bcc"
},
{
"network": "bitcoin",
"message": "Vires is Numeris",
"magicHash": "f8a5affbef4a3241b19067aa694562f64f513310817297089a8929a930f4f933"
},
{
"network": "dogecoin",
"message": "Vires is Numeris",
"magicHash": "c0963d20d0accd0ea0df6c1020bf85a7e629a40e7b5363f2c3e9dcafd5638f12"
}
]
"valid": {
"magicHash": [
{
"network": "bitcoin",
"message": "",
"magicHash": "80e795d4a4caadd7047af389d9f7f220562feb6196032e2131e10563352c4bcc"
},
{
"network": "bitcoin",
"message": "Vires is Numeris",
"magicHash": "f8a5affbef4a3241b19067aa694562f64f513310817297089a8929a930f4f933"
},
{
"network": "dogecoin",
"message": "Vires is Numeris",
"magicHash": "c0963d20d0accd0ea0df6c1020bf85a7e629a40e7b5363f2c3e9dcafd5638f12"
}
],
"verify": [
{
"message": "vires is numeris",
"network": "bitcoin",
"address": "16UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM",
"signature": "G8JawPtQOrybrSP1WHQnQPr67B9S3qrxBrl1mlzoTJOSHEpmnF7D3+t+LX0Xei9J20B5AIdPbeL3AaTBZ4N3bY0=",
"compressed": {
"address": "1PMycacnJaSqwwJqjawXBErnLsZ7RkXUAs",
"signature": "H8JawPtQOrybrSP1WHQnQPr67B9S3qrxBrl1mlzoTJOSHEpmnF7D3+t+LX0Xei9J20B5AIdPbeL3AaTBZ4N3bY0="
}
},
{
"message": "vires is numeris",
"network": "dogecoin",
"address": "DFpN6QqFfUm3gKNaxN6tNcab1FArL9cZLE",
"signature": "H6k+dZwJ8oOei3PCSpdj603fDvhlhQ+sqaFNIDvo/bI+Xh6zyIKGzZpyud6YhZ1a5mcrwMVtTWL+VXq/hC5Zj7s="
}
],
"signing": [
{
"description": "gives equal r, s values irrespective of point compression",
"message": "vires is numeris",
"network": "bitcoin",
"D": "1",
"signature": "HF8nHqFr3K2UKYahhX3soVeoW8W1ECNbr0wfck7lzyXjCS5Q16Ek45zyBuy1Fiy9sTPKVgsqqOuPvbycuVSSVl8=",
"compressed": {
"signature": "IF8nHqFr3K2UKYahhX3soVeoW8W1ECNbr0wfck7lzyXjCS5Q16Ek45zyBuy1Fiy9sTPKVgsqqOuPvbycuVSSVl8="
}
},
{
"description": "supports alternative networks",
"message": "vires is numeris",
"network": "dogecoin",
"D": "1",
"signature": "G6k+dZwJ8oOei3PCSpdj603fDvhlhQ+sqaFNIDvo/bI+Xh6zyIKGzZpyud6YhZ1a5mcrwMVtTWL+VXq/hC5Zj7s="
}
]
},
"invalid": {
"verify": [
{
"description": "will fail for the wrong message",
"message": "foobar",
"address": "16UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM",
"signature": "G8JawPtQOrybrSP1WHQnQPr67B9S3qrxBrl1mlzoTJOSHEpmnF7D3+t+LX0Xei9J20B5AIdPbeL3AaTBZ4N3bY0="
},
{
"description": "will fail for the wrong address",
"message": "vires is numeris",
"address": "1111111111111111111114oLvT2",
"signature": "H8JawPtQOrybrSP1WHQnQPr67B9S3qrxBrl1mlzoTJOSHEpmnF7D3+t+LX0Xei9J20B5AIdPbeL3AaTBZ4N3bY0="
},
{
"description": "does not cross verify (uncompressed address, compressed signature)",
"message": "vires is numeris",
"address": "16UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM",
"signature": "H8JawPtQOrybrSP1WHQnQPr67B9S3qrxBrl1mlzoTJOSHEpmnF7D3+t+LX0Xei9J20B5AIdPbeL3AaTBZ4N3bY0="
},
{
"description": "does not cross verify (compressed address, uncompressed signature)",
"message": "vires is numeris",
"address": "1PMycacnJaSqwwJqjawXBErnLsZ7RkXUAs",
"signature": "G8JawPtQOrybrSP1WHQnQPr67B9S3qrxBrl1mlzoTJOSHEpmnF7D3+t+LX0Xei9J20B5AIdPbeL3AaTBZ4N3bY0="
}
]
}
}

75
test/message.js

@ -8,15 +8,9 @@ var Message = require('../src/message')
var fixtures = require('./fixtures/message.json')
describe('Message', function() {
var message
beforeEach(function() {
message = 'vires is numeris'
})
describe('magicHash', function() {
it('matches the test vectors', function() {
fixtures.magicHash.forEach(function(f) {
fixtures.valid.magicHash.forEach(function(f) {
var network = networks[f.network]
var actual = Message.magicHash(f.message, network)
@ -26,58 +20,45 @@ describe('Message', function() {
})
describe('verify', function() {
var addr, sig, caddr, csig
beforeEach(function() {
addr = '16UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM' // uncompressed
caddr = '1PMycacnJaSqwwJqjawXBErnLsZ7RkXUAs' // compressed
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))
})
it('verifies a valid signature', function() {
fixtures.valid.verify.forEach(function(f) {
var network = networks[f.network]
it('will fail for the wrong message', function() {
assert.ok(!Message.verify(addr, sig, 'foobar'))
})
var s1 = new Buffer(f.signature, 'base64')
assert.ok(Message.verify(f.address, s1, f.message, network))
it('will fail for the wrong address', function() {
assert.ok(!Message.verify('1MsHWS1BnwMc3tLE8G35UXsS58fKipzB7a', sig, message))
})
if (f.compressed) {
var s2 = new Buffer(f.compressed.signature, 'base64')
it('does not cross verify (compressed/uncompressed)', function() {
assert.ok(!Message.verify(addr, csig, message))
assert.ok(!Message.verify(caddr, sig, message))
assert.ok(Message.verify(f.compressed.address, s2, f.message, network))
}
})
})
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))
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))
})
})
})
describe('signing', function() {
it('gives matching signatures irrespective of point compression', function() {
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)
fixtures.valid.signing.forEach(function(f) {
it(f.description, function() {
var network = networks[f.network]
assert.notDeepEqual(sig.slice(0, 2), csig.slice(0, 2)) // unequal compression flags
assert.deepEqual(sig.slice(2), csig.slice(2)) // equal signatures
})
var k1 = new ECKey(new BigInteger(f.D), false)
var s1 = Message.sign(k1, f.message, network)
assert.equal(s1.toString('base64'), f.signature)
it('supports alternate networks', function() {
var privKey = new ECKey(BigInteger.ONE)
var signature = Message.sign(privKey, message, networks.dogecoin)
if (f.compressed) {
var k2 = new ECKey(new BigInteger(f.D))
var s2 = Message.sign(k2, f.message)
assert.equal(signature.toString('base64'), 'H6k+dZwJ8oOei3PCSpdj603fDvhlhQ+sqaFNIDvo/bI+Xh6zyIKGzZpyud6YhZ1a5mcrwMVtTWL+VXq/hC5Zj7s=')
assert.equal(s2.toString('base64'), f.compressed.signature)
}
})
})
})
})

Loading…
Cancel
Save