Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: improved crypto visibility #2

Merged
merged 1 commit into from
Apr 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions src/crypto/base58/mod.rs
Original file line number Diff line number Diff line change
@@ -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] =
Expand All @@ -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),
Expand All @@ -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;
Expand Down Expand Up @@ -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<String>) -> Result<Vec<u8>, Error> {
pub fn b58_decode(encoded: impl Into<String>) -> Result<Vec<u8>, Error> {
let encoded: String = encoded.into();

if encoded.is_empty() {
Expand Down Expand Up @@ -109,7 +109,7 @@ pub(crate) fn b58_decode(encoded: impl Into<String>) -> Result<Vec<u8>, Error> {
}

/// Encode a byte slice into a `Base58Check` encoded string.
pub(crate) fn base58check_encode(hash: &[u8], network: impl Into<BitcoinNetworkVersion>) -> String {
pub fn base58check_encode(hash: &[u8], network: impl Into<BitcoinNetworkVersion>) -> String {
let version = network.into().version();

let mut payload = Vec::with_capacity(21);
Expand All @@ -130,7 +130,7 @@ pub(crate) fn base58check_encode(hash: &[u8], network: impl Into<BitcoinNetworkV
}

/// Decode a `Base58Check` encoded string into a byte vector.
pub(crate) fn base58check_decode(
pub fn base58check_decode(
address: impl Into<String>,
) -> Result<(Vec<u8>, BitcoinNetworkVersion), Error> {
let address: String = address.into();
Expand Down
4 changes: 2 additions & 2 deletions src/crypto/base58/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
10 changes: 3 additions & 7 deletions src/crypto/bip32/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -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)
}

Expand All @@ -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)
}

Expand All @@ -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)
}
}
19 changes: 8 additions & 11 deletions src/crypto/c32/mod.rs
Original file line number Diff line number Diff line change
@@ -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";

Expand All @@ -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,
Expand All @@ -35,7 +35,7 @@ pub(crate) enum Error {
FromUtf8Error(#[from] std::string::FromUtf8Error),
}

pub(crate) fn c32_encode(data: &[u8]) -> Result<String, Error> {
pub fn c32_encode(data: &[u8]) -> Result<String, Error> {
let mut encoded = Vec::new();

let mut buffer = 0u32;
Expand Down Expand Up @@ -75,7 +75,7 @@ pub(crate) fn c32_encode(data: &[u8]) -> Result<String, Error> {
Ok(String::from_utf8(encoded)?)
}

pub(crate) fn c32_decode(input: impl Into<String>) -> Result<Vec<u8>, Error> {
pub fn c32_decode(input: impl Into<String>) -> Result<Vec<u8>, Error> {
let input: String = input.into();

if !input.is_ascii() {
Expand Down Expand Up @@ -136,7 +136,7 @@ pub(crate) fn c32_decode(input: impl Into<String>) -> Result<Vec<u8>, Error> {
Ok(decoded)
}

pub(crate) fn c32check_encode(data: &[u8], version: u8) -> Result<String, Error> {
pub fn c32check_encode(data: &[u8], version: u8) -> Result<String, Error> {
let mut check = vec![version];
check.extend_from_slice(data);
let checksum = DSha256Hash::from_slice(&check).checksum();
Expand All @@ -151,7 +151,7 @@ pub(crate) fn c32check_encode(data: &[u8], version: u8) -> Result<String, Error>
Ok(String::from_utf8(encoded)?)
}

pub(crate) fn c32check_decode(input: impl Into<String>) -> Result<(Vec<u8>, u8), Error> {
pub fn c32check_decode(input: impl Into<String>) -> Result<(Vec<u8>, u8), Error> {
let input: String = input.into();

if !input.is_ascii() {
Expand Down Expand Up @@ -179,10 +179,7 @@ pub(crate) fn c32check_decode(input: impl Into<String>) -> Result<(Vec<u8>, u8),
Ok((bytes.to_vec(), check[0]))
}

pub(crate) fn c32_address(
data: &[u8],
network: impl Into<StacksNetworkVersion>,
) -> Result<String, Error> {
pub fn c32_address(data: &[u8], network: impl Into<StacksNetworkVersion>) -> Result<String, Error> {
let version = network.into().version();

if ![22, 26, 20, 21].contains(&version) {
Expand All @@ -194,7 +191,7 @@ pub(crate) fn c32_address(
Ok(address)
}

pub(crate) fn c32_address_decode(address: impl Into<String>) -> Result<(Vec<u8>, u8), Error> {
pub fn c32_address_decode(address: impl Into<String>) -> Result<(Vec<u8>, u8), Error> {
let address: String = address.into();

if !address.starts_with('S') {
Expand Down
4 changes: 2 additions & 2 deletions src/crypto/c32/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
22 changes: 11 additions & 11 deletions src/crypto/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand All @@ -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];
Expand All @@ -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]);
Expand All @@ -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();

Expand All @@ -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]);
Expand All @@ -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);
Expand All @@ -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]);
Expand Down
10 changes: 5 additions & 5 deletions src/crypto/hex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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<Self, Error> {
pub fn new(value: &'a str) -> Result<Self, Error> {
let value_len = value.len();

if value_len % 2 > 0 {
Expand Down Expand Up @@ -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<String>) -> Result<Vec<u8>, Error> {
pub fn hex_to_bytes(value: impl Into<String>) -> Result<Vec<u8>, Error> {
let value: String = value.into();
let value_len = value.len();

Expand All @@ -69,7 +69,7 @@ pub(crate) fn hex_to_bytes(value: impl Into<String>) -> Result<Vec<u8>, 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();
Expand Down
9 changes: 5 additions & 4 deletions src/crypto/mod.rs
Original file line number Diff line number Diff line change
@@ -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;