-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Update AddHoldInvoice to add support for optional preimage/hash generation #10685
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
saubyk
wants to merge
4
commits into
lightningnetwork:master
Choose a base branch
from
saubyk:update-holdinvoice
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+332
−30
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| # Release Notes | ||
| - [Bug Fixes](#bug-fixes) | ||
| - [New Features](#new-features) | ||
| - [Functional Enhancements](#functional-enhancements) | ||
| - [RPC Additions](#rpc-additions) | ||
| - [lncli Additions](#lncli-additions) | ||
| - [Improvements](#improvements) | ||
| - [Functional Updates](#functional-updates) | ||
| - [RPC Updates](#rpc-updates) | ||
| - [lncli Updates](#lncli-updates) | ||
| - [Breaking Changes](#breaking-changes) | ||
| - [Performance Improvements](#performance-improvements) | ||
| - [Deprecations](#deprecations) | ||
| - [Technical and Architectural Updates](#technical-and-architectural-updates) | ||
| - [BOLT Spec Updates](#bolt-spec-updates) | ||
| - [Testing](#testing) | ||
| - [Database](#database) | ||
| - [Code Health](#code-health) | ||
| - [Tooling and Documentation](#tooling-and-documentation) | ||
|
|
||
| # Bug Fixes | ||
|
|
||
| # New Features | ||
|
|
||
| ## Functional Enhancements | ||
|
|
||
| ## RPC Additions | ||
|
|
||
| ## lncli Additions | ||
|
|
||
| # Improvements | ||
|
|
||
| ## Functional Updates | ||
|
|
||
| ## RPC Updates | ||
|
|
||
| * [`AddHoldInvoice` hash field is now | ||
| optional](https://github.com/lightningnetwork/lnd/pull/10685). When omitted, | ||
| the server generates a random preimage, derives the payment hash, and | ||
| returns the preimage in the response. The preimage is never persisted — the | ||
| caller must save the returned value to settle the invoice later, preserving | ||
| the hold-invoice invariant that lnd only learns the preimage at | ||
| `SettleInvoice` time. The response adds `payment_preimage` (populated only | ||
| for the auto-generated case) and `payment_hash` (always populated). | ||
|
|
||
| ## lncli Updates | ||
|
|
||
| * The [`addholdinvoice` command now accepts `--hash` and `--preimage` | ||
| flags](https://github.com/lightningnetwork/lnd/pull/10685). When neither is | ||
| provided, the server generates both automatically. The legacy positional hash | ||
| argument is still supported for backward compatibility. | ||
|
|
||
| ## Code Health | ||
|
|
||
| ## Breaking Changes | ||
|
|
||
| ## Performance Improvements | ||
|
|
||
| ## Deprecations | ||
|
|
||
| # Technical and Architectural Updates | ||
|
|
||
| ## BOLT Spec Updates | ||
|
|
||
| ## Testing | ||
|
|
||
| ## Database | ||
|
|
||
| ## Code Health | ||
|
|
||
| ## Tooling and Documentation | ||
|
|
||
| # Contributors (Alphabetical Order) | ||
|
|
||
| * Suheb |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| package itest | ||
|
|
||
| import ( | ||
| "github.com/btcsuite/btcd/btcutil" | ||
| "github.com/lightningnetwork/lnd/lnrpc" | ||
| "github.com/lightningnetwork/lnd/lnrpc/invoicesrpc" | ||
| "github.com/lightningnetwork/lnd/lnrpc/routerrpc" | ||
| "github.com/lightningnetwork/lnd/lntest" | ||
| "github.com/lightningnetwork/lnd/lntypes" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| // testHoldInvoiceAutoGenerate tests that hold invoices can be created without | ||
| // providing a hash. The server auto-generates a preimage, derives the hash, | ||
| // and returns the preimage in the response without persisting it. It also | ||
| // verifies the hash-only flow still works unchanged. | ||
| func testHoldInvoiceAutoGenerate(ht *lntest.HarnessTest) { | ||
| // Open a channel between Alice and Bob. | ||
| _, nodes := ht.CreateSimpleNetwork( | ||
| [][]string{nil, nil}, lntest.OpenChannelParams{ | ||
| Amt: btcutil.Amount(1_000_000), | ||
| }, | ||
| ) | ||
| alice, bob := nodes[0], nodes[1] | ||
|
|
||
| // Test 1: Auto-generate preimage and hash (no hash provided). | ||
| autoReq := &invoicesrpc.AddHoldInvoiceRequest{ | ||
| Memo: "auto-generated", | ||
| Value: 10_000, | ||
| } | ||
| autoResp := bob.RPC.AddHoldInvoice(autoReq) | ||
|
|
||
| // The response must contain both a preimage and hash. | ||
| require.Len(ht, autoResp.PaymentPreimage, 32, | ||
| "expected 32-byte preimage") | ||
| require.Len(ht, autoResp.PaymentHash, 32, | ||
| "expected 32-byte payment hash") | ||
|
|
||
| // Verify the hash matches SHA256 of the preimage. | ||
| var autoPreimage lntypes.Preimage | ||
| copy(autoPreimage[:], autoResp.PaymentPreimage) | ||
| autoHash := autoPreimage.Hash() | ||
| require.Equal(ht, autoHash[:], autoResp.PaymentHash, | ||
| "hash should be SHA256 of preimage") | ||
|
|
||
| // Verify the generated preimage is not persisted: a LookupInvoice | ||
| // before settlement must report an empty r_preimage. The server | ||
| // only learns the preimage again when the caller passes it to | ||
| // SettleInvoice. | ||
| lookup := bob.RPC.LookupInvoice(autoResp.PaymentHash) | ||
| require.Empty(ht, lookup.RPreimage, | ||
| "auto-generated preimage must not be stored in the DB") | ||
|
|
||
| // Subscribe, pay, and settle. | ||
| autoStream := bob.RPC.SubscribeSingleInvoice(autoResp.PaymentHash) | ||
|
|
||
| ht.SendPaymentAndAssertStatus(alice, &routerrpc.SendPaymentRequest{ | ||
| PaymentRequest: autoResp.PaymentRequest, | ||
| FeeLimitSat: 1_000_000, | ||
| }, lnrpc.Payment_IN_FLIGHT) | ||
|
|
||
| ht.AssertInvoiceState(autoStream, lnrpc.Invoice_ACCEPTED) | ||
|
|
||
| bob.RPC.SettleInvoice(autoResp.PaymentPreimage) | ||
| ht.AssertInvoiceState(autoStream, lnrpc.Invoice_SETTLED) | ||
| ht.AssertPaymentStatus( | ||
| alice, autoHash, lnrpc.Payment_SUCCEEDED, | ||
| ) | ||
|
|
||
| // Test 2: Traditional hash-only flow still works. The response | ||
| // preimage must be empty because the server does not know it. | ||
| var hashPreimage lntypes.Preimage | ||
| copy(hashPreimage[:], ht.Random32Bytes()) | ||
| payHash := hashPreimage.Hash() | ||
|
|
||
| hashResp := bob.RPC.AddHoldInvoice( | ||
| &invoicesrpc.AddHoldInvoiceRequest{ | ||
| Memo: "hash-only", | ||
| Value: 10_000, | ||
| Hash: payHash[:], | ||
| }, | ||
| ) | ||
|
|
||
| require.Empty(ht, hashResp.PaymentPreimage, | ||
| "preimage should be empty for hash-only") | ||
| require.Equal(ht, payHash[:], hashResp.PaymentHash, | ||
| "returned hash should match provided hash") | ||
|
|
||
| // Subscribe, pay, and settle. | ||
| hashStream := bob.RPC.SubscribeSingleInvoice(payHash[:]) | ||
|
|
||
| ht.SendPaymentAndAssertStatus(alice, &routerrpc.SendPaymentRequest{ | ||
| PaymentRequest: hashResp.PaymentRequest, | ||
| FeeLimitSat: 1_000_000, | ||
| }, lnrpc.Payment_IN_FLIGHT) | ||
|
|
||
| ht.AssertInvoiceState(hashStream, lnrpc.Invoice_ACCEPTED) | ||
|
|
||
| bob.RPC.SettleInvoice(hashPreimage[:]) | ||
| ht.AssertInvoiceState(hashStream, lnrpc.Invoice_SETTLED) | ||
| ht.AssertPaymentStatus( | ||
| alice, payHash, lnrpc.Payment_SUCCEEDED, | ||
| ) | ||
| } |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
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.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we return this? We don't do so for normal (non-hold) invoices. If we do want to return this, I think it should be based on a macaroon scope.