-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstructs.rs
More file actions
110 lines (82 loc) · 2.19 KB
/
Copy pathstructs.rs
File metadata and controls
110 lines (82 loc) · 2.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
use std::{
fmt::{self, Display},
str::FromStr,
};
use anyhow::{Error, Result};
pub struct Blake3Hash(pub blake3::Hash);
impl fmt::Display for Blake3Hash {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let Self(hash) = self;
f.write_str(&hash.to_string())
}
}
impl TryFrom<&[u8]> for Blake3Hash {
type Error = Error;
fn try_from(value: &[u8]) -> Result<Self, Self::Error> {
let mut hash = [0_u8; 32];
hash.copy_from_slice(&value[0..32]);
Ok(Self(blake3::Hash::try_from(hash)?))
}
}
pub struct BaoHash(pub bao::Hash);
impl BaoHash {
pub fn to_bytes(&self) -> Vec<u8> {
let Self(hash) = self;
hash.as_bytes().to_vec()
}
}
impl fmt::Display for BaoHash {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let Self(hash) = self;
f.write_str(&hash.to_string())
}
}
impl TryFrom<&[u8]> for BaoHash {
type Error = Error;
fn try_from(value: &[u8]) -> Result<Self, Self::Error> {
let mut hash = [0_u8; 32];
hash.copy_from_slice(&value[0..32]);
Ok(Self(bao::Hash::try_from(hash)?))
}
}
// impl AsRef<Path> for BaoHash {
// fn as_ref(&self) -> &Path {
// let Self(hash) = self;
// let hash = hash.to_string();
// Path::new(&hash).as_ref()
// }
// }
pub enum Hash {
Blake3Bytes(Box<[u8]>),
BaoBytes(Box<[u8]>),
Blake3(Blake3Hash),
Bao(BaoHash),
}
pub struct Secp256k1PubKey(pub secp256k1::PublicKey);
impl TryFrom<&str> for Secp256k1PubKey {
type Error = Error;
fn try_from(value: &str) -> Result<Self, Self::Error> {
let pk = secp256k1::PublicKey::from_str(value)?;
Ok(Self(pk))
}
}
impl Display for Secp256k1PubKey {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let Self(pk) = self;
f.write_str(&pk.to_string())
}
}
impl Secp256k1PubKey {
pub fn to_bytes(&self) -> Vec<u8> {
let Self(pk) = self;
pk.serialize().to_vec()
}
pub fn into_inner(&self) -> secp256k1::PublicKey {
let Self(pk) = self;
pk.to_owned()
}
}
pub enum PubKey {
Secp256k1Bytes(Box<[u8]>),
Secp256k1(secp256k1::PublicKey),
}