2019-01-03 21:13:08 +03:00
|
|
|
package invoicesrpc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/hex"
|
2019-12-05 14:52:11 +03:00
|
|
|
"errors"
|
2019-01-03 21:13:08 +03:00
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/btcsuite/btcd/chaincfg"
|
|
|
|
"github.com/lightningnetwork/lnd/channeldb"
|
|
|
|
"github.com/lightningnetwork/lnd/lnrpc"
|
2019-12-12 03:37:02 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lnwire"
|
2019-01-03 21:13:08 +03:00
|
|
|
"github.com/lightningnetwork/lnd/zpay32"
|
|
|
|
)
|
|
|
|
|
2019-12-05 14:52:11 +03:00
|
|
|
// decodePayReq decodes the invoice payment request if present. This is needed,
|
|
|
|
// because not all information is stored in dedicated invoice fields. If there
|
|
|
|
// is no payment request present, a dummy request will be returned. This can
|
|
|
|
// happen with just-in-time inserted key send invoices.
|
|
|
|
func decodePayReq(invoice *channeldb.Invoice,
|
|
|
|
activeNetParams *chaincfg.Params) (*zpay32.Invoice, error) {
|
|
|
|
|
|
|
|
paymentRequest := string(invoice.PaymentRequest)
|
|
|
|
if paymentRequest == "" {
|
|
|
|
preimage := invoice.Terms.PaymentPreimage
|
|
|
|
if preimage == channeldb.UnknownPreimage {
|
|
|
|
return nil, errors.New("cannot reconstruct pay req")
|
|
|
|
}
|
|
|
|
hash := [32]byte(preimage.Hash())
|
|
|
|
return &zpay32.Invoice{
|
|
|
|
PaymentHash: &hash,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
decoded, err := zpay32.Decode(paymentRequest, activeNetParams)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("unable to decode payment "+
|
|
|
|
"request: %v", err)
|
|
|
|
}
|
|
|
|
return decoded, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-01-03 21:13:08 +03:00
|
|
|
// CreateRPCInvoice creates an *lnrpc.Invoice from the *channeldb.Invoice.
|
|
|
|
func CreateRPCInvoice(invoice *channeldb.Invoice,
|
|
|
|
activeNetParams *chaincfg.Params) (*lnrpc.Invoice, error) {
|
|
|
|
|
2019-12-05 14:52:11 +03:00
|
|
|
decoded, err := decodePayReq(invoice, activeNetParams)
|
2019-01-03 21:13:08 +03:00
|
|
|
if err != nil {
|
2019-12-05 14:52:11 +03:00
|
|
|
return nil, err
|
2019-01-03 21:13:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
var descHash []byte
|
|
|
|
if decoded.DescriptionHash != nil {
|
|
|
|
descHash = decoded.DescriptionHash[:]
|
|
|
|
}
|
|
|
|
|
|
|
|
fallbackAddr := ""
|
|
|
|
if decoded.FallbackAddr != nil {
|
|
|
|
fallbackAddr = decoded.FallbackAddr.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
settleDate := int64(0)
|
|
|
|
if !invoice.SettleDate.IsZero() {
|
|
|
|
settleDate = invoice.SettleDate.Unix()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convert between the `lnrpc` and `routing` types.
|
|
|
|
routeHints := CreateRPCRouteHints(decoded.RouteHints)
|
|
|
|
|
|
|
|
preimage := invoice.Terms.PaymentPreimage
|
|
|
|
satAmt := invoice.Terms.Value.ToSatoshis()
|
|
|
|
satAmtPaid := invoice.AmtPaid.ToSatoshis()
|
|
|
|
|
2019-11-22 13:25:02 +03:00
|
|
|
isSettled := invoice.State == channeldb.ContractSettled
|
2019-01-03 21:13:08 +03:00
|
|
|
|
|
|
|
var state lnrpc.Invoice_InvoiceState
|
2019-11-22 13:25:02 +03:00
|
|
|
switch invoice.State {
|
2019-01-03 21:13:08 +03:00
|
|
|
case channeldb.ContractOpen:
|
|
|
|
state = lnrpc.Invoice_OPEN
|
|
|
|
case channeldb.ContractSettled:
|
|
|
|
state = lnrpc.Invoice_SETTLED
|
2019-01-11 13:19:16 +03:00
|
|
|
case channeldb.ContractCanceled:
|
|
|
|
state = lnrpc.Invoice_CANCELED
|
2019-02-11 14:01:05 +03:00
|
|
|
case channeldb.ContractAccepted:
|
|
|
|
state = lnrpc.Invoice_ACCEPTED
|
2019-01-03 21:13:08 +03:00
|
|
|
default:
|
2019-01-11 13:19:16 +03:00
|
|
|
return nil, fmt.Errorf("unknown invoice state %v",
|
2019-11-22 13:25:02 +03:00
|
|
|
invoice.State)
|
2019-01-03 21:13:08 +03:00
|
|
|
}
|
|
|
|
|
2019-08-09 18:28:08 +03:00
|
|
|
rpcHtlcs := make([]*lnrpc.InvoiceHTLC, 0, len(invoice.Htlcs))
|
|
|
|
for key, htlc := range invoice.Htlcs {
|
|
|
|
var state lnrpc.InvoiceHTLCState
|
|
|
|
switch htlc.State {
|
|
|
|
case channeldb.HtlcStateAccepted:
|
|
|
|
state = lnrpc.InvoiceHTLCState_ACCEPTED
|
|
|
|
case channeldb.HtlcStateSettled:
|
|
|
|
state = lnrpc.InvoiceHTLCState_SETTLED
|
2019-10-03 18:22:43 +03:00
|
|
|
case channeldb.HtlcStateCanceled:
|
|
|
|
state = lnrpc.InvoiceHTLCState_CANCELED
|
2019-08-09 18:28:08 +03:00
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("unknown state %v", htlc.State)
|
|
|
|
}
|
|
|
|
|
|
|
|
rpcHtlc := lnrpc.InvoiceHTLC{
|
2019-09-03 13:23:39 +03:00
|
|
|
ChanId: key.ChanID.ToUint64(),
|
|
|
|
HtlcIndex: key.HtlcID,
|
|
|
|
AcceptHeight: int32(htlc.AcceptHeight),
|
|
|
|
AcceptTime: htlc.AcceptTime.Unix(),
|
|
|
|
ExpiryHeight: int32(htlc.Expiry),
|
|
|
|
AmtMsat: uint64(htlc.Amt),
|
|
|
|
State: state,
|
|
|
|
CustomRecords: htlc.CustomRecords,
|
|
|
|
MppTotalAmtMsat: uint64(htlc.MppTotalAmt),
|
2019-08-09 18:28:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Only report resolved times if htlc is resolved.
|
|
|
|
if htlc.State != channeldb.HtlcStateAccepted {
|
|
|
|
rpcHtlc.ResolveTime = htlc.ResolveTime.Unix()
|
|
|
|
}
|
|
|
|
|
|
|
|
rpcHtlcs = append(rpcHtlcs, &rpcHtlc)
|
|
|
|
}
|
|
|
|
|
2018-10-05 11:14:56 +03:00
|
|
|
rpcInvoice := &lnrpc.Invoice{
|
2019-01-03 21:13:08 +03:00
|
|
|
Memo: string(invoice.Memo[:]),
|
|
|
|
RHash: decoded.PaymentHash[:],
|
|
|
|
Value: int64(satAmt),
|
2019-11-15 10:59:14 +03:00
|
|
|
ValueMsat: int64(invoice.Terms.Value),
|
2019-01-03 21:13:08 +03:00
|
|
|
CreationDate: invoice.CreationDate.Unix(),
|
|
|
|
SettleDate: settleDate,
|
|
|
|
Settled: isSettled,
|
2019-12-05 14:52:11 +03:00
|
|
|
PaymentRequest: string(invoice.PaymentRequest),
|
2019-01-03 21:13:08 +03:00
|
|
|
DescriptionHash: descHash,
|
2019-11-22 13:25:02 +03:00
|
|
|
Expiry: int64(invoice.Terms.Expiry.Seconds()),
|
|
|
|
CltvExpiry: uint64(invoice.Terms.FinalCltvDelta),
|
2019-01-03 21:13:08 +03:00
|
|
|
FallbackAddr: fallbackAddr,
|
|
|
|
RouteHints: routeHints,
|
|
|
|
AddIndex: invoice.AddIndex,
|
|
|
|
Private: len(routeHints) > 0,
|
|
|
|
SettleIndex: invoice.SettleIndex,
|
|
|
|
AmtPaidSat: int64(satAmtPaid),
|
|
|
|
AmtPaidMsat: int64(invoice.AmtPaid),
|
|
|
|
AmtPaid: int64(invoice.AmtPaid),
|
|
|
|
State: state,
|
2019-08-09 18:28:08 +03:00
|
|
|
Htlcs: rpcHtlcs,
|
2019-12-12 03:37:02 +03:00
|
|
|
Features: CreateRPCFeatures(invoice.Terms.Features),
|
2019-12-12 10:10:15 +03:00
|
|
|
IsKeySend: len(invoice.PaymentRequest) == 0,
|
2018-10-05 11:14:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if preimage != channeldb.UnknownPreimage {
|
|
|
|
rpcInvoice.RPreimage = preimage[:]
|
|
|
|
}
|
|
|
|
|
|
|
|
return rpcInvoice, nil
|
2019-01-03 21:13:08 +03:00
|
|
|
}
|
|
|
|
|
2019-12-12 03:37:02 +03:00
|
|
|
// CreateRPCFeatures maps a feature vector into a list of lnrpc.Features.
|
2019-12-14 18:05:21 +03:00
|
|
|
func CreateRPCFeatures(fv *lnwire.FeatureVector) map[uint32]*lnrpc.Feature {
|
2019-12-14 18:04:51 +03:00
|
|
|
if fv == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-12-12 03:37:02 +03:00
|
|
|
features := fv.Features()
|
2019-12-14 18:05:21 +03:00
|
|
|
rpcFeatures := make(map[uint32]*lnrpc.Feature, len(features))
|
2019-12-12 03:37:02 +03:00
|
|
|
for bit := range features {
|
2019-12-14 18:05:21 +03:00
|
|
|
rpcFeatures[uint32(bit)] = &lnrpc.Feature{
|
2019-12-12 03:37:02 +03:00
|
|
|
Name: fv.Name(bit),
|
|
|
|
IsRequired: bit.IsRequired(),
|
|
|
|
IsKnown: fv.IsKnown(bit),
|
2019-12-14 18:05:21 +03:00
|
|
|
}
|
2019-12-12 03:37:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return rpcFeatures
|
|
|
|
}
|
|
|
|
|
2019-01-03 21:13:08 +03:00
|
|
|
// CreateRPCRouteHints takes in the decoded form of an invoice's route hints
|
|
|
|
// and converts them into the lnrpc type.
|
2019-02-19 11:09:01 +03:00
|
|
|
func CreateRPCRouteHints(routeHints [][]zpay32.HopHint) []*lnrpc.RouteHint {
|
2019-01-03 21:13:08 +03:00
|
|
|
var res []*lnrpc.RouteHint
|
|
|
|
|
|
|
|
for _, route := range routeHints {
|
|
|
|
hopHints := make([]*lnrpc.HopHint, 0, len(route))
|
|
|
|
for _, hop := range route {
|
|
|
|
pubKey := hex.EncodeToString(
|
|
|
|
hop.NodeID.SerializeCompressed(),
|
|
|
|
)
|
|
|
|
|
|
|
|
hint := &lnrpc.HopHint{
|
|
|
|
NodeId: pubKey,
|
|
|
|
ChanId: hop.ChannelID,
|
|
|
|
FeeBaseMsat: hop.FeeBaseMSat,
|
|
|
|
FeeProportionalMillionths: hop.FeeProportionalMillionths,
|
|
|
|
CltvExpiryDelta: uint32(hop.CLTVExpiryDelta),
|
|
|
|
}
|
|
|
|
|
|
|
|
hopHints = append(hopHints, hint)
|
|
|
|
}
|
|
|
|
|
|
|
|
routeHint := &lnrpc.RouteHint{HopHints: hopHints}
|
|
|
|
res = append(res, routeHint)
|
|
|
|
}
|
|
|
|
|
|
|
|
return res
|
|
|
|
}
|