Function sc_cli::utils::format_seed

source ·
pub fn format_seed<P: Pair>(seed: SeedFor<P>) -> String
Expand description

formats seed as hex

Examples found in repository?
src/commands/vanity.rs (line 106)
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
fn generate_key<Pair>(
	desired: &str,
	network_override: Ss58AddressFormat,
) -> Result<String, &'static str>
where
	Pair: sp_core::Pair,
	Pair::Public: IdentifyAccount,
	<Pair::Public as IdentifyAccount>::AccountId: Ss58Codec,
{
	println!("Generating key containing pattern '{}'", desired);

	let top = 45 + (desired.len() * 48);
	let mut best = 0;
	let mut seed = Pair::Seed::default();
	let mut done = 0;

	loop {
		if done % 100000 == 0 {
			OsRng.fill_bytes(seed.as_mut());
		} else {
			next_seed(seed.as_mut());
		}

		let p = Pair::from_seed(&seed);
		let ss58 = p.public().into_account().to_ss58check_with_version(network_override);
		let score = calculate_score(desired, &ss58);
		if score > best || desired.len() < 2 {
			best = score;
			if best >= top {
				println!("best: {} == top: {}", best, top);
				return Ok(utils::format_seed::<Pair>(seed.clone()))
			}
		}
		done += 1;

		if done % good_waypoint(done) == 0 {
			println!("{} keys searched; best is {}/{} complete", done, best, top);
		}
	}
}
More examples
Hide additional examples
src/commands/utils.rs (line 86)
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
pub fn print_from_uri<Pair>(
	uri: &str,
	password: Option<SecretString>,
	network_override: Option<Ss58AddressFormat>,
	output: OutputType,
) where
	Pair: sp_core::Pair,
	Pair::Public: Into<MultiSigner>,
{
	let password = password.as_ref().map(|s| s.expose_secret().as_str());
	let network_id = String::from(unwrap_or_default_ss58_version(network_override));
	if let Ok((pair, seed)) = Pair::from_phrase(uri, password) {
		let public_key = pair.public();
		let network_override = unwrap_or_default_ss58_version(network_override);

		match output {
			OutputType::Json => {
				let json = json!({
					"secretPhrase": uri,
					"networkId": network_id,
					"secretSeed": format_seed::<Pair>(seed),
					"publicKey": format_public_key::<Pair>(public_key.clone()),
					"ss58PublicKey": public_key.to_ss58check_with_version(network_override),
					"accountId": format_account_id::<Pair>(public_key),
					"ss58Address": pair.public().into().into_account().to_ss58check_with_version(network_override),
				});
				println!(
					"{}",
					serde_json::to_string_pretty(&json).expect("Json pretty print failed")
				);
			},
			OutputType::Text => {
				println!(
					"Secret phrase:       {}\n  \
					Network ID:        {}\n  \
					Secret seed:       {}\n  \
					Public key (hex):  {}\n  \
					Account ID:        {}\n  \
					Public key (SS58): {}\n  \
					SS58 Address:      {}",
					uri,
					network_id,
					format_seed::<Pair>(seed),
					format_public_key::<Pair>(public_key.clone()),
					format_account_id::<Pair>(public_key.clone()),
					public_key.to_ss58check_with_version(network_override),
					pair.public().into().into_account().to_ss58check_with_version(network_override),
				);
			},
		}
	} else if let Ok((pair, seed)) = Pair::from_string_with_seed(uri, password) {
		let public_key = pair.public();
		let network_override = unwrap_or_default_ss58_version(network_override);

		match output {
			OutputType::Json => {
				let json = json!({
					"secretKeyUri": uri,
					"networkId": network_id,
					"secretSeed": if let Some(seed) = seed { format_seed::<Pair>(seed) } else { "n/a".into() },
					"publicKey": format_public_key::<Pair>(public_key.clone()),
					"ss58PublicKey": public_key.to_ss58check_with_version(network_override),
					"accountId": format_account_id::<Pair>(public_key),
					"ss58Address": pair.public().into().into_account().to_ss58check_with_version(network_override),
				});
				println!(
					"{}",
					serde_json::to_string_pretty(&json).expect("Json pretty print failed")
				);
			},
			OutputType::Text => {
				println!(
					"Secret Key URI `{}` is account:\n  \
					Network ID:        {} \n \
					Secret seed:       {}\n  \
					Public key (hex):  {}\n  \
					Account ID:        {}\n  \
					Public key (SS58): {}\n  \
					SS58 Address:      {}",
					uri,
					network_id,
					if let Some(seed) = seed { format_seed::<Pair>(seed) } else { "n/a".into() },
					format_public_key::<Pair>(public_key.clone()),
					format_account_id::<Pair>(public_key.clone()),
					public_key.to_ss58check_with_version(network_override),
					pair.public().into().into_account().to_ss58check_with_version(network_override),
				);
			},
		}
	} else if let Ok((public_key, network)) = Pair::Public::from_string_with_version(uri) {
		let network_override = network_override.unwrap_or(network);

		match output {
			OutputType::Json => {
				let json = json!({
					"publicKeyUri": uri,
					"networkId": String::from(network_override),
					"publicKey": format_public_key::<Pair>(public_key.clone()),
					"accountId": format_account_id::<Pair>(public_key.clone()),
					"ss58PublicKey": public_key.to_ss58check_with_version(network_override),
					"ss58Address": public_key.to_ss58check_with_version(network_override),
				});

				println!(
					"{}",
					serde_json::to_string_pretty(&json).expect("Json pretty print failed")
				);
			},
			OutputType::Text => {
				println!(
					"Public Key URI `{}` is account:\n  \
					 Network ID/Version: {}\n  \
					 Public key (hex):   {}\n  \
					 Account ID:         {}\n  \
					 Public key (SS58):  {}\n  \
					 SS58 Address:       {}",
					uri,
					String::from(network_override),
					format_public_key::<Pair>(public_key.clone()),
					format_account_id::<Pair>(public_key.clone()),
					public_key.to_ss58check_with_version(network_override),
					public_key.to_ss58check_with_version(network_override),
				);
			},
		}
	} else {
		println!("Invalid phrase/URI given");
	}
}