Browse Source

fix: derive will error when a path does not start with master extended key (#20)

master
Matthew Stevens 7 years ago
committed by Ryan Zimmerman
parent
commit
4bb95824c7
  1. 2
      lib/hdkey.js
  2. 19
      test/hdkey.test.js

2
lib/hdkey.js

@ -78,7 +78,7 @@ HDKey.prototype.derive = function (path) {
var hdkey = this var hdkey = this
entries.forEach(function (c, i) { entries.forEach(function (c, i) {
if (i === 0) { if (i === 0) {
assert(c, 'm', 'Invalid path') assert(/^[mM]{1}/.test(c), 'Path must start with "m" or "M"')
return return
} }

19
test/hdkey.test.js

@ -192,4 +192,23 @@ describe('hdkey', function () {
assert.ok(!masterKey.privateExtendedKey, 'xpriv is falsy') assert.ok(!masterKey.privateExtendedKey, 'xpriv is falsy')
}) })
}) })
describe(' - when the path given to derive contains only the master extended key', function () {
const hdKeyInstance = HDKey.fromMasterSeed(Buffer.from(fixtures.valid[0].seed, 'hex'))
it('should return the same hdkey instance', function () {
assert.equal(hdKeyInstance.derive('m'), hdKeyInstance)
assert.equal(hdKeyInstance.derive('M'), hdKeyInstance)
assert.equal(hdKeyInstance.derive("m'"), hdKeyInstance)
assert.equal(hdKeyInstance.derive("M'"), hdKeyInstance)
})
})
describe(' - when the path given to derive does not begin with master extended key', function () {
it('should throw an error', function () {
assert.throws(function () {
HDKey.prototype.derive('123')
}, /Path must start with "m" or "M"/)
})
})
}) })

Loading…
Cancel
Save