enable wallet only when it's needed

This commit is contained in:
Max Alekseenko 2019-11-19 14:04:14 +03:00
parent 795472ec57
commit 9559fe4273
6 changed files with 94 additions and 28 deletions

View File

@ -6,6 +6,7 @@ import { constants } from './utils/constants'
import { getNetworkBranch } from './utils/utils'
import { inject, observer } from 'mobx-react'
import messages from './utils/messages'
import { enableWallet } from './utils/getWeb3'
import './assets/stylesheets/index.css'
@ -42,28 +43,31 @@ class App extends Component {
const { commonStore, contractsStore } = this.props
if (!commonStore.loading) {
if (!contractsStore.injectedWeb3) {
commonStore.hideLoading()
swal({
title: 'Error',
html: messages.NO_METAMASK_MSG,
type: 'error'
enableWallet(contractsStore.setKeys)
.then(() => {
if (!contractsStore.injectedWeb3) {
swal({
title: 'Error',
html: messages.NO_METAMASK_MSG,
type: 'error'
})
} else if (!contractsStore.networkMatch) {
swal({
title: 'Warning!',
html: messages.networkMatchError(contractsStore.netId),
type: 'warning'
})
} else if (contractsStore.votingKey && !contractsStore.isValidVotingKey) {
swal({
title: 'Warning!',
html: messages.invalidVotingKeyMsg(contractsStore.votingKey),
type: 'warning'
})
}
})
} else if (!contractsStore.networkMatch) {
commonStore.hideLoading()
swal({
title: 'Warning!',
html: messages.networkMatchError(contractsStore.netId),
type: 'warning'
.catch(error => {
swal('Error', error.message, 'error')
})
} else if (contractsStore.votingKey && !contractsStore.isValidVotingKey) {
commonStore.hideLoading()
swal({
title: 'Warning!',
html: messages.invalidVotingKeyMsg(contractsStore.votingKey),
type: 'warning'
})
}
}
return <NewBallot networkBranch={this.getVotingNetworkBranch()} />
}

View File

@ -10,6 +10,7 @@ import { inject, observer } from 'mobx-react'
import messages from '../../utils/messages'
import { observable, action, computed } from 'mobx'
import { sendTransactionByVotingKey } from '../../utils/helpers'
import { enableWallet } from '../../utils/getWeb3'
const ACCEPT = 1
const REJECT = 2
@ -373,7 +374,16 @@ export class BallotCard extends React.Component {
swal('Warning!', messages.ballotIsNotActiveMsg(this.timeTo.displayValue), 'warning')
return
}
const { commonStore, contractsStore, id, votingType, ballotsStore, pos } = this.props
try {
await enableWallet(contractsStore.setKeys)
} catch (error) {
swal('Error', error.message, 'error')
return
}
const { push } = this.props.routing
if (!contractsStore.votingKey) {
swal('Warning!', messages.NO_METAMASK_MSG, 'warning')
@ -463,6 +473,25 @@ export class BallotCard extends React.Component {
const { votingState, contractsStore, commonStore, ballotsStore, votingType, id, pos } = this.props
const { push } = this.props.routing
const contract = this.getContract(contractsStore, votingType)
try {
await enableWallet(contractsStore.setKeys)
} catch (error) {
swal('Error', error.message, 'error')
return
}
if (!contractsStore.votingKey) {
swal('Warning!', messages.NO_METAMASK_MSG, 'warning')
return
} else if (!contractsStore.networkMatch) {
swal('Warning!', messages.networkMatchError(contractsStore.netId), 'warning')
return
} else if (!contractsStore.isValidVotingKey) {
swal('Warning!', messages.invalidVotingKeyMsg(contractsStore.votingKey), 'warning')
return
}
let canCancel = true
if (!this.timeToCancel.val) {
@ -515,6 +544,14 @@ export class BallotCard extends React.Component {
}
const { commonStore, contractsStore, id, votingType, ballotsStore, pos } = this.props
const { push } = this.props.routing
try {
await enableWallet(contractsStore.setKeys)
} catch (error) {
swal('Error', error.message, 'error')
return
}
if (!contractsStore.votingKey) {
swal('Warning!', messages.NO_METAMASK_MSG, 'warning')
return

View File

@ -17,6 +17,7 @@ import { getNetworkBranch } from '../../utils/utils'
import { inject, observer } from 'mobx-react'
import messages from '../../utils/messages'
import { sendTransactionByVotingKey } from '../../utils/helpers'
import { enableWallet } from '../../utils/getWeb3'
@inject('commonStore', 'ballotStore', 'validatorStore', 'contractsStore', 'routing', 'ballotsStore')
@observer
@ -253,6 +254,13 @@ export class NewBallot extends React.Component {
const { commonStore, contractsStore, ballotStore, ballotsStore } = this.props
const { push } = this.props.routing
try {
await enableWallet(contractsStore.setKeys)
} catch (error) {
swal('Error', error.message, 'error')
return
}
if (!contractsStore.votingKey) {
swal('Warning!', messages.NO_METAMASK_MSG, 'warning')
return

View File

@ -113,9 +113,7 @@ class AppMainRouter extends Component {
}
setKeys = async account => {
await contractsStore.setMiningKey(account)
await contractsStore.setVotingKey(account)
await contractsStore.setKeys(account)
console.log('votingKey', contractsStore.votingKey)
console.log('miningKey', contractsStore.miningKey)
}

View File

@ -214,6 +214,12 @@ class ContractsStore {
}
}
@action('Set keys')
setKeys = async account => {
this.setVotingKey(account)
await this.setMiningKey(account)
}
@action('Get all keys ballots')
getAllBallots = async () => {
let keysNextBallotId = 0,

View File

@ -1,9 +1,27 @@
import Web3 from 'web3'
import helpers from './helpers'
import { constants } from './constants'
import messages from './messages'
const defaultNetId = helpers.netIdByBranch(constants.CORE)
export async function enableWallet(setKeys) {
if (window.ethereum) {
try {
await window.ethereum.enable()
} catch (e) {
throw Error(messages.USER_DENIED_ACCOUNT_ACCESS)
}
const web3 = new Web3(window.ethereum)
const accounts = await web3.eth.getAccounts()
if (accounts[0]) {
await setKeys(accounts[0])
}
}
}
export default async function getWeb3(netId = defaultNetId, onAccountChange) {
let web3 = null
netId = Number(netId)
@ -12,11 +30,6 @@ export default async function getWeb3(netId = defaultNetId, onAccountChange) {
if (window.ethereum) {
web3 = new Web3(window.ethereum)
console.log('Injected web3 detected.')
try {
await window.ethereum.enable()
} catch (e) {
throw Error('You have denied access to your accounts')
}
window.ethereum.autoRefreshOnNetworkChange = true
} else if (window.web3) {
web3 = new Web3(window.web3.currentProvider)