use codec::Encode;
use ed25519_dalek::{Keypair, PublicKey, SecretKey, Signature};
use finality_grandpa::voter_set::VoterSet;
use sp_consensus_grandpa::{AuthorityId, AuthorityList, AuthorityWeight};
use sp_core::crypto::UncheckedFrom;
use sp_runtime::RuntimeDebug;
use sp_std::{prelude::*, vec};
pub const ALICE: Account = Account(0);
pub const BOB: Account = Account(1);
pub const CHARLIE: Account = Account(2);
pub const DAVE: Account = Account(3);
pub const EVE: Account = Account(4);
pub const FERDIE: Account = Account(5);
#[derive(RuntimeDebug, Clone, Copy)]
pub struct Account(pub u16);
impl Account {
pub fn public(&self) -> PublicKey {
(&self.secret()).into()
}
pub fn secret(&self) -> SecretKey {
let data = self.0.encode();
let mut bytes = [0_u8; 32];
bytes[0..data.len()].copy_from_slice(&data);
SecretKey::from_bytes(&bytes)
.expect("A static array of the correct length is a known good.")
}
pub fn pair(&self) -> Keypair {
let mut pair: [u8; 64] = [0; 64];
let secret = self.secret();
pair[..32].copy_from_slice(&secret.to_bytes());
let public = self.public();
pair[32..].copy_from_slice(&public.to_bytes());
Keypair::from_bytes(&pair)
.expect("We expect the SecretKey to be good, so this must also be good.")
}
pub fn sign(&self, msg: &[u8]) -> Signature {
use ed25519_dalek::Signer;
self.pair().sign(msg)
}
}
impl From<Account> for AuthorityId {
fn from(p: Account) -> Self {
UncheckedFrom::unchecked_from(p.public().to_bytes())
}
}
pub fn voter_set() -> VoterSet<AuthorityId> {
VoterSet::new(authority_list()).unwrap()
}
pub fn authority_list() -> AuthorityList {
test_keyring()
.iter()
.map(|(id, w)| (AuthorityId::from(*id), *w))
.collect()
}
pub fn authorities() -> Vec<AuthorityId> {
test_keyring()
.iter()
.map(|(id, _w)| AuthorityId::from(*id))
.collect()
}
pub fn test_keyring() -> Vec<(Account, AuthorityWeight)> {
vec![(ALICE, 1), (BOB, 1), (CHARLIE, 1)]
}
pub fn accounts(len: u16) -> Vec<Account> {
(0..len).map(Account).collect()
}