• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

tari-project / tari_utilities / 11273345312

10 Oct 2024 11:44AM UTC coverage: 95.176% (-0.9%) from 96.047%
11273345312

push

github

web-flow
feat!: change Monero encoding trait name (#75)

Changes the Monero base58 encoding name to reflect this is not standard
base58 encoding and rather Monero-specific Base58 encoding. See:
https://docs.getmonero.org/cryptography/base58/

Deprecates old name.

Release new version 0.8

17 of 17 new or added lines in 2 files covered. (100.0%)

8 existing lines in 1 file now uncovered.

730 of 767 relevant lines covered (95.18%)

2395.22 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

72.73
/src/encoding.rs
1
// Copyright 2020. The Tari Project
2
//
3
// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
4
// following conditions are met:
5
//
6
// 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following
7
// disclaimer.
8
//
9
// 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
10
// following disclaimer in the documentation and/or other materials provided with the distribution.
11
//
12
// 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote
13
// products derived from this software without specific prior written permission.
14
//
15
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
16
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
18
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
20
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
21
// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
22

23
//! A trait that handles [base58](https://crates.io/crates/base58-monero) encoding and decoding.
24

25
use alloc::string::{String, ToString};
26

27
use snafu::prelude::*;
28

29
use crate::ByteArray;
30

31
/// Trait for encoding/decoding to base58.
32
#[deprecated(since = "0.8.0", note = "please use `MBase58` instead")]
33
pub trait Base58 {
34
    /// Convert from base58 string.
35
    fn from_base58(hex: &str) -> Result<Self, Base58Error>
36
    where Self: Sized;
37

38
    /// Convert to base58 string.
39
    fn to_base58(&self) -> String;
40
}
41

42
/// Errors for trait Base58.
43
#[derive(Debug, Snafu)]
×
44
#[allow(missing_docs)]
45
pub enum Base58Error {
46
    #[snafu(display("Byte array error: `{reason}'"))]
47
    ByteArrayError { reason: String },
48
    #[snafu(display("Decode error: `{reason}'"))]
49
    DecodeError { reason: String },
50
}
51

52
#[allow(deprecated)]
53
impl<T: ByteArray> Base58 for T {
UNCOV
54
    fn from_base58(data: &str) -> Result<Self, Base58Error>
×
UNCOV
55
    where Self: Sized {
×
UNCOV
56
        let bytes = base58_monero::decode(data).map_err(|e| Base58Error::DecodeError { reason: e.to_string() })?;
×
UNCOV
57
        Self::from_canonical_bytes(&bytes).map_err(|e| Base58Error::ByteArrayError { reason: e.to_string() })
×
UNCOV
58
    }
×
59

UNCOV
60
    fn to_base58(&self) -> String {
×
UNCOV
61
        base58_monero::encode(self.as_bytes()).expect("base58_monero::encode is infallible")
×
UNCOV
62
    }
×
63
}
64

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

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

75
impl<T: ByteArray> crate::encoding::MBase58 for T {
76
    fn from_monero_base58(data: &str) -> Result<Self, Base58Error>
5✔
77
    where Self: Sized {
5✔
78
        let bytes = base58_monero::decode(data).map_err(|e| Base58Error::DecodeError { reason: e.to_string() })?;
5✔
79
        Self::from_canonical_bytes(&bytes)
3✔
80
            .map_err(|e| crate::encoding::Base58Error::ByteArrayError { reason: e.to_string() })
3✔
81
    }
5✔
82

83
    fn to_monero_base58(&self) -> String {
3✔
84
        base58_monero::encode(self.as_bytes()).expect("base58_monero::encode is infallible")
3✔
85
    }
3✔
86
}
87

88
#[cfg(test)]
89
mod test {
90
    use alloc::vec::Vec;
91

92
    use rand_core::{OsRng, RngCore};
93

94
    use super::*;
95

96
    #[test]
97
    fn decoding() {
1✔
98
        assert_eq!(Vec::from_monero_base58("111111").unwrap(), vec![0; 4]);
1✔
99
        assert_eq!(Vec::from_monero_base58("11115Q").unwrap(), vec![0, 0, 0, 255]);
1✔
100
        assert!(Vec::from_monero_base58("11111O").is_err());
1✔
101
        assert!(Vec::from_monero_base58("🖖🥴").is_err());
1✔
102
    }
1✔
103

104
    #[test]
105
    fn encoding() {
1✔
106
        assert_eq!(vec![0; 4].to_monero_base58(), "111111");
1✔
107
        assert_eq!(vec![0, 2, 250, 39].to_monero_base58(), "111zzz");
1✔
108
    }
1✔
109

110
    #[test]
111
    fn inverse_operations() {
1✔
112
        let mut bytes = vec![0; 10];
1✔
113
        OsRng.fill_bytes(&mut bytes);
1✔
114
        assert_eq!(Vec::from_monero_base58(&bytes.to_monero_base58()).unwrap(), bytes);
1✔
115
    }
1✔
116
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc