From 253fba1be4a60b3c424810a25bd107deae0f0aa6 Mon Sep 17 00:00:00 2001 From: James Brown Date: Wed, 1 Nov 2023 13:31:43 +1100 Subject: [PATCH] Refactor and simplify setup of jsInject --- .../app/repository/EthereumNetworkBase.java | 53 ++++--------------- .../EthereumNetworkRepositoryType.java | 2 - .../app/ui/AssetDisplayActivity.java | 1 + .../app/ui/DappBrowserFragment.java | 10 ++-- .../alphawallet/app/ui/FunctionActivity.java | 1 - .../app/ui/ImportTokenActivity.java | 4 ++ .../app/ui/NFTAssetDetailActivity.java | 9 ++-- .../ui/RedeemSignatureDisplayActivity.java | 1 + .../app/ui/TokenFunctionActivity.java | 1 + .../holder/AssetInstanceScriptHolder.java | 1 + .../ui/widget/holder/BaseTicketHolder.java | 3 +- .../app/viewmodel/DappBrowserViewModel.java | 6 --- .../app/viewmodel/TokenFunctionViewModel.java | 5 -- .../app/web3/JsInjectorClient.java | 35 +++++------- .../alphawallet/app/web3/Web3TokenView.java | 8 +-- .../com/alphawallet/app/web3/Web3View.java | 5 -- 16 files changed, 45 insertions(+), 100 deletions(-) diff --git a/app/src/main/java/com/alphawallet/app/repository/EthereumNetworkBase.java b/app/src/main/java/com/alphawallet/app/repository/EthereumNetworkBase.java index 3d947f9fc7..ba377fc0eb 100644 --- a/app/src/main/java/com/alphawallet/app/repository/EthereumNetworkBase.java +++ b/app/src/main/java/com/alphawallet/app/repository/EthereumNetworkBase.java @@ -203,48 +203,6 @@ public abstract class EthereumNetworkBase implements EthereumNetworkRepositoryTy private static final String INFURA_ENDPOINT = ".infura.io/v3/"; - @Override - public String getDappBrowserRPC(long chainId) - { - NetworkInfo info = getNetworkByChain(chainId); - - if (info == null) - { - return ""; - } - - int index = info.rpcServerUrl.indexOf(INFURA_ENDPOINT); - if (index > 0) - { - return info.rpcServerUrl.substring(0, index + INFURA_ENDPOINT.length()) + keyProvider.getTertiaryInfuraKey(); - } - else - { - return info.backupNodeUrl != null ? info.backupNodeUrl : info.rpcServerUrl; - } - } - - @Override - public String getTokenScriptRPC(long chainId) - { - NetworkInfo info = getNetworkByChain(chainId); - - if (info == null) - { - return ""; - } - - int index = info.rpcServerUrl.indexOf(INFURA_ENDPOINT); - if (index > 0) - { - return info.rpcServerUrl.substring(0, index + INFURA_ENDPOINT.length()) + keyProvider.getTSInfuraKey(); - } - else - { - return info.backupNodeUrl != null ? info.backupNodeUrl : info.rpcServerUrl; - } - } - public static boolean isInfura(String rpcServerUrl) { return rpcServerUrl.contains(INFURA_ENDPOINT); @@ -1100,6 +1058,15 @@ public static String getNodeURLByNetworkId(long networkId) * @return */ public static String getDefaultNodeURL(long chainId) + { + return getNodeRPC(keyProvider.getTertiaryInfuraKey(), chainId); + } + public static String getTSNodeURL(long chainId) + { + return getNodeRPC(keyProvider.getTSInfuraKey(), chainId); + } + + private static String getNodeRPC(String infuraKey, long chainId) { NetworkInfo info = networkMap.get(chainId); @@ -1111,7 +1078,7 @@ public static String getDefaultNodeURL(long chainId) int index = info.rpcServerUrl.indexOf(INFURA_ENDPOINT); if (index > 0) { - return info.rpcServerUrl.substring(0, index + INFURA_ENDPOINT.length()) + keyProvider.getTertiaryInfuraKey(); + return info.rpcServerUrl.substring(0, index + INFURA_ENDPOINT.length()) + infuraKey; } else { diff --git a/app/src/main/java/com/alphawallet/app/repository/EthereumNetworkRepositoryType.java b/app/src/main/java/com/alphawallet/app/repository/EthereumNetworkRepositoryType.java index 1b80c28cc8..8e11cc8660 100644 --- a/app/src/main/java/com/alphawallet/app/repository/EthereumNetworkRepositoryType.java +++ b/app/src/main/java/com/alphawallet/app/repository/EthereumNetworkRepositoryType.java @@ -51,8 +51,6 @@ public interface EthereumNetworkRepositoryType { String getCurrentWalletAddress(); boolean hasSetNetworkFilters(); void setHasSetNetworkFilters(); - String getDappBrowserRPC(long chainId); - String getTokenScriptRPC(long chainId); void saveCustomRPCNetwork(String networkName, String rpcUrl, long chainId, String symbol, String blockExplorerUrl, String explorerApiUrl, boolean isTestnet, Long oldChainId); void removeCustomRPCNetwork(long chainId); diff --git a/app/src/main/java/com/alphawallet/app/ui/AssetDisplayActivity.java b/app/src/main/java/com/alphawallet/app/ui/AssetDisplayActivity.java index 6e693fc7d5..4b2ae33a76 100644 --- a/app/src/main/java/com/alphawallet/app/ui/AssetDisplayActivity.java +++ b/app/src/main/java/com/alphawallet/app/ui/AssetDisplayActivity.java @@ -212,6 +212,7 @@ private void initWebViewCheck(TokenDefinition td) { BigInteger tokenId = token.getArrayBalance().get(0); TicketRange data = new TicketRange(tokenId, token.getAddress()); + testView.setChainId(token.tokenInfo.chainId); testView.renderTokenScriptView(token, data, viewModel.getAssetDefinitionService(), ViewType.ITEM_VIEW, td); testView.setOnReadyCallback(this); } diff --git a/app/src/main/java/com/alphawallet/app/ui/DappBrowserFragment.java b/app/src/main/java/com/alphawallet/app/ui/DappBrowserFragment.java index 62a36e41af..33e3e9bf70 100644 --- a/app/src/main/java/com/alphawallet/app/ui/DappBrowserFragment.java +++ b/app/src/main/java/com/alphawallet/app/ui/DappBrowserFragment.java @@ -721,10 +721,10 @@ private void stopBalanceListener() private void onDefaultWallet(Wallet wallet) { this.wallet = wallet; - if (activeNetwork != null) + if (activeNetwork != null && wallet != null) { boolean needsReload = loadOnInit == null; - setupWeb3(); + setupWeb3(wallet); if (needsReload) reloadPage(); } } @@ -745,7 +745,7 @@ public void switchNetworkAndLoadUrl(long chainId, String url) updateNetworkMenuItem(); viewModel.setNetwork(chainId); startBalanceListener(); - setupWeb3(); + setupWeb3(wallet); web3.resetView(); web3.reload(); } @@ -816,10 +816,10 @@ private void displayCloseWC() }); } - private void setupWeb3() + private void setupWeb3(Wallet wallet) { + if (wallet == null) { return; } web3.setChainId(activeNetwork.chainId); - web3.setRpcUrl(viewModel.getNetworkNodeRPC(activeNetwork.chainId)); web3.setWalletAddress(new Address(wallet.address)); web3.setWebChromeClient(new WebChromeClient() diff --git a/app/src/main/java/com/alphawallet/app/ui/FunctionActivity.java b/app/src/main/java/com/alphawallet/app/ui/FunctionActivity.java index de3eebd48d..12cda501c4 100644 --- a/app/src/main/java/com/alphawallet/app/ui/FunctionActivity.java +++ b/app/src/main/java/com/alphawallet/app/ui/FunctionActivity.java @@ -145,7 +145,6 @@ private void initViews() tokenView.setChainId(token.tokenInfo.chainId); tokenView.setWalletAddress(new Address(token.getWallet())); tokenView.setupWindowCallback(this); - tokenView.setRpcUrl(viewModel.getBrowserRPC(token.tokenInfo.chainId)); tokenView.setOnReadyCallback(this); tokenView.setOnSignPersonalMessageListener(this); tokenView.setOnSetValuesListener(this); diff --git a/app/src/main/java/com/alphawallet/app/ui/ImportTokenActivity.java b/app/src/main/java/com/alphawallet/app/ui/ImportTokenActivity.java index 5227266c49..6b07008efa 100644 --- a/app/src/main/java/com/alphawallet/app/ui/ImportTokenActivity.java +++ b/app/src/main/java/com/alphawallet/app/ui/ImportTokenActivity.java @@ -349,6 +349,10 @@ private void displayImportAction() importTickets.setVisibility(View.VISIBLE); importTickets.setAlpha(1.0f); MagicLinkData data = viewModel.getSalesOrder(); + if (token != null) + { + tokenView.setChainId(token.tokenInfo.chainId); + } switch (data.contractType) { diff --git a/app/src/main/java/com/alphawallet/app/ui/NFTAssetDetailActivity.java b/app/src/main/java/com/alphawallet/app/ui/NFTAssetDetailActivity.java index bb7e3f3f45..09d8cf3849 100644 --- a/app/src/main/java/com/alphawallet/app/ui/NFTAssetDetailActivity.java +++ b/app/src/main/java/com/alphawallet/app/ui/NFTAssetDetailActivity.java @@ -454,15 +454,19 @@ private void onNftAsset(NFTAsset asset) private void updateDefaultTokenData() { + String displayTokenId = ""; if (!TextUtils.isEmpty(sequenceId)) { - tivTokenId.setValue(sequenceId); + displayTokenId = sequenceId; } else if (tokenId != null) { - tivTokenId.setValue(tokenId.toString()); + displayTokenId = tokenId.toString(); } + tivTokenId.setValue(displayTokenId); + tivTokenId.setCopyableValue(displayTokenId); + tivNetwork.setValue(token.getNetworkName()); tivContractAddress.setCopyableValue(token.tokenInfo.address); @@ -861,7 +865,6 @@ private boolean displayTokenView(final TokenDefinition td) webWrapper.setVisibility(View.VISIBLE); tokenScriptView.setChainId(token.tokenInfo.chainId); tokenScriptView.setWalletAddress(new Address(token.getWallet())); - tokenScriptView.setRpcUrl(viewModel.getBrowserRPC(token.tokenInfo.chainId)); webWrapper.addView(tokenScriptView); couldDisplay = true; } diff --git a/app/src/main/java/com/alphawallet/app/ui/RedeemSignatureDisplayActivity.java b/app/src/main/java/com/alphawallet/app/ui/RedeemSignatureDisplayActivity.java index 88a16a9a8d..85754cb482 100644 --- a/app/src/main/java/com/alphawallet/app/ui/RedeemSignatureDisplayActivity.java +++ b/app/src/main/java/com/alphawallet/app/ui/RedeemSignatureDisplayActivity.java @@ -101,6 +101,7 @@ protected void onCreate(@Nullable Bundle savedInstanceState) { tv.setVisibility(View.VISIBLE); //given a webview populate with rendered token + tokenView.setChainId(token.tokenInfo.chainId); tokenView.displayTicketHolder(token, ticketRange.range, viewModel.getAssetDefinitionService()); tokenView.setOnReadyCallback(this); tokenView.setLayout(token, ViewType.ITEM_VIEW); diff --git a/app/src/main/java/com/alphawallet/app/ui/TokenFunctionActivity.java b/app/src/main/java/com/alphawallet/app/ui/TokenFunctionActivity.java index d836644a77..27c6a75ef5 100644 --- a/app/src/main/java/com/alphawallet/app/ui/TokenFunctionActivity.java +++ b/app/src/main/java/com/alphawallet/app/ui/TokenFunctionActivity.java @@ -92,6 +92,7 @@ private void initViews(Token t) { TicketRange data = new TicketRange(idList, token.tokenInfo.address, false); + tokenView.setChainId(token.tokenInfo.chainId); tokenView.displayTicketHolder(token, data, viewModel.getAssetDefinitionService(), ViewType.VIEW); tokenView.setOnReadyCallback(this); tokenView.setOnSetValuesListener(this); diff --git a/app/src/main/java/com/alphawallet/app/ui/widget/holder/AssetInstanceScriptHolder.java b/app/src/main/java/com/alphawallet/app/ui/widget/holder/AssetInstanceScriptHolder.java index 1ef18f89ca..9d064525fa 100644 --- a/app/src/main/java/com/alphawallet/app/ui/widget/holder/AssetInstanceScriptHolder.java +++ b/app/src/main/java/com/alphawallet/app/ui/widget/holder/AssetInstanceScriptHolder.java @@ -53,6 +53,7 @@ public AssetInstanceScriptHolder(int resId, ViewGroup parent, Token t, AssetDefi clickWrapper = findViewById(R.id.click_layer); itemSelect = findViewById(R.id.radioBox); token = t; + tokenView.setChainId(t.tokenInfo.chainId); tokenView.setOnReadyCallback(this); this.iconified = iconified; } diff --git a/app/src/main/java/com/alphawallet/app/ui/widget/holder/BaseTicketHolder.java b/app/src/main/java/com/alphawallet/app/ui/widget/holder/BaseTicketHolder.java index cc23338c89..1b4511bbb1 100644 --- a/app/src/main/java/com/alphawallet/app/ui/widget/holder/BaseTicketHolder.java +++ b/app/src/main/java/com/alphawallet/app/ui/widget/holder/BaseTicketHolder.java @@ -28,19 +28,18 @@ public class BaseTicketHolder extends BinderViewHolder implements V private TokensAdapterCallback tokensAdapterCallback; private final AssetDefinitionService assetService; //need to cache this locally, unless we cache every string we need in the constructor - private final View activityView; protected final RelativeLayout ticketLayout; public BaseTicketHolder(int resId, ViewGroup parent, Token ticket, AssetDefinitionService service) { super(resId, parent); - activityView = this.itemView; tokenView = findViewById(R.id.web3_tokenview); webWrapper = findViewById(R.id.layout_webwrapper); itemView.setOnClickListener(this); ticketLayout = findViewById(R.id.layout_select_ticket); assetService = service; token = ticket; + tokenView.setChainId(ticket.tokenInfo.chainId); tokenView.setOnReadyCallback(this); } diff --git a/app/src/main/java/com/alphawallet/app/viewmodel/DappBrowserViewModel.java b/app/src/main/java/com/alphawallet/app/viewmodel/DappBrowserViewModel.java index c6b4608cfd..5249eb4a1d 100644 --- a/app/src/main/java/com/alphawallet/app/viewmodel/DappBrowserViewModel.java +++ b/app/src/main/java/com/alphawallet/app/viewmodel/DappBrowserViewModel.java @@ -416,12 +416,6 @@ public Single calculateGasEstimate(Wallet wallet, Web3Transaction t } } - // Use the backup node if avail - public String getNetworkNodeRPC(long chainId) - { - return ethereumNetworkRepository.getDappBrowserRPC(chainId); - } - public NetworkInfo getNetworkInfo(long chainId) { return ethereumNetworkRepository.getNetworkByChain(chainId); diff --git a/app/src/main/java/com/alphawallet/app/viewmodel/TokenFunctionViewModel.java b/app/src/main/java/com/alphawallet/app/viewmodel/TokenFunctionViewModel.java index 827f1b727b..61d095d887 100644 --- a/app/src/main/java/com/alphawallet/app/viewmodel/TokenFunctionViewModel.java +++ b/app/src/main/java/com/alphawallet/app/viewmodel/TokenFunctionViewModel.java @@ -902,11 +902,6 @@ private void onAsset(String result, Token token, BigInteger tokenId) } } - public String getBrowserRPC(long chainId) - { - return ethereumNetworkRepository.getTokenScriptRPC(chainId); - } - public boolean hasTokenScript(Token token) { return token != null && assetDefinitionService.getAssetDefinition(token) != null; diff --git a/app/src/main/java/com/alphawallet/app/web3/JsInjectorClient.java b/app/src/main/java/com/alphawallet/app/web3/JsInjectorClient.java index 195abb477a..12083fd398 100644 --- a/app/src/main/java/com/alphawallet/app/web3/JsInjectorClient.java +++ b/app/src/main/java/com/alphawallet/app/web3/JsInjectorClient.java @@ -34,17 +34,13 @@ public class JsInjectorClient { private static final String DEFAULT_MIME_TYPE = "text/html"; private final static String JS_TAG_TEMPLATE = ""; - private final Context context; - private final OkHttpClient httpClient; - - private long chainId = 1; + private long chainId; private Address walletAddress; - //Note: this default RPC is overridden before injection - private String rpcUrl = EthereumNetworkRepository.getDefaultNodeURL(MAINNET_ID); + + private String rpcUrl; public JsInjectorClient(Context context) { - this.context = context; - this.httpClient = createHttpClient(); + } public Address getWalletAddress() { @@ -59,16 +55,17 @@ public long getChainId() { return chainId; } - public void setChainId(long chainId) { + public void setChainId(long chainId) + { this.chainId = chainId; + this.rpcUrl = EthereumNetworkRepository.getDefaultNodeURL(chainId); } - public String getRpcUrl() { - return rpcUrl; - } - - public void setRpcUrl(String rpcUrl) { - this.rpcUrl = rpcUrl; + // Set ChainId for TokenScript inject + public void setTSChainId(long chainId) + { + this.chainId = chainId; + this.rpcUrl = EthereumNetworkRepository.getTSNodeURL(chainId); } public String initJs(Context context) @@ -86,7 +83,7 @@ String injectWeb3TokenInit(Context ctx, String view, String tokenContent, BigInt String initSrc = loadFile(ctx, R.raw.init_token); //put the view in here String tokenIdWrapperName = "token-card-" + tokenId.toString(10); - initSrc = String.format(initSrc, tokenContent, walletAddress, EthereumNetworkRepository.getDefaultNodeURL(chainId), chainId, tokenIdWrapperName); + initSrc = String.format(initSrc, tokenContent, walletAddress, rpcUrl, chainId, tokenIdWrapperName); //now insert this source into the view // note that the
is not closed because it is closed in injectStyleAndWrap(). String wrapper = "
"; @@ -217,10 +214,4 @@ private String getContentTypeHeader(Response response) { } return contentType; } - - private OkHttpClient createHttpClient() { - return new OkHttpClient.Builder() - .cookieJar(new WebViewCookieJar()) - .build(); - } } diff --git a/app/src/main/java/com/alphawallet/app/web3/Web3TokenView.java b/app/src/main/java/com/alphawallet/app/web3/Web3TokenView.java index 3a589dbc84..5aea50ac18 100644 --- a/app/src/main/java/com/alphawallet/app/web3/Web3TokenView.java +++ b/app/src/main/java/com/alphawallet/app/web3/Web3TokenView.java @@ -240,13 +240,9 @@ public boolean onJsAlert(WebView view, String url, String message, JsResult resu ); } - public void setChainId(long chainId) { - jsInjectorClient.setChainId(chainId); - } - - public void setRpcUrl(@NonNull String useRPC) + public void setChainId(long chainId) { - jsInjectorClient.setRpcUrl(useRPC); + jsInjectorClient.setTSChainId(chainId); } public void onSignPersonalMessageSuccessful(@NotNull Signable message, String signHex) { diff --git a/app/src/main/java/com/alphawallet/app/web3/Web3View.java b/app/src/main/java/com/alphawallet/app/web3/Web3View.java index 1e2f820c18..71e0270680 100644 --- a/app/src/main/java/com/alphawallet/app/web3/Web3View.java +++ b/app/src/main/java/com/alphawallet/app/web3/Web3View.java @@ -235,11 +235,6 @@ public void setWebLoadCallback(URLLoadInterface iFace) loadInterface = iFace; } - public void setRpcUrl(@NonNull String rpcUrl) - { - webViewClient.getJsInjectorClient().setRpcUrl(rpcUrl); - } - public void setOnSignTransactionListener(@Nullable OnSignTransactionListener onSignTransactionListener) { this.onSignTransactionListener = onSignTransactionListener;