diff --git a/src/hdnode.js b/src/hdnode.js index 62cac56..a18fb98 100644 --- a/src/hdnode.js +++ b/src/hdnode.js @@ -291,6 +291,30 @@ HDNode.prototype.isNeutered = function () { 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 module.exports = HDNode