I want to reason about SS58 addresses in any context with access to std, using Rust. What tools and techniques are needed to access the underlying keys?

[Related What is the SS58 Format?]

1 Answer 1


I made a working example of how to go about generating keys with sp-core::crypto primitives

Here is a snapshot of that source for quick reference:

//! Example generating SS58 Address format from Substrate keys.
//! See also https://polkadot.subscan.io/tools/ss58_transform for easy conversion of addresses from hex, H160, those listed on the registry: https://github.com/paritytech/ss58-registry/blob/main/ss58-registry.json

use sp_core::{
    crypto::{Ss58AddressFormatRegistry, Ss58Codec},
    Pair as _,

fn main() {
        "---------- Generating a secret key and use as seed for HDKD derived keys ----------\n"

    // Get a new pair & it's mnemonic phrase.
    let (pair, mnemonic, _) = Pair::generate_with_phrase(None);

    // Derive the public key.
    let pk = pair.public();

    println!("Mnemonic: {:?}", &mnemonic);
    println!("Pubkey (hex): {:?}", <HexDisplay>::from(&pk.0));

        "Default SS58 Address (AccountId) for Root Public Key: {:?}",

    println!("\n---------- Generating Polakdot & Kusama derived keys ----------\n");
    // Hard derive new key pair using `//polkadot`.
    let pair_polkadot = Pair::from_string(&format!("{}//polkadot", &mnemonic), None);
    let pk_polkadot = pair_polkadot.unwrap().public();

    // Hard derive new key pair using `//kusama`.
    let pair_kusama = Pair::from_string(&format!("{}//kusama", &mnemonic), None);
    let pk_kusama = pair_kusama.unwrap().public();

    // Access `AccountId`
        "SS58 Address (AccountId) for Polkadot Public Key: {:?}",
        "SS58 Address (AccountId) for Kusama Public Key: {:?}",

    println!("\n---------- Cross check with Subkey ----------");
    println!("Install: https://github.com/paritytech/substrate/tree/master/bin/utils/subkey\n");
    println!("$ subkey inspect \"<mnemonic phrase>\" ");
    println!("$ subkey inspect --network <polkadot, kusama, etc.> \"<mnemonic phrase>\"");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.