Conversation

edited

In this PR, a new feature called "hold invoice" is added to lnd. A hold invoice is new type of invoice that triggers a different flow at the receiver end.

Instead of immediately locking in and settling the htlc when the payment arrives, the htlc for a hold invoice is only locked in and not yet settled. At that point, it is not possible anymore for the sender to revoke the payment, but the receiver still can choose whether to settle or cancel the htlc and invoice.

From the sender perspective, a hold invoice payment request looks identical to a regular payment request. There is no way for the sender to know when a hold invoice is paid to.

Hold invoices enable several new use cases:

Receiver gains the ability to check a condition before settling. If the condition is not met, the invoice can still be canceled and a potential refund does not need to be given.

Example 1: web shop performing an inventory check. If the item is no longer in stock at the time the invoice is paid, the payment can be canceled.

Example 2: exchange accepting spontaneous LN deposits (building on top of #2455). The account to credit is specified as user data in the EOB and the exchange first checks whether this account is valid before settling the incoming htlc.

A service can use a hold invoice as an insurance against bad behaviour by its users, similar to a fidelity bond. The user gains access to the service by paying the hold invoice. When the session is over, the service cancels the invoice and the user regains its funds. The only cost for the user has been the time value of those funds. When the user misbehaves during the session, the service can settle the hold invoice as a penalty.

Example 3: prevention of dos attacks

A hold invoice may also be created if the preimage of the payment hash is not yet known to the receiver. Receiver can accept the payment and perform a follow up action to obtain the preimage. Only once the preimage is obtained, the receiver can settle the payment.

Example 4: pizza delivery service wanting to offer "no pizza no pay". A customer generates a random preimage and adds the hash of it to the pizza order. The delivery service creates a hold invoice tied to this hash and returns the invoice to the customer. Customer pays the invoice. At this point, the delivery service can accept the payment, but not settle it because it doesn't know the preimage yet. Then the driver goes out to deliver the pizza. At the door he asks the customer for the preimage, verifies that it is indeed the preimage of the hold invoice for this order and hands over the pizza. Verification can even be done offline. The delivery service can then settle the invoice when the driver returns with the preimage. Atomic pizza swap completed.

Example 5: parcel delivery service. Similar to pizza, but here the payer and recipient are different people. Payer pays the hold invoice of the delivery service and gives the preimage to the recipient of the parcel.

[In this example there is a risk associated with reuse of the payment hash. Intermediate nodes on both paths may shortcut the preimage and disallow the webshop from receiving any money.]

Example 7: using hold invoice to execute a trustless atomic off-chain to on-chain swap. User A creates a secret preimage and passes the hash of it to user B. User B creates a hold invoice tied to this hash. User A pays the invoice. User B publishes an on-chain htlc tied to the same hash. When user A sweeps the on-chain htlc with the preimage it knows, user B learns it too and can pull the off-chain invoice.

Usage

To enable hold invoice functionality, lnd needs to be built with the invoices sub server enabled:

make tags="invoicesrpc"

Creation of a hold invoice

To add a holdinvoice, invoke:

lncli addholdinvoice <hash> --amt <amt>

Waiting for acceptance

Typically an application wants to receive a notification when the htlc is accepted. This event isn't notified through the general SubscribeInvoices rpc. The reason for this is not to break backwards compatibility and prevent difficulties in the interaction with the add and settle indices of that call.Instead, the new SubscribeSingleInvoice rpc can be used.

SubscribeSingleInvoice is not available through lncli. For testing, the invoice state can be observed through lncli lookupinvoice <hash>.

Settling or canceling

Once the htlc has been accepted in lnd, it is held there until either a cancel or settle rpc is received. Both can be issued through lncli:

lncli cancelinvoice <hash>

or

lncli settleinvoice <preimage>

Todo

This PR leaves some issues unaddressed:

Automatic invoice cancelation when the incoming htlc is about to time out. Canceling the invoice fails back the htlc. If the invoice wouldn't be canceled, the htlc remains pending and the channel will be force closed on chain. Typical use of hodl invoices involves an application that is monitoring this timeout as well. That application would have either canceled or settled the invoice already before automatic cancelation is triggered. Automatic cancelation can be seen as a safety net to prevent channel closure.

Automatic invoice cancelation when the invoice expires (invoice expiry property). This does not only apply to hodl invoices, but to regular invoices as well. Currently expiry is only checked by the sender before sending the payment and can be worked around. Automatic cancelation would prevent any payment of the invoice from being accepted by the receiver, thereby preventing potential refund flows.

Handling of multiple htlcs paying to the same accepted invoice. Currently amtPaid does not reflect the total amount accepted. This is true for regular invoices as well. Calls for a higher level plan of when and when not to accept/settle htlcs.

This comment has been minimized.

one thing we may consider is logically groupingswitchSettledPackets from switchPackets. both can be forwarded in a single call to forwardBatch, but we should post-process accepting the switchSettledPackets after they have been forwarded. rationale here is that AcceptInvoice will fire a notification, which if instantly acted upon, could result in the circuit not being opened when the switch tries to lookup the circuit
delaying until after the circuits have been atomically opened and committed will ensure this case doesn't arise

Is this still how we want to do this? Or is it better to make the AcceptInvoice call inside the switch?

This comment has been minimized.

Thanks for the reminder on this! Yes I think we'd still want to do that. I'd prefer to have it done in the link to keep the circuit map operations as isolated as possible. It would need to be done after forwardPackets is called, and also check that the link isn't exiting by checking quit preemptively before accepting

This comment has been minimized.

Thanks for the reminder on this! Yes I think we'd still want to do that. I'd prefer to have it done in the link to keep the circuit map operations as isolated as possible. It would need to be done after forwardPackets is called, and also check that the link isn't exiting by checking quit preemptively before accepting

This comment has been minimized.

@cfromknecht I park the link/switch comments for now. First define the rpc interface for this in #2263, implement that (new invoice states and events) and then continue with the switch/link modifications.

Previously a function pointer was passed to chain arbitrator to avoid a
circular dependency. Now that the routetypes package exists, we can pass
the full invoice registry to chain arbitrator.
This is a preparation to be able to use other invoice registry methods
in contract resolvers.

Previously it could happen that an invoice was open at the time of the
LookupInvoice call, the htlc was settled because of that, but when the
SettleInvoice call was made eventually, it would fail because the
invoice was canceled in the mean time. The htlc would then be settled,
but the invoice not marked as such.

Previously it was difficult to use the invoice registry in unit tests,
because it used zpay32 to decode the invoice. For that to succeed, a
valid signature is required on the payment request.
This commit injects the decode dependency on a different level so that
it is easier to mock.

In the TestChannelLinkMultiHopUnknownPaymentHash test, a preimage was
modified to trigger an unknown payment hash failure. The way the mock is
implemented, it would take the hash of that modified preimage and store
it. It basically stores a completely different invoice. For this test,
it is just as good to store no invoice at all.

This commit modifies the invoice registry to handle invoices for which
the preimage is not known yet (hodl invoices). In that case, the
resolution channel passed in from links and resolvers is stored until we
either learn the preimage or want to cancel the htlc.

Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.