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

Alameda Engineer SBF stole my life savings

As an engineer at Alameda Research, my entire life savings were stolen by my former boss, Sam Bankman-Fried (known to...

Blockchain

Being shot, being controlled, and being exiled globally, is the founder of the exchange the most dangerous occupation in the currency circle?

A week ago, Tobias Niemiro, the co-owner of Bitmarket, Poland's second-largest cryptocurrency exchange, was shot...

Blockchain

After FCoin's "incident", key figures from the team responded!

On the evening of February 12, an announcement on the FCoin Exchange regarding "the latest progress of system ma...

Blockchain

Full text of South Korea's first independent "Encryption Act": Insider trading carries a maximum sentence of life imprisonment.

On June 30, 2023, the South Korean National Assembly's Political Affairs Committee passed the country's first legisla...

Opinion

Overview of International Cryptocurrency Regulatory Agencies

We have studied 45 countries, including G20 member countries, as well as countries with the highest adoption rate of ...

Blockchain

PAData: FCoin potential victims or more than 2000 people, per capita loss or more than 25 BTC

Analyst | Carol Editor | Bi Tongtong Production | PANews Data Partner | Chain.info On February 17, the FCoin, which w...