forked from getAlby/nostr-wallet-connect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lnd.go
387 lines (346 loc) · 12.3 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
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
package main
import (
"context"
"crypto/rand"
"crypto/sha256"
"encoding/hex"
"errors"
"sort"
"strings"
"time"
"github.com/getAlby/nostr-wallet-connect/lnd"
decodepay "github.com/nbd-wtf/ln-decodepay"
"github.com/sirupsen/logrus"
"gorm.io/gorm"
"github.com/labstack/echo-contrib/session"
"github.com/labstack/echo/v4"
"github.com/lightningnetwork/lnd/lnrpc"
)
type LNClient interface {
SendPaymentSync(ctx context.Context, senderPubkey string, payReq string) (preimage string, err error)
SendKeysend(ctx context.Context, senderPubkey string, amount int64, destination, preimage string, custom_records []TLVRecord) (preImage string, err error)
GetBalance(ctx context.Context, senderPubkey string) (balance int64, err error)
GetInfo(ctx context.Context, senderPubkey string) (info *NodeInfo, err error)
MakeInvoice(ctx context.Context, senderPubkey string, amount int64, description string, descriptionHash string, expiry int64) (transaction *Nip47Transaction, err error)
LookupInvoice(ctx context.Context, senderPubkey string, paymentHash string) (transaction *Nip47Transaction, err error)
ListTransactions(ctx context.Context, senderPubkey string, from, until, limit, offset uint64, unpaid bool, invoiceType string) (transactions []Nip47Transaction, err error)
}
// wrap it again :sweat_smile:
// todo: drop dependency on lndhub package
type LNDService struct {
client *lnd.LNDWrapper
db *gorm.DB
Logger *logrus.Logger
}
func (svc *LNDService) AuthHandler(c echo.Context) error {
user := &User{}
err := svc.db.FirstOrInit(user, User{AlbyIdentifier: "lnd"}).Error
if err != nil {
return err
}
sess, _ := session.Get(CookieName, c)
sess.Values["user_id"] = user.ID
sess.Save(c.Request(), c.Response())
return c.Redirect(302, "/")
}
func (svc *LNDService) GetBalance(ctx context.Context, senderPubkey string) (balance int64, err error) {
resp, err := svc.client.ChannelBalance(ctx, &lnrpc.ChannelBalanceRequest{})
if err != nil {
return 0, err
}
return int64(resp.LocalBalance.Sat), nil
}
func (svc *LNDService) ListTransactions(ctx context.Context, senderPubkey string, from, until, limit, offset uint64, unpaid bool, invoiceType string) (transactions []Nip47Transaction, err error) {
// Fetch invoices
var invoices []*lnrpc.Invoice
if invoiceType == "" || invoiceType == "incoming" {
incomingResp, err := svc.client.ListInvoices(ctx, &lnrpc.ListInvoiceRequest{Reversed: true, NumMaxInvoices: limit, IndexOffset: offset})
if err != nil {
return nil, err
}
invoices = incomingResp.Invoices
}
for _, invoice := range invoices {
// this will cause retrieved amount to be less than limit if unpaid is false
if !unpaid && invoice.State != lnrpc.Invoice_SETTLED {
continue
}
transaction := lndInvoiceToTransaction(invoice)
transactions = append(transactions, *transaction)
}
// Fetch payments
var payments []*lnrpc.Payment
if invoiceType == "" || invoiceType == "outgoing" {
// Not just pending but failed payments will also be included because of IncludeIncomplete
outgoingResp, err := svc.client.ListPayments(ctx, &lnrpc.ListPaymentsRequest{Reversed: true, MaxPayments: limit, IndexOffset: offset, IncludeIncomplete: unpaid})
if err != nil {
return nil, err
}
payments = outgoingResp.Payments
}
for _, payment := range payments {
if payment.Status == lnrpc.Payment_FAILED {
// don't return failed payments for now
// this will cause retrieved amount to be less than limit
continue
}
var paymentRequest decodepay.Bolt11
var expiresAt *int64
var description string
var descriptionHash string
if payment.PaymentRequest != "" {
paymentRequest, err = decodepay.Decodepay(strings.ToLower(payment.PaymentRequest))
if err != nil {
svc.Logger.WithFields(logrus.Fields{
"bolt11": payment.PaymentRequest,
}).Errorf("Failed to decode bolt11 invoice: %v", err)
return nil, err
}
expiresAtUnix := time.UnixMilli(int64(paymentRequest.CreatedAt) * 1000).Add(time.Duration(paymentRequest.Expiry) * time.Second).Unix()
expiresAt = &expiresAtUnix
description = paymentRequest.Description
descriptionHash = paymentRequest.DescriptionHash
}
var settledAt *int64
if payment.Status == lnrpc.Payment_SUCCEEDED {
// FIXME: how to get the actual settled at time?
settledAtUnix := time.Unix(0, payment.CreationTimeNs).Unix()
settledAt = &settledAtUnix
}
transaction := Nip47Transaction{
Type: "outgoing",
Invoice: payment.PaymentRequest,
Preimage: payment.PaymentPreimage,
PaymentHash: payment.PaymentHash,
Amount: payment.ValueMsat,
FeesPaid: payment.FeeMsat,
CreatedAt: time.Unix(0, payment.CreationTimeNs).Unix(),
Description: description,
DescriptionHash: descriptionHash,
ExpiresAt: expiresAt,
SettledAt: settledAt,
//TODO: Metadata: (e.g. keysend),
}
transactions = append(transactions, transaction)
}
// sort by created date descending
sort.SliceStable(transactions, func(i, j int) bool {
return transactions[i].CreatedAt > transactions[j].CreatedAt
})
return transactions, nil
}
func (svc *LNDService) GetInfo(ctx context.Context, senderPubkey string) (info *NodeInfo, err error) {
resp, err := svc.client.GetInfo(ctx, &lnrpc.GetInfoRequest{})
if err != nil {
return nil, err
}
return &NodeInfo{
Alias: resp.Alias,
Color: resp.Color,
Pubkey: resp.IdentityPubkey,
Network: resp.Chains[0].Network,
BlockHeight: resp.BlockHeight,
BlockHash: resp.BlockHash,
}, nil
}
func (svc *LNDService) MakeInvoice(ctx context.Context, senderPubkey string, amount int64, description string, descriptionHash string, expiry int64) (transaction *Nip47Transaction, err error) {
var descriptionHashBytes []byte
if descriptionHash != "" {
descriptionHashBytes, err = hex.DecodeString(descriptionHash)
if err != nil || len(descriptionHashBytes) != 32 {
svc.Logger.WithFields(logrus.Fields{
"senderPubkey": senderPubkey,
"amount": amount,
"description": description,
"descriptionHash": descriptionHash,
"expiry": expiry,
}).Errorf("Invalid description hash")
return nil, errors.New("Description hash must be 32 bytes hex")
}
}
resp, err := svc.client.AddInvoice(ctx, &lnrpc.Invoice{ValueMsat: amount, Memo: description, DescriptionHash: descriptionHashBytes, Expiry: expiry})
if err != nil {
return nil, err
}
inv, err := svc.client.LookupInvoice(ctx, &lnrpc.PaymentHash{RHash: resp.RHash})
if err != nil {
return nil, err
}
transaction = lndInvoiceToTransaction(inv)
return transaction, nil
}
func (svc *LNDService) LookupInvoice(ctx context.Context, senderPubkey string, paymentHash string) (transaction *Nip47Transaction, err error) {
paymentHashBytes, err := hex.DecodeString(paymentHash)
if err != nil || len(paymentHashBytes) != 32 {
svc.Logger.WithFields(logrus.Fields{
"paymentHash": paymentHash,
}).Errorf("Invalid payment hash")
return nil, errors.New("Payment hash must be 32 bytes hex")
}
lndInvoice, err := svc.client.LookupInvoice(ctx, &lnrpc.PaymentHash{RHash: paymentHashBytes})
if err != nil {
return nil, err
}
transaction = lndInvoiceToTransaction(lndInvoice)
return transaction, nil
}
func (svc *LNDService) SendPaymentSync(ctx context.Context, senderPubkey, payReq string) (preimage string, err error) {
resp, err := svc.client.SendPaymentSync(ctx, &lnrpc.SendRequest{PaymentRequest: payReq})
if err != nil {
return "", err
}
return hex.EncodeToString(resp.PaymentPreimage), nil
}
func (svc *LNDService) SendKeysend(ctx context.Context, senderPubkey string, amount int64, destination, preimage string, custom_records []TLVRecord) (respPreimage string, err error) {
destBytes, err := hex.DecodeString(destination)
if err != nil {
return "", err
}
var preImageBytes []byte
if preimage == "" {
preImageBytes, err = makePreimageHex()
preimage = hex.EncodeToString(preImageBytes)
} else {
preImageBytes, err = hex.DecodeString(preimage)
}
if err != nil || len(preImageBytes) != 32 {
svc.Logger.WithFields(logrus.Fields{
"senderPubkey": senderPubkey,
"amount": amount,
"destination": destination,
"preimage": preimage,
"customRecords": custom_records,
"error": err,
}).Errorf("Invalid preimage")
return "", err
}
paymentHash := sha256.New()
paymentHash.Write(preImageBytes)
paymentHashBytes := paymentHash.Sum(nil)
paymentHashHex := hex.EncodeToString(paymentHashBytes)
destCustomRecords := map[uint64][]byte{}
for _, record := range custom_records {
destCustomRecords[record.Type] = []byte(record.Value)
}
const KEYSEND_CUSTOM_RECORD = 5482373484
destCustomRecords[KEYSEND_CUSTOM_RECORD] = preImageBytes
sendPaymentRequest := &lnrpc.SendRequest{
Dest: destBytes,
Amt: amount,
PaymentHash: paymentHashBytes,
DestFeatures: []lnrpc.FeatureBit{lnrpc.FeatureBit_TLV_ONION_REQ},
DestCustomRecords: destCustomRecords,
}
resp, err := svc.client.SendPaymentSync(ctx, sendPaymentRequest)
if err != nil {
svc.Logger.WithFields(logrus.Fields{
"senderPubkey": senderPubkey,
"amount": amount,
"payeePubkey": destination,
"paymentHash": paymentHashHex,
"preimage": preimage,
"customRecords": custom_records,
"error": err,
}).Errorf("Failed to send keysend payment")
return "", err
}
if resp.PaymentError != "" {
svc.Logger.WithFields(logrus.Fields{
"senderPubkey": senderPubkey,
"amount": amount,
"payeePubkey": destination,
"paymentHash": paymentHashHex,
"preimage": preimage,
"customRecords": custom_records,
"paymentError": resp.PaymentError,
}).Errorf("Keysend payment has payment error")
return "", errors.New(resp.PaymentError)
}
respPreimage = hex.EncodeToString(resp.PaymentPreimage)
if respPreimage == "" {
svc.Logger.WithFields(logrus.Fields{
"senderPubkey": senderPubkey,
"amount": amount,
"payeePubkey": destination,
"paymentHash": paymentHashHex,
"preimage": preimage,
"customRecords": custom_records,
"paymentError": resp.PaymentError,
}).Errorf("No preimage in keysend response")
return "", errors.New("No preimage in keysend response")
}
svc.Logger.WithFields(logrus.Fields{
"senderPubkey": senderPubkey,
"amount": amount,
"payeePubkey": destination,
"paymentHash": paymentHashHex,
"preimage": preimage,
"customRecords": custom_records,
"respPreimage": respPreimage,
}).Info("Keysend payment successful")
return respPreimage, nil
}
func makePreimageHex() ([]byte, error) {
bytes := make([]byte, 32) // 32 bytes * 8 bits/byte = 256 bits
_, err := rand.Read(bytes)
if err != nil {
return nil, err
}
return bytes, nil
}
func NewLNDService(ctx context.Context, svc *Service, e *echo.Echo) (result *LNDService, err error) {
lndClient, err := lnd.NewLNDclient(lnd.LNDoptions{
Address: svc.cfg.LNDAddress,
CertFile: svc.cfg.LNDCertFile,
MacaroonFile: svc.cfg.LNDMacaroonFile,
}, ctx)
if err != nil {
return nil, err
}
info, err := lndClient.GetInfo(ctx, &lnrpc.GetInfoRequest{})
if err != nil {
return nil, err
}
//add default user to db
user := &User{}
err = svc.db.FirstOrInit(user, User{AlbyIdentifier: "lnd"}).Error
if err != nil {
return nil, err
}
err = svc.db.Save(user).Error
if err != nil {
return nil, err
}
lndService := &LNDService{client: lndClient, Logger: svc.Logger, db: svc.db}
e.GET("/lnd/auth", lndService.AuthHandler)
svc.Logger.Infof("Connected to LND - alias %s", info.Alias)
return lndService, nil
}
func lndInvoiceToTransaction(invoice *lnrpc.Invoice) *Nip47Transaction {
var settledAt *int64
var preimage string
if invoice.State == lnrpc.Invoice_SETTLED {
settledAt = &invoice.SettleDate
// only set preimage if invoice is settled
preimage = hex.EncodeToString(invoice.RPreimage)
}
var expiresAt *int64
if invoice.Expiry > 0 {
expiresAtUnix := invoice.CreationDate + invoice.Expiry
expiresAt = &expiresAtUnix
}
return &Nip47Transaction{
Type: "incoming",
Invoice: invoice.PaymentRequest,
Description: invoice.Memo,
DescriptionHash: hex.EncodeToString(invoice.DescriptionHash),
Preimage: preimage,
PaymentHash: hex.EncodeToString(invoice.RHash),
Amount: invoice.ValueMsat,
FeesPaid: invoice.AmtPaidMsat,
CreatedAt: invoice.CreationDate,
SettledAt: settledAt,
ExpiresAt: expiresAt,
// TODO: Metadata (e.g. keysend)
}
}