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
use crate::keys::BlsKeypair;
use crate::{utils, Ed25519Digest, Error, Keypair, PublicKey, Signature, XorName};
use ed25519_dalek::Keypair as Ed25519Keypair;
use multibase::Decodable;
use rand::{CryptoRng, Rng};
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use std::fmt::{self, Debug, Display, Formatter};
use threshold_crypto::{
serde_impl::SerdeSecret, SecretKey as BlsSecretKey, SecretKeyShare as BlsSecretKeyShare,
};
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub struct FullId {
pub(super) keypair: Keypair,
public_id: PublicId,
}
impl FullId {
pub fn new_ed25519<T: CryptoRng + Rng>(rng: &mut T) -> Self {
let keypair = Keypair::new_ed25519(rng);
let public_key = keypair.public_key();
let public_id = PublicId {
name: public_key.into(),
public_key,
};
Self { keypair, public_id }
}
pub fn new_bls<T: CryptoRng + Rng>(rng: &mut T) -> Self {
let keypair = Keypair::new_bls(rng);
let public_key = keypair.public_key();
let public_id = PublicId {
name: public_key.into(),
public_key,
};
Self { keypair, public_id }
}
pub fn new_bls_share(bls_secret_key_share: BlsSecretKeyShare) -> Self {
let keypair = Keypair::new_bls_share(bls_secret_key_share);
let public_key = keypair.public_key();
let public_id = PublicId {
name: public_key.into(),
public_key,
};
Self { keypair, public_id }
}
pub fn sign<T: AsRef<[u8]>>(&self, data: T) -> Signature {
match &self.keypair {
Keypair::Ed25519(keys) => Signature::Ed25519(keys.sign::<Ed25519Digest>(data.as_ref())),
Keypair::Bls(keys) => Signature::Bls(keys.secret.inner().sign(data)),
Keypair::BlsShare(keys) => Signature::BlsShare(keys.secret.inner().sign(data)),
}
}
pub fn public_id(&self) -> &PublicId {
&self.public_id
}
}
impl From<BlsSecretKey> for FullId {
fn from(bls_sk: BlsSecretKey) -> Self {
let public = bls_sk.public_key();
let keypair = Keypair::Bls(BlsKeypair {
secret: SerdeSecret(bls_sk),
public,
});
let public_key = keypair.public_key();
let public_id = PublicId {
name: public_key.into(),
public_key,
};
Self { keypair, public_id }
}
}
impl From<Ed25519Keypair> for FullId {
fn from(ed25519_keypair: Ed25519Keypair) -> Self {
let keypair = Keypair::Ed25519(ed25519_keypair);
let public_key = keypair.public_key();
let public_id = PublicId {
name: public_key.into(),
public_key,
};
Self { keypair, public_id }
}
}
impl From<BlsSecretKeyShare> for FullId {
fn from(bls_secret_key_share: BlsSecretKeyShare) -> Self {
Self::new_bls_share(bls_secret_key_share)
}
}
#[derive(Clone, Eq, PartialEq, PartialOrd, Ord, Hash)]
pub struct PublicId {
name: XorName,
public_key: PublicKey,
}
impl PublicId {
pub fn name(&self) -> &XorName {
&self.name
}
pub fn public_key(&self) -> &PublicKey {
&self.public_key
}
pub fn encode_to_zbase32(&self) -> String {
utils::encode(&self)
}
pub fn decode_from_zbase32<T: Decodable>(encoded: T) -> Result<Self, Error> {
utils::decode(encoded)
}
}
impl Serialize for PublicId {
fn serialize<S: Serializer>(&self, serialiser: S) -> Result<S::Ok, S::Error> {
(&self.public_key).serialize(serialiser)
}
}
impl<'de> Deserialize<'de> for PublicId {
fn deserialize<D: Deserializer<'de>>(deserialiser: D) -> Result<Self, D::Error> {
let public_key: PublicKey = Deserialize::deserialize(deserialiser)?;
let name = public_key.into();
Ok(PublicId { name, public_key })
}
}
impl Debug for PublicId {
fn fmt(&self, formatter: &mut Formatter) -> fmt::Result {
write!(formatter, "Client({:?})", self.public_key)
}
}
impl Display for PublicId {
fn fmt(&self, formatter: &mut Formatter) -> fmt::Result {
Debug::fmt(self, formatter)
}
}