Search in sources :

Example 71 with Transaction

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

the class AbstractWalletActivityViewModel method broadcastTransaction.

public ListenableFuture<Transaction> broadcastTransaction(final Transaction tx) throws VerificationException {
    final SettableFuture<Transaction> future = SettableFuture.create();
    wallet.observeForever(new Observer<Wallet>() {

        @Override
        public void onChanged(final Wallet wallet) {
            blockchainService.observeForever(new Observer<BlockchainService>() {

                @Override
                public void onChanged(final BlockchainService blockchainService) {
                    if (wallet.isTransactionRelevant(tx)) {
                        wallet.receivePending(tx, null);
                        final TransactionBroadcast broadcast = blockchainService.broadcastTransaction(tx);
                        if (broadcast != null) {
                            broadcast.future().addListener(() -> {
                                log.info("broadcasting transaction {} complete, dropping all peers", tx.getTxId());
                                blockchainService.dropAllPeers();
                            }, Threading.SAME_THREAD);
                            future.setFuture(broadcast.future());
                        } else {
                            log.info("impediments; will send {} later", tx.getTxId());
                            future.cancel(false);
                        }
                    } else {
                        log.info("tx {} irrelevant", tx.getTxId());
                        future.cancel(false);
                    }
                    AbstractWalletActivityViewModel.this.blockchainService.removeObserver(this);
                }
            });
            AbstractWalletActivityViewModel.this.wallet.removeObserver(this);
        }
    });
    return future;
}
Also used : Transaction(org.bitcoinj.core.Transaction) Wallet(org.bitcoinj.wallet.Wallet) Observer(androidx.lifecycle.Observer) TransactionBroadcast(org.bitcoinj.core.TransactionBroadcast) BlockchainService(de.schildbach.wallet.service.BlockchainService)

Example 72 with Transaction

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

the class WalletActivity method onActivityResult.

@Override
public void onActivityResult(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) {
                    SendCoinsActivity.start(WalletActivity.this, paymentIntent);
                }

                @Override
                protected void handlePrivateKey(final PrefixedChecksummedBytes key) {
                    if (Constants.ENABLE_SWEEP_WALLET)
                        SweepWalletActivity.start(WalletActivity.this, key);
                    else
                        super.handlePrivateKey(key);
                }

                @Override
                protected void handleDirectTransaction(final Transaction tx) throws VerificationException {
                    walletActivityViewModel.broadcastTransaction(tx);
                }

                @Override
                protected void error(final int messageResId, final Object... messageArgs) {
                    final DialogBuilder dialog = DialogBuilder.dialog(WalletActivity.this, R.string.button_scan, messageResId, messageArgs);
                    dialog.singleDismissButton(null);
                    dialog.show();
                }
            }.parse();
        }
    } else {
        super.onActivityResult(requestCode, resultCode, intent);
    }
}
Also used : PrefixedChecksummedBytes(org.bitcoinj.core.PrefixedChecksummedBytes) Transaction(org.bitcoinj.core.Transaction) StringInputParser(de.schildbach.wallet.ui.InputParser.StringInputParser) VerificationException(org.bitcoinj.core.VerificationException) PaymentIntent(de.schildbach.wallet.data.PaymentIntent)

Example 73 with Transaction

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

the class ReportIssueDialogFragment method onCreateDialog.

@Override
public Dialog onCreateDialog(final Bundle savedInstanceState) {
    final Bundle args = getArguments();
    final int titleResId = args.getInt(KEY_TITLE);
    final int messageResId = args.getInt(KEY_MESSAGE);
    final String subject = args.getString(KEY_SUBJECT);
    final Sha256Hash contextualTransactionHash = args.containsKey(KEY_CONTEXTUAL_TRANSACTION_HASH) ? Sha256Hash.wrap(args.getByteArray(KEY_CONTEXTUAL_TRANSACTION_HASH)) : null;
    final ReportIssueDialogBuilder builder = new ReportIssueDialogBuilder(activity, titleResId, messageResId) {

        @Override
        protected String subject() {
            final StringBuilder builder = new StringBuilder(subject).append(": ");
            final PackageInfo pi = application.packageInfo();
            builder.append(WalletApplication.versionLine(pi));
            final String installer = Installer.installerPackageName(application);
            if (installer != null)
                builder.append(", installer ").append(installer);
            builder.append(", android ").append(Build.VERSION.RELEASE);
            builder.append(" (").append(Build.VERSION.SECURITY_PATCH).append(")");
            builder.append(", ").append(Build.MANUFACTURER);
            if (!Build.BRAND.equalsIgnoreCase(Build.MANUFACTURER))
                builder.append(' ').append(Build.BRAND);
            builder.append(' ').append(Build.MODEL);
            return builder.toString();
        }

        @Override
        protected CharSequence collectApplicationInfo() throws IOException {
            final StringBuilder applicationInfo = new StringBuilder();
            appendApplicationInfo(applicationInfo, application);
            return applicationInfo;
        }

        @Override
        protected CharSequence collectStackTrace() throws IOException {
            final StringBuilder stackTrace = new StringBuilder();
            CrashReporter.appendSavedCrashTrace(stackTrace);
            return stackTrace.length() > 0 ? stackTrace : null;
        }

        @Override
        protected CharSequence collectDeviceInfo() throws IOException {
            final StringBuilder deviceInfo = new StringBuilder();
            appendDeviceInfo(deviceInfo, activity);
            return deviceInfo;
        }

        @Override
        protected CharSequence collectContextualData() {
            if (contextualTransactionHash == null)
                return null;
            final Wallet wallet = walletActivityViewModel.wallet.getValue();
            final Transaction tx = wallet.getTransaction(contextualTransactionHash);
            final StringBuilder contextualData = new StringBuilder();
            try {
                contextualData.append(tx.getValue(wallet).toFriendlyString()).append(" total value");
            } catch (final ScriptException x) {
                contextualData.append(x.getMessage());
            }
            contextualData.append('\n');
            if (tx.hasConfidence())
                contextualData.append("  confidence: ").append(tx.getConfidence()).append('\n');
            final String[] blockExplorers = activity.getResources().getStringArray(R.array.preferences_block_explorer_values);
            for (final String blockExplorer : blockExplorers) contextualData.append(Uri.withAppendedPath(Uri.parse(blockExplorer), "tx/" + tx.getTxId().toString())).append('\n');
            contextualData.append(tx.toString()).append('\n');
            contextualData.append(Constants.HEX.encode(tx.unsafeBitcoinSerialize())).append('\n');
            return contextualData;
        }

        @Override
        protected CharSequence collectWalletDump() {
            return walletActivityViewModel.wallet.getValue().toString(false, false, null, true, true, null);
        }
    };
    final AlertDialog dialog = builder.create();
    dialog.setOnShowListener(d -> {
        positiveButton = dialog.getButton(DialogInterface.BUTTON_POSITIVE);
        positiveButton.setEnabled(false);
        walletActivityViewModel.wallet.observe(ReportIssueDialogFragment.this, wallet -> positiveButton.setEnabled(true));
    });
    return dialog;
}
Also used : AlertDialog(android.app.AlertDialog) ScriptException(org.bitcoinj.script.ScriptException) Transaction(org.bitcoinj.core.Transaction) Bundle(android.os.Bundle) PackageInfo(android.content.pm.PackageInfo) Wallet(org.bitcoinj.wallet.Wallet) Sha256Hash(org.bitcoinj.core.Sha256Hash)

Example 74 with Transaction

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

the class BlockListAdapter method buildTransactionItems.

private static List<ListItem.TxItem> buildTransactionItems(final Context context, final Sha256Hash blockHash, @Nullable final Set<Transaction> transactions, @Nullable final Wallet wallet, @Nullable final Map<String, AddressBookEntry> addressBook) {
    final List<ListItem.TxItem> transactionItems = new LinkedList<>();
    if (transactions != null && wallet != null) {
        for (final Transaction tx : transactions) {
            final Map<Sha256Hash, Integer> appearsInHashes = tx.getAppearsInHashes();
            if (appearsInHashes != null && appearsInHashes.containsKey(blockHash)) {
                final boolean isCoinBase = tx.isCoinBase();
                final boolean isInternal = tx.getPurpose() == Purpose.KEY_ROTATION;
                final Coin value = tx.getValue(wallet);
                final boolean sent = value.signum() < 0;
                final boolean self = WalletUtils.isEntirelySelf(tx, wallet);
                final Address address;
                if (sent)
                    address = WalletUtils.getToAddressOfSent(tx, wallet);
                else
                    address = WalletUtils.getWalletAddressOfReceived(tx, wallet);
                final CharSequence fromTo;
                if (isInternal || self)
                    fromTo = context.getString(R.string.symbol_internal);
                else if (sent)
                    fromTo = context.getString(R.string.symbol_to);
                else
                    fromTo = context.getString(R.string.symbol_from);
                final CharSequence label;
                if (isCoinBase) {
                    label = context.getString(R.string.wallet_transactions_fragment_coinbase);
                } else if (isInternal || self) {
                    label = context.getString(R.string.wallet_transactions_fragment_internal);
                } else if (address != null && addressBook != null) {
                    final AddressBookEntry entry = addressBook.get(address.toString());
                    if (entry != null)
                        label = entry.getLabel();
                    else
                        label = "?";
                } else {
                    label = "?";
                }
                final CharSequence addressText = label != null ? label : address.toString();
                final Typeface addressTypeface = label != null ? Typeface.DEFAULT : Typeface.MONOSPACE;
                transactionItems.add(new ListItem.TxItem(fromTo, addressText, addressTypeface, label, value));
            }
        }
    }
    return transactionItems;
}
Also used : Address(org.bitcoinj.core.Address) Typeface(android.graphics.Typeface) Sha256Hash(org.bitcoinj.core.Sha256Hash) AddressBookEntry(de.schildbach.wallet.addressbook.AddressBookEntry) LinkedList(java.util.LinkedList) Coin(org.bitcoinj.core.Coin) Transaction(org.bitcoinj.core.Transaction)

Example 75 with Transaction

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

the class SendCoinsQrActivity method onActivityResult.

@Override
public void onActivityResult(final int requestCode, final int resultCode, final Intent intent) {
    if (requestCode == REQUEST_CODE_SCAN && resultCode == Activity.RESULT_OK) {
        final String input = intent.getStringExtra(ScanActivity.INTENT_EXTRA_RESULT);
        new StringInputParser(input) {

            @Override
            protected void handlePaymentIntent(final PaymentIntent paymentIntent) {
                SendCoinsActivity.start(SendCoinsQrActivity.this, paymentIntent);
                SendCoinsQrActivity.this.finish();
            }

            @Override
            protected void handlePrivateKey(final PrefixedChecksummedBytes key) {
                if (Constants.ENABLE_SWEEP_WALLET) {
                    SweepWalletActivity.start(SendCoinsQrActivity.this, key);
                    SendCoinsQrActivity.this.finish();
                } else {
                    super.handlePrivateKey(key);
                }
            }

            @Override
            protected void handleDirectTransaction(final Transaction transaction) throws VerificationException {
                walletActivityViewModel.broadcastTransaction(transaction);
                SendCoinsQrActivity.this.finish();
            }

            @Override
            protected void error(final int messageResId, final Object... messageArgs) {
                final DialogBuilder dialog = DialogBuilder.dialog(SendCoinsQrActivity.this, 0, messageResId, messageArgs);
                dialog.singleDismissButton(dismissListener);
                dialog.show();
            }

            private final OnClickListener dismissListener = (dialog, which) -> SendCoinsQrActivity.this.finish();
        }.parse();
    } else {
        finish();
    }
}
Also used : PaymentIntent(de.schildbach.wallet.data.PaymentIntent) Bundle(android.os.Bundle) Transaction(org.bitcoinj.core.Transaction) ViewModelProvider(androidx.lifecycle.ViewModelProvider) OnClickListener(android.content.DialogInterface.OnClickListener) ScanActivity(de.schildbach.wallet.ui.scan.ScanActivity) Intent(android.content.Intent) VerificationException(org.bitcoinj.core.VerificationException) PrefixedChecksummedBytes(org.bitcoinj.core.PrefixedChecksummedBytes) StringInputParser(de.schildbach.wallet.ui.InputParser.StringInputParser) SendCoinsActivity(de.schildbach.wallet.ui.send.SendCoinsActivity) SweepWalletActivity(de.schildbach.wallet.ui.send.SweepWalletActivity) Activity(android.app.Activity) Constants(de.schildbach.wallet.Constants) PrefixedChecksummedBytes(org.bitcoinj.core.PrefixedChecksummedBytes) Transaction(org.bitcoinj.core.Transaction) StringInputParser(de.schildbach.wallet.ui.InputParser.StringInputParser) VerificationException(org.bitcoinj.core.VerificationException) OnClickListener(android.content.DialogInterface.OnClickListener) PaymentIntent(de.schildbach.wallet.data.PaymentIntent)

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