8000 Improve `sighash_u32` handling by dpc · Pull Request #401 · rust-bitcoin/rust-bitcoin · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Improve sighash_u32 handling #401

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 2 commits into from
Dec 7, 2020
Merged

Improve sighash_u32 handling #401

merged 2 commits into from
Dec 7, 2020

Conversation

dpc
Copy link
Contributor
@dpc dpc commented Jan 25, 2020

The fact that sighash_u32 is u32 is a bit confusing and inconvenient.

First commit adds some explanation.

Second helps with the convenience.

@stevenroose
Copy link
Collaborator
stevenroose 8000 commented Jan 25, 2020

This means we will be supporting using sighash flags that we don't know about. Unknown sighash flags will almost certainly have new semantics that this method is not taking into consideration.

Instead of this, I'd prefer to just take a SigHashType argument so that we are sure we're never using unknown sighash flags.

@dpc
Copy link
Contributor Author
dpc commented Jan 25, 2020

@stevenroose I'm not really sure what you mean (what would you prefer the code to look like? take SigHashType directly? I was told that it is useful to support arbitrary u32 because that's what used for reply protection with Bitcoin Cash, and it is useful. And I'm not really changing existing API, other than just allowing passing SigHashType directly without calling .as_u32() on SigHashType::All etc.

@stevenroose
Copy link
Collaborator

Well, we don't support Bitcoin Cash.

What I suggest is not allowing to use an arbitrary u32. While segwit made this unlikely, but if ever a softfork is introduced to the legacy sighash mechanism, or perhaps more likely if segwit would introduce new sighash types that the legacy CHECKSIG doesn't support, people could pass in those into this method, expecting it to generate the new sighash type. But if rust-bitcoin wasn't updated yet for this change, or if they are using an older rust-bitcoin version, the signature hash will be incorrect.

@dr-orlovsky
Copy link
Collaborator

Soft fork definetely takes more coordination effort than changing a set of enum fields in an open-source library like this after the actual softfork happened. So I am after having enum type as an input w/o Other(u32), but maybe with #[non_exhaustive], indicating that a softfork may change this, but nobody knows how so you should be able to generate a error in _ case. While being not a person who decides, this is the case when I am quite sure why it's important to do this way.

@apoelstra
Copy link
Member

I like this Into approach. Needs rebase.

@@ -618,6 +623,11 @@ impl SigHashType {
pub fn as_u32(&self) -> u32 { *self as u32 }
}

impl From<SigHashType> for u32 {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Hmm, how come you're allowed to do that here? I thought you could only make trait implementation "in the same crate as the definition of either the trait or the type implemented on"? I thought for this we needed impl Into<u32> for SigHashType instead. Interesting...

Copy link
Member

Choose a reason for hiding this comment

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

Yeah this also surprises me.

Copy link
Contributor Author
@dpc dpc Nov 25, 2020

Choose a reason for hiding this comment

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

Orphan rules are more complex than that. My understanding is that since SigHashType is defined in the current crate, there can be no other crates that would be able to impl From<SigHashType> for u32 as all types would be foreign to it. I don't remember all the rules but my mental model is "if anything in that impl block is yours, you're free to do it, unless you can't".

The best thing I could google about it RN is rust-lang/rfcs#1856

Copy link
Collaborator

Choose a reason for hiding this comment

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

Hmm, this then somehow makes the assumption that "stdlib can never impl traits using types outside of stdlib", which is a special assumption that only holds for stdlib and not for other crates.

It does make sense though. I often implement Into in that case instead of From, but a From impl results in an Into impl but not the other way around IIRC. Even though Into is usually the one used in methods.

@@ -325,7 +329,8 @@ impl Transaction {
/// # Panics
/// Panics if `input_index` is greater than or equal to `self.input.len()`
///
pub fn signature_hash(&self, input_index: usize, script_pubkey: &Script, sighash_u32: u32) -> SigHash {
pub fn signature_hash<U: Into<u32>>(&self, input_index: usize, script_pubkey: &Script, sighash_u32: U) -> SigHash {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Perhaps also rename the argument to sighash_type then?

@dpc
Copy link
Contributor Author
dpc commented Nov 25, 2020

Rebased.

Copy link
Member
@apoelstra apoelstra left a comment

Choose a reason for hiding this comment

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

Nice

@stevenroose stevenroose merged commit 02c3d8f into rust-bitcoin:master Dec 7, 2020
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.

4 participants
0