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

tari-project / tari_utilities / 5543897128

pending completion
5543897128

push

github

web-flow
feat: ledger compatability (#54)

This PR is to add no_std support. This is in-line to ensure that
tari_utilities can compile for nonstandard tier 1 rust targets. This is
required for the push to ensure we can create a ledger-compatible
library and allow us to run some of the handy utilities from this crate.

The changes can be summarized in the following:

- Made the library `no_std`
- Upgraded borsch to version 0.10, up from 0.9
- Added feature flags to hide crates and features that are not possible
on `no_std` targets

Feature flags added:

- `std` - This will allow epoch_time, encoding and safe_array
- `zero` - This will include zeroize support
- `serialize` - This will include support for serde
- `borsh` - This will include support for borsh
- `default` - This will include all feature flags and allow all
utilities.

---------

Co-authored-by: stringhandler <stringhandler@gmail.com>

31 of 31 new or added lines in 7 files covered. (100.0%)

779 of 814 relevant lines covered (95.7%)

2.65 hits per line

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

96.3
/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
pub trait Base58 {
33
    /// Convert from base58 string.
34
    fn from_base58(hex: &str) -> Result<Self, Base58Error>
35
    where Self: Sized;
36

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

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

51
impl<T: ByteArray> Base58 for T {
52
    fn from_base58(data: &str) -> Result<Self, Base58Error>
5✔
53
    where Self: Sized {
5✔
54
        let bytes = base58_monero::decode(data).map_err(|e| Base58Error::DecodeError { reason: e.to_string() })?;
5✔
55
        Self::from_bytes(&bytes).map_err(|e| Base58Error::ByteArrayError { reason: e.to_string() })
3✔
56
    }
5✔
57

58
    fn to_base58(&self) -> String {
3✔
59
        base58_monero::encode(self.as_bytes()).expect("base58_monero::encode is infallible")
3✔
60
    }
3✔
61
}
62

63
#[cfg(test)]
64
mod test {
65
    use alloc::vec::Vec;
66

67
    use rand::{rngs::OsRng, RngCore};
68

69
    use super::*;
70

71
    #[test]
1✔
72
    fn decoding() {
1✔
73
        assert_eq!(Vec::from_base58("111111").unwrap(), vec![0; 4]);
1✔
74
        assert_eq!(Vec::from_base58("11115Q").unwrap(), vec![0, 0, 0, 255]);
1✔
75
        assert!(Vec::from_base58("11111O").is_err());
1✔
76
        assert!(Vec::from_base58("🖖🥴").is_err());
1✔
77
    }
1✔
78

79
    #[test]
1✔
80
    fn encoding() {
1✔
81
        assert_eq!(vec![0; 4].to_base58(), "111111");
1✔
82
        assert_eq!(vec![0, 2, 250, 39].to_base58(), "111zzz");
1✔
83
    }
1✔
84

85
    #[test]
1✔
86
    fn inverse_operations() {
1✔
87
        let mut bytes = vec![0; 10];
1✔
88
        OsRng.fill_bytes(&mut bytes);
1✔
89
        assert_eq!(Vec::from_base58(&bytes.to_base58()).unwrap(), bytes);
1✔
90
    }
1✔
91
}
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

© 2025 Coveralls, Inc