Web3 Improvements (#740)

* increase timeout for web3 auto-login; don't attempt login when web3 is unavailable

* only auto-login when metamask available

* combine multiple imports
This commit is contained in:
Daniel Ternyak 2018-01-07 19:21:11 -06:00 committed by GitHub
parent 897fc3f9b3
commit 92d8ecf353
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 7 deletions

View File

@ -31,7 +31,7 @@ import {
WalletButton
} from './components';
import { AppState } from 'reducers';
import { knowledgeBaseURL } from 'config/data';
import { knowledgeBaseURL, isWeb3NodeAvailable } from 'config/data';
import { IWallet } from 'libs/wallet';
import DigitalBitboxIcon from 'assets/images/wallets/digital-bitbox.svg';
import LedgerIcon from 'assets/images/wallets/ledger.svg';
@ -39,7 +39,6 @@ import MetamaskIcon from 'assets/images/wallets/metamask.svg';
import MistIcon from 'assets/images/wallets/mist.svg';
import TrezorIcon from 'assets/images/wallets/trezor.svg';
import './WalletDecrypt.scss';
type UnlockParams = {} | PrivateKeyValue;
interface Props {
@ -277,16 +276,18 @@ export class WalletDecrypt extends Component<Props, State> {
);
}
public handleWalletChoice = (walletType: string) => {
public handleWalletChoice = async (walletType: string) => {
const wallet = this.WALLETS[walletType];
if (!wallet) {
return;
}
let timeout = 0;
if (wallet.attemptUnlock) {
timeout = 250;
const web3Available = await isWeb3NodeAvailable();
if (wallet.attemptUnlock && web3Available) {
// timeout is only the maximum wait time before secondary view is shown
// send view will be shown immediately on web3 resolve
timeout = 1000;
wallet.unlock();
}

View File

@ -278,7 +278,12 @@ export const NODES: { [key: string]: NodeConfig } = {
}
};
export async function initWeb3Node(): Promise<void> {
interface Web3NodeInfo {
networkId: string;
lib: Web3Node;
}
export async function setupWeb3Node(): Promise<Web3NodeInfo> {
const { web3 } = window as any;
if (!web3 || !web3.currentProvider || !web3.currentProvider.sendAsync) {
@ -299,6 +304,20 @@ export async function initWeb3Node(): Promise<void> {
throw new Error('MetaMask / Mist is still loading. Please refresh the page and try again.');
}
return { networkId, lib };
}
export async function isWeb3NodeAvailable(): Promise<boolean> {
try {
await setupWeb3Node();
return true;
} catch (e) {
return false;
}
}
export async function initWeb3Node(): Promise<void> {
const { networkId, lib } = await setupWeb3Node();
NODES.web3 = {
network: networkIdToName(networkId),
service: 'MetaMask / Mist',