Skip to content
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

Improving bip32 ChildNumber display implementation #567

Merged
merged 1 commit into from Mar 12, 2021
Merged

Improving bip32 ChildNumber display implementation #567

merged 1 commit into from Mar 12, 2021

Conversation

dr-orlovsky
Copy link
Collaborator

Trivial changes:

  1. Support for _h format in hardened indexes using alternate format string (format!("{:#}", ChildNumber::from_hardened_idx(5)) will print 5h instead of 5')
  2. Using Display trait allows custom number formatting (for instance format!("{:#06}", ChildNumber::from_hardened_idx(5)) will print 000005h)

@dr-orlovsky dr-orlovsky added this to the 0.26.1 milestone Feb 6, 2021
Copy link
Contributor

@sgeisler sgeisler left a comment

Choose a reason for hiding this comment

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

I fixed the inverted alt mode error and added some tests in a9cf712, feel free to just use that instead. It should also fix the 1.29.0 compatibility issue.

ChildNumber::Normal { index } => write!(f, "{}", index),
ChildNumber::Hardened { index } => {
fmt::Display::fmt(&index, f)?;
f.write_str(if f.alternate() { "'" } else { "h" })
Copy link
Contributor

Choose a reason for hiding this comment

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

This is reversed, in normal mode it should still print ' and not h.

Also: just out of curiosity, what's the performance characteristic of write_str vs write_char? Even if it doesn't get optimized out it's probably nothing major though.

Copy link
Member

Choose a reason for hiding this comment

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

Hmm, I think we should default to h rather than '.

Copy link
Contributor

Choose a reason for hiding this comment

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

We can certainly do that, but it would break backwards compatibility in some sense. What would be the reason to do so?

Copy link
Member

Choose a reason for hiding this comment

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

' sucks, it breaks shell escaping and is harder to read. I'm okay with the compatibility breakage.

Alternately we could make {#} output the h form like was suggested in the Decimal PR

Copy link
Contributor

Choose a reason for hiding this comment

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

Alternately we could make {#} output the h form like was suggested in the Decimal PR

That's what I wanted. But I'm ok with {} outputting h and {#} outputting ' too if you consider our current behavior of always outputting ' bad.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Alternately we could make {#} output the h form like was suggested in the Decimal PR

This is what happens now with the present version of the code: Sorry, this code does what you originaly suggested: displays h as a default for hardened derivation.

                f.write_str(if f.alternate() { "'" } else { "h" })

So do I need to change this?

Copy link
Contributor

Choose a reason for hiding this comment

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

The current version (f.write_str(if f.alternate() { "'" } else { "h" })) is what @apoelstra wants afaik, but breaks compatibility. I'm fine with that as long as it happens consciously and not accidentally.

@dr-orlovsky
Copy link
Collaborator Author

@sgeisler thank you for fixing! But how I can cherry-pick a detached head on GitHub?

@sgeisler
Copy link
Contributor

sgeisler commented Feb 8, 2021

You can fetch it with git fetch origin a9cf712192b3ef7929580c8679d567733f2f065f and then do a hard reset I guess since I just amended your commit. Just change whatever you like with another amendment.

@dr-orlovsky
Copy link
Collaborator Author

@apoelstra updated with @sgeisler 1.29 fix and displays h instead of ' by default (uses ' for the {#}). Pls let me know will this work for both of you

Copy link
Contributor

@sgeisler sgeisler left a comment

Choose a reason for hiding this comment

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

ACK 017cd71

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.

ack 017cd71

@apoelstra
Copy link
Member

I'm comfortable merging this in a minor release, but maybe some others disagree?

@sgeisler
Copy link
Contributor

I'm comfortable merging this in a minor release, but maybe some others disagree?

I'm ok with it.

@TheBlueMatt
Copy link
Member

TheBlueMatt commented Mar 12, 2021 via email

@apoelstra apoelstra merged commit bee5e8a into rust-bitcoin:master Mar 12, 2021
sgeisler added a commit to sgeisler/rust-bitcoin that referenced this pull request Jun 8, 2021
Fixes rust-bitcoin#608. In rust-bitcoin#567 the Display impl for ChildNumber was
consciously changed, assuming the semver break would not
affect any correctly implemented downstream projects. We
were wrong.
sgeisler added a commit to sgeisler/rust-bitcoin that referenced this pull request Jun 8, 2021
Fixes rust-bitcoin#608. In rust-bitcoin#567 the Display impl for ChildNumber was
consciously changed, assuming the semver break would not
affect any correctly implemented downstream projects. We
were wrong.
tobin-crypto pushed a commit to monacohq/rust-bitcoin that referenced this pull request Sep 8, 2021
Fixes rust-bitcoin#608. In rust-bitcoin#567 the Display impl for ChildNumber was
consciously changed, assuming the semver break would not
affect any correctly implemented downstream projects. We
were wrong.
tcharding pushed a commit to tcharding/rust-bitcoin that referenced this pull request Sep 10, 2021
Fixes rust-bitcoin#608. In rust-bitcoin#567 the Display impl for ChildNumber was
consciously changed, assuming the semver break would not
affect any correctly implemented downstream projects. We
were wrong.
tobin-crypto pushed a commit to monacohq/rust-bitcoin that referenced this pull request Oct 6, 2021
Fixes rust-bitcoin#608. In rust-bitcoin#567 the Display impl for ChildNumber was
consciously changed, assuming the semver break would not
affect any correctly implemented downstream projects. We
were wrong.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants