sp_keyring/
ed25519.rs

1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: Apache-2.0
5
6// Licensed under the Apache License, Version 2.0 (the "License");
7// you may not use this file except in compliance with the License.
8// You may obtain a copy of the License at
9//
10// 	http://www.apache.org/licenses/LICENSE-2.0
11//
12// Unless required by applicable law or agreed to in writing, software
13// distributed under the License is distributed on an "AS IS" BASIS,
14// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15// See the License for the specific language governing permissions and
16// limitations under the License.
17
18//! Support code for the runtime. A set of test accounts.
19
20pub use sp_core::ed25519;
21
22use crate::ParseKeyringError;
23#[cfg(feature = "std")]
24use sp_core::ed25519::Signature;
25use sp_core::{
26	ed25519::{Pair, Public},
27	hex2array, ByteArray, Pair as PairT, H256,
28};
29use sp_runtime::AccountId32;
30
31extern crate alloc;
32use alloc::{format, str::FromStr, string::String, vec::Vec};
33
34/// Set of test accounts.
35#[derive(
36	Debug, Clone, Copy, PartialEq, Eq, Hash, strum::Display, strum::EnumIter, Ord, PartialOrd,
37)]
38pub enum Keyring {
39	Alice,
40	Bob,
41	Charlie,
42	Dave,
43	Eve,
44	Ferdie,
45	AliceStash,
46	BobStash,
47	CharlieStash,
48	DaveStash,
49	EveStash,
50	FerdieStash,
51	One,
52	Two,
53}
54
55impl Keyring {
56	pub fn from_public(who: &Public) -> Option<Keyring> {
57		Self::iter().find(|&k| &Public::from(k) == who)
58	}
59
60	pub fn from_account_id(who: &AccountId32) -> Option<Keyring> {
61		Self::iter().find(|&k| &k.to_account_id() == who)
62	}
63
64	pub fn from_raw_public(who: [u8; 32]) -> Option<Keyring> {
65		Self::from_public(&Public::from_raw(who))
66	}
67
68	pub fn to_raw_public(self) -> [u8; 32] {
69		*Public::from(self).as_array_ref()
70	}
71
72	pub fn from_h256_public(who: H256) -> Option<Keyring> {
73		Self::from_public(&Public::from_raw(who.into()))
74	}
75
76	pub fn to_h256_public(self) -> H256 {
77		Public::from(self).as_array_ref().into()
78	}
79
80	pub fn to_raw_public_vec(self) -> Vec<u8> {
81		Public::from(self).to_raw_vec()
82	}
83
84	pub fn to_account_id(self) -> AccountId32 {
85		self.to_raw_public().into()
86	}
87
88	#[cfg(feature = "std")]
89	pub fn sign(self, msg: &[u8]) -> Signature {
90		Pair::from(self).sign(msg)
91	}
92
93	pub fn pair(self) -> Pair {
94		Pair::from_string(&format!("//{}", <&'static str>::from(self)), None)
95			.expect("static values are known good; qed")
96	}
97
98	/// Returns an iterator over all test accounts.
99	pub fn iter() -> impl Iterator<Item = Keyring> {
100		<Self as strum::IntoEnumIterator>::iter()
101	}
102
103	pub fn public(self) -> Public {
104		Public::from(self)
105	}
106
107	pub fn to_seed(self) -> String {
108		format!("//{}", self)
109	}
110
111	pub fn well_known() -> impl Iterator<Item = Keyring> {
112		Self::iter().take(12)
113	}
114
115	pub fn invulnerable() -> impl Iterator<Item = Keyring> {
116		Self::iter().take(6)
117	}
118}
119
120impl From<Keyring> for &'static str {
121	fn from(k: Keyring) -> Self {
122		match k {
123			Keyring::Alice => "Alice",
124			Keyring::Bob => "Bob",
125			Keyring::Charlie => "Charlie",
126			Keyring::Dave => "Dave",
127			Keyring::Eve => "Eve",
128			Keyring::Ferdie => "Ferdie",
129			Keyring::AliceStash => "Alice//stash",
130			Keyring::BobStash => "Bob//stash",
131			Keyring::CharlieStash => "Charlie//stash",
132			Keyring::DaveStash => "Dave//stash",
133			Keyring::EveStash => "Eve//stash",
134			Keyring::FerdieStash => "Ferdie//stash",
135			Keyring::One => "One",
136			Keyring::Two => "Two",
137		}
138	}
139}
140
141impl From<Keyring> for sp_runtime::MultiSigner {
142	fn from(x: Keyring) -> Self {
143		sp_runtime::MultiSigner::Ed25519(x.into())
144	}
145}
146
147impl FromStr for Keyring {
148	type Err = ParseKeyringError;
149
150	fn from_str(s: &str) -> Result<Self, <Self as FromStr>::Err> {
151		match s {
152			"Alice" | "alice" => Ok(Keyring::Alice),
153			"Bob" | "bob" => Ok(Keyring::Bob),
154			"Charlie" | "charlie" => Ok(Keyring::Charlie),
155			"Dave" | "dave" => Ok(Keyring::Dave),
156			"Eve" | "eve" => Ok(Keyring::Eve),
157			"Ferdie" | "ferdie" => Ok(Keyring::Ferdie),
158			"Alice//stash" | "alice//stash" => Ok(Keyring::AliceStash),
159			"Bob//stash" | "bob//stash" => Ok(Keyring::BobStash),
160			"Charlie//stash" | "charlie//stash" => Ok(Keyring::CharlieStash),
161			"Dave//stash" | "dave//stash" => Ok(Keyring::DaveStash),
162			"Eve//stash" | "eve//stash" => Ok(Keyring::EveStash),
163			"Ferdie//stash" | "ferdie//stash" => Ok(Keyring::FerdieStash),
164			"One" | "one" => Ok(Keyring::One),
165			"Two" | "two" => Ok(Keyring::Two),
166			_ => Err(ParseKeyringError),
167		}
168	}
169}
170
171impl From<Keyring> for Public {
172	fn from(k: Keyring) -> Self {
173		Public::from_raw(k.into())
174	}
175}
176
177impl From<Keyring> for AccountId32 {
178	fn from(k: Keyring) -> Self {
179		k.to_account_id()
180	}
181}
182
183impl From<Keyring> for Pair {
184	fn from(k: Keyring) -> Self {
185		k.pair()
186	}
187}
188
189impl From<Keyring> for [u8; 32] {
190	fn from(k: Keyring) -> Self {
191		match k {
192			Keyring::Alice =>
193				hex2array!("88dc3417d5058ec4b4503e0c12ea1a0a89be200fe98922423d4334014fa6b0ee"),
194			Keyring::Bob =>
195				hex2array!("d17c2d7823ebf260fd138f2d7e27d114c0145d968b5ff5006125f2414fadae69"),
196			Keyring::Charlie =>
197				hex2array!("439660b36c6c03afafca027b910b4fecf99801834c62a5e6006f27d978de234f"),
198			Keyring::Dave =>
199				hex2array!("5e639b43e0052c47447dac87d6fd2b6ec50bdd4d0f614e4299c665249bbd09d9"),
200			Keyring::Eve =>
201				hex2array!("1dfe3e22cc0d45c70779c1095f7489a8ef3cf52d62fbd8c2fa38c9f1723502b5"),
202			Keyring::Ferdie =>
203				hex2array!("568cb4a574c6d178feb39c27dfc8b3f789e5f5423e19c71633c748b9acf086b5"),
204			Keyring::AliceStash =>
205				hex2array!("451781cd0c5504504f69ceec484cc66e4c22a2b6a9d20fb1a426d91ad074a2a8"),
206			Keyring::BobStash =>
207				hex2array!("292684abbb28def63807c5f6e84e9e8689769eb37b1ab130d79dbfbf1b9a0d44"),
208			Keyring::CharlieStash =>
209				hex2array!("dd6a6118b6c11c9c9e5a4f34ed3d545e2c74190f90365c60c230fa82e9423bb9"),
210			Keyring::DaveStash =>
211				hex2array!("1d0432d75331ab299065bee79cdb1bdc2497c597a3087b4d955c67e3c000c1e2"),
212			Keyring::EveStash =>
213				hex2array!("c833bdd2e1a7a18acc1c11f8596e2e697bb9b42d6b6051e474091a1d43a294d7"),
214			Keyring::FerdieStash =>
215				hex2array!("199d749dbf4b8135cb1f3c8fd697a390fc0679881a8a110c1d06375b3b62cd09"),
216			Keyring::One =>
217				hex2array!("16f97016bbea8f7b45ae6757b49efc1080accc175d8f018f9ba719b60b0815e4"),
218			Keyring::Two =>
219				hex2array!("5079bcd20fd97d7d2f752c4607012600b401950260a91821f73e692071c82bf5"),
220		}
221	}
222}
223
224impl From<Keyring> for H256 {
225	fn from(k: Keyring) -> Self {
226		k.into()
227	}
228}
229
230#[cfg(test)]
231mod tests {
232	use super::*;
233	use sp_core::{ed25519::Pair, Pair as PairT};
234
235	#[test]
236	fn should_work() {
237		assert!(Pair::verify(
238			&Keyring::Alice.sign(b"I am Alice!"),
239			b"I am Alice!",
240			&Keyring::Alice.public(),
241		));
242		assert!(!Pair::verify(
243			&Keyring::Alice.sign(b"I am Alice!"),
244			b"I am Bob!",
245			&Keyring::Alice.public(),
246		));
247		assert!(!Pair::verify(
248			&Keyring::Alice.sign(b"I am Alice!"),
249			b"I am Alice!",
250			&Keyring::Bob.public(),
251		));
252	}
253
254	#[test]
255	fn verify_static_public_keys() {
256		assert!(Keyring::iter().all(|k| { k.pair().public().as_ref() == <[u8; 32]>::from(k) }));
257	}
258
259	#[test]
260	fn verify_well_known() {
261		assert_eq!(
262			Keyring::well_known().collect::<Vec<Keyring>>(),
263			vec![
264				Keyring::Alice,
265				Keyring::Bob,
266				Keyring::Charlie,
267				Keyring::Dave,
268				Keyring::Eve,
269				Keyring::Ferdie,
270				Keyring::AliceStash,
271				Keyring::BobStash,
272				Keyring::CharlieStash,
273				Keyring::DaveStash,
274				Keyring::EveStash,
275				Keyring::FerdieStash
276			]
277		);
278	}
279
280	#[test]
281	fn verify_invulnerable() {
282		assert_eq!(
283			Keyring::invulnerable().collect::<Vec<Keyring>>(),
284			vec![
285				Keyring::Alice,
286				Keyring::Bob,
287				Keyring::Charlie,
288				Keyring::Dave,
289				Keyring::Eve,
290				Keyring::Ferdie
291			]
292		);
293	}
294}