From 6f6bf02e625a4c8a53ddbe171a6eabb629c8f1e1 Mon Sep 17 00:00:00 2001 From: Oliver Gugger Date: Wed, 9 Nov 2022 19:05:40 +0100 Subject: [PATCH] genimportscript: add addresses to bitcoin-cli-watchonly --- btc/bitcoind.go | 40 ++++++++++++++++++++++++-------- cmd/chantools/genimportscript.go | 10 +++++++- 2 files changed, 39 insertions(+), 11 deletions(-) diff --git a/btc/bitcoind.go b/btc/bitcoind.go index e884129..7fd78c2 100644 --- a/btc/bitcoind.go +++ b/btc/bitcoind.go @@ -29,22 +29,22 @@ type KeyExporter interface { // ParseFormat parses the given format name and returns its associated print // function. -func ParseFormat(format string) KeyExporter { +func ParseFormat(format string) (KeyExporter, error) { switch format { - default: - fallthrough - case FormatCli: - return &Cli{} + return &Cli{}, nil case FormatCliWatchOnly: - return &CliWatchOnly{} + return &CliWatchOnly{}, nil case FormatImportwallet: - return &ImportWallet{} + return &ImportWallet{}, nil case FormatElectrum: - return &Electrum{} + return &Electrum{}, nil + + default: + return nil, fmt.Errorf("invalid format: %s", format) } } @@ -166,12 +166,32 @@ func (c *CliWatchOnly) Format(hdKey *hdkeychain.ExtendedKey, if err != nil { return "", fmt.Errorf("could not derive private key: %w", err) } + + addrP2PKH, err := lnd.P2PKHAddr(pubKey, params) + if err != nil { + return "", fmt.Errorf("could not create address: %w", err) + } + addrP2WKH, err := lnd.P2WKHAddr(pubKey, params) + if err != nil { + return "", fmt.Errorf("could not create address: %w", err) + } + addrNP2WKH, err := lnd.NP2WKHAddr(pubKey, params) + if err != nil { + return "", fmt.Errorf("could not create address: %w", err) + } + addrP2TR, err := lnd.P2TRAddr(pubKey, params) + if err != nil { + return "", fmt.Errorf("could not create address: %w", err) + } + flags := "" if params.Net == wire.TestNet || params.Net == wire.TestNet3 { flags = " -testnet" } - return fmt.Sprintf("bitcoin-cli%s importpubkey %x \"%s/%d/%d/\" false", - flags, pubKey.SerializeCompressed(), path, branch, index), nil + return fmt.Sprintf("bitcoin-cli%s importpubkey %x \"%s/%d/%d/\" "+ + "false # addr=%s,%s,%s,%s", flags, pubKey.SerializeCompressed(), + path, branch, index, addrP2PKH, addrP2WKH, addrNP2WKH, + addrP2TR), nil } func (c *CliWatchOnly) Trailer(birthdayBlock uint32) string { diff --git a/cmd/chantools/genimportscript.go b/cmd/chantools/genimportscript.go index 49a4736..7e9cff5 100644 --- a/cmd/chantools/genimportscript.go +++ b/cmd/chantools/genimportscript.go @@ -124,6 +124,10 @@ func (c *genImportScriptCommand) Execute(_ *cobra.Command, _ []string) error { c.DerivationPath = lnd.WalletDefaultDerivationPath fallthrough + case c.DerivationPath == "-": + strPaths = []string{""} + paths = [][]uint32{{}} + case c.DerivationPath != "": derivationPath, err := lnd.ParsePath(c.DerivationPath) if err != nil { @@ -158,7 +162,11 @@ func (c *genImportScriptCommand) Execute(_ *cobra.Command, _ []string) error { } } - exporter := btc.ParseFormat(c.Format) + exporter, err := btc.ParseFormat(c.Format) + if err != nil { + return fmt.Errorf("error parsing format: %w", err) + } + err = btc.ExportKeys( extendedKey, strPaths, paths, chainParams, c.RecoveryWindow, c.RescanFrom, exporter, writer,