Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update docs on EVM account <> Substrate account interactions #379

Closed
Tracked by #355
shekohex opened this issue Dec 28, 2023 · 1 comment · Fixed by tangle-network/docs#7
Closed
Tracked by #355

Update docs on EVM account <> Substrate account interactions #379

shekohex opened this issue Dec 28, 2023 · 1 comment · Fixed by tangle-network/docs#7

Comments

@shekohex
Copy link
Contributor

shekohex commented Dec 28, 2023

Overview

Converting between Substrate and EVM addresses can initially be a bit complicated, but once you understand the basic idea behind it, it becomes easy. Let's define the different scenarios we will be using:

  1. Alice only has an account on Tangle EVM using the Metamask wallet.
  2. Bob has an account on Tangle using the Polkadot.js wallet, and another account on Tangle EVM using the Metamask wallet.
  3. Charlie only has an account on Tangle using the Polkadot.js wallet.

Let's assign the following values:

  • Alice's account: 0xa5fAA47a324754354CB0A305941C8cCc6b5de296
  • Bob's accounts: 5FHneW46xGXgs5mUiveU4sbTyGBzmstUspZC92UhjJM694ty and 0x690B9A9E9aa1C9dB991C7721a92d351Db4FaC990
  • Charlie's account: 5FLSigC9HGRKVhB9FiEo4Y3koPsNmBmLJbpXg2mp1hXcS59Y

Convert Substrate Address to EVM

function substrateToEvm(input: string): string {
	// Decode the address to the 32 bytes.
	const accountId = decodeAddress(input);
	// Truncate it and take first 20 bytes
	const res = accountId.subarray(0, 20);
	// convert these bytes to hex.
	const output = u8aToHex(res);
	// The result is our EVM address.
	return output;
}

Convert EVM Address to Substrate

function evmToSubstrate(input: string): string {
	// Convert EVM address to bytes
	const addr = hexToU8a(input);
	// Add a "evm:" prefix
	const data = stringToU8a("evm:");
	// concat the address and the prefix and hash them together
	// using blake2
	const res = blake2AsU8a(u8aConcat(data, addr));
	// encode the output and using 42 address prefix (substrate default)
	const output = encodeAddress(res, 42);
	// The result is our Substrate address.
	return output;
}

Case 1: Sending from Substrate to EVM

Bob wants to send 100 TNT to Alice, but he does not have the 100 TNT on his EVM account in Metamask. Therefore, he decided to use his Tangle account in the polkadot.js wallet.

  1. Alice's address is 0xa5fAA47a324754354CB0A305941C8cCc6b5de296.
  2. Bob converts Alice's address to a substrate address using the evmToSubstrate function:
evmToSubstrate("0xa5fAA47a324754354CB0A305941C8cCc6b5de296");
// => 5C9ysBsWKpw3D8MFaEauFgdtMPqboS64YNYHyu1rCynLyKMZ
  1. Bob sends the 100 TNT to 5C9ysBsWKpw3D8MFaEauFgdtMPqboS64YNYHyu1rCynLyKMZ.
  2. Alice receives the 100 TNT in her Metamask wallet.

Case 2: Sending from EVM to Substrate

Alice wants to send 50 TNT to Charlie. However, Charlie only has a Substrate account that he controls in his Polkadot.js wallet.

  1. Charlie's address is 5FLSigC9HGRKVhB9FiEo4Y3koPsNmBmLJbpXg2mp1hXcS59Y.
  2. Alice converts Charlie's address to an EVM address using the substrateToEvm function.
substrateToEvm("5FLSigC9HGRKVhB9FiEo4Y3koPsNmBmLJbpXg2mp1hXcS59Y");
// => 0x90b5ab205c6974c9ea841be688864633dc9ca8a3
  1. Alice uses her Metamask and sends 50 TNT to 0x90b5ab205c6974c9ea841be688864633dc9ca8a3.
  2. Charlie's balance on Substrate remains the same!

The main reason here is that Charlie needs to withdraw the balance from his EVM account.

  1. Charlie goes to Polkadot.js (maybe we can make it in our dApp?) and calls evm.withdraw("0x90b5ab205c6974c9ea841be688864633dc9ca8a3", 50 TNT).
  2. Charlie sees that he has now received 50 TNT in his account.

TASK CHECKLIST

  • Add a page on how to transfer tokens from your substrate account to your EVM account
  • Add a page on how to transfer tokens from your EVM account to your substrate account
  • Extra: a web page that allow you to transfer between them without the need to do it by hand.
@shekohex shekohex mentioned this issue Dec 28, 2023
39 tasks
@shekohex shekohex self-assigned this Dec 28, 2023
@github-project-automation github-project-automation bot moved this to Not Started 🕧 in Webb Universe Dec 28, 2023
@shekohex shekohex moved this from Not Started 🕧 to Building 🏗️ in Webb Universe Jan 3, 2024
@shekohex
Copy link
Contributor Author

shekohex commented Jan 3, 2024

@shekohex you closed my issue (webb-tools/webb-docs#156) but as I see it, this information is totally different. I will merge these into a general account management section, if that's alright.

Yes! I closed the old one as it was in the wrong repo. Feel free to change this file or move it around.
This the new one: tangle-network/docs#7

@github-project-automation github-project-automation bot moved this from Building 🏗️ to Completed ✅ in Webb Universe Jan 5, 2024
@thomivy thomivy moved this from Completed ✅ to Building 🏗️ in Webb Universe Jan 5, 2024
@shekohex shekohex moved this from Building 🏗️ to Completed ✅ in Webb Universe Jan 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Archived in project
1 participant