diff --git a/Cargo.lock b/Cargo.lock index 031b3f4550758c9b421d34b60b4ca6c144ff9ec4..b246e363a78a8422c8eeb8dda61b597f9d61a28f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -343,11 +343,6 @@ dependencies = [ "libc 0.2.65 (registry+https://github.com/rust-lang/crates.io-index)", ] -[[package]] -name = "base58" -version = "0.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" - [[package]] name = "base64" version = "0.9.3" @@ -443,6 +438,11 @@ dependencies = [ "libc 0.2.65 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "bs58" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "bumpalo" version = "2.6.0" @@ -838,9 +838,9 @@ dependencies = [ name = "dup-crypto" version = "0.7.0" dependencies = [ - "base58 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "base64 0.10.1 (registry+https://github.com/rust-lang/crates.io-index)", "bincode 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)", + "bs58 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "byteorder 1.3.2 (registry+https://github.com/rust-lang/crates.io-index)", "chacha20-poly1305-aead 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", "clear_on_drop 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1124,7 +1124,6 @@ dependencies = [ name = "durs-network-documents" version = "0.4.0" dependencies = [ - "base58 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "bincode 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "dubp-common-doc 0.1.0", "dubp-currency-params 0.2.0", @@ -3366,7 +3365,6 @@ dependencies = [ "checksum awc 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "5e995283278dd3bf0449e7534e77184adb1570c0de8b6a50bf7c9d01ad8db8c4" "checksum backtrace 0.3.40 (registry+https://github.com/rust-lang/crates.io-index)" = "924c76597f0d9ca25d762c25a4d369d51267536465dc5064bdf0eb073ed477ea" "checksum backtrace-sys 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)" = "5d6575f128516de27e3ce99689419835fce9643a9b215a14d2b5b685be018491" -"checksum base58 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "5024ee8015f02155eee35c711107ddd9a9bf3cb689cf2a9089c97e79b6e1ae83" "checksum base64 0.10.1 (registry+https://github.com/rust-lang/crates.io-index)" = "0b25d992356d2eb0ed82172f5248873db5560c4721f564b13cb5193bda5e668e" "checksum base64 0.9.3 (registry+https://github.com/rust-lang/crates.io-index)" = "489d6c0ed21b11d038c31b6ceccca973e65d73ba3bd8ecb9a2babf5546164643" "checksum bincode 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b8ab639324e3ee8774d296864fbc0dbbb256cf1a41c490b94cba90c082915f92" @@ -3376,6 +3374,7 @@ dependencies = [ "checksum block-padding 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "fa79dedbb091f449f1f39e53edf88d5dbe95f895dae6135a8d7b881fb5af73f5" "checksum brotli-sys 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "4445dea95f4c2b41cde57cc9fee236ae4dbae88d8fcbdb4750fc1bb5d86aaecd" "checksum brotli2 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "0cb036c3eade309815c15ddbacec5b22c4d1f3983a774ab2eac2e3e9ea85568e" +"checksum bs58 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b170cd256a3f9fa6b9edae3e44a7dfdfc77e8124dbc3e2612d75f9c3e2396dae" "checksum bumpalo 2.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ad807f2fc2bf185eeb98ff3a901bd46dc5ad58163d0fa4577ba0d25674d71708" "checksum byte-tools 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "e3b5ca7a04898ad4bcd41c90c5285445ff5b791899bb1b0abdd2a2aa791211d7" "checksum byteorder 1.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "a7c3dd8985a7111efc5c80b44e23ecdd8c007de8ade3b96595387e812b957cf5" diff --git a/lib/crypto/Cargo.toml b/lib/crypto/Cargo.toml index 497a2849bb393f2fb3e0a0432747ae155351eee1..bc0eca70d9587ef462f6a8388a450bfb07bd3425 100644 --- a/lib/crypto/Cargo.toml +++ b/lib/crypto/Cargo.toml @@ -13,8 +13,8 @@ edition = "2018" path = "src/lib.rs" [dependencies] -base58 = "0.1.*" base64 = "0.10.*" +bs58 = "0.3.0" byteorder = "1.2.3" chacha20-poly1305-aead = "0.1.2" clear_on_drop = "0.2.3" diff --git a/lib/crypto/src/bases/b58.rs b/lib/crypto/src/bases/b58.rs index 53686c245d28914adc20486480e47c3c3b144507..592f2076830d8e61573ed339cf1552a684805508 100644 --- a/lib/crypto/src/bases/b58.rs +++ b/lib/crypto/src/bases/b58.rs @@ -15,14 +15,17 @@ //! Provide base58 convertion tools -pub use base58::ToBase58; - use crate::bases::BaseConvertionError; -use base58::{FromBase58, FromBase58Error}; + +/// Convert to base58 string +pub trait ToBase58 { + /// Convert to base58 string + fn to_base58(&self) -> String; +} /// Create an array of 32 bytes from a Base58 string. pub fn str_base58_to_32bytes(base58_data: &str) -> Result<[u8; 32], BaseConvertionError> { - match base58_data.from_base58() { + match bs58::decode(base58_data).into_vec() { Ok(result) => { if result.len() == 32 { let mut u8_array = [0; 32]; @@ -37,15 +40,24 @@ pub fn str_base58_to_32bytes(base58_data: &str) -> Result<[u8; 32], BaseConverti }) } } - Err(FromBase58Error::InvalidBase58Character(character, offset)) => { - Err(BaseConvertionError::InvalidCharacter { character, offset }) + Err(bs58::decode::Error::InvalidCharacter { character, index }) => { + Err(BaseConvertionError::InvalidCharacter { + character, + offset: index, + }) } - Err(FromBase58Error::InvalidBase58Length) => { + Err(bs58::decode::Error::BufferTooSmall) => { Err(BaseConvertionError::InvalidBaseConverterLength) } + _ => Err(BaseConvertionError::UnknownError), } } +/// Create a Base58 string from a slice of bytes. +pub fn bytes_to_str_base58(bytes: &[u8]) -> String { + bs58::encode(bytes).into_string() +} + /*/// Create an array of 64bytes from a Base58 string. pub fn str_base58_to_64bytes(base58_data: &str) -> Result<[u8; 64], BaseConvertionError> { match base58_data.from_base58() { diff --git a/lib/crypto/src/bases/mod.rs b/lib/crypto/src/bases/mod.rs index 62dc0e8d48cbcfbadcda912b25bbcb345844506d..4bb0dc9f75b197aa65c1433a088387e7496c5988 100644 --- a/lib/crypto/src/bases/mod.rs +++ b/lib/crypto/src/bases/mod.rs @@ -57,6 +57,9 @@ pub enum BaseConvertionError { /// Offset (=position) offset: usize, }, + /// Unknown error + #[fail(display = "Unknown error.")] + UnknownError, } impl From<base64::DecodeError> for BaseConvertionError { diff --git a/lib/crypto/src/keys/ed25519.rs b/lib/crypto/src/keys/ed25519.rs index e51c8304c7710e721a77ca31e7aac8acf2c71408..bd998df7226b6b0cf989299e148160b3fb551fa8 100644 --- a/lib/crypto/src/keys/ed25519.rs +++ b/lib/crypto/src/keys/ed25519.rs @@ -20,9 +20,9 @@ //! [`KeyPairGenerator`]: struct.KeyPairGenerator.html use super::PublicKey as PublicKeyMethods; +use crate::bases::b58::{bytes_to_str_base58, ToBase58}; use crate::bases::*; use crate::seeds::Seed32; -use base58::ToBase58; use base64; use clear_on_drop::clear::Clear; use ring::signature::{Ed25519KeyPair as RingKeyPair, KeyPair, UnparsedPublicKey, ED25519}; @@ -148,13 +148,13 @@ pub struct PublicKey(pub [u8; 32]); impl ToBase58 for PublicKey { fn to_base58(&self) -> String { - self.0.to_base58() + bytes_to_str_base58(&self.0[..]) } } impl Display for PublicKey { fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> { - write!(f, "{}", self.to_base58()) + write!(f, "{}", bytes_to_str_base58(&self.0[..])) } } @@ -369,7 +369,6 @@ mod tests { use super::*; use crate::keys::{KeyPair, Sig, Signator, Signature}; use crate::seeds::Seed32; - use base58::FromBase58; use bincode; use std::collections::hash_map::DefaultHasher; @@ -430,7 +429,10 @@ mod tests { " ) .unwrap_err(), - BaseConvertionError::InvalidBaseConverterLength + BaseConvertionError::InvalidLength { + expected: 32, + found: 161 + } ); } @@ -441,8 +443,8 @@ mod tests { // Test base58 encoding/decoding (loop for every bytes) assert_eq!(public_key.to_base58(), public58); - let public_raw = public58.from_base58().unwrap(); - assert_eq!(public_raw, public_key.to_bytes_vector()); + let public_raw = b58::str_base58_to_32bytes(public58).unwrap(); + assert_eq!(public_raw.to_vec(), public_key.to_bytes_vector()); for (key, raw) in public_key.0.iter().zip(public_raw.iter()) { assert_eq!(key, raw); } @@ -488,7 +490,10 @@ mod tests { " ) .unwrap_err(), - BaseConvertionError::InvalidBaseConverterLength + BaseConvertionError::InvalidLength { + expected: 32, + found: 161 + } ); } diff --git a/lib/crypto/src/keys/mod.rs b/lib/crypto/src/keys/mod.rs index 0b90c88f1586750735eaefdd30cbf00516ee7cfa..f3a142e5fcbdee8c1ecf93ce1a01259522e2f16a 100644 --- a/lib/crypto/src/keys/mod.rs +++ b/lib/crypto/src/keys/mod.rs @@ -54,8 +54,8 @@ pub mod text_signable; pub use crate::seeds::Seed32; +use crate::bases::b58::ToBase58; use crate::bases::BaseConvertionError; -use base58::ToBase58; use durs_common_tools::fatal_error; use failure::Fail; use std::fmt::Debug; diff --git a/lib/crypto/src/seeds.rs b/lib/crypto/src/seeds.rs index 0dcca5eeaebb95f513c821981317d1fcd61af849..ea7a91ea5d2f7de3404a36382bb45734f738808d 100644 --- a/lib/crypto/src/seeds.rs +++ b/lib/crypto/src/seeds.rs @@ -15,8 +15,8 @@ //! Provide wrappers around cryptographic seeds +use crate::bases::b58::{bytes_to_str_base58, ToBase58}; use crate::bases::*; -use base58::ToBase58; use clear_on_drop::clear::Clear; use durs_common_tools::fatal_error; use log::error; @@ -77,7 +77,7 @@ impl AsRef<[u8]> for Seed32 { impl ToBase58 for Seed32 { fn to_base58(&self) -> String { - self.0.to_base58() + bytes_to_str_base58(&self.0[..]) } } diff --git a/lib/dunp/network-documents/Cargo.toml b/lib/dunp/network-documents/Cargo.toml index b1e327bac71a9ca6b86e2a308a2a0adc5d2cc43c..e0843e450446f01edaf87daa0f03698c24077ac5 100644 --- a/lib/dunp/network-documents/Cargo.toml +++ b/lib/dunp/network-documents/Cargo.toml @@ -12,7 +12,6 @@ edition = "2018" path = "src/lib.rs" [dependencies] -base58 = "0.1.*" dubp-common-doc = { path = "../../dubp/common-doc" } #, version = "0.1.0" } dubp-currency-params = { path = "../../dubp/currency-params" } dubp-user-docs= { path = "../../dubp/user-docs" } diff --git a/lib/dunp/network-documents/src/network_head_v3.rs b/lib/dunp/network-documents/src/network_head_v3.rs index 1dddb956ac7fccadeae2f061c42646424788c634..109d09b2e2213dcee5ccd46dd5eb9fb773f0b08e 100644 --- a/lib/dunp/network-documents/src/network_head_v3.rs +++ b/lib/dunp/network-documents/src/network_head_v3.rs @@ -17,11 +17,11 @@ use crate::network_head::NetworkHead; use crate::*; -use base58::ToBase58; use dubp_common_doc::blockstamp::Blockstamp; use dubp_common_doc::traits::ToStringObject; use dubp_common_doc::{BlockHash, BlockNumber}; use dubp_currency_params::CurrencyName; +use dup_crypto::bases::b58::ToBase58; use dup_crypto::keys::text_signable::TextSignable; use dup_crypto::keys::*; use pest::iterators::Pair; diff --git a/lib/dunp/network-documents/src/network_peer.rs b/lib/dunp/network-documents/src/network_peer.rs index c125d4266a141db022f72321d7d9b49c0247a40d..2ea02da7c2f7ca7ed676905d1353e98a9c6010c8 100644 --- a/lib/dunp/network-documents/src/network_peer.rs +++ b/lib/dunp/network-documents/src/network_peer.rs @@ -17,11 +17,11 @@ use crate::network_endpoint::*; use crate::*; -use base58::ToBase58; use dubp_common_doc::blockstamp::Blockstamp; use dubp_common_doc::traits::ToStringObject; use dubp_common_doc::BlockNumber; use dubp_currency_params::CurrencyName; +use dup_crypto::bases::b58::ToBase58; use dup_crypto::keys::text_signable::TextSignable; use dup_crypto::keys::*; use pest::iterators::Pair;