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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
use crate::{Error, PublicKey, Result};
use blsttc::{serde_impl::SerdeSecret, SecretKey};
use std::fmt;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use crate::rand::{distributions::Standard, Rng, RngCore};
pub type DerivationIndex = [u8; 32];
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone)]
pub enum Owner {
SecretKey(SerdeSecret<SecretKey>),
PublicKey(PublicKey),
}
impl fmt::Debug for Owner {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut f = f.debug_tuple("");
match self {
Self::SecretKey(sk) => f.field(sk),
Self::PublicKey(pk) => f.field(pk),
};
f.finish()
}
}
impl From<SecretKey> for Owner {
fn from(s: SecretKey) -> Self {
Self::SecretKey(SerdeSecret(s))
}
}
impl From<PublicKey> for Owner {
fn from(p: PublicKey) -> Self {
Self::PublicKey(p)
}
}
impl PartialEq for Owner {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(Self::SecretKey(a), Self::SecretKey(b)) => a == b,
(Self::PublicKey(a), Self::PublicKey(b)) => a == b,
_ => false,
}
}
}
impl Eq for Owner {}
impl Owner {
pub fn public_key(&self) -> PublicKey {
match self {
Self::SecretKey(sk) => sk.public_key(),
Self::PublicKey(pk) => *pk,
}
}
pub fn secret_key(&self) -> Result<SecretKey> {
match self {
Self::SecretKey(sk) => Ok(sk.inner().clone()),
Self::PublicKey(_pk) => Err(Error::SecretKeyUnavailable),
}
}
pub fn derive(&self, i: &DerivationIndex) -> Self {
match self {
Self::SecretKey(sk) => Self::from(sk.inner().derive_child(i)),
Self::PublicKey(pk) => Self::from(pk.derive_child(i)),
}
}
pub fn to_bytes(&self) -> Vec<u8> {
match self {
Self::SecretKey(sk) => sk.to_bytes().to_vec(),
Self::PublicKey(pk) => pk.to_bytes().to_vec(),
}
}
pub fn has_secret_key(&self) -> bool {
match self {
Self::SecretKey(_) => true,
Self::PublicKey(_) => false,
}
}
pub fn from_random_secret_key(rng: &mut impl RngCore) -> Self {
let sk: SecretKey = rng.sample(Standard);
Self::from(sk)
}
}
impl From<OwnerOnce> for Owner {
fn from(o: OwnerOnce) -> Self {
o.as_owner()
}
}
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, PartialEq)]
pub struct OwnerOnce {
pub owner_base: Owner,
pub derivation_index: DerivationIndex,
}
impl OwnerOnce {
pub fn owner_base(&self) -> &Owner {
&self.owner_base
}
pub fn as_owner(&self) -> Owner {
self.owner_base.derive(&self.derivation_index)
}
pub fn from_owner_base(owner_base: Owner, rng: &mut impl RngCore) -> Self {
Self {
owner_base,
derivation_index: Self::random_derivation_index(rng),
}
}
pub(crate) fn random_derivation_index(rng: &mut impl RngCore) -> [u8; 32] {
let mut bytes = [0u8; 32];
rng.fill_bytes(&mut bytes);
bytes
}
}