Skip to content

Commit

Permalink
Change monero encoding trait
Browse files Browse the repository at this point in the history
release new version
  • Loading branch information
SWvheerden committed Oct 10, 2024
1 parent ae44946 commit 8682398
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 10 deletions.
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
# Changelog

All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.

## [0.8.0](https://github.com/tari-project/tari_utilities/compare/v0.7...v0.8.0) (2024-10-10)


### ⚠ BREAKING CHANGES

* Changes the `ByteArray` trait.

### Features

* Changes the `ByteArray` trait.

### [0.7.0](https://github.com/tari-project/tari_utilities/compare/v0.6.1...v0.7.0) (2023-12-06)

### Features
Expand Down
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ repository = "https://github.com/tari-project/tari"
homepage = "https://tari.com"
readme = "README.md"
license = "BSD-3-Clause"
version = "0.7.0"
edition = "2018"
version = "0.8.0"
edition = "2021"

[dependencies]
base58-monero = { version = "0.3", default-features = false,optional = true}
base58-monero = { version = "2.0", default-features = false,optional = true}
base64 = { version = "0.13", default-features = false, optional = true , features = ["alloc"] }
bincode = { version = "1.3", default-features = false, optional = true }
newtype-ops = { version = "0.1", default-features = false , optional = true}
Expand Down
49 changes: 42 additions & 7 deletions src/encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ use snafu::prelude::*;
use crate::ByteArray;

/// Trait for encoding/decoding to base58.
#[deprecated(since = "0.8.0", note = "please use `MBase58` instead")]
pub trait Base58 {
/// Convert from base58 string.
fn from_base58(hex: &str) -> Result<Self, Base58Error>
Expand Down Expand Up @@ -60,6 +61,40 @@ impl<T: ByteArray> Base58 for T {
}
}

/// Trait for encoding/decoding to base58.
pub trait MBase58 {
/// Convert from base58 string.
fn from_monero_base58(hex: &str) -> Result<Self, crate::encoding::Base58Error>
where Self: Sized;

/// Convert to base58 string.
fn to_monero_base58(&self) -> String;
}

/// Errors for trait Base58.
#[derive(Debug, Snafu)]
#[allow(missing_docs)]
pub enum MBase58Error {
#[snafu(display("Byte array error: `{reason}'"))]
ByteArrayError { reason: String },
#[snafu(display("Decode error: `{reason}'"))]
DecodeError { reason: String },
}

impl<T: ByteArray> crate::encoding::MBase58 for T {
fn from_monero_base58(data: &str) -> Result<Self, crate::encoding::Base58Error>
where Self: Sized {
let bytes = base58_monero::decode(data)
.map_err(|e| crate::encoding::Base58Error::DecodeError { reason: e.to_string() })?;
Self::from_canonical_bytes(&bytes)
.map_err(|e| crate::encoding::Base58Error::ByteArrayError { reason: e.to_string() })
}

fn to_monero_base58(&self) -> String {
base58_monero::encode(self.as_bytes()).expect("base58_monero::encode is infallible")
}
}

#[cfg(test)]
mod test {
use alloc::vec::Vec;
Expand All @@ -70,22 +105,22 @@ mod test {

#[test]
fn decoding() {
assert_eq!(Vec::from_base58("111111").unwrap(), vec![0; 4]);
assert_eq!(Vec::from_base58("11115Q").unwrap(), vec![0, 0, 0, 255]);
assert!(Vec::from_base58("11111O").is_err());
assert!(Vec::from_base58("🖖🥴").is_err());
assert_eq!(Vec::from_monero_base58("111111").unwrap(), vec![0; 4]);
assert_eq!(Vec::from_monero_base58("11115Q").unwrap(), vec![0, 0, 0, 255]);
assert!(Vec::from_monero_base58("11111O").is_err());
assert!(Vec::from_monero_base58("🖖🥴").is_err());
}

#[test]
fn encoding() {
assert_eq!(vec![0; 4].to_base58(), "111111");
assert_eq!(vec![0, 2, 250, 39].to_base58(), "111zzz");
assert_eq!(vec![0; 4].to_monero_base58(), "111111");
assert_eq!(vec![0, 2, 250, 39].to_monero_base58(), "111zzz");
}

#[test]
fn inverse_operations() {
let mut bytes = vec![0; 10];
OsRng.fill_bytes(&mut bytes);
assert_eq!(Vec::from_base58(&bytes.to_base58()).unwrap(), bytes);
assert_eq!(Vec::from_monero_base58(&bytes.to_base58()).unwrap(), bytes);
}
}

0 comments on commit 8682398

Please sign in to comment.