Skip to content

Commit

Permalink
improve TokenScript handling, use separate key for TokenScript inject
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesSmartCell committed Oct 31, 2023
1 parent 50f6973 commit de8f1ef
Show file tree
Hide file tree
Showing 14 changed files with 92 additions and 31 deletions.
12 changes: 12 additions & 0 deletions app/src/main/cpp/keys.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ public interface KeyProvider

String getInfuraSecret();

String getTSInfuraKey();

String getUnstoppableDomainsKey();

String getOkLinkKey();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ public KeyProviderJNIImpl()

public native String getInfuraKey();

public native String getTSInfuraKey();

public native String getSecondaryInfuraKey();

public native String getTertiaryInfuraKey();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -675,10 +675,6 @@ public Single<TokenTicker> 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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -1217,7 +1233,7 @@ private Single<TokenDefinition> 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));
Expand Down Expand Up @@ -3109,7 +3125,17 @@ public Single<TokenDefinition> 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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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.
}

Expand Down Expand Up @@ -677,7 +679,6 @@ public void onPause()
{
super.onPause();
viewModel.resetSignDialog();
tokenView.destroy();
}

public void onSaveInstanceState(@NonNull Bundle savedInstanceState)
Expand Down Expand Up @@ -937,6 +938,7 @@ public void dismissed(String txHash, long callbackId, boolean actionCompleted)
else
{
setResult(RESULT_CANCELED, intent);
tokenView.destroy();
finish();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 0 additions & 6 deletions app/src/main/java/com/alphawallet/app/web3/Web3TokenView.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ public String getTertiaryInfuraKey()
return null;
}

@Override
public String getTSInfuraKey() { return null; }

@Override
public String getRampKey()
{
Expand Down

0 comments on commit de8f1ef

Please sign in to comment.