referrerpolicy=no-referrer-when-downgrade

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			},
195			Keyring::Bob => {
196				hex2array!("d17c2d7823ebf260fd138f2d7e27d114c0145d968b5ff5006125f2414fadae69")
197			},
198			Keyring::Charlie => {
199				hex2array!("439660b36c6c03afafca027b910b4fecf99801834c62a5e6006f27d978de234f")
200			},
201			Keyring::Dave => {
202				hex2array!("5e639b43e0052c47447dac87d6fd2b6ec50bdd4d0f614e4299c665249bbd09d9")
203			},
204			Keyring::Eve => {
205				hex2array!("1dfe3e22cc0d45c70779c1095f7489a8ef3cf52d62fbd8c2fa38c9f1723502b5")
206			},
207			Keyring::Ferdie => {
208				hex2array!("568cb4a574c6d178feb39c27dfc8b3f789e5f5423e19c71633c748b9acf086b5")
209			},
210			Keyring::AliceStash => {
211				hex2array!("451781cd0c5504504f69ceec484cc66e4c22a2b6a9d20fb1a426d91ad074a2a8")
212			},
213			Keyring::BobStash => {
214				hex2array!("292684abbb28def63807c5f6e84e9e8689769eb37b1ab130d79dbfbf1b9a0d44")
215			},
216			Keyring::CharlieStash => {
217				hex2array!("dd6a6118b6c11c9c9e5a4f34ed3d545e2c74190f90365c60c230fa82e9423bb9")
218			},
219			Keyring::DaveStash => {
220				hex2array!("1d0432d75331ab299065bee79cdb1bdc2497c597a3087b4d955c67e3c000c1e2")
221			},
222			Keyring::EveStash => {
223				hex2array!("c833bdd2e1a7a18acc1c11f8596e2e697bb9b42d6b6051e474091a1d43a294d7")
224			},
225			Keyring::FerdieStash => {
226				hex2array!("199d749dbf4b8135cb1f3c8fd697a390fc0679881a8a110c1d06375b3b62cd09")
227			},
228			Keyring::One => {
229				hex2array!("16f97016bbea8f7b45ae6757b49efc1080accc175d8f018f9ba719b60b0815e4")
230			},
231			Keyring::Two => {
232				hex2array!("5079bcd20fd97d7d2f752c4607012600b401950260a91821f73e692071c82bf5")
233			},
234		}
235	}
236}
237
238impl From<Keyring> for H256 {
239	fn from(k: Keyring) -> Self {
240		k.into()
241	}
242}
243
244#[cfg(test)]
245mod tests {
246	use super::*;
247	use sp_core::{ed25519::Pair, Pair as PairT};
248
249	#[test]
250	fn should_work() {
251		assert!(Pair::verify(
252			&Keyring::Alice.sign(b"I am Alice!"),
253			b"I am Alice!",
254			&Keyring::Alice.public(),
255		));
256		assert!(!Pair::verify(
257			&Keyring::Alice.sign(b"I am Alice!"),
258			b"I am Bob!",
259			&Keyring::Alice.public(),
260		));
261		assert!(!Pair::verify(
262			&Keyring::Alice.sign(b"I am Alice!"),
263			b"I am Alice!",
264			&Keyring::Bob.public(),
265		));
266	}
267
268	#[test]
269	fn verify_static_public_keys() {
270		assert!(Keyring::iter().all(|k| { k.pair().public().as_ref() == <[u8; 32]>::from(k) }));
271	}
272
273	#[test]
274	fn verify_well_known() {
275		assert_eq!(
276			Keyring::well_known().collect::<Vec<Keyring>>(),
277			vec![
278				Keyring::Alice,
279				Keyring::Bob,
280				Keyring::Charlie,
281				Keyring::Dave,
282				Keyring::Eve,
283				Keyring::Ferdie,
284				Keyring::AliceStash,
285				Keyring::BobStash,
286				Keyring::CharlieStash,
287				Keyring::DaveStash,
288				Keyring::EveStash,
289				Keyring::FerdieStash
290			]
291		);
292	}
293
294	#[test]
295	fn verify_invulnerable() {
296		assert_eq!(
297			Keyring::invulnerable().collect::<Vec<Keyring>>(),
298			vec![
299				Keyring::Alice,
300				Keyring::Bob,
301				Keyring::Charlie,
302				Keyring::Dave,
303				Keyring::Eve,
304				Keyring::Ferdie
305			]
306		);
307	}
308}