|
| 1 | +package invoicesrpc |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/btcsuite/btcd/chaincfg" |
| 5 | + "github.com/lightningnetwork/lnd/invoices" |
| 6 | + "github.com/lightningnetwork/lnd/lntypes" |
| 7 | + "google.golang.org/grpc/codes" |
| 8 | + "google.golang.org/grpc/status" |
| 9 | +) |
| 10 | + |
| 11 | +// invoiceAcceptorConfig contains the configuration for an RPC invoice acceptor |
| 12 | +// server. |
| 13 | +type invoiceAcceptorConfig struct { |
| 14 | + // chainParams is required to properly marshall an invoice for RPC. |
| 15 | + chainParams *chaincfg.Params |
| 16 | + |
| 17 | + // rpcServer is a bidirectional RPC server to send invoices to a client |
| 18 | + // and receive accept responses from the client. |
| 19 | + rpcServer Invoices_InvoiceAcceptorServer |
| 20 | + |
| 21 | + // interceptor is the invoice interceptor that will be used to intercept |
| 22 | + // and resolve invoices. |
| 23 | + interceptor invoices.SettlementInterceptorInterface |
| 24 | +} |
| 25 | + |
| 26 | +// invoiceAcceptor is a helper struct that handles the lifecycle of an RPC |
| 27 | +// invoice acceptor server instance. |
| 28 | +// |
| 29 | +// This struct handles passing send and receive RPC messages between the client |
| 30 | +// and the invoice service. |
| 31 | +type invoiceAcceptor struct { |
| 32 | + // cfg contains the configuration for the invoice acceptor. |
| 33 | + cfg invoiceAcceptorConfig |
| 34 | +} |
| 35 | + |
| 36 | +// newInvoiceAcceptor creates a new RPC invoice acceptor handler. |
| 37 | +func newInvoiceAcceptor(cfg invoiceAcceptorConfig) *invoiceAcceptor { |
| 38 | + return &invoiceAcceptor{ |
| 39 | + cfg: cfg, |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +// run sends the intercepted invoices to the client and receives the |
| 44 | +// corresponding responses. |
| 45 | +func (r *invoiceAcceptor) run() error { |
| 46 | + // Register our invoice interceptor. |
| 47 | + r.cfg.interceptor.SetClientCallback(r.onIntercept) |
| 48 | + defer r.cfg.interceptor.SetClientCallback(nil) |
| 49 | + |
| 50 | + // Listen for a response from the client in a loop. |
| 51 | + for { |
| 52 | + resp, err := r.cfg.rpcServer.Recv() |
| 53 | + if err != nil { |
| 54 | + return err |
| 55 | + } |
| 56 | + |
| 57 | + if err := r.resolveFromClient(resp); err != nil { |
| 58 | + return err |
| 59 | + } |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +// onIntercept is called when an invoice is intercepted by the invoice |
| 64 | +// interceptor. This method sends the invoice to the client. |
| 65 | +func (r *invoiceAcceptor) onIntercept( |
| 66 | + req invoices.InterceptClientRequest) error { |
| 67 | + |
| 68 | + // Convert the circuit key to an RPC circuit key. |
| 69 | + rpcCircuitKey := &CircuitKey{ |
| 70 | + ChanId: req.ExitHtlcCircuitKey.ChanID.ToUint64(), |
| 71 | + HtlcId: req.ExitHtlcCircuitKey.HtlcID, |
| 72 | + } |
| 73 | + |
| 74 | + // Convert the invoice to an RPC invoice. |
| 75 | + rpcInvoice, err := CreateRPCInvoice(&req.Invoice, r.cfg.chainParams) |
| 76 | + if err != nil { |
| 77 | + return err |
| 78 | + } |
| 79 | + |
| 80 | + return r.cfg.rpcServer.Send(&InvoiceAcceptorRequest{ |
| 81 | + Invoice: rpcInvoice, |
| 82 | + ExitHtlcCircuitKey: rpcCircuitKey, |
| 83 | + ExitHtlcAmt: uint64(req.ExitHtlcAmt), |
| 84 | + ExitHtlcExpiry: req.ExitHtlcExpiry, |
| 85 | + CurrentHeight: req.CurrentHeight, |
| 86 | + }) |
| 87 | +} |
| 88 | + |
| 89 | +// resolveFromClient handles an invoice resolution received from the client. |
| 90 | +func (r *invoiceAcceptor) resolveFromClient( |
| 91 | + in *InvoiceAcceptorResponse) error { |
| 92 | + |
| 93 | + log.Tracef("Resolving invoice acceptor response %v", in) |
| 94 | + |
| 95 | + // Parse the invoice preimage from the response. |
| 96 | + if len(in.Preimage) != lntypes.HashSize { |
| 97 | + return status.Errorf(codes.InvalidArgument, |
| 98 | + "Preimage has invalid length: %d", len(in.Preimage)) |
| 99 | + } |
| 100 | + preimage, err := lntypes.MakePreimage(in.Preimage) |
| 101 | + if err != nil { |
| 102 | + return status.Errorf(codes.InvalidArgument, |
| 103 | + "Preimage is invalid: %v", err) |
| 104 | + } |
| 105 | + |
| 106 | + // Derive the payment hash from the preimage. |
| 107 | + paymentHash := preimage.Hash() |
| 108 | + |
| 109 | + // Pass the resolution to the interceptor. |
| 110 | + return r.cfg.interceptor.Resolve(paymentHash, in.SkipAmountCheck) |
| 111 | +} |
0 commit comments