Skip to content

Commit

Permalink
recoverloopin: add sqlite option
Browse files Browse the repository at this point in the history
This commit will allow to recover loop ins that have been made with
sqlite.
  • Loading branch information
sputn1ck committed Jan 11, 2024
1 parent 21a9ad5 commit 551a8e2
Showing 1 changed file with 57 additions and 9 deletions.
66 changes: 57 additions & 9 deletions cmd/chantools/recoverloopin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -30,7 +33,8 @@ type recoverLoopInCommand struct {
APIURL string
Publish bool

LoopDbDir string
LoopDbDir string
SqliteFile string

rootKey *rootKey
cmd *cobra.Command
Expand Down Expand Up @@ -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")

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 551a8e2

Please sign in to comment.