sp_keyring/
bandersnatch.rs1pub 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#[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 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 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 },
148 Keyring::Bob => {
149 hex2array!("aa6daf4784d581804d8f5cc1edb2ad171dbdf9c5188ddc071b11c3479c150c37")
150 },
151 Keyring::Charlie => {
152 hex2array!("331d681392223b35b92319e059d3dbc2869b2ef74400a70e678b4a5108c81ec0")
153 },
154 Keyring::Dave => {
155 hex2array!("374384c19a877040c84bb07fcf3aac74ff7fafface0b1c01a93fd2ddbf5c1660")
156 },
157 Keyring::Eve => {
158 hex2array!("14bdd9381e80c07b75b8f1e92d6b2e4652e5135beaad1eedb1b81ff01ee562ad")
159 },
160 Keyring::Ferdie => {
161 hex2array!("e13bd31b0575076479914c16c5ad69779f206375dbf19519219eeba3b10cc063")
162 },
163 Keyring::One => {
164 hex2array!("03466a4de97ae18bc4604a3c40dfbddc6bac9f707c9b3c31a94a2c1725a03253")
165 },
166 Keyring::Two => {
167 hex2array!("0fda0d1336e8d6ee687ebf6d14eaa9b92b5601068e6159222623c8e14c004293")
168 },
169 }
170 }
171}
172
173#[cfg(test)]
174mod tests {
175 use super::*;
176 use sp_core::{bandersnatch::Pair, Pair as PairT};
177
178 #[test]
179 fn should_work() {
180 assert!(Pair::verify(
181 &Keyring::Alice.sign(b"I am Alice!"),
182 b"I am Alice!",
183 &Keyring::Alice.public(),
184 ));
185 assert!(!Pair::verify(
186 &Keyring::Alice.sign(b"I am Alice!"),
187 b"I am Bob!",
188 &Keyring::Alice.public(),
189 ));
190 assert!(!Pair::verify(
191 &Keyring::Alice.sign(b"I am Alice!"),
192 b"I am Alice!",
193 &Keyring::Bob.public(),
194 ));
195 }
196 #[test]
197 fn verify_static_public_keys() {
198 assert!(Keyring::iter()
199 .all(|k| { k.pair().public().as_ref() == <[u8; PUBLIC_RAW_LEN]>::from(k) }));
200 }
201}