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
//! Moonriver 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 moonriver_runtime::{
29
	currency::MOVR, 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
pub fn development_chain_spec(mnemonic: Option<String>, num_accounts: Option<u32>) -> ChainSpec {
41
	// Default mnemonic if none was provided
42
	let parent_mnemonic = mnemonic.unwrap_or_else(|| {
43
		"bottom drive obey lake curtain smoke basket hold race lonely fit walk".to_string()
44
	});
45
	let mut accounts = generate_accounts(parent_mnemonic, num_accounts.unwrap_or(10));
46
	// We add Gerald here
47
	accounts.push(AccountId::from(hex!(
48
		"6Be02d1d3665660d22FF9624b7BE0551ee1Ac91b"
49
	)));
50

            
51
	ChainSpec::builder(
52
		WASM_BINARY.expect("WASM binary was not build, please build it!"),
53
		Extensions {
54
			relay_chain: "dev-service".into(),
55
			para_id: Default::default(),
56
		},
57
	)
58
	.with_name("Moonriver Development Testnet")
59
	.with_id("moonriver_dev")
60
	.with_chain_type(ChainType::Development)
61
	.with_properties(
62
		serde_json::from_str(
63
			"{\"tokenDecimals\": 18, \"tokenSymbol\": \"MOVR\", \"SS58Prefix\": 1285}",
64
		)
65
		.expect("Provided valid json map"),
66
	)
67
	.with_genesis_config(testnet_genesis(
68
		// Treasury Council members: Baltathar, Charleth and Dorothy
69
		vec![accounts[1], accounts[2], accounts[3]],
70
		// Open Tech committee members: Alith and Baltathar
71
		vec![accounts[0], accounts[1]],
72
		// Collator Candidate: Alice -> Alith
73
		vec![(
74
			AccountId::from(hex!("f24FF3a9CF04c71Dbc94D0b566f7A27B94566cac")),
75
			get_from_seed::<NimbusId>("Alice"),
76
			100_000 * MOVR,
77
		)],
78
		// Delegations
79
		vec![],
80
		accounts.clone(),
81
		3_000_000 * MOVR,
82
		Default::default(), // para_id
83
		1281,               //ChainId
84
	))
85
	.build()
86
}
87

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

            
153
#[cfg(test)]
154
mod tests {
155
	use super::*;
156
	#[test]
157
	fn test_derived_pairs_1() {
158
		let mnemonic =
159
			"bottom drive obey lake curtain smoke basket hold race lonely fit walk".to_string();
160
		let accounts = 10;
161
		let pairs = derive_bip44_pairs_from_mnemonic::<ecdsa::Public>(&mnemonic, accounts);
162
		let first_account = get_account_id_from_pair(pairs.first().unwrap().clone()).unwrap();
163
		let last_account = get_account_id_from_pair(pairs.last().unwrap().clone()).unwrap();
164

            
165
		let expected_first_account =
166
			AccountId::from(hex!("f24FF3a9CF04c71Dbc94D0b566f7A27B94566cac"));
167
		let expected_last_account =
168
			AccountId::from(hex!("2898FE7a42Be376C8BC7AF536A940F7Fd5aDd423"));
169
		assert_eq!(first_account, expected_first_account);
170
		assert_eq!(last_account, expected_last_account);
171
		assert_eq!(pairs.len(), 10);
172
	}
173
	#[test]
174
	fn test_derived_pairs_2() {
175
		let mnemonic =
176
			"slab nerve salon plastic filter inherit valve ozone crash thumb quality whale"
177
				.to_string();
178
		let accounts = 20;
179
		let pairs = derive_bip44_pairs_from_mnemonic::<ecdsa::Public>(&mnemonic, accounts);
180
		let first_account = get_account_id_from_pair(pairs.first().unwrap().clone()).unwrap();
181
		let last_account = get_account_id_from_pair(pairs.last().unwrap().clone()).unwrap();
182

            
183
		let expected_first_account =
184
			AccountId::from(hex!("1e56ca71b596f2b784a27a2fdffef053dbdeff83"));
185
		let expected_last_account =
186
			AccountId::from(hex!("4148202BF0c0Ad7697Cff87EbB83340C80c947f8"));
187
		assert_eq!(first_account, expected_first_account);
188
		assert_eq!(last_account, expected_last_account);
189
		assert_eq!(pairs.len(), 20);
190
	}
191
}