Skip to content

feat: add onchain address validation for network-specific addresses #519

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

Merged
merged 1 commit into from
May 14, 2025

Conversation

Camillarhi
Copy link
Contributor

Addresses need to be validated against the target network before use to ensure correctness
and prevent potential loss of funds.

This commit introduces a function to validate addresses based on the target network:

  • Implements a function to validate addresses based on the network.
  • Adds unit tests to verify the correctness of the validation logic.

This change enhances the robustness of address handling and mitigates risks associated with invalid or mismatched addresses.

Fixes #386

@ldk-reviews-bot
Copy link

ldk-reviews-bot commented Apr 4, 2025

👋 Thanks for assigning @tnull as a reviewer!
I'll wait for their review and will help manage the review process.
Once they submit their review, I'll check if a second reviewer would be helpful.

@ldk-reviews-bot ldk-reviews-bot requested a review from jkczyz April 4, 2025 23:56
@ldk-reviews-bot
Copy link

🔔 1st Reminder

Hey @jkczyz! This PR has been waiting for your review.
Please take a look when you have a chance. If you're unable to review, please let us know so we can find another reviewer.

Copy link
Contributor

@enigbe enigbe left a comment

Choose a reason for hiding this comment

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

Hi @Camillarhi,

Thanks for picking this. I reviewed this over the weekend and from my understanding of the issue, the expectation is to expose functionality for the bindings targets, so that they can validate addresses against the network.

We use UniFFI to generate the bindings targeting ldk-node, defining interfaces in here. Your approach uses an already exposed interface OnchainPayment. You can update the interface to add parse_and_validate_address.

@tnull is this enough of a middle ground or do you have other ideas? Would it be preferable to adapt rust-bitcoin's Address and implement the validation on the wrapper?

@tnull tnull requested review from tnull and removed request for jkczyz April 7, 2025 07:58
Copy link
Collaborator

@tnull tnull left a comment

Choose a reason for hiding this comment

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

Thanks for looking into this!

@tnull
Copy link
Collaborator

tnull commented Apr 7, 2025

@tnull is this enough of a middle ground or do you have other ideas? Would it be preferable to adapt rust-bitcoin's Address and implement the validation on the wrapper?

Yeah, honestly, I'm a bit on the fence. rust-bitcoin's enforced validation via types is a bit cumbersome, and generally something I don't want to force down our user's throats, especially in bindings targets where it may be even less idiomatic. Maybe just making sure to validate the addresses before using them is the way to go, we just have to make sure we're not missing any codepaths.

Copy link
Collaborator

@tnull tnull left a comment

Choose a reason for hiding this comment

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

Ah, seems CI is also unhappy due to formatting issues. Please make sure to run cargo fmt before committing.

@enigbe
Copy link
Contributor

enigbe commented Apr 8, 2025

Yeah, honestly, I'm a bit on the fence. rust-bitcoin's enforced validation via types is a bit cumbersome, and generally something I don't want to force down our user's throats, especially in bindings targets where it may be even less idiomatic. Maybe just making sure to validate the addresses before using them is the way to go, we just have to make sure we're not missing any codepaths.

Thanks for clarifying this.

pub fn parse_and_validate_address(
&self, network: Network, address: &Address,
) -> Result<Address, Error> {
Address::<NetworkUnchecked>::from_str(address.to_string().as_str())
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is kind of annoying, but I guess there is no other way to do this if we don't want to take an UncheckedAddress type ourselves, so fine to leave as is I guess.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Alright , do I go ahead and expose this functionality for the binding targets?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yes please go ahead.

Copy link
Collaborator

@tnull tnull left a comment

Choose a reason for hiding this comment

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

Seems CI is still failing, please let me know when this is ready for another round of review.

@Camillarhi
Copy link
Contributor Author

Seems CI is still failing, please let me know when this is ready for another round of review.

Hello @tnull , this is ready for another review, though the CI is still failing

@Camillarhi Camillarhi force-pushed the validate-onchain-address branch from 63a6895 to 8273f07 Compare April 28, 2025 20:54
Copy link
Collaborator

@tnull tnull left a comment

Choose a reason for hiding this comment

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

Excuse the delay here. Basically looks good, just a few minor comments.

Copy link
Collaborator

@tnull tnull left a comment

Choose a reason for hiding this comment

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

Please excuse the delay here!

LGTM!

Please squash the fixup commits into the feature commits to cleanup the commit history. Feel free to address the nit below while squashing.

Note it's usually prerable to keep a clean commit history at all times, and to clearly mark the fixup commits as you go. This link is a good starting point for some guidance on how to write commit messages: https://cbea.ms/git-commit/

@Camillarhi
Copy link
Contributor Author

Please excuse the delay here!

LGTM!

Please squash the fixup commits into the feature commits to cleanup the commit history. Feel free to address the nit below while squashing.

Note it's usually prerable to keep a clean commit history at all times, and to clearly mark the fixup commits as you go. This link is a good starting point for some guidance on how to write commit messages: https://cbea.ms/git-commit/

Thanks, I will get on this and tag you as soon as it's done

@Camillarhi Camillarhi force-pushed the validate-onchain-address branch 2 times, most recently from 1f16658 to 5030750 Compare May 13, 2025 23:12
@Camillarhi
Copy link
Contributor Author

Please excuse the delay here!

LGTM!

Please squash the fixup commits into the feature commits to cleanup the commit history. Feel free to address the nit below while squashing.

Note it's usually prerable to keep a clean commit history at all times, and to clearly mark the fixup commits as you go. This link is a good starting point for some guidance on how to write commit messages: https://cbea.ms/git-commit/

Hello @tnull , I have squashed the commits and cleaned up the commit history.

Validate onchain addresses match their network type before processing
transactions. This prevents sending funds to invalid addresses by:
- Checking address network match expected network
- Providing clear error messages for mismatches

The validation is now integrated into Wallet::send_to_address flow
@Camillarhi Camillarhi force-pushed the validate-onchain-address branch from 5030750 to 94b32c9 Compare May 14, 2025 08:46
@tnull tnull merged commit 5586b69 into lightningdevkit:main May 14, 2025
15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Validate onchain addresses (in bindings)
4 participants