0

I have my hex public key, how can I convert it to an SS58 Address?

I have found the snippet to do it using PolkadotJS here: How to convert Public key (hex) to SS58 Address in JavaScript / Polkadot-JS? simply using const { encodeAddress } = require("@polkadot/keyring");

Is there a similar way to do that in Rust?

2

3 Answers 3

1

Yes, there is a similar way to do that in Rust. You can use the sp_core crate. Here is an example of how to convert a hex public key to an SS58 address in Rust:

you can use the sp_core crate, which is part of the Substrate framework, to perform this conversion in Rust. Here's an example of how to achieve it:

extern crate sp_core;

use sp_core::crypto::Ss58Codec;

fn main() {
    let hex_public_key = "46ebddef8cd9bb167dc30878d7113b7e168e6f0646beffd77d69d39bad76b47a";
    let public_key = hex::decode(hex_public_key).expect("Failed to decode hex public key");
    let ss58_address = sp_core::crypto::AccountId32::from(public_key.as_slice()).to_ss58check();

    println!("SS58 address: {}", ss58_address);
}

Ensure you have the sp-core crate added as a dependency in your Cargo.toml file:

[dependencies]
sp-core = "2.0"

In this example, we decode the hex public key using hex::decode assuming you have the hex crate as a dependency as well. Then, we create an AccountId32 instance using sp_core::crypto::AccountId32::from by passing the decoded public key as a slice.

Finally, we use the to_ss58check method on the AccountId32 instance to obtain the SS58 address representation.

3
  • Following this instructions I get an error importing {encode, AddressFormat}
    – Alex Bean
    Jul 6 at 9:49
  • 1
    can you please follow the above , sorry for my previos response Jul 6 at 9:57
  • Thanks! that snippet still gives me an error: the trait From<&[u8]> is not implemented for sp_core::crypto::AccountId32
    – Alex Bean
    Jul 6 at 10:21
0

Not sure if there is an easy way to do it like it is with PolkadotJS, but I check the code of the subkey tool and can share how I am getting a SS58 Address from a hex public key.

use sp_core::{crypto::{Ss58AddressFormatRegistry, Ss58Codec}, sr25519::Pair};
use sp_runtime::{MultiSigner};

let ss58_address = hex_public_key_to_ss58_address::<Pair>(public_str)?;

fn hex_public_key_to_ss58_address<Pair>(
    public_str: String,
) -> Result<String, Box<dyn std::error::Error>>
where
    Pair: sp_core::Pair,
    Pair::Public: Into<MultiSigner>,
{
    let public = array_bytes::hex2bytes(&public_str).expect("Failed to convert hex to bytes");
    let public_key = Pair::Public::try_from(&public)
        .map_err(|_| "Failed to construct public key from given hex")?;
    let ss58_address = public_key.to_ss58check_with_version(Ss58AddressFormatRegistry::SubstrateAccount.into()):

    Ok(ss58_address)
}
-1

You can use this tool to get ss58 from your Public key(Hex) you may also check this

1
  • 1
    Thanks, but I am not looking for a tool, I am looking for a code snippet
    – Alex Bean
    Jul 6 at 10:16

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.