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/>.
16

            
17
//! Moonbase Chain Specifications and utilities for building them.
18
//!
19
//! Learn more about Substrate chain specifications at
20
//! https://substrate.dev/docs/en/knowledgebase/integrate/chain-spec
21

            
22
#[cfg(test)]
23
use crate::chain_spec::{derive_bip44_pairs_from_mnemonic, get_account_id_from_pair};
24
use crate::chain_spec::{generate_accounts, get_from_seed, Extensions};
25
use crate::HostFunctions;
26
use cumulus_primitives_core::ParaId;
27
use hex_literal::hex;
28
use moonbase_runtime::{
29
	currency::UNIT, genesis_config_preset::testnet_genesis, AccountId, WASM_BINARY,
30
};
31
use nimbus_primitives::NimbusId;
32
use sc_service::ChainType;
33
#[cfg(test)]
34
use sp_core::ecdsa;
35

            
36
/// Specialized `ChainSpec`. This is a specialization of the general Substrate ChainSpec type.
37
pub type ChainSpec = sc_service::GenericChainSpec<Extensions, HostFunctions>;
38

            
39
/// Generate a chain spec for use with the development service.
40
2
pub fn development_chain_spec(mnemonic: Option<String>, num_accounts: Option<u32>) -> ChainSpec {
41
2
	// Default mnemonic if none was provided
42
2
	let parent_mnemonic = mnemonic.unwrap_or_else(|| {
43
2
		"bottom drive obey lake curtain smoke basket hold race lonely fit walk".to_string()
44
2
	});
45
2
	// We prefund the standard dev accounts plus Gerald
46
2
	let mut accounts = generate_accounts(parent_mnemonic, num_accounts.unwrap_or(10));
47
2
	accounts.push(AccountId::from(hex!(
48
2
		"6Be02d1d3665660d22FF9624b7BE0551ee1Ac91b"
49
2
	)));
50
2

            
51
2
	// Prefund the benchmark account for frontier, if compiling for benchmarks
52
2
	#[cfg(feature = "runtime-benchmarks")]
53
2
	accounts.push(AccountId::from(hex!(
54
2
		"1000000000000000000000000000000000000001"
55
2
	)));
56
2

            
57
2
	ChainSpec::builder(
58
2
		WASM_BINARY.expect("WASM binary was not build, please build it!"),
59
2
		Extensions {
60
2
			relay_chain: "dev-service".into(),
61
2
			para_id: Default::default(),
62
2
		},
63
2
	)
64
2
	.with_name("Moonbase Development Testnet")
65
2
	.with_id("moonbase_dev")
66
2
	.with_chain_type(ChainType::Development)
67
2
	.with_properties(
68
2
		serde_json::from_str(
69
2
			"{\"tokenDecimals\": 18, \"tokenSymbol\": \"DEV\", \"SS58Prefix\": 1287}",
70
2
		)
71
2
		.expect("Provided valid json map"),
72
2
	)
73
2
	.with_genesis_config(testnet_genesis(
74
2
		// Alith is Sudo
75
2
		accounts[0],
76
2
		// Treasury Council members: Baltathar, Charleth and Dorothy
77
2
		vec![accounts[1], accounts[2], accounts[3]],
78
2
		// Open Tech committee members: Alith, Baltathar and Charleth
79
2
		vec![accounts[0], accounts[1], accounts[2]],
80
2
		// Collator Candidate: Alice -> Alith
81
2
		vec![(
82
2
			accounts[0],
83
2
			get_from_seed::<NimbusId>("Alice"),
84
2
			1_000 * UNIT,
85
2
		)],
86
2
		// Delegations
87
2
		vec![],
88
2
		accounts.clone(),
89
2
		3_000_000 * UNIT,
90
2
		Default::default(), // para_id
91
2
		1281,               //ChainId
92
2
	))
93
2
	.build()
94
2
}
95

            
96
/// Generate a default spec for the parachain service. Use this as a starting point when launching
97
/// a custom chain.
98
pub fn get_chain_spec(para_id: ParaId) -> ChainSpec {
99
	ChainSpec::builder(
100
		WASM_BINARY.expect("WASM binary was not build, please build it!"),
101
		Extensions {
102
			relay_chain: "westend-local".into(),
103
			para_id: para_id.into(),
104
		},
105
	)
106
	// TODO Apps depends on this string to determine whether the chain is an ethereum compat
107
	// or not. We should decide the proper strings, and update Apps accordingly.
108
	// Or maybe Apps can be smart enough to say if the string contains "moonbeam" at all...
109
	.with_name("Moonbase Local Testnet")
110
	.with_id("moonbase_local")
111
	.with_chain_type(ChainType::Local)
112
	.with_properties(
113
		serde_json::from_str(
114
			"{\"tokenDecimals\": 18, \"tokenSymbol\": \"DEV\", \"SS58Prefix\": 1287}",
115
		)
116
		.expect("Provided valid json map"),
117
	)
118
	.with_genesis_config(testnet_genesis(
119
		// Alith is Sudo
120
		AccountId::from(hex!("f24FF3a9CF04c71Dbc94D0b566f7A27B94566cac")),
121
		// Treasury Council members: Baltathar, Charleth and Dorothy
122
		vec![
123
			AccountId::from(hex!("3Cd0A705a2DC65e5b1E1205896BaA2be8A07c6e0")),
124
			AccountId::from(hex!("798d4Ba9baf0064Ec19eB4F0a1a45785ae9D6DFc")),
125
			AccountId::from(hex!("773539d4Ac0e786233D90A233654ccEE26a613D9")),
126
		],
127
		// Open Tech committee members: Alith and Baltathar
128
		vec![
129
			AccountId::from(hex!("f24FF3a9CF04c71Dbc94D0b566f7A27B94566cac")),
130
			AccountId::from(hex!("3Cd0A705a2DC65e5b1E1205896BaA2be8A07c6e0")),
131
		],
132
		// Collator Candidates
133
		vec![
134
			// Alice -> Alith
135
			(
136
				AccountId::from(hex!("f24FF3a9CF04c71Dbc94D0b566f7A27B94566cac")),
137
				get_from_seed::<NimbusId>("Alice"),
138
				1_000 * UNIT,
139
			),
140
			// Bob -> Baltathar
141
			(
142
				AccountId::from(hex!("3Cd0A705a2DC65e5b1E1205896BaA2be8A07c6e0")),
143
				get_from_seed::<NimbusId>("Bob"),
144
				1_000 * UNIT,
145
			),
146
		],
147
		// Delegations
148
		vec![],
149
		// Endowed: Alith, Baltathar, Charleth and Dorothy
150
		vec![
151
			AccountId::from(hex!("f24FF3a9CF04c71Dbc94D0b566f7A27B94566cac")),
152
			AccountId::from(hex!("3Cd0A705a2DC65e5b1E1205896BaA2be8A07c6e0")),
153
			AccountId::from(hex!("798d4Ba9baf0064Ec19eB4F0a1a45785ae9D6DFc")),
154
			AccountId::from(hex!("773539d4Ac0e786233D90A233654ccEE26a613D9")),
155
		],
156
		3_000_000 * UNIT,
157
		para_id,
158
		1280, //ChainId
159
	))
160
	.build()
161
}
162

            
163
#[cfg(test)]
164
mod tests {
165
	use super::*;
166
	#[test]
167
	fn test_derived_pairs_1() {
168
		let mnemonic =
169
			"bottom drive obey lake curtain smoke basket hold race lonely fit walk".to_string();
170
		let accounts = 10;
171
		let pairs = derive_bip44_pairs_from_mnemonic::<ecdsa::Public>(&mnemonic, accounts);
172
		let first_account = get_account_id_from_pair(pairs.first().unwrap().clone()).unwrap();
173
		let last_account = get_account_id_from_pair(pairs.last().unwrap().clone()).unwrap();
174

            
175
		let expected_first_account =
176
			AccountId::from(hex!("f24FF3a9CF04c71Dbc94D0b566f7A27B94566cac"));
177
		let expected_last_account =
178
			AccountId::from(hex!("2898FE7a42Be376C8BC7AF536A940F7Fd5aDd423"));
179
		assert_eq!(first_account, expected_first_account);
180
		assert_eq!(last_account, expected_last_account);
181
		assert_eq!(pairs.len(), 10);
182
	}
183
	#[test]
184
	fn test_derived_pairs_2() {
185
		let mnemonic =
186
			"slab nerve salon plastic filter inherit valve ozone crash thumb quality whale"
187
				.to_string();
188
		let accounts = 20;
189
		let pairs = derive_bip44_pairs_from_mnemonic::<ecdsa::Public>(&mnemonic, accounts);
190
		let first_account = get_account_id_from_pair(pairs.first().unwrap().clone()).unwrap();
191
		let last_account = get_account_id_from_pair(pairs.last().unwrap().clone()).unwrap();
192

            
193
		let expected_first_account =
194
			AccountId::from(hex!("1e56ca71b596f2b784a27a2fdffef053dbdeff83"));
195
		let expected_last_account =
196
			AccountId::from(hex!("4148202BF0c0Ad7697Cff87EbB83340C80c947f8"));
197
		assert_eq!(first_account, expected_first_account);
198
		assert_eq!(last_account, expected_last_account);
199
		assert_eq!(pairs.len(), 20);
200
	}
201
}