Skip to content

Update AddHoldInvoice to add support for optional preimage/hash generation#10685

Draft
saubyk wants to merge 3 commits intolightningnetwork:masterfrom
saubyk:update-holdinvoice
Draft

Update AddHoldInvoice to add support for optional preimage/hash generation#10685
saubyk wants to merge 3 commits intolightningnetwork:masterfrom
saubyk:update-holdinvoice

Conversation

@saubyk
Copy link
Copy Markdown
Collaborator

@saubyk saubyk commented Mar 28, 2026

Fixes #7220

Change Description

Update AddHoldInvoice to add support for optional preimage/hash generation. Following scenarios are handled:

  • User provides only a preimage, for which the hash will be automatically generated
  • User provides only a hash. This is the original behavior
  • User provides neither a preimage or hash, both of which will then be automatically generated
  • User provides both preimage and hash, which will result in an error condition

Steps to Test

  • Run make unit pkg=./lnrpc/invoicesrpc/... to verify compilation and existing unit tests
  • Run make itest icase=hold_invoice_auto_generate to test the new integration test covering all 4 modes:
    • Auto-generate (no hash, no preimage) — creates invoice, pays, settles with returned preimage
    • User preimage — server derives hash, verifies round-trip
    • Hash only — backward compat, preimage not returned
    • Both hash and preimage — returns error
  • Run make itest icase=hold_invoice_sender_persistence to verify existing hold invoice flow is unaffected
  • Manual lncli verification:
    • lncli addholdinvoice 10000 (legacy positional arg still works)
    • lncli addholdinvoice --hash= 10000 (new flag)
    • lncli addholdinvoice --preimage= 10000 (server derives hash)
    • lncli addholdinvoice 10000 (auto-generate)

Pull Request Checklist

Testing

  • Your PR passes all CI checks.
  • Tests covering the positive and negative (error paths) are included.
  • Bug fixes contain tests triggering the bug to prevent regressions.

Code Style and Documentation

📝 Please see our Contribution Guidelines for further guidance.

saubyk added 3 commits March 27, 2026 19:04
Make the hash field optional in AddHoldInvoiceRequest and add a new
preimage field. When neither hash nor preimage is provided, the server
auto-generates a random preimage and derives the hash. When a preimage
is provided, the server validates it and derives the hash from it.

The response now includes payment_preimage (populated when the server
knows the preimage) and payment_hash (always populated), giving
callers everything needed for the full hold invoice lifecycle.

This leverages the existing mppPaymentHashAndPreimage logic in
AddInvoiceData which already handles all four input combinations.
Add --hash and --preimage flags to the lncli addholdinvoice command.
The old positional hash argument still works for backward compatibility
(detected by 64 hex char length). The --preimage flag is validated
client-side to be exactly 64 hex characters. When neither flag is
provided, the server auto-generates both values.
Add integration test covering all four AddHoldInvoice input modes:
auto-generate (neither hash nor preimage), user-supplied preimage,
hash-only (backward compat), and the error case when both are set.
Each subtest verifies the full payment lifecycle including settle.
@saubyk saubyk added this to the v0.22.0 milestone Mar 28, 2026
@saubyk saubyk self-assigned this Mar 28, 2026
@saubyk saubyk added this to lnd v0.22 Mar 28, 2026
@gemini-code-assist
Copy link
Copy Markdown

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request enhances the AddHoldInvoice functionality by introducing more flexible preimage and hash management. It allows users to either provide a preimage for the server to derive a hash from, or opt for the server to auto-generate both a preimage and hash. The changes include updates to the RPC interface, command-line tool, and server-side logic, ensuring backward compatibility while providing robust error handling for invalid configurations.

Highlights

  • AddHoldInvoice Enhancements: Updated AddHoldInvoice to support optional preimage/hash generation, allowing the server to auto-generate a preimage or derive a hash from a user-supplied preimage.
  • RPC Interface Updates: Modified the AddHoldInvoiceRequest and AddHoldInvoiceResp protobuf definitions to include new fields for preimage and hash handling.
  • New Integration Tests: Added a new integration test suite, testHoldInvoiceAutoGenerate, to verify the new functionality across various modes including auto-generation, user-supplied preimage, and legacy hash-only flows.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@saubyk saubyk moved this from Todo to In progress in lnd v0.22 Mar 28, 2026
@saubyk saubyk changed the title Update AddHoldInvoice to add support for optional presage/hash generation Update AddHoldInvoice to add support for optional preimage/hash generation Mar 28, 2026
Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request enhances the AddHoldInvoice RPC and CLI command to support optional hash and preimage parameters. Users can now provide a preimage directly (from which the server derives the hash), provide a hash as before, or provide neither to have the server auto-generate both. The changes include updates to the protobuf definitions, CLI flag handling, server-side logic, and a comprehensive integration test suite. I have no feedback to provide.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: In progress

Development

Successfully merging this pull request may close these issues.

[feature]: New rpc for generating pre-image and corresponding hash

1 participant