From 281c7c6608bec03c1233bf1d1f0de21f1ae485c6 Mon Sep 17 00:00:00 2001 From: Florian Franzen Date: Fri, 14 Feb 2025 11:09:02 +0100 Subject: [PATCH] primitives: properly integrate with ss58 registry (#1516) --- Cargo.lock | 4 ++-- Cargo.toml | 2 +- chronicle/src/main.rs | 1 + node/src/chain_spec.rs | 7 +++++-- node/src/main.rs | 1 + primitives/src/lib.rs | 24 +++++++++++++++++++----- runtime/src/configs/core.rs | 4 ++-- runtime/src/lib.rs | 1 - tc-cli/src/main.rs | 1 + 9 files changed, 32 insertions(+), 13 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 87ba68d6f..aa8d7fcdd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -18911,8 +18911,8 @@ dependencies = [ [[package]] name = "ss58-registry" -version = "1.49.0" -source = "git+https://github.com/Analog-Labs/ss58-registry.git#8067ef285927fc37dafb5a63eb72bc79c466f190" +version = "1.51.0" +source = "git+https://github.com/Analog-Labs/ss58-registry.git?tag=v1.51.0-anlog0#98cbe337340c34182a69e136aa5c46f5d02acf9f" dependencies = [ "Inflector", "num-format", diff --git a/Cargo.toml b/Cargo.toml index 1af03af14..d2f8c19b3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -182,4 +182,4 @@ zeroize = { opt-level = 3 } [patch.crates-io] # Testnet assginment was not accepted upstream -ss58-registry = { git = 'https://github.com/Analog-Labs/ss58-registry.git' } +ss58-registry = { git = 'https://github.com/Analog-Labs/ss58-registry.git', tag='v1.51.0-anlog0' } diff --git a/chronicle/src/main.rs b/chronicle/src/main.rs index 1c4b16615..f1dcd694c 100644 --- a/chronicle/src/main.rs +++ b/chronicle/src/main.rs @@ -79,6 +79,7 @@ fn generate_key(path: &Path) -> Result<()> { #[tokio::main] async fn main() -> Result<()> { init_logger(); + time_primitives::init_ss58_version(); let args = ChronicleArgs::parse(); if args.cctp_sender.is_some() && args.cctp_attestation.is_none() { diff --git a/node/src/chain_spec.rs b/node/src/chain_spec.rs index d435b2b25..d6fa16c07 100644 --- a/node/src/chain_spec.rs +++ b/node/src/chain_spec.rs @@ -11,9 +11,12 @@ use sp_authority_discovery::AuthorityId as DiscoveryId; use sp_consensus_babe::AuthorityId as BabeId; use sp_consensus_grandpa::AuthorityId as GrandpaId; use sp_core::crypto::UncheckedInto; + use sp_keyring::{AccountKeyring, Ed25519Keyring}; -use time_primitives::{AccountId, Balance, Block, BlockNumber, ANLOG, SS58_PREFIX, TOKEN_DECIMALS}; +use time_primitives::{ + AccountId, Balance, Block, BlockNumber, ANLOG, SS58_ADDRESS_PREFIX, TOKEN_DECIMALS, +}; use timechain_runtime::{RUNTIME_VARIANT, WASM_BINARY}; /// Stash and float for validators @@ -146,7 +149,7 @@ impl GenesisKeysConfig { let mut properties = sc_chain_spec::Properties::new(); properties.insert("tokenSymbol".into(), token_symbol.into()); properties.insert("tokenDecimals".into(), TOKEN_DECIMALS.into()); - properties.insert("ss58Format".into(), SS58_PREFIX.into()); + properties.insert("ss58Format".into(), SS58_ADDRESS_PREFIX.into()); // Add default telemetry for all deployed networks let telemetry = if chain_type != ChainType::Local { diff --git a/node/src/main.rs b/node/src/main.rs index 8e220e820..01975aa9a 100644 --- a/node/src/main.rs +++ b/node/src/main.rs @@ -11,5 +11,6 @@ mod rpc; use polkadot_sdk::*; fn main() -> sc_cli::Result<()> { + time_primitives::init_ss58_version(); command::run() } diff --git a/primitives/src/lib.rs b/primitives/src/lib.rs index 64a4b4472..c81edf9e9 100644 --- a/primitives/src/lib.rs +++ b/primitives/src/lib.rs @@ -4,7 +4,9 @@ use anyhow::Result; use polkadot_sdk::{sp_api, sp_core, sp_runtime}; use scale_info::prelude::{string::String, vec::Vec}; -use sp_core::crypto::Ss58Codec; +use sp_core::crypto::{ + from_known_address_format, set_default_ss58_version, Ss58AddressFormatRegistry, Ss58Codec, +}; use sp_runtime::{ generic, traits::{BlakeTwo256, Get, IdentifyAccount, Verify}, @@ -85,19 +87,30 @@ pub type PublicKey = MultiSigner; /// Official timechain SS58 prefix (`an`) #[cfg(not(any(feature = "testnet", feature = "develop")))] -pub const SS58_PREFIX: u16 = 12850; +pub const SS58_ADDRESS_FORMAT: Ss58AddressFormatRegistry = + Ss58AddressFormatRegistry::AnalogTimechainAccount; /// Unofficial testnet SS58 prefix (`at`) #[cfg(all(feature = "testnet", not(feature = "develop")))] -pub const SS58_PREFIX: u16 = 12851; +pub const SS58_ADDRESS_FORMAT: Ss58AddressFormatRegistry = + Ss58AddressFormatRegistry::AnalogTestnetAccount; /// Unofficial develop SS58 prefix (`az`) #[cfg(feature = "develop")] -pub const SS58_PREFIX: u16 = 42; +pub const SS58_ADDRESS_FORMAT: Ss58AddressFormatRegistry = + Ss58AddressFormatRegistry::AnalogDevelopAccount; + +/// Export const primitive of raw prefifx +pub const SS58_ADDRESS_PREFIX: u16 = from_known_address_format(SS58_ADDRESS_FORMAT); + +/// Helper to set default ss58 format +pub fn init_ss58_version() { + set_default_ss58_version(SS58_ADDRESS_FORMAT.into()) +} /// Helper to format address with correct prefix pub fn format_address(account: &AccountId) -> String { - account.to_ss58check_with_version(sp_core::crypto::Ss58AddressFormat::custom(SS58_PREFIX)) + account.to_ss58check_with_version(SS58_ADDRESS_FORMAT.into()) } uint::construct_uint! { @@ -199,6 +212,7 @@ mod tests { "5DiPYChakvifNd4oC9s5ezGYn2WebiVdf8cUXRcG1XF9Jcfm", "an7DqmJUeV3tjLf1NH7LRi4hu2YBftLs9K6LrAbf9UEvR8ePm", "atVc4Jo8T5fsFLQu4FBdwsjj6PbLVQ5ATPHoFYLpR6xyd9N1V", + "azszGrHnFgHqmLAnkDFwU3QkHkeVJuoTmTVFev5ygjh2q9wqM", ]; for addr in addrs { let account = AccountId::from_string(addr).unwrap(); diff --git a/runtime/src/configs/core.rs b/runtime/src/configs/core.rs index e02195caa..8fee2ee77 100644 --- a/runtime/src/configs/core.rs +++ b/runtime/src/configs/core.rs @@ -16,7 +16,7 @@ use frame_system::limits::{BlockLength, BlockWeights}; use sp_version::RuntimeVersion; use sp_runtime::Perbill; -use time_primitives::{BlockHash, BlockNumber, Moment, SS58_PREFIX}; +use time_primitives::{BlockHash, BlockNumber, Moment, SS58_ADDRESS_PREFIX}; // Can't use `FungibleAdapter` here until Treasury pallet migrates to fungibles // @@ -92,7 +92,7 @@ impl frame_system::Config for Runtime { type Version = Version; type AccountData = pallet_balances::AccountData; type SystemWeightInfo = weights::frame_system::WeightInfo; - type SS58Prefix = ConstU16<{ SS58_PREFIX }>; + type SS58Prefix = ConstU16<{ SS58_ADDRESS_PREFIX }>; type MaxConsumers = ConstU32<16>; } diff --git a/runtime/src/lib.rs b/runtime/src/lib.rs index 837c7e94c..0ea3b3f02 100644 --- a/runtime/src/lib.rs +++ b/runtime/src/lib.rs @@ -155,7 +155,6 @@ pub use time_primitives::{ ErrorMsg, Gateway, GatewayMessage, Header, MemberStatus, MembersInterface, Moment, NetworkId, NetworksInterface, Nonce, PeerId, ProofOfKnowledge, PublicKey, ShardId, ShardStatus, Signature, Task, TaskId, TaskResult, TssPublicKey, TssSignature, ANLOG, MICROANLOG, MILLIANLOG, - SS58_PREFIX, }; // A few exports that help ease life for downstream crates. diff --git a/tc-cli/src/main.rs b/tc-cli/src/main.rs index d5c802c91..7ebc63cff 100644 --- a/tc-cli/src/main.rs +++ b/tc-cli/src/main.rs @@ -178,6 +178,7 @@ enum Command { #[tokio::main] async fn main() { + time_primitives::init_ss58_version(); rustls::crypto::ring::default_provider() .install_default() .expect("Failed to install rustls crypto provider");