Technical Tutorials | ENS: Add Multicurrency Support to Your Wallet

After the multi-currency support feature was announced on Devcon5, we soon implemented this feature on the ENS manager .

Many wallets also began to support this feature shortly.

Developers can read the latest implementation details by reading EIP , documentation, and our JavaScript address encoding library .

In this article, I will introduce our experience of integrating multi-currency support features into our own app, and provide other wallet developers with ideas and ideas.

Parser

Since this feature is brand new, many Ethereum libraries are not yet supported (currently ethers.js, go-ens, and ethreal support).

In order to interact directly with the parser contract, you download our contract via npm and import the following abi.

  import {abi} from
  '@ ensdomains / resolver / build / contracts / Resolver.json' 

First let's look at the difference between setting / getting an Ethereum address and other digital currency addresses.

  ## Get and set the Ethereum address function addr (bytes32 node);
 function setAddr (bytes32 node, address addr);  
 ## Get and set multi-currency address function addr (bytes32 node, uint coinType); 
 function setAddr (bytes32 node, uint coinType, bytes calldata a); 

The biggest difference is that both the getter and setter functions now have a coinType parameter. Please note that the parameter of the setAddr function is now bytes instead of address.

Address encoder

address-encoder is a js codec library that processes records stored in the ENS parser. It has two functions, including formatsByName and formatsByCoinType.

  import {formatsByName, formatsByCoinType} from '@ ensdomains / address-encoder';  
 formatsByName ['BTC'] 
 {coinType: 0, decoder: [Function], encoder: [Function], name: 'BTC'} 
 formatsByCoinType ['0'] 
 {coinType: 0, decoder: [Function], encoder: [Function], name: 'BTC'} 
 From now on, we only use formatsByName. 

Below we only use the formatsByName function.

Get a list of supported currencies

Maybe you want to access your list of supported currencies. If you want to directly access the tokens that we have implemented codecs, you should construct this currency list with the following code.

export const COIN_LIST = Object.keys (formatsByName)

Get address

Here is a super simplified version of our implementation of the getAddr function in our React component.

With the first line of code, we get the coinType and encoder functions. Next, the coinType and namehash parameters will be used to obtain the specific address of a certain token from the Resolver contract.

The fourth line of code is to check whether the address is an empty address before passing it to the decoder, and if so, return directly. Because if an empty string is passed directly to the encoder, it is possible to throw certain token type errors.

The fifth line of code passes the binary representation of the address to the encoding function and displays the address as text.

Set address

Here is a simplified version of our setAddr function.

As we did in the getAddr function, when the address is empty, we return this result in advance without passing it to the decoder. The fifth line simply uses the binary representation of the empty string.

verification

It is critical to verify that the address conforms to the format of the corresponding currency.

If you pass an invalid text to the address-encoder library, you will throw an error.

In this example, we caught this error and showed it.

BCH Tips

Normally, if you decode and encode the same text, you will still get the same text. However, Bitcoin Cash's performance is not the same (for specific technical reasons, you can refer to the "CashAddr" in this EIP ). Bitcoin Cash will add a "bitcoincash" prefix to the returned text after encoding. The following example shows the raw text, the hexadecimal representation, and the canonical representation of the BCH encoding at query time (you can find it in the test case ).

to sum up

In this article, we walked through the process of implementing multi-currency support and introduced some details that need attention. In fact, this is very close to the original operation of setting / getting the address, except that one more coinType parameter is passed. Also, be extra careful with issues involving validation and empty strings.

As more and more libraries support multi-currency features, wallet developers will easily add this feature in the future.

(Finish)

Original link: https://medium.com/the-ethereum-name-service/how-to-integrate-ens-multi-coin-support-into-your-wallet-for-developers-8d3a8a37d1eb Author: makoto_inoue translation & proofreading : Anzai & Ajian

We will continue to update Blocking; if you have any questions or suggestions, please contact us!

Share:

Was this article helpful?

93 out of 132 found this helpful

Discover more

Blockchain

Hong Kong's anti-acquisition new deal will take effect, and the "coin stock" of the fire currency will be renamed as a risk

Tongcheng Holdings Co., Ltd. (HK1611) issued an announcement proposing to change the company name to Firecoin Technol...

Blockchain

How does the derivatives market fight on the platform of the 5-year-old exchange?

Derivatives trading has become a battleground for the military, and OKex, Huobi, Gate, Fcoin, which are well-known ex...

Blockchain

Long Push Receiving 1 million ARB airdrop, Summary and Reflections on 2 Years in the Circle

Note This article is from @0xfarmer_ on Twitter, summarized by MarsBit as follows Time flies. I have finished my subs...

Blockchain

The exchange is frequently stolen, and where is the security of digital currency going?

In the food chain of digital currency, the exchange has always stood at the top. But if you want to wear a crown, you...

Blockchain

Featured | Messari Founder: Recent Frustrations and Hopes in the Industry; Innovations in the Bitcoin Technology Stack

Today's content includes: 1. Founder of Messari: the recent frustration and hope of the industry; 2.Innovation i...

Blockchain

Ieo, which used to be ten times easier, is there anyone else involved?

At the beginning of the new year, the coin opened the curtain of the IEO "issuing the first exchange" stage...