referrerpolicy=no-referrer-when-downgrade

node_testing/
keyring.rs

1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
5
6// This program is free software: you can redistribute it and/or modify
7// it under the terms of the GNU General Public License as published by
8// the Free Software Foundation, either version 3 of the License, or
9// (at your option) any later version.
10
11// This program is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15
16// You should have received a copy of the GNU General Public License
17// along with this program. If not, see <https://www.gnu.org/licenses/>.
18
19//! Test accounts.
20
21use codec::Encode;
22use kitchensink_runtime::{CheckedExtrinsic, SessionKeys, TxExtension, UncheckedExtrinsic};
23use node_primitives::{AccountId, Balance, Nonce};
24use sp_core::{crypto::get_public_from_string_or_panic, ecdsa, ed25519, sr25519};
25use sp_crypto_hashing::blake2_256;
26use sp_keyring::Sr25519Keyring;
27use sp_runtime::generic::{self, Era, ExtrinsicFormat};
28
29/// Alice's account id.
30pub fn alice() -> AccountId {
31	Sr25519Keyring::Alice.into()
32}
33
34/// Bob's account id.
35pub fn bob() -> AccountId {
36	Sr25519Keyring::Bob.into()
37}
38
39/// Charlie's account id.
40pub fn charlie() -> AccountId {
41	Sr25519Keyring::Charlie.into()
42}
43
44/// Dave's account id.
45pub fn dave() -> AccountId {
46	Sr25519Keyring::Dave.into()
47}
48
49/// Eve's account id.
50pub fn eve() -> AccountId {
51	Sr25519Keyring::Eve.into()
52}
53
54/// Ferdie's account id.
55pub fn ferdie() -> AccountId {
56	Sr25519Keyring::Ferdie.into()
57}
58
59/// Convert keyrings into `SessionKeys`.
60///
61/// # Panics
62///
63/// Function will panic when invalid string is provided.
64pub fn session_keys_from_seed(seed: &str) -> SessionKeys {
65	SessionKeys {
66		grandpa: get_public_from_string_or_panic::<ed25519::Public>(seed).into(),
67		babe: get_public_from_string_or_panic::<sr25519::Public>(seed).into(),
68		im_online: get_public_from_string_or_panic::<sr25519::Public>(seed).into(),
69		authority_discovery: get_public_from_string_or_panic::<sr25519::Public>(seed).into(),
70		mixnet: get_public_from_string_or_panic::<sr25519::Public>(seed).into(),
71		beefy: get_public_from_string_or_panic::<ecdsa::Public>(seed).into(),
72	}
73}
74
75/// Returns transaction extra.
76pub fn tx_ext(nonce: Nonce, extra_fee: Balance) -> TxExtension {
77	(
78		frame_system::AuthorizeCall::new(),
79		frame_system::CheckNonZeroSender::new(),
80		frame_system::CheckSpecVersion::new(),
81		frame_system::CheckTxVersion::new(),
82		frame_system::CheckGenesis::new(),
83		frame_system::CheckEra::from(Era::mortal(256, 0)),
84		frame_system::CheckNonce::from(nonce),
85		frame_system::CheckWeight::new(),
86		pallet_skip_feeless_payment::SkipCheckIfFeeless::from(
87			pallet_asset_conversion_tx_payment::ChargeAssetTxPayment::from(extra_fee, None),
88		),
89		frame_metadata_hash_extension::CheckMetadataHash::new(false),
90		frame_system::WeightReclaim::new(),
91	)
92}
93
94/// Sign given `CheckedExtrinsic`.
95pub fn sign(
96	xt: CheckedExtrinsic,
97	spec_version: u32,
98	tx_version: u32,
99	genesis_hash: [u8; 32],
100	metadata_hash: Option<[u8; 32]>,
101) -> UncheckedExtrinsic {
102	match xt.format {
103		ExtrinsicFormat::Signed(signed, tx_ext) => {
104			let payload = (
105				xt.function,
106				tx_ext.clone(),
107				spec_version,
108				tx_version,
109				genesis_hash,
110				genesis_hash,
111				metadata_hash,
112			);
113			let key = Sr25519Keyring::from_account_id(&signed).unwrap();
114			let signature =
115				payload
116					.using_encoded(|b| {
117						if b.len() > 256 {
118							key.sign(&blake2_256(b))
119						} else {
120							key.sign(b)
121						}
122					})
123					.into();
124			generic::UncheckedExtrinsic::new_signed(
125				payload.0,
126				sp_runtime::MultiAddress::Id(signed),
127				signature,
128				tx_ext,
129			)
130			.into()
131		},
132		ExtrinsicFormat::Bare => generic::UncheckedExtrinsic::new_bare(xt.function).into(),
133		ExtrinsicFormat::General(ext_version, tx_ext) => generic::UncheckedExtrinsic::from_parts(
134			xt.function,
135			generic::Preamble::General(ext_version, tx_ext),
136		)
137		.into(),
138	}
139}