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:
parent
b3b239739b
commit
7defe6fe4c
1 changed files with 24 additions and 0 deletions
|
@ -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
|
||||
|
|
Loading…
Add table
Reference in a new issue