1pub use sp_core::ed25519;
21#[cfg(feature = "std")]
22use sp_core::ed25519::Signature;
23use sp_core::{
24 ed25519::{Pair, Public},
25 hex2array, ByteArray, Pair as PairT, H256,
26};
27use sp_runtime::AccountId32;
28
29extern crate alloc;
30use alloc::{format, string::String, vec::Vec};
31
32#[derive(
34 Debug, Clone, Copy, PartialEq, Eq, Hash, strum::Display, strum::EnumIter, Ord, PartialOrd,
35)]
36pub enum Keyring {
37 Alice,
38 Bob,
39 Charlie,
40 Dave,
41 Eve,
42 Ferdie,
43 AliceStash,
44 BobStash,
45 CharlieStash,
46 DaveStash,
47 EveStash,
48 FerdieStash,
49 One,
50 Two,
51}
52
53impl Keyring {
54 pub fn from_public(who: &Public) -> Option<Keyring> {
55 Self::iter().find(|&k| &Public::from(k) == who)
56 }
57
58 pub fn from_account_id(who: &AccountId32) -> Option<Keyring> {
59 Self::iter().find(|&k| &k.to_account_id() == who)
60 }
61
62 pub fn from_raw_public(who: [u8; 32]) -> Option<Keyring> {
63 Self::from_public(&Public::from_raw(who))
64 }
65
66 pub fn to_raw_public(self) -> [u8; 32] {
67 *Public::from(self).as_array_ref()
68 }
69
70 pub fn from_h256_public(who: H256) -> Option<Keyring> {
71 Self::from_public(&Public::from_raw(who.into()))
72 }
73
74 pub fn to_h256_public(self) -> H256 {
75 Public::from(self).as_array_ref().into()
76 }
77
78 pub fn to_raw_public_vec(self) -> Vec<u8> {
79 Public::from(self).to_raw_vec()
80 }
81
82 pub fn to_account_id(self) -> AccountId32 {
83 self.to_raw_public().into()
84 }
85
86 #[cfg(feature = "std")]
87 pub fn sign(self, msg: &[u8]) -> Signature {
88 Pair::from(self).sign(msg)
89 }
90
91 pub fn pair(self) -> Pair {
92 Pair::from_string(&format!("//{}", <&'static str>::from(self)), None)
93 .expect("static values are known good; qed")
94 }
95
96 pub fn iter() -> impl Iterator<Item = Keyring> {
98 <Self as strum::IntoEnumIterator>::iter()
99 }
100
101 pub fn public(self) -> Public {
102 Public::from(self)
103 }
104
105 pub fn to_seed(self) -> String {
106 format!("//{}", self)
107 }
108}
109
110impl From<Keyring> for &'static str {
111 fn from(k: Keyring) -> Self {
112 match k {
113 Keyring::Alice => "Alice",
114 Keyring::Bob => "Bob",
115 Keyring::Charlie => "Charlie",
116 Keyring::Dave => "Dave",
117 Keyring::Eve => "Eve",
118 Keyring::Ferdie => "Ferdie",
119 Keyring::AliceStash => "Alice//stash",
120 Keyring::BobStash => "Bob//stash",
121 Keyring::CharlieStash => "Charlie//stash",
122 Keyring::DaveStash => "Dave//stash",
123 Keyring::EveStash => "Eve//stash",
124 Keyring::FerdieStash => "Ferdie//stash",
125 Keyring::One => "One",
126 Keyring::Two => "Two",
127 }
128 }
129}
130
131impl From<Keyring> for sp_runtime::MultiSigner {
132 fn from(x: Keyring) -> Self {
133 sp_runtime::MultiSigner::Ed25519(x.into())
134 }
135}
136
137impl From<Keyring> for Public {
138 fn from(k: Keyring) -> Self {
139 Public::from_raw(k.into())
140 }
141}
142
143impl From<Keyring> for AccountId32 {
144 fn from(k: Keyring) -> Self {
145 k.to_account_id()
146 }
147}
148
149impl From<Keyring> for Pair {
150 fn from(k: Keyring) -> Self {
151 k.pair()
152 }
153}
154
155impl From<Keyring> for [u8; 32] {
156 fn from(k: Keyring) -> Self {
157 match k {
158 Keyring::Alice =>
159 hex2array!("88dc3417d5058ec4b4503e0c12ea1a0a89be200fe98922423d4334014fa6b0ee"),
160 Keyring::Bob =>
161 hex2array!("d17c2d7823ebf260fd138f2d7e27d114c0145d968b5ff5006125f2414fadae69"),
162 Keyring::Charlie =>
163 hex2array!("439660b36c6c03afafca027b910b4fecf99801834c62a5e6006f27d978de234f"),
164 Keyring::Dave =>
165 hex2array!("5e639b43e0052c47447dac87d6fd2b6ec50bdd4d0f614e4299c665249bbd09d9"),
166 Keyring::Eve =>
167 hex2array!("1dfe3e22cc0d45c70779c1095f7489a8ef3cf52d62fbd8c2fa38c9f1723502b5"),
168 Keyring::Ferdie =>
169 hex2array!("568cb4a574c6d178feb39c27dfc8b3f789e5f5423e19c71633c748b9acf086b5"),
170 Keyring::AliceStash =>
171 hex2array!("451781cd0c5504504f69ceec484cc66e4c22a2b6a9d20fb1a426d91ad074a2a8"),
172 Keyring::BobStash =>
173 hex2array!("292684abbb28def63807c5f6e84e9e8689769eb37b1ab130d79dbfbf1b9a0d44"),
174 Keyring::CharlieStash =>
175 hex2array!("dd6a6118b6c11c9c9e5a4f34ed3d545e2c74190f90365c60c230fa82e9423bb9"),
176 Keyring::DaveStash =>
177 hex2array!("1d0432d75331ab299065bee79cdb1bdc2497c597a3087b4d955c67e3c000c1e2"),
178 Keyring::EveStash =>
179 hex2array!("c833bdd2e1a7a18acc1c11f8596e2e697bb9b42d6b6051e474091a1d43a294d7"),
180 Keyring::FerdieStash =>
181 hex2array!("199d749dbf4b8135cb1f3c8fd697a390fc0679881a8a110c1d06375b3b62cd09"),
182 Keyring::One =>
183 hex2array!("16f97016bbea8f7b45ae6757b49efc1080accc175d8f018f9ba719b60b0815e4"),
184 Keyring::Two =>
185 hex2array!("5079bcd20fd97d7d2f752c4607012600b401950260a91821f73e692071c82bf5"),
186 }
187 }
188}
189
190impl From<Keyring> for H256 {
191 fn from(k: Keyring) -> Self {
192 k.into()
193 }
194}
195
196#[cfg(test)]
197mod tests {
198 use super::*;
199 use sp_core::{ed25519::Pair, Pair as PairT};
200
201 #[test]
202 fn should_work() {
203 assert!(Pair::verify(
204 &Keyring::Alice.sign(b"I am Alice!"),
205 b"I am Alice!",
206 &Keyring::Alice.public(),
207 ));
208 assert!(!Pair::verify(
209 &Keyring::Alice.sign(b"I am Alice!"),
210 b"I am Bob!",
211 &Keyring::Alice.public(),
212 ));
213 assert!(!Pair::verify(
214 &Keyring::Alice.sign(b"I am Alice!"),
215 b"I am Alice!",
216 &Keyring::Bob.public(),
217 ));
218 }
219
220 #[test]
221 fn verify_static_public_keys() {
222 assert!(Keyring::iter().all(|k| { k.pair().public().as_ref() == <[u8; 32]>::from(k) }));
223 }
224}