MetaMask Web3 Extension 마이그레이션 이슈

ONBUFF-DEV
3 min readMar 6, 2021

IPBLOCK은 METAMASK API를 이용하여 ONIT Token과 밀접하게 연동하고 있습니다. (해당 내용은 추후 별도의 스토리로 공유 드리겠습니다)

2021년 1월 MetaMask Web3 Extension API에 중대한 변경점이 있었고 ONBUFF또한 이에 대처하여 개발을 진행하였습니다.

주요 변경점은 아래와 같습니다.

Provider Migration Guide | MetaMask Docs 에서 발췌하였습니다

Migrating to the New Provider API

Handling eth_chainId Return Values

The eth_chainId RPC method now returns correctly formatted values, e.g. 0x1 and 0x2, instead of incorrectly formatted values, e.g. 0x01 and 0x02. MetaMask's implementation of eth_chainId used to return 0-padded values for the default Ethereum chains except Kovan. If you expect 0-padded chain ID values from eth_chainId, make sure to update your code to expect the correct format instead.

For more details on chain IDs and how to handle them, see the chainChanged event.

Handling the Removal of chainIdChanged

chainIdChanged is a typo of chainChanged. To migrate, simply listen for chainChanged instead:

// Instead of this:
ethereum.on('chainIdChanged', (chainId) => {
/* handle the chainId */
});

// Do this:
ethereum.on('chainChanged', (chainId) => {
/* handle the chainId */
});

Handling the Removal of isEnabled() and isApproved()

Before the new provider API shipped, we added the _metamask.isEnabled and _metamask.isApproved methods to enable web3 sites to check if they have access to the user's accounts. isEnabled and isApproved functioned identically, except that isApproved was async. These methods were arguably never that useful, and they became completely redundant with the introduction of MetaMask's permission system.

We recommend that you check for account access in the following ways:

You can call the wallet_getPermissions RPC method and check for the eth_accounts permission.

You can call the eth_accounts RPC method and the ethereum._metamask.isUnlocked() method.

MetaMask has to be unlocked before you can access the user’s accounts. If the array returned by eth_accounts is empty, check if MetaMask is locked using isUnlocked().

If MetaMask is unlocked and you still aren’t receiving any accounts, it’s time to request accounts using the eth_requestAccounts RPC method.

Handling the Removal of ethereum.publicConfigStore

How to handle this change depends on if and how you relied on the publicConfigStore. We have seen examples of listening for provider state changes the publicConfigStore data event, and accessing the publicConfigStore internal state directly.

We recommend that you search your code and its dependencies for references to publicConfigStore. If you find any references, you should understand what it's being used for, and migrate to one of the recommended provider APIs instead. If you don't find any references, you should not be affected by this change.

Although it is possible that your dependencies use the publicConfigStore, we have confirmed that the latest versions (as of January 2021) of the following common libraries were not affected by this change:

ethers

web3 (web3.js)

Handling the Removal of ethereum.autoRefreshOnNetworkChange

The ethereum.autoRefreshOnNetworkChange was a mutable boolean property used to control whether MetaMask reloaded the page on chain/network changes. However, it only caused the page to be reloaded if the a script access a property on window.web3. Therefore, this property was removed along with window.web3.

Despite this, we still recommend reloading the page on chain changes. Some convenience libraries, such as ethers (opens new window), will continue to reload the page by default. If you don’t use such a convenience library, you’ll have to reload the page manually. Please see the chainChanged event for details.

--

--

ONBUFF-DEV

ONBUFF_OFFICIAL IP Business Platform and IP Value-up Chain