Clean up definition for custom ss58 address formats (#4470)

* Clearer definition for custom ss58 address formats

* Fix subkey compile
This commit is contained in:
Wei Tang
2019-12-21 06:34:36 -08:00
committed by Gavin Wood
parent 2c2e0d772d
commit c3413fdea3
4 changed files with 42 additions and 30 deletions
+1
View File
@@ -6669,6 +6669,7 @@ dependencies = [
"frame-system 2.0.0",
"hex 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
"hex-literal 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
"itertools 0.8.2 (registry+https://github.com/rust-lang/crates.io-index)",
"node-primitives 2.0.0",
"node-runtime 2.0.0",
"pallet-balances 2.0.0",
+1
View File
@@ -21,6 +21,7 @@ frame-system = { version = "2.0.0", path = "../../../frame/system" }
pallet-balances = { version = "2.0.0", path = "../../../frame/balances" }
pallet-transaction-payment = { version = "2.0.0", path = "../../../frame/transaction-payment" }
rpassword = "4.0.1"
itertools = "0.8.2"
[features]
bench = []
+22 -14
View File
@@ -22,6 +22,7 @@ use bip39::{Language, Mnemonic, MnemonicType};
use clap::{App, ArgMatches, SubCommand};
use codec::{Decode, Encode};
use hex_literal::hex;
use itertools::Itertools;
use node_primitives::{Balance, Hash, Index, AccountId, Signature};
use node_runtime::{BalancesCall, Call, Runtime, SignedPayload, UncheckedExtrinsic, VERSION};
use sp_core::{
@@ -155,20 +156,25 @@ impl PublicT for sr25519::Public { fn into_runtime(self) -> AccountPublic { self
impl PublicT for ed25519::Public { fn into_runtime(self) -> AccountPublic { self.into() } }
impl PublicT for ecdsa::Public { fn into_runtime(self) -> AccountPublic { self.into() } }
fn get_app<'a, 'b>() -> App<'a, 'b> {
fn get_usage() -> String {
let networks = Ss58AddressFormat::all().iter().cloned().map(String::from).join("/");
let default_network = String::from(Ss58AddressFormat::default());
format!("
-e, --ed25519 'Use Ed25519/BIP39 cryptography'
-k, --secp256k1 'Use SECP256k1/ECDSA/BIP39 cryptography'
-s, --sr25519 'Use Schnorr/Ristretto x25519/BIP39 cryptography'
[network] -n, --network <network> 'Specify a network. One of {}. Default is {}'
[password] -p, --password <password> 'The password for the key'
--password-interactive 'You will be prompted for the password for the key.'
", networks, default_network)
}
fn get_app<'a, 'b>(usage: &'a str) -> App<'a, 'b> {
App::new("subkey")
.author("Parity Team <admin@parity.io>")
.about("Utility for generating and restoring with Substrate keys")
.version(env!("CARGO_PKG_VERSION"))
.args_from_usage("
-e, --ed25519 'Use Ed25519/BIP39 cryptography'
-k, --secp256k1 'Use SECP256k1/ECDSA/BIP39 cryptography'
-s, --sr25519 'Use Schnorr/Ristretto x25519/BIP39 cryptography'
[network] -n, --network <network> 'Specify a network. One of substrate \
(default), polkadot, kusama, dothereum, edgeware, or kulupu'
[password] -p, --password <password> 'The password for the key'
--password-interactive 'You will be prompted for the password for the key.'
")
.args_from_usage(usage)
.subcommands(vec![
SubCommand::with_name("generate")
.about("Generate a random account")
@@ -226,7 +232,8 @@ fn get_app<'a, 'b>() -> App<'a, 'b> {
}
fn main() {
let matches = get_app().get_matches();
let usage = get_usage();
let matches = get_app(&usage).get_matches();
if matches.is_present("ed25519") {
return execute::<Ed25519>(matches)
@@ -260,7 +267,7 @@ where
let maybe_network: Option<Ss58AddressFormat> = matches.value_of("network").map(|network| {
network
.try_into()
.expect("Invalid network name: must be polkadot/substrate/kusama/dothereum/edgeware")
.expect("Invalid network name. See --help for available networks.")
});
if let Some(network) = maybe_network {
set_default_ss58_version(network);
@@ -553,7 +560,8 @@ mod tests {
SignatureOf<CryptoType>: SignatureT,
PublicOf<CryptoType>: PublicT,
{
let app = get_app();
let usage = get_usage();
let app = get_app(&usage);
let password = None;
// Generate public key and seed.
@@ -581,7 +589,7 @@ mod tests {
// Verify the previous signature.
let arg_vec = vec!["subkey", "verify", &signature[..], &public_key[..]];
let matches = get_app().get_matches_from(arg_vec);
let matches = get_app(&usage).get_matches_from(arg_vec);
let matches = matches.subcommand().1.unwrap();
assert!(do_verify::<CryptoType>(matches, message));
}
+18 -16
View File
@@ -264,11 +264,7 @@ pub trait Ss58Codec: Sized + AsMut<[u8]> + AsRef<[u8]> + Default {
fn from_ss58check(s: &str) -> Result<Self, PublicError> {
Self::from_ss58check_with_version(s)
.and_then(|(r, v)| match v {
Ss58AddressFormat::SubstrateAccountDirect => Ok(r),
Ss58AddressFormat::PolkadotAccountDirect => Ok(r),
Ss58AddressFormat::KusamaAccountDirect => Ok(r),
Ss58AddressFormat::DothereumAccountDirect => Ok(r),
Ss58AddressFormat::EdgewareAccountDirect => Ok(r),
v if !v.is_custom() => Ok(r),
v if v == *DEFAULT_VERSION.lock() => Ok(r),
_ => Err(PublicError::UnknownVersion),
})
@@ -298,11 +294,7 @@ pub trait Ss58Codec: Sized + AsMut<[u8]> + AsRef<[u8]> + Default {
fn from_string(s: &str) -> Result<Self, PublicError> {
Self::from_string_with_version(s)
.and_then(|(r, v)| match v {
Ss58AddressFormat::SubstrateAccountDirect => Ok(r),
Ss58AddressFormat::PolkadotAccountDirect => Ok(r),
Ss58AddressFormat::KusamaAccountDirect => Ok(r),
Ss58AddressFormat::DothereumAccountDirect => Ok(r),
Ss58AddressFormat::EdgewareAccountDirect => Ok(r),
v if !v.is_custom() => Ok(r),
v if v == *DEFAULT_VERSION.lock() => Ok(r),
_ => Err(PublicError::UnknownVersion),
})
@@ -377,6 +369,14 @@ macro_rules! ss58_address_format {
pub fn all() -> &'static [Ss58AddressFormat] {
&ALL_SS58_ADDRESS_FORMATS
}
/// Whether the address is custom.
pub fn is_custom(&self) -> bool {
match self {
Self::Custom(_) => true,
_ => false,
}
}
}
impl From<Ss58AddressFormat> for u8 {
@@ -410,6 +410,13 @@ macro_rules! ss58_address_format {
}
}
#[cfg(feature = "std")]
impl Default for Ss58AddressFormat {
fn default() -> Self {
*DEFAULT_VERSION.lock()
}
}
#[cfg(feature = "std")]
impl From<Ss58AddressFormat> for String {
fn from(x: Ss58AddressFormat) -> String {
@@ -442,12 +449,7 @@ ss58_address_format!(
/// typically used not just to encode format/version but also network identity) that is used for
/// encoding and decoding SS58 addresses. If an unknown version is provided then it fails.
///
/// Current known "versions" are:
/// - 0 direct (payload) checksum for 32-byte *25519 Polkadot addresses.
/// - 2 direct (payload) checksum for 32-byte *25519 Kusama addresses.
/// - 7 direct (payload) checksum for 32-byte *25519 Edgeware addresses.
/// - 20 direct (payload) checksum for 32-byte *25519 Dothereum addresses.
/// - 42 direct (payload) checksum for 32-byte *25519 addresses on any Substrate-based network.
/// See `ss58_address_format!` for all current known "versions".
#[cfg(feature = "std")]
pub fn set_default_ss58_version(version: Ss58AddressFormat) {
*DEFAULT_VERSION.lock() = version