Merge pull request #94 from poanetwork/core-metamask-provider-updated
(Fix) Update getWeb3 script according to MetaMask breaking changes
This commit is contained in:
commit
f6518f660c
|
@ -28,6 +28,7 @@ function generateElement(msg) {
|
||||||
class App extends Component {
|
class App extends Component {
|
||||||
constructor(props) {
|
constructor(props) {
|
||||||
super(props)
|
super(props)
|
||||||
|
this.onAccountChanged = this.onAccountChanged.bind(this)
|
||||||
this.onClick = this.onClick.bind(this)
|
this.onClick = this.onClick.bind(this)
|
||||||
this.saveFile = blob => {
|
this.saveFile = blob => {
|
||||||
FileSaver.saveAs(blob, `poa_network_validator_keys.zip`)
|
FileSaver.saveAs(blob, `poa_network_validator_keys.zip`)
|
||||||
|
@ -40,7 +41,7 @@ class App extends Component {
|
||||||
}
|
}
|
||||||
this.keysManager = null
|
this.keysManager = null
|
||||||
|
|
||||||
getWeb3()
|
getWeb3(this.onAccountChanged)
|
||||||
.then(async web3Config => {
|
.then(async web3Config => {
|
||||||
return networkAddresses(web3Config)
|
return networkAddresses(web3Config)
|
||||||
})
|
})
|
||||||
|
@ -88,6 +89,12 @@ class App extends Component {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
onAccountChanged(newAccount) {
|
||||||
|
let web3Config = this.state.web3Config
|
||||||
|
web3Config.defaultAccount = newAccount
|
||||||
|
this.setState({ web3Config })
|
||||||
|
}
|
||||||
|
|
||||||
async generateKeys(cb) {
|
async generateKeys(cb) {
|
||||||
const mining = await addressGenerator()
|
const mining = await addressGenerator()
|
||||||
const voting = await addressGenerator()
|
const voting = await addressGenerator()
|
||||||
|
|
|
@ -14,7 +14,29 @@ function generateElement(msg) {
|
||||||
</div>`
|
</div>`
|
||||||
return errorNode
|
return errorNode
|
||||||
}
|
}
|
||||||
let getWeb3 = () => {
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
let getWeb3 = (onAccountChanged) => {
|
||||||
return new Promise(function(resolve, reject) {
|
return new Promise(function(resolve, reject) {
|
||||||
// Wait for loading completion to avoid race conditions with web3 injection timing.
|
// Wait for loading completion to avoid race conditions with web3 injection timing.
|
||||||
window.addEventListener('load', async function() {
|
window.addEventListener('load', async function() {
|
||||||
|
@ -24,8 +46,13 @@ let getWeb3 = () => {
|
||||||
if (window.ethereum) {
|
if (window.ethereum) {
|
||||||
web3 = new Web3(window.ethereum)
|
web3 = new Web3(window.ethereum)
|
||||||
console.log('Injected web3 detected.')
|
console.log('Injected web3 detected.')
|
||||||
|
if (!window.ethereum.autoRefreshOnNetworkChange) {
|
||||||
|
window.ethereum.on('chainChanged', () => {
|
||||||
|
window.location.reload()
|
||||||
|
})
|
||||||
|
}
|
||||||
try {
|
try {
|
||||||
await window.ethereum.enable()
|
await window.ethereum.request({ method: 'eth_requestAccounts' })
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
reject({
|
reject({
|
||||||
msg: errorMsgDeniedAccess,
|
msg: errorMsgDeniedAccess,
|
||||||
|
@ -45,7 +72,7 @@ let getWeb3 = () => {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
const netId = await web3.eth.net.getId()
|
const netId = await getNetId(web3)
|
||||||
console.log('netId', netId)
|
console.log('netId', netId)
|
||||||
|
|
||||||
let netIdName
|
let netIdName
|
||||||
|
@ -70,8 +97,28 @@ let getWeb3 = () => {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
const accounts = await web3.eth.getAccounts()
|
const accounts = await getAccounts(web3)
|
||||||
const defaultAccount = accounts[0] || null
|
const defaultAccount = accounts[0] || null
|
||||||
|
|
||||||
|
let currentAccount = defaultAccount ? defaultAccount.toLowerCase() : null
|
||||||
|
function onUpdateAccount(account) {
|
||||||
|
if (account && account !== currentAccount) {
|
||||||
|
currentAccount = account
|
||||||
|
onAccountChanged(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)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
if (defaultAccount === null) {
|
if (defaultAccount === null) {
|
||||||
reject({
|
reject({
|
||||||
msg: errorMsgNoMetamaskAccount,
|
msg: errorMsgNoMetamaskAccount,
|
||||||
|
|
Loading…
Reference in New Issue