Skip to content

Commit

Permalink
lnd: replicate btcwallet's key derivation exactly
Browse files Browse the repository at this point in the history
There's this special case in lnd's wallet (btcwallet) where
the coin type and account keys are always serialized as a
string and encrypted, which actually fixes the key padding
issue that makes the difference between DeriveNonStandard and
Derive. To replicate lnd's behavior exactly, we need to
serialize and de-serialize the extended key at the coin type
and account level (depth = 2 or depth = 3). This does not
apply to the default account (id = 0) because that is always
derived directly.
  • Loading branch information
guggero committed Mar 15, 2022
1 parent d67675e commit af563bb
Showing 1 changed file with 24 additions and 5 deletions.
29 changes: 24 additions & 5 deletions lnd/hdkeychain.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,34 @@ const (
func DeriveChildren(key *hdkeychain.ExtendedKey, path []uint32) (
*hdkeychain.ExtendedKey, error) {

var (
currentKey = key
err error
)
var currentKey = key
for _, pathPart := range path {
currentKey, err = currentKey.DeriveNonStandard(pathPart)
derivedKey, err := currentKey.DeriveNonStandard(pathPart)
if err != nil {
return nil, err
}

// There's this special case in lnd's wallet (btcwallet) where
// the coin type and account keys are always serialized as a
// string and encrypted, which actually fixes the key padding
// issue that makes the difference between DeriveNonStandard and
// Derive. To replicate lnd's behavior exactly, we need to
// serialize and de-serialize the extended key at the coin type
// and account level (depth = 2 or depth = 3). This does not
// apply to the default account (id = 0) because that is always
// derived directly.
depth := derivedKey.Depth()
keyID := pathPart - hdkeychain.HardenedKeyStart
if (depth == 3 && keyID != 0) || depth == 2 {
currentKey, err = hdkeychain.NewKeyFromString(
derivedKey.String(),
)
if err != nil {
return nil, err
}
} else {
currentKey = derivedKey
}
}
return currentKey, nil
}
Expand Down

0 comments on commit af563bb

Please sign in to comment.