moonbeam_service/chain_spec/
mod.rs

1// Copyright 2019-2025 PureStake Inc.
2// This file is part of Moonbeam.
3
4// Moonbeam 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// Moonbeam 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 Moonbeam.  If not, see <http://www.gnu.org/licenses/>.
16use bip32::ExtendedPrivateKey;
17use bip39::{Language, Mnemonic, Seed};
18use libsecp256k1::{PublicKey, PublicKeyFormat};
19use log::debug;
20use moonbeam_cli_opt::account_key::Secp256k1SecretKey;
21pub use moonbeam_core_primitives::AccountId;
22use sc_chain_spec::{ChainSpecExtension, ChainSpecGroup};
23use serde::{Deserialize, Serialize};
24use sha3::{Digest, Keccak256};
25use sp_core::{ecdsa, Pair, Public, H160, H256};
26
27#[cfg(feature = "moonbase-native")]
28pub mod moonbase;
29#[cfg(feature = "moonbeam-native")]
30pub mod moonbeam;
31#[cfg(feature = "moonriver-native")]
32pub mod moonriver;
33
34pub type RawChainSpec = sc_service::GenericChainSpec<Extensions>;
35
36#[derive(Default, Clone, Serialize, Deserialize, ChainSpecExtension, ChainSpecGroup)]
37#[serde(rename_all = "camelCase")]
38pub struct Extensions {
39	/// The relay chain of the Parachain.
40	pub relay_chain: String,
41	/// The id of the Parachain.
42	pub para_id: u32,
43}
44
45impl Extensions {
46	/// Try to get the extension from the given `ChainSpec`.
47	pub fn try_get(chain_spec: &dyn sc_service::ChainSpec) -> Option<&Self> {
48		sc_chain_spec::get_extension(chain_spec.extensions())
49	}
50}
51
52/// Helper function to derive `num_accounts` child pairs from mnemonics
53/// Substrate derive function cannot be used because the derivation is different than Ethereum's
54/// https://substrate.dev/rustdocs/v2.0.0/src/sp_core/ecdsa.rs.html#460-470
55pub fn derive_bip44_pairs_from_mnemonic<TPublic: Public>(
56	mnemonic: &str,
57	num_accounts: u32,
58) -> Vec<TPublic::Pair> {
59	let seed = Mnemonic::from_phrase(mnemonic, Language::English)
60		.map(|x| Seed::new(&x, ""))
61		.expect("Wrong mnemonic provided");
62
63	let mut childs = Vec::new();
64	for i in 0..num_accounts {
65		if let Some(child_pair) = format!("m/44'/60'/0'/0/{}", i)
66			.parse()
67			.ok()
68			.and_then(|derivation_path| {
69				ExtendedPrivateKey::<Secp256k1SecretKey>::derive_from_path(&seed, &derivation_path)
70					.ok()
71			})
72			.and_then(|extended| {
73				TPublic::Pair::from_seed_slice(&extended.private_key().0.serialize()).ok()
74			}) {
75			childs.push(child_pair);
76		} else {
77			log::error!("An error ocurred while deriving key {} from parent", i)
78		}
79	}
80	childs
81}
82
83/// Helper function to get an `AccountId` from an ECDSA Key Pair.
84pub fn get_account_id_from_pair(pair: ecdsa::Pair) -> Option<AccountId> {
85	let decompressed = PublicKey::parse_slice(&pair.public().0, Some(PublicKeyFormat::Compressed))
86		.ok()?
87		.serialize();
88
89	let mut m = [0u8; 64];
90	m.copy_from_slice(&decompressed[1..65]);
91
92	Some(H160::from(H256::from_slice(Keccak256::digest(&m).as_slice())).into())
93}
94
95/// Function to generate accounts given a mnemonic and a number of child accounts to be generated
96/// Defaults to a default mnemonic if no mnemonic is supplied
97pub fn generate_accounts(mnemonic: String, num_accounts: u32) -> Vec<AccountId> {
98	let childs = derive_bip44_pairs_from_mnemonic::<ecdsa::Public>(&mnemonic, num_accounts);
99	debug!("Account Generation");
100	childs
101		.iter()
102		.filter_map(|par| {
103			let account = get_account_id_from_pair(par.clone());
104			debug!(
105				"private_key {} --------> Account {:x?}",
106				sp_core::hexdisplay::HexDisplay::from(&par.clone().seed()),
107				account
108			);
109			account
110		})
111		.collect()
112}
113
114/// Helper function to generate a crypto pair from seed
115pub fn get_from_seed<TPublic: Public>(seed: &str) -> <TPublic::Pair as Pair>::Public {
116	TPublic::Pair::from_string(&format!("//{}", seed), None)
117		.expect("static values are valid; qed")
118		.public()
119}