From 551a8e2e66c8535084f9ffe8cbb14503128404a6 Mon Sep 17 00:00:00 2001 From: sputn1ck Date: Thu, 11 Jan 2024 22:35:02 +0100 Subject: [PATCH] recoverloopin: add sqlite option This commit will allow to recover loop ins that have been made with sqlite. --- cmd/chantools/recoverloopin.go | 66 +++++++++++++++++++++++++++++----- 1 file changed, 57 insertions(+), 9 deletions(-) diff --git a/cmd/chantools/recoverloopin.go b/cmd/chantools/recoverloopin.go index 8c372a9..adb2db5 100644 --- a/cmd/chantools/recoverloopin.go +++ b/cmd/chantools/recoverloopin.go @@ -2,8 +2,11 @@ package main import ( "bytes" + "context" "encoding/hex" "fmt" + "path/filepath" + "time" "github.com/btcsuite/btcd/chaincfg/chainhash" "github.com/btcsuite/btcd/txscript" @@ -30,7 +33,8 @@ type recoverLoopInCommand struct { APIURL string Publish bool - LoopDbDir string + LoopDbDir string + SqliteFile string rootKey *rootKey cmd *cobra.Command @@ -92,6 +96,10 @@ func newRecoverLoopInCommand() *cobra.Command { &cc.Publish, "publish", false, "publish sweep TX to the chain "+ "API instead of just printing the TX", ) + cc.cmd.Flags().StringVar( + &cc.SqliteFile, "sqlite_file", "", "optional path to the loop "+ + "sqlite database file", + ) cc.rootKey = newRootKey(cc.cmd, "deriving starting key") @@ -125,32 +133,53 @@ func (c *recoverLoopInCommand) Execute(_ *cobra.Command, _ []string) error { } api := newExplorerAPI(c.APIURL) + ctx, cancel := context.WithTimeout(context.Background(), time.Second*30) + defer cancel() + signer := &lnd.Signer{ ExtendedKey: extendedKey, ChainParams: chainParams, } - // Try to fetch the swap from the database. + // Try to fetch the swap from the boltdb. store, err := loopdb.NewBoltSwapStore(c.LoopDbDir, chainParams) if err != nil { return err } defer store.Close() - swaps, err := store.FetchLoopInSwaps() + loopIn, err := findLoopInSwap(ctx, store, c.SwapHash) if err != nil { return err } - var loopIn *loopdb.LoopIn - for _, s := range swaps { - if s.Hash.String() == c.SwapHash { - loopIn = s - break + // If the loopin is not found yet, try to fetch it from the sqlite db. + if loopIn == nil { + if c.SqliteFile == "" { + c.SqliteFile = filepath.Join( + c.LoopDbDir, "loop_sqlite.db", + ) + } + + sqliteDb, err := loopdb.NewSqliteStore( + &loopdb.SqliteConfig{ + DatabaseFileName: c.SqliteFile, + SkipMigrations: true, + }, chainParams) + if err != nil { + return err + } + defer sqliteDb.Close() + + loopIn, err = findLoopInSwap(ctx, sqliteDb, c.SwapHash) + if err != nil { + return err } } + + // If the loopin is still not found, return an error. if loopIn == nil { - return fmt.Errorf("swap not found") + return fmt.Errorf("loop in swap not found") } fmt.Println("Loop expires at block height", loopIn.Contract.CltvExpiry) @@ -331,6 +360,25 @@ func getSignedTx(signer *lnd.Signer, loopIn *loopdb.LoopIn, sweepTx *wire.MsgTx, return rawTx, nil } +func findLoopInSwap(ctx context.Context, store loopdb.SwapStore, + swapHash string) (*loopdb.LoopIn, error) { + + swaps, err := store.FetchLoopInSwaps(ctx) + if err != nil { + return nil, err + } + + var loopIn *loopdb.LoopIn + for _, s := range swaps { + if s.Hash.String() == swapHash { + loopIn = s + break + } + } + + return loopIn, nil +} + // encodeTx encodes a tx to raw bytes. func encodeTx(tx *wire.MsgTx) ([]byte, error) { var buffer bytes.Buffer