forked from lnproxy/lnproxy-relay
/
lightning_client_mock.go
279 lines (224 loc) · 7.16 KB
/
lightning_client_mock.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
package test
import (
"crypto/rand"
"encoding/hex"
"fmt"
"sync"
"time"
"github.com/btcsuite/btcd/btcec/v2"
"github.com/btcsuite/btcd/btcec/v2/ecdsa"
"github.com/btcsuite/btcd/btcutil"
"github.com/btcsuite/btcd/wire"
"github.com/lightninglabs/lndclient"
"github.com/lightningnetwork/lnd/invoices"
"github.com/lightningnetwork/lnd/lnrpc/invoicesrpc"
"github.com/lightningnetwork/lnd/lntypes"
"github.com/lightningnetwork/lnd/routing/route"
"github.com/lightningnetwork/lnd/zpay32"
"golang.org/x/net/context"
)
type mockLightningClient struct {
lndclient.LightningClient
lnd *LndMockServices
wg sync.WaitGroup
}
var _ lndclient.LightningClient = (*mockLightningClient)(nil)
// PayInvoice pays an invoice.
func (h *mockLightningClient) PayInvoice(ctx context.Context, invoice string,
maxFee btcutil.Amount,
outgoingChannel *uint64) chan lndclient.PaymentResult {
done := make(chan lndclient.PaymentResult, 1)
h.lnd.SendPaymentChannel <- PaymentChannelMessage{
PaymentRequest: invoice,
Done: done,
}
return done
}
func (h *mockLightningClient) WaitForFinished() {
h.wg.Wait()
}
func (h *mockLightningClient) ConfirmedWalletBalance(ctx context.Context) (
btcutil.Amount, error) {
return 1000000, nil
}
func (h *mockLightningClient) GetInfo(ctx context.Context) (*lndclient.Info,
error) {
pubKeyBytes, err := hex.DecodeString(h.lnd.NodePubkey)
if err != nil {
return nil, err
}
var pubKey [33]byte
copy(pubKey[:], pubKeyBytes)
return &lndclient.Info{
BlockHeight: 600,
IdentityPubkey: pubKey,
Uris: []string{h.lnd.NodePubkey + "@127.0.0.1:9735"},
}, nil
}
func (h *mockLightningClient) EstimateFeeToP2WSH(ctx context.Context,
amt btcutil.Amount, confTarget int32) (btcutil.Amount,
error) {
return 3000, nil
}
func (h *mockLightningClient) AddInvoice(ctx context.Context,
in *invoicesrpc.AddInvoiceData) (lntypes.Hash, string, error) {
h.lnd.lock.Lock()
defer h.lnd.lock.Unlock()
var hash lntypes.Hash
switch {
case in.Hash != nil:
hash = *in.Hash
case in.Preimage != nil:
hash = (*in.Preimage).Hash()
default:
if _, err := rand.Read(hash[:]); err != nil {
return lntypes.Hash{}, "", err
}
}
// Create and encode the payment request as a bech32 (zpay32) string.
creationDate := time.Now()
payReq, err := zpay32.NewInvoice(
h.lnd.ChainParams, hash, creationDate,
zpay32.Description(in.Memo),
zpay32.CLTVExpiry(in.CltvExpiry),
zpay32.Amount(in.Value),
)
if err != nil {
return lntypes.Hash{}, "", err
}
privKey, err := btcec.NewPrivateKey()
if err != nil {
return lntypes.Hash{}, "", err
}
payReqString, err := payReq.Encode(
zpay32.MessageSigner{
SignCompact: func(hash []byte) ([]byte, error) {
// ecdsa.SignCompact returns a
// pubkey-recoverable signature.
sig, err := ecdsa.SignCompact(
privKey, hash, true,
)
if err != nil {
return nil, fmt.Errorf("can't sign "+
"the hash: %v", err)
}
return sig, nil
},
},
)
if err != nil {
return lntypes.Hash{}, "", err
}
// Add the invoice we have created to our mock's set of invoices.
h.lnd.Invoices[hash] = &lndclient.Invoice{
Preimage: nil,
Hash: hash,
PaymentRequest: payReqString,
Amount: in.Value,
CreationDate: creationDate,
State: invoices.ContractOpen,
IsKeysend: false,
}
return hash, payReqString, nil
}
// LookupInvoice looks up an invoice in the mock's set of stored invoices.
// If it is not found, this call will fail. Note that these invoices should
// be settled using settleInvoice to have a preimage, settled state and settled
// date set.
func (h *mockLightningClient) LookupInvoice(_ context.Context,
hash lntypes.Hash) (*lndclient.Invoice, error) {
h.lnd.lock.Lock()
defer h.lnd.lock.Unlock()
inv, ok := h.lnd.Invoices[hash]
if !ok {
return nil, fmt.Errorf("invoice: %x not found", hash)
}
return inv, nil
}
// ListTransactions returns all known transactions of the backing lnd node.
func (h *mockLightningClient) ListTransactions(
_ context.Context, _ int32, _ int32,
_ ...lndclient.ListTransactionsOption) ([]lndclient.Transaction, error) {
h.lnd.lock.Lock()
txs := h.lnd.Transactions
h.lnd.lock.Unlock()
return txs, nil
}
// ListChannels retrieves all channels of the backing lnd node.
func (h *mockLightningClient) ListChannels(ctx context.Context,
activeOnly, publicOnly bool) ([]lndclient.ChannelInfo, error) {
return h.lnd.Channels, nil
}
// ClosedChannels returns a list of our closed channels.
func (h *mockLightningClient) ClosedChannels(
_ context.Context) ([]lndclient.ClosedChannel, error) {
return h.lnd.ClosedChannels, nil
}
// PendingChannels returns a list of lnd's pending channels.
func (h *mockLightningClient) PendingChannels(_ context.Context) (
*lndclient.PendingChannels, error) {
return nil, nil
}
// ForwardingHistory returns the mock's set of forwarding events.
func (h *mockLightningClient) ForwardingHistory(_ context.Context,
_ lndclient.ForwardingHistoryRequest) (*lndclient.ForwardingHistoryResponse,
error) {
return &lndclient.ForwardingHistoryResponse{
LastIndexOffset: 0,
Events: h.lnd.ForwardingEvents,
}, nil
}
// ListInvoices returns our mock's invoices.
func (h *mockLightningClient) ListInvoices(_ context.Context,
_ lndclient.ListInvoicesRequest) (*lndclient.ListInvoicesResponse,
error) {
invoices := make([]lndclient.Invoice, 0, len(h.lnd.Invoices))
for _, invoice := range h.lnd.Invoices {
invoices = append(invoices, *invoice)
}
return &lndclient.ListInvoicesResponse{
Invoices: invoices,
}, nil
}
// ListPayments makes a paginated call to our list payments endpoint.
func (h *mockLightningClient) ListPayments(_ context.Context,
_ lndclient.ListPaymentsRequest) (*lndclient.ListPaymentsResponse,
error) {
return &lndclient.ListPaymentsResponse{
Payments: h.lnd.Payments,
}, nil
}
// ChannelBackup retrieves the backup for a particular channel. The
// backup is returned as an encrypted chanbackup.Single payload.
func (h *mockLightningClient) ChannelBackup(context.Context, wire.OutPoint) ([]byte, error) {
return nil, nil
}
// ChannelBackups retrieves backups for all existing pending open and
// open channels. The backups are returned as an encrypted
// chanbackup.Multi payload.
func (h *mockLightningClient) ChannelBackups(ctx context.Context) ([]byte, error) {
return nil, nil
}
// DecodePaymentRequest decodes a payment request.
func (h *mockLightningClient) DecodePaymentRequest(_ context.Context,
_ string) (*lndclient.PaymentRequest, error) {
return nil, nil
}
// OpenChannel opens a channel to the peer provided with the amounts
// specified.
func (h *mockLightningClient) OpenChannel(_ context.Context, _ route.Vertex,
_ btcutil.Amount, _ btcutil.Amount, _ bool,
_ ...lndclient.OpenChannelOption) (*wire.OutPoint, error) {
return nil, nil
}
// CloseChannel closes the channel provided.
func (h *mockLightningClient) CloseChannel(_ context.Context, _ *wire.OutPoint,
_ bool, _ int32, _ btcutil.Address) (chan lndclient.CloseChannelUpdate,
chan error, error) {
return nil, nil, nil
}
// Connect attempts to connect to a peer at the host specified.
func (h *mockLightningClient) Connect(_ context.Context, _ route.Vertex,
_ string, _ bool) error {
return nil
}