Replace external uses of base58 encoding with Base58CkString
What changed, and why it matters
This commit is a routine internal code cleanup in the rust-bitcoin project. It swaps old base58 encoding helper functions for a new equivalent type, Base58CkString, in three source files. There is no indication from the commit itself that this fixes a security bug; it appears to be part of a refactoring to remove deprecated functions.
No security action required. Treat as normal refactoring; verify via existing tests that base58 output remains unchanged.
Security signals we found
No security-relevant signal in the diff: equivalent API swap only
No new dependencies or unsafe blocks introduced
No changes to input validation, parsing, or secret handling
Evidence from the diff
The diff replaces calls to base58::encode_check and base58::encode_check_to_fmt with base58::Base58CkString::encode_unbounded, then adapts surrounding code to use .as_str() or .fmt(fmt). The change is behavior-preserving: the same base58-check payload is produced, only through a different API. No logic changes, bounds checks, or cryptographic operations are modified.
Changed components
bitcoin/src/address/mod.rscrypto/src/key.rskey_expression/src/bip32.rsInspect captured patch +10 / −10
diff --git a/bitcoin/src/address/mod.rs b/bitcoin/src/address/mod.rs
index 4964f06b..b085668c 100644
--- a/bitcoin/src/address/mod.rs
+++ b/bitcoin/src/address/mod.rs
@@ -195,7 +195,7 @@ impl fmt::Display for AddressInner {
NetworkKind::Test => PUBKEY_ADDRESS_PREFIX_TEST,
};
prefixed[1..].copy_from_slice(hash.as_byte_array());
- base58::encode_check_to_fmt(fmt, &prefixed[..])
+ base58::Base58CkString::encode_unbounded(&prefixed[..]).fmt(fmt)
}
P2sh { hash, network } => {
let mut prefixed = [0; 21];
@@ -204,7 +204,7 @@ impl fmt::Display for AddressInner {
NetworkKind::Test => SCRIPT_ADDRESS_PREFIX_TEST,
};
prefixed[1..].copy_from_slice(hash.as_byte_array());
- base58::encode_check_to_fmt(fmt, &prefixed[..])
+ base58::Base58CkString::encode_unbounded(&prefixed[..]).fmt(fmt)
}
Segwit { program, hrp } => {
let hrp = hrp.to_hrp();
@@ -1620,13 +1620,13 @@ mod tests {
let mut payload = [0u8; 22]; // Invalid: should be 21
payload[0] = PUBKEY_ADDRESS_PREFIX_MAIN;
- let encoded = base58::encode_check(&payload);
+ let encoded = base58::Base58CkString::encode_unbounded(&payload);
- let err = Address::<NetworkUnchecked>::from_base58_str(&encoded).unwrap_err();
+ let err = Address::<NetworkUnchecked>::from_base58_str(encoded.as_str()).unwrap_err();
match err {
Base58Error::InvalidBase58PayloadLength(inner) => {
assert_eq!(inner.invalid_base58_payload_length(), 22); // Payload size
- assert_ne!(inner.invalid_base58_payload_length(), encoded.len()); // Not string size
+ assert_ne!(inner.invalid_base58_payload_length(), encoded.as_str().len()); // Not string size
}
other => panic!("unexpected error: {other:?}"),
}
diff --git a/crypto/src/key.rs b/crypto/src/key.rs
index 6d7ddd2a..28770338 100644
--- a/crypto/src/key.rs
+++ b/crypto/src/key.rs
@@ -1185,11 +1185,11 @@ impl WifKey {
ret[1..33].copy_from_slice(&self.private_key.as_inner()[..]);
let privkey = if self.private_key.compressed() {
ret[33] = 1;
- base58::encode_check(&ret[..])
+ base58::Base58CkString::encode_unbounded(&ret[..])
} else {
- base58::encode_check(&ret[..33])
+ base58::Base58CkString::encode_unbounded(&ret[..33])
};
- fmt.write_str(&privkey)
+ fmt.write_str(privkey.as_str())
}
/// Gets the WIF encoding of this private key.
diff --git a/key_expression/src/bip32.rs b/key_expression/src/bip32.rs
index c34d4d1d..15b493ea 100644
--- a/key_expression/src/bip32.rs
+++ b/key_expression/src/bip32.rs
@@ -1053,7 +1053,7 @@ impl Xpub {
impl fmt::Display for Xpriv {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
- base58::encode_check_to_fmt(fmt, &self.encode()[..])
+ base58::Base58CkString::encode_unbounded(&self.encode()).fmt(fmt)
}
}
@@ -1075,7 +1075,7 @@ impl FromStr for Xpriv {
impl fmt::Display for Xpub {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
- base58::encode_check_to_fmt(fmt, &self.encode()[..])
+ base58::Base58CkString::encode_unbounded(&self.encode()[..]).fmt(fmt)
}
}
Why this scored 17/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.