From 9559fe4273809533179e6dde91420e38612e3b27 Mon Sep 17 00:00:00 2001 From: Max Alekseenko Date: Tue, 19 Nov 2019 14:04:14 +0300 Subject: [PATCH] enable wallet only when it's needed --- src/App.js | 44 ++++++++++++++++-------------- src/components/BallotCard/index.js | 37 +++++++++++++++++++++++++ src/components/NewBallot/index.js | 8 ++++++ src/index.js | 4 +-- src/stores/ContractsStore.js | 6 ++++ src/utils/getWeb3.js | 23 ++++++++++++---- 6 files changed, 94 insertions(+), 28 deletions(-) diff --git a/src/App.js b/src/App.js index fc8024a..84dcc16 100644 --- a/src/App.js +++ b/src/App.js @@ -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 } diff --git a/src/components/BallotCard/index.js b/src/components/BallotCard/index.js index 5a219ac..c6fbbc1 100644 --- a/src/components/BallotCard/index.js +++ b/src/components/BallotCard/index.js @@ -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 diff --git a/src/components/NewBallot/index.js b/src/components/NewBallot/index.js index 0a189ab..93e987d 100644 --- a/src/components/NewBallot/index.js +++ b/src/components/NewBallot/index.js @@ -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 diff --git a/src/index.js b/src/index.js index abc3853..0513867 100644 --- a/src/index.js +++ b/src/index.js @@ -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) } diff --git a/src/stores/ContractsStore.js b/src/stores/ContractsStore.js index 1ec1914..7a4100b 100644 --- a/src/stores/ContractsStore.js +++ b/src/stores/ContractsStore.js @@ -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, diff --git a/src/utils/getWeb3.js b/src/utils/getWeb3.js index d69a135..888a172 100644 --- a/src/utils/getWeb3.js +++ b/src/utils/getWeb3.js @@ -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)