From 4314ae8d965e0931db85f6af39cbd047912f3928 Mon Sep 17 00:00:00 2001 From: mxnkarou Date: Sat, 15 Apr 2023 01:51:21 +0200 Subject: [PATCH] feat: improved crypto visibility --- src/crypto/base58/mod.rs | 12 ++++++------ src/crypto/base58/network.rs | 4 ++-- src/crypto/bip32/mod.rs | 10 +++------- src/crypto/c32/mod.rs | 19 ++++++++----------- src/crypto/c32/network.rs | 4 ++-- src/crypto/hash.rs | 22 +++++++++++----------- src/crypto/hex.rs | 10 +++++----- src/crypto/mod.rs | 9 +++++---- 8 files changed, 42 insertions(+), 48 deletions(-) diff --git a/src/crypto/base58/mod.rs b/src/crypto/base58/mod.rs index 7f804d2..924389b 100644 --- a/src/crypto/base58/mod.rs +++ b/src/crypto/base58/mod.rs @@ -1,7 +1,7 @@ -use crate::crypto::base58::network::BitcoinNetworkVersion; use crate::crypto::hash::DSha256Hash; pub(crate) mod network; +pub use network::BitcoinNetworkVersion; /// `Base58` alphabet, used for encoding/decoding. pub(crate) const B58_ALPHABET: &[u8; 58] = @@ -24,7 +24,7 @@ pub(crate) const B58_BYTE_MAP: [i8; 256] = [ /// Error variants for `Base58` encoding/decoding. #[derive(thiserror::Error, Clone, Debug, Eq, PartialEq)] -pub(crate) enum Error { +pub enum Error { /// Invalid character. #[error("Invalid B58 character: {0}")] InvalidChar(char), @@ -34,7 +34,7 @@ pub(crate) enum Error { } /// Encode a byte slice into a `Base58` string. -pub(crate) fn b58_encode(data: &[u8]) -> String { +pub fn b58_encode(data: &[u8]) -> String { let mut zeros = 0; while zeros < data.len() && data[zeros] == 0 { zeros += 1; @@ -68,7 +68,7 @@ pub(crate) fn b58_encode(data: &[u8]) -> String { } /// Decode a `Base58` string into a byte vector. -pub(crate) fn b58_decode(encoded: impl Into) -> Result, Error> { +pub fn b58_decode(encoded: impl Into) -> Result, Error> { let encoded: String = encoded.into(); if encoded.is_empty() { @@ -109,7 +109,7 @@ pub(crate) fn b58_decode(encoded: impl Into) -> Result, Error> { } /// Encode a byte slice into a `Base58Check` encoded string. -pub(crate) fn base58check_encode(hash: &[u8], network: impl Into) -> String { +pub fn base58check_encode(hash: &[u8], network: impl Into) -> String { let version = network.into().version(); let mut payload = Vec::with_capacity(21); @@ -130,7 +130,7 @@ pub(crate) fn base58check_encode(hash: &[u8], network: impl Into, ) -> Result<(Vec, BitcoinNetworkVersion), Error> { let address: String = address.into(); diff --git a/src/crypto/base58/network.rs b/src/crypto/base58/network.rs index 5d15c41..27dcfd3 100644 --- a/src/crypto/base58/network.rs +++ b/src/crypto/base58/network.rs @@ -9,7 +9,7 @@ pub enum BitcoinNetworkVersion { } impl BitcoinNetworkVersion { - pub(crate) fn version(self) -> u8 { + pub fn version(self) -> u8 { match self { BitcoinNetworkVersion::MainnetP2PKH => 0, BitcoinNetworkVersion::MainnetP2SH => 5, @@ -18,7 +18,7 @@ impl BitcoinNetworkVersion { } } - pub(crate) fn to_stacks_network_version(self) -> StacksNetworkVersion { + pub fn to_stacks_network_version(self) -> StacksNetworkVersion { match self { BitcoinNetworkVersion::MainnetP2PKH => StacksNetworkVersion::MainnetP2PKH, BitcoinNetworkVersion::MainnetP2SH => StacksNetworkVersion::MainnetP2SH, diff --git a/src/crypto/bip32/mod.rs b/src/crypto/bip32/mod.rs index 413a015..10a67ba 100644 --- a/src/crypto/bip32/mod.rs +++ b/src/crypto/bip32/mod.rs @@ -131,10 +131,6 @@ impl ExtendedPrivateKey { pub(crate) fn public_key(&self) -> PublicKey { self.private_key.public_key(&Secp256k1::new()) } - - pub(crate) fn into_bytes(&self) -> [u8; KEY_BYTE_SIZE] { - self.private_key.secret_bytes() - } } #[cfg(test)] @@ -154,7 +150,7 @@ mod tests { let seed = Mnemonic::parse(MNEMONIC_PHRASE).unwrap().to_seed(""); let key = super::ExtendedPrivateKey::from_seed(seed).unwrap(); - assert_eq!(key.into_bytes(), expected_bytes); + assert_eq!(key.private_key.secret_bytes(), expected_bytes); assert_eq!(key.depth, expected_depth) } @@ -172,7 +168,7 @@ mod tests { let child = key.child(1.into()).unwrap(); - assert_eq!(child.into_bytes(), expected_bytes); + assert_eq!(child.private_key.secret_bytes(), expected_bytes); assert_eq!(child.depth, expected_depth) } @@ -191,7 +187,7 @@ mod tests { .derive("m/0/2147483647'/1/2147483646'/2") .unwrap(); - assert_eq!(key.into_bytes(), expected_bytes); + assert_eq!(key.private_key.secret_bytes(), expected_bytes); assert_eq!(key.depth, expected_depth) } } diff --git a/src/crypto/c32/mod.rs b/src/crypto/c32/mod.rs index b9e82da..a9bb61c 100644 --- a/src/crypto/c32/mod.rs +++ b/src/crypto/c32/mod.rs @@ -1,7 +1,7 @@ -use crate::crypto::c32::network::StacksNetworkVersion; use crate::crypto::hash::DSha256Hash; pub(crate) mod network; +pub use network::StacksNetworkVersion; pub(crate) const C32_ALPHABET: &[u8; 32] = b"0123456789ABCDEFGHJKMNPQRSTVWXYZ"; @@ -15,7 +15,7 @@ pub(crate) const C32_BYTE_MAP: [i8; 128] = [ ]; #[derive(thiserror::Error, Clone, Debug, Eq, PartialEq)] -pub(crate) enum Error { +pub enum Error { /// Invalid C32 string. #[error("Invalid C32 string")] InvalidC32, @@ -35,7 +35,7 @@ pub(crate) enum Error { FromUtf8Error(#[from] std::string::FromUtf8Error), } -pub(crate) fn c32_encode(data: &[u8]) -> Result { +pub fn c32_encode(data: &[u8]) -> Result { let mut encoded = Vec::new(); let mut buffer = 0u32; @@ -75,7 +75,7 @@ pub(crate) fn c32_encode(data: &[u8]) -> Result { Ok(String::from_utf8(encoded)?) } -pub(crate) fn c32_decode(input: impl Into) -> Result, Error> { +pub fn c32_decode(input: impl Into) -> Result, Error> { let input: String = input.into(); if !input.is_ascii() { @@ -136,7 +136,7 @@ pub(crate) fn c32_decode(input: impl Into) -> Result, Error> { Ok(decoded) } -pub(crate) fn c32check_encode(data: &[u8], version: u8) -> Result { +pub fn c32check_encode(data: &[u8], version: u8) -> Result { let mut check = vec![version]; check.extend_from_slice(data); let checksum = DSha256Hash::from_slice(&check).checksum(); @@ -151,7 +151,7 @@ pub(crate) fn c32check_encode(data: &[u8], version: u8) -> Result Ok(String::from_utf8(encoded)?) } -pub(crate) fn c32check_decode(input: impl Into) -> Result<(Vec, u8), Error> { +pub fn c32check_decode(input: impl Into) -> Result<(Vec, u8), Error> { let input: String = input.into(); if !input.is_ascii() { @@ -179,10 +179,7 @@ pub(crate) fn c32check_decode(input: impl Into) -> Result<(Vec, u8), Ok((bytes.to_vec(), check[0])) } -pub(crate) fn c32_address( - data: &[u8], - network: impl Into, -) -> Result { +pub fn c32_address(data: &[u8], network: impl Into) -> Result { let version = network.into().version(); if ![22, 26, 20, 21].contains(&version) { @@ -194,7 +191,7 @@ pub(crate) fn c32_address( Ok(address) } -pub(crate) fn c32_address_decode(address: impl Into) -> Result<(Vec, u8), Error> { +pub fn c32_address_decode(address: impl Into) -> Result<(Vec, u8), Error> { let address: String = address.into(); if !address.starts_with('S') { diff --git a/src/crypto/c32/network.rs b/src/crypto/c32/network.rs index c8be9b9..1bac737 100644 --- a/src/crypto/c32/network.rs +++ b/src/crypto/c32/network.rs @@ -9,7 +9,7 @@ pub enum StacksNetworkVersion { } impl StacksNetworkVersion { - pub(crate) fn version(self) -> u8 { + pub fn version(self) -> u8 { match self { StacksNetworkVersion::MainnetP2PKH => 22, StacksNetworkVersion::MainnetP2SH => 20, @@ -18,7 +18,7 @@ impl StacksNetworkVersion { } } - pub(crate) fn to_bitcoin_network_version(self) -> BitcoinNetworkVersion { + pub fn to_bitcoin_network_version(self) -> BitcoinNetworkVersion { match self { StacksNetworkVersion::MainnetP2PKH => BitcoinNetworkVersion::MainnetP2PKH, StacksNetworkVersion::MainnetP2SH => BitcoinNetworkVersion::MainnetP2SH, diff --git a/src/crypto/hash.rs b/src/crypto/hash.rs index 0cf25fb..798c9bf 100644 --- a/src/crypto/hash.rs +++ b/src/crypto/hash.rs @@ -6,11 +6,11 @@ use ripemd::Ripemd160; macro_rules! impl_hash { ($name:ident, $size:expr) => { impl $name { - pub(crate) fn as_bytes(&self) -> &[u8] { + pub fn as_bytes(&self) -> &[u8] { &self.0 } - pub(crate) fn into_bytes(self) -> [u8; $size] { + pub fn into_bytes(self) -> [u8; $size] { self.0 } } @@ -20,11 +20,11 @@ macro_rules! impl_hash { pub(crate) const SHA256_ENCODED_SIZE: usize = 32; #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)] -pub(crate) struct Sha256Hash([u8; SHA256_ENCODED_SIZE]); +pub struct Sha256Hash([u8; SHA256_ENCODED_SIZE]); impl_hash!(Sha256Hash, SHA256_ENCODED_SIZE); impl Sha256Hash { - pub(crate) fn from_slice(value: &[u8]) -> Self { + pub fn from_slice(value: &[u8]) -> Self { let bytes = { let mut ctx = Context::new(&HashSha256); let mut buff = [0u8; SHA256_ENCODED_SIZE]; @@ -37,7 +37,7 @@ impl Sha256Hash { Sha256Hash(bytes) } - pub(crate) fn checksum(&self) -> [u8; 4] { + pub fn checksum(&self) -> [u8; 4] { let bytes = self.as_bytes(); let mut buff = [0u8; 4]; buff.copy_from_slice(&bytes[0..4]); @@ -46,11 +46,11 @@ impl Sha256Hash { } #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)] -pub(crate) struct DSha256Hash([u8; SHA256_ENCODED_SIZE]); +pub struct DSha256Hash([u8; SHA256_ENCODED_SIZE]); impl_hash!(DSha256Hash, SHA256_ENCODED_SIZE); impl DSha256Hash { - pub(crate) fn from_slice(value: &[u8]) -> Self { + pub fn from_slice(value: &[u8]) -> Self { let sha = Sha256Hash::from_slice(value); let bytes = sha.as_bytes(); @@ -63,7 +63,7 @@ impl DSha256Hash { DSha256Hash(buff) } - pub(crate) fn checksum(&self) -> [u8; 4] { + pub fn checksum(&self) -> [u8; 4] { let bytes = self.as_bytes(); let mut buff = [0u8; 4]; buff.copy_from_slice(&bytes[0..4]); @@ -74,11 +74,11 @@ impl DSha256Hash { pub(crate) const HASH160_ENCODED_SIZE: usize = 20; #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)] -pub(crate) struct Ripemd160Hash([u8; HASH160_ENCODED_SIZE]); +pub struct Ripemd160Hash([u8; HASH160_ENCODED_SIZE]); impl_hash!(Ripemd160Hash, HASH160_ENCODED_SIZE); impl Ripemd160Hash { - pub(crate) fn from_slice(value: &[u8]) -> Self { + pub fn from_slice(value: &[u8]) -> Self { let mut buff = [0u8; HASH160_ENCODED_SIZE]; let sha = Sha256Hash::from_slice(value); @@ -90,7 +90,7 @@ impl Ripemd160Hash { Ripemd160Hash(buff) } - pub(crate) fn checksum(&self) -> [u8; 4] { + pub fn checksum(&self) -> [u8; 4] { let bytes = self.as_bytes(); let mut buff = [0u8; 4]; buff.copy_from_slice(&bytes[0..4]); diff --git a/src/crypto/hex.rs b/src/crypto/hex.rs index f5ba24b..0367b97 100644 --- a/src/crypto/hex.rs +++ b/src/crypto/hex.rs @@ -2,7 +2,7 @@ use std::fmt::Write; /// Error variants for Hex encoding/decoding. #[derive(thiserror::Error, Clone, Debug, Eq, PartialEq)] -pub(crate) enum Error { +pub enum Error { /// Non-hexadecimal character. #[error("Invalid hex character")] InvalidChar, @@ -11,12 +11,12 @@ pub(crate) enum Error { UnpaddedHex(String, usize), } -pub(crate) struct HexIterator<'a> { +pub struct HexIterator<'a> { iter: std::str::Bytes<'a>, } impl<'a> HexIterator<'a> { - pub(crate) fn new(value: &'a str) -> Result { + pub fn new(value: &'a str) -> Result { let value_len = value.len(); if value_len % 2 > 0 { @@ -52,7 +52,7 @@ impl<'a> Iterator for HexIterator<'a> { } /// Convert a hex string to a byte array. -pub(crate) fn hex_to_bytes(value: impl Into) -> Result, Error> { +pub fn hex_to_bytes(value: impl Into) -> Result, Error> { let value: String = value.into(); let value_len = value.len(); @@ -69,7 +69,7 @@ pub(crate) fn hex_to_bytes(value: impl Into) -> Result, Error> { } /// Convert a byte array to a hex string. -pub(crate) fn bytes_to_hex(value: &[u8]) -> String { +pub fn bytes_to_hex(value: &[u8]) -> String { let mut buff = String::with_capacity(value.len()); for b in value.iter() { write!(buff, "{b:02x}").unwrap(); diff --git a/src/crypto/mod.rs b/src/crypto/mod.rs index 5c06bef..8038c1d 100644 --- a/src/crypto/mod.rs +++ b/src/crypto/mod.rs @@ -1,5 +1,6 @@ -pub(crate) mod base58; pub(crate) mod bip32; -pub(crate) mod c32; -pub(crate) mod hash; -pub(crate) mod hex; + +pub mod base58; +pub mod c32; +pub mod hash; +pub mod hex;