Skip to content

Commit d8ad98a

Browse files
committed
Merge rust-bitcoin#5093: fix(address): report decoded base58 payload length in error
ac78c57 add test (Daniel) 34b927d fix(address): report decoded base58 payload length in error (Daniel) Pull request description: Ensure `InvalidBase58PayloadLengthError` reflects the length of the decoded base58 payload instead of the input string length. Aligns the error message with the actual bytes produced by`base58::decode_check` and improves diagnostics for malformed legacy addresses ACKs for top commit: apoelstra: ACK ac78c57; successfully ran local tests tcharding: ACK ac78c57 Tree-SHA512: 0af80d44bde3e87919cff3c22dfc6afe0287df098fbca628d8f63544092bafeec209c1a59f81d0b69e007b4d66d031e4c41de4882fea12ea0d277b34cdb9da43
2 parents f02d029 + ac78c57 commit d8ad98a

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

bitcoin/src/address/mod.rs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -919,8 +919,9 @@ impl Address<NetworkUnchecked> {
919919
return Err(LegacyAddressTooLongError { length: s.len() }.into());
920920
}
921921
let data = base58::decode_check(s)?;
922-
let data: &[u8; 21] =
923-
(&*data).try_into().map_err(|_| InvalidBase58PayloadLengthError { length: s.len() })?;
922+
let data: &[u8; 21] = (&*data)
923+
.try_into()
924+
.map_err(|_| InvalidBase58PayloadLengthError { length: data.len() })?;
924925

925926
let (prefix, &data) = data.split_first();
926927

@@ -1602,4 +1603,22 @@ mod tests {
16021603
assert!(address.is_spend_standard());
16031604
assert_eq!(address.address_type(), Some(AddressType::P2a));
16041605
}
1606+
1607+
#[test]
1608+
fn base58_invalid_payload_length_reports_decoded_size() {
1609+
use crate::constants::PUBKEY_ADDRESS_PREFIX_MAIN;
1610+
1611+
let mut payload = [0u8; 22]; // Invalid: should be 21
1612+
payload[0] = PUBKEY_ADDRESS_PREFIX_MAIN;
1613+
let encoded = base58::encode_check(&payload);
1614+
1615+
let err = Address::<NetworkUnchecked>::from_base58_str(&encoded).unwrap_err();
1616+
match err {
1617+
Base58Error::InvalidBase58PayloadLength(inner) => {
1618+
assert_eq!(inner.invalid_base58_payload_length(), 22); // Payload size
1619+
assert_ne!(inner.invalid_base58_payload_length(), encoded.len()); // Not string size
1620+
}
1621+
other => panic!("unexpected error: {other:?}"),
1622+
}
1623+
}
16051624
}

0 commit comments

Comments
 (0)