Skip to content

Commit

Permalink
fix: various fixes for chain ID
Browse files Browse the repository at this point in the history
  • Loading branch information
micwallace committed Jan 31, 2024
1 parent 9ca0b18 commit d7ce205
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -819,7 +819,7 @@ private void displayCloseWC()
private void setupWeb3(Wallet wallet)
{
if (wallet == null) { return; }
web3.setChainId(activeNetwork.chainId);
web3.setChainId(activeNetwork.chainId, false);
web3.setWalletAddress(new Address(wallet.address));

web3.setWebChromeClient(new WebChromeClient()
Expand Down
47 changes: 12 additions & 35 deletions app/src/main/java/com/alphawallet/app/ui/TokenScriptJsActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ public class TokenScriptJsActivity extends BaseActivity implements StandardFunct
private AWalletAlertDialog errorDialog;
private AddEthereumChainPrompt addCustomChainDialog;

private static String VIEWER_URL = //"https://viewer.tokenscript.org";
"http://192.168.1.15:3333";

@Override
protected void onCreate(@Nullable Bundle savedInstanceState)
{
Expand Down Expand Up @@ -247,8 +250,10 @@ private void initViewModel()
.get(TokenFunctionViewModel.class);
tsViewModel.sig().observe(this, this::onSignature);

getIntentData();

viewModel.setNetwork(chainId);
activeNetwork = viewModel.getActiveNetwork();
activeNetwork = viewModel.getNetworkInfo(chainId);

viewModel.findWallet();
}
Expand Down Expand Up @@ -427,7 +432,7 @@ private void openTokenscriptWebview(Wallet wallet)
tokenScriptView.getSettings().setSupportMultipleWindows(true);

tokenScriptView.setWebViewClient(new WebViewClient());
tokenScriptView.setChainId(activeNetwork.chainId);
tokenScriptView.setChainId(activeNetwork.chainId, false);
tokenScriptView.setWalletAddress(new Address(wallet.address));

tokenScriptView.setOnSignMessageListener(this);
Expand All @@ -439,7 +444,7 @@ private void openTokenscriptWebview(Wallet wallet)
tokenScriptView.setOnWalletActionListener(this);

tokenScriptView.resetView();
tokenScriptView.loadUrl("http://192.168.1.15:3333/?viewType=alphawallet&chain=" + chainId + "&contract=" + token.tokenInfo.address + "&tokenId=" + tokenId);
tokenScriptView.loadUrl(VIEWER_URL + "/?viewType=alphawallet&chain=" + chainId + "&contract=" + token.tokenInfo.address + "&tokenId=" + tokenId);
}
catch (Exception e)
{
Expand Down Expand Up @@ -559,21 +564,10 @@ private void changeChainRequest(long callbackId, NetworkInfo info)
return;
}

//if we're switching between mainnet and testnet we need to pop open the 'switch to testnet' dialog (class TestNetDialog)
// - after the user switches to testnet, go straight to switching the network (loadNewNetwork)
// - if user is switching form testnet to mainnet, simply add the title below

// at this stage, we know if it's testnet or not
/*if (!info.hasRealValue() && (activeNetwork != null && activeNetwork.hasRealValue()))
{
TestNetDialog testnetDialog = new TestNetDialog(this, info.chainId, this);
testnetDialog.show();
}
else
{*/
//go straight to chain change dialog
showChainChangeDialog(callbackId, info);
//}
activeNetwork = info;
tokenScriptView.setChainId(info.chainId, true);
viewModel.setNetwork(info.chainId);
tokenScriptView.onWalletActionSuccessful(callbackId, null);
}

@Override
Expand Down Expand Up @@ -617,23 +611,6 @@ public void onWalletSwitchEthereumChain(long callbackId, WalletAddEthereumChainO
}
}

/**
* This will pop the ActionSheetDialog to request a chain change, with appropriate warning
* if switching between mainnets and testnets
*
* @param callbackId
* @param newNetwork
*/
private void showChainChangeDialog(long callbackId, NetworkInfo newNetwork)
{
Token baseToken = viewModel.getTokenService().getTokenOrBase(newNetwork.chainId, wallet.address);
confirmationDialog = new ActionSheetDialog(this, this, R.string.switch_chain_request, R.string.switch_and_reload,
callbackId, baseToken, activeNetwork, newNetwork);
confirmationDialog.setCanceledOnTouchOutside(true);
confirmationDialog.show();
confirmationDialog.fullExpand();
}

private void handleSignMessage(Signable message)
{
if (message.getMessageType() == SignMessageType.SIGN_TYPED_DATA_V3 && message.getChainId() != activeNetwork.chainId)
Expand Down
8 changes: 6 additions & 2 deletions app/src/main/java/com/alphawallet/app/web3/Web3View.java
Original file line number Diff line number Diff line change
Expand Up @@ -225,9 +225,13 @@ public long getChainId()
return webViewClient.getJsInjectorClient().getChainId();
}

public void setChainId(long chainId)
public void setChainId(long chainId, boolean isTokenscript)
{
webViewClient.getJsInjectorClient().setChainId(chainId);
if (isTokenscript){
webViewClient.getJsInjectorClient().setTSChainId(chainId);
} else {
webViewClient.getJsInjectorClient().setChainId(chainId);
}
}

public void setWebLoadCallback(URLLoadInterface iFace)
Expand Down

0 comments on commit d7ce205

Please sign in to comment.