Skip to content

Commit 1ea58ad

Browse files
authored
Merge pull request #125 from wpaulino/lndclient-listtransactions
lndclient: expose ListTransactions as part of LightningClient
2 parents 518ec24 + 43579b0 commit 1ea58ad

File tree

4 files changed

+57
-1
lines changed

4 files changed

+57
-1
lines changed

lndclient/lightning_client.go

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package lndclient
22

33
import (
4+
"bytes"
45
"context"
56
"encoding/hex"
67
"errors"
@@ -9,13 +10,13 @@ import (
910
"time"
1011

1112
"github.com/btcsuite/btcd/chaincfg"
13+
"github.com/btcsuite/btcd/wire"
1214
"github.com/btcsuite/btcutil"
1315
"github.com/lightningnetwork/lnd/channeldb"
1416
"github.com/lightningnetwork/lnd/lnrpc"
1517
"github.com/lightningnetwork/lnd/lnrpc/invoicesrpc"
1618
"github.com/lightningnetwork/lnd/lntypes"
1719
"github.com/lightningnetwork/lnd/zpay32"
18-
1920
"google.golang.org/grpc"
2021
"google.golang.org/grpc/codes"
2122
"google.golang.org/grpc/status"
@@ -36,6 +37,10 @@ type LightningClient interface {
3637

3738
AddInvoice(ctx context.Context, in *invoicesrpc.AddInvoiceData) (
3839
lntypes.Hash, string, error)
40+
41+
// ListTransactions returns all known transactions of the backing lnd
42+
// node.
43+
ListTransactions(ctx context.Context) ([]*wire.MsgTx, error)
3944
}
4045

4146
// Info contains info about the connected lnd node.
@@ -350,3 +355,32 @@ func (s *lightningClient) AddInvoice(ctx context.Context,
350355

351356
return hash, resp.PaymentRequest, nil
352357
}
358+
359+
// ListTransactions returns all known transactions of the backing lnd node.
360+
func (s *lightningClient) ListTransactions(ctx context.Context) ([]*wire.MsgTx, error) {
361+
rpcCtx, cancel := context.WithTimeout(ctx, rpcTimeout)
362+
defer cancel()
363+
364+
rpcCtx = s.adminMac.WithMacaroonAuth(rpcCtx)
365+
rpcIn := &lnrpc.GetTransactionsRequest{}
366+
resp, err := s.client.GetTransactions(rpcCtx, rpcIn)
367+
if err != nil {
368+
return nil, err
369+
}
370+
371+
txs := make([]*wire.MsgTx, 0, len(resp.Transactions))
372+
for _, respTx := range resp.Transactions {
373+
rawTx, err := hex.DecodeString(respTx.RawTxHex)
374+
if err != nil {
375+
return nil, err
376+
}
377+
378+
var tx wire.MsgTx
379+
if err := tx.Deserialize(bytes.NewReader(rawTx)); err != nil {
380+
return nil, err
381+
}
382+
txs = append(txs, &tx)
383+
}
384+
385+
return txs, nil
386+
}

test/lightning_client_mock.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"time"
99

1010
"github.com/btcsuite/btcd/btcec"
11+
"github.com/btcsuite/btcd/wire"
1112
"github.com/btcsuite/btcutil"
1213
"github.com/lightninglabs/loop/lndclient"
1314
"github.com/lightningnetwork/lnd/lnrpc/invoicesrpc"
@@ -126,3 +127,13 @@ func (h *mockLightningClient) AddInvoice(ctx context.Context,
126127

127128
return hash, payReqString, nil
128129
}
130+
131+
// ListTransactions returns all known transactions of the backing lnd node.
132+
func (h *mockLightningClient) ListTransactions(
133+
ctx context.Context) ([]*wire.MsgTx, error) {
134+
135+
h.lnd.lock.Lock()
136+
txs := h.lnd.Transactions
137+
h.lnd.lock.Unlock()
138+
return txs, nil
139+
}

test/lnd_services_mock.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,8 @@ type LndMockServices struct {
135135
Signature []byte
136136
SignatureMsg string
137137

138+
Transactions []*wire.MsgTx
139+
138140
WaitForFinished func()
139141

140142
lock sync.Mutex
@@ -152,6 +154,13 @@ func (s *LndMockServices) NotifyHeight(height int32) error {
152154
return nil
153155
}
154156

157+
// AddRelevantTx marks the given transaction as relevant.
158+
func (s *LndMockServices) AddTx(tx *wire.MsgTx) {
159+
s.lock.Lock()
160+
s.Transactions = append(s.Transactions, tx.Copy())
161+
s.lock.Unlock()
162+
}
163+
155164
// IsDone checks whether all channels have been fully emptied. If not this may
156165
// indicate unexpected behaviour of the code under test.
157166
func (s *LndMockServices) IsDone() error {

test/walletkit_mock.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ func (m *mockWalletKit) NextAddr(ctx context.Context) (btcutil.Address, error) {
6060
}
6161

6262
func (m *mockWalletKit) PublishTransaction(ctx context.Context, tx *wire.MsgTx) error {
63+
m.lnd.AddTx(tx)
6364
m.lnd.TxPublishChannel <- tx
6465
return nil
6566
}
@@ -84,6 +85,7 @@ func (m *mockWalletKit) SendOutputs(ctx context.Context, outputs []*wire.TxOut,
8485
})
8586
}
8687

88+
m.lnd.AddTx(&tx)
8789
m.lnd.SendOutputsChannel <- tx
8890

8991
return &tx, nil

0 commit comments

Comments
 (0)