1use std::collections::HashMap;
21use std::ops::Deref;
22use lazy_static::lazy_static;
23use tet_core::{sr25519::{Pair, Public, Signature}, Pair as PairT, Public as PublicT, H256};
24pub use tet_core::sr25519;
25use tp_runtime::AccountId32;
26
27#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, strum::Display, strum::EnumIter)]
29pub enum Keyring {
30 Alice,
31 Bob,
32 Charlie,
33 Dave,
34 Eve,
35 Ferdie,
36 One,
37 Two,
38}
39
40impl Keyring {
41 pub fn from_public(who: &Public) -> Option<Keyring> {
42 Self::iter().find(|&k| &Public::from(k) == who)
43 }
44
45 pub fn from_account_id(who: &AccountId32) -> Option<Keyring> {
46 Self::iter().find(|&k| &k.to_account_id() == who)
47 }
48
49 pub fn from_raw_public(who: [u8; 32]) -> Option<Keyring> {
50 Self::from_public(&Public::from_raw(who))
51 }
52
53 pub fn to_raw_public(self) -> [u8; 32] {
54 *Public::from(self).as_array_ref()
55 }
56
57 pub fn from_h256_public(who: H256) -> Option<Keyring> {
58 Self::from_public(&Public::from_raw(who.into()))
59 }
60
61 pub fn to_h256_public(self) -> H256 {
62 Public::from(self).as_array_ref().into()
63 }
64
65 pub fn to_raw_public_vec(self) -> Vec<u8> {
66 Public::from(self).to_raw_vec()
67 }
68
69 pub fn to_account_id(self) -> AccountId32 {
70 self.to_raw_public().into()
71 }
72
73 pub fn sign(self, msg: &[u8]) -> Signature {
74 Pair::from(self).sign(msg)
75 }
76
77 pub fn pair(self) -> Pair {
78 Pair::from_string(&format!("//{}", <&'static str>::from(self)), None)
79 .expect("static values are known good; qed")
80 }
81
82 pub fn iter() -> impl Iterator<Item=Keyring> {
84 <Self as strum::IntoEnumIterator>::iter()
85 }
86
87 pub fn public(self) -> Public {
88 self.pair().public()
89 }
90 pub fn to_seed(self) -> String {
91 format!("//{}", self)
92 }
93}
94
95impl From<Keyring> for &'static str {
96 fn from(k: Keyring) -> Self {
97 match k {
98 Keyring::Alice => "Alice",
99 Keyring::Bob => "Bob",
100 Keyring::Charlie => "Charlie",
101 Keyring::Dave => "Dave",
102 Keyring::Eve => "Eve",
103 Keyring::Ferdie => "Ferdie",
104 Keyring::One => "One",
105 Keyring::Two => "Two",
106 }
107 }
108}
109
110impl From<Keyring> for tp_runtime::MultiSigner {
111 fn from(x: Keyring) -> Self {
112 tp_runtime::MultiSigner::Sr25519(x.into())
113 }
114}
115
116#[derive(Debug)]
117pub struct ParseKeyringError;
118
119impl std::fmt::Display for ParseKeyringError {
120 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
121 write!(f, "ParseKeyringError")
122 }
123}
124
125impl std::str::FromStr for Keyring {
126 type Err = ParseKeyringError;
127
128 fn from_str(s: &str) -> Result<Self, <Self as std::str::FromStr>::Err> {
129 match s {
130 "alice" => Ok(Keyring::Alice),
131 "bob" => Ok(Keyring::Bob),
132 "charlie" => Ok(Keyring::Charlie),
133 "dave" => Ok(Keyring::Dave),
134 "eve" => Ok(Keyring::Eve),
135 "ferdie" => Ok(Keyring::Ferdie),
136 "one" => Ok(Keyring::One),
137 "two" => Ok(Keyring::Two),
138 _ => Err(ParseKeyringError)
139 }
140 }
141}
142
143lazy_static! {
144 static ref PRIVATE_KEYS: HashMap<Keyring, Pair> = {
145 Keyring::iter().map(|i| (i, i.pair())).collect()
146 };
147
148 static ref PUBLIC_KEYS: HashMap<Keyring, Public> = {
149 PRIVATE_KEYS.iter().map(|(&name, pair)| (name, pair.public())).collect()
150 };
151}
152
153impl From<Keyring> for AccountId32 {
154 fn from(k: Keyring) -> Self {
155 k.to_account_id()
156 }
157}
158
159impl From<Keyring> for Public {
160 fn from(k: Keyring) -> Self {
161 (*PUBLIC_KEYS).get(&k).unwrap().clone()
162 }
163}
164
165impl From<Keyring> for Pair {
166 fn from(k: Keyring) -> Self {
167 k.pair()
168 }
169}
170
171impl From<Keyring> for [u8; 32] {
172 fn from(k: Keyring) -> Self {
173 *(*PUBLIC_KEYS).get(&k).unwrap().as_array_ref()
174 }
175}
176
177impl From<Keyring> for H256 {
178 fn from(k: Keyring) -> Self {
179 (*PUBLIC_KEYS).get(&k).unwrap().as_array_ref().into()
180 }
181}
182
183impl From<Keyring> for &'static [u8; 32] {
184 fn from(k: Keyring) -> Self {
185 (*PUBLIC_KEYS).get(&k).unwrap().as_array_ref()
186 }
187}
188
189impl AsRef<[u8; 32]> for Keyring {
190 fn as_ref(&self) -> &[u8; 32] {
191 (*PUBLIC_KEYS).get(self).unwrap().as_array_ref()
192 }
193}
194
195impl AsRef<Public> for Keyring {
196 fn as_ref(&self) -> &Public {
197 (*PUBLIC_KEYS).get(self).unwrap()
198 }
199}
200
201impl Deref for Keyring {
202 type Target = [u8; 32];
203 fn deref(&self) -> &[u8; 32] {
204 (*PUBLIC_KEYS).get(self).unwrap().as_array_ref()
205 }
206}
207
208#[cfg(test)]
209mod tests {
210 use super::*;
211 use tet_core::{sr25519::Pair, Pair as PairT};
212
213 #[test]
214 fn should_work() {
215 assert!(
216 Pair::verify(
217 &Keyring::Alice.sign(b"I am Alice!"),
218 b"I am Alice!",
219 &Keyring::Alice.public(),
220 )
221 );
222 assert!(
223 !Pair::verify(
224 &Keyring::Alice.sign(b"I am Alice!"),
225 b"I am Bob!",
226 &Keyring::Alice.public(),
227 )
228 );
229 assert!(
230 !Pair::verify(
231 &Keyring::Alice.sign(b"I am Alice!"),
232 b"I am Alice!",
233 &Keyring::Bob.public(),
234 )
235 );
236 }
237}