Skip to content

Commit 084f7d4

Browse files
committed
Avoid instantiating base58 errors in Address::from_base58
This is propedeutic to upgrading to bitcoin 0.32 where this address-related variants are removed from base58::Error
1 parent 5eefe85 commit 084f7d4

1 file changed

Lines changed: 18 additions & 6 deletions

File tree

src/address.rs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,12 @@ pub enum AddressError {
6262

6363
/// An invalid blinding pubkey was encountered.
6464
InvalidBlindingPubKey(secp256k1_zkp::UpstreamError),
65+
66+
/// The length (in bytes) of the object was not correct.
67+
InvalidLength(usize),
68+
69+
/// Address version byte were not recognized.
70+
InvalidAddressVersion(u8),
6571
}
6672

6773
impl From<crate::bech32::primitives::decode::SegwitHrpstringError> for AddressError {
@@ -111,6 +117,12 @@ impl fmt::Display for AddressError {
111117
AddressError::InvalidSegwitV0Encoding => {
112118
write!(f, "v0 witness program must use b(l)ech32 not b(l)ech32m")
113119
}
120+
AddressError::InvalidLength(len) => {
121+
write!(f, "Address data has invalid length {}", len)
122+
}
123+
AddressError::InvalidAddressVersion(v) => {
124+
write!(f, "address version {} is invalid for this type", v)
125+
}
114126
}
115127
}
116128
}
@@ -478,13 +490,13 @@ impl Address {
478490
let (blinded, prefix) = match data[0] == params.blinded_prefix {
479491
true => {
480492
if data.len() != 55 {
481-
return Err(base58::Error::InvalidLength(data.len()).into());
493+
return Err(AddressError::InvalidLength(data.len()));
482494
}
483495
(true, data[1])
484496
}
485497
false => {
486498
if data.len() != 21 {
487-
return Err(base58::Error::InvalidLength(data.len()).into());
499+
return Err(AddressError::InvalidLength(data.len()));
488500
}
489501
(false, data[0])
490502
}
@@ -506,7 +518,7 @@ impl Address {
506518
} else if prefix == params.p2sh_prefix {
507519
Payload::ScriptHash(ScriptHash::from_slice(payload_data).unwrap())
508520
} else {
509-
return Err(base58::Error::InvalidAddressVersion(prefix).into());
521+
return Err(AddressError::InvalidAddressVersion(prefix));
510522
};
511523

512524
Ok(Address {
@@ -532,7 +544,7 @@ impl Address {
532544

533545
// Base58.
534546
if s.len() > 150 {
535-
return Err(base58::Error::InvalidLength(s.len() * 11 / 15).into());
547+
return Err(AddressError::InvalidLength(s.len() * 11 / 15));
536548
}
537549
let data = base58::decode_check(s)?;
538550
Address::from_base58(&data, params)
@@ -687,11 +699,11 @@ impl FromStr for Address {
687699

688700
// Base58.
689701
if s.len() > 150 {
690-
return Err(base58::Error::InvalidLength(s.len() * 11 / 15).into());
702+
return Err(AddressError::InvalidLength(s.len() * 11 / 15));
691703
}
692704
let data = base58::decode_check(s)?;
693705
if data.is_empty() {
694-
return Err(base58::Error::InvalidLength(data.len()).into());
706+
return Err(AddressError::InvalidLength(data.len()));
695707
}
696708

697709
let p = data[0];

0 commit comments

Comments
 (0)