referrerpolicy=no-referrer-when-downgrade

sp_keyring/
bandersnatch.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//! A set of well-known keys used for testing.
19
20pub use sp_core::bandersnatch;
21
22use crate::ParseKeyringError;
23#[cfg(feature = "std")]
24use sp_core::bandersnatch::Signature;
25use sp_core::{
26	bandersnatch::{Pair, Public},
27	crypto::UncheckedFrom,
28	hex2array, ByteArray, Pair as PairT,
29};
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	One,
46	Two,
47}
48
49const PUBLIC_RAW_LEN: usize = <Public as ByteArray>::LEN;
50
51impl Keyring {
52	pub fn from_public(who: &Public) -> Option<Keyring> {
53		Self::iter().find(|&k| &Public::from(k) == who)
54	}
55
56	pub fn from_raw_public(who: [u8; PUBLIC_RAW_LEN]) -> Option<Keyring> {
57		Self::from_public(&Public::unchecked_from(who))
58	}
59
60	pub fn to_raw_public(self) -> [u8; PUBLIC_RAW_LEN] {
61		*Public::from(self).as_ref()
62	}
63
64	pub fn to_raw_public_vec(self) -> Vec<u8> {
65		Public::from(self).to_raw_vec()
66	}
67
68	#[cfg(feature = "std")]
69	pub fn sign(self, msg: &[u8]) -> Signature {
70		Pair::from(self).sign(msg)
71	}
72
73	pub fn pair(self) -> Pair {
74		Pair::from_string(&format!("//{}", <&'static str>::from(self)), None)
75			.expect("static values are known good; qed")
76	}
77
78	/// Returns an iterator over all test accounts.
79	pub fn iter() -> impl Iterator<Item = Keyring> {
80		<Self as strum::IntoEnumIterator>::iter()
81	}
82
83	pub fn public(self) -> Public {
84		Public::from(self)
85	}
86
87	pub fn to_seed(self) -> String {
88		format!("//{}", self)
89	}
90
91	/// Create a crypto `Pair` from a numeric value.
92	pub fn numeric(idx: usize) -> Pair {
93		Pair::from_string(&format!("//{}", idx), None).expect("numeric values are known good; qed")
94	}
95}
96
97impl From<Keyring> for &'static str {
98	fn from(k: Keyring) -> Self {
99		match k {
100			Keyring::Alice => "Alice",
101			Keyring::Bob => "Bob",
102			Keyring::Charlie => "Charlie",
103			Keyring::Dave => "Dave",
104			Keyring::Eve => "Eve",
105			Keyring::Ferdie => "Ferdie",
106			Keyring::One => "One",
107			Keyring::Two => "Two",
108		}
109	}
110}
111
112impl FromStr for Keyring {
113	type Err = ParseKeyringError;
114
115	fn from_str(s: &str) -> Result<Self, <Self as FromStr>::Err> {
116		match s {
117			"Alice" => Ok(Keyring::Alice),
118			"Bob" => Ok(Keyring::Bob),
119			"Charlie" => Ok(Keyring::Charlie),
120			"Dave" => Ok(Keyring::Dave),
121			"Eve" => Ok(Keyring::Eve),
122			"Ferdie" => Ok(Keyring::Ferdie),
123			"One" => Ok(Keyring::One),
124			"Two" => Ok(Keyring::Two),
125			_ => Err(ParseKeyringError),
126		}
127	}
128}
129
130impl From<Keyring> for Public {
131	fn from(k: Keyring) -> Self {
132		Public::unchecked_from(<[u8; PUBLIC_RAW_LEN]>::from(k))
133	}
134}
135
136impl From<Keyring> for Pair {
137	fn from(k: Keyring) -> Self {
138		k.pair()
139	}
140}
141
142impl From<Keyring> for [u8; PUBLIC_RAW_LEN] {
143	fn from(k: Keyring) -> Self {
144		match k {
145			Keyring::Alice =>
146				hex2array!("4d8e57b723e8bb4eca5c28d79cb95b9e84b4e2319d9851d45504014633e55d01"),
147			Keyring::Bob =>
148				hex2array!("aa6daf4784d581804d8f5cc1edb2ad171dbdf9c5188ddc071b11c3479c150c37"),
149			Keyring::Charlie =>
150				hex2array!("331d681392223b35b92319e059d3dbc2869b2ef74400a70e678b4a5108c81ec0"),
151			Keyring::Dave =>
152				hex2array!("374384c19a877040c84bb07fcf3aac74ff7fafface0b1c01a93fd2ddbf5c1660"),
153			Keyring::Eve =>
154				hex2array!("14bdd9381e80c07b75b8f1e92d6b2e4652e5135beaad1eedb1b81ff01ee562ad"),
155			Keyring::Ferdie =>
156				hex2array!("e13bd31b0575076479914c16c5ad69779f206375dbf19519219eeba3b10cc063"),
157			Keyring::One =>
158				hex2array!("03466a4de97ae18bc4604a3c40dfbddc6bac9f707c9b3c31a94a2c1725a03253"),
159			Keyring::Two =>
160				hex2array!("0fda0d1336e8d6ee687ebf6d14eaa9b92b5601068e6159222623c8e14c004293"),
161		}
162	}
163}
164
165#[cfg(test)]
166mod tests {
167	use super::*;
168	use sp_core::{bandersnatch::Pair, Pair as PairT};
169
170	#[test]
171	fn should_work() {
172		assert!(Pair::verify(
173			&Keyring::Alice.sign(b"I am Alice!"),
174			b"I am Alice!",
175			&Keyring::Alice.public(),
176		));
177		assert!(!Pair::verify(
178			&Keyring::Alice.sign(b"I am Alice!"),
179			b"I am Bob!",
180			&Keyring::Alice.public(),
181		));
182		assert!(!Pair::verify(
183			&Keyring::Alice.sign(b"I am Alice!"),
184			b"I am Alice!",
185			&Keyring::Bob.public(),
186		));
187	}
188	#[test]
189	fn verify_static_public_keys() {
190		assert!(Keyring::iter()
191			.all(|k| { k.pair().public().as_ref() == <[u8; PUBLIC_RAW_LEN]>::from(k) }));
192	}
193}