sn_dbc/
lib.rs

1// Copyright 2023 MaidSafe.net limited.
2//
3// This SAFE Network Software is licensed to you under The General Public License (GPL), version 3.
4// Unless required by applicable law or agreed to in writing, the SAFE Network Software distributed
5// under the GPL Licence is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
6// KIND, either express or implied. Please review the Licences for the specific language governing
7// permissions and limitations relating to use of the SAFE Network Software.
8
9#![allow(clippy::result_large_err)]
10
11mod builder;
12mod dbc;
13mod dbc_ciphers;
14mod dbc_id;
15mod error;
16mod fee_output;
17mod signed_spend;
18mod spentbook;
19mod token;
20mod transaction;
21
22#[cfg(feature = "mock")]
23pub mod mock;
24// re-export crates used in our public API
25pub use crate::{
26    builder::{DbcBuilder, TransactionBuilder},
27    dbc::Dbc,
28    dbc_ciphers::DbcSecrets,
29    dbc_id::{random_derivation_index, DbcId, DerivationIndex, DerivedKey, MainKey, PublicAddress},
30    error::{Error, Result},
31    fee_output::FeeOutput,
32    signed_spend::{SignedSpend, Spend},
33    token::Token,
34    transaction::{DbcTransaction, Input, Output},
35};
36pub use blsttc::{self, rand, Ciphertext, PublicKey, PublicKeySet, Signature, SignatureShare};
37
38#[cfg(feature = "serde")]
39use serde::{Deserialize, Serialize};
40use std::{fmt, str::FromStr};
41
42#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
43#[derive(Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Default)]
44pub struct Hash([u8; 32]);
45
46impl Hash {
47    #[allow(clippy::self_named_constructors)]
48    /// sha3 256 hash
49    pub fn hash(input: &[u8]) -> Self {
50        Self::from(sha3_256(input))
51    }
52
53    /// Access the 32 byte slice of the hash
54    pub fn slice(&self) -> &[u8; 32] {
55        &self.0
56    }
57
58    /// Deserializes a `Hash` represented as a hex string to a `Hash`.
59    pub fn from_hex(hex: &str) -> Result<Self, Error> {
60        let mut h = Self::default();
61        hex::decode_to_slice(hex, &mut h.0)
62            .map_err(|e| Error::HexDeserializationFailed(e.to_string()))?;
63        Ok(h)
64    }
65
66    /// Serialize this `Hash` instance to a hex string.
67    pub fn to_hex(&self) -> String {
68        hex::encode(self.0)
69    }
70}
71
72impl FromStr for Hash {
73    type Err = Error;
74
75    fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
76        Hash::from_hex(s)
77    }
78}
79
80impl From<[u8; 32]> for Hash {
81    fn from(val: [u8; 32]) -> Hash {
82        Hash(val)
83    }
84}
85
86// Display Hash value as hex in Debug output.  consolidates 36 lines to 3 for pretty output
87impl fmt::Debug for Hash {
88    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
89        f.debug_tuple("Hash").field(&self.to_hex()).finish()
90    }
91}
92
93impl AsRef<[u8]> for Hash {
94    fn as_ref(&self) -> &[u8] {
95        &self.0
96    }
97}
98
99/// This is a helper module to make it a bit easier
100/// and regular for API callers to instantiate
101/// an Rng when calling sn_dbc methods that require
102/// them.
103pub mod rng {
104    use crate::rand::{
105        rngs::{StdRng, ThreadRng},
106        SeedableRng,
107    };
108
109    pub fn thread_rng() -> ThreadRng {
110        crate::rand::thread_rng()
111    }
112
113    pub fn from_seed(seed: <StdRng as SeedableRng>::Seed) -> StdRng {
114        StdRng::from_seed(seed)
115    }
116}
117
118pub(crate) fn sha3_256(input: &[u8]) -> [u8; 32] {
119    use tiny_keccak::{Hasher, Sha3};
120
121    let mut sha3 = Sha3::v256();
122    let mut output = [0; 32];
123    sha3.update(input);
124    sha3.finalize(&mut output);
125    output
126}
127
128#[cfg(test)]
129mod tests {
130    use super::*;
131    use quickcheck::{Arbitrary, Gen};
132
133    #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
134    pub struct TinyInt(pub u8);
135
136    impl TinyInt {
137        pub fn coerce<T: From<u8>>(self) -> T {
138            self.0.into()
139        }
140    }
141
142    impl std::fmt::Debug for TinyInt {
143        fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
144            write!(f, "{}", self.0)
145        }
146    }
147
148    impl Arbitrary for TinyInt {
149        fn arbitrary(g: &mut Gen) -> Self {
150            Self(u8::arbitrary(g) % 5)
151        }
152
153        fn shrink(&self) -> Box<dyn Iterator<Item = Self>> {
154            Box::new((0..(self.0)).rev().map(Self))
155        }
156    }
157
158    #[derive(Clone, PartialEq, Eq, PartialOrd, Ord)]
159    pub struct TinyVec<T>(pub Vec<T>);
160
161    impl<T> TinyVec<T> {
162        pub fn into_iter(self) -> impl Iterator<Item = T> {
163            self.0.into_iter()
164        }
165    }
166
167    impl<T: std::fmt::Debug> std::fmt::Debug for TinyVec<T> {
168        fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
169            write!(f, "{:?}", self.0)
170        }
171    }
172
173    impl<T: Arbitrary> Arbitrary for TinyVec<T> {
174        fn arbitrary(g: &mut Gen) -> Self {
175            let n = u8::arbitrary(g) % 7;
176            let mut vec = Vec::new();
177            for _ in 0..n {
178                vec.push(T::arbitrary(g));
179            }
180            Self(vec)
181        }
182
183        fn shrink(&self) -> Box<dyn Iterator<Item = Self>> {
184            Box::new(self.0.shrink().map(Self))
185        }
186    }
187
188    #[test]
189    fn hash() {
190        let data = b"hello world";
191        let expected = b"\
192            \x64\x4b\xcc\x7e\x56\x43\x73\x04\x09\x99\xaa\xc8\x9e\x76\x22\xf3\
193            \xca\x71\xfb\xa1\xd9\x72\xfd\x94\xa3\x1c\x3b\xfb\xf2\x4e\x39\x38\
194        ";
195        assert_eq!(sha3_256(data), *expected);
196
197        let hash = Hash::hash(data);
198        assert_eq!(hash.slice(), expected);
199    }
200
201    #[test]
202    fn hex_encoding() {
203        let data = b"hello world";
204        let expected_hex = "644bcc7e564373040999aac89e7622f3ca71fba1d972fd94a31c3bfbf24e3938";
205
206        let hash = Hash::hash(data);
207
208        assert_eq!(hash.to_hex(), expected_hex.to_string());
209        assert_eq!(Hash::from_hex(expected_hex), Ok(hash));
210
211        let too_long_hex = format!("{expected_hex}ab");
212        assert_eq!(
213            Hash::from_hex(&too_long_hex),
214            Err(Error::HexDeserializationFailed(
215                "Invalid string length".to_string()
216            ))
217        );
218
219        assert_eq!(
220            Hash::from_hex(&expected_hex[0..30]),
221            Err(Error::HexDeserializationFailed(
222                "Invalid string length".to_string()
223            ))
224        );
225    }
226}