use bp_header_chain::{justification::JustificationVerificationContext, AuthoritySet};
use codec::Encode;
use ed25519_dalek::{Signature, SigningKey, VerifyingKey};
use finality_grandpa::voter_set::VoterSet;
use sp_consensus_grandpa::{AuthorityId, AuthorityList, AuthorityWeight, SetId};
use sp_runtime::RuntimeDebug;
use sp_std::prelude::*;
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) -> VerifyingKey {
self.pair().verifying_key()
}
pub fn pair(&self) -> SigningKey {
let data = self.0.encode();
let mut bytes = [0_u8; 32];
bytes[0..data.len()].copy_from_slice(&data);
SigningKey::from_bytes(&bytes)
}
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 {
sp_application_crypto::UncheckedFrom::unchecked_from(p.public().to_bytes())
}
}
pub fn voter_set() -> VoterSet<AuthorityId> {
VoterSet::new(authority_list()).unwrap()
}
pub fn verification_context(set_id: SetId) -> JustificationVerificationContext {
AuthoritySet { authorities: authority_list(), set_id }.try_into().unwrap()
}
pub fn authority_list() -> AuthorityList {
test_keyring().iter().map(|(id, w)| (AuthorityId::from(*id), *w)).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()
}