Skip to content

Commit 4c4536a

Browse files
committed
lnrpc: move invoice marshall code to package
As a preparation for reusing the marshall code in the invoices sub server.
1 parent acb0162 commit 4c4536a

File tree

2 files changed

+133
-110
lines changed

2 files changed

+133
-110
lines changed

Diff for: lnrpc/invoicesrpc/utils.go

+119
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
package invoicesrpc
2+
3+
import (
4+
"encoding/hex"
5+
"fmt"
6+
7+
"github.com/btcsuite/btcd/chaincfg"
8+
"github.com/lightningnetwork/lnd/channeldb"
9+
"github.com/lightningnetwork/lnd/lnrpc"
10+
"github.com/lightningnetwork/lnd/routing"
11+
"github.com/lightningnetwork/lnd/zpay32"
12+
)
13+
14+
// CreateRPCInvoice creates an *lnrpc.Invoice from the *channeldb.Invoice.
15+
func CreateRPCInvoice(invoice *channeldb.Invoice,
16+
activeNetParams *chaincfg.Params) (*lnrpc.Invoice, error) {
17+
18+
paymentRequest := string(invoice.PaymentRequest)
19+
decoded, err := zpay32.Decode(paymentRequest, activeNetParams)
20+
if err != nil {
21+
return nil, fmt.Errorf("unable to decode payment request: %v",
22+
err)
23+
}
24+
25+
var descHash []byte
26+
if decoded.DescriptionHash != nil {
27+
descHash = decoded.DescriptionHash[:]
28+
}
29+
30+
fallbackAddr := ""
31+
if decoded.FallbackAddr != nil {
32+
fallbackAddr = decoded.FallbackAddr.String()
33+
}
34+
35+
settleDate := int64(0)
36+
if !invoice.SettleDate.IsZero() {
37+
settleDate = invoice.SettleDate.Unix()
38+
}
39+
40+
// Expiry time will default to 3600 seconds if not specified
41+
// explicitly.
42+
expiry := int64(decoded.Expiry().Seconds())
43+
44+
// The expiry will default to 9 blocks if not specified explicitly.
45+
cltvExpiry := decoded.MinFinalCLTVExpiry()
46+
47+
// Convert between the `lnrpc` and `routing` types.
48+
routeHints := CreateRPCRouteHints(decoded.RouteHints)
49+
50+
preimage := invoice.Terms.PaymentPreimage
51+
satAmt := invoice.Terms.Value.ToSatoshis()
52+
satAmtPaid := invoice.AmtPaid.ToSatoshis()
53+
54+
isSettled := invoice.Terms.State == channeldb.ContractSettled
55+
56+
var state lnrpc.Invoice_InvoiceState
57+
switch invoice.Terms.State {
58+
case channeldb.ContractOpen:
59+
state = lnrpc.Invoice_OPEN
60+
case channeldb.ContractSettled:
61+
state = lnrpc.Invoice_SETTLED
62+
default:
63+
return nil, fmt.Errorf("unknown invoice state")
64+
}
65+
66+
return &lnrpc.Invoice{
67+
Memo: string(invoice.Memo[:]),
68+
Receipt: invoice.Receipt[:],
69+
RHash: decoded.PaymentHash[:],
70+
RPreimage: preimage[:],
71+
Value: int64(satAmt),
72+
CreationDate: invoice.CreationDate.Unix(),
73+
SettleDate: settleDate,
74+
Settled: isSettled,
75+
PaymentRequest: paymentRequest,
76+
DescriptionHash: descHash,
77+
Expiry: expiry,
78+
CltvExpiry: cltvExpiry,
79+
FallbackAddr: fallbackAddr,
80+
RouteHints: routeHints,
81+
AddIndex: invoice.AddIndex,
82+
Private: len(routeHints) > 0,
83+
SettleIndex: invoice.SettleIndex,
84+
AmtPaidSat: int64(satAmtPaid),
85+
AmtPaidMsat: int64(invoice.AmtPaid),
86+
AmtPaid: int64(invoice.AmtPaid),
87+
State: state,
88+
}, nil
89+
}
90+
91+
// CreateRPCRouteHints takes in the decoded form of an invoice's route hints
92+
// and converts them into the lnrpc type.
93+
func CreateRPCRouteHints(routeHints [][]routing.HopHint) []*lnrpc.RouteHint {
94+
var res []*lnrpc.RouteHint
95+
96+
for _, route := range routeHints {
97+
hopHints := make([]*lnrpc.HopHint, 0, len(route))
98+
for _, hop := range route {
99+
pubKey := hex.EncodeToString(
100+
hop.NodeID.SerializeCompressed(),
101+
)
102+
103+
hint := &lnrpc.HopHint{
104+
NodeId: pubKey,
105+
ChanId: hop.ChannelID,
106+
FeeBaseMsat: hop.FeeBaseMSat,
107+
FeeProportionalMillionths: hop.FeeProportionalMillionths,
108+
CltvExpiryDelta: uint32(hop.CLTVExpiryDelta),
109+
}
110+
111+
hopHints = append(hopHints, hint)
112+
}
113+
114+
routeHint := &lnrpc.RouteHint{HopHints: hopHints}
115+
res = append(res, routeHint)
116+
}
117+
118+
return res
119+
}

Diff for: rpcserver.go

+14-110
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import (
3535
"github.com/lightningnetwork/lnd/invoices"
3636
"github.com/lightningnetwork/lnd/lncfg"
3737
"github.com/lightningnetwork/lnd/lnrpc"
38+
"github.com/lightningnetwork/lnd/lnrpc/invoicesrpc"
3839
"github.com/lightningnetwork/lnd/lnwallet"
3940
"github.com/lightningnetwork/lnd/lnwire"
4041
"github.com/lightningnetwork/lnd/macaroons"
@@ -3332,111 +3333,6 @@ func (r *rpcServer) AddInvoice(ctx context.Context,
33323333
}, nil
33333334
}
33343335

3335-
// createRPCInvoice creates an *lnrpc.Invoice from the *channeldb.Invoice.
3336-
func createRPCInvoice(invoice *channeldb.Invoice) (*lnrpc.Invoice, error) {
3337-
paymentRequest := string(invoice.PaymentRequest)
3338-
decoded, err := zpay32.Decode(paymentRequest, activeNetParams.Params)
3339-
if err != nil {
3340-
return nil, fmt.Errorf("unable to decode payment request: %v",
3341-
err)
3342-
}
3343-
3344-
descHash := []byte("")
3345-
if decoded.DescriptionHash != nil {
3346-
descHash = decoded.DescriptionHash[:]
3347-
}
3348-
3349-
fallbackAddr := ""
3350-
if decoded.FallbackAddr != nil {
3351-
fallbackAddr = decoded.FallbackAddr.String()
3352-
}
3353-
3354-
settleDate := int64(0)
3355-
if !invoice.SettleDate.IsZero() {
3356-
settleDate = invoice.SettleDate.Unix()
3357-
}
3358-
3359-
// Expiry time will default to 3600 seconds if not specified
3360-
// explicitly.
3361-
expiry := int64(decoded.Expiry().Seconds())
3362-
3363-
// The expiry will default to 9 blocks if not specified explicitly.
3364-
cltvExpiry := decoded.MinFinalCLTVExpiry()
3365-
3366-
// Convert between the `lnrpc` and `routing` types.
3367-
routeHints := createRPCRouteHints(decoded.RouteHints)
3368-
3369-
preimage := invoice.Terms.PaymentPreimage
3370-
satAmt := invoice.Terms.Value.ToSatoshis()
3371-
satAmtPaid := invoice.AmtPaid.ToSatoshis()
3372-
3373-
isSettled := invoice.Terms.State == channeldb.ContractSettled
3374-
3375-
var state lnrpc.Invoice_InvoiceState
3376-
switch invoice.Terms.State {
3377-
case channeldb.ContractOpen:
3378-
state = lnrpc.Invoice_OPEN
3379-
case channeldb.ContractSettled:
3380-
state = lnrpc.Invoice_SETTLED
3381-
default:
3382-
return nil, fmt.Errorf("unknown invoice state")
3383-
}
3384-
3385-
return &lnrpc.Invoice{
3386-
Memo: string(invoice.Memo[:]),
3387-
Receipt: invoice.Receipt[:],
3388-
RHash: decoded.PaymentHash[:],
3389-
RPreimage: preimage[:],
3390-
Value: int64(satAmt),
3391-
CreationDate: invoice.CreationDate.Unix(),
3392-
SettleDate: settleDate,
3393-
Settled: isSettled,
3394-
PaymentRequest: paymentRequest,
3395-
DescriptionHash: descHash,
3396-
Expiry: expiry,
3397-
CltvExpiry: cltvExpiry,
3398-
FallbackAddr: fallbackAddr,
3399-
RouteHints: routeHints,
3400-
AddIndex: invoice.AddIndex,
3401-
Private: len(routeHints) > 0,
3402-
SettleIndex: invoice.SettleIndex,
3403-
AmtPaidSat: int64(satAmtPaid),
3404-
AmtPaidMsat: int64(invoice.AmtPaid),
3405-
AmtPaid: int64(invoice.AmtPaid),
3406-
State: state,
3407-
}, nil
3408-
}
3409-
3410-
// createRPCRouteHints takes in the decoded form of an invoice's route hints
3411-
// and converts them into the lnrpc type.
3412-
func createRPCRouteHints(routeHints [][]routing.HopHint) []*lnrpc.RouteHint {
3413-
var res []*lnrpc.RouteHint
3414-
3415-
for _, route := range routeHints {
3416-
hopHints := make([]*lnrpc.HopHint, 0, len(route))
3417-
for _, hop := range route {
3418-
pubKey := hex.EncodeToString(
3419-
hop.NodeID.SerializeCompressed(),
3420-
)
3421-
3422-
hint := &lnrpc.HopHint{
3423-
NodeId: pubKey,
3424-
ChanId: hop.ChannelID,
3425-
FeeBaseMsat: hop.FeeBaseMSat,
3426-
FeeProportionalMillionths: hop.FeeProportionalMillionths,
3427-
CltvExpiryDelta: uint32(hop.CLTVExpiryDelta),
3428-
}
3429-
3430-
hopHints = append(hopHints, hint)
3431-
}
3432-
3433-
routeHint := &lnrpc.RouteHint{HopHints: hopHints}
3434-
res = append(res, routeHint)
3435-
}
3436-
3437-
return res
3438-
}
3439-
34403336
// LookupInvoice attempts to look up an invoice according to its payment hash.
34413337
// The passed payment hash *must* be exactly 32 bytes, if not an error is
34423338
// returned.
@@ -3479,7 +3375,9 @@ func (r *rpcServer) LookupInvoice(ctx context.Context,
34793375
return spew.Sdump(invoice)
34803376
}))
34813377

3482-
rpcInvoice, err := createRPCInvoice(&invoice)
3378+
rpcInvoice, err := invoicesrpc.CreateRPCInvoice(
3379+
&invoice, activeNetParams.Params,
3380+
)
34833381
if err != nil {
34843382
return nil, err
34853383
}
@@ -3519,7 +3417,9 @@ func (r *rpcServer) ListInvoices(ctx context.Context,
35193417
LastIndexOffset: invoiceSlice.LastIndexOffset,
35203418
}
35213419
for i, invoice := range invoiceSlice.Invoices {
3522-
resp.Invoices[i], err = createRPCInvoice(&invoice)
3420+
resp.Invoices[i], err = invoicesrpc.CreateRPCInvoice(
3421+
&invoice, activeNetParams.Params,
3422+
)
35233423
if err != nil {
35243424
return nil, err
35253425
}
@@ -3541,7 +3441,9 @@ func (r *rpcServer) SubscribeInvoices(req *lnrpc.InvoiceSubscription,
35413441
for {
35423442
select {
35433443
case newInvoice := <-invoiceClient.NewInvoices:
3544-
rpcInvoice, err := createRPCInvoice(newInvoice)
3444+
rpcInvoice, err := invoicesrpc.CreateRPCInvoice(
3445+
newInvoice, activeNetParams.Params,
3446+
)
35453447
if err != nil {
35463448
return err
35473449
}
@@ -3551,7 +3453,9 @@ func (r *rpcServer) SubscribeInvoices(req *lnrpc.InvoiceSubscription,
35513453
}
35523454

35533455
case settledInvoice := <-invoiceClient.SettledInvoices:
3554-
rpcInvoice, err := createRPCInvoice(settledInvoice)
3456+
rpcInvoice, err := invoicesrpc.CreateRPCInvoice(
3457+
settledInvoice, activeNetParams.Params,
3458+
)
35553459
if err != nil {
35563460
return err
35573461
}
@@ -4455,7 +4359,7 @@ func (r *rpcServer) DecodePayReq(ctx context.Context,
44554359
expiry := int64(payReq.Expiry().Seconds())
44564360

44574361
// Convert between the `lnrpc` and `routing` types.
4458-
routeHints := createRPCRouteHints(payReq.RouteHints)
4362+
routeHints := invoicesrpc.CreateRPCRouteHints(payReq.RouteHints)
44594363

44604364
amt := int64(0)
44614365
if payReq.MilliSat != nil {

0 commit comments

Comments
 (0)