Skip to content

Commit

Permalink
Merge pull request #2 from KeystoneHQ/decode_tfcard
Browse files Browse the repository at this point in the history
Decode tfcard
  • Loading branch information
soralit authored Jul 14, 2021
2 parents c002fda + cb1c67a commit ec55984
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import android.os.Handler;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
Expand All @@ -32,6 +33,7 @@
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProviders;

import com.keystone.cold.AppExecutors;
import com.keystone.cold.MainApplication;
import com.keystone.cold.R;
import com.keystone.cold.Utilities;
Expand All @@ -57,7 +59,6 @@
import static com.keystone.cold.callables.FingerprintPolicyCallable.READ;
import static com.keystone.cold.callables.FingerprintPolicyCallable.TYPE_SIGN_TX;
import static com.keystone.cold.ui.fragment.main.BroadcastTxFragment.KEY_TXID;
import static com.keystone.cold.ui.fragment.main.TxConfirmFragment.KEY_TX_DATA;
import static com.keystone.cold.ui.fragment.setup.PreImportFragment.ACTION;

public class EthTxConfirmFragment extends BaseFragment<EthTxConfirmBinding> {
Expand All @@ -83,26 +84,12 @@ protected int setView() {

@Override
protected void init(View view) {
Bundle data = requireArguments();
mBinding.ethTx.checkInfo.setVisibility(View.VISIBLE);
mBinding.toolbar.setNavigationOnClickListener(v -> navigateUp());
viewModel = ViewModelProviders.of(this).get(EthTxConfirmViewModel.class);
try {
JSONObject txData = new JSONObject(data.getString(KEY_TX_DATA));
viewModel.parseTxData(txData);
viewModel.getObservableTx().observe(this, txEntity -> {
this.txEntity = txEntity;
if (this.txEntity != null) {
updateUI();
}
});
viewModel.parseTxException().observe(this, this::handleParseException);
} catch (JSONException e) {
e.printStackTrace();
}
mBinding.sign.setOnClickListener(v -> handleSign());
mBinding.ethTx.info.setOnClickListener(view1 -> realShowDialog());
showDialog();
viewModel.parseTxData(requireArguments());
}

private void showDialog() {
Expand Down Expand Up @@ -198,6 +185,7 @@ private void updateUI() {
mBinding.ethTx.data.setVisibility(View.GONE);
mBinding.ethTx.undecodedData.setVisibility(View.VISIBLE);
mBinding.ethTx.inputData.setText("0x" + viewModel.getInputData());
showDialog();
}
mBinding.ethTx.setTx(txEntity);
processAndUpdateTo();
Expand All @@ -208,14 +196,17 @@ private void updateNetworkName() {
}

private void processAndUpdateTo() {
String to = txEntity.getTo();
String addressSymbol = viewModel.recognizeAddress(to);
if (addressSymbol != null) {
to = to + String.format(" (%s)", addressSymbol);
} else {
to = to + String.format(" [%s]", "Unknown Address");
}
mBinding.ethTx.to.setText(highLight(to));
AppExecutors.getInstance().diskIO().execute(() -> {
String to = txEntity.getTo();
String addressSymbol = viewModel.recognizeAddress(to);
if (!TextUtils.isEmpty(addressSymbol)) {
to = to + String.format(" (%s)", addressSymbol);
} else {
to = to + String.format(" [%s]", "Unknown Address");
}
String finalTo = to;
AppExecutors.getInstance().mainThread().execute(() -> mBinding.ethTx.to.setText(highLight(finalTo)));
});
}

private void updateAbiView(JSONObject abi) {
Expand All @@ -227,30 +218,42 @@ private void updateAbiView(JSONObject abi) {
}
String contract = abi.getString("contract");
boolean isUniswap = contract.toLowerCase().contains("uniswap");
List<AbiItemAdapter.AbiItem> itemList = new AbiItemAdapter(txEntity.getFrom(), viewModel).adapt(abi);
for (AbiItemAdapter.AbiItem item : itemList) {
AbiItemBinding binding = DataBindingUtil.inflate(LayoutInflater.from(mActivity),
R.layout.abi_item, null, false);
binding.key.setText(item.key);
if (isUniswap && "to".equals(item.key)) {
if (!item.value.equalsIgnoreCase(txEntity.getFrom())) {
item.value += String.format(" [%s]", getString(R.string.inconsistent_address));
}
binding.value.setText(highLight(item.value));
} else {
binding.value.setText(highLight(item.value));
}
mBinding.ethTx.container.addView(binding.getRoot());
}
AppExecutors.getInstance().diskIO().execute(() -> {
List<AbiItemAdapter.AbiItem> itemList = new AbiItemAdapter(txEntity.getFrom(), viewModel).adapt(abi);
AppExecutors.getInstance().mainThread().execute(() -> addViewToData(isUniswap, itemList));
});
} catch (JSONException e) {
e.printStackTrace();
}
}
}

private void addViewToData(boolean isUniswap, List<AbiItemAdapter.AbiItem> itemList) {
for (AbiItemAdapter.AbiItem item : itemList) {
AbiItemBinding binding = DataBindingUtil.inflate(LayoutInflater.from(mActivity),
R.layout.abi_item, null, false);
binding.key.setText(item.key);
if (isUniswap && "to".equals(item.key)) {
if (!item.value.equalsIgnoreCase(txEntity.getFrom())) {
item.value += String.format(" [%s]", getString(R.string.inconsistent_address));
}
binding.value.setText(highLight(item.value));
} else {
binding.value.setText(highLight(item.value));
}
mBinding.ethTx.container.addView(binding.getRoot());
}
}

@Override
protected void initData(Bundle savedInstanceState) {

viewModel.getObservableTx().observe(this, txEntity -> {
this.txEntity = txEntity;
if (this.txEntity != null) {
updateUI();
}
});
viewModel.parseTxException().observe(this, this::handleParseException);
}

public static SpannableStringBuilder highLight(String content) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import android.annotation.SuppressLint;
import android.app.Application;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;

Expand Down Expand Up @@ -55,6 +56,7 @@
import java.util.concurrent.CountDownLatch;

import static com.keystone.coinlib.v8.ScriptLoader.readAsset;
import static com.keystone.cold.ui.fragment.main.TxConfirmFragment.KEY_TX_DATA;

public class EthTxConfirmViewModel extends TxConfirmViewModel {
private final MutableLiveData<Boolean> addingAddress = new MutableLiveData<>();
Expand Down Expand Up @@ -116,6 +118,12 @@ public String recognizeAddress(String to) {
addressSymbol = recognizeAddressFromTFCard(to);
}
}
if (addressSymbol != null && addressSymbol.length() > 25) {
String abbreviationString = addressSymbol.substring(0, 10) +
"..." +
addressSymbol.substring(addressSymbol.length() - 10);
addressSymbol = abbreviationString;
}
return addressSymbol;
} catch (JSONException e) {
e.printStackTrace();
Expand Down Expand Up @@ -157,9 +165,10 @@ public MutableLiveData<Exception> parseTxException() {
return parseTxException;
}

public void parseTxData(JSONObject object) {
public void parseTxData(Bundle bundle) {
AppExecutors.getInstance().diskIO().execute(() -> {
try {
JSONObject object = new JSONObject(bundle.getString(KEY_TX_DATA));
Log.i(TAG, "object = " + object.toString(4));
hdPath = object.getString("hdPath");
signId = object.getString("signId");
Expand All @@ -179,7 +188,6 @@ public void parseTxData(JSONObject object) {
}
TxEntity tx = generateTxEntity(ethTx);
observableTx.postValue(tx);

} catch (JSONException e) {
e.printStackTrace();
}
Expand Down
6 changes: 3 additions & 3 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -554,10 +554,10 @@
<string name="calc_mnemonic_warning_checkbox">I fully understand entropy, BIP39, private key generation and other related material to generate my recovery phrase.</string>
<string name="note">DANGER!</string>
<string name="tip">Note</string>
<string name="learn_more"><Data><![CDATA[Keystone supports decoding transactions, you can go to<font color="#00CDC3"><b> https://keyst.one/eth </b></font>for details.]]></Data></string>
<string name="learn_more"><Data><![CDATA[Keystone supports the decoding of transactions. Please visit<font color="#00CDC3"><b> https://keyst.one/eth </b></font>for more details.]]></Data></string>
<string name="check_info">* Please check transaction details before signing</string>
<string name="undecoded_tip">Attention! It’s a undecoded data.</string>
<string name="from_tfcard">(Decoding resources are provided by MicroSD Card)</string>
<string name="undecoded_tip">Attention! This is undecoded data.</string>
<string name="from_tfcard">(Derived from your MicroSD Card)</string>
<string name="select_last_word_hint">Please choose one of the following words as the 24th word</string>
<string name="please_insert_sdcard">Please make sure you have inserted a microSD card</string>
<string name="confirm_format">Are you SURE ?</string>
Expand Down

0 comments on commit ec55984

Please sign in to comment.