use of org.bitcoinj.core.Transaction in project bitcoin-wallet by bitcoin-wallet.
the class SendCoinsFragment method onActivityResultResumed.
private void onActivityResultResumed(final int requestCode, final int resultCode, final Intent intent) {
if (requestCode == REQUEST_CODE_SCAN) {
if (resultCode == Activity.RESULT_OK) {
final String input = intent.getStringExtra(ScanActivity.INTENT_EXTRA_RESULT);
new StringInputParser(input) {
@Override
protected void handlePaymentIntent(final PaymentIntent paymentIntent) {
setState(null);
updateStateFrom(paymentIntent);
}
@Override
protected void handleDirectTransaction(final Transaction transaction) throws VerificationException {
cannotClassify(input);
}
@Override
protected void error(final int messageResId, final Object... messageArgs) {
final DialogBuilder dialog = DialogBuilder.dialog(activity, R.string.button_scan, messageResId, messageArgs);
dialog.singleDismissButton(null);
dialog.show();
}
}.parse();
}
} else if (requestCode == REQUEST_CODE_ENABLE_BLUETOOTH_FOR_PAYMENT_REQUEST) {
if (viewModel.paymentIntent.isBluetoothPaymentRequestUrl())
requestPaymentRequest();
} else if (requestCode == REQUEST_CODE_ENABLE_BLUETOOTH_FOR_DIRECT_PAYMENT) {
if (viewModel.paymentIntent.isBluetoothPaymentUrl())
directPaymentEnableView.setChecked(resultCode == Activity.RESULT_OK);
}
}
use of org.bitcoinj.core.Transaction in project bitcoin-wallet by bitcoin-wallet.
the class SendCoinsFragment method updateView.
private void updateView() {
final Wallet wallet = walletActivityViewModel.wallet.getValue();
final Map<FeeCategory, Coin> fees = viewModel.dynamicFees.getValue();
final BlockchainState blockchainState = application.blockchainState.getValue();
final Map<String, AddressBookEntry> addressBook = AddressBookEntry.asMap(viewModel.addressBook.getValue());
if (viewModel.paymentIntent != null) {
final MonetaryFormat btcFormat = config.getFormat();
getView().setVisibility(View.VISIBLE);
if (viewModel.paymentIntent.hasPayee()) {
payeeNameView.setVisibility(View.VISIBLE);
payeeNameView.setText(viewModel.paymentIntent.payeeName);
payeeVerifiedByView.setVisibility(View.VISIBLE);
final String verifiedBy = viewModel.paymentIntent.payeeVerifiedBy != null ? viewModel.paymentIntent.payeeVerifiedBy : getString(R.string.send_coins_fragment_payee_verified_by_unknown);
payeeVerifiedByView.setText(Constants.CHAR_CHECKMARK + String.format(getString(R.string.send_coins_fragment_payee_verified_by), verifiedBy));
} else {
payeeNameView.setVisibility(View.GONE);
payeeVerifiedByView.setVisibility(View.GONE);
}
if (viewModel.paymentIntent.hasOutputs()) {
payeeGroup.setVisibility(View.VISIBLE);
receivingAddressView.setVisibility(View.GONE);
receivingStaticView.setVisibility(!viewModel.paymentIntent.hasPayee() || viewModel.paymentIntent.payeeVerifiedBy == null ? View.VISIBLE : View.GONE);
receivingStaticLabelView.setText(viewModel.paymentIntent.memo);
if (viewModel.paymentIntent.hasAddress())
receivingStaticAddressView.setText(WalletUtils.formatAddress(viewModel.paymentIntent.getAddress(), Constants.ADDRESS_FORMAT_GROUP_SIZE, Constants.ADDRESS_FORMAT_LINE_SIZE));
else
receivingStaticAddressView.setText(R.string.send_coins_fragment_receiving_address_complex);
} else if (viewModel.validatedAddress != null) {
payeeGroup.setVisibility(View.VISIBLE);
receivingAddressView.setVisibility(View.GONE);
receivingStaticView.setVisibility(View.VISIBLE);
receivingStaticAddressView.setText(WalletUtils.formatAddress(viewModel.validatedAddress.address, Constants.ADDRESS_FORMAT_GROUP_SIZE, Constants.ADDRESS_FORMAT_LINE_SIZE));
final String addressBookLabel = addressBookDao.resolveLabel(viewModel.validatedAddress.address.toString());
final String staticLabel;
if (addressBookLabel != null)
staticLabel = addressBookLabel;
else if (viewModel.validatedAddress.label != null)
staticLabel = viewModel.validatedAddress.label;
else
staticLabel = getString(R.string.address_unlabeled);
receivingStaticLabelView.setText(staticLabel);
receivingStaticLabelView.setTextColor(activity.getColor(viewModel.validatedAddress.label != null ? R.color.fg_significant : R.color.fg_insignificant));
} else if (viewModel.paymentIntent.standard == null) {
payeeGroup.setVisibility(View.VISIBLE);
receivingStaticView.setVisibility(View.GONE);
receivingAddressView.setVisibility(View.VISIBLE);
} else {
payeeGroup.setVisibility(View.GONE);
}
receivingAddressView.setEnabled(viewModel.state == SendCoinsViewModel.State.INPUT);
amountGroup.setVisibility(viewModel.paymentIntent.hasAmount() || (viewModel.state != null && viewModel.state.compareTo(SendCoinsViewModel.State.INPUT) >= 0) ? View.VISIBLE : View.GONE);
amountCalculatorLink.setEnabled(viewModel.state == SendCoinsViewModel.State.INPUT && viewModel.paymentIntent.mayEditAmount());
final boolean directPaymentVisible;
if (viewModel.paymentIntent.hasPaymentUrl()) {
if (viewModel.paymentIntent.isBluetoothPaymentUrl())
directPaymentVisible = bluetoothAdapter != null;
else
directPaymentVisible = true;
} else {
directPaymentVisible = false;
}
directPaymentEnableView.setVisibility(directPaymentVisible ? View.VISIBLE : View.GONE);
directPaymentEnableView.setEnabled(viewModel.state == SendCoinsViewModel.State.INPUT);
hintView.setVisibility(View.GONE);
if (viewModel.state == SendCoinsViewModel.State.INPUT) {
if (blockchainState != null && blockchainState.replaying) {
hintView.setTextColor(activity.getColor(R.color.fg_error));
hintView.setVisibility(View.VISIBLE);
hintView.setText(R.string.send_coins_fragment_hint_replaying);
} else if (viewModel.paymentIntent.mayEditAddress() && viewModel.validatedAddress == null && !receivingAddressView.getText().toString().trim().isEmpty()) {
hintView.setTextColor(activity.getColor(R.color.fg_error));
hintView.setVisibility(View.VISIBLE);
hintView.setText(R.string.send_coins_fragment_receiving_address_error);
} else if (viewModel.dryrunException != null) {
hintView.setTextColor(activity.getColor(R.color.fg_error));
hintView.setVisibility(View.VISIBLE);
if (viewModel.dryrunException instanceof DustySendRequested)
hintView.setText(getString(R.string.send_coins_fragment_hint_dusty_send));
else if (viewModel.dryrunException instanceof InsufficientMoneyException)
hintView.setText(getString(R.string.send_coins_fragment_hint_insufficient_money, btcFormat.format(((InsufficientMoneyException) viewModel.dryrunException).missing)));
else if (viewModel.dryrunException instanceof CouldNotAdjustDownwards)
hintView.setText(getString(R.string.send_coins_fragment_hint_empty_wallet_failed));
else
hintView.setText(viewModel.dryrunException.toString());
} else if (viewModel.dryrunTransaction != null && viewModel.dryrunTransaction.getFee() != null) {
hintView.setVisibility(View.VISIBLE);
final int hintResId;
final int colorResId;
if (viewModel.feeCategory == FeeCategory.ECONOMIC) {
hintResId = R.string.send_coins_fragment_hint_fee_economic;
colorResId = R.color.fg_less_significant;
} else if (viewModel.feeCategory == FeeCategory.PRIORITY) {
hintResId = R.string.send_coins_fragment_hint_fee_priority;
colorResId = R.color.fg_less_significant;
} else {
hintResId = R.string.send_coins_fragment_hint_fee;
colorResId = R.color.fg_insignificant;
}
hintView.setTextColor(activity.getColor(colorResId));
hintView.setText(getString(hintResId, btcFormat.format(viewModel.dryrunTransaction.getFee())));
} else if (viewModel.paymentIntent.mayEditAddress() && viewModel.validatedAddress != null && wallet != null && wallet.isAddressMine(viewModel.validatedAddress.address)) {
hintView.setTextColor(activity.getColor(R.color.fg_insignificant));
hintView.setVisibility(View.VISIBLE);
hintView.setText(R.string.send_coins_fragment_receiving_address_own);
}
}
final Transaction sentTransaction = viewModel.sentTransaction.getValue();
if (sentTransaction != null && wallet != null) {
sentTransactionView.setVisibility(View.VISIBLE);
sentTransactionViewHolder.fullBind(new TransactionsAdapter.ListItem.TransactionItem(activity, sentTransaction, wallet, addressBook, btcFormat, application.maxConnectedPeers()));
} else {
sentTransactionView.setVisibility(View.GONE);
}
if (viewModel.directPaymentAck != null) {
directPaymentMessageView.setVisibility(View.VISIBLE);
directPaymentMessageView.setText(viewModel.directPaymentAck ? R.string.send_coins_fragment_direct_payment_ack : R.string.send_coins_fragment_direct_payment_nack);
} else {
directPaymentMessageView.setVisibility(View.GONE);
}
viewCancel.setEnabled(viewModel.state != SendCoinsViewModel.State.REQUEST_PAYMENT_REQUEST && viewModel.state != SendCoinsViewModel.State.DECRYPTING && viewModel.state != SendCoinsViewModel.State.SIGNING);
viewGo.setEnabled(everythingPlausible() && viewModel.dryrunTransaction != null && wallet != null && fees != null && (blockchainState == null || !blockchainState.replaying));
if (viewModel.state == null || viewModel.state == SendCoinsViewModel.State.REQUEST_PAYMENT_REQUEST) {
viewCancel.setText(R.string.button_cancel);
viewGo.setText(null);
} else if (viewModel.state == SendCoinsViewModel.State.INPUT) {
viewCancel.setText(R.string.button_cancel);
viewGo.setText(R.string.send_coins_fragment_button_send);
} else if (viewModel.state == SendCoinsViewModel.State.DECRYPTING) {
viewCancel.setText(R.string.button_cancel);
viewGo.setText(R.string.send_coins_fragment_state_decrypting);
} else if (viewModel.state == SendCoinsViewModel.State.SIGNING) {
viewCancel.setText(R.string.button_cancel);
viewGo.setText(R.string.send_coins_preparation_msg);
} else if (viewModel.state == SendCoinsViewModel.State.SENDING) {
viewCancel.setText(R.string.send_coins_fragment_button_back);
viewGo.setText(R.string.send_coins_sending_msg);
} else if (viewModel.state == SendCoinsViewModel.State.SENT) {
viewCancel.setText(R.string.send_coins_fragment_button_back);
viewGo.setText(R.string.send_coins_sent_msg);
} else if (viewModel.state == SendCoinsViewModel.State.FAILED) {
viewCancel.setText(R.string.send_coins_fragment_button_back);
viewGo.setText(R.string.send_coins_failed_msg);
}
final boolean privateKeyPasswordViewVisible = (viewModel.state == SendCoinsViewModel.State.INPUT || viewModel.state == SendCoinsViewModel.State.DECRYPTING) && wallet != null && wallet.isEncrypted();
privateKeyPasswordViewGroup.setVisibility(privateKeyPasswordViewVisible ? View.VISIBLE : View.GONE);
privateKeyPasswordView.setEnabled(viewModel.state == SendCoinsViewModel.State.INPUT);
// focus linking
final int activeAmountViewId = amountCalculatorLink.activeTextView().getId();
receivingAddressView.setNextFocusDownId(activeAmountViewId);
receivingAddressView.setNextFocusForwardId(activeAmountViewId);
amountCalculatorLink.setNextFocusId(privateKeyPasswordViewVisible ? R.id.send_coins_private_key_password : R.id.send_coins_go);
privateKeyPasswordView.setNextFocusUpId(activeAmountViewId);
privateKeyPasswordView.setNextFocusDownId(R.id.send_coins_go);
privateKeyPasswordView.setNextFocusForwardId(R.id.send_coins_go);
viewGo.setNextFocusUpId(privateKeyPasswordViewVisible ? R.id.send_coins_private_key_password : activeAmountViewId);
} else {
getView().setVisibility(View.GONE);
}
}
use of org.bitcoinj.core.Transaction in project bitcoin-wallet by bitcoin-wallet.
the class SweepWalletFragment method requestWalletBalance.
private void requestWalletBalance() {
viewModel.progress.setValue(getString(R.string.sweep_wallet_fragment_request_wallet_balance_progress));
final RequestWalletBalanceTask.ResultCallback callback = new RequestWalletBalanceTask.ResultCallback() {
@Override
public void onResult(final Set<UTXO> utxos) {
final Wallet wallet = walletActivityViewModel.wallet.getValue();
viewModel.progress.setValue(null);
// Filter UTXOs we've already spent and sort the rest.
final Set<Transaction> walletTxns = wallet.getTransactions(false);
final Set<UTXO> sortedUtxos = new TreeSet<>(UTXO_COMPARATOR);
for (final UTXO utxo : utxos) if (!utxoSpentBy(walletTxns, utxo))
sortedUtxos.add(utxo);
// Fake transaction funding the wallet to sweep.
final Map<Sha256Hash, Transaction> fakeTxns = new HashMap<>();
for (final UTXO utxo : sortedUtxos) {
Transaction fakeTx = fakeTxns.get(utxo.getHash());
if (fakeTx == null) {
fakeTx = new FakeTransaction(Constants.NETWORK_PARAMETERS, utxo.getHash(), utxo.getHash());
fakeTx.getConfidence().setConfidenceType(ConfidenceType.BUILDING);
fakeTxns.put(fakeTx.getTxId(), fakeTx);
}
final TransactionOutput fakeOutput = new TransactionOutput(Constants.NETWORK_PARAMETERS, fakeTx, utxo.getValue(), utxo.getScript().getProgram());
// Fill with output dummies as needed.
while (fakeTx.getOutputs().size() < utxo.getIndex()) fakeTx.addOutput(new TransactionOutput(Constants.NETWORK_PARAMETERS, fakeTx, Coin.NEGATIVE_SATOSHI, new byte[] {}));
// Add the actual output we will spend later.
fakeTx.addOutput(fakeOutput);
}
final Wallet walletToSweep = viewModel.walletToSweep.getValue();
walletToSweep.clearTransactions(0);
for (final Transaction tx : fakeTxns.values()) walletToSweep.addWalletTransaction(new WalletTransaction(WalletTransaction.Pool.UNSPENT, tx));
log.info("built wallet to sweep:\n{}", walletToSweep.toString(false, false, null, true, false, null));
viewModel.walletToSweep.setValue(walletToSweep);
}
private boolean utxoSpentBy(final Set<Transaction> transactions, final UTXO utxo) {
for (final Transaction tx : transactions) {
for (final TransactionInput input : tx.getInputs()) {
final TransactionOutPoint outpoint = input.getOutpoint();
if (outpoint.getHash().equals(utxo.getHash()) && outpoint.getIndex() == utxo.getIndex())
return true;
}
}
return false;
}
@Override
public void onFail(final int messageResId, final Object... messageArgs) {
viewModel.progress.setValue(null);
viewModel.showDialogWithRetryRequestBalance.setValue(DialogEvent.warn(R.string.sweep_wallet_fragment_request_wallet_balance_failed_title, messageResId, messageArgs));
}
};
final Wallet walletToSweep = viewModel.walletToSweep.getValue();
final ECKey key = walletToSweep.getImportedKeys().iterator().next();
new RequestWalletBalanceTask(backgroundHandler, callback).requestWalletBalance(activity.getAssets(), key);
}
use of org.bitcoinj.core.Transaction in project bitcoin-wallet by bitcoin-wallet.
the class TransactionLiveData method setValue.
@Override
public void setValue(final Transaction newTransaction) {
final Transaction oldTransaction = getValue();
maybeRemoveEventListener(oldTransaction);
super.setValue(newTransaction);
maybeAddEventListener(newTransaction);
}
use of org.bitcoinj.core.Transaction in project bisq-core by bisq-network.
the class BtcWalletService method getFeeEstimationTransactionForMultipleAddresses.
public Transaction getFeeEstimationTransactionForMultipleAddresses(Set<String> fromAddresses, Coin amount) throws AddressFormatException, AddressEntryException, InsufficientFundsException {
Set<AddressEntry> addressEntries = fromAddresses.stream().map(address -> {
Optional<AddressEntry> addressEntryOptional = findAddressEntry(address, AddressEntry.Context.AVAILABLE);
if (!addressEntryOptional.isPresent())
addressEntryOptional = findAddressEntry(address, AddressEntry.Context.OFFER_FUNDING);
if (!addressEntryOptional.isPresent())
addressEntryOptional = findAddressEntry(address, AddressEntry.Context.TRADE_PAYOUT);
if (!addressEntryOptional.isPresent())
addressEntryOptional = findAddressEntry(address, AddressEntry.Context.ARBITRATOR);
return addressEntryOptional;
}).filter(Optional::isPresent).map(Optional::get).collect(Collectors.toSet());
if (addressEntries.isEmpty())
throw new AddressEntryException("No Addresses for withdraw found in our wallet");
try {
Coin fee;
int counter = 0;
int txSize = 0;
Transaction tx;
Coin txFeeForWithdrawalPerByte = getTxFeeForWithdrawalPerByte();
do {
counter++;
fee = txFeeForWithdrawalPerByte.multiply(txSize);
// We use a dummy address for the output
final String dummyReceiver = getFreshAddressEntry().getAddressString();
SendRequest sendRequest = getSendRequestForMultipleAddresses(fromAddresses, dummyReceiver, amount, fee, null, aesKey);
wallet.completeTx(sendRequest);
tx = sendRequest.tx;
txSize = tx.bitcoinSerialize().length;
printTx("FeeEstimationTransactionForMultipleAddresses", tx);
} while (feeEstimationNotSatisfied(counter, tx));
if (counter == 10)
log.error("Could not calculate the fee. Tx=" + tx);
return tx;
} catch (InsufficientMoneyException e) {
throw new InsufficientFundsException("The fees for that transaction exceed the available funds " + "or the resulting output value is below the min. dust value:\n" + "Missing " + (e.missing != null ? e.missing.toFriendlyString() : "null"));
}
}
Aggregations