Skip to content

Latest commit

 

History

History
56 lines (38 loc) · 2.51 KB

alternative-approach.md

File metadata and controls

56 lines (38 loc) · 2.51 KB

Alternative Approach

Instead of relying on MultiSigner and complex conversions, we can directly generate the Substrate AccountId32 using the Blake2 hash of the ECDSA public key, which is a more straightforward and robust method.

use sp_core::{ecdsa, H160, crypto::Ss58Codec};
use sp_io::hashing::{blake2_256, keccak_256};
use secp256k1::{SecretKey, PublicKey, Secp256k1};
use sp_runtime::AccountId32;

fn main() {
    // Step 1: Generate a secp256k1 private key (or use an existing one)
    let secp = Secp256k1::new();
    let secret_key = SecretKey::from_slice(&[0x01; 32]).expect("32 bytes, within curve order"); // Example secret key

    // Step 2: Derive the Ethereum address

    let public_key = PublicKey::from_secret_key(&secp, &secret_key);
    let public_key_bytes = public_key.serialize_uncompressed();
    let eth_address = H160::from_slice(&keccak_256(&public_key_bytes[1..])[12..]);

    // Step 3: Convert the secp256k1 public key to a Substrate SS58 address
    let ecdsa_public_key = ecdsa::Public::from_slice(&public_key_bytes[1..]).expect("Valid public key slice");

    // Hash the ecdsa public key using Blake2 to get a 32-byte hash (H256)
    let hash = blake2_256(ecdsa_public_key.as_ref());

    // Create AccountId32 from the hash directly
    let account_id = AccountId32::new(hash);

    let ss58_address = account_id.to_ss58check();

    println!("Ethereum Address: 0x{:x}", eth_address);
    println!("Substrate SS58 Address: {}", ss58_address);
}

Explanation of the Simplified Approach:

  1. Key Generation:

    • The code generates a secp256k1 private key, which is compatible with both Ethereum and Substrate's ECDSA.
  2. Ethereum Address:

    • The public key is derived from the private key, and the Ethereum address is generated using the standard Keccak-256 hashing.
  3. Substrate SS58 Address:

    • The public key is hashed using Blake2-256 to obtain a 32-byte value.
    • This hash is then used to create an AccountId32 object directly.
    • Finally, the AccountId32 is converted into an SS58 address format.

Result:

  • This code will correctly generate both the Ethereum address and a Substrate-compatible SS58 address from the same private key, avoiding the complexity of dealing with MultiSigner or other traits that might lead to size mismatches or similar issues.

If you still encounter issues related to the environment or specific Substrate dependencies, ensuring that your project is up-to-date with compatible versions or using a clean build might help resolve those.