8000 Add new hex parse error variant by tcharding · Pull Request #1584 · rust-bitcoin/rust-bitcoin · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Add new hex parse error variant #1584

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

Conversation

tcharding
Copy link
Member
@tcharding tcharding commented Jan 23, 2023

Recently we used an error type that holds only one expected hex string length when parsing but for PublicKeys we have two (66 and 130). Add a new error variant to express the error. Requires adding a variant to bip32 for the same thing.

Fix: #1281

@tcharding tcharding force-pushed the 01-23-hex-parse-error-length branch from 3c5b625 to a705e76 Compare January 23, 2023 04:26
Copy link
Collaborator
@Kixunil Kixunil left a comment

Choose a reason for hiding this comment

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

Apart from naming this looks OK.

@@ -455,6 +455,8 @@ pub enum Error {
Base58(base58::Error),
/// Hexadecimal decoding error
Hex(hex::Error),
/// `PublicKey` hex should be 66 or 130 digits long.
InvalidPublicKeyHex(usize),
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why is this not called InvalidHexLength as below? That one is more clear.

Also, we have this problem everywhere: I think all enum variants should use named fields except for inner errors (e.g. hex::Error in this case) which are obvious enough.

Copy link
Member Author

Choose a reason for hiding this comment

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

I put PublicKey in the name to future proof the varian because the 66/130 in the comments and output are pubkey specific.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Can you explain a bit more what you're future-proofing against?

Copy link
Member Author

Choose a reason for hiding this comment

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

I went digging into the code and my brain to try and remember what I was thinking when I wrote both this PR and the comment above. Seems I misspoke in the comment, I believe I chose the name because the error enum in the bip32 module is more general than just parsing keys, where as in thekey module the error enum is only related to keys. Moreover the InvalidHexLength is only returned when parsing pubkeys. I've changed the bip32 error variant to InvalidPublicKeyHexLength to better show that it is the same as key::InvalidHexLength but with pubkey in the variant to disambiguate in like other bip32::Error variants.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Ah, I think I understand, makes sense.

@tcharding tcharding force-pushed the 01-23-hex-parse-error-length branch from a705e76 to 024f704 Compare January 27, 2023 20:52
Recently we used an error type that holds only one expected hex string
length when parsing but for `PublicKey`s we have two (66 and 130). Add a
new error variant to express the error. Requires adding a variant to
`bip32` for the same thing.

Fix: rust-bitcoin#1281
@tcharding tcharding force-pushed the 01-23-hex-parse-error-length branch from 024f704 to 877f9af Compare January 27, 2023 20:54
Copy link
Collaborator
@Kixunil Kixunil left a comment

Choose a reason for hiding this comment

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

ACK 877f9af

@@ -473,6 +475,7 @@ impl fmt::Display for Error {
write!(f, "encoded extended key data has wrong length {}", len),
Error::Base58(ref e) => write_err!(f, "base58 encoding error"; e),
Error::Hex(ref e) => write_err!(f, "Hexadecimal decoding error"; e),
Error::InvalidPublicKeyHexLength(got) => write!(f, "PublicKey hex should be 66 or 130 digits long, got: {}", got),
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think we should use user friendly descriptions rather than type names in error messages but I guess there are other instances, so we may want to do complete audit and cleanup.

Copy link
Member Author

Choose a reason for hiding this comment

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

Since we are on the topic, error messages should not be capitialised, right? I always forget and we have so many of both forms.

Copy link
Member Author
@tcharding tcharding Jan 28, 2023

Choose a reason for hiding this comment

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

Noted however not to use types in error messages. Will leave it as it is for now since upcoming error work is imminent.

Copy link
Collaborator

Choose a reason for hiding this comment

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

error messages should not be capitialised, right?

I think so.

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 877f9af

8074
@apoelstra apoelstra merged commit a43de83 into rust-bitcoin:master Jan 30, 2023
@tcharding tcharding deleted the 01-23-hex-parse-error-length branch February 4, 2023 02:34
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.

Improve unexpected length error message
3 participants
0