Skip to content
This repository has been archived by the owner on Jun 10, 2021. It is now read-only.

[WIP] Hotfix/authorize dapp prompt #136

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 44 additions & 30 deletions src/components/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ class App extends Component {
network = 'private';
}
if (!this.state.network.stopIntervals // To avoid race condition
&& this.state.network.network !== network) {
&& this.state.network.network !== network) {
this.initNetwork(network);
}
}, () => {
Expand Down Expand Up @@ -158,7 +158,8 @@ class App extends Component {
})
}
});
}, () => {});
}, () => {
});
}

componentDidMount = () => {
Expand Down Expand Up @@ -1178,10 +1179,23 @@ class App extends Component {
network.stopIntervals = false;
return {network};
}, async () => {
await Blockchain.setWebClientProvider();
this.checkNetwork();
this.checkAccountsInterval = setInterval(this.checkAccounts, 1000);
this.checkNetworkInterval = setInterval(this.checkNetwork, 3000);
const isSet = await Blockchain.setWebClientProvider().catch(_ => {
return false;
});

if (isSet) {
this.checkNetwork();
this.checkAccountsInterval = setInterval(this.checkAccounts, 1000);
this.checkNetworkInterval = setInterval(this.checkNetwork, 3000);
} else {
this.setState(prevState => {
const network = {...prevState.network};
network.loadingAddress = false;
network.loadingFirstAddress = false;
network.stopIntervals = true;
return {network};
});
}
});
}

Expand Down Expand Up @@ -1268,30 +1282,30 @@ class App extends Component {

renderWidget = () => {
return <Widget isConnected={this.state.network.isConnected}
section={this.state.section}
network={this.state.network.network}
loadingAddress={this.state.network.loadingAddress}
loadingFirstAddress={this.state.network.loadingFirstAddress}
account={this.state.network.defaultAccount}
proxy={this.state.proxy}
trade={this.state.trade}
balances={this.state.balances}
showTxMessage={this.state.showTxMessage}
transactions={this.state.transactions}
setMainState={this.setMainState}
fasterGasPrice={this.fasterGasPrice}
doTrade={this.doTrade}
reset={this.reset}
calculateBuyAmount={this.calculateBuyAmount}
calculatePayAmount={this.calculatePayAmount}
cleanInputs={this.cleanInputs}
setWeb3WebClient={this.setWeb3WebClient}
hw={this.state.hw}
showHW={this.showHW}
showClientChoice={this.showClientChoice}
loadHWAddresses={this.loadHWAddresses}
selectHWAddress={this.selectHWAddress}
importAddress={this.importAddress}/>
section={this.state.section}
network={this.state.network.network}
loadingAddress={this.state.network.loadingAddress}
loadingFirstAddress={this.state.network.loadingFirstAddress}
account={this.state.network.defaultAccount}
proxy={this.state.proxy}
trade={this.state.trade}
balances={this.state.balances}
showTxMessage={this.state.showTxMessage}
transactions={this.state.transactions}
setMainState={this.setMainState}
fasterGasPrice={this.fasterGasPrice}
doTrade={this.doTrade}
reset={this.reset}
calculateBuyAmount={this.calculateBuyAmount}
calculatePayAmount={this.calculatePayAmount}
cleanInputs={this.cleanInputs}
setWeb3WebClient={this.setWeb3WebClient}
hw={this.state.hw}
showHW={this.showHW}
showClientChoice={this.showClientChoice}
loadHWAddresses={this.loadHWAddresses}
selectHWAddress={this.selectHWAddress}
importAddress={this.importAddress}/>
}

render = () => {
Expand Down
64 changes: 41 additions & 23 deletions src/web3.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,32 @@ import TrezorSubProvider from './vendor/trezor-subprovider';

const settings = require('./settings');

export const getCurrentProviderName = () => {
if (!window.web3 || typeof window.web3.currentProvider === 'undefined')
return '';
export const getCurrentProviderName = ( provider = window.web3.currentProvider ) => {
if (provider.isMetaMask)
return "metamask";

if (window.web3.currentProvider.isMetaMask)
return 'metamask';
if (provider.isTrust)
return "trust";

if (window.web3.currentProvider.isTrust)
return 'trust';
if (window.web3.currentProvider.isStatus)
return "status";

if (typeof window.SOFA !== 'undefined')
return 'coinbase';

if (typeof window.__CIPHER__ !== 'undefined')
return 'cipher';

if (window.web3.currentProvider.constructor.name === 'EthereumProvider')
if (provider.constructor.name === 'EthereumProvider')
return 'mist';

if (window.web3.currentProvider.constructor.name === 'Web3FrameProvider')
if (provider.constructor.name === 'Web3FrameProvider')
return 'parity';

if (window.web3.currentProvider.host && window.web3.currentProvider.host.indexOf('infura') !== -1)
if (provider.host && provider.host.indexOf('infura') !== -1)
return 'infura';

if (window.web3.currentProvider.host && window.web3.currentProvider.host.indexOf('localhost') !== -1)
if (provider.host && provider.host.indexOf('localhost') !== -1)
return 'localhost';

return 'other';
Expand All @@ -59,35 +59,53 @@ class Web3Extended extends Web3 {
this.currentProvider.addProvider(hwWalletSubProvider);
this.currentProvider.addProvider(new RpcSource({rpcUrl: settings.chain[network].nodeURL}));
this.currentProvider.start();
this.useLogs = false;
resolve(true);
} catch(e) {
} catch (e) {
reject(e);
}
});
}
};

bindProvider = provider => {
this.setProvider(provider);
this.currentProvider.name = getCurrentProviderName(provider);
};

setWebClientProvider = () => {
this.stop();
return new Promise(async (resolve, reject) => {
try {
if (window.web3) {
this.setProvider(window.web3.currentProvider);
} else {
alert('error');
// Checking if the the provider is compliant with the new EIP1102 Standard.
if (window.ethereum) { //following the new EIP1102 standard
window.ethereum.enable().then(
() => {
this.bindProvider(window.ethereum);
resolve(true);
},
() => {
reject();
});

return;
}
this.useLogs = true;
this.currentProvider.name = getCurrentProviderName();
resolve(true);
} catch(e) {

if (window.web3) { // This is the case for Provider Injectors which don't follow EIP1102 ( parity-extension ? )
this.bindProvider(window.web3.currentProvider);
resolve(true);

return;
}

reject();
} catch (e) {
reject(e);
}
});
}
}

const web3 = new Web3Extended();
web3.BigNumber.config({EXPONENTIAL_AT:[-18,21]});
web3.BigNumber.config({EXPONENTIAL_AT: [-18, 21]});
window.web3Provider = web3;

export default web3;