Skip to content

Commit 7defe6f

Browse files
committed
Adding path derivation
The argument is path of either numbers or strings. String with "'" at the end signifies hardened path.
1 parent b3b2397 commit 7defe6f

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

src/hdnode.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,30 @@ HDNode.prototype.isNeutered = function () {
291291
return !(this.keyPair.d)
292292
}
293293

294+
HDNode.prototype.derivePath = function (path) {
295+
typeforce(types.String, path)
296+
297+
var splitPath = path.split('/')
298+
if (splitPath[0] === 'm') {
299+
if (this.parentFingerprint) {
300+
throw new Error('Not a master node')
301+
}
302+
303+
splitPath = splitPath.slice(1)
304+
}
305+
306+
return splitPath.reduce(function (prevHd, indexStr) {
307+
var index
308+
if (indexStr.slice(-1) === "'") {
309+
index = +(indexStr.slice(0, -1))
310+
return prevHd.deriveHardened(index)
311+
} else {
312+
index = +indexStr
313+
return prevHd.derive(index)
314+
}
315+
}, this)
316+
}
317+
294318
HDNode.prototype.toString = HDNode.prototype.toBase58
295319

296320
module.exports = HDNode

0 commit comments

Comments
 (0)