1use rst_common::standard::bytes::Bytes;
2use rst_common::with_cryptography::blake3::{self, Hash};
3use rst_common::with_cryptography::ed25519_dalek::VerifyingKey;
4use rst_common::with_cryptography::hex;
5use rst_common::with_cryptography::x25519_dalek::{PublicKey as ECDHPublicKey, SharedSecret};
6
7use super::errors::CommonError;
8
9pub trait Hexer {
12 fn hex(&self) -> String;
13}
14
15pub trait Value<T> {
16 fn get(&self) -> Result<T, CommonError>;
17}
18
19pub trait StringValue {
20 fn get_string(&self) -> String;
21}
22
23pub trait BytesValue {
27 fn bytes(&self) -> Bytes;
28}
29
30pub trait VectorValue<T> {
33 fn vec(&self) -> Vec<T>;
34}
35
36#[derive(PartialEq, Debug, Clone)]
40pub struct ByteHex(String);
41
42impl Hexer for ByteHex {
43 fn hex(&self) -> String {
44 self.0.to_owned()
45 }
46}
47
48impl From<String> for ByteHex {
49 fn from(value: String) -> Self {
50 ByteHex(value)
51 }
52}
53
54impl From<SharedSecret> for ByteHex {
55 fn from(value: SharedSecret) -> Self {
56 ByteHex::from(hex::encode(value.to_bytes()))
57 }
58}
59
60impl From<ECDHPublicKey> for ByteHex {
61 fn from(value: ECDHPublicKey) -> Self {
62 ByteHex::from(hex::encode(value.to_bytes()))
63 }
64}
65
66impl From<VerifyingKey> for ByteHex {
67 fn from(value: VerifyingKey) -> Self {
68 ByteHex::from(hex::encode(value.to_bytes()))
69 }
70}
71
72#[derive(PartialEq, Debug, Clone)]
77pub struct Blake3Hash(Hash);
78
79impl BytesValue for Blake3Hash {
80 fn bytes(&self) -> Bytes {
81 Bytes::from(self.0.as_bytes().to_vec())
82 }
83}
84
85impl Hexer for Blake3Hash {
86 fn hex(&self) -> String {
87 hex::encode(self.0.as_bytes())
88 }
89}
90
91impl From<Vec<u8>> for Blake3Hash {
92 fn from(value: Vec<u8>) -> Self {
93 let hashed = blake3::hash(value.as_slice());
94 Blake3Hash(hashed)
95 }
96}