-
Notifications
You must be signed in to change notification settings - Fork 3
/
lnclient.go
639 lines (593 loc) · 17.3 KB
/
lnclient.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
package lightning
import (
"bytes"
"context"
"encoding/hex"
"errors"
"fmt"
"github.com/btcsuite/btcd/blockchain"
"github.com/btcsuite/btcd/chaincfg"
"github.com/lightningnetwork/lnd/lnrpc/invoicesrpc"
"runtime"
/* "github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcwallet/wallet/txrules"
"github.com/btcsuite/btcwallet/wallet/txsizes"*/
"io/ioutil"
"net"
"strings"
"github.com/btcsuite/btcd/wire"
"github.com/btcsuite/btcutil"
"github.com/lightningnetwork/lnd/lnrpc/signrpc"
"github.com/btcsuite/btcd/txscript"
"github.com/lightningnetwork/lnd/lnrpc/walletrpc"
"github.com/tendermint/tendermint/libs/log"
"github.com/lightningnetwork/lnd/lnrpc"
"github.com/lightningnetwork/lnd/lncfg"
"github.com/lightningnetwork/lnd/macaroons"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
macaroon "gopkg.in/macaroon.v2"
)
type LnClient struct {
ServerHostPort string
TlsPath string
MacPath string
MinConfs int64
TargetConfs int64
Logger log.Logger
Testnet bool
WalletAddress string
WalletPass string
LastFee int64
HashPrice int64
SessionSecret string
}
var (
maxMsgRecvSize = grpc.MaxCallRecvMsgSize(1 * 1024 * 1024 * 200)
)
// LoggerError : Log error if it exists using a logger
func (ln *LnClient) LoggerError(err error) error {
if err != nil {
ln.Logger.Error(fmt.Sprintf("Error in %s: %s", GetCurrentFuncName(2), err.Error()))
}
return err
}
// GetCurrentFuncName : get name of function being called
func GetCurrentFuncName(numCallStack int) string {
pc, _, _, _ := runtime.Caller(numCallStack)
return fmt.Sprintf("%s", runtime.FuncForPC(pc).Name())
}
func (ln *LnClient) GetClient() (lnrpc.LightningClient, func(), error) {
conn, err := ln.CreateConn()
closeIt := func() {
conn.Close()
}
if ln.LoggerError(err) != nil {
return nil, nil, err
}
return lnrpc.NewLightningClient(conn), closeIt, nil
}
func (ln *LnClient) GetWalletUnlockerClient() (lnrpc.WalletUnlockerClient, func(), error) {
conn, err := ln.CreateConn()
closeIt := func() {
conn.Close()
}
if ln.LoggerError(err) != nil {
return nil, nil, err
}
return lnrpc.NewWalletUnlockerClient(conn), closeIt, nil
}
func (ln *LnClient) GetWalletClient() (walletrpc.WalletKitClient, func(), error) {
conn, err := ln.CreateConn()
closeIt := func() {
conn.Close()
}
if ln.LoggerError(err) != nil {
return nil, nil, err
}
return walletrpc.NewWalletKitClient(conn), closeIt, nil
}
func (ln *LnClient) GetInvoiceClient() (invoicesrpc.InvoicesClient, func(), error) {
conn, err := ln.CreateConn()
closeIt := func() {
conn.Close()
}
if ln.LoggerError(err) != nil {
return nil, nil, err
}
return invoicesrpc.NewInvoicesClient(conn), closeIt, nil
}
func (ln *LnClient) Unlocker() error {
conn, close, err := ln.GetWalletUnlockerClient()
defer close()
if err != nil {
return err
}
unlockReq := lnrpc.UnlockWalletRequest{
WalletPassword: []byte(ln.WalletPass),
RecoveryWindow: 10000,
ChannelBackups: nil,
XXX_NoUnkeyedLiteral: struct{}{},
XXX_unrecognized: nil,
XXX_sizecache: 0,
}
_, err = conn.UnlockWallet(context.Background(), &unlockReq)
if err != nil {
if strings.Contains(err.Error(), "unknown service lnrpc.WalletUnlocker") {
return nil
}
return ln.LoggerError(err)
}
return nil
}
func CreateClient(serverHostPort string, tlsPath string, macPath string) LnClient {
return LnClient{
ServerHostPort: serverHostPort,
TlsPath: tlsPath,
MacPath: macPath,
}
}
func IsLnUri(uri string) bool {
peerParts := strings.Split(uri, "@")
if len(peerParts) != 2 {
return false
}
if _, err := hex.DecodeString(peerParts[0]); err != nil {
return false
}
if _, _, err := net.SplitHostPort(peerParts[1]); err != nil {
return false
}
return true
}
func GetIpFromUri(uri string) string {
peerParts := strings.Split(uri, "@")
if len(peerParts) != 2 {
return ""
}
addrPort := peerParts[1]
ipArr := strings.Split(addrPort, ":")
if len(ipArr) != 2 {
return ""
}
return ipArr[0]
}
func (ln *LnClient) GetInfo() (*lnrpc.GetInfoResponse, error) {
client, closeFunc, err := ln.GetClient()
if ln.LoggerError(err) != nil {
return nil, err
}
defer closeFunc()
resp, err := client.GetInfo(context.Background(), &lnrpc.GetInfoRequest{})
return resp, err
}
func (ln *LnClient) GetWalletBalance() (*lnrpc.WalletBalanceResponse, error) {
client, closeFunc, err := ln.GetClient()
if ln.LoggerError(err) != nil {
return nil, err
}
defer closeFunc()
resp, err := client.WalletBalance(context.Background(), &lnrpc.WalletBalanceRequest{})
return resp, err
}
func (ln *LnClient) GetTransaction(id []byte) (lnrpc.TransactionDetails, error) {
client, closeFunc, err := ln.GetClient()
if ln.LoggerError(err) != nil {
return lnrpc.TransactionDetails{}, err
}
defer closeFunc()
txResponse, err := client.GetTransactions(context.Background(), &lnrpc.GetTransactionsRequest{
Txid: id,
})
if ln.LoggerError(err) != nil {
return lnrpc.TransactionDetails{}, err
}
return *txResponse, nil
}
func (ln *LnClient) GetBlockByHeight(height int64) (lnrpc.BlockDetails, error) {
client, closeFunc, err := ln.GetClient()
if ln.LoggerError(err) != nil {
return lnrpc.BlockDetails{}, err
}
defer closeFunc()
block, err := client.GetBlock(context.Background(), &lnrpc.GetBlockRequest{BlockHeight: uint32(height)})
if ln.LoggerError(err) != nil {
return lnrpc.BlockDetails{}, err
}
return *block, nil
}
func (ln *LnClient) GetBlockByHash(hash string) (lnrpc.BlockDetails, error) {
client, closeFunc, err := ln.GetClient()
if ln.LoggerError(err) != nil {
return lnrpc.BlockDetails{}, err
}
defer closeFunc()
block, err := client.GetBlock(context.Background(), &lnrpc.GetBlockRequest{BlockHash: hash})
if ln.LoggerError(err) != nil {
return lnrpc.BlockDetails{}, err
}
return *block, nil
}
func (ln *LnClient) PeerExists(peer string) (bool, error) {
peerParts := strings.Split(peer, "@")
if len(peerParts) != 2 {
return false, errors.New("Malformed peer string (must be pubKey@host)")
}
pubKey := peerParts[0]
addr := peerParts[1]
client, closeFunc, err := ln.GetClient()
if ln.LoggerError(err) != nil {
return false, err
}
defer closeFunc()
peers, err := client.ListPeers(context.Background(), &lnrpc.ListPeersRequest{})
if err != nil {
return false, err
}
for _, peer := range peers.Peers {
if peer.PubKey == pubKey && peer.Address == addr {
return true, nil
}
}
return false, nil
}
func (ln *LnClient) AddPeer(peer string) error {
peerParts := strings.Split(peer, "@")
if len(peerParts) != 2 {
return errors.New("Malformed peer string (must be pubKey@host)")
}
peerAddr := lnrpc.LightningAddress{
Pubkey: peerParts[0],
Host: peerParts[1],
}
connectPeer := lnrpc.ConnectPeerRequest{
Addr: &peerAddr,
}
client, closeFunc, err := ln.GetClient()
if ln.LoggerError(err) != nil {
return err
}
defer closeFunc()
_, err = client.ConnectPeer(context.Background(), &connectPeer)
if err != nil {
return err
}
return nil
}
func (ln *LnClient) ChannelExists(peer string, satVal int64) (bool, error) {
peerParts := strings.Split(peer, "@")
if len(peerParts) != 2 {
return false, errors.New("Malformed peer string (must be pubKey@host)")
}
remotePubkey := peerParts[0]
channels, err := ln.GetChannels()
if ln.LoggerError(err) != nil {
return false, err
}
for _, chann := range channels.Channels {
if chann.RemotePubkey == remotePubkey {
ln.Logger.Info("Channel found")
if chann.Capacity >= satVal {
ln.Logger.Info("Funding is correct value ", "Capacity", chann.Capacity)
return true, nil
}
}
}
pending, err := ln.GetPendingChannels()
if ln.LoggerError(err) != nil {
return false, err
}
for _, chann := range pending.PendingOpenChannels {
if chann.Channel.RemoteNodePub == remotePubkey {
ln.Logger.Info("Pending Channel found")
if chann.Channel.Capacity >= satVal {
ln.Logger.Info("Funding is correct value ", "Capacity", chann.Channel.Capacity)
return true, nil
}
}
}
return false, nil
}
func (ln *LnClient) OurChannelOpenAndFunded(peer string, satVal int64) (bool, error) {
peerParts := strings.Split(peer, "@")
if len(peerParts) != 2 {
return false, errors.New("Malformed peer string (must be pubKey@host)")
}
remotePubkey := peerParts[0]
channels, err := ln.GetChannels()
if ln.LoggerError(err) != nil {
return false, err
}
for _, chann := range channels.Channels {
if chann.RemotePubkey == remotePubkey {
ln.Logger.Info("Channel found")
if chann.Capacity >= satVal {
ln.Logger.Info("Funding is correct value ", "Capacity", chann.Capacity)
return true, nil
}
}
}
return false, nil
}
func (ln *LnClient) RemoteChannelOpenAndFunded(peer string, satVal int64) (bool, error) {
peerParts := strings.Split(peer, "@")
if len(peerParts) != 2 {
return false, errors.New("Malformed peer string (must be pubKey@host)")
}
remotePubkey := peerParts[0]
channels, err := ln.GetChannels()
if ln.LoggerError(err) != nil {
return false, err
}
for _, chann := range channels.Channels {
if chann.RemotePubkey == remotePubkey {
ln.Logger.Info("Channel found")
if chann.Capacity >= satVal {
ln.Logger.Info("Funding is correct value ", "Capacity", chann.Capacity)
return true, nil
}
}
}
return false, nil
}
func (ln *LnClient) GetChannels() (*lnrpc.ListChannelsResponse, error) {
client, closeFunc, err := ln.GetClient()
if ln.LoggerError(err) != nil {
return nil, err
}
defer closeFunc()
channels, err := client.ListChannels(context.Background(), &lnrpc.ListChannelsRequest{})
return channels, err
}
func (ln *LnClient) GetPendingChannels() (*lnrpc.PendingChannelsResponse, error) {
client, closeFunc, err := ln.GetClient()
if ln.LoggerError(err) != nil {
return nil, err
}
defer closeFunc()
channels, err := client.PendingChannels(context.Background(), &lnrpc.PendingChannelsRequest{})
return channels, err
}
func (ln *LnClient) CreateChannel(peer string, satVal int64) (lnrpc.Lightning_OpenChannelClient, error) {
peerParts := strings.Split(peer, "@")
if len(peerParts) != 2 {
return nil, errors.New("Malformed peer string (must be pubKey@host)")
}
pubKey, err := hex.DecodeString(peerParts[0])
if ln.LoggerError(err) != nil {
return nil, err
}
openSesame := lnrpc.OpenChannelRequest{
NodePubkey: pubKey,
}
openSesame.LocalFundingAmount = satVal
if ln.MinConfs != 0 {
openSesame.MinConfs = int32(ln.MinConfs)
}
if ln.TargetConfs != 0 {
openSesame.TargetConf = int32(ln.TargetConfs)
}
client, closeFunc, err := ln.GetClient()
if ln.LoggerError(err) != nil {
return nil, err
}
defer closeFunc()
resp, err := client.OpenChannel(context.Background(), &openSesame)
if ln.LoggerError(err) != nil {
return nil, err
}
return resp, nil
}
func (ln *LnClient) CreateConn() (*grpc.ClientConn, error) {
// Load the specified TLS certificate and build transport credentials
// with it.
creds, err := credentials.NewClientTLSFromFile(ln.TlsPath, "")
if ln.LoggerError(err) != nil {
return nil, err
}
// Create a dial options array.
opts := []grpc.DialOption{
grpc.WithTransportCredentials(creds),
}
macBytes, err := ioutil.ReadFile(ln.MacPath)
if ln.LoggerError(err) != nil {
return nil, err
}
mac := &macaroon.Macaroon{}
if err = mac.UnmarshalBinary(macBytes); err != nil {
return nil, err
}
macConstraints := []macaroons.Constraint{
macaroons.TimeoutConstraint(60),
}
// Apply constraints to the macaroon.
constrainedMac, err := macaroons.AddConstraints(mac, macConstraints...)
if ln.LoggerError(err) != nil {
return nil, err
}
// Now we append the macaroon credentials to the dial options.
cred := macaroons.NewMacaroonCredential(constrainedMac)
opts = append(opts, grpc.WithPerRPCCredentials(cred))
// We need to use a custom dialer so we can also connect to unix sockets
// and not just TCP addresses.
hostPortArr := strings.Split(ln.ServerHostPort, ":")
defaultRPCPort := "10009"
if len(hostPortArr) > 1 {
defaultRPCPort = hostPortArr[1]
}
genericDialer := lncfg.ClientAddressDialer(defaultRPCPort)
opts = append(opts, grpc.WithContextDialer(genericDialer))
opts = append(opts, grpc.WithDefaultCallOptions(maxMsgRecvSize))
conn, err := grpc.Dial(ln.ServerHostPort, opts...)
if ln.LoggerError(err) != nil {
return nil, err
}
return conn, nil
}
func (ln *LnClient) feeSatByteToWeight() int64 {
return int64(ln.LastFee * 1000 / blockchain.WitnessScaleFactor)
}
func (ln *LnClient) GetLndFeeEstimate() (int64, error) {
wallet, closeFunc, err := ln.GetWalletClient()
defer closeFunc()
fee, err := wallet.EstimateFee(context.Background(), &walletrpc.EstimateFeeRequest{ConfTarget: 2})
if err != nil {
return 0, err
}
if fee.SatPerKw == 12500 {
return fee.SatPerKw, errors.New("static fee has been returned")
}
return fee.SatPerKw, nil
}
func (ln *LnClient) AnchorData(hash []byte) (string, string, error) {
b := txscript.NewScriptBuilder()
b.AddOp(txscript.OP_RETURN)
b.AddData(hash)
outputScript, err := b.Script()
if ln.LoggerError(err) != nil {
return "", "", err
}
wallet, closeFunc, err := ln.GetWalletClient()
defer closeFunc()
ln.Logger.Info("Ln Wallet client created")
outputs := []*signrpc.TxOut{
{
Value: 0,
PkScript: outputScript,
},
}
ln.Logger.Info(fmt.Sprintf("Sending Outputs: %v", outputs))
ln.Logger.Info(fmt.Sprintf("Anchoring with FEE: %d", ln.LastFee))
outputRequest := walletrpc.SendOutputsRequest{SatPerKw: ln.LastFee, Outputs: outputs}
resp, err := wallet.SendOutputs(context.Background(), &outputRequest)
if ln.LoggerError(err) != nil {
return "", "", err
}
tx, err := btcutil.NewTxFromBytes(resp.RawTx)
if ln.LoggerError(err) != nil {
return "", "", err
}
ln.Logger.Info(fmt.Sprintf("Ln SendOutputs Response: %+v", resp))
var msgTx wire.MsgTx
if ln.LoggerError(msgTx.BtcDecode(bytes.NewReader(resp.RawTx), 0, wire.WitnessEncoding)); err != nil {
return "", "", err
}
buf := bytes.NewBuffer(make([]byte, 0, msgTx.SerializeSizeStripped()))
if ln.LoggerError(msgTx.SerializeNoWitness(buf)); err != nil {
return "", "", err
}
return tx.Hash().String(), hex.EncodeToString(buf.Bytes()), nil
}
func (ln *LnClient) SendCoins(addr string, amt int64, confs int32) (lnrpc.SendCoinsResponse, error) {
wallet, closeWalletFunc, err := ln.GetWalletClient()
if ln.LoggerError(err) != nil {
return lnrpc.SendCoinsResponse{}, err
}
defer closeWalletFunc()
estimatedFee, err := wallet.EstimateFee(context.Background(), &walletrpc.EstimateFeeRequest{ConfTarget: 2})
if err != nil {
return lnrpc.SendCoinsResponse{}, err
}
client, closeFunc, err := ln.GetClient()
defer closeFunc()
sendCoinsReq := lnrpc.SendCoinsRequest{
Addr: addr,
Amount: amt,
TargetConf: confs,
SatPerByte: estimatedFee.SatPerKw,
}
resp, err := client.SendCoins(context.Background(), &sendCoinsReq)
ln.LoggerError(err)
return *resp, err
}
func (ln *LnClient) LookupInvoice(payhash []byte) (lnrpc.Invoice, error) {
lightning, close, err := ln.GetClient()
if ln.LoggerError(err) != nil {
return lnrpc.Invoice{}, err
}
defer close()
invoice, err := lightning.LookupInvoice(context.Background(), &lnrpc.PaymentHash{RHash: payhash})
if ln.LoggerError(err) != nil {
return lnrpc.Invoice{}, err
}
return *invoice, nil
}
func (ln *LnClient) ReplaceByFee(txid string, OPRETURNIndex bool, newfee int) (walletrpc.BumpFeeResponse, error) {
wallet, close, err := ln.GetWalletClient()
if ln.LoggerError(err) != nil {
return walletrpc.BumpFeeResponse{}, err
}
defer close()
decodedId, err := hex.DecodeString(txid)
if err != nil {
return walletrpc.BumpFeeResponse{}, err
}
tx, err := ln.GetTransaction(decodedId)
if err != nil {
return walletrpc.BumpFeeResponse{}, err
}
if len(tx.Transactions) == 0 {
return walletrpc.BumpFeeResponse{}, errors.New("no transaction found")
}
rawTxHex := tx.GetTransactions()[0].RawTxHex
decodedTx, err := hex.DecodeString(rawTxHex)
if err != nil {
return walletrpc.BumpFeeResponse{}, err
}
var msgTx wire.MsgTx
if ln.LoggerError(msgTx.BtcDecode(bytes.NewReader(decodedTx), 0, wire.WitnessEncoding)); err != nil {
ln.Logger.Info("RBF Decoding for tx output failed")
return walletrpc.BumpFeeResponse{}, err
}
chainParam := chaincfg.Params{}
if ln.Testnet {
chainParam = chaincfg.TestNet3Params
} else {
chainParam = chaincfg.MainNetParams
}
var outputIndex uint32
for i, txOut := range msgTx.TxOut {
script, _, _, err := txscript.ExtractPkScriptAddrs(
txOut.PkScript, &chainParam,
)
ln.Logger.Info(fmt.Sprintf("Extracted script %s from output %d", script.String(), i))
if ln.LoggerError(err) != nil {
continue
}
if OPRETURNIndex {
if script.String() == txscript.NullDataTy.String() {
ln.Logger.Info("Selected OP_RETURN output")
outputIndex = uint32(i)
break
}
} else {
if script.String() != txscript.NullDataTy.String() {
ln.Logger.Info("Selected Payment output")
outputIndex = uint32(i)
break
}
}
}
txIdHash := (msgTx).TxHash()
txIdBytes := txIdHash.CloneBytes()
outpoint := lnrpc.OutPoint{
TxidBytes: txIdBytes,
OutputIndex: outputIndex,
XXX_NoUnkeyedLiteral: struct{}{},
XXX_unrecognized: nil,
XXX_sizecache: 0,
}
rbfReq := walletrpc.BumpFeeRequest{
Outpoint: &outpoint,
TargetConf: 0,
SatPerByte: uint32(newfee),
XXX_NoUnkeyedLiteral: struct{}{},
XXX_unrecognized: nil,
XXX_sizecache: 0,
}
resp, err := wallet.BumpFee(context.Background(), &rbfReq)
if ln.LoggerError(err) != nil {
return walletrpc.BumpFeeResponse{}, err
}
return *resp, nil
}