-
Notifications
You must be signed in to change notification settings - Fork 2
/
lnd.go
164 lines (138 loc) · 3.95 KB
/
lnd.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
package main
import (
"context"
"encoding/hex"
"github.com/hashicorp/golang-lru/v2"
"github.com/lightningnetwork/lnd/lnrpc"
"github.com/lightningnetwork/lnd/macaroons"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"gopkg.in/macaroon.v2"
"log"
"os"
"time"
)
type LndConfig struct {
Address string
CertFile string `yaml:"cert-file"`
MacaroonFile string `yaml:"macaroon-file"`
CacheSize uint16 `yaml:"cache-size"`
}
type Invoice struct {
paymentHash []byte
paymentRequest string
amount int64
settleDate time.Time
memo string
}
func (invoice *Invoice) getPaymentHash() string {
return hex.EncodeToString(invoice.paymentHash)
}
func (invoice *Invoice) isSettled() bool {
return !invoice.settleDate.IsZero()
}
type LndClient struct {
lnClient lnrpc.LightningClient
ctx context.Context
invoices *lru.Cache[string, Invoice]
}
func newLndClient(config LndConfig) *LndClient {
if config.CertFile == "" {
log.Fatal("LND certificate file missing")
}
if config.MacaroonFile == "" {
log.Fatal("LND macaroon file missing")
}
transportCredentials, err := credentials.NewClientTLSFromFile(config.CertFile, "")
if err != nil {
log.Fatal(err)
}
macaroonData, err := os.ReadFile(config.MacaroonFile)
if err != nil {
log.Fatal(err)
}
macaroonInstance := &macaroon.Macaroon{}
if err := macaroonInstance.UnmarshalBinary(macaroonData); err != nil {
log.Fatal(err)
}
macaroonCredentials, err := macaroons.NewMacaroonCredential(macaroonInstance)
if err != nil {
log.Fatal(err)
}
connection, err := grpc.Dial(config.Address,
grpc.WithTransportCredentials(transportCredentials),
grpc.WithPerRPCCredentials(macaroonCredentials),
)
if err != nil {
log.Fatal(err)
}
invoices, err := lru.New[string, Invoice](int(config.CacheSize))
if err != nil {
log.Fatal(err)
}
return &LndClient{
lnClient: lnrpc.NewLightningClient(connection),
ctx: context.Background(),
invoices: invoices,
}
}
func (client *LndClient) createInvoice(msats int64, memo string, descriptionHash []byte) (*Invoice, error) {
lnInvoice := lnrpc.Invoice{
Memo: memo,
DescriptionHash: descriptionHash,
ValueMsat: msats,
Expiry: 300,
}
newLnInvoice, err := client.lnClient.AddInvoice(client.ctx, &lnInvoice)
if err != nil {
return nil, err
}
return &Invoice{
paymentHash: newLnInvoice.RHash,
paymentRequest: newLnInvoice.PaymentRequest,
amount: msats / 1000,
}, nil
}
func (client *LndClient) getInvoice(paymentHash string) (*Invoice, error) {
if invoice, invoiceCached := client.invoices.Get(paymentHash); invoiceCached {
return &invoice, nil
}
paymentHashBytes, err := hex.DecodeString(paymentHash)
if err != nil {
return nil, err
}
lnPaymentHash := lnrpc.PaymentHash{RHash: paymentHashBytes}
lnInvoice, err := client.lnClient.LookupInvoice(client.ctx, &lnPaymentHash)
if err != nil {
return nil, err
}
var settleDate time.Time
if lnInvoice.State == lnrpc.Invoice_SETTLED {
settleDate = time.Unix(lnInvoice.SettleDate, 0)
}
invoice := Invoice{
paymentHash: lnInvoice.RHash,
paymentRequest: lnInvoice.PaymentRequest,
amount: lnInvoice.Value,
settleDate: settleDate,
memo: lnInvoice.Memo,
}
if lnInvoice.State == lnrpc.Invoice_SETTLED || lnInvoice.State == lnrpc.Invoice_CANCELED {
client.invoices.Add(paymentHash, invoice)
}
return &invoice, nil
}
func (client *LndClient) decodePaymentRequest(paymentRequest string) (string, int64) {
payReqString := lnrpc.PayReqString{PayReq: paymentRequest}
payReq, err := client.lnClient.DecodePayReq(client.ctx, &payReqString)
if err != nil {
log.Println("error decoding payment request:", err)
return "", 0
}
return payReq.PaymentHash, payReq.NumSatoshis
}
func (client *LndClient) sendPayment(paymentRequest string) error {
sendRequest := lnrpc.SendRequest{PaymentRequest: paymentRequest}
_, err := client.lnClient.SendPaymentSync(client.ctx, &sendRequest)
return err
}