From e07cd9f7f4a55c81c630481b8dba761faa3c4c8a Mon Sep 17 00:00:00 2001 From: 0xzoz <97761083+0xzoz@users.noreply.github.com> Date: Fri, 1 Apr 2022 15:08:44 +1100 Subject: [PATCH] Handle Tally provider type (#67) * handle tally provider type * Fix error handling issues - Issue #65 * Fix error handling issues - get Accounts - Issue #65 * Provide user warning when clicking Metamask and Tally is set to default * fix translations * Check translations issue * fix translations * fix translations * Align modal text * Align modal text * Align modal text * Align modal text * Fix resfresh issue and wallet auto opening issue * Remove redundant code * Use chosen provider storage --- sample-app/src/elm/Strings/Translations.elm | 16 ++++++ .../Eth/ConnectedEthWallet.elm | 18 +++++-- src/js/sharedEth/connectedWalletPorts.js | 4 +- src/js/sharedEth/connectors.js | 12 +++-- src/js/sharedEth/eth.js | 50 +++++++++++++------ src/js/sharedEth/utils.js | 13 ++++- 6 files changed, 86 insertions(+), 27 deletions(-) diff --git a/sample-app/src/elm/Strings/Translations.elm b/sample-app/src/elm/Strings/Translations.elm index 43235d9..d587318 100644 --- a/sample-app/src/elm/Strings/Translations.elm +++ b/sample-app/src/elm/Strings/Translations.elm @@ -133,12 +133,14 @@ unlock_tally_wallet lang = En -> "Unlock Tally Wallet" + decline_unlock_tally_wallet : Lang -> String decline_unlock_tally_wallet lang = case lang of En -> "We have detected that you have MetaMask installed." + click_tally_extension : Lang -> String click_tally_extension lang = case lang of @@ -146,6 +148,20 @@ click_tally_extension lang = "Tip: If you would prefer to use Tally instead, please download Tally or ensure it is set to default" +decline_unlock_mm_wallet : Lang -> String +decline_unlock_mm_wallet lang = + case lang of + En -> + "We have detected that you have Tally installed." + + +click_mm_extension : Lang -> String +click_mm_extension lang = + case lang of + En -> + "Tip: If you would prefer to use Metamask instead, please ensure Tally is not set as default" + + click_extension : Lang -> String click_extension lang = case lang of diff --git a/src/elm/CompoundComponents/Eth/ConnectedEthWallet.elm b/src/elm/CompoundComponents/Eth/ConnectedEthWallet.elm index 2a52f5a..58225dc 100644 --- a/src/elm/CompoundComponents/Eth/ConnectedEthWallet.elm +++ b/src/elm/CompoundComponents/Eth/ConnectedEthWallet.elm @@ -644,9 +644,16 @@ connectingModal userLanguage maybeSelectedProvider ({ chooseWalletState } as mod ) _ -> - ( Translations.unlock_wallet userLanguage - , Translations.click_extension userLanguage - ) + if model.providerType == EthProviderInfo.Tally then + ( Translations.decline_unlock_mm_wallet userLanguage + , Translations.click_mm_extension userLanguage + ) + + else + ( Translations.unlock_wallet userLanguage + , Translations.click_extension userLanguage + ) + showBorderClass = if isCompoundChain then @@ -659,7 +666,7 @@ connectingModal userLanguage maybeSelectedProvider ({ chooseWalletState } as mod [ inCopyBackArrow isCompoundChain , markSpan isCompoundChain , h4 [] [ text headerText ] - , p [] [ text instructionsText ] + , p [ class "center-text"] [ text instructionsText ] , div [ class "connecting-ring" ] [ div [] [] ] , termsView userLanguage isCompoundChain @@ -990,6 +997,9 @@ providerTypeFromId id = 4 -> Just WalletConnect + 5 -> + Just Tally + _ -> Nothing diff --git a/src/js/sharedEth/connectedWalletPorts.js b/src/js/sharedEth/connectedWalletPorts.js index 549db45..c171263 100644 --- a/src/js/sharedEth/connectedWalletPorts.js +++ b/src/js/sharedEth/connectedWalletPorts.js @@ -1,4 +1,4 @@ -import { shouldAutoConnect } from './utils'; +import { providerTypeId, shouldAutoConnect } from './utils'; import { getAccounts, getLedgerAddressAndBalance, getNetworkId, makeEth, setNetworkId } from './eth'; import { connectLedger, @@ -158,7 +158,7 @@ function subscribeToTryConnect(app, eth, globEthereum, defaultNetworkId) { // We'll try to set to the user's last chosen provider, otherwise // defaulting to Web3. - let providerType = Number(storage('chosenProvider').get(PROVIDER_TYPE_WEB3)); + let providerType = Number(storage('chosenProvider').get(providerTypeId(globEthereum))); let connected = await connectToTrxProvider(app, eth, globEthereum, providerType, '', true); if (!connected) { diff --git a/src/js/sharedEth/connectors.js b/src/js/sharedEth/connectors.js index ccbac01..f9452de 100644 --- a/src/js/sharedEth/connectors.js +++ b/src/js/sharedEth/connectors.js @@ -66,9 +66,15 @@ async function connectWalletLink(eth, disallowAuthDialog = false) { } async function requiresAuthDialog(ethereum) { - let [account, _] = await new Eth(ethereum).getAccounts(); - - return !account; + try{ + let [account, _] = await new Eth(ethereum).getAccounts(); + return !account; + + }catch(e){ + console.log(e); + return true; + + } } async function connectWeb3(eth, ethereum, disallowAuthDialog = false, isAutoConnect = false) { diff --git a/src/js/sharedEth/eth.js b/src/js/sharedEth/eth.js index 6ebebc3..dcfd643 100644 --- a/src/js/sharedEth/eth.js +++ b/src/js/sharedEth/eth.js @@ -372,12 +372,16 @@ function withWeb3Eth(eth) { } function withTrxWeb3(eth, fnTrxWeb3, fnEls) { - if (eth.trxEth) { - let res = fnTrxWeb3(eth.trxEth, eth.trxEth.trxPromise); - eth.trxEth.trxPromise = res; - return res; - } else { - return fnEls(); + try{ + if (eth.trxEth) { + let res = fnTrxWeb3(eth.trxEth, eth.trxEth.trxPromise); + eth.trxEth.trxPromise = res; + return res; + } else { + return fnEls(); + } + }catch(e){ + console.log(e); } } @@ -463,11 +467,18 @@ function setNetworkId(eth, networkId) { } async function getNetworkId(eth) { - return withTrxWeb3( - eth, - (trxEth) => trxEth.net.getId(), - () => eth.defaultNetworkId - ); + try{ + let networkId = await withTrxWeb3( + eth, + (trxEth) => trxEth.net.getId(), + () => eth.defaultNetworkId + ); + return networkId; + }catch(e){ + console.log(e) + return eth.defaultNetworkId; + + } } async function getBalance(eth, address) { @@ -483,11 +494,18 @@ async function getBlockNumber(eth) { } async function getAccounts(eth) { - return withTrxWeb3( - eth, - (trxEth) => trxEth.getAccounts(), - () => (eth.showAccount ? [eth.showAccount] : []) - ); + try{ + let accs = await withTrxWeb3( + eth, + (trxEth) => trxEth.getAccounts(), + () => (eth.showAccount ? [eth.showAccount] : []) + ) + return accs; + + }catch(e){ + console.log(e) + return []; + } } async function getTransaction(eth, trxHash) { diff --git a/src/js/sharedEth/utils.js b/src/js/sharedEth/utils.js index d617340..b01e8fb 100644 --- a/src/js/sharedEth/utils.js +++ b/src/js/sharedEth/utils.js @@ -6,7 +6,7 @@ const PROVIDER_TYPE_META_MASK = 'meta_mask'; const PROVIDER_TYPE_META_MASK_MOBILE = 'meta_mask_mobile'; const PROVIDER_TYPE_OTHER = 'other'; -const NON_AUTOCONNECT_PROVIDERS = [PROVIDER_TYPE_NONE, PROVIDER_TYPE_META_MASK]; +const NON_AUTOCONNECT_PROVIDERS = [PROVIDER_TYPE_NONE, PROVIDER_TYPE_META_MASK, PROVIDER_TYPE_TALLY]; function reverseObject(obj) { return Object.keys(obj).reduce((acc, key) => { @@ -61,6 +61,14 @@ function providerType(provider) { } } +function providerTypeId(provider) { + if (provider.isTally) { + return 5; + } else { + return 3; + } +} + function networkFromId(id) { switch (id) { case 0: @@ -143,6 +151,7 @@ export { log, networkFromId, providerType, + providerTypeId, PROVIDER_TYPE_NONE, PROVIDER_TYPE_COINBASE_WALLET, PROVIDER_TYPE_IM_TOKEN, @@ -152,4 +161,4 @@ export { shouldAutoConnect, supportFromEntries, langFromURL, -}; +}; \ No newline at end of file