1use sp_core::{Pair, Public, sr25519};2use node_template_runtime::{3 AccountId, AuraConfig, BalancesConfig, GenesisConfig, GrandpaConfig,4 SudoConfig, IndicesConfig, SystemConfig, WASM_BINARY, Signature5};6use sp_consensus_aura::sr25519::{AuthorityId as AuraId};7use grandpa_primitives::{AuthorityId as GrandpaId};8use sc_service;9use sp_runtime::traits::{Verify, IdentifyAccount};101112131415pub type ChainSpec = sc_service::ChainSpec<GenesisConfig>;1617181920#[derive(Clone, Debug)]21pub enum Alternative {22 23 Development,24 25 LocalTestnet,26}272829pub fn get_from_seed<TPublic: Public>(seed: &str) -> <TPublic::Pair as Pair>::Public {30 TPublic::Pair::from_string(&format!("//{}", seed), None)31 .expect("static values are valid; qed")32 .public()33}3435type AccountPublic = <Signature as Verify>::Signer;363738pub fn get_account_id_from_seed<TPublic: Public>(seed: &str) -> AccountId where39 AccountPublic: From<<TPublic::Pair as Pair>::Public>40{41 AccountPublic::from(get_from_seed::<TPublic>(seed)).into_account()42}434445pub fn get_authority_keys_from_seed(s: &str) -> (AuraId, GrandpaId) {46 (47 get_from_seed::<AuraId>(s),48 get_from_seed::<GrandpaId>(s),49 )50}5152impl Alternative {53 54 pub(crate) fn load(self) -> Result<ChainSpec, String> {55 Ok(match self {56 Alternative::Development => ChainSpec::from_genesis(57 "Development",58 "dev",59 || testnet_genesis(vec![60 get_authority_keys_from_seed("Alice"),61 ],62 get_account_id_from_seed::<sr25519::Public>("Alice"),63 vec![64 get_account_id_from_seed::<sr25519::Public>("Alice"),65 get_account_id_from_seed::<sr25519::Public>("Bob"),66 get_account_id_from_seed::<sr25519::Public>("Alice//stash"),67 get_account_id_from_seed::<sr25519::Public>("Bob//stash"),68 ],69 true),70 vec![],71 None,72 None,73 None,74 None75 ),76 Alternative::LocalTestnet => ChainSpec::from_genesis(77 "Local Testnet",78 "local_testnet",79 || testnet_genesis(vec![80 get_authority_keys_from_seed("Alice"),81 get_authority_keys_from_seed("Bob"),82 ],83 get_account_id_from_seed::<sr25519::Public>("Alice"),84 vec![85 get_account_id_from_seed::<sr25519::Public>("Alice"),86 get_account_id_from_seed::<sr25519::Public>("Bob"),87 get_account_id_from_seed::<sr25519::Public>("Charlie"),88 get_account_id_from_seed::<sr25519::Public>("Dave"),89 get_account_id_from_seed::<sr25519::Public>("Eve"),90 get_account_id_from_seed::<sr25519::Public>("Ferdie"),91 get_account_id_from_seed::<sr25519::Public>("Alice//stash"),92 get_account_id_from_seed::<sr25519::Public>("Bob//stash"),93 get_account_id_from_seed::<sr25519::Public>("Charlie//stash"),94 get_account_id_from_seed::<sr25519::Public>("Dave//stash"),95 get_account_id_from_seed::<sr25519::Public>("Eve//stash"),96 get_account_id_from_seed::<sr25519::Public>("Ferdie//stash"),97 ],98 true),99 vec![],100 None,101 None,102 None,103 None104 ),105 })106 }107108 pub(crate) fn from(s: &str) -> Option<Self> {109 match s {110 "dev" => Some(Alternative::Development),111 "" | "local" => Some(Alternative::LocalTestnet),112 _ => None,113 }114 }115}116117fn testnet_genesis(initial_authorities: Vec<(AuraId, GrandpaId)>,118 root_key: AccountId,119 endowed_accounts: Vec<AccountId>,120 _enable_println: bool) -> GenesisConfig {121 GenesisConfig {122 system: Some(SystemConfig {123 code: WASM_BINARY.to_vec(),124 changes_trie_config: Default::default(),125 }),126 indices: Some(IndicesConfig {127 ids: endowed_accounts.clone(),128 }),129 balances: Some(BalancesConfig {130 balances: endowed_accounts.iter().cloned().map(|k|(k, 1 << 60)).collect(),131 vesting: vec![],132 }),133 sudo: Some(SudoConfig {134 key: root_key,135 }),136 aura: Some(AuraConfig {137 authorities: initial_authorities.iter().map(|x| (x.0.clone())).collect(),138 }),139 grandpa: Some(GrandpaConfig {140 authorities: initial_authorities.iter().map(|x| (x.1.clone(), 1)).collect(),141 }),142 }143}