Skip to content

Commit

Permalink
Update Wallet diagnostics
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesSmartCell committed May 19, 2024
1 parent 07de70b commit 51d7c72
Show file tree
Hide file tree
Showing 13 changed files with 171 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
import static com.alphawallet.ethereum.EthereumNetworkBase.GOERLI_ID;
import static com.alphawallet.ethereum.EthereumNetworkBase.HECO_ID;
import static com.alphawallet.ethereum.EthereumNetworkBase.HECO_RPC_URL;
import static com.alphawallet.ethereum.EthereumNetworkBase.HOLESKY_FALLBACK_URL;
import static com.alphawallet.ethereum.EthereumNetworkBase.HOLESKY_ID;
import static com.alphawallet.ethereum.EthereumNetworkBase.HOLESKY_RPC_URL;
import static com.alphawallet.ethereum.EthereumNetworkBase.IOTEX_MAINNET_ID;
Expand Down Expand Up @@ -752,7 +751,7 @@ else if (isTestnet && !testnetList.contains(info.chainId))

private void checkCustomNetworkSetting()
{
if (list.size() > 0 && !list.get(0).isCustom)
if (!list.isEmpty() && !list.get(0).isCustom)
{ //need to update the list
List<NetworkInfo> copyList = new ArrayList<>(list);
list.clear();
Expand Down Expand Up @@ -902,7 +901,7 @@ private void addNetworks(List<NetworkInfo> result, boolean withValue)

public static String getChainOverrideAddress(long chainId)
{
return addressOverride.containsKey(chainId) ? addressOverride.get(chainId) : "";
return addressOverride.getOrDefault(chainId, "");
}

@Override
Expand Down Expand Up @@ -969,7 +968,7 @@ public List<Long> getSelectedFilters()
if (check != null) selectedIds.add(networkId);
}

if (selectedIds.size() == 0)
if (selectedIds.isEmpty())
{
selectedIds.add(getDefaultNetwork());
}
Expand Down Expand Up @@ -1337,14 +1336,7 @@ public static String getChainSymbol(long chainId)

public static boolean isEventBlockLimitEnforced(long chainId)
{
if (chainId == POLYGON_ID || chainId == POLYGON_TEST_ID || chainId == POLYGON_AMOY_ID)
{
return true;
}
else
{
return false;
}
return chainId == POLYGON_ID || chainId == POLYGON_TEST_ID || chainId == POLYGON_AMOY_ID;
}

public static BigInteger getMaxEventFetch(long chainId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,11 @@
import com.alphawallet.app.util.Utils;
import com.alphawallet.app.viewmodel.BackupKeyViewModel;
import com.alphawallet.app.widget.AWalletAlertDialog;
import com.alphawallet.app.widget.CopyTextView;
import com.alphawallet.app.widget.FunctionButtonBar;
import com.alphawallet.app.widget.SignTransactionDialog;

import org.web3j.crypto.WalletUtils;
import org.web3j.utils.Numeric;
import com.fasterxml.jackson.databind.ObjectMapper;

Expand All @@ -40,6 +43,7 @@
import org.web3j.crypto.WalletFile;

import java.io.File;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Collections;
import java.util.regex.Matcher;
Expand Down Expand Up @@ -354,11 +358,21 @@ private void testKeyStore()
}
}

private void exposeStatus()
{
LinearLayout llStatus = findViewById(R.id.layout_status);
llStatus.setVisibility(View.VISIBLE);
CopyTextView pkView = findViewById(R.id.copy_pk);
pkView.setVisibility(View.GONE);
}

private boolean testKeyType(String keyData)
{
//could either be a seed phrase or a keystore
Pattern pattern = Pattern.compile(ImportSeedFragment.validator, Pattern.MULTILINE);
TextView status = findViewById(R.id.status_txt);
TextView status = findViewById(R.id.text_status);
CopyTextView pubKeyText = findViewById(R.id.copy_public);
exposeStatus();

//first check for seed phrase
final Matcher matcher = pattern.matcher(keyData);
Expand All @@ -371,9 +385,14 @@ private boolean testKeyType(String keyData)
//is valid seed phrase
HDWallet newWallet = new HDWallet(keyData, "");
PrivateKey pk = newWallet.getKeyForCoin(CoinType.ETHEREUM);

status.setText(getString(R.string.seed_phrase_public_key, Numeric.toHexString(pk.getPublicKeySecp256k1(false).data())));
status.setText(R.string.seed_phrase_public_key);
status.setTextColor(getColor(R.color.green));
pubKeyText.setText(Numeric.toHexString(pk.getPublicKeySecp256k1(false).data()));

CopyTextView pkView = findViewById(R.id.copy_pk);
pkView.setVisibility(View.VISIBLE);
String pkStr = (new BigInteger(1, pk.data())).toString(16);
pkView.setFixedText(pkStr);
isSeedPhrase = true;
return true;
}
Expand All @@ -393,7 +412,15 @@ private boolean testKeyType(String keyData)
}
else
{
status.setText(getString(R.string.keystore_public_key, credentials.getEcKeyPair().getPublicKey().toString(16)));
status.setText(R.string.keystore_public_key);
status.setTextColor(getColor(R.color.green));
pubKeyText.setText(credentials.getEcKeyPair().getPublicKey().toString(16));

//show PK
CopyTextView pkView = findViewById(R.id.copy_pk);
pkView.setVisibility(View.VISIBLE);
String pk = credentials.getEcKeyPair().getPrivateKey().toString(16);
pkView.setFixedText(pk);
isKeyStore = true;
return true;
}
Expand Down Expand Up @@ -470,7 +497,7 @@ private String dumpKeystoreFromSeedPhrase(String seedPhrase, String keystorePass

private void showError(String error)
{
TextView statusTxt = findViewById(R.id.status_txt);
TextView statusTxt = findViewById(R.id.text_status);
statusTxt.setText(error);
statusTxt.setTextColor(getColor(R.color.danger));
if (dialog != null && dialog.isShowing()) dialog.dismiss();
Expand Down
44 changes: 39 additions & 5 deletions app/src/main/java/com/alphawallet/app/util/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import com.alphawallet.token.entity.Signable;
import com.google.gson.Gson;
import com.google.zxing.client.android.Intents;
import com.google.zxing.common.StringUtils;
import com.journeyapps.barcodescanner.ScanOptions;

import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -619,13 +620,41 @@ public static String formatAddress(String address, int frontCharCount)
}
}

public static String splitAddress(String address)
public static String splitAddress(String address, int lines)
{
address = Keys.toChecksumAddress(address);
int split = address.length()/2;
String front = address.substring(0, split);
String back = address.substring(split);
return front + " " + back;
return splitHex(address, lines);
}

public static String splitHex(String hex, int lines)
{
int split = hex.length()/lines;
StringBuilder sb = new StringBuilder();
int index = 0;
int addend = 0;
for (int i = 0; i < (lines-1); i++)
{
addend = 0;
if (index > 0)
{
sb.append(" ");
}
else
{
if (lines%2 != 0)
{
addend = 1;
}
}
sb.append(hex.substring(0, split + addend));
index += split;
hex = hex.substring(split + addend);
}
sb.append(" ");
sb.append(hex);
//String front = hex.substring(0, split);
//String back = hex.substring(split);
return sb.toString();
}

public static String formatTxHash(String txHash)
Expand Down Expand Up @@ -1522,4 +1551,9 @@ public static String parseResponseValue(@Nullable String metaDataURI, BigInteger
return metaDataURI;
}
}

public static boolean isDivisibleString(String originalText)
{
return !TextUtils.isEmpty(originalText) && originalText.length() <= 64;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.alphawallet.token.entity.SignMessageType;
import com.alphawallet.token.entity.Signable;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;

import java.util.ArrayList;
import java.util.Collections;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,12 @@ private void getAttrs(Context context, AttributeSet attrs)

private void bindViews()
{
if (lines == 2)
if (lines > 1)
{
button = findViewById(R.id.button_address);
findViewById(R.id.button).setVisibility(View.GONE);
button.setVisibility(View.VISIBLE);
button.setLines(lines);
}
else
{
Expand All @@ -95,7 +96,11 @@ public void setFixedText(CharSequence text)

if (Utils.isAddressValid(originalText))
{
button.setText(Utils.splitAddress(originalText));
button.setText(Utils.splitAddress(originalText, lines));
}
else if (Utils.isDivisibleString(originalText))
{
button.setText(Utils.splitHex(originalText, lines));
}
else if (Utils.isTxHashValid(originalText))
{
Expand Down
87 changes: 74 additions & 13 deletions app/src/main/res/layout/activity_wallet_diagnostic.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:custom="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:orientation="vertical">

<!-- NB: Wrapped in RelativeLayout to enable the 'big green success tick' -->
<LinearLayout
android:id="@+id/main_block"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<include layout="@layout/layout_simple_toolbar" />
Expand Down Expand Up @@ -92,32 +94,92 @@

</LinearLayout>

<LinearLayout
</LinearLayout>
</LinearLayout>

<ScrollView
android:id="@+id/scroll_status"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@id/layoutButtons"
android:layout_below="@id/main_block">

<LinearLayout
android:id="@+id/layout_status"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
android:orientation="vertical"
android:visibility="gone"
tools:visibility="visible">

<View style="@style/Aw.Component.Separator" />
<View style="@style/Aw.Component.Separator" />

<TextView
android:layout_margin="@dimen/standard_16"
<TextView
android:id="@+id/text_status"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/standard_16"
android:text="@string/status" />

<TextView
android:id="@+id/status_txt"
<com.alphawallet.app.widget.CopyTextView
android:id="@+id/copy_public"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:visibility="gone"
custom:bold="true"
custom:removePadding="true"
custom:text="@string/ethereum" />

<View style="@style/Aw.Component.Separator" />

<TextView
android:id="@+id/title_pk"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/standard_16"
android:text="@string/tab_private_key" />

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/standard_16"
android:orientation="horizontal"
tools:ignore="UseCompoundDrawables">

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginEnd="@dimen/dp5"
android:contentDescription="@string/tab_private_key"
android:src="@drawable/ic_red_warning" />

<TextView
android:id="@+id/title_sub_pk"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/key_secure" />

</LinearLayout>

<View style="@style/Aw.Component.Separator" />
<com.alphawallet.app.widget.CopyTextView
android:id="@+id/copy_pk"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/dp5"
android:gravity="center"
android:visibility="gone"
custom:bold="true"
custom:lines="3"
custom:removePadding="true"
custom:text="@string/ethereum" />

</LinearLayout>

</LinearLayout>
</ScrollView>



<LinearLayout
android:id="@+id/layout_success_overlay"
Expand All @@ -138,7 +200,6 @@

</LinearLayout>


<com.alphawallet.app.widget.FunctionButtonBar
android:id="@+id/layoutButtons"
android:layout_width="match_parent"
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/res/values-es/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -912,8 +912,8 @@
<string name="key_diagnostic">Key Diagnostic</string>
<string name="key_found">Key found</string>
<string name="key_not_found">Unable to find Key</string>
<string name="seed_phrase_public_key">Seed Phrase detected public key: %1$s</string>
<string name="keystore_public_key">Decoded Keystore public key: %1$s</string>
<string name="seed_phrase_public_key">Seed Phrase detected public key</string>
<string name="keystore_public_key">Decoded Keystore public key</string>
<string name="enclave_locked">Locked</string>
<string name="key_type_in_database">Key type in Database</string>
<string name="key_entry_in_secure_enclave">Key Entry in Secure Enclave</string>
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/res/values-fr/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -926,8 +926,8 @@
<string name="key_diagnostic">Key Diagnostic</string>
<string name="key_found">Key found</string>
<string name="key_not_found">Unable to find Key</string>
<string name="seed_phrase_public_key">Seed Phrase detected public key: %1$s</string>
<string name="keystore_public_key">Decoded Keystore public key: %1$s</string>
<string name="seed_phrase_public_key">Seed Phrase detected public key</string>
<string name="keystore_public_key">Decoded Keystore public key</string>
<string name="enclave_locked">Locked</string>
<string name="key_type_in_database">Key type in Database</string>
<string name="key_entry_in_secure_enclave">Key Entry in Secure Enclave</string>
Expand Down
Loading

0 comments on commit 51d7c72

Please sign in to comment.