Substrate is an SDK and a framework that allows developers to create their own customized blockchain nodes. The blockchain node and network developed using Substrate are what we call a substrate-based blockchain.
Some of the popular Substrate-based blockchains are Polkadot, Moonbeam.
Ethereum-compatible blockchains implement the EVM (Ethereum Virtual Machine). EVM is a state machine that defines how the blockchain state should be changed from block to block. EVM is where the smart contracts are executed and the blockchain transactions are generated.
Smart contracts are programs stored on a blockchain that run when invoked through qualified external accounts. Solidity is the most popular language used to write smart contracts on Ethereum and Ethereum-compatible blockchains.
Some of the popular Ethereum-compatible blockchains are Binance Smart Chain, Avalanche, etc.
In an abstract sense, accounts in blockchain are actors or entities that are able to perform certain actions or transactions on the blockchain. Similar to how user accounts are identified by their username and password, blockchain accounts are represented by their public/private key pairs, where the public key is analogous to a username and the private key is like a password for that account. Different blockchain networks may generate these key pairs using different cryptographic algorithms and encoding formats.
Substrate supports different algorithms like ECDSA, Ed25519, and SR25519 to generate key pairs while EVM based blockchains only supports ECDSA.
A blockchain address can be thought like a bank account for crypto assets. Encoding raw bytes of the public key of an account results in a human-readable string, which is regarded as the address of that account. A blockchain address can be reverse mapped to the account it represents.
Ethereum uses Keccak256 hash function to generate 20-byte addresses of H160 format, from the public key of the account.
Substrate uses Blake256 hash function to generate addresses of format SS58.
This difference in address format poses a challenge to interoperability between EVM-based blockchains and Substrate-based blockchains.
H160 addresses are 42 hex-characters long. Example: 0x1016f75c54c607f082ae6b0881fac0abeda21781
First, look at the process of ethereum account generation: ecdsa-secp256k1 private key
-> ecdsa-secp256k1 public key
-> ethereum AccountId20
-> Hex format address
-
Step 1: private key example of 256bit private key generated by pseudo-random number (256bit, hexadecimal, 32 bytes)
18e14a7b6a307f426a94f8114701e7c8e774e7f9a47e2c2035db29a206321725
-
Step 2: public key
- 1.use the ECDSA-secp256k1, map the private key (32 bytes) to the public key (65 bytes) (prefix 04 + public_key_X + public_key_Y):
04 50863ad64a87ae8a2fe83c1af1a8403cb53f53e486d8511dad8a04887e5b2352 2cd470243453a299fa9e77237716103abc11a1df38855ed6f2ee187e9c582ba6
- 2.skip the first byte (prefix
04
), take the remaining 64-byte public key (uncompressed public key) to hash, and calculate the Keccak-256 hash value (32bytes) of the public key:
fc12ad814631ba689f7abe671016f75c54c607f082ae6b0881fac0abeda21781
- 3.take the last 20 bytes of the result of the previous step, which is the
AccountId20
stored on the chain:
1016f75c54c607f082ae6b0881fac0abeda21781
-
Step 3: the Hex format of
AccountId20
is the ethereum address:0x1016f75c54c607f082ae6b0881fac0abeda21781
On Substrate, each EVM account is backed by a native Substrate account.
SS58 is a simple address format designed for Substrate based chains. SS58 address is base58 encoding of an account’s 32 byte Substrate public key. By default, SS58 address start with number 1 but this can be customized by blockchain developers ( For eg: Polkadot addresses always start with the number 1). Eg: 5DAAnrj7VHTznn2AWBemMuyBwZWs6FNFjdyVXUeYum3PTXFy (public key: 0x306721211d5404bd9da88e0204360a1a9ab8b87c66c1bc2fcdd37f3c2222cc20)
Similarly, the process of substrate account generation: sr25519 private key
-> sr25519 public key
-> substrate AccountId32
-> Base58 format address
-
Step 1: private key
-
Step 2: public key: sr25519 private key generates a 32-byte public key, which is the
AccountId32
stored on the chain -
Step 3: the Base58 format of
AccountId32
, which is the substrate address.
The conversion between 32 byte public key and SS58 representation of an account can be done using the Subkey tool.
Substrate has an Ethereum compatibility layer, called Frontier, which brings EVM into Substrate-based blockchains. This allows users to use their metamask wallets for doing transactions and enables developers to use Solidity for writing smart contracts and using web3.js and Ethers.js libraries for interacting with the blockchain.
EVM in Substrate-based blockchains runs as a sandboxed runtime which makes it more secure as well. The transaction and state mutations from EVM are saved contiguously with the native Substrate state as well but it can’t directly modify the state of the native blockchain. However, the native blockchain can access EVM’s data. In short, EVM lives on the native blockchain as a subset of Ethereum compatible services. The extrinsic exposed by EVM can be called and browsed via any Ethereum compliant client.
Though it has these leverages, the challenge is interoperability between EVM accounts and native Substrate accounts due to the above-mentioned differences in addresses.
On PSC, the current implementation that allows us to overcome this obstacle is as follows:
The balance of all H160 accounts are tied with the native Substrate account which is referred to as Proxy Substrate account.
The generated proxy substrate account does not have a private key hence it is unable to sign any transaction but is able to receive and hold the balance. Any Substrate account can directly transfer funds to it. The funds associated with this proxy substrate account represents funds for its equivalent Ethereum (H160) address as well.
On PSC, we use the default and the most popular way is Hash Truncated Hash Mapping.
The steps for this mapping are as follows:
- Prefix ethereum address (buffer) with hex equivalent of string “evm:”.
- Hash (Blake-2b is widely used) the above result. This gives the Substrate public key.
- Base-58 encode the above public key to get the SS58
The interaction between H160 and SS58 address only supports SS58 -> SS58
, SS58 -> H160
, H160 -> H160
, does not support H160 -> SS58
on PSC.
How the funds in the proxy Substrate addressed are withdrawn on PSC ?
Although the conversion from H160 to SS58 is not supported (that is, the assets under the H160 address cannot be directly transferred to the SS58 address through metamask), but by establishing a two-way mapping between H160 and SS58, the original Substrate account can withdraw the funds in the proxy Substrate addressed to self.
pallet-assets-bridge not only established H160 and SS58 two-way address mapping, but also established substrate assets and erc20 tokens two-way address mapping, which is a necessary facility for OmniBTC to realize full-chain finance.
references