Skip to content

Commit

Permalink
chore: dep bumps base64, bs58, pbkdf2, sha2
Browse files Browse the repository at this point in the history
  • Loading branch information
prestwich committed Mar 15, 2023
1 parent a67b16c commit d04f740
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 101 deletions.
2 changes: 1 addition & 1 deletion bip39/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ coins-bip32 = { version ="0.8.1", path = "../bip32" }
hex = "0.4.2"
hmac = "0.12"
once_cell = "1.17.1"
pbkdf2 = "0.11"
pbkdf2 = "0.12"
rand = "0.8.4"
sha2 = "0.10"
thiserror = "1.0"
Expand Down
3 changes: 2 additions & 1 deletion bip39/src/mnemonic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,8 @@ where
salt.as_bytes(),
PBKDF2_ROUNDS,
&mut seed,
);
)
.expect("cannot have invalid length");

Ok(seed)
}
Expand Down
5 changes: 2 additions & 3 deletions core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,17 @@ repository = "https://github.com/summa-tx/bitcoins-rs"
license = "MIT OR Apache-2.0"

[dependencies]
base58check = "0.1.0"
bech32 = "0.7.2"
hex = "0.4.2"
thiserror = "1.0"
base64 = "0.12.0"
base64 = "0.21.0"
serde_derive = "1.0.106"
serde = { version = "1.0.106", features = ["derive"] }
bs58 = { version = "0.4.0", features = ["check"] }

# update in parallel
generic-array = "0.14.4"
digest = "0.10"
blake2 = { version = "0.10", features = ["reset"] }
sha2 = "0.10"
sha3 = "0.10"
ripemd = "0.1"
64 changes: 34 additions & 30 deletions core/src/enc/bases.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ use bech32::{
decode as b32_decode, encode as b32_encode, u5, Error as BechError, FromBase32, ToBase32,
};

use base58check::{FromBase58Check, FromBase58CheckError, ToBase58Check};
use bs58::{
decode as bs58_decode, decode::Error as Bs58DecodeError, encode as bs58_encode,
encode::Error as Bs58EncodeError,
};

use thiserror::Error;

Expand Down Expand Up @@ -35,8 +38,12 @@ pub enum EncodingError {
},

/// Bubbled up error from base58check library
#[error("FromBase58CheckError: {0:?}")]
B58Error(FromBase58CheckError),
#[error("{0}")]
Bs58Decode(#[from] Bs58DecodeError),

/// Bubbled up error from base58check library
#[error("{0}")]
Bs58Encode(#[from] Bs58EncodeError),

/// Bubbled up error from bech32 library
#[error(transparent)]
Expand All @@ -55,13 +62,6 @@ pub enum EncodingError {
InvalidSizeError,
}

/// Impl explicitly because FromBase58CheckError doesn't implement the std error format
impl From<FromBase58CheckError> for EncodingError {
fn from(e: FromBase58CheckError) -> Self {
EncodingError::B58Error(e)
}
}

/// A simple result type alias
pub type EncodingResult<T> = Result<T, EncodingError>;

Expand Down Expand Up @@ -92,21 +92,25 @@ pub fn decode_bech32(expected_hrp: &str, s: &str) -> EncodingResult<(u8, Vec<u8>
}

/// Encodes a byte slice to base58check with the specified version byte.
pub fn encode_base58(version: u8, v: &[u8]) -> String {
v.to_base58check(version)
pub fn encode_base58(v: &[u8]) -> String {
bs58_encode(v).with_check().into_string()
}

/// Decodes base58check into a byte string. Returns a `FromBase58CheckError` if the checksum or
/// encoding is wrong. Returns a `WrongVersion` if it decodes an unexpected version.
pub fn decode_base58(expected_version: u8, s: &str) -> EncodingResult<Vec<u8>> {
let (version, data) = s.from_base58check()?;
if version != expected_version {
return Err(EncodingError::WrongVersion {
got: version,
expected: expected_version,
});
};
Ok(data)
/// Decodes base58check into a byte string. Returns a
/// `EncodingError::Bs58Decode` if unsuccesful
pub fn decode_base58(expected_prefix: u8, s: &str) -> EncodingResult<Vec<u8>> {
let res = bs58_decode(s).with_check(None).into_vec()?;

if let Some(version) = res.first() {
if version != &expected_prefix {
return Err(EncodingError::Bs58Decode(Bs58DecodeError::InvalidVersion {
ver: *version,
expected_ver: expected_prefix,
}));
}
}

Ok(res)
}

#[cfg(test)]
Expand Down Expand Up @@ -150,7 +154,7 @@ mod test {
];
for addr in addrs.iter() {
let s = decode_base58(version, addr).unwrap();
let reencoded = encode_base58(version, &s);
let reencoded = encode_base58(&s);
assert_eq!(*addr, reencoded);
}
}
Expand All @@ -171,7 +175,7 @@ mod test {
];
for addr in addrs.iter() {
let s = decode_base58(version, addr).unwrap();
let reencoded = encode_base58(version, &s);
let reencoded = encode_base58(&s);
assert_eq!(*addr, reencoded);
}
}
Expand All @@ -188,10 +192,10 @@ mod test {
}
match decode_base58(1, "3HXNFmJpxjgTVFN35Y9f6Waje5YFsLEQZ2") {
Ok(_) => panic!("expected an error"),
Err(EncodingError::WrongVersion {
got: _,
expected: _,
}) => {}
Err(EncodingError::Bs58Decode(Bs58DecodeError::InvalidVersion {
ver: 5,
expected_ver: 1,
})) => {}
_ => panic!("Got the wrong error"),
}
match decode_bech32("bc", "bc1qqh9ue57m6227627j8ztscl9") {
Expand All @@ -201,7 +205,7 @@ mod test {
}
match decode_base58(5, "3HXNf6Waje5YFsLEQZ2") {
Ok(_) => panic!("expected an error"),
Err(EncodingError::B58Error(_)) => {}
Err(EncodingError::Bs58Decode(_)) => {}
_ => panic!("Got the wrong error"),
}
}
Expand Down
62 changes: 1 addition & 61 deletions core/src/hashes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use digest::{
core_api::{BlockSizeUser, OutputSizeUser},
HashMarker, Output, VariableOutput,
HashMarker, Output,
};
use std::io::Write;

Expand Down Expand Up @@ -179,66 +179,6 @@ impl digest::Update for Hash160 {
}
}

#[derive(Clone)]
/// A `Digest` implementation that performs Bitcoin style double-sha256
pub struct Blake2b256(blake2::Blake2bVar);

impl std::io::Write for Blake2b256 {
fn flush(&mut self) -> std::io::Result<()> {
Ok(())
}

fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
self.update(buf);
Ok(buf.len())
}
}

impl Default for Blake2b256 {
fn default() -> Self {
Self(<blake2::Blake2bVar as digest::VariableOutput>::new(32).unwrap())
}
}

// there is a blanket implementation for Digest: Update + FixedOutput + Reset + Default + Clone
impl digest::Update for Blake2b256 {
fn update(&mut self, data: &[u8]) {
self.0.update(data.as_ref())
}
}

impl HashMarker for Blake2b256 {}

impl OutputSizeUser for Blake2b256 {
type OutputSize = <sha2::Sha256 as OutputSizeUser>::OutputSize; // cheating
}

impl digest::FixedOutput for Blake2b256 {
fn finalize_into(self, out: &mut DigestOutput<Self>) {
// variable output size is set to 32 matches `out`
self.0
.finalize_variable(out.as_mut())
.expect("correct output size")
}
}

impl digest::FixedOutputReset for Blake2b256 {
// TODO: see if we can avoid cloning hasher state?
fn finalize_into_reset(&mut self, out: &mut Output<Self>) {
self.0
.clone()
.finalize_variable(out.as_mut())
.expect("correct output size");
self.reset();
}
}

impl digest::Reset for Blake2b256 {
fn reset(&mut self) {
self.0.reset()
}
}

marked_digest!(
/// A bitcoin-style Hash160
Hash160Digest,
Expand Down
6 changes: 3 additions & 3 deletions core/src/ser.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! A simple trait for binary (de)Serialization using std `Read` and `Write` traits.
use base64::DecodeError;
use base64::{prelude::*, DecodeError};
use hex::FromHexError;
use std::{
convert::TryInto,
Expand Down Expand Up @@ -332,7 +332,7 @@ pub trait ByteFormat {
where
Self: std::marker::Sized,
{
let v: Vec<u8> = base64::decode(s).map_err(SerError::from)?;
let v: Vec<u8> = BASE64_STANDARD.decode(s).map_err(SerError::from)?;
let mut cursor = Cursor::new(v);
Self::read_from(&mut cursor)
}
Expand All @@ -348,7 +348,7 @@ pub trait ByteFormat {
fn serialize_base64(&self) -> String {
let mut v: Vec<u8> = vec![];
self.write_to(&mut v).expect("No error on heap write");
base64::encode(v)
BASE64_STANDARD.encode(v)
}
}

Expand Down
3 changes: 1 addition & 2 deletions ledger/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,10 @@ hex = "0.4.3"

# native
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
cfg-if = "1.0.0"
lazy_static = "1.3.0"
byteorder = "1.3.1"
blake2b_simd = "0.5.10"
libc = "0.2.50"
cfg-if = "0.1.7"
matches = "0.1.8"
tracing = "0.1.37"

Expand Down

0 comments on commit d04f740

Please sign in to comment.