automata-network / automata

Automata Network is a modular attestation layer that extends machine trust to Ethereum with TEE Coprocessors.
https://ata.network
Apache License 2.0
94 stars 19 forks source link

Evm/Substrate address mapping #20

Closed Liao1 closed 3 years ago

Liao1 commented 3 years ago

We can use subkey to generate a sr25519 key pair, we will get the Secret Seed and Account ID, Account ID is the substrate address(let us call it as addressA), and you can genarate the private key using this secret seed. Besides, we can import the generated private key into Metamask and get a corresponding evm address(call it addressB), but actually these 2 addresses are address of 2 different account. We can do a experimentation:

  1. using the evm address we get above to get 10 testnet ATA from faucet
  2. import the substrate address into blockchain explorer
  3. get balance of the imported account in step#2(Developer -> Chain state -> system -> account) We will find tha balance of the imported account is not 10, actually is 0 if it is a new generated account

Then you can use code below to calculate a substrate address from the evm address, <evm address> is the address in Metamask without leading 0x

use blake2::VarBlake2b;
use blake2::digest::{Update, VariableOutput};
use hex;

fn main() {
    // create a Blake2b object
    let mut hasher = VarBlake2b::new(32).unwrap();

    // write input message
    let mut data = [0_u8; 24];
    data[0..4].copy_from_slice(b"evm:");
    data[4..24].copy_from_slice(&hex::decode("<evm address>").unwrap());

    hasher.update(&data);

    // read hash digest and consume hasher
    let _res = hasher.finalize_variable(|res| {
        println!("{:?}", hex::encode(&res[..]));
    });
}

you will get a string like 65f5fbd10250447019bb8b9e06f6918d033b2feb6478470137b1a552656e2911 by running code above, then come back to blockchain explorer, paste the string here(we will get a another address, call it addressC ) and call account function: image you will find that balance of this account is 10 ATA(or 10 BATA) The result verifies that actually addressA and addressB are 2 different account in our chain, addressB and addressC are same account in our chain. Currently, assume that someone have some ata in his evm address account, and he want to interact(send extrinsics) with our chain, it is impossible because he doesn't know the private key of corresponding substrate address. So maybe we need to change the mapping methods in evm pallet, take the previous example, we should mapping addressA to addressB

Liao1 commented 3 years ago

https://github.com/hicommonwealth/edgeware-documentation/blob/master/docs/edgeware-runtime/evm/evm-balances.md According to the document above, actaully we can do EVM - Substrate bidirectional fund transfer now

scenario 1: someone want to send transactions to contracts deployed in our testnet

  1. prepare a evm address, call it D(he has private key for this evm address)
  2. calculate the corresponding substrate address of D( hash(evm:)), call it A
  3. transfer to address A using the transfer extrinsic from a substrate account with tokens(call it B, he has private key for this substrate address)

scenario 2: the person don't want to do anything more with contracts, but there are still some token in evm address D, he wants to transfer these tokens back to B

  1. take the first 20 bytes of B, he get a new evm address, call it C
  2. transfer from D to C
  3. send a withdraw extrinsic to testnet, using substrate address B, and he get the remainning tokens

But the process above is a little complex, to simply it, we will add support 2 extrinsics: transferToSubstrate and transferToEVM, which support directly transfer between EVM and Substrate account