Split invalid version for address and extended key, saving also 16 bytes on the stack

This commit is contained in:
Riccardo Casatta 2020-11-18 14:05:55 +01:00
parent bace07d8f8
commit 9613181601
No known key found for this signature in database
GPG Key ID: FD986A969E450397
3 changed files with 8 additions and 5 deletions

View File

@ -507,7 +507,7 @@ impl FromStr for Address {
Network::Testnet, Network::Testnet,
Payload::ScriptHash(ScriptHash::from_slice(&data[1..]).unwrap()), Payload::ScriptHash(ScriptHash::from_slice(&data[1..]).unwrap()),
), ),
x => return Err(Error::Base58(base58::Error::InvalidVersion(vec![x]))), x => return Err(Error::Base58(base58::Error::InvalidAddressVersion(x))),
}; };
Ok(Address { Ok(Address {

View File

@ -32,8 +32,10 @@ pub enum Error {
/// Note that if the length is excessively long the provided length may be /// Note that if the length is excessively long the provided length may be
/// an estimate (and the checksum step may be skipped). /// an estimate (and the checksum step may be skipped).
InvalidLength(usize), InvalidLength(usize),
/// Version byte(s) were not recognized /// Extended Key version byte(s) were not recognized
InvalidVersion(Vec<u8>), InvalidExtendedKeyVersion([u8; 4]),
/// Address version byte were not recognized
InvalidAddressVersion(u8),
/// Checked data was less than 4 bytes /// Checked data was less than 4 bytes
TooShort(usize), TooShort(usize),
/// Secp256k1 error while parsing a secret key /// Secp256k1 error while parsing a secret key
@ -46,7 +48,8 @@ impl fmt::Display for Error {
Error::BadByte(b) => write!(f, "invalid base58 character 0x{:x}", b), Error::BadByte(b) => write!(f, "invalid base58 character 0x{:x}", b),
Error::BadChecksum(exp, actual) => write!(f, "base58ck checksum 0x{:x} does not match expected 0x{:x}", actual, exp), Error::BadChecksum(exp, actual) => write!(f, "base58ck checksum 0x{:x} does not match expected 0x{:x}", actual, exp),
Error::InvalidLength(ell) => write!(f, "length {} invalid for this base58 type", ell), Error::InvalidLength(ell) => write!(f, "length {} invalid for this base58 type", ell),
Error::InvalidVersion(ref v) => write!(f, "version {:?} invalid for this base58 type", v), Error::InvalidAddressVersion(ref v) => write!(f, "address version {:?} invalid for this base58 type", v),
Error::InvalidExtendedKeyVersion(ref v) => write!(f, "extended key version {:?} invalid for this base58 type", v),
Error::TooShort(_) => write!(f, "base58ck data not even long enough for a checksum"), Error::TooShort(_) => write!(f, "base58ck data not even long enough for a checksum"),
Error::Secp256k1(ref e) => fmt::Display::fmt(&e, f), Error::Secp256k1(ref e) => fmt::Display::fmt(&e, f),
} }

View File

@ -247,7 +247,7 @@ impl PrivateKey {
let network = match data[0] { let network = match data[0] {
128 => Network::Bitcoin, 128 => Network::Bitcoin,
239 => Network::Testnet, 239 => Network::Testnet,
x => { return Err(Error::Base58(base58::Error::InvalidVersion(vec![x]))); } x => { return Err(Error::Base58(base58::Error::InvalidAddressVersion(x))); }
}; };
Ok(PrivateKey { Ok(PrivateKey {