diff --git a/lndclient/lightning_client.go b/lndclient/lightning_client.go index 8a09871..6eeb3fb 100644 --- a/lndclient/lightning_client.go +++ b/lndclient/lightning_client.go @@ -1,6 +1,7 @@ package lndclient import ( + "bytes" "context" "encoding/hex" "errors" @@ -9,13 +10,13 @@ import ( "time" "github.com/btcsuite/btcd/chaincfg" + "github.com/btcsuite/btcd/wire" "github.com/btcsuite/btcutil" "github.com/lightningnetwork/lnd/channeldb" "github.com/lightningnetwork/lnd/lnrpc" "github.com/lightningnetwork/lnd/lnrpc/invoicesrpc" "github.com/lightningnetwork/lnd/lntypes" "github.com/lightningnetwork/lnd/zpay32" - "google.golang.org/grpc" "google.golang.org/grpc/codes" "google.golang.org/grpc/status" @@ -36,6 +37,10 @@ type LightningClient interface { AddInvoice(ctx context.Context, in *invoicesrpc.AddInvoiceData) ( lntypes.Hash, string, error) + + // ListTransactions returns all known transactions of the backing lnd + // node. + ListTransactions(ctx context.Context) ([]*wire.MsgTx, error) } // Info contains info about the connected lnd node. @@ -350,3 +355,32 @@ func (s *lightningClient) AddInvoice(ctx context.Context, return hash, resp.PaymentRequest, nil } + +// ListTransactions returns all known transactions of the backing lnd node. +func (s *lightningClient) ListTransactions(ctx context.Context) ([]*wire.MsgTx, error) { + rpcCtx, cancel := context.WithTimeout(ctx, rpcTimeout) + defer cancel() + + rpcCtx = s.adminMac.WithMacaroonAuth(rpcCtx) + rpcIn := &lnrpc.GetTransactionsRequest{} + resp, err := s.client.GetTransactions(rpcCtx, rpcIn) + if err != nil { + return nil, err + } + + txs := make([]*wire.MsgTx, 0, len(resp.Transactions)) + for _, respTx := range resp.Transactions { + rawTx, err := hex.DecodeString(respTx.RawTxHex) + if err != nil { + return nil, err + } + + var tx wire.MsgTx + if err := tx.Deserialize(bytes.NewReader(rawTx)); err != nil { + return nil, err + } + txs = append(txs, &tx) + } + + return txs, nil +} diff --git a/test/lightning_client_mock.go b/test/lightning_client_mock.go index afa39e1..ec030e0 100644 --- a/test/lightning_client_mock.go +++ b/test/lightning_client_mock.go @@ -8,6 +8,7 @@ import ( "time" "github.com/btcsuite/btcd/btcec" + "github.com/btcsuite/btcd/wire" "github.com/btcsuite/btcutil" "github.com/lightninglabs/loop/lndclient" "github.com/lightningnetwork/lnd/lnrpc/invoicesrpc" @@ -126,3 +127,13 @@ func (h *mockLightningClient) AddInvoice(ctx context.Context, return hash, payReqString, nil } + +// ListTransactions returns all known transactions of the backing lnd node. +func (h *mockLightningClient) ListTransactions( + ctx context.Context) ([]*wire.MsgTx, error) { + + h.lnd.lock.Lock() + txs := h.lnd.Transactions + h.lnd.lock.Unlock() + return txs, nil +} diff --git a/test/lnd_services_mock.go b/test/lnd_services_mock.go index a40aa66..0687bb6 100644 --- a/test/lnd_services_mock.go +++ b/test/lnd_services_mock.go @@ -135,6 +135,8 @@ type LndMockServices struct { Signature []byte SignatureMsg string + Transactions []*wire.MsgTx + WaitForFinished func() lock sync.Mutex @@ -152,6 +154,13 @@ func (s *LndMockServices) NotifyHeight(height int32) error { return nil } +// AddRelevantTx marks the given transaction as relevant. +func (s *LndMockServices) AddTx(tx *wire.MsgTx) { + s.lock.Lock() + s.Transactions = append(s.Transactions, tx.Copy()) + s.lock.Unlock() +} + // IsDone checks whether all channels have been fully emptied. If not this may // indicate unexpected behaviour of the code under test. func (s *LndMockServices) IsDone() error { diff --git a/test/walletkit_mock.go b/test/walletkit_mock.go index 6a557ad..5581fdf 100644 --- a/test/walletkit_mock.go +++ b/test/walletkit_mock.go @@ -60,6 +60,7 @@ func (m *mockWalletKit) NextAddr(ctx context.Context) (btcutil.Address, error) { } func (m *mockWalletKit) PublishTransaction(ctx context.Context, tx *wire.MsgTx) error { + m.lnd.AddTx(tx) m.lnd.TxPublishChannel <- tx return nil } @@ -84,6 +85,7 @@ func (m *mockWalletKit) SendOutputs(ctx context.Context, outputs []*wire.TxOut, }) } + m.lnd.AddTx(&tx) m.lnd.SendOutputsChannel <- tx return &tx, nil