From 5c39df02d30e3a275d8c083392506a7400256abf Mon Sep 17 00:00:00 2001 From: Oliver Gugger Date: Tue, 12 Mar 2024 11:29:55 +0100 Subject: [PATCH] cmd/chantools: allow root key to be read from wallet DB With this commit we allow a third option for reading the master root key for any command that requires access to it: Reading and decrypting it directly from an lnd wallet password. --- cmd/chantools/root.go | 44 +++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 42 insertions(+), 2 deletions(-) diff --git a/cmd/chantools/root.go b/cmd/chantools/root.go index 5dfae0e..1d70faa 100644 --- a/cmd/chantools/root.go +++ b/cmd/chantools/root.go @@ -140,8 +140,9 @@ func main() { } type rootKey struct { - RootKey string - BIP39 bool + RootKey string + BIP39 bool + WalletDB string } func newRootKey(cmd *cobra.Command, desc string) *rootKey { @@ -156,6 +157,12 @@ func newRootKey(cmd *cobra.Command, desc string) *rootKey { "passphrase from the terminal instead of asking for "+ "lnd seed format or providing the --rootkey flag", ) + cmd.Flags().StringVar( + &r.WalletDB, "walletdb", "", "read the seed/master root key "+ + "to use fro "+desc+" from an lnd wallet.db file "+ + "instead of asking for a seed or providing the "+ + "--rootkey flag", + ) return r } @@ -178,6 +185,39 @@ func (r *rootKey) readWithBirthday() (*hdkeychain.ExtendedKey, time.Time, extendedKey, err := btc.ReadMnemonicFromTerminal(chainParams) return extendedKey, time.Unix(0, 0), err + case r.WalletDB != "": + wallet, pw, cleanup, err := lnd.OpenWallet( + r.WalletDB, chainParams, + ) + if err != nil { + return nil, time.Unix(0, 0), fmt.Errorf("error "+ + "opening wallet '%s': %w", r.WalletDB, err) + } + + defer func() { + if err := cleanup(); err != nil { + log.Errorf("error closing wallet: %v", err) + } + }() + + extendedKeyBytes, err := lnd.DecryptWalletRootKey( + wallet.Database(), pw, + ) + if err != nil { + return nil, time.Unix(0, 0), fmt.Errorf("error "+ + "decrypting wallet root key: %w", err) + } + + extendedKey, err := hdkeychain.NewKeyFromString( + string(extendedKeyBytes), + ) + if err != nil { + return nil, time.Unix(0, 0), fmt.Errorf("error "+ + "parsing master key: %w", err) + } + + return extendedKey, wallet.Manager.Birthday(), nil + default: return lnd.ReadAezeed(chainParams) }