Skip to content

Commit

Permalink
Update crates
Browse files Browse the repository at this point in the history
  • Loading branch information
cryptoquick committed Jun 5, 2024
1 parent ba836bf commit 8354e02
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 24 deletions.
5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ include = ["src/**/*", "LICENSE", "README.md"]

[dependencies]
bao = "0.12.1"
bech32 = "0.9.1"
bech32 = "0.11.0"
bitmask-enum = "2.1.0"
bytes = "1.4.0"
ecies = { version = "0.2.6", default-features = false, features = [
Expand All @@ -23,7 +23,7 @@ libsecp256k1 = { version = "0.7.1", features = ["std"] }
log = "0.4.19"
nom = "7.1.3"
pretty_env_logger = "0.5.0"
secp256k1 = { version = "0.28.0", features = [
secp256k1 = { version = "0.29.0", features = [
"global-context",
"rand-std",
"serde",
Expand All @@ -33,6 +33,7 @@ snap = "1.1.0"
thiserror = "1.0"
zfec-rs = "0.1.0"
once_cell = "1.19.0"
libsecp256k1-core = "0.3.0"

[dev-dependencies]
anyhow = "1.0.71"
Expand Down
2 changes: 1 addition & 1 deletion src/decoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ pub fn decode(
padding: u32,
format: u8,
) -> Result<Vec<u8>, CarbonadoError> {
let format = Format::try_from(format)?;
let format = Format::from(format);

let verified = if format.contains(Format::Bao) {
bao(input, hash)?
Expand Down
2 changes: 1 addition & 1 deletion src/encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ pub fn zfec(input: &[u8]) -> Result<(Vec<u8>, u32, u32), CarbonadoError> {
/// `snap -> ecies -> zfec -> bao`
pub fn encode(pubkey: &[u8], input: &[u8], format: u8) -> Result<Encoded, CarbonadoError> {
let input_len = input.len() as u32;
let format = Format::try_from(format)?;
let format = Format::from(format);

let compressed;
let encrypted;
Expand Down
14 changes: 11 additions & 3 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,17 @@ pub enum CarbonadoError {
#[error(transparent)]
HexDecodeError(#[from] hex::FromHexError),

/// Bech32 error
/// Bech32 encode error
#[error(transparent)]
Bech32Error(#[from] bech32::Error),
Bech32EncodeError(#[from] bech32::EncodeError),

/// Bech32 decode error
#[error(transparent)]
Bech32DecodeError(#[from] bech32::DecodeError),

/// Bech32 human readable part error
#[error(transparent)]
InvalidHrp(#[from] bech32::primitives::hrp::Error),

/// snap error
#[error(transparent)]
Expand All @@ -32,7 +40,7 @@ pub enum CarbonadoError {

/// ecies error
#[error(transparent)]
EciesError(#[from] ecies::SecpError),
EciesError(#[from] libsecp256k1_core::Error),

/// bao decode error
#[error(transparent)]
Expand Down
18 changes: 9 additions & 9 deletions src/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ impl TryFrom<&File> for Header {
// Verify hash against signature
signature.verify(&Message::from_digest_slice(&hash)?, &pubkey)?;

let hash = bao::Hash::try_from(hash)?;
let hash = bao::Hash::from(hash);

let format = Format::try_from(format[0])?;
let format = Format::from(format[0]);
let chunk_index = u8::from_le_bytes(chunk_index);
let encoded_len = u32::from_le_bytes(encoded_len);
let padding_len = u32::from_le_bytes(padding_len);
Expand Down Expand Up @@ -135,9 +135,9 @@ impl TryFrom<&[u8]> for Header {
signature.verify(&Message::from_digest_slice(hash)?, &pubkey)?;

let hash: [u8; 32] = hash[0..32].try_into()?;
let hash = bao::Hash::try_from(hash)?;
let hash = bao::Hash::from(hash);

let format = Format::try_from(format)?;
let format = Format::from(format);

Ok(Header {
pubkey,
Expand Down Expand Up @@ -183,9 +183,9 @@ impl TryFrom<Bytes> for Header {
signature.verify(&Message::from_digest_slice(hash)?, &pubkey)?;

let hash: [u8; 32] = hash[0..32].try_into()?;
let hash = bao::Hash::try_from(hash)?;
let hash = bao::Hash::from(hash);

let format = Format::try_from(format)?;
let format = Format::from(format);

Ok(Header {
pubkey,
Expand Down Expand Up @@ -231,9 +231,9 @@ impl TryFrom<&Bytes> for Header {
signature.verify(&Message::from_digest_slice(hash)?, &pubkey)?;

let hash: [u8; 32] = hash[0..32].try_into()?;
let hash = bao::Hash::try_from(hash)?;
let hash = bao::Hash::from(hash);

let format = Format::try_from(format)?;
let format = Format::from(format);

Ok(Header {
pubkey,
Expand Down Expand Up @@ -416,7 +416,7 @@ pub fn encode(

let Encoded(mut encoded, hash, encode_info) = encoding::encode(&pubkey, input, level)?;

let format = Format::try_from(level)?;
let format = Format::from(level);
let header = Header::new(
sk,
&pubkey,
Expand Down
16 changes: 10 additions & 6 deletions src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::sync::Once;

use bao::Hash;
use bech32::{decode, encode, FromBase32, ToBase32, Variant};
use bech32::{decode, encode_to_fmt, Bech32m, Hrp};
use log::trace;

use crate::{
Expand Down Expand Up @@ -54,12 +54,16 @@ pub fn calc_padding_len(input_len: usize) -> (u32, u32) {
}

/// Helper for encoding data to bech32m.
pub fn bech32m_encode(hrp: &str, bytes: &[u8]) -> Result<String, CarbonadoError> {
Ok(encode(hrp, bytes.to_base32(), Variant::Bech32m)?)
pub fn bech32m_encode(hrp_str: &str, bytes: &[u8]) -> Result<String, CarbonadoError> {
let hrp = Hrp::parse(hrp_str).map_err(CarbonadoError::InvalidHrp)?;
let mut buf = String::new();
encode_to_fmt::<Bech32m, String>(&mut buf, hrp, bytes)?;
Ok(buf)
}

/// Helper for decoding bech32-encoded data.
pub fn bech32_decode(bech32_str: &str) -> Result<(String, Vec<u8>, Variant), CarbonadoError> {
let (hrp, words, variant) = decode(bech32_str)?;
Ok((hrp, Vec::<u8>::from_base32(&words)?, variant))
pub fn bech32_decode(bech32_str: &str) -> Result<(String, Vec<u8>), CarbonadoError> {
let (hrp, data) = decode(bech32_str).map_err(CarbonadoError::Bech32DecodeError)?;
let hrp_str = hrp.to_string();
Ok((hrp_str, data))
}
3 changes: 2 additions & 1 deletion tests/codec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ fn codec(path: &str) -> Result<()> {
assert_eq!(decoded, input, "Decoded output is same as encoded input");

let carbonado_level = 15;
let format = Format::try_from(carbonado_level)?;
let format = Format::from(carbonado_level);
let header = Header::new(
&sk.secret_bytes(),
&pk.serialize(),
Expand All @@ -121,6 +121,7 @@ fn codec(path: &str) -> Result<()> {
.read(true)
.write(true)
.create(true)
.truncate(true)
.open(&file_path)?;
file.write_all(&header_bytes)?;
file.write_all(&encoded)?;
Expand Down
3 changes: 2 additions & 1 deletion tests/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ fn format() -> Result<()> {

let input = "Hello world!".as_bytes();
let carbonado_level = 15;
let format = Format::try_from(carbonado_level)?;
let format = Format::from(carbonado_level);

let (file_sk, file_pk) = generate_keypair(&mut thread_rng());
let (node_sk, node_pk) = generate_keypair(&mut thread_rng());
Expand Down Expand Up @@ -63,6 +63,7 @@ fn format() -> Result<()> {
.read(true)
.write(true)
.create(true)
.truncate(true)
.open(file_path)?;
file.write_all(&header_bytes)?;
file.write_all(&encoded)?;
Expand Down

0 comments on commit 8354e02

Please sign in to comment.