How to DeFi on Polkadot Series Part 1: Addresses

PoCoCo will be beginning a series of articles on how to DeFi in the Polkadot and Kusama ecosystems as we have identified that, because of the architectural differences between Ethereum and Polkadot and Kusama, this affects the composition and user experience of DeFi in these networks which confuses DeFi users familiar with Ethereum, L2’s and Metamask.

Polkadot and Kusama aren’t Layer 1’s like Ethereum, they are base layer/layer 0 relay chains or “meta protocols” where the parachains and other free-floating substrate chains are the layer 1’s. This is a different way to scale, from the ground up, instead of having a Layer 1 chain with multiple Layer 2’s, there is a layer 0 relay chain (Kusama and Polkadot) which the layer 1’s (parachains and free-floating substrate chains) connect to via becoming a parachain (or bridges) to share security and in the future, after the roll-out of XCMP, will be able to communicate cross-chain.

While the aim of both is decentralization, each network is following a different approach in terms of development. Ethereum offers smart contracts while Polkadot motivates the developers to build a complete blockchain of their own which includes smart contract logic and then joins the relay chain to benefit from the shared security Polkadot and/or Kusama offers and to communicate with other networks that are connected as well via XCMP.

Since the development goals are different it’s normal that the way addresses work is different as well. If you’re coming from Ethereum, or any other network using EVM, you might be used to the ERC20 standard- this means on Ethereum tokens are considered to be the same type, all of them can be transferred from one account to another, getting the current balance of an account and approval of the same token to a third party. The original idea behind the ERC20 standard was the implementation of a single API for any smart contract so they could be reused by other smart contracts without having compatibility issues. In the case of Polkadot the basic actions of an account such as balance transfer are considered extrisincs and are checked by the runtime of each chain.

Part 1: Addresses Before we start discussing addresses we need to know how they’re integrated with Polkadot, for that we need to discuss wallets first. The integration of a wallet with Polkadot (or Kusama) allows us to sign extrinsic calls with our accounts and interact with dApps.

The most well-known wallet for Polkadot and Kusama ecosystems is Polkadot.js. Polkadot.js is a proof-of-concept wallet designed specifically for Substrate based chains. It was originally developed by Parity but the source code is now public with an Apache 2 license and it is hosted on Github for anyone aiming to help with future development.

Polkadot.js is available in three formats: Desktop, Extension, and Browser. It can be downloaded here.

The browser wallet is the option with the features we will need to interact with the network. It makes some processes easier like voting on referendums, staking and using the development tools. To make it easier to use for developers Polkadot.js published an API that allows users to set up an interaction channel with the network nodes using javascript.

The Polkadot.js extension is a must-have since it allows us to log into dApps using our Substrate account. As of now, it is supported by Chrome and Firefox.

As the ecosystem grows, new wallets are announced so it is impossible to follow them all but below you can find some examples for different platforms.

Browser extension: Polkadot.js extension Windows, Mac and Linux: Polkadot.js desktop Fearless Wallet: Android / iPhone

Now that you already know how accounts are stored on Polkadot and the different options you have for wallets we will focus on how accounts are created and what is happening in the background when we’re generating one.

How is the address format composed in Substrate chains? User accounts in Polkadot and other substrate-based networks represent the public key, so whenever you switch networks you will see a different representation of the same public key.

What is SS58, and why do Substrate based chains use it?

SS58 is an address format designed for Substrate. The reason to have “58” in its name is that the basic idea of this address format is to have a base58 encoded value that can be used as an identifier in the network. SS58 is a modification of the Base-58-check format used originally by Bitcoin with some minor changes.

As part of those modifications, a prefix was added to the address type to identify which network an address belongs to.

Some examples of this prefix in the Polkadot ecosystem are:

  1. Polkadot addresses will always start with ‘1’.

  2. Kusama addresses will always begin with a capital letter, let it be ‘A, B, C, D,…’.

  3. Address of a generic substrate-based network will always start with ‘5’.

A checksum is a small block of data derived from another block of data to detect errors during the transmission of the message. We could say a checksum is a kind of validation done to an account ID to see if it is valid or not. Substrate offers different checksum strategies, you can choose by checksum by preimage (SS58/Account ID) or checksum by length (1 to 8 bytes).

The resulting addresses of the SS58 preimage format are trivially checked for validity even if they’re being used offline. If we choose to use the ‘Account ID’ preimage format we need to be aware that any change in the underlying account id must have the same hash of the original account id.

Substrate uses the Blake2–256 hash function for any checksum scenario, the variants depend only on what kind of preimage is used.

How are accounts generated? When you are creating a new account in Polkadot (or any substrate chain) you might see a screen similar to this:

In a basic scenario, the window will be composed of two elements, the account ID to be generated at the top and a mnemonic seed phrase.

What is a mnemonic seed? A mnemonic seed is a 12,15, 18, 21, or 24-word seed phrase generated with the BIP39 standard. It is recommended to write down your mnemonic seed on paper and store it in a secure place since any person with access to your seed phrase could make use of your funds.

if you click over ‘Advanced creation options’ new fields will be displayed below

Under the new options displayed you will find a dropdown where you can select what keyring crypto type you want to use.

There are no differences in terms of security between sr25519 and ed25519. The reason why Polkadot is using sr25519 as default is that it helps to implement complex protocols safer and it has high compatibility with many protocols used in other networks, like Bitcoin and Ethereum, so it simplifies the development of a multichain ecosystem.

Besides the keyring, we will be asked for a password during the generation of the account.

Your password is used to encode your private seed (an encoded version of your private key) so your account cannot be restored by someone who accidentally got an identical mnemonic seed generated in the same network.

Can I use the same address for multiple networks? This is one of the most asked questions in the Polkadot ecosystem which mainly comes from people who are used to chains where storing multiple assets in a single address is possible like Ethereum does with ERC-20 assets on Layer 2’s. In this aspect, Substrate-based networks work differently; even though it is theoretically possible to use the same address for different networks, not even the support team of Polkadot recommends it.

The reason why using the same address is not recommended is basically because of security. If you derive multiple accounts from one private key and your private key gets compromised, you will lose control of all your assets no matter the network. (You might be familiar with this security issue if you’re coming from Ethereum).

Below you can find some examples of how addresses change between networks using the same private key.

If you are a Polkadot.js user, you will see the following message displayed if you set any of your accounts with a prefix for ‘use on any chain.’

It is recommended to have different accounts with their own private keys for each network.

In the following link, you can find a list of the prefixes designated to each network as well as details like how many decimals they’re using for their assets and the symbol that identifies them. https://wiki.polkadot.network/docs/build-ss58-registry

If I sent my funds to an address with a prefix of a different network, can I get them back? No, you can’t. This is because of what was explained before, if you use a valid address from network ‘A’ that address could be a valid address for a different public key on network ‘B’ for example. Hence once the hashing function of the network ‘B’ hashes your account you will get a different account so you won’t be able to claim your funds as your address won’t match. Be careful whenever you’re sending assets, always be sure the account was switched to the correct network before you copy it and if sending large amounts make sure to do a test with a small sum first.

How is an account recalculated for multiple networks without having to switch it manually? When we connect to a site with support for Polkadot.js, behind the scenes a re-encoding of our public key is happening, this is done using a combination of the checksum strategies substrate offers and our public key.

Some examples would be:

Public key: 16,178,46,190,137,179,33,55,11,238,141,57,213,197,212,17,218,241,232,252,145,201,209,83,64,68,89,15,31,150,110,188

console.log(keyring.encodeAddress(pair.publicKey, 42));

Output: 5CSbZ7wG456oty4WoiX6a1J88VUbrCXLhrKVJ9q95BsYH4TZ

console.log(keyring.encodeAddress(pair.publicKey, 2));

Output: CxDDSH8gS7jecsxaRL9Txf8H5kqesLXAEAEgp76Yz632J9M

console.log(keyring.encodeAddress(pair.publicKey, 0)); Output: 1NthTCKurNHLW52mMa6iA8Gz7UFYW5UnM3yTSpVdGu4Th7h

Thanks to this feature of Substrate users don’t need to switch between networks as you would do on Ethereum, on Polkadot every time you connect to a different network your address will be recalculated automatically.

Existential deposit On Polkadot or any substrate-based network, an account is only active if it holds a minimum amount called an ‘existential deposit’. When the funds in an account are less than the amount needed for a transaction the account is considered a dust account; when there are too many dust accounts in the network it becomes hard for the nodes to sync with the network. To solve this for Substrate, this type of account is ‘deactivated’. If any of your accounts are deactivated the network will ‘destroy’ all the funds they contain but you can ‘reactivate’ it by doing a transfer of assets bigger than the existential deposit (destroyed funds won’t come back).

One way to prevent your account from going under the ‘existential deposit’ line is by activating the following toggle while transferring assets with Polkadot.js

This will force the account to remain with at least the existential deposit, even if this causes the transaction to fail.

How to create a Kusama account. There are several ways to create an account, depending on how technical you are you can choose one or the other. To make this tutorial easier for newcomers, this tutorial will use the Polkadot.js extension as it is the most recommended tool for new users. Prerequisites:

Install PolkadotJS extension (Currently available for Chrome and Firefox).

1.After the installation of the extension is complete, you will have to click the Polkadot.js extension logo next to your search tab. Once it opens, your wallet will be empty, so you will see a screen similar to the one displayed in the above image.

2.In the next window you will get a preview of your account id (this account id might change depending on the configurations set in the next steps). In this step, you will get your mnemonic seed phrase. It consists of 12 words that must be inserted in the same order if we want to restore our network.

Our mnemonic seed phrase is not needed for our day to day activities, you don’t need to insert them to interact with the network, but, if you need to restore your account on a different device or you simply want to restore your account in a new wallet you will need them.

Take the time you need to copy the words and do it in the same order they were displayed, do this at least 2 times but preferably 3, once you finish, click the checkbox and click ‘Next step’.

It is considered good practice to write down your mnemonic seed and store them offline. Remember any person with access to your mnemonic seed and your password will gain access to your funds so be sure you don’t disclose it to anyone.

3.The third step is critical, it will calculate the final account id based on the network you select. The account generated by default starts with ‘5’ as this is the prefix used for any custom Substrate based chain, for purposes of this tutorial select ‘Kusama Relay Chain’ and click ‘Add the account with the generated seed’.

4.As the last step you will be asked three things: the network where you will be using this address (remember an address is calculated differently for each network), a name for the account, and a password.

The name required in this step is for personal use only and it is not stored in the blockchain, the next time you will recover your account you will be able to use a different name without modifying your account id.

Be careful while typing your password, the password you use during the generation of the account is used to restore your account with your seed words or JSON. If you happen to lose or forget your password you won’t be able to restore your account even if your seed words or JSON file are valid.

Something worth mentioning here is that as a consequence of the change of network we did in the previous step, our account id has changed to the format defined on Kusama (In Kusama account IDs must start with a capital letter).

Reminder: Keep in mind that the only way to restore your account is through your seed words or with the generated JSON file and the password you used while creating your account.

Once you have set up your name and your password the account will be generated on-chain and you will see your account listed in the extension. If you pay attention to your account, you will see that at the right top it is tagged with the network for which this account ID works.

If you want to verify your account works go to polkadot.js.org. Before connecting you will be requested to grant permissions to the site to connect to your wallet (polkadot.js extension), allow it.

and once the UI has loaded, switch the network to Kusama in the side menu as shown below.

If you followed the tutorial you should see your account displayed on the ‘accounts’ page, it will be marked as ‘injected’ since this account was not directly generated in the polkadot.js web but it was ‘injected’ from the account stored in your polkadot.js extension.

I hope this small tutorial was helpful enough to explain the concepts discussed along with the article. If you have any questions don’t hesitate to send us a tweet!

Sources Polkadot keys — https://wiki.polkadot.network/docs/learn-keys Keyring — https://polkadot.js.org/docs/api/start/keyring/ SS58 — https://polkadot.js.org/docs/keyring/start/ss58/ Wallets — https://wiki.polkadot.network/docs/build-wallets Polkadot.Js

0 comments