Skip to content

Commit

Permalink
lnd: fix key derivation for good
Browse files Browse the repository at this point in the history
  • Loading branch information
guggero committed Mar 22, 2022
1 parent 34263d7 commit 3fd5218
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions lnd/hdkeychain.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func DeriveChildren(key *hdkeychain.ExtendedKey, path []uint32) (
*hdkeychain.ExtendedKey, error) {

var currentKey = key
for _, pathPart := range path {
for idx, pathPart := range path {
derivedKey, err := currentKey.DeriveNonStandard(pathPart)
if err != nil {
return nil, err
Expand All @@ -45,7 +45,11 @@ func DeriveChildren(key *hdkeychain.ExtendedKey, path []uint32) (
// derived directly.
depth := derivedKey.Depth()
keyID := pathPart - hdkeychain.HardenedKeyStart
if (depth == 3 && keyID != 0) || (depth == 2 && keyID != 0) {
nextID := uint32(0)
if depth == 2 && len(path) > 2 {
nextID = path[idx+1] - hdkeychain.HardenedKeyStart
}
if (depth == 2 && nextID != 0) || (depth == 3 && keyID != 0) {
currentKey, err = hdkeychain.NewKeyFromString(
derivedKey.String(),
)
Expand Down

0 comments on commit 3fd5218

Please sign in to comment.