Adding path derivation

The argument is path of either numbers or strings.

String with "'" at the end signifies hardened path.
This commit is contained in:
Karel Bilek 2016-02-06 02:01:20 +01:00
parent b3b239739b
commit 7defe6fe4c

View file

@ -291,6 +291,30 @@ HDNode.prototype.isNeutered = function () {
return !(this.keyPair.d) return !(this.keyPair.d)
} }
HDNode.prototype.derivePath = function (path) {
typeforce(types.String, path)
var splitPath = path.split('/')
if (splitPath[0] === 'm') {
if (this.parentFingerprint) {
throw new Error('Not a master node')
}
splitPath = splitPath.slice(1)
}
return splitPath.reduce(function (prevHd, indexStr) {
var index
if (indexStr.slice(-1) === "'") {
index = +(indexStr.slice(0, -1))
return prevHd.deriveHardened(index)
} else {
index = +indexStr
return prevHd.derive(index)
}
}, this)
}
HDNode.prototype.toString = HDNode.prototype.toBase58 HDNode.prototype.toString = HDNode.prototype.toBase58
module.exports = HDNode module.exports = HDNode