From de8f1ef999a793698b4b8dca721dd6d0e99bf696 Mon Sep 17 00:00:00 2001 From: James Brown Date: Wed, 1 Nov 2023 10:37:29 +1100 Subject: [PATCH] improve TokenScript handling, use separate key for TokenScript inject --- app/src/main/cpp/keys.c | 12 +++++ .../app/repository/EthereumNetworkBase.java | 21 ++++++++ .../EthereumNetworkRepositoryType.java | 1 + .../app/repository/KeyProvider.java | 2 + .../app/repository/KeyProviderJNIImpl.java | 2 + .../app/repository/TokenRepository.java | 4 -- .../app/service/AWHttpService.java | 5 -- .../app/service/AssetDefinitionService.java | 54 ++++++++++++++----- .../alphawallet/app/ui/FunctionActivity.java | 4 +- .../app/ui/NFTAssetDetailActivity.java | 1 + .../app/viewmodel/TokenFunctionViewModel.java | 2 +- .../alphawallet/app/web3/Web3TokenView.java | 6 --- .../app/di/mock/KeyProviderMockImpl.java | 6 +++ .../KeyProviderMockNonProductionImpl.java | 3 ++ 14 files changed, 92 insertions(+), 31 deletions(-) diff --git a/app/src/main/cpp/keys.c b/app/src/main/cpp/keys.c index a69b160647..8d7ba966cf 100644 --- a/app/src/main/cpp/keys.c +++ b/app/src/main/cpp/keys.c @@ -232,6 +232,18 @@ Java_com_alphawallet_app_repository_KeyProviderJNIImpl_getInfuraSecret(JNIEnv *e #endif } +JNIEXPORT jstring JNICALL +Java_com_alphawallet_app_repository_KeyProviderJNIImpl_getTSInfuraKey(JNIEnv *env, jobject thiz) { +#if (HAS_KEYS == 1) + return getDecryptedKey(env, tokenscriptInfuraKey); +#elif (HAS_INFURA == 1) + return (*env)->NewStringUTF(env, INFURA_Q); +#else + const jstring key = "da3717f25f824cc1baa32d812386d93f"; + return (*env)->NewStringUTF(env, key); +#endif +} + JNIEXPORT jstring JNICALL Java_com_alphawallet_app_repository_KeyProviderJNIImpl_getUnstoppableDomainsKey( JNIEnv* env, jclass thiz ) { 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 ae04356676..3d947f9fc7 100644 --- a/app/src/main/java/com/alphawallet/app/repository/EthereumNetworkBase.java +++ b/app/src/main/java/com/alphawallet/app/repository/EthereumNetworkBase.java @@ -224,6 +224,27 @@ public String getDappBrowserRPC(long chainId) } } + @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); 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 ad6fa7f1a8..1b80c28cc8 100644 --- a/app/src/main/java/com/alphawallet/app/repository/EthereumNetworkRepositoryType.java +++ b/app/src/main/java/com/alphawallet/app/repository/EthereumNetworkRepositoryType.java @@ -52,6 +52,7 @@ public interface EthereumNetworkRepositoryType { 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/repository/KeyProvider.java b/app/src/main/java/com/alphawallet/app/repository/KeyProvider.java index 5611ff445b..6ab5b91d55 100644 --- a/app/src/main/java/com/alphawallet/app/repository/KeyProvider.java +++ b/app/src/main/java/com/alphawallet/app/repository/KeyProvider.java @@ -34,6 +34,8 @@ public interface KeyProvider String getInfuraSecret(); + String getTSInfuraKey(); + String getUnstoppableDomainsKey(); String getOkLinkKey(); diff --git a/app/src/main/java/com/alphawallet/app/repository/KeyProviderJNIImpl.java b/app/src/main/java/com/alphawallet/app/repository/KeyProviderJNIImpl.java index b8d611dc4c..fe54320142 100644 --- a/app/src/main/java/com/alphawallet/app/repository/KeyProviderJNIImpl.java +++ b/app/src/main/java/com/alphawallet/app/repository/KeyProviderJNIImpl.java @@ -9,6 +9,8 @@ public KeyProviderJNIImpl() public native String getInfuraKey(); + public native String getTSInfuraKey(); + public native String getSecondaryInfuraKey(); public native String getTertiaryInfuraKey(); diff --git a/app/src/main/java/com/alphawallet/app/repository/TokenRepository.java b/app/src/main/java/com/alphawallet/app/repository/TokenRepository.java index 8e5abdded1..5eb3d6951a 100644 --- a/app/src/main/java/com/alphawallet/app/repository/TokenRepository.java +++ b/app/src/main/java/com/alphawallet/app/repository/TokenRepository.java @@ -675,10 +675,6 @@ public Single getEthTicker(long chainId) private BigDecimal getEthBalance(Wallet wallet, long chainId) { - if (chainId == 17000) - { - System.out.println("YOLESS"); - } try { return new BigDecimal(getService(chainId).ethGetBalance(wallet.address, DefaultBlockParameterName.LATEST) .send() diff --git a/app/src/main/java/com/alphawallet/app/service/AWHttpService.java b/app/src/main/java/com/alphawallet/app/service/AWHttpService.java index bfef03f5e3..ab9dbd80f2 100644 --- a/app/src/main/java/com/alphawallet/app/service/AWHttpService.java +++ b/app/src/main/java/com/alphawallet/app/service/AWHttpService.java @@ -125,11 +125,6 @@ protected InputStream performIO(String request) throws IOException requestBody = RequestBody.create("", MEDIA_TYPE_TEXT); } - if (url.contains("stormbird")) - { - System.out.println("YOLESS"); - } - okhttp3.Request httpRequest = new okhttp3.Request.Builder() .url(url) diff --git a/app/src/main/java/com/alphawallet/app/service/AssetDefinitionService.java b/app/src/main/java/com/alphawallet/app/service/AssetDefinitionService.java index 65a2e3e37b..3bc28df404 100644 --- a/app/src/main/java/com/alphawallet/app/service/AssetDefinitionService.java +++ b/app/src/main/java/com/alphawallet/app/service/AssetDefinitionService.java @@ -804,18 +804,10 @@ public TokenDefinition getDefinition(String tsKey) String address = elements[0]; long chainId = Long.parseLong(elements[1]); - //try cache - if (cachedDefinition != null) + + if (checkCachedDefinition(chainId, address) != null) { - //only match holding token - ContractInfo holdingContracts = cachedDefinition.contracts.get(cachedDefinition.holdingToken); - if (holdingContracts != null && holdingContracts.addresses.containsKey(chainId)) - { - for (String addr : holdingContracts.addresses.get(chainId)) - { - if (addr.equalsIgnoreCase(address.toLowerCase())) return cachedDefinition; - } - } + return cachedDefinition; } try (Realm realm = realmManager.getRealmInstance(ASSET_DEFINITION_DB)) @@ -846,6 +838,24 @@ public TokenDefinition getDefinition(String tsKey) return result; } + private TokenDefinition checkCachedDefinition(long chainId, String address) + { + if (cachedDefinition != null) + { + //only match holding token + ContractInfo holdingContracts = cachedDefinition.contracts.get(cachedDefinition.holdingToken); + if (holdingContracts != null && holdingContracts.addresses.containsKey(chainId)) + { + for (String addr : holdingContracts.addresses.get(chainId)) + { + if (addr.equalsIgnoreCase(address.toLowerCase())) return cachedDefinition; + } + } + } + + return null; + } + public TokenScriptFile getTokenScriptFile(long chainId, String address) { //pull from database @@ -1031,10 +1041,16 @@ public TokenDefinition getAssetDefinition(Token token) return null; } + if (checkCachedDefinition(token.tokenInfo.chainId, token.getAddress()) != null) + { + return cachedDefinition; + } + try { TokenScriptFile tsf = getTokenScriptFile(token); - return parseFile(tsf.getInputStream()); + cachedDefinition = parseFile(tsf.getInputStream()); + return cachedDefinition; } catch (Exception e) { @@ -1217,7 +1233,7 @@ private Single handleNewTSFile(File newFile) boolean isDebugOverride = tsf.isDebug(); //remove all old definitions & certificates updateScriptEntriesInRealm(originContracts, isDebugOverride, tsf.calcMD5(), schemaUID); - cachedDefinition = null; + cachedDefinition = td; return tsf; }).flatMap(tt -> cacheSignature(tsf)) .map(a -> fileLoadComplete(originContracts, tsf, td)); @@ -3109,7 +3125,17 @@ public Single checkServerForScript(Token token, MutableLiveData { try { - TokenDefinition td = parseFile(tf.getInputStream()); + TokenDefinition td; + if (checkCachedDefinition(token.tokenInfo.chainId, token.getAddress()) != null) + { + td = cachedDefinition; + } + else + { + td = parseFile(tf.getInputStream()); + cachedDefinition = td; + } + return Single.fromCallable(() -> td); } catch (Exception ignored) 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 84fcc413db..de3eebd48d 100644 --- a/app/src/main/java/com/alphawallet/app/ui/FunctionActivity.java +++ b/app/src/main/java/com/alphawallet/app/ui/FunctionActivity.java @@ -621,6 +621,7 @@ public void functionSuccess() { LinearLayout successOverlay = findViewById(R.id.layout_success_overlay); if (successOverlay != null) successOverlay.setVisibility(View.VISIBLE); + tokenView.destroy(); handler.postDelayed(closer, 1000); } @@ -630,6 +631,7 @@ public void functionSuccess() */ private void txWritten(TransactionReturn transactionReturn) { + tokenView.destroy(); confirmationDialog.transactionWritten(transactionReturn.hash); //display hash and success in ActionSheet, start 1 second timer to dismiss. } @@ -677,7 +679,6 @@ public void onPause() { super.onPause(); viewModel.resetSignDialog(); - tokenView.destroy(); } public void onSaveInstanceState(@NonNull Bundle savedInstanceState) @@ -937,6 +938,7 @@ public void dismissed(String txHash, long callbackId, boolean actionCompleted) else { setResult(RESULT_CANCELED, intent); + tokenView.destroy(); finish(); } } 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 493592c141..bb7e3f3f45 100644 --- a/app/src/main/java/com/alphawallet/app/ui/NFTAssetDetailActivity.java +++ b/app/src/main/java/com/alphawallet/app/ui/NFTAssetDetailActivity.java @@ -861,6 +861,7 @@ 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/viewmodel/TokenFunctionViewModel.java b/app/src/main/java/com/alphawallet/app/viewmodel/TokenFunctionViewModel.java index 208e05b78f..827f1b727b 100644 --- a/app/src/main/java/com/alphawallet/app/viewmodel/TokenFunctionViewModel.java +++ b/app/src/main/java/com/alphawallet/app/viewmodel/TokenFunctionViewModel.java @@ -904,7 +904,7 @@ private void onAsset(String result, Token token, BigInteger tokenId) public String getBrowserRPC(long chainId) { - return ethereumNetworkRepository.getDappBrowserRPC(chainId); + return ethereumNetworkRepository.getTokenScriptRPC(chainId); } public boolean hasTokenScript(Token token) 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 bb2d9a56e9..3a589dbc84 100644 --- a/app/src/main/java/com/alphawallet/app/web3/Web3TokenView.java +++ b/app/src/main/java/com/alphawallet/app/web3/Web3TokenView.java @@ -404,12 +404,6 @@ public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request return false; } } - - @Override - public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) - { - showError(RENDERING_ERROR.replace("${ERR1}", error.getDescription())); - } } // Rendering diff --git a/app/src/test/java/com/alphawallet/app/di/mock/KeyProviderMockImpl.java b/app/src/test/java/com/alphawallet/app/di/mock/KeyProviderMockImpl.java index 39cf5acc8b..0969a84731 100644 --- a/app/src/test/java/com/alphawallet/app/di/mock/KeyProviderMockImpl.java +++ b/app/src/test/java/com/alphawallet/app/di/mock/KeyProviderMockImpl.java @@ -102,6 +102,12 @@ public String getInfuraSecret() return FAKE_KEY_FOR_TESTING; } + @Override + public String getTSInfuraKey() + { + return FAKE_KEY_FOR_TESTING; + } + @Override public String getUnstoppableDomainsKey() { diff --git a/app/src/test/java/com/alphawallet/app/di/mock/KeyProviderMockNonProductionImpl.java b/app/src/test/java/com/alphawallet/app/di/mock/KeyProviderMockNonProductionImpl.java index 5945698bca..b9def34506 100644 --- a/app/src/test/java/com/alphawallet/app/di/mock/KeyProviderMockNonProductionImpl.java +++ b/app/src/test/java/com/alphawallet/app/di/mock/KeyProviderMockNonProductionImpl.java @@ -65,6 +65,9 @@ public String getTertiaryInfuraKey() return null; } + @Override + public String getTSInfuraKey() { return null; } + @Override public String getRampKey() {