1use bp_header_chain::{justification::JustificationVerificationContext, AuthoritySet};
20use codec::Encode;
21use ed25519_dalek::{Signature, SigningKey, VerifyingKey};
22use finality_grandpa::voter_set::VoterSet;
23use sp_consensus_grandpa::{AuthorityId, AuthorityList, AuthorityWeight, SetId};
24use sp_runtime::RuntimeDebug;
25use sp_std::prelude::*;
26
27pub const ALICE: Account = Account(0);
29pub const BOB: Account = Account(1);
31pub const CHARLIE: Account = Account(2);
33pub const DAVE: Account = Account(3);
35pub const EVE: Account = Account(4);
37pub const FERDIE: Account = Account(5);
39
40#[derive(RuntimeDebug, Clone, Copy)]
42pub struct Account(pub u16);
43
44impl Account {
45 pub fn public(&self) -> VerifyingKey {
47 self.pair().verifying_key()
48 }
49
50 pub fn pair(&self) -> SigningKey {
52 let data = self.0.encode();
53 let mut bytes = [0_u8; 32];
54 bytes[0..data.len()].copy_from_slice(&data);
55 SigningKey::from_bytes(&bytes)
56 }
57
58 pub fn sign(&self, msg: &[u8]) -> Signature {
60 use ed25519_dalek::Signer;
61 self.pair().sign(msg)
62 }
63}
64
65impl From<Account> for AuthorityId {
66 fn from(p: Account) -> Self {
67 sp_application_crypto::UncheckedFrom::unchecked_from(p.public().to_bytes())
68 }
69}
70
71pub fn voter_set() -> VoterSet<AuthorityId> {
73 VoterSet::new(authority_list()).unwrap()
74}
75
76pub fn verification_context(set_id: SetId) -> JustificationVerificationContext {
78 AuthoritySet { authorities: authority_list(), set_id }.try_into().unwrap()
79}
80
81pub fn authority_list() -> AuthorityList {
83 test_keyring().iter().map(|(id, w)| (AuthorityId::from(*id), *w)).collect()
84}
85
86pub fn test_keyring() -> Vec<(Account, AuthorityWeight)> {
88 vec![(ALICE, 1), (BOB, 1), (CHARLIE, 1)]
89}
90
91pub fn accounts(len: u16) -> Vec<Account> {
93 (0..len).map(Account).collect()
94}