mirror of
https://github.com/lightninglabs/loop.git
synced 2026-08-16 13:00:37 +02:00
Merge pull request #208 from carlaKC/lndclient-lookupinvoice
lndclient: add lookup invoice
This commit is contained in:
commit
3f10407e9c
3 changed files with 136 additions and 0 deletions
|
|
@ -16,6 +16,7 @@ import (
|
|||
"github.com/lightningnetwork/lnd/lnrpc"
|
||||
"github.com/lightningnetwork/lnd/lnrpc/invoicesrpc"
|
||||
"github.com/lightningnetwork/lnd/lntypes"
|
||||
"github.com/lightningnetwork/lnd/lnwire"
|
||||
"github.com/lightningnetwork/lnd/zpay32"
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/codes"
|
||||
|
|
@ -38,6 +39,9 @@ type LightningClient interface {
|
|||
AddInvoice(ctx context.Context, in *invoicesrpc.AddInvoiceData) (
|
||||
lntypes.Hash, string, error)
|
||||
|
||||
// LookupInvoice looks up an invoice by hash.
|
||||
LookupInvoice(ctx context.Context, hash lntypes.Hash) (*Invoice, error)
|
||||
|
||||
// ListTransactions returns all known transactions of the backing lnd
|
||||
// node.
|
||||
ListTransactions(ctx context.Context) ([]*wire.MsgTx, error)
|
||||
|
|
@ -365,6 +369,103 @@ func (s *lightningClient) AddInvoice(ctx context.Context,
|
|||
return hash, resp.PaymentRequest, nil
|
||||
}
|
||||
|
||||
// Invoice represents an invoice in lnd.
|
||||
type Invoice struct {
|
||||
// Preimage is the invoice's preimage, which is set if the invoice
|
||||
// is settled.
|
||||
Preimage *lntypes.Preimage
|
||||
|
||||
// Hash is the invoice hash.
|
||||
Hash lntypes.Hash
|
||||
|
||||
// Memo is an optional memo field for hte invoice.
|
||||
Memo string
|
||||
|
||||
// PaymentRequest is the invoice's payment request.
|
||||
PaymentRequest string
|
||||
|
||||
// Amount is the amount of the invoice in millisatoshis.
|
||||
Amount lnwire.MilliSatoshi
|
||||
|
||||
// AmountPaid is the amount that was paid for the invoice. This field
|
||||
// will only be set if the invoice is settled.
|
||||
AmountPaid lnwire.MilliSatoshi
|
||||
|
||||
// CreationDate is the time the invoice was created.
|
||||
CreationDate time.Time
|
||||
|
||||
// SettleDate is the time the invoice was settled.
|
||||
SettleDate time.Time
|
||||
|
||||
// State is the invoice's current state.
|
||||
State channeldb.ContractState
|
||||
|
||||
// IsKeysend indicates whether the invoice was a spontaneous payment.
|
||||
IsKeysend bool
|
||||
}
|
||||
|
||||
// LookupInvoice looks up an invoice in lnd, it will error if the invoice is
|
||||
// not known to lnd.
|
||||
func (s *lightningClient) LookupInvoice(ctx context.Context,
|
||||
hash lntypes.Hash) (*Invoice, error) {
|
||||
|
||||
rpcCtx, cancel := context.WithTimeout(ctx, rpcTimeout)
|
||||
defer cancel()
|
||||
|
||||
rpcIn := &lnrpc.PaymentHash{
|
||||
RHash: hash[:],
|
||||
}
|
||||
|
||||
rpcCtx = s.adminMac.WithMacaroonAuth(rpcCtx)
|
||||
resp, err := s.client.LookupInvoice(rpcCtx, rpcIn)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
invoice := &Invoice{
|
||||
Preimage: nil,
|
||||
Hash: hash,
|
||||
Memo: resp.Memo,
|
||||
PaymentRequest: resp.PaymentRequest,
|
||||
Amount: lnwire.MilliSatoshi(resp.ValueMsat),
|
||||
AmountPaid: lnwire.MilliSatoshi(resp.AmtPaidMsat),
|
||||
CreationDate: time.Unix(resp.CreationDate, 0),
|
||||
IsKeysend: resp.IsKeysend,
|
||||
}
|
||||
|
||||
switch resp.State {
|
||||
case lnrpc.Invoice_OPEN:
|
||||
invoice.State = channeldb.ContractOpen
|
||||
|
||||
case lnrpc.Invoice_ACCEPTED:
|
||||
invoice.State = channeldb.ContractAccepted
|
||||
|
||||
// If the invoice is settled, it also has a non-nil preimage, which we
|
||||
// can set on our invoice.
|
||||
case lnrpc.Invoice_SETTLED:
|
||||
invoice.State = channeldb.ContractSettled
|
||||
preimage, err := lntypes.MakePreimage(resp.RPreimage)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
invoice.Preimage = &preimage
|
||||
|
||||
case lnrpc.Invoice_CANCELED:
|
||||
invoice.State = channeldb.ContractCanceled
|
||||
|
||||
default:
|
||||
return nil, fmt.Errorf("unknown invoice state: %v", resp.State)
|
||||
}
|
||||
|
||||
// Only set settle date if it is non-zero, because 0 unix time is
|
||||
// not the same as a zero time struct.
|
||||
if resp.SettleDate != 0 {
|
||||
invoice.SettleDate = time.Unix(resp.SettleDate, 0)
|
||||
}
|
||||
|
||||
return invoice, nil
|
||||
}
|
||||
|
||||
// ListTransactions returns all known transactions of the backing lnd node.
|
||||
func (s *lightningClient) ListTransactions(ctx context.Context) ([]*wire.MsgTx, error) {
|
||||
rpcCtx, cancel := context.WithTimeout(ctx, rpcTimeout)
|
||||
|
|
|
|||
|
|
@ -11,6 +11,7 @@ import (
|
|||
"github.com/btcsuite/btcd/wire"
|
||||
"github.com/btcsuite/btcutil"
|
||||
"github.com/lightninglabs/loop/lndclient"
|
||||
"github.com/lightningnetwork/lnd/channeldb"
|
||||
"github.com/lightningnetwork/lnd/lnrpc/invoicesrpc"
|
||||
"github.com/lightningnetwork/lnd/lntypes"
|
||||
"github.com/lightningnetwork/lnd/zpay32"
|
||||
|
|
@ -125,9 +126,38 @@ func (h *mockLightningClient) AddInvoice(ctx context.Context,
|
|||
return lntypes.Hash{}, "", err
|
||||
}
|
||||
|
||||
// Add the invoice we have created to our mock's set of invoices.
|
||||
h.lnd.Invoices[hash] = &lndclient.Invoice{
|
||||
Preimage: nil,
|
||||
Hash: hash,
|
||||
PaymentRequest: payReqString,
|
||||
Amount: in.Value,
|
||||
CreationDate: creationDate,
|
||||
State: channeldb.ContractOpen,
|
||||
IsKeysend: false,
|
||||
}
|
||||
|
||||
return hash, payReqString, nil
|
||||
}
|
||||
|
||||
// LookupInvoice looks up an invoice in the mock's set of stored invoices.
|
||||
// If it is not found, this call will fail. Note that these invoices should
|
||||
// be settled using settleInvoice to have a preimage, settled state and settled
|
||||
// date set.
|
||||
func (h *mockLightningClient) LookupInvoice(_ context.Context,
|
||||
hash lntypes.Hash) (*lndclient.Invoice, error) {
|
||||
|
||||
h.lnd.lock.Lock()
|
||||
defer h.lnd.lock.Unlock()
|
||||
|
||||
inv, ok := h.lnd.Invoices[hash]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("invoice: %x not found", hash)
|
||||
}
|
||||
|
||||
return inv, nil
|
||||
}
|
||||
|
||||
// ListTransactions returns all known transactions of the backing lnd node.
|
||||
func (h *mockLightningClient) ListTransactions(
|
||||
ctx context.Context) ([]*wire.MsgTx, error) {
|
||||
|
|
|
|||
|
|
@ -69,6 +69,7 @@ func NewMockLnd() *LndMockServices {
|
|||
NodePubkey: testNodePubkey,
|
||||
Signature: testSignature,
|
||||
SignatureMsg: testSignatureMsg,
|
||||
Invoices: make(map[lntypes.Hash]*lndclient.Invoice),
|
||||
}
|
||||
|
||||
lightningClient.lnd = &lnd
|
||||
|
|
@ -158,6 +159,10 @@ type LndMockServices struct {
|
|||
|
||||
Transactions []*wire.MsgTx
|
||||
|
||||
// Invoices is a set of invoices that have been created by the mock,
|
||||
// keyed by hash string.
|
||||
Invoices map[lntypes.Hash]*lndclient.Invoice
|
||||
|
||||
WaitForFinished func()
|
||||
|
||||
lock sync.Mutex
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue