Browse Source

ECKey: fix exception tests

These weren't broken as such, but they weren't distinctly checking that
the right exception was thrown either.
hk-custom-address
Daniel Cousens 11 years ago
parent
commit
7494a146a6
  1. 2
      src/eckey.js
  2. 14
      test/eckey.js
  3. 12
      test/fixtures/eckey.json

2
src/eckey.js

@ -27,7 +27,7 @@ ECKey.fromWIF = function(string) {
var compressed = false var compressed = false
if (payload.length === 33) { if (payload.length === 33) {
assert.strictEqual(payload[32], 0x01, 'Invalid WIF string') assert.strictEqual(payload[32], 0x01, 'Invalid compression flag')
payload = payload.slice(0, -1) payload = payload.slice(0, -1)
compressed = true compressed = true

14
test/eckey.js

@ -21,8 +21,8 @@ describe('ECKey', function() {
assert.equal(privKey.pub.compressed, false) assert.equal(privKey.pub.compressed, false)
}) })
it('calculates the matching pubKey', function() {
fixtures.valid.forEach(function(f) { fixtures.valid.forEach(function(f) {
it('calculates the matching pubKey for ' + f.D, function() {
var privKey = new ECKey(new BigInteger(f.D)) var privKey = new ECKey(new BigInteger(f.D))
assert.equal(privKey.pub.Q.toString(), f.Q.toString()) assert.equal(privKey.pub.Q.toString(), f.Q.toString())
@ -30,18 +30,18 @@ describe('ECKey', function() {
}) })
fixtures.invalid.constructor.forEach(function(f) { fixtures.invalid.constructor.forEach(function(f) {
it('throws on ' + f.description, function() { it('throws on ' + f.D, function() {
assert.throws(function() { assert.throws(function() {
new ECKey(new BigInteger(f.D)) new ECKey(new BigInteger(f.D))
}) }, new RegExp(f.exception))
}) })
}) })
}) })
describe('fromWIF', function() { describe('fromWIF', function() {
it('matches the test vectors', function() {
fixtures.valid.forEach(function(f) { fixtures.valid.forEach(function(f) {
f.WIFs.forEach(function(wif) { f.WIFs.forEach(function(wif) {
it('imports ' + wif.string + ' correctly', function() {
var privKey = ECKey.fromWIF(wif.string) var privKey = ECKey.fromWIF(wif.string)
assert.equal(privKey.D.toString(), f.D) assert.equal(privKey.D.toString(), f.D)
@ -51,18 +51,18 @@ describe('ECKey', function() {
}) })
fixtures.invalid.WIF.forEach(function(f) { fixtures.invalid.WIF.forEach(function(f) {
it('throws on ' + f.description, function() { it('throws on ' + f.string, function() {
assert.throws(function() { assert.throws(function() {
ECKey.fromWIF(f.string) ECKey.fromWIF(f.string)
}) }, new RegExp(f.exception))
}) })
}) })
}) })
describe('toWIF', function() { describe('toWIF', function() {
it('matches the test vectors', function() {
fixtures.valid.forEach(function(f) { fixtures.valid.forEach(function(f) {
f.WIFs.forEach(function(wif) { f.WIFs.forEach(function(wif) {
it('exports ' + wif.string + ' correctly', function() {
var privKey = ECKey.fromWIF(wif.string) var privKey = ECKey.fromWIF(wif.string)
var version = networks[wif.network].wif var version = networks[wif.network].wif
var result = privKey.toWIF(version) var result = privKey.toWIF(version)

12
test/fixtures/eckey.json

@ -68,29 +68,29 @@
"invalid": { "invalid": {
"constructor": [ "constructor": [
{ {
"description": "Private key ZERO", "exception": "Private key must be greater than 0",
"D": "0" "D": "0"
}, },
{ {
"description": "Private key equal to the curve order", "exception": "Private key must be less than the curve order",
"D": "115792089237316195423570985008687907852837564279074904382605163141518161494337" "D": "115792089237316195423570985008687907852837564279074904382605163141518161494337"
}, },
{ {
"description": "Private key greater than the curve order", "exception": "Private key must be less than the curve order",
"D": "115792089237316195423570985008687907853269984665640564039457584007913129639935" "D": "115792089237316195423570985008687907853269984665640564039457584007913129639935"
} }
], ],
"WIF": [ "WIF": [
{ {
"description": "Invalid compression flag", "exception": "Invalid compression flag",
"string": "ju9rooVsmagsb4qmNyTysUSFB1GB6MdpD7eoGjUTPmZRAApJxRz" "string": "ju9rooVsmagsb4qmNyTysUSFB1GB6MdpD7eoGjUTPmZRAApJxRz"
}, },
{ {
"description": "Payload too short", "exception": "Invalid WIF payload length",
"string": "7ZEtRQLhCsDQrd6ZKfmcESdXgas8ggZPN24ByEi5ey6VJW" "string": "7ZEtRQLhCsDQrd6ZKfmcESdXgas8ggZPN24ByEi5ey6VJW"
}, },
{ {
"description": "Payload too long", "exception": "Invalid WIF payload length",
"string": "5qibUKwsnMo1qDiNp3prGaQkD2JfVJa8F8Na87H2CkMHvuVg6uKhw67Rh" "string": "5qibUKwsnMo1qDiNp3prGaQkD2JfVJa8F8Na87H2CkMHvuVg6uKhw67Rh"
} }
] ]

Loading…
Cancel
Save