1pub use sp_core::sr25519;
21#[cfg(feature = "std")]
22use sp_core::sr25519::Signature;
23use sp_core::{
24 hex2array,
25 sr25519::{Pair, Public},
26 ByteArray, Pair as PairT, H256,
27};
28use sp_runtime::AccountId32;
29
30extern crate alloc;
31use alloc::{fmt, format, str::FromStr, string::String, vec::Vec};
32
33#[derive(
35 Debug, Clone, Copy, PartialEq, Eq, Hash, strum::Display, strum::EnumIter, Ord, PartialOrd,
36)]
37pub enum Keyring {
38 Alice,
39 Bob,
40 Charlie,
41 Dave,
42 Eve,
43 Ferdie,
44 AliceStash,
45 BobStash,
46 CharlieStash,
47 DaveStash,
48 EveStash,
49 FerdieStash,
50 One,
51 Two,
52}
53
54impl Keyring {
55 pub fn from_public(who: &Public) -> Option<Keyring> {
56 Self::iter().find(|&k| &Public::from(k) == who)
57 }
58
59 pub fn from_account_id(who: &AccountId32) -> Option<Keyring> {
60 Self::iter().find(|&k| &k.to_account_id() == who)
61 }
62
63 pub fn from_raw_public(who: [u8; 32]) -> Option<Keyring> {
64 Self::from_public(&Public::from_raw(who))
65 }
66
67 pub fn to_raw_public(self) -> [u8; 32] {
68 *Public::from(self).as_array_ref()
69 }
70
71 pub fn from_h256_public(who: H256) -> Option<Keyring> {
72 Self::from_public(&Public::from_raw(who.into()))
73 }
74
75 pub fn to_h256_public(self) -> H256 {
76 Public::from(self).as_array_ref().into()
77 }
78
79 pub fn to_raw_public_vec(self) -> Vec<u8> {
80 Public::from(self).to_raw_vec()
81 }
82
83 pub fn to_account_id(self) -> AccountId32 {
84 self.to_raw_public().into()
85 }
86
87 #[cfg(feature = "std")]
88 pub fn sign(self, msg: &[u8]) -> Signature {
89 Pair::from(self).sign(msg)
90 }
91
92 pub fn pair(self) -> Pair {
93 Pair::from_string(&format!("//{}", <&'static str>::from(self)), None)
94 .expect("static values are known good; qed")
95 }
96
97 pub fn iter() -> impl Iterator<Item = Keyring> {
99 <Self as strum::IntoEnumIterator>::iter()
100 }
101
102 pub fn public(self) -> Public {
103 Public::from(self)
104 }
105
106 pub fn to_seed(self) -> String {
107 format!("//{}", self)
108 }
109
110 pub fn numeric(idx: usize) -> Pair {
112 Pair::from_string(&format!("//{}", idx), None).expect("numeric values are known good; qed")
113 }
114
115 pub fn numeric_id(idx: usize) -> AccountId32 {
117 (*Self::numeric(idx).public().as_array_ref()).into()
118 }
119}
120
121impl From<Keyring> for &'static str {
122 fn from(k: Keyring) -> Self {
123 match k {
124 Keyring::Alice => "Alice",
125 Keyring::Bob => "Bob",
126 Keyring::Charlie => "Charlie",
127 Keyring::Dave => "Dave",
128 Keyring::Eve => "Eve",
129 Keyring::Ferdie => "Ferdie",
130 Keyring::AliceStash => "Alice//stash",
131 Keyring::BobStash => "Bob//stash",
132 Keyring::CharlieStash => "Charlie//stash",
133 Keyring::DaveStash => "Dave//stash",
134 Keyring::EveStash => "Eve//stash",
135 Keyring::FerdieStash => "Ferdie//stash",
136 Keyring::One => "One",
137 Keyring::Two => "Two",
138 }
139 }
140}
141
142impl From<Keyring> for sp_runtime::MultiSigner {
143 fn from(x: Keyring) -> Self {
144 sp_runtime::MultiSigner::Sr25519(x.into())
145 }
146}
147
148#[derive(Debug)]
149pub struct ParseKeyringError;
150
151impl fmt::Display for ParseKeyringError {
152 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
153 write!(f, "ParseKeyringError")
154 }
155}
156
157impl FromStr for Keyring {
158 type Err = ParseKeyringError;
159
160 fn from_str(s: &str) -> Result<Self, <Self as FromStr>::Err> {
161 match s {
162 "alice" => Ok(Keyring::Alice),
163 "bob" => Ok(Keyring::Bob),
164 "charlie" => Ok(Keyring::Charlie),
165 "dave" => Ok(Keyring::Dave),
166 "eve" => Ok(Keyring::Eve),
167 "ferdie" => Ok(Keyring::Ferdie),
168 "one" => Ok(Keyring::One),
169 "two" => Ok(Keyring::Two),
170 _ => Err(ParseKeyringError),
171 }
172 }
173}
174
175impl From<Keyring> for AccountId32 {
176 fn from(k: Keyring) -> Self {
177 k.to_account_id()
178 }
179}
180
181impl From<Keyring> for Public {
182 fn from(k: Keyring) -> Self {
183 Public::from_raw(k.into())
184 }
185}
186
187impl From<Keyring> for Pair {
188 fn from(k: Keyring) -> Self {
189 k.pair()
190 }
191}
192
193impl From<Keyring> for [u8; 32] {
194 fn from(k: Keyring) -> Self {
195 match k {
196 Keyring::Alice =>
197 hex2array!("d43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d"),
198 Keyring::Bob =>
199 hex2array!("8eaf04151687736326c9fea17e25fc5287613693c912909cb226aa4794f26a48"),
200 Keyring::Charlie =>
201 hex2array!("90b5ab205c6974c9ea841be688864633dc9ca8a357843eeacf2314649965fe22"),
202 Keyring::Dave =>
203 hex2array!("306721211d5404bd9da88e0204360a1a9ab8b87c66c1bc2fcdd37f3c2222cc20"),
204 Keyring::Eve =>
205 hex2array!("e659a7a1628cdd93febc04a4e0646ea20e9f5f0ce097d9a05290d4a9e054df4e"),
206 Keyring::Ferdie =>
207 hex2array!("1cbd2d43530a44705ad088af313e18f80b53ef16b36177cd4b77b846f2a5f07c"),
208 Keyring::AliceStash =>
209 hex2array!("be5ddb1579b72e84524fc29e78609e3caf42e85aa118ebfe0b0ad404b5bdd25f"),
210 Keyring::BobStash =>
211 hex2array!("fe65717dad0447d715f660a0a58411de509b42e6efb8375f562f58a554d5860e"),
212 Keyring::CharlieStash =>
213 hex2array!("1e07379407fecc4b89eb7dbd287c2c781cfb1907a96947a3eb18e4f8e7198625"),
214 Keyring::DaveStash =>
215 hex2array!("e860f1b1c7227f7c22602f53f15af80747814dffd839719731ee3bba6edc126c"),
216 Keyring::EveStash =>
217 hex2array!("8ac59e11963af19174d0b94d5d78041c233f55d2e19324665bafdfb62925af2d"),
218 Keyring::FerdieStash =>
219 hex2array!("101191192fc877c24d725b337120fa3edc63d227bbc92705db1e2cb65f56981a"),
220 Keyring::One =>
221 hex2array!("ac859f8a216eeb1b320b4c76d118da3d7407fa523484d0a980126d3b4d0d220a"),
222 Keyring::Two =>
223 hex2array!("1254f7017f0b8347ce7ab14f96d818802e7e9e0c0d1b7c9acb3c726b080e7a03"),
224 }
225 }
226}
227
228impl From<Keyring> for H256 {
229 fn from(k: Keyring) -> Self {
230 k.into()
231 }
232}
233
234#[cfg(test)]
235mod tests {
236 use super::*;
237 use sp_core::{sr25519::Pair, Pair as PairT};
238
239 #[test]
240 fn should_work() {
241 assert!(Pair::verify(
242 &Keyring::Alice.sign(b"I am Alice!"),
243 b"I am Alice!",
244 &Keyring::Alice.public(),
245 ));
246 assert!(!Pair::verify(
247 &Keyring::Alice.sign(b"I am Alice!"),
248 b"I am Bob!",
249 &Keyring::Alice.public(),
250 ));
251 assert!(!Pair::verify(
252 &Keyring::Alice.sign(b"I am Alice!"),
253 b"I am Alice!",
254 &Keyring::Bob.public(),
255 ));
256 }
257 #[test]
258 fn verify_static_public_keys() {
259 assert!(Keyring::iter().all(|k| { k.pair().public().as_ref() == <[u8; 32]>::from(k) }));
260 }
261}