Browse Source

Renaming Path to Bip32Path, invalid to fixtures

Only thing I haven't been able to do is to add "undefined" to fixtures. So I could not properly test
that node.derivePath() properly fails as it should. However, I added "null" there, and "null" and "undefined"
behave in similar way in JavaScript, so that should catch that.
hk-custom-address
Karel Bilek 9 years ago
parent
commit
6a74eb6993
  1. 2
      src/hdnode.js
  2. 4
      src/types.js
  3. 21
      test/fixtures/hdnode.json
  4. 75
      test/hdnode.js

2
src/hdnode.js

@ -292,7 +292,7 @@ HDNode.prototype.isNeutered = function () {
} }
HDNode.prototype.derivePath = function (path) { HDNode.prototype.derivePath = function (path) {
typeforce(types.Path, path) typeforce(types.Bip32Path, path)
var splitPath = path.split('/') var splitPath = path.split('/')
if (splitPath[0] === 'm') { if (splitPath[0] === 'm') {

4
src/types.js

@ -26,7 +26,7 @@ function UInt53 (value) {
Math.floor(value) === value Math.floor(value) === value
} }
function Path (value) { function Bip32Path (value) {
return typeforce.String(value) && return typeforce.String(value) &&
value.match(/^([m]\/)?([0-9]+[']?\/)*([0-9]+[']?)$/) value.match(/^([m]\/)?([0-9]+[']?\/)*([0-9]+[']?)$/)
} }
@ -63,7 +63,7 @@ var types = {
UInt31: UInt31, UInt31: UInt31,
UInt32: UInt32, UInt32: UInt32,
UInt53: UInt53, UInt53: UInt53,
Path: Path Bip32Path: Bip32Path
} }
for (var typeName in typeforce) { for (var typeName in typeforce) {

21
test/fixtures/hdnode.json

@ -232,6 +232,27 @@
"exception": "Point is not on the curve", "exception": "Point is not on the curve",
"hex": "0488b21e000000000000000000873dff81c02f525623fd1fe5167eac3a55a049de3d314bb42ee227ffed37d508020045400697100007000037899988826500030092003000016366806305909050" "hex": "0488b21e000000000000000000873dff81c02f525623fd1fe5167eac3a55a049de3d314bb42ee227ffed37d508020045400697100007000037899988826500030092003000016366806305909050"
} }
],
"deriveHardened": [
2147483648,
null,
"foo",
-1
],
"derive": [
4294967296,
null,
"foo",
-1
],
"derivePath": [
2,
[2, 3, 4],
"/",
"m/m/123",
"a/0/1/2",
"m/0/ 1 /2",
"m/0/1.5/2"
] ]
} }
} }

75
test/hdnode.js

@ -365,70 +365,27 @@ describe('HDNode', function () {
}, /Could not derive hardened child key/) }, /Could not derive hardened child key/)
}) })
it('throws on negative indexes', function () { it('throws on wrong types', function () {
var f = fixtures.valid[0]
var master = HDNode.fromBase58(f.master.base58, NETWORKS_LIST)
assert.throws(function () {
master.deriveHardened(-1)
}, /Expected UInt31/)
assert.throws(function () {
master.derive(-1)
}, /Expected UInt32/)
})
it('throws on high indexes', function () {
var f = fixtures.valid[0] var f = fixtures.valid[0]
var master = HDNode.fromBase58(f.master.base58, NETWORKS_LIST) var master = HDNode.fromBase58(f.master.base58, NETWORKS_LIST)
assert.throws(function () { fixtures.invalid.derive.forEach(function (fx) {
master.deriveHardened(0x80000000) assert.throws(function () {
}, /Expected UInt31/) master.derive(fx)
assert.throws(function () { }, /Expected UInt32/)
master.derive(0x100000000) })
}, /Expected UInt32/)
})
it('throws on wrong types', function () { fixtures.invalid.deriveHardened.forEach(function (fx) {
var f = fixtures.valid[0] assert.throws(function () {
var master = HDNode.fromBase58(f.master.base58, NETWORKS_LIST) master.deriveHardened(fx)
}, /Expected UInt31/)
})
assert.throws(function () { fixtures.invalid.derivePath.forEach(function (fx) {
master.deriveHardened() assert.throws(function () {
}, /Expected UInt31/) master.derivePath(fx)
assert.throws(function () { }, /Expected Bip32Path/)
master.derive() })
}, /Expected UInt32/)
assert.throws(function () {
master.deriveHardened('foo')
}, /Expected UInt31/)
assert.throws(function () {
master.derive('foo')
}, /Expected UInt32/)
assert.throws(function () {
master.derivePath()
}, /Expected Path/)
assert.throws(function () {
master.derivePath(2)
}, /Expected Path/)
assert.throws(function () {
master.derivePath([2, 3, 4])
}, /Expected Path/)
assert.throws(function () {
master.derivePath('/')
}, /Expected Path/)
assert.throws(function () {
master.derivePath('m/m/123')
}, /Expected Path/)
assert.throws(function () {
master.derivePath('a/0/1/2')
}, /Expected Path/)
assert.throws(function () {
master.derivePath('m/0/ 1 /2')
}, /Expected Path/)
assert.throws(function () {
master.derivePath('m/0/1.5/2')
}, /Expected Path/)
}) })
}) })
}) })

Loading…
Cancel
Save