referrerpolicy=no-referrer-when-downgrade

bp_test_utils/
keyring.rs

1// Copyright (C) Parity Technologies (UK) Ltd.
2// This file is part of Parity Bridges Common.
3
4// Parity Bridges Common is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8
9// Parity Bridges Common is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12// GNU General Public License for more details.
13
14// You should have received a copy of the GNU General Public License
15// along with Parity Bridges Common.  If not, see <http://www.gnu.org/licenses/>.
16
17//! Utilities for working with test accounts.
18
19use 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
27/// Set of test accounts with friendly names: Alice.
28pub const ALICE: Account = Account(0);
29/// Set of test accounts with friendly names: Bob.
30pub const BOB: Account = Account(1);
31/// Set of test accounts with friendly names: Charlie.
32pub const CHARLIE: Account = Account(2);
33/// Set of test accounts with friendly names: Dave.
34pub const DAVE: Account = Account(3);
35/// Set of test accounts with friendly names: Eve.
36pub const EVE: Account = Account(4);
37/// Set of test accounts with friendly names: Ferdie.
38pub const FERDIE: Account = Account(5);
39
40/// A test account which can be used to sign messages.
41#[derive(RuntimeDebug, Clone, Copy)]
42pub struct Account(pub u16);
43
44impl Account {
45	/// Returns public key of this account.
46	pub fn public(&self) -> VerifyingKey {
47		self.pair().verifying_key()
48	}
49
50	/// Returns key pair, used to sign data on behalf of this account.
51	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	/// Generate a signature of given message.
59	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
71/// Get a valid set of voters for a Grandpa round.
72pub fn voter_set() -> VoterSet<AuthorityId> {
73	VoterSet::new(authority_list()).unwrap()
74}
75
76/// Get a valid justification verification context for a GRANDPA round.
77pub fn verification_context(set_id: SetId) -> JustificationVerificationContext {
78	AuthoritySet { authorities: authority_list(), set_id }.try_into().unwrap()
79}
80
81/// Convenience function to get a list of Grandpa authorities.
82pub fn authority_list() -> AuthorityList {
83	test_keyring().iter().map(|(id, w)| (AuthorityId::from(*id), *w)).collect()
84}
85
86/// Get the corresponding identities from the keyring for the "standard" authority set.
87pub fn test_keyring() -> Vec<(Account, AuthorityWeight)> {
88	vec![(ALICE, 1), (BOB, 1), (CHARLIE, 1)]
89}
90
91/// Get a list of "unique" accounts.
92pub fn accounts(len: u16) -> Vec<Account> {
93	(0..len).map(Account).collect()
94}