Skip to content

Commit 7a32ec0

Browse files
authored
Merge pull request #136 from KeystoneHQ/refactor
fix tx display code not correct
2 parents 3102522 + 298808c commit 7a32ec0

File tree

4 files changed

+22
-24
lines changed

4 files changed

+22
-24
lines changed

app/src/main/java/com/keystone/cold/ui/fragment/main/TxFragment.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ private void refreshReceiveList() {
163163
JSONArray outputs = new JSONArray(to);
164164
for (int i = 0; i < outputs.length(); i++) {
165165
JSONObject output = outputs.getJSONObject(i);
166-
if (output.optBoolean("isChange") && !(Coins.isBTCMainnet(txEntity.getCoinCode()) || Coins.isBTCTestnet(txEntity.getCoinCode()))) {
166+
if (output.optBoolean("isChange") && !Coins.isBTCFamily(txEntity.getCoinCode())) {
167167
continue;
168168
}
169169
long value;
@@ -179,7 +179,7 @@ private void refreshReceiveList() {
179179
items.add(new TransactionItem(i,
180180
value,
181181
output.getString("address"),
182-
txEntity.getCoinCode()
182+
txEntity.getDisplayName()
183183
));
184184
if (output.optBoolean("isChange", false)) {
185185
changeAddresses.add(output.getString("address"));
@@ -189,7 +189,7 @@ private void refreshReceiveList() {
189189
return;
190190
}
191191
TransactionItemAdapter adapter;
192-
if (Coins.isBTCMainnet(txEntity.getCoinCode()) || Coins.isBTCTestnet(txEntity.getCoinCode())) {
192+
if (Coins.isBTCFamily(txEntity.getCoinCode())) {
193193
adapter = new TransactionItemAdapter(mActivity,
194194
TransactionItem.ItemType.OUTPUT, changeAddresses);
195195
} else {
@@ -205,15 +205,15 @@ private void refreshReceiveList() {
205205

206206
private void refreshFromList() {
207207
String from = txEntity.getFrom();
208-
if (txEntity.getCoinCode().startsWith("BTC")) {
208+
if (Coins.isBTCFamily(txEntity.getCoinCode())) {
209209
try {
210210
List<TransactionItem> items = new ArrayList<>();
211211
JSONArray inputs = new JSONArray(from);
212212
for (int i = 0; i < inputs.length(); i++) {
213213
items.add(new TransactionItem(i,
214214
0,
215215
inputs.getJSONObject(i).getString("address"),
216-
"BTC"
216+
txEntity.getDisplayName()
217217
));
218218
}
219219
TransactionItemAdapter adapter = new TransactionItemAdapter(mActivity,

app/src/main/java/com/keystone/cold/ui/fragment/main/keystone/TxConfirmFragment.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ private void subscribeTxEntityState() {
153153
if (txEntity != null) {
154154
this.txEntity = txEntity;
155155
mBinding.setTx(txEntity);
156-
if (Coins.isBTCMainnet(txEntity.getCoinCode()) || Coins.isBTCTestnet(txEntity.getCoinCode())) {
156+
if (Coins.isBTCFamily(txEntity.getCoinCode())) {
157157
mBinding.txDetail.fromRow.setVisibility(View.GONE);
158158
mBinding.txDetail.arrowDown.setVisibility(View.GONE);
159159
}
@@ -282,28 +282,32 @@ private void refreshReceiveList() {
282282
} else {
283283
mBinding.txDetail.info.setText(to.replace(",", "\n\n"));
284284
}
285+
List<String> changeAddresses = new ArrayList<>();
285286
List<TransactionItem> items = new ArrayList<>();
286287

287288
try {
288289
JSONArray outputs = new JSONArray(to);
289290
for (int i = 0; i < outputs.length(); i++) {
290291
JSONObject output = outputs.getJSONObject(i);
291-
if (output.optBoolean("isChange") && !(Coins.isBTCMainnet(txEntity.getCoinCode()) || Coins.isBTCTestnet(txEntity.getCoinCode()))) {
292+
if (output.optBoolean("isChange") && !Coins.isBTCFamily(txEntity.getCoinCode())) {
292293
continue;
293294
}
294295
items.add(new TransactionItem(i,
295296
output.getLong("value"),
296297
output.getString("address"),
297-
txEntity.getCoinCode()
298+
txEntity.getDisplayName()
298299
));
300+
if (output.optBoolean("isChange", false)) {
301+
changeAddresses.add(output.getString("address"));
302+
}
299303
}
300304
} catch (JSONException e) {
301305
return;
302306
}
303307
TransactionItemAdapter adapter;
304-
if (Coins.isBTCMainnet(txEntity.getCoinCode()) || Coins.isBTCTestnet(txEntity.getCoinCode())) {
308+
if (Coins.isBTCFamily(txEntity.getCoinCode())) {
305309
adapter = new TransactionItemAdapter(mActivity,
306-
TransactionItem.ItemType.OUTPUT, viewModel.getChangeAddresses());
310+
TransactionItem.ItemType.OUTPUT, changeAddresses);
307311
}
308312
else {
309313
adapter = new TransactionItemAdapter(mActivity,
@@ -326,7 +330,7 @@ private void refreshFromList() {
326330
items.add(new TransactionItem(i,
327331
0,
328332
inputs.getJSONObject(i).getString("address"),
329-
txEntity.getCoinCode()
333+
txEntity.getDisplayName()
330334
));
331335
}
332336
TransactionItemAdapter adapter = new TransactionItemAdapter(mActivity,

app/src/main/java/com/keystone/cold/viewmodel/tx/KeystoneTxViewModel.java

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@
7070

7171
import java.security.SignatureException;
7272
import java.text.NumberFormat;
73-
import java.util.ArrayList;
7473
import java.util.Arrays;
7574
import java.util.HashMap;
7675
import java.util.List;
@@ -94,8 +93,6 @@ public class KeystoneTxViewModel extends Base {
9493
private static String BTC_TESTNET_LEGACY_PATH = "M/44'/1'/0'/";
9594
private static String BTC_TESTNET_NATIVE_SEGWIT_PATH = "M/84'/1'/0'/";
9695

97-
private List<String> changeAddresses = new ArrayList<>();
98-
9996
public KeystoneTxViewModel(@NonNull Application application) {
10097
super(application);
10198
watchWallet = WatchWallet.getWatchWallet(application);
@@ -107,16 +104,11 @@ public MutableLiveData<TxEntity> getObservableTx() {
107104
return observableTx;
108105
}
109106

110-
public List<String> getChangeAddresses() {
111-
return changeAddresses;
112-
}
113-
114107
public MutableLiveData<Exception> parseTxException() {
115108
return parseTxException;
116109
}
117110

118111
public void parseTxData(String json) {
119-
changeAddresses = new ArrayList<>();
120112
AppExecutors.getInstance().diskIO().execute(() -> {
121113
try {
122114
JSONObject object = new JSONObject(json);
@@ -129,9 +121,9 @@ public void parseTxData(String json) {
129121
}
130122
TxEntity tx = generateTxEntity(object);
131123
observableTx.postValue(tx);
132-
if (Coins.isBTCTestnet(transaction.getCoinCode()) || Coins.isBTCMainnet(transaction.getCoinCode())) {
124+
if (Coins.isBTCFamily(transaction.getCoinCode())) {
133125
feeAttackChecking(tx);
134-
if (!checkAndSetBTCChangeAddress((UtxoTx) transaction)) {
126+
if (!checkBTCChangeAddress((UtxoTx) transaction)) {
135127
parseTxException.postValue(new InvalidTransactionException("invalid change address"));
136128
}
137129
} else {
@@ -170,7 +162,7 @@ private boolean checkChangeAddress(AbsTx utxoTx) {
170162
}
171163
}
172164

173-
private boolean checkAndSetBTCChangeAddress(UtxoTx tx) {
165+
private boolean checkBTCChangeAddress(UtxoTx tx) {
174166
boolean flag = true;
175167
for (UtxoTx.ChangeAddressInfo changeAddress :
176168
tx.getChangeAddressInfoList()) {
@@ -188,8 +180,6 @@ private boolean checkAndSetBTCChangeAddress(UtxoTx tx) {
188180
String derivedAddress = deriver.derive(change, addressIndex);
189181
if (!derivedAddress.equalsIgnoreCase(address)) {
190182
flag = false;
191-
} else {
192-
changeAddresses.add(derivedAddress);
193183
}
194184
}
195185
}

coinlib/src/main/java/com/keystone/coinlib/utils/Coins.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,10 @@ public static boolean supportMultiSigner(@NonNull String coinCode) {
9898
}
9999
}
100100

101+
public static boolean isBTCFamily(@NonNull String coinCode) {
102+
return isBTCTestnet(coinCode) || isBTCMainnet(coinCode);
103+
}
104+
101105
public static boolean isBTCMainnet(@NonNull String coinCode) {
102106
switch (coinCode) {
103107
case "BTC":

0 commit comments

Comments
 (0)