From 96131816012fe63beaa7d219d11c853d4d1ae475 Mon Sep 17 00:00:00 2001 From: Riccardo Casatta Date: Wed, 18 Nov 2020 14:05:55 +0100 Subject: [PATCH] Split invalid version for address and extended key, saving also 16 bytes on the stack --- src/util/address.rs | 2 +- src/util/base58.rs | 9 ++++++--- src/util/ecdsa.rs | 2 +- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/util/address.rs b/src/util/address.rs index ca97dd74..36576375 100644 --- a/src/util/address.rs +++ b/src/util/address.rs @@ -507,7 +507,7 @@ impl FromStr for Address { Network::Testnet, 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 { diff --git a/src/util/base58.rs b/src/util/base58.rs index 3fb7cf95..c2900a1d 100644 --- a/src/util/base58.rs +++ b/src/util/base58.rs @@ -32,8 +32,10 @@ pub enum Error { /// Note that if the length is excessively long the provided length may be /// an estimate (and the checksum step may be skipped). InvalidLength(usize), - /// Version byte(s) were not recognized - InvalidVersion(Vec), + /// Extended Key version byte(s) were not recognized + InvalidExtendedKeyVersion([u8; 4]), + /// Address version byte were not recognized + InvalidAddressVersion(u8), /// Checked data was less than 4 bytes TooShort(usize), /// 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::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::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::Secp256k1(ref e) => fmt::Display::fmt(&e, f), } diff --git a/src/util/ecdsa.rs b/src/util/ecdsa.rs index 99d7af67..d528d76c 100644 --- a/src/util/ecdsa.rs +++ b/src/util/ecdsa.rs @@ -247,7 +247,7 @@ impl PrivateKey { let network = match data[0] { 128 => Network::Bitcoin, 239 => Network::Testnet, - x => { return Err(Error::Base58(base58::Error::InvalidVersion(vec![x]))); } + x => { return Err(Error::Base58(base58::Error::InvalidAddressVersion(x))); } }; Ok(PrivateKey {