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

rust: simplify bech32 hrp check #1315

Merged
merged 1 commit into from
Oct 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions src/rust/bitbox02-rust/src/hww/api/cardano/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,8 @@ pub const ADDRESS_HASH_SIZE: usize = 28;
fn decode_shelley_payment_address(params: &params::Params, address: &str) -> Result<Vec<u8>, ()> {
let result =
bech32::primitives::decode::CheckedHrpstring::new::<bech32::Bech32>(address).or(Err(()))?;
// TODO: use `result.hrp().as_str()` once bech32 has a new release.
let hrp: String = result.hrp().char_iter().collect();
if hrp != params.bech32_hrp_payment {
let hrp = result.hrp();
if hrp.as_str() != params.bech32_hrp_payment {
Comment on lines +46 to +47
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
let hrp = result.hrp();
if hrp.as_str() != params.bech32_hrp_payment {
if result.hrp().as_str() != params.bech32_hrp_payment {

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Can't do this as as_str() returns a reference to something inside the hrp instance, so the hrp() value needs to stay alive :(.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I see 👍 thanks

return Err(());
}
let data: Vec<u8> = result.byte_iter().collect();
Expand Down
3 changes: 1 addition & 2 deletions src/rust/streaming-silent-payments/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,7 @@ fn decode_address(address: &str, expected_hrp: &str) -> Result<SilentPaymentAddr
.map_err(|_| ())?;

let hrp = decoded_addr.hrp();
let hrp: &str = hrp.as_str();
if hrp != expected_hrp {
if hrp.as_str() != expected_hrp {
Comment on lines 79 to +80
Copy link
Collaborator

Choose a reason for hiding this comment

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

Same here, I think you could get rid of the hrp variable.

return Err(());
}
let witness_version = decoded_addr.remove_witness_version().unwrap();
Expand Down