Search in sources :

Example 66 with Transaction

use of org.bitcoinj.core.Transaction in project bitcoin-wallet by bitcoin-wallet.

the class SweepWalletFragment method updateView.

private void updateView() {
    final PrefixedChecksummedBytes privateKeyToSweep = viewModel.privateKeyToSweep.getValue();
    final Wallet wallet = walletActivityViewModel.wallet.getValue();
    final Map<FeeCategory, Coin> fees = viewModel.getDynamicFees().getValue();
    final MonetaryFormat btcFormat = config.getFormat();
    if (viewModel.state == SweepWalletViewModel.State.DECODE_KEY && privateKeyToSweep == null) {
        messageView.setVisibility(View.VISIBLE);
        messageView.setText(R.string.sweep_wallet_fragment_wallet_unknown);
    } else if (viewModel.state == SweepWalletViewModel.State.DECODE_KEY && privateKeyToSweep != null) {
        messageView.setVisibility(View.VISIBLE);
        messageView.setText(R.string.sweep_wallet_fragment_encrypted);
    } else if (privateKeyToSweep != null) {
        messageView.setVisibility(View.GONE);
    }
    passwordViewGroup.setVisibility(viewModel.state == SweepWalletViewModel.State.DECODE_KEY && privateKeyToSweep != null ? View.VISIBLE : View.GONE);
    hintView.setVisibility(viewModel.state == SweepWalletViewModel.State.DECODE_KEY && privateKeyToSweep == null ? View.VISIBLE : View.GONE);
    final Transaction sentTransaction = viewModel.sentTransaction.getValue();
    if (sentTransaction != null) {
        sweepTransactionView.setVisibility(View.VISIBLE);
        sweepTransactionViewHolder.fullBind(new TransactionsAdapter.ListItem.TransactionItem(activity, sentTransaction, wallet, null, btcFormat, application.maxConnectedPeers()));
    } else {
        sweepTransactionView.setVisibility(View.GONE);
    }
    final Wallet walletToSweep = viewModel.walletToSweep.getValue();
    if (viewModel.state == SweepWalletViewModel.State.DECODE_KEY) {
        viewCancel.setText(R.string.button_cancel);
        viewGo.setText(R.string.sweep_wallet_fragment_button_decrypt);
        viewGo.setEnabled(privateKeyToSweep != null);
    } else if (viewModel.state == SweepWalletViewModel.State.CONFIRM_SWEEP) {
        viewCancel.setText(R.string.button_cancel);
        viewGo.setText(R.string.sweep_wallet_fragment_button_sweep);
        viewGo.setEnabled(wallet != null && walletToSweep != null && walletToSweep.getBalance(BalanceType.ESTIMATED).signum() > 0 && fees != null);
    } else if (viewModel.state == SweepWalletViewModel.State.PREPARATION) {
        viewCancel.setText(R.string.button_cancel);
        viewGo.setText(R.string.send_coins_preparation_msg);
        viewGo.setEnabled(false);
    } else if (viewModel.state == SweepWalletViewModel.State.SENDING) {
        viewCancel.setText(R.string.send_coins_fragment_button_back);
        viewGo.setText(R.string.send_coins_sending_msg);
        viewGo.setEnabled(false);
    } else if (viewModel.state == SweepWalletViewModel.State.SENT) {
        viewCancel.setText(R.string.send_coins_fragment_button_back);
        viewGo.setText(R.string.send_coins_sent_msg);
        viewGo.setEnabled(false);
    } else if (viewModel.state == SweepWalletViewModel.State.FAILED) {
        viewCancel.setText(R.string.send_coins_fragment_button_back);
        viewGo.setText(R.string.send_coins_failed_msg);
        viewGo.setEnabled(false);
    }
    viewCancel.setEnabled(viewModel.state != SweepWalletViewModel.State.PREPARATION);
    // enable actions
    if (reloadAction != null)
        reloadAction.setEnabled(viewModel.state == SweepWalletViewModel.State.CONFIRM_SWEEP && walletToSweep != null);
    if (scanAction != null)
        scanAction.setEnabled(viewModel.state == SweepWalletViewModel.State.DECODE_KEY || viewModel.state == SweepWalletViewModel.State.CONFIRM_SWEEP);
}
Also used : Coin(org.bitcoinj.core.Coin) PrefixedChecksummedBytes(org.bitcoinj.core.PrefixedChecksummedBytes) MonetaryFormat(org.bitcoinj.utils.MonetaryFormat) Transaction(org.bitcoinj.core.Transaction) WalletTransaction(org.bitcoinj.wallet.WalletTransaction) Wallet(org.bitcoinj.wallet.Wallet)

Example 67 with Transaction

use of org.bitcoinj.core.Transaction in project bitcoin-wallet by bitcoin-wallet.

the class RaiseFeeDialogFragment method doRaiseFee.

private void doRaiseFee(final Wallet wallet, final KeyParameter encryptionKey) {
    // construct child-pays-for-parent
    final TransactionOutput outputToSpend = checkNotNull(findSpendableOutput(wallet, transaction, feeRaise));
    final Transaction transactionToSend = new Transaction(Constants.NETWORK_PARAMETERS);
    transactionToSend.addInput(outputToSpend);
    transactionToSend.addOutput(outputToSpend.getValue().subtract(feeRaise), wallet.freshAddress(KeyPurpose.CHANGE));
    transactionToSend.setPurpose(Transaction.Purpose.RAISE_FEE);
    final SendRequest sendRequest = SendRequest.forTx(transactionToSend);
    sendRequest.aesKey = encryptionKey;
    try {
        wallet.signTransaction(sendRequest);
        log.info("raise fee: cpfp {}", transactionToSend);
        walletActivityViewModel.broadcastTransaction(transactionToSend);
        state = State.DONE;
        updateView();
        dismiss();
    } catch (final Wallet.BadWalletEncryptionKeyException x) {
        badPasswordView.setVisibility(View.VISIBLE);
        state = State.INPUT;
        updateView();
        passwordView.requestFocus();
        log.info("raise fee: bad spending password");
    }
}
Also used : TransactionOutput(org.bitcoinj.core.TransactionOutput) SendRequest(org.bitcoinj.wallet.SendRequest) Transaction(org.bitcoinj.core.Transaction) Wallet(org.bitcoinj.wallet.Wallet)

Example 68 with Transaction

use of org.bitcoinj.core.Transaction in project bitcoin-wallet by bitcoin-wallet.

the class SendCoinsOfflineTask method sendCoinsOffline.

public final void sendCoinsOffline(final SendRequest sendRequest) {
    backgroundHandler.post(() -> {
        org.bitcoinj.core.Context.propagate(Constants.CONTEXT);
        try {
            log.info("sending: {}", sendRequest);
            // can take long
            final Transaction transaction = wallet.sendCoinsOffline(sendRequest);
            log.info("send successful, transaction committed: {}", transaction.getTxId());
            callbackHandler.post(() -> onSuccess(transaction));
        } catch (final InsufficientMoneyException x) {
            final Coin missing = x.missing;
            if (missing != null)
                log.info("send failed, {} missing", missing.toFriendlyString());
            else
                log.info("send failed, insufficient coins");
            callbackHandler.post(() -> onInsufficientMoney(x.missing));
        } catch (final ECKey.KeyIsEncryptedException x) {
            log.info("send failed, key is encrypted: {}", x.getMessage());
            callbackHandler.post(() -> onFailure(x));
        } catch (final Wallet.BadWalletEncryptionKeyException x) {
            log.info("send failed, bad spending password: {}", x.getMessage());
            final boolean isEncrypted = wallet.isEncrypted();
            callbackHandler.post(() -> {
                if (isEncrypted)
                    onInvalidEncryptionKey();
                else
                    onFailure(x);
            });
        } catch (final CouldNotAdjustDownwards x) {
            log.info("send failed, could not adjust downwards: {}", x.getMessage());
            callbackHandler.post(() -> onEmptyWalletFailed());
        } catch (final CompletionException x) {
            log.info("send failed, cannot complete: {}", x.getMessage());
            callbackHandler.post(() -> onFailure(x));
        }
    });
}
Also used : Coin(org.bitcoinj.core.Coin) Transaction(org.bitcoinj.core.Transaction) Wallet(org.bitcoinj.wallet.Wallet) CompletionException(org.bitcoinj.wallet.Wallet.CompletionException) InsufficientMoneyException(org.bitcoinj.core.InsufficientMoneyException) ECKey(org.bitcoinj.core.ECKey) CouldNotAdjustDownwards(org.bitcoinj.wallet.Wallet.CouldNotAdjustDownwards)

Example 69 with Transaction

use of org.bitcoinj.core.Transaction in project bitcoin-wallet by bitcoin-wallet.

the class WalletTransactionsFragment method onInflateTransactionContextMenu.

@Override
public void onInflateTransactionContextMenu(final MenuInflater inflater, final Menu menu, final Sha256Hash transactionId) {
    final Wallet wallet = viewModel.wallet.getValue();
    final Transaction tx = wallet.getTransaction(transactionId);
    final boolean txSent = tx.getValue(wallet).signum() < 0;
    final Address txAddress = txSent ? WalletUtils.getToAddressOfSent(tx, wallet) : WalletUtils.getWalletAddressOfReceived(tx, wallet);
    final byte[] txSerialized = tx.unsafeBitcoinSerialize();
    inflater.inflate(R.menu.wallet_transactions_context, menu);
    final MenuItem editAddressMenuItem = menu.findItem(R.id.wallet_transactions_context_edit_address);
    if (txAddress != null) {
        editAddressMenuItem.setVisible(true);
        final boolean isAdd = addressBookDao.resolveLabel(txAddress.toString()) == null;
        final boolean isOwn = wallet.isAddressMine(txAddress);
        if (isOwn)
            editAddressMenuItem.setTitle(isAdd ? R.string.edit_address_book_entry_dialog_title_add_receive : R.string.edit_address_book_entry_dialog_title_edit_receive);
        else
            editAddressMenuItem.setTitle(isAdd ? R.string.edit_address_book_entry_dialog_title_add : R.string.edit_address_book_entry_dialog_title_edit);
    } else {
        editAddressMenuItem.setVisible(false);
    }
    menu.findItem(R.id.wallet_transactions_context_show_qr).setVisible(txSerialized.length < SHOW_QR_THRESHOLD_BYTES);
    menu.findItem(R.id.wallet_transactions_context_raise_fee).setVisible(RaiseFeeDialogFragment.feeCanLikelyBeRaised(wallet, tx));
    menu.findItem(R.id.wallet_transactions_context_browse).setVisible(Constants.ENABLE_BROWSE);
}
Also used : Transaction(org.bitcoinj.core.Transaction) Address(org.bitcoinj.core.Address) Wallet(org.bitcoinj.wallet.Wallet) MenuItem(android.view.MenuItem)

Example 70 with Transaction

use of org.bitcoinj.core.Transaction in project bitcoin-wallet by bitcoin-wallet.

the class AcceptBluetoothService method onCreate.

@Override
public void onCreate() {
    serviceCreatedAt = System.currentTimeMillis();
    log.debug(".onCreate()");
    super.onCreate();
    this.application = (WalletApplication) getApplication();
    final BluetoothAdapter bluetoothAdapter = checkNotNull(BluetoothAdapter.getDefaultAdapter());
    final PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
    wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, getClass().getName());
    wakeLock.acquire();
    final NotificationCompat.Builder notification = new NotificationCompat.Builder(this, Constants.NOTIFICATION_CHANNEL_ID_ONGOING);
    notification.setColor(getColor(R.color.fg_network_significant));
    notification.setSmallIcon(R.drawable.stat_notify_bluetooth_24dp);
    notification.setContentTitle(getString(R.string.notification_bluetooth_service_listening));
    notification.setWhen(System.currentTimeMillis());
    notification.setOngoing(true);
    notification.setPriority(NotificationCompat.PRIORITY_LOW);
    startForeground(Constants.NOTIFICATION_ID_BLUETOOTH, notification.build());
    registerReceiver(bluetoothStateChangeReceiver, new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED));
    try {
        classicThread = new AcceptBluetoothThread.ClassicBluetoothThread(bluetoothAdapter) {

            @Override
            public boolean handleTx(final Transaction tx) {
                return AcceptBluetoothService.this.handleTx(tx);
            }
        };
        paymentProtocolThread = new AcceptBluetoothThread.PaymentProtocolThread(bluetoothAdapter) {

            @Override
            public boolean handleTx(final Transaction tx) {
                return AcceptBluetoothService.this.handleTx(tx);
            }
        };
    } catch (final IOException x) {
        new Toast(this).longToast(R.string.error_bluetooth, x.getMessage());
        log.warn("problem with listening, stopping service", x);
        CrashReporter.saveBackgroundTrace(x, application.packageInfo());
        stopSelf();
    }
    wallet = new WalletLiveData(application);
    wallet.observe(this, wallet -> {
        classicThread.start();
        paymentProtocolThread.start();
    });
}
Also used : IntentFilter(android.content.IntentFilter) IOException(java.io.IOException) WalletLiveData(de.schildbach.wallet.data.WalletLiveData) PowerManager(android.os.PowerManager) Transaction(org.bitcoinj.core.Transaction) Toast(de.schildbach.wallet.util.Toast) NotificationCompat(androidx.core.app.NotificationCompat) BluetoothAdapter(android.bluetooth.BluetoothAdapter)

Aggregations

Transaction (org.bitcoinj.core.Transaction)214 Coin (org.bitcoinj.core.Coin)71 TransactionInput (org.bitcoinj.core.TransactionInput)48 TransactionOutput (org.bitcoinj.core.TransactionOutput)42 TransactionOutPoint (org.bitcoinj.core.TransactionOutPoint)38 Address (org.bitcoinj.core.Address)35 ECKey (org.bitcoinj.core.ECKey)32 Script (org.bitcoinj.script.Script)32 ArrayList (java.util.ArrayList)31 HashMap (java.util.HashMap)29 SendRequest (org.bitcoinj.wallet.SendRequest)25 Wallet (org.bitcoinj.wallet.Wallet)25 IOException (java.io.IOException)24 List (java.util.List)24 InsufficientMoneyException (org.bitcoinj.core.InsufficientMoneyException)20 MyTransactionOutPoint (com.samourai.wallet.send.MyTransactionOutPoint)19 AddressFormatException (org.bitcoinj.core.AddressFormatException)19 Sha256Hash (org.bitcoinj.core.Sha256Hash)19 UTXO (com.samourai.wallet.send.UTXO)17 Nullable (javax.annotation.Nullable)17