Search in sources :

Example 1 with PrefixedChecksummedBytes

use of org.bitcoinj.core.PrefixedChecksummedBytes 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)

Example 2 with PrefixedChecksummedBytes

use of org.bitcoinj.core.PrefixedChecksummedBytes 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 3 with PrefixedChecksummedBytes

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

the class SweepWalletFragment method maybeDecodeKey.

private void maybeDecodeKey() {
    checkState(viewModel.state == SweepWalletViewModel.State.DECODE_KEY);
    final PrefixedChecksummedBytes privateKeyToSweep = viewModel.privateKeyToSweep.getValue();
    checkState(privateKeyToSweep != null);
    if (privateKeyToSweep instanceof DumpedPrivateKey) {
        final ECKey key = ((DumpedPrivateKey) privateKeyToSweep).getKey();
        askConfirmSweep(key);
    } else if (privateKeyToSweep instanceof BIP38PrivateKey) {
        badPasswordView.setVisibility(View.INVISIBLE);
        final String password = passwordView.getText().toString().trim();
        // get rid of it asap
        passwordView.setText(null);
        if (!password.isEmpty()) {
            viewModel.progress.setValue(getString(R.string.sweep_wallet_fragment_decrypt_progress));
            new DecodePrivateKeyTask(backgroundHandler) {

                @Override
                protected void onSuccess(ECKey decryptedKey) {
                    log.info("successfully decoded BIP38 private key");
                    viewModel.progress.setValue(null);
                    askConfirmSweep(decryptedKey);
                }

                @Override
                protected void onBadPassphrase() {
                    log.info("failed decoding BIP38 private key (bad password)");
                    viewModel.progress.setValue(null);
                    badPasswordView.setVisibility(View.VISIBLE);
                    passwordView.requestFocus();
                }
            }.decodePrivateKey((BIP38PrivateKey) privateKeyToSweep, password);
        }
    } else {
        throw new IllegalStateException("cannot handle type: " + privateKeyToSweep.getClass().getName());
    }
}
Also used : PrefixedChecksummedBytes(org.bitcoinj.core.PrefixedChecksummedBytes) BIP38PrivateKey(org.bitcoinj.crypto.BIP38PrivateKey) ECKey(org.bitcoinj.core.ECKey) DumpedPrivateKey(org.bitcoinj.core.DumpedPrivateKey)

Example 4 with PrefixedChecksummedBytes

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

the class SweepWalletFragment method onCreate.

@Override
public void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.fragmentManager = getChildFragmentManager();
    setHasOptionsMenu(true);
    if (!Constants.ENABLE_SWEEP_WALLET)
        throw new IllegalStateException("ENABLE_SWEEP_WALLET is disabled");
    walletActivityViewModel = new ViewModelProvider(activity).get(AbstractWalletActivityViewModel.class);
    walletActivityViewModel.wallet.observe(this, wallet -> updateView());
    viewModel = new ViewModelProvider(this).get(SweepWalletViewModel.class);
    viewModel.getDynamicFees().observe(this, dynamicFees -> updateView());
    viewModel.progress.observe(this, new ProgressDialogFragment.Observer(fragmentManager));
    viewModel.privateKeyToSweep.observe(this, privateKeyToSweep -> updateView());
    viewModel.walletToSweep.observe(this, walletToSweep -> {
        if (walletToSweep != null) {
            balanceView.setVisibility(View.VISIBLE);
            final MonetaryFormat btcFormat = config.getFormat();
            final MonetarySpannable balanceSpannable = new MonetarySpannable(btcFormat, walletToSweep.getBalance(BalanceType.ESTIMATED));
            balanceSpannable.applyMarkup(null, null);
            final SpannableStringBuilder balance = new SpannableStringBuilder(balanceSpannable);
            balance.insert(0, ": ");
            balance.insert(0, getString(R.string.sweep_wallet_fragment_balance));
            balanceView.setText(balance);
        } else {
            balanceView.setVisibility(View.GONE);
        }
        updateView();
    });
    viewModel.sentTransaction.observe(this, transaction -> {
        if (viewModel.state == SweepWalletViewModel.State.SENDING) {
            final TransactionConfidence confidence = transaction.getConfidence();
            final ConfidenceType confidenceType = confidence.getConfidenceType();
            final int numBroadcastPeers = confidence.numBroadcastPeers();
            if (confidenceType == ConfidenceType.DEAD)
                setState(SweepWalletViewModel.State.FAILED);
            else if (numBroadcastPeers > 1 || confidenceType == ConfidenceType.BUILDING)
                setState(SweepWalletViewModel.State.SENT);
        }
        updateView();
    });
    viewModel.showDialog.observe(this, new DialogEvent.Observer(activity));
    viewModel.showDialogWithRetryRequestBalance.observe(this, new DialogEvent.Observer(activity) {

        @Override
        protected void onBuildButtons(final DialogBuilder dialog) {
            dialog.setPositiveButton(R.string.button_retry, (d, which) -> requestWalletBalance());
            dialog.setNegativeButton(R.string.button_dismiss, null);
        }
    });
    backgroundThread = new HandlerThread("backgroundThread", Process.THREAD_PRIORITY_BACKGROUND);
    backgroundThread.start();
    backgroundHandler = new Handler(backgroundThread.getLooper());
    if (savedInstanceState == null) {
        final Intent intent = activity.getIntent();
        if (intent.hasExtra(SweepWalletActivity.INTENT_EXTRA_KEY)) {
            final PrefixedChecksummedBytes privateKeyToSweep = (PrefixedChecksummedBytes) intent.getSerializableExtra(SweepWalletActivity.INTENT_EXTRA_KEY);
            viewModel.privateKeyToSweep.setValue(privateKeyToSweep);
            // delay until fragment is resumed
            handler.post(maybeDecodeKeyRunnable);
        }
    }
}
Also used : MonetarySpannable(de.schildbach.wallet.util.MonetarySpannable) Bundle(android.os.Bundle) DumpedPrivateKey(org.bitcoinj.core.DumpedPrivateKey) Transaction(org.bitcoinj.core.Transaction) PackageManager(android.content.pm.PackageManager) Coin(org.bitcoinj.core.Coin) WalletTransaction(org.bitcoinj.wallet.WalletTransaction) LoggerFactory(org.slf4j.LoggerFactory) ScanActivity(de.schildbach.wallet.ui.scan.ScanActivity) AbstractWalletActivityViewModel(de.schildbach.wallet.ui.AbstractWalletActivityViewModel) Process(android.os.Process) NetworkParameters(org.bitcoinj.core.NetworkParameters) PrefixedChecksummedBytes(org.bitcoinj.core.PrefixedChecksummedBytes) SendRequest(org.bitcoinj.wallet.SendRequest) Handler(android.os.Handler) Map(java.util.Map) Fragment(androidx.fragment.app.Fragment) View(android.view.View) Button(android.widget.Button) R(de.schildbach.wallet.R) Configuration(de.schildbach.wallet.Configuration) Constants(de.schildbach.wallet.Constants) PaymentIntent(de.schildbach.wallet.data.PaymentIntent) BalanceType(org.bitcoinj.wallet.Wallet.BalanceType) ConfidenceType(org.bitcoinj.core.TransactionConfidence.ConfidenceType) Set(java.util.Set) ComparisonChain(com.google.common.collect.ComparisonChain) ViewGroup(android.view.ViewGroup) Preconditions.checkState(androidx.core.util.Preconditions.checkState) ECKey(org.bitcoinj.core.ECKey) TextView(android.widget.TextView) Nullable(androidx.annotation.Nullable) TransactionsAdapter(de.schildbach.wallet.ui.TransactionsAdapter) TransactionOutput(org.bitcoinj.core.TransactionOutput) UTXO(org.bitcoinj.core.UTXO) Context(android.content.Context) TransactionConfidence(org.bitcoinj.core.TransactionConfidence) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) Wallet(org.bitcoinj.wallet.Wallet) Intent(android.content.Intent) HashMap(java.util.HashMap) TreeSet(java.util.TreeSet) MenuItem(android.view.MenuItem) AnimationUtils(android.view.animation.AnimationUtils) ProgressDialogFragment(de.schildbach.wallet.ui.ProgressDialogFragment) SpannableStringBuilder(android.text.SpannableStringBuilder) VerificationException(org.bitcoinj.core.VerificationException) MenuInflater(android.view.MenuInflater) Menu(android.view.Menu) DialogEvent(de.schildbach.wallet.ui.DialogEvent) Sha256Hash(org.bitcoinj.core.Sha256Hash) BIP38PrivateKey(org.bitcoinj.crypto.BIP38PrivateKey) FragmentManager(androidx.fragment.app.FragmentManager) TransactionOutPoint(org.bitcoinj.core.TransactionOutPoint) Logger(org.slf4j.Logger) ViewModelProvider(androidx.lifecycle.ViewModelProvider) LayoutInflater(android.view.LayoutInflater) MonetaryFormat(org.bitcoinj.utils.MonetaryFormat) Threading(org.bitcoinj.utils.Threading) AbstractWalletActivity(de.schildbach.wallet.ui.AbstractWalletActivity) DialogBuilder(de.schildbach.wallet.ui.DialogBuilder) HandlerThread(android.os.HandlerThread) TransactionInput(org.bitcoinj.core.TransactionInput) StringInputParser(de.schildbach.wallet.ui.InputParser.StringInputParser) WalletApplication(de.schildbach.wallet.WalletApplication) Comparator(java.util.Comparator) Activity(android.app.Activity) EditText(android.widget.EditText) MonetaryFormat(org.bitcoinj.utils.MonetaryFormat) PrefixedChecksummedBytes(org.bitcoinj.core.PrefixedChecksummedBytes) ProgressDialogFragment(de.schildbach.wallet.ui.ProgressDialogFragment) Handler(android.os.Handler) PaymentIntent(de.schildbach.wallet.data.PaymentIntent) Intent(android.content.Intent) MonetarySpannable(de.schildbach.wallet.util.MonetarySpannable) TransactionOutPoint(org.bitcoinj.core.TransactionOutPoint) ConfidenceType(org.bitcoinj.core.TransactionConfidence.ConfidenceType) DialogEvent(de.schildbach.wallet.ui.DialogEvent) HandlerThread(android.os.HandlerThread) AbstractWalletActivityViewModel(de.schildbach.wallet.ui.AbstractWalletActivityViewModel) DialogBuilder(de.schildbach.wallet.ui.DialogBuilder) TransactionConfidence(org.bitcoinj.core.TransactionConfidence) SpannableStringBuilder(android.text.SpannableStringBuilder) ViewModelProvider(androidx.lifecycle.ViewModelProvider)

Example 5 with PrefixedChecksummedBytes

use of org.bitcoinj.core.PrefixedChecksummedBytes 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)

Aggregations

PrefixedChecksummedBytes (org.bitcoinj.core.PrefixedChecksummedBytes)7 Transaction (org.bitcoinj.core.Transaction)6 PaymentIntent (de.schildbach.wallet.data.PaymentIntent)5 StringInputParser (de.schildbach.wallet.ui.InputParser.StringInputParser)5 VerificationException (org.bitcoinj.core.VerificationException)5 WalletTransaction (org.bitcoinj.wallet.WalletTransaction)3 Activity (android.app.Activity)2 Intent (android.content.Intent)2 Bundle (android.os.Bundle)2 ViewModelProvider (androidx.lifecycle.ViewModelProvider)2 Constants (de.schildbach.wallet.Constants)2 ScanActivity (de.schildbach.wallet.ui.scan.ScanActivity)2 Coin (org.bitcoinj.core.Coin)2 DumpedPrivateKey (org.bitcoinj.core.DumpedPrivateKey)2 ECKey (org.bitcoinj.core.ECKey)2 BIP38PrivateKey (org.bitcoinj.crypto.BIP38PrivateKey)2 MonetaryFormat (org.bitcoinj.utils.MonetaryFormat)2 Wallet (org.bitcoinj.wallet.Wallet)2 Context (android.content.Context)1 OnClickListener (android.content.DialogInterface.OnClickListener)1