Skip to content

Commit

Permalink
primitives: properly integrate with ss58 registry (#1516)
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianFranzen authored Feb 14, 2025
1 parent e77e6d3 commit 281c7c6
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 13 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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' }
1 change: 1 addition & 0 deletions chronicle/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
7 changes: 5 additions & 2 deletions node/src/chain_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions node/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ mod rpc;
use polkadot_sdk::*;

fn main() -> sc_cli::Result<()> {
time_primitives::init_ss58_version();
command::run()
}
24 changes: 19 additions & 5 deletions primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -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! {
Expand Down Expand Up @@ -199,6 +212,7 @@ mod tests {
"5DiPYChakvifNd4oC9s5ezGYn2WebiVdf8cUXRcG1XF9Jcfm",
"an7DqmJUeV3tjLf1NH7LRi4hu2YBftLs9K6LrAbf9UEvR8ePm",
"atVc4Jo8T5fsFLQu4FBdwsjj6PbLVQ5ATPHoFYLpR6xyd9N1V",
"azszGrHnFgHqmLAnkDFwU3QkHkeVJuoTmTVFev5ygjh2q9wqM",
];
for addr in addrs {
let account = AccountId::from_string(addr).unwrap();
Expand Down
4 changes: 2 additions & 2 deletions runtime/src/configs/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
// <https://github.com/paritytech/polkadot-sdk/issues/226>
Expand Down Expand Up @@ -92,7 +92,7 @@ impl frame_system::Config for Runtime {
type Version = Version;
type AccountData = pallet_balances::AccountData<Balance>;
type SystemWeightInfo = weights::frame_system::WeightInfo<Runtime>;
type SS58Prefix = ConstU16<{ SS58_PREFIX }>;
type SS58Prefix = ConstU16<{ SS58_ADDRESS_PREFIX }>;
type MaxConsumers = ConstU32<16>;
}

Expand Down
1 change: 0 additions & 1 deletion runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions tc-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down

0 comments on commit 281c7c6

Please sign in to comment.