3

How can I convert a Substrate address to an Etherum address?

There is an EVM to substrate address conversion that Edgeware provides: https://edgewa.re/keygen

1
  • 2
    Please provide more context and explain the question in more detail: Which address formats? Do you want to do it in code or just in general?. Also, it's best not to repeat the title of the question in the question body.
    – Dcompoze
    Apr 9 at 19:47

1 Answer 1

9

The Ethereum address correspond to the last 20 bytes of the keccak-256 hash of the (decompressed) secp256k1 public key.

For example, given the following key generated via subkey tool:

❯ subkey generate --scheme Ecdsa
Secret phrase `normal whale earth envelope wash bench drip latin please inform crisp congress` is account:
Secret seed:      0x6c51a7f71537a60e666288a292ceabf322202af75e39bc34fd10f576cee489a5
Public key (hex): 0x025f13d211338f64fcc941e80b9d71929c38a7147762541da4123de45cedb0b312
Account ID:       0x211e23eda1f535feb3abd690513a0c2731c4bb9940aad59f0fb3c8abe89308e1
SS58 Address:     5Cp8PdGYPGe2Vc5WLb4eUmbeuhxuDjPSQcpyWgVSjh97D9sR

Public key is given in compressed form:

Compressed:         0x025f13d211338f64fcc941e80b9d71929c38a7147762541da4123de45cedb0b312

Decompress it

Decompressed:       0x045f13d211338f64fcc941e80b9d71929c38a7147762541da4123de45cedb0b31290c16565dbc3c937fd1d9cf4ecc8ad489551a7d608ec5c649bec747183334754

Compute the Keccak-256 of the decompressed key (excluded the first byte (0x04))

Keccak-256:         0x09ed410a80b44d0a8c3e7e1fd924abdb51e253b19f69fd8114fe6d9a1794d2cf

The last 20 bytes is the Ethereum address

Eth Address:        0xd924abdb51e253b19f69fd8114fe6d9a1794d2cf

Said that, if instead you only have a Substrate AccountId (without the public key) it is not possible to convert it to the corresponding ETH address.

The reason is that the AccountId is the result of a hash applied to the PublicKey (+ some other data) and thus it is not possible to revert it in order to recover the public key (and apply the procedure I've described above).


Here is a code snip doing the conversion in the Substrate Beefy pallet:

https://github.com/paritytech/substrate/blob/87ebfdbcf8242c04856e3881a78ea0bc369d77a2/frame/beefy-mmr/src/lib.rs#L70-L89

4
  • What does it mean for a key to be compressed/decompressed? Nov 17 at 5:26
  • A public key is a point in a curve. In order to recover the point is sufficient to have only one between x and y (typically x is chosen) and the sign of the other. Nov 18 at 9:06
  • But what does it have to do with compression? Nov 18 at 13:05
  • Since you are actually representing the same information with half of the data this is in fact a compression. It is called "point compression" in the SEC1 standard. Please refer to paragraph 2.3.3 of secg.org/sec1-v2.pdf Nov 19 at 9:54

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