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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
use lazy_static::lazy_static;
pub use sp_core::sr25519;
use sp_core::{
sr25519::{Pair, Public, Signature},
Pair as PairT, Public as PublicT, H256,
};
use sp_runtime::AccountId32;
use std::{collections::HashMap, ops::Deref};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, strum::Display, strum::EnumIter)]
pub enum Keyring {
Alice,
Bob,
Charlie,
Dave,
Eve,
Ferdie,
One,
Two,
}
impl Keyring {
pub fn from_public(who: &Public) -> Option<Keyring> {
Self::iter().find(|&k| &Public::from(k) == who)
}
pub fn from_account_id(who: &AccountId32) -> Option<Keyring> {
Self::iter().find(|&k| &k.to_account_id() == who)
}
pub fn from_raw_public(who: [u8; 32]) -> Option<Keyring> {
Self::from_public(&Public::from_raw(who))
}
pub fn to_raw_public(self) -> [u8; 32] {
*Public::from(self).as_array_ref()
}
pub fn from_h256_public(who: H256) -> Option<Keyring> {
Self::from_public(&Public::from_raw(who.into()))
}
pub fn to_h256_public(self) -> H256 {
Public::from(self).as_array_ref().into()
}
pub fn to_raw_public_vec(self) -> Vec<u8> {
Public::from(self).to_raw_vec()
}
pub fn to_account_id(self) -> AccountId32 {
self.to_raw_public().into()
}
pub fn sign(self, msg: &[u8]) -> Signature {
Pair::from(self).sign(msg)
}
pub fn pair(self) -> Pair {
Pair::from_string(&format!("//{}", <&'static str>::from(self)), None)
.expect("static values are known good; qed")
}
pub fn iter() -> impl Iterator<Item = Keyring> {
<Self as strum::IntoEnumIterator>::iter()
}
pub fn public(self) -> Public {
self.pair().public()
}
pub fn to_seed(self) -> String {
format!("//{}", self)
}
}
impl From<Keyring> for &'static str {
fn from(k: Keyring) -> Self {
match k {
Keyring::Alice => "Alice",
Keyring::Bob => "Bob",
Keyring::Charlie => "Charlie",
Keyring::Dave => "Dave",
Keyring::Eve => "Eve",
Keyring::Ferdie => "Ferdie",
Keyring::One => "One",
Keyring::Two => "Two",
}
}
}
impl From<Keyring> for sp_runtime::MultiSigner {
fn from(x: Keyring) -> Self {
sp_runtime::MultiSigner::Sr25519(x.into())
}
}
#[derive(Debug)]
pub struct ParseKeyringError;
impl std::fmt::Display for ParseKeyringError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "ParseKeyringError")
}
}
impl std::str::FromStr for Keyring {
type Err = ParseKeyringError;
fn from_str(s: &str) -> Result<Self, <Self as std::str::FromStr>::Err> {
match s {
"alice" => Ok(Keyring::Alice),
"bob" => Ok(Keyring::Bob),
"charlie" => Ok(Keyring::Charlie),
"dave" => Ok(Keyring::Dave),
"eve" => Ok(Keyring::Eve),
"ferdie" => Ok(Keyring::Ferdie),
"one" => Ok(Keyring::One),
"two" => Ok(Keyring::Two),
_ => Err(ParseKeyringError),
}
}
}
lazy_static! {
static ref PRIVATE_KEYS: HashMap<Keyring, Pair> =
Keyring::iter().map(|i| (i, i.pair())).collect();
static ref PUBLIC_KEYS: HashMap<Keyring, Public> =
PRIVATE_KEYS.iter().map(|(&name, pair)| (name, pair.public())).collect();
}
impl From<Keyring> for AccountId32 {
fn from(k: Keyring) -> Self {
k.to_account_id()
}
}
impl From<Keyring> for Public {
fn from(k: Keyring) -> Self {
(*PUBLIC_KEYS).get(&k).unwrap().clone()
}
}
impl From<Keyring> for Pair {
fn from(k: Keyring) -> Self {
k.pair()
}
}
impl From<Keyring> for [u8; 32] {
fn from(k: Keyring) -> Self {
*(*PUBLIC_KEYS).get(&k).unwrap().as_array_ref()
}
}
impl From<Keyring> for H256 {
fn from(k: Keyring) -> Self {
(*PUBLIC_KEYS).get(&k).unwrap().as_array_ref().into()
}
}
impl From<Keyring> for &'static [u8; 32] {
fn from(k: Keyring) -> Self {
(*PUBLIC_KEYS).get(&k).unwrap().as_array_ref()
}
}
impl AsRef<[u8; 32]> for Keyring {
fn as_ref(&self) -> &[u8; 32] {
(*PUBLIC_KEYS).get(self).unwrap().as_array_ref()
}
}
impl AsRef<Public> for Keyring {
fn as_ref(&self) -> &Public {
(*PUBLIC_KEYS).get(self).unwrap()
}
}
impl Deref for Keyring {
type Target = [u8; 32];
fn deref(&self) -> &[u8; 32] {
(*PUBLIC_KEYS).get(self).unwrap().as_array_ref()
}
}
#[cfg(test)]
mod tests {
use super::*;
use sp_core::{sr25519::Pair, Pair as PairT};
#[test]
fn should_work() {
assert!(Pair::verify(
&Keyring::Alice.sign(b"I am Alice!"),
b"I am Alice!",
&Keyring::Alice.public(),
));
assert!(!Pair::verify(
&Keyring::Alice.sign(b"I am Alice!"),
b"I am Bob!",
&Keyring::Alice.public(),
));
assert!(!Pair::verify(
&Keyring::Alice.sign(b"I am Alice!"),
b"I am Alice!",
&Keyring::Bob.public(),
));
}
}