Search in sources :

Example 1 with CompletionException

use of org.bitcoinj.wallet.Wallet.CompletionException in project bitcoin-wallet by bitcoin-wallet.

the class SendCoinsOfflineTask method sendCoinsOffline.

public final void sendCoinsOffline(final SendRequest sendRequest) {
    backgroundHandler.post(new Runnable() {

        @Override
        public void run() {
            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.getHashAsString());
                callbackHandler.post(new Runnable() {

                    @Override
                    public void run() {
                        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(new Runnable() {

                    @Override
                    public void run() {
                        onInsufficientMoney(x.missing);
                    }
                });
            } catch (final ECKey.KeyIsEncryptedException x) {
                log.info("send failed, key is encrypted: {}", x.getMessage());
                callbackHandler.post(new Runnable() {

                    @Override
                    public void run() {
                        onFailure(x);
                    }
                });
            } catch (final KeyCrypterException x) {
                log.info("send failed, key crypter exception: {}", x.getMessage());
                final boolean isEncrypted = wallet.isEncrypted();
                callbackHandler.post(new Runnable() {

                    @Override
                    public void run() {
                        if (isEncrypted)
                            onInvalidEncryptionKey();
                        else
                            onFailure(x);
                    }
                });
            } catch (final CouldNotAdjustDownwards x) {
                log.info("send failed, could not adjust downwards: {}", x.getMessage());
                callbackHandler.post(new Runnable() {

                    @Override
                    public void run() {
                        onEmptyWalletFailed();
                    }
                });
            } catch (final CompletionException x) {
                log.info("send failed, cannot complete: {}", x.getMessage());
                callbackHandler.post(new Runnable() {

                    @Override
                    public void run() {
                        onFailure(x);
                    }
                });
            }
        }
    });
}
Also used : Coin(org.bitcoinj.core.Coin) Transaction(org.bitcoinj.core.Transaction) CompletionException(org.bitcoinj.wallet.Wallet.CompletionException) InsufficientMoneyException(org.bitcoinj.core.InsufficientMoneyException) CouldNotAdjustDownwards(org.bitcoinj.wallet.Wallet.CouldNotAdjustDownwards) KeyCrypterException(org.bitcoinj.crypto.KeyCrypterException)

Aggregations

Coin (org.bitcoinj.core.Coin)1 InsufficientMoneyException (org.bitcoinj.core.InsufficientMoneyException)1 Transaction (org.bitcoinj.core.Transaction)1 KeyCrypterException (org.bitcoinj.crypto.KeyCrypterException)1 CompletionException (org.bitcoinj.wallet.Wallet.CompletionException)1 CouldNotAdjustDownwards (org.bitcoinj.wallet.Wallet.CouldNotAdjustDownwards)1