Cleanup code, fix bugs, etc

This commit is contained in:
pokkst 2022-09-22 16:35:53 -05:00
parent 2ebc828d3e
commit 998836ebd1
No known key found for this signature in database
GPG key ID: 90C2ED85E67A50FF
8 changed files with 62 additions and 34 deletions

View file

@ -1083,14 +1083,17 @@ Java_net_mynero_wallet_model_Wallet_getCoinsJ(JNIEnv *env, jobject instance) {
jobject newCoinsInfo(JNIEnv *env, Monero::CoinsInfo *info) { jobject newCoinsInfo(JNIEnv *env, Monero::CoinsInfo *info) {
jmethodID c = env->GetMethodID(class_CoinsInfo, "<init>", jmethodID c = env->GetMethodID(class_CoinsInfo, "<init>",
"(JZLjava/lang/String;J)V"); "(JZLjava/lang/String;JLjava/lang/String;)V");
jstring _key_image = env->NewStringUTF(info->keyImage().c_str()); jstring _key_image = env->NewStringUTF(info->keyImage().c_str());
jstring _hash = env->NewStringUTF(info->hash().c_str());
jobject result = env->NewObject(class_CoinsInfo, c, jobject result = env->NewObject(class_CoinsInfo, c,
static_cast<jlong> (info->globalOutputIndex()), static_cast<jlong> (info->globalOutputIndex()),
info->spent(), info->spent(),
_key_image, _key_image,
static_cast<jlong> (info->amount())); static_cast<jlong> (info->amount()),
_hash);
env->DeleteLocalRef(_key_image); env->DeleteLocalRef(_key_image);
env->DeleteLocalRef(_hash);
return result; return result;
} }

View file

@ -52,14 +52,6 @@ public class ReceiveBottomSheetDialog extends BottomSheetDialogFragment {
addressTextView.setText(addr.getAddress()); addressTextView.setText(addr.getAddress());
addressImageView.setImageBitmap(generate(addr.getAddress(), 256, 256)); addressImageView.setImageBitmap(generate(addr.getAddress(), 256, 256));
copyAddressImageButton.setOnClickListener(view1 -> Helper.clipBoardCopy(getContext(), "address", addr.getAddress())); copyAddressImageButton.setOnClickListener(view1 -> Helper.clipBoardCopy(getContext(), "address", addr.getAddress()));
List<CoinsInfo> coins = WalletManager.getInstance().getWallet().getCoins().getAll();
System.out.println("COINS::");
for(CoinsInfo coinsInfo : coins) {
if(!coinsInfo.isSpent()) {
System.out.println(coinsInfo.getGlobalOutputIndex()+": "+coinsInfo.getKeyImage());
}
}
} }
public Bitmap generate(String text, int width, int height) { public Bitmap generate(String text, int width, int height) {

View file

@ -254,6 +254,7 @@ public class SendBottomSheetDialog extends BottomSheetDialogFragment {
private void createTx(String address, String amount, boolean sendAll, PendingTransaction.Priority feePriority) { private void createTx(String address, String amount, boolean sendAll, PendingTransaction.Priority feePriority) {
AsyncTask.execute(() -> { AsyncTask.execute(() -> {
try {
PendingTransaction pendingTx = TxService.getInstance().createTx(address, amount, sendAll, feePriority, selectedUtxos); PendingTransaction pendingTx = TxService.getInstance().createTx(address, amount, sendAll, feePriority, selectedUtxos);
if (pendingTx != null && pendingTx.getStatus() == PendingTransaction.Status.Status_Ok) { if (pendingTx != null && pendingTx.getStatus() == PendingTransaction.Status.Status_Ok) {
_pendingTransaction.postValue(pendingTx); _pendingTransaction.postValue(pendingTx);
@ -266,6 +267,15 @@ public class SendBottomSheetDialog extends BottomSheetDialogFragment {
}); });
} }
} }
} catch (Exception e) {
Activity activity = getActivity();
if (activity != null) {
activity.runOnUiThread(() -> {
createButton.setEnabled(true);
Toast.makeText(getActivity(), e.getMessage(), Toast.LENGTH_SHORT).show();
});
}
}
}); });
} }

View file

@ -78,10 +78,8 @@ public class UtxosFragment extends Fragment implements CoinsInfoAdapter.CoinsInf
public void onUtxoSelected(CoinsInfo coinsInfo) { public void onUtxoSelected(CoinsInfo coinsInfo) {
boolean selected = selectedUtxos.contains(coinsInfo.getKeyImage()); boolean selected = selectedUtxos.contains(coinsInfo.getKeyImage());
if(selected) { if(selected) {
System.out.println("Deselecting: " + coinsInfo.getKeyImage());
selectedUtxos.remove(coinsInfo.getKeyImage()); selectedUtxos.remove(coinsInfo.getKeyImage());
} else { } else {
System.out.println("Selecting: " + coinsInfo.getKeyImage());
selectedUtxos.add(coinsInfo.getKeyImage()); selectedUtxos.add(coinsInfo.getKeyImage());
} }

View file

@ -33,12 +33,14 @@ public class CoinsInfo implements Parcelable {
boolean spent; boolean spent;
String keyImage; String keyImage;
long amount; long amount;
String hash;
public CoinsInfo(long globalOutputIndex, boolean spent, String keyImage, long amount) { public CoinsInfo(long globalOutputIndex, boolean spent, String keyImage, long amount, String hash) {
this.globalOutputIndex = globalOutputIndex; this.globalOutputIndex = globalOutputIndex;
this.spent = spent; this.spent = spent;
this.keyImage = keyImage; this.keyImage = keyImage;
this.amount = amount; this.amount = amount;
this.hash = hash;
} }
protected CoinsInfo(Parcel in) { protected CoinsInfo(Parcel in) {
@ -69,6 +71,10 @@ public class CoinsInfo implements Parcelable {
return keyImage; return keyImage;
} }
public String getHash() {
return hash;
}
public long getAmount() { public long getAmount() {
return amount; return amount;
} }

View file

@ -17,11 +17,10 @@
package net.mynero.wallet.service; package net.mynero.wallet.service;
import static net.mynero.wallet.model.Wallet.SWEEP_ALL;
import net.mynero.wallet.data.DefaultNodes; import net.mynero.wallet.data.DefaultNodes;
import net.mynero.wallet.data.Node; import net.mynero.wallet.data.Node;
import net.mynero.wallet.data.TxData; import net.mynero.wallet.data.TxData;
import net.mynero.wallet.model.CoinsInfo;
import net.mynero.wallet.model.PendingTransaction; import net.mynero.wallet.model.PendingTransaction;
import net.mynero.wallet.model.Wallet; import net.mynero.wallet.model.Wallet;
import net.mynero.wallet.model.WalletListener; import net.mynero.wallet.model.WalletListener;
@ -122,17 +121,33 @@ public class MoneroHandlerThread extends Thread implements WalletListener {
listener.onRefresh(); listener.onRefresh();
} }
public PendingTransaction createTx(String address, String amountStr, boolean sendAll, PendingTransaction.Priority feePriority, ArrayList<String> selectedUtxos) { public PendingTransaction createTx(String address, String amountStr, boolean sendAll, PendingTransaction.Priority feePriority, ArrayList<String> selectedUtxos) throws Exception {
long amount = sendAll ? SWEEP_ALL : Wallet.getAmountFromString(amountStr); long amount = sendAll ? Wallet.SWEEP_ALL : Wallet.getAmountFromString(amountStr);
ArrayList<String> preferredInputs; ArrayList<String> preferredInputs;
if(selectedUtxos.isEmpty()) { if(selectedUtxos.isEmpty()) {
preferredInputs = UTXOService.getInstance().selectUtxos(amount, sendAll); preferredInputs = UTXOService.getInstance().selectUtxos(amount, sendAll);
} else { } else {
preferredInputs = selectedUtxos; preferredInputs = selectedUtxos;
checkSelectedAmounts(selectedUtxos, amount, sendAll);
} }
return wallet.createTransaction(new TxData(address, amount, 0, feePriority, preferredInputs)); return wallet.createTransaction(new TxData(address, amount, 0, feePriority, preferredInputs));
} }
private void checkSelectedAmounts(ArrayList<String> selectedUtxos, long amount, boolean sendAll) throws Exception {
if(!sendAll) {
long amountSelected = 0;
for(CoinsInfo coinsInfo : UTXOService.getInstance().getUtxos()) {
if(selectedUtxos.contains(coinsInfo.getKeyImage())) {
amountSelected += coinsInfo.getAmount();
}
}
if(amountSelected <= amount) {
throw new Exception("insufficient wallet balance");
}
}
}
public boolean sendTx(PendingTransaction pendingTx) { public boolean sendTx(PendingTransaction pendingTx) {
return pendingTx.commit("", true); return pendingTx.commit("", true);
} }

View file

@ -16,7 +16,7 @@ public class TxService extends ServiceBase {
return instance; return instance;
} }
public PendingTransaction createTx(String address, String amount, boolean sendAll, PendingTransaction.Priority feePriority, ArrayList<String> selectedUtxos) { public PendingTransaction createTx(String address, String amount, boolean sendAll, PendingTransaction.Priority feePriority, ArrayList<String> selectedUtxos) throws Exception {
return this.getThread().createTx(address, amount, sendAll, feePriority, selectedUtxos); return this.getThread().createTx(address, amount, sendAll, feePriority, selectedUtxos);
} }

View file

@ -33,28 +33,32 @@ public class UTXOService extends ServiceBase {
return WalletManager.getInstance().getWallet().getCoins().getAll(); return WalletManager.getInstance().getWallet().getCoins().getAll();
} }
public ArrayList<String> selectUtxos(long amount, boolean sendAll) { public ArrayList<String> selectUtxos(long amount, boolean sendAll) throws Exception {
ArrayList<String> selectedUtxos = new ArrayList<>(); ArrayList<String> selectedUtxos = new ArrayList<>();
ArrayList<String> seenTxs = new ArrayList<>();
List<CoinsInfo> utxos = getUtxos(); List<CoinsInfo> utxos = getUtxos();
if(sendAll) {
for(CoinsInfo coinsInfo : utxos) {
selectedUtxos.add(coinsInfo.getKeyImage());
}
} else {
long amountSelected = 0; long amountSelected = 0;
Collections.shuffle(utxos); Collections.shuffle(utxos);
for (CoinsInfo coinsInfo : utxos) { for (CoinsInfo coinsInfo : utxos) {
if (amount == Wallet.SWEEP_ALL) { if(!coinsInfo.isSpent()) {
if (sendAll) {
selectedUtxos.add(coinsInfo.getKeyImage()); selectedUtxos.add(coinsInfo.getKeyImage());
amountSelected = Wallet.SWEEP_ALL;
} else { } else {
if (amountSelected <= amount) { if (amountSelected <= amount && !seenTxs.contains(coinsInfo.getHash())) {
selectedUtxos.add(coinsInfo.getKeyImage()); selectedUtxos.add(coinsInfo.getKeyImage());
// we don't want to spend multiple utxos from the same transaction, so we prevent that from happening here.
seenTxs.add(coinsInfo.getHash());
amountSelected += coinsInfo.getAmount(); amountSelected += coinsInfo.getAmount();
} }
} }
} }
} }
if (amountSelected <= amount && !sendAll) {
throw new Exception("insufficient wallet balance");
}
return selectedUtxos; return selectedUtxos;
} }
} }