Skip to content

Commit

Permalink
Refactor and simplify setup of jsInject
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesSmartCell committed Nov 1, 2023
1 parent 1c010ab commit 253fba1
Show file tree
Hide file tree
Showing 16 changed files with 45 additions and 100 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);

Expand All @@ -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
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Expand All @@ -745,7 +745,7 @@ public void switchNetworkAndLoadUrl(long chainId, String url)
updateNetworkMenuItem();
viewModel.setNetwork(chainId);
startBalanceListener();
setupWeb3();
setupWeb3(wallet);
web3.resetView();
web3.reload();
}
Expand Down Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,18 @@ public class BaseTicketHolder extends BinderViewHolder<TicketRange> 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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -416,12 +416,6 @@ public Single<GasEstimate> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
35 changes: 13 additions & 22 deletions app/src/main/java/com/alphawallet/app/web3/JsInjectorClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,13 @@ public class JsInjectorClient {
private static final String DEFAULT_MIME_TYPE = "text/html";
private final static String JS_TAG_TEMPLATE = "<script type=\"text/javascript\">%1$s%2$s</script>";

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() {
Expand All @@ -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)
Expand All @@ -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 <div> is not closed because it is closed in injectStyleAndWrap().
String wrapper = "<div id=\"token-card-" + tokenId.toString(10) + "\" class=\"token-card\">";
Expand Down Expand Up @@ -217,10 +214,4 @@ private String getContentTypeHeader(Response response) {
}
return contentType;
}

private OkHttpClient createHttpClient() {
return new OkHttpClient.Builder()
.cookieJar(new WebViewCookieJar())
.build();
}
}
8 changes: 2 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 @@ -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) {
Expand Down
5 changes: 0 additions & 5 deletions app/src/main/java/com/alphawallet/app/web3/Web3View.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 253fba1

Please sign in to comment.