Merge pull request #220 from poanetwork/core-metamask-provider-updated
(Fix) Update getWeb3 script according to MetaMask breaking changes
This commit is contained in:
commit
8cafb1fd7f
|
@ -5,17 +5,38 @@ import messages from './messages'
|
|||
|
||||
const defaultNetId = helpers.netIdByBranch(constants.CORE)
|
||||
|
||||
async function getAccounts(web3) {
|
||||
let accounts
|
||||
if (window.ethereum) {
|
||||
accounts = await window.ethereum.request({ method: 'eth_accounts' })
|
||||
} else {
|
||||
accounts = await web3.eth.getAccounts()
|
||||
}
|
||||
return accounts
|
||||
}
|
||||
|
||||
async function getNetId(web3) {
|
||||
let netId
|
||||
if (window.ethereum) {
|
||||
const { chainId } = window.ethereum
|
||||
netId = web3.utils.isHex(chainId) ? web3.utils.hexToNumber(chainId) : chainId
|
||||
} else {
|
||||
netId = await web3.eth.net.getId()
|
||||
}
|
||||
return netId
|
||||
}
|
||||
|
||||
export async function enableWallet(updateKeys) {
|
||||
if (window.ethereum) {
|
||||
try {
|
||||
await window.ethereum.enable()
|
||||
await window.ethereum.request({ method: 'eth_requestAccounts' })
|
||||
} catch (e) {
|
||||
await updateKeys(null)
|
||||
throw Error(messages.USER_DENIED_ACCOUNT_ACCESS)
|
||||
}
|
||||
|
||||
const web3 = new Web3(window.ethereum)
|
||||
const accounts = await web3.eth.getAccounts()
|
||||
const accounts = await getAccounts(web3)
|
||||
|
||||
await updateKeys(accounts[0])
|
||||
}
|
||||
|
@ -28,7 +49,11 @@ export default async function getWeb3(netId, updateKeys) {
|
|||
if (window.ethereum) {
|
||||
web3 = new Web3(window.ethereum)
|
||||
console.log('Injected web3 detected.')
|
||||
window.ethereum.autoRefreshOnNetworkChange = true
|
||||
if (!window.ethereum.autoRefreshOnNetworkChange) {
|
||||
window.ethereum.on('chainChanged', () => {
|
||||
window.location.reload()
|
||||
})
|
||||
}
|
||||
} else if (window.web3) {
|
||||
web3 = new Web3(window.web3.currentProvider)
|
||||
console.log('Injected web3 detected.')
|
||||
|
@ -38,7 +63,7 @@ export default async function getWeb3(netId, updateKeys) {
|
|||
// Load for the first time in the current browser's session
|
||||
if (web3) {
|
||||
// MetaMask (or another plugin) is injected
|
||||
netId = await web3.eth.net.getId()
|
||||
netId = await getNetId(web3)
|
||||
if (!(netId in constants.NETWORKS)) {
|
||||
// If plugin's netId is unsupported, try to use
|
||||
// the previously chosen netId
|
||||
|
@ -67,25 +92,33 @@ export default async function getWeb3(netId, updateKeys) {
|
|||
let networkMatch = false
|
||||
|
||||
if (web3) {
|
||||
const accounts = await web3.eth.getAccounts()
|
||||
const accounts = await getAccounts(web3)
|
||||
defaultAccount = accounts[0] || null
|
||||
|
||||
if (!defaultAccount) {
|
||||
console.error('Unlock your wallet')
|
||||
console.log('Unlock your wallet')
|
||||
}
|
||||
|
||||
if (web3.currentProvider.publicConfigStore) {
|
||||
let currentAccount = defaultAccount ? defaultAccount.toLowerCase() : null
|
||||
web3.currentProvider.publicConfigStore.on('update', function(obj) {
|
||||
const account = obj.selectedAddress
|
||||
if (account !== currentAccount) {
|
||||
currentAccount = account
|
||||
updateKeys(account)
|
||||
}
|
||||
let currentAccount = defaultAccount ? defaultAccount.toLowerCase() : null
|
||||
function onUpdateAccount(account) {
|
||||
if (account && account !== currentAccount) {
|
||||
currentAccount = account
|
||||
updateKeys(account)
|
||||
}
|
||||
}
|
||||
if (window.ethereum) {
|
||||
window.ethereum.on('accountsChanged', accs => {
|
||||
const account = accs && accs.length > 0 ? accs[0].toLowerCase() : null
|
||||
onUpdateAccount(account)
|
||||
})
|
||||
} else if (web3.currentProvider.publicConfigStore) {
|
||||
web3.currentProvider.publicConfigStore.on('update', obj => {
|
||||
const account = obj.selectedAddress ? obj.selectedAddress.toLowerCase() : null
|
||||
onUpdateAccount(account)
|
||||
})
|
||||
}
|
||||
|
||||
const web3NetId = await web3.eth.net.getId()
|
||||
const web3NetId = await getNetId(web3)
|
||||
if (web3NetId === netId) {
|
||||
networkMatch = true
|
||||
} else {
|
||||
|
|
Loading…
Reference in New Issue