// Copyright 2017-2019 Parity Technologies (UK) Ltd. // This file is part of Substrate. // Substrate is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // Substrate is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // You should have received a copy of the GNU General Public License // along with Substrate. If not, see . //! Support code for the runtime. A set of test accounts. use std::{collections::HashMap, ops::Deref}; use lazy_static::lazy_static; use substrate_primitives::{ed25519::{Pair, Public, Signature}, Pair as PairT, Public as PublicT, H256}; pub use substrate_primitives::ed25519; /// Set of test accounts. #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, strum_macros::Display, strum_macros::EnumIter)] pub enum Keyring { Alice, Bob, Charlie, Dave, Eve, Ferdie, One, Two, } impl Keyring { pub fn from_public(who: &Public) -> Option { [ Keyring::Alice, Keyring::Bob, Keyring::Charlie, Keyring::Dave, Keyring::Eve, Keyring::Ferdie, Keyring::One, Keyring::Two, ].iter() .map(|i| *i) .find(|&k| &Public::from(k) == who) } pub fn from_raw_public(who: [u8; 32]) -> Option { Self::from_public(&Public::from_raw(who)) } pub fn to_raw_public(self) -> [u8; 32] { *Public::from(self).as_array_ref() } pub fn from_h256_public(who: H256) -> Option { Self::from_public(&Public::from_raw(who.into())) } pub fn to_h256_public(self) -> H256 { Public::from(self).as_array_ref().into() } pub fn to_raw_public_vec(self) -> Vec { Public::from(self).to_raw_vec() } pub fn sign(self, msg: &[u8]) -> Signature { Pair::from(self).sign(msg) } pub fn pair(self) -> Pair { Pair::from_string(&format!("//{}", <&'static str>::from(self)), None) .expect("static values are known good; qed") } /// Returns an iterator over all test accounts. pub fn iter() -> impl Iterator { ::iter() } } impl From for &'static str { fn from(k: Keyring) -> Self { match k { Keyring::Alice => "Alice", Keyring::Bob => "Bob", Keyring::Charlie => "Charlie", Keyring::Dave => "Dave", Keyring::Eve => "Eve", Keyring::Ferdie => "Ferdie", Keyring::One => "One", Keyring::Two => "Two", } } } impl From for sr_primitives::MultiSigner { fn from(x: Keyring) -> Self { sr_primitives::MultiSigner::Ed25519(x.into()) } } lazy_static! { static ref PRIVATE_KEYS: HashMap = { Keyring::iter().map(|i| (i, i.pair())).collect() }; static ref PUBLIC_KEYS: HashMap = { PRIVATE_KEYS.iter().map(|(&name, pair)| (name, pair.public())).collect() }; } impl From for Public { fn from(k: Keyring) -> Self { (*PUBLIC_KEYS).get(&k).unwrap().clone() } } impl From for Pair { fn from(k: Keyring) -> Self { k.pair() } } impl From for [u8; 32] { fn from(k: Keyring) -> Self { *(*PUBLIC_KEYS).get(&k).unwrap().as_array_ref() } } impl From for H256 { fn from(k: Keyring) -> Self { (*PUBLIC_KEYS).get(&k).unwrap().as_array_ref().into() } } impl From for &'static [u8; 32] { fn from(k: Keyring) -> Self { (*PUBLIC_KEYS).get(&k).unwrap().as_array_ref() } } impl AsRef<[u8; 32]> for Keyring { fn as_ref(&self) -> &[u8; 32] { (*PUBLIC_KEYS).get(self).unwrap().as_array_ref() } } impl AsRef for Keyring { fn as_ref(&self) -> &Public { (*PUBLIC_KEYS).get(self).unwrap() } } impl Deref for Keyring { type Target = [u8; 32]; fn deref(&self) -> &[u8; 32] { (*PUBLIC_KEYS).get(self).unwrap().as_array_ref() } } #[cfg(test)] mod tests { use super::*; use substrate_primitives::{ed25519::Pair, Pair as PairT}; #[test] fn should_work() { assert!(Pair::verify(&Keyring::Alice.sign(b"I am Alice!"), b"I am Alice!", Keyring::Alice)); assert!(!Pair::verify(&Keyring::Alice.sign(b"I am Alice!"), b"I am Bob!", Keyring::Alice)); assert!(!Pair::verify(&Keyring::Alice.sign(b"I am Alice!"), b"I am Alice!", Keyring::Bob)); } }