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
Enforce segwit v0 script validity when creating address. #1021
Conversation
I will also do this in a separate PR. |
Was working on the same issue today. Looks good to me! What do you think about adding tests for the same? |
@@ -672,6 +672,13 @@ impl Address { | |||
|
|||
/// Constructs an [`Address`] from an output script (`scriptPubkey`). | |||
pub fn from_script(script: &script::Script, network: Network) -> Option<Address> { |
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.
It might not be to be done in this PR, but if we have multiple ways of "not turning script into an address", maybe we should make it enum Error { ... list all the ways things can go wrong ... }
, so the caller has something better to show to the user than just "didn't turn into address".
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.
Agreed. Address::from_script()
and a few other functions should return Result<_, Error>
instead of Option<>
but this is for another PR.
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.
#1022 just to not forget.
I have left out tests as I thought the change wasn't so significant and existing tests covered cases where it won't fail, however I am happy to add tests for cases where it is expected to fail and squash it into the commit. |
This would be really great |
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.
ACK 971c285
ACK, though I agree that tests would be greatly appreciated |
971c285
to
6c10d77
Compare
6c10d77 Squashed in unit tests and checks to see if scripts are valid in general if witness version is v0. I also had a play around with trying to add similar checks for v1 scripts for consistency, however adding the checks would fail one case in The particular case that fails when the v1 check is present is: |
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.
ACK 6c10d77
@nlanson I change the issue mention in the PR desciption so that GitHub would pick it up and auto-close the issue when merging. Hope you don't mind. |
We also need to check the length of witness program when creating a witness variant of Payload. Accoridng to definition of witness program in BIP141
We need to check the between 2 and 40 bytes constraint in the |
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.
ACK 6c10d77. Left a comment can be addressed in separate PR.
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.
ACK 6c10d77
@nlanson do you want this one? Just asking so I dont tread on your toes, cheers. |
I believe this is already done: #1022 (comment) |
…ty when creating address. 6c10d77 Address::from_script() - Check witness v0 program lengths. (Noah) Pull request description: Adds a check in `Address::from_script()` that checks if segwit v0 scripts have a valid length. Fix: #995 ACKs for top commit: tcharding: ACK 6c10d77 sanket1729: ACK 6c10d77. Left a comment can be addressed in separate PR. apoelstra: ACK 6c10d77 Tree-SHA512: 32aebb13477958b1455c688f668aaa3d3af4db0a7936b9549bcd1d03bd0e16635b8471549d96f1e8d408d6501e8fb515df2eb86b17a08c3152774a5be78ae8b1
Adds a check in
Address::from_script()
that checks if segwit v0 scripts have a valid length.Fix: #995