Search in sources :

Example 1 with TransactionOutput

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

the class SweepWalletFragment method requestWalletBalance.

private void requestWalletBalance() {
    ProgressDialogFragment.showProgress(fragmentManager, 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) {
            ProgressDialogFragment.dismissProgress(fragmentManager);
            // Filter UTXOs we've already spent and sort the rest.
            final Set<Transaction> walletTxns = application.getWallet().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());
                    fakeTx.getConfidence().setConfidenceType(ConfidenceType.BUILDING);
                    fakeTxns.put(fakeTx.getHash(), 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);
            }
            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, true, false, null));
            updateView();
        }

        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) {
            ProgressDialogFragment.dismissProgress(fragmentManager);
            final DialogBuilder dialog = DialogBuilder.warn(activity, R.string.sweep_wallet_fragment_request_wallet_balance_failed_title);
            dialog.setMessage(getString(messageResId, messageArgs));
            dialog.setPositiveButton(R.string.button_retry, new DialogInterface.OnClickListener() {

                @Override
                public void onClick(final DialogInterface dialog, final int which) {
                    requestWalletBalance();
                }
            });
            dialog.setNegativeButton(R.string.button_dismiss, null);
            dialog.show();
        }
    };
    final Address address = walletToSweep.getImportedKeys().iterator().next().toAddress(Constants.NETWORK_PARAMETERS);
    new RequestWalletBalanceTask(backgroundHandler, callback).requestWalletBalance(activity.getAssets(), address);
}
Also used : Set(java.util.Set) TreeSet(java.util.TreeSet) TransactionOutput(org.bitcoinj.core.TransactionOutput) Address(org.bitcoinj.core.Address) HashMap(java.util.HashMap) DialogInterface(android.content.DialogInterface) Sha256Hash(org.bitcoinj.core.Sha256Hash) TransactionInput(org.bitcoinj.core.TransactionInput) TransactionOutPoint(org.bitcoinj.core.TransactionOutPoint) UTXO(org.bitcoinj.core.UTXO) Transaction(org.bitcoinj.core.Transaction) WalletTransaction(org.bitcoinj.wallet.WalletTransaction) TreeSet(java.util.TreeSet) WalletTransaction(org.bitcoinj.wallet.WalletTransaction) DialogBuilder(de.schildbach.wallet.ui.DialogBuilder) TransactionOutPoint(org.bitcoinj.core.TransactionOutPoint)

Example 2 with TransactionOutput

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

the class RaiseFeeDialogFragment method doRaiseFee.

private void doRaiseFee(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);
        wallet.commitTx(transactionToSend);
        BlockchainService.broadcastTransaction(activity, transactionToSend);
        state = State.DONE;
        updateView();
        dismiss();
    } catch (final KeyCrypterException 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) KeyCrypterException(org.bitcoinj.crypto.KeyCrypterException)

Example 3 with TransactionOutput

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

the class ReportIssueDialogFragment method appendApplicationInfo.

private static void appendApplicationInfo(final Appendable report, final WalletApplication application) throws IOException {
    final PackageInfo pi = application.packageInfo();
    final Configuration configuration = application.getConfiguration();
    final Calendar calendar = new GregorianCalendar(UTC);
    report.append("Version: " + pi.versionName + " (" + pi.versionCode + ")\n");
    report.append("Package: " + pi.packageName + "\n");
    report.append("Installer: " + application.getPackageManager().getInstallerPackageName(pi.packageName) + "\n");
    report.append("Test/Prod: " + (Constants.TEST ? "test" : "prod") + "\n");
    report.append("Timezone: " + TimeZone.getDefault().getID() + "\n");
    calendar.setTimeInMillis(System.currentTimeMillis());
    report.append("Time: " + String.format(Locale.US, "%tF %tT %tZ", calendar, calendar, calendar) + "\n");
    calendar.setTimeInMillis(WalletApplication.TIME_CREATE_APPLICATION);
    report.append("Time of launch: " + String.format(Locale.US, "%tF %tT %tZ", calendar, calendar, calendar) + "\n");
    calendar.setTimeInMillis(pi.lastUpdateTime);
    report.append("Time of last update: " + String.format(Locale.US, "%tF %tT %tZ", calendar, calendar, calendar) + "\n");
    calendar.setTimeInMillis(pi.firstInstallTime);
    report.append("Time of first install: " + String.format(Locale.US, "%tF %tT %tZ", calendar, calendar, calendar) + "\n");
    final long lastBackupTime = configuration.getLastBackupTime();
    calendar.setTimeInMillis(lastBackupTime);
    report.append("Time of backup: " + (lastBackupTime > 0 ? String.format(Locale.US, "%tF %tT %tZ", calendar, calendar, calendar) : "none") + "\n");
    report.append("Network: " + Constants.NETWORK_PARAMETERS.getId() + "\n");
    final Wallet wallet = application.getWallet();
    report.append("Encrypted: " + wallet.isEncrypted() + "\n");
    report.append("Keychain size: " + wallet.getKeyChainGroupSize() + "\n");
    final Set<Transaction> transactions = wallet.getTransactions(true);
    int numInputs = 0;
    int numOutputs = 0;
    int numSpentOutputs = 0;
    for (final Transaction tx : transactions) {
        numInputs += tx.getInputs().size();
        final List<TransactionOutput> outputs = tx.getOutputs();
        numOutputs += outputs.size();
        for (final TransactionOutput txout : outputs) {
            if (!txout.isAvailableForSpending())
                numSpentOutputs++;
        }
    }
    report.append("Transactions: " + transactions.size() + "\n");
    report.append("Inputs: " + numInputs + "\n");
    report.append("Outputs: " + numOutputs + " (spent: " + numSpentOutputs + ")\n");
    report.append("Last block seen: " + wallet.getLastBlockSeenHeight() + " (" + wallet.getLastBlockSeenHash() + ")\n");
    report.append("Databases:");
    for (final String db : application.databaseList()) report.append(" " + db);
    report.append("\n");
    final File filesDir = application.getFilesDir();
    report.append("\nContents of FilesDir " + filesDir + ":\n");
    appendDir(report, filesDir, 0);
}
Also used : TransactionOutput(org.bitcoinj.core.TransactionOutput) Configuration(de.schildbach.wallet.Configuration) PackageInfo(android.content.pm.PackageInfo) Wallet(org.bitcoinj.wallet.Wallet) Calendar(java.util.Calendar) GregorianCalendar(java.util.GregorianCalendar) GregorianCalendar(java.util.GregorianCalendar) Transaction(org.bitcoinj.core.Transaction) File(java.io.File)

Aggregations

Transaction (org.bitcoinj.core.Transaction)3 TransactionOutput (org.bitcoinj.core.TransactionOutput)3 DialogInterface (android.content.DialogInterface)1 PackageInfo (android.content.pm.PackageInfo)1 Configuration (de.schildbach.wallet.Configuration)1 DialogBuilder (de.schildbach.wallet.ui.DialogBuilder)1 File (java.io.File)1 Calendar (java.util.Calendar)1 GregorianCalendar (java.util.GregorianCalendar)1 HashMap (java.util.HashMap)1 Set (java.util.Set)1 TreeSet (java.util.TreeSet)1 Address (org.bitcoinj.core.Address)1 Sha256Hash (org.bitcoinj.core.Sha256Hash)1 TransactionInput (org.bitcoinj.core.TransactionInput)1 TransactionOutPoint (org.bitcoinj.core.TransactionOutPoint)1 UTXO (org.bitcoinj.core.UTXO)1 KeyCrypterException (org.bitcoinj.crypto.KeyCrypterException)1 SendRequest (org.bitcoinj.wallet.SendRequest)1 Wallet (org.bitcoinj.wallet.Wallet)1