Search in sources :

Example 1 with AddressEntryException

use of bisq.core.btc.AddressEntryException in project bisq-core by bisq-network.

the class BtcWalletService method getSendRequest.

private SendRequest getSendRequest(String fromAddress, String toAddress, Coin amount, Coin fee, @Nullable KeyParameter aesKey, AddressEntry.Context context) throws AddressFormatException, AddressEntryException {
    Transaction tx = new Transaction(params);
    Preconditions.checkArgument(Restrictions.isAboveDust(amount, fee), "The amount is too low (dust limit).");
    tx.addOutput(amount.subtract(fee), Address.fromBase58(params, toAddress));
    SendRequest sendRequest = SendRequest.forTx(tx);
    sendRequest.fee = fee;
    sendRequest.feePerKb = Coin.ZERO;
    sendRequest.ensureMinRequiredFee = false;
    sendRequest.aesKey = aesKey;
    sendRequest.shuffleOutputs = false;
    Optional<AddressEntry> addressEntry = findAddressEntry(fromAddress, context);
    if (!addressEntry.isPresent())
        throw new AddressEntryException("WithdrawFromAddress is not found in our wallet.");
    checkNotNull(addressEntry.get(), "addressEntry.get() must not be null");
    checkNotNull(addressEntry.get().getAddress(), "addressEntry.get().getAddress() must not be null");
    sendRequest.coinSelector = new BtcCoinSelector(addressEntry.get().getAddress());
    sendRequest.changeAddress = addressEntry.get().getAddress();
    return sendRequest;
}
Also used : SendRequest(org.bitcoinj.wallet.SendRequest) AddressEntryException(bisq.core.btc.AddressEntryException) Transaction(org.bitcoinj.core.Transaction) AddressEntry(bisq.core.btc.AddressEntry)

Example 2 with AddressEntryException

use of bisq.core.btc.AddressEntryException in project bisq-core by bisq-network.

the class BtcWalletService method getSendRequestForMultipleAddresses.

private SendRequest getSendRequestForMultipleAddresses(Set<String> fromAddresses, String toAddress, Coin amount, Coin fee, @Nullable String changeAddress, @Nullable KeyParameter aesKey) throws AddressFormatException, AddressEntryException, InsufficientMoneyException {
    Transaction tx = new Transaction(params);
    checkArgument(Restrictions.isAboveDust(amount), "The amount is too low (dust limit).");
    final Coin netValue = amount.subtract(fee);
    if (netValue.isNegative())
        throw new InsufficientMoneyException(netValue.multiply(-1), "The mining fee for that transaction exceed the available amount.");
    tx.addOutput(netValue, Address.fromBase58(params, toAddress));
    SendRequest sendRequest = SendRequest.forTx(tx);
    sendRequest.fee = fee;
    sendRequest.feePerKb = Coin.ZERO;
    sendRequest.ensureMinRequiredFee = false;
    sendRequest.aesKey = aesKey;
    sendRequest.shuffleOutputs = false;
    Set<AddressEntry> addressEntries = fromAddresses.stream().map(address -> {
        Optional<AddressEntry> addressEntryOptional = findAddressEntry(address, AddressEntry.Context.AVAILABLE);
        if (!addressEntryOptional.isPresent())
            addressEntryOptional = findAddressEntry(address, AddressEntry.Context.OFFER_FUNDING);
        if (!addressEntryOptional.isPresent())
            addressEntryOptional = findAddressEntry(address, AddressEntry.Context.TRADE_PAYOUT);
        if (!addressEntryOptional.isPresent())
            addressEntryOptional = findAddressEntry(address, AddressEntry.Context.ARBITRATOR);
        return addressEntryOptional;
    }).filter(Optional::isPresent).map(Optional::get).collect(Collectors.toSet());
    if (addressEntries.isEmpty())
        throw new AddressEntryException("No Addresses for withdraw found in our wallet");
    sendRequest.coinSelector = new BtcCoinSelector(walletsSetup.getAddressesFromAddressEntries(addressEntries));
    Optional<AddressEntry> addressEntryOptional = Optional.<AddressEntry>empty();
    AddressEntry changeAddressAddressEntry = null;
    if (changeAddress != null)
        addressEntryOptional = findAddressEntry(changeAddress, AddressEntry.Context.AVAILABLE);
    if (addressEntryOptional.isPresent()) {
        changeAddressAddressEntry = addressEntryOptional.get();
    } else {
        ArrayList<AddressEntry> list = new ArrayList<>(addressEntries);
        if (!list.isEmpty())
            changeAddressAddressEntry = list.get(0);
    }
    checkNotNull(changeAddressAddressEntry, "change address must not be null");
    sendRequest.changeAddress = changeAddressAddressEntry.getAddress();
    return sendRequest;
}
Also used : Arrays(java.util.Arrays) Transaction(org.bitcoinj.core.Transaction) TransactionConfidence(org.bitcoinj.core.TransactionConfidence) Coin(org.bitcoinj.core.Coin) Wallet(org.bitcoinj.wallet.Wallet) LoggerFactory(org.slf4j.LoggerFactory) ArrayList(java.util.ArrayList) Inject(javax.inject.Inject) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) TransactionVerificationException(bisq.core.btc.exceptions.TransactionVerificationException) ImmutableList(com.google.common.collect.ImmutableList) AddressEntryList(bisq.core.btc.AddressEntryList) SendRequest(org.bitcoinj.wallet.SendRequest) ErrorMessageHandler(bisq.common.handlers.ErrorMessageHandler) KeyCrypterScrypt(org.bitcoinj.crypto.KeyCrypterScrypt) KeyParameter(org.spongycastle.crypto.params.KeyParameter) DeterministicKey(org.bitcoinj.crypto.DeterministicKey) Nullable(javax.annotation.Nullable) ScriptBuilder(org.bitcoinj.script.ScriptBuilder) AddressFormatException(org.bitcoinj.core.AddressFormatException) AddressEntryException(bisq.core.btc.AddressEntryException) WalletException(bisq.core.btc.exceptions.WalletException) TransactionOutPoint(org.bitcoinj.core.TransactionOutPoint) Logger(org.slf4j.Logger) InsufficientFundsException(bisq.core.btc.InsufficientFundsException) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) Set(java.util.Set) InsufficientMoneyException(org.bitcoinj.core.InsufficientMoneyException) Collectors(java.util.stream.Collectors) FutureCallback(com.google.common.util.concurrent.FutureCallback) Futures(com.google.common.util.concurrent.Futures) List(java.util.List) AddressEntry(bisq.core.btc.AddressEntry) TransactionInput(org.bitcoinj.core.TransactionInput) Preferences(bisq.core.user.Preferences) TransactionOutput(org.bitcoinj.core.TransactionOutput) Optional(java.util.Optional) FeeService(bisq.core.provider.fee.FeeService) Address(org.bitcoinj.core.Address) Preconditions(com.google.common.base.Preconditions) NotNull(org.jetbrains.annotations.NotNull) Restrictions(bisq.core.btc.Restrictions) SendRequest(org.bitcoinj.wallet.SendRequest) AddressEntryException(bisq.core.btc.AddressEntryException) Optional(java.util.Optional) AddressEntry(bisq.core.btc.AddressEntry) ArrayList(java.util.ArrayList) InsufficientMoneyException(org.bitcoinj.core.InsufficientMoneyException) Coin(org.bitcoinj.core.Coin) Transaction(org.bitcoinj.core.Transaction)

Example 3 with AddressEntryException

use of bisq.core.btc.AddressEntryException in project bisq-core by bisq-network.

the class BtcWalletService method getFeeEstimationTransactionForMultipleAddresses.

public Transaction getFeeEstimationTransactionForMultipleAddresses(Set<String> fromAddresses, Coin amount) throws AddressFormatException, AddressEntryException, InsufficientFundsException {
    Set<AddressEntry> addressEntries = fromAddresses.stream().map(address -> {
        Optional<AddressEntry> addressEntryOptional = findAddressEntry(address, AddressEntry.Context.AVAILABLE);
        if (!addressEntryOptional.isPresent())
            addressEntryOptional = findAddressEntry(address, AddressEntry.Context.OFFER_FUNDING);
        if (!addressEntryOptional.isPresent())
            addressEntryOptional = findAddressEntry(address, AddressEntry.Context.TRADE_PAYOUT);
        if (!addressEntryOptional.isPresent())
            addressEntryOptional = findAddressEntry(address, AddressEntry.Context.ARBITRATOR);
        return addressEntryOptional;
    }).filter(Optional::isPresent).map(Optional::get).collect(Collectors.toSet());
    if (addressEntries.isEmpty())
        throw new AddressEntryException("No Addresses for withdraw  found in our wallet");
    try {
        Coin fee;
        int counter = 0;
        int txSize = 0;
        Transaction tx;
        Coin txFeeForWithdrawalPerByte = getTxFeeForWithdrawalPerByte();
        do {
            counter++;
            fee = txFeeForWithdrawalPerByte.multiply(txSize);
            // We use a dummy address for the output
            SendRequest sendRequest = getSendRequestForMultipleAddresses(fromAddresses, getOrCreateAddressEntry(AddressEntry.Context.AVAILABLE).getAddressString(), amount, fee, null, aesKey);
            wallet.completeTx(sendRequest);
            tx = sendRequest.tx;
            txSize = tx.bitcoinSerialize().length;
            printTx("FeeEstimationTransactionForMultipleAddresses", tx);
        } while (feeEstimationNotSatisfied(counter, tx));
        if (counter == 10)
            log.error("Could not calculate the fee. Tx=" + tx);
        return tx;
    } catch (InsufficientMoneyException e) {
        throw new InsufficientFundsException("The fees for that transaction exceed the available funds " + "or the resulting output value is below the min. dust value:\n" + "Missing " + (e.missing != null ? e.missing.toFriendlyString() : "null"));
    }
}
Also used : Arrays(java.util.Arrays) Transaction(org.bitcoinj.core.Transaction) TransactionConfidence(org.bitcoinj.core.TransactionConfidence) Coin(org.bitcoinj.core.Coin) Wallet(org.bitcoinj.wallet.Wallet) LoggerFactory(org.slf4j.LoggerFactory) ArrayList(java.util.ArrayList) Inject(javax.inject.Inject) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) TransactionVerificationException(bisq.core.btc.exceptions.TransactionVerificationException) ImmutableList(com.google.common.collect.ImmutableList) AddressEntryList(bisq.core.btc.AddressEntryList) SendRequest(org.bitcoinj.wallet.SendRequest) ErrorMessageHandler(bisq.common.handlers.ErrorMessageHandler) KeyCrypterScrypt(org.bitcoinj.crypto.KeyCrypterScrypt) KeyParameter(org.spongycastle.crypto.params.KeyParameter) DeterministicKey(org.bitcoinj.crypto.DeterministicKey) Nullable(javax.annotation.Nullable) ScriptBuilder(org.bitcoinj.script.ScriptBuilder) AddressFormatException(org.bitcoinj.core.AddressFormatException) AddressEntryException(bisq.core.btc.AddressEntryException) WalletException(bisq.core.btc.exceptions.WalletException) TransactionOutPoint(org.bitcoinj.core.TransactionOutPoint) Logger(org.slf4j.Logger) InsufficientFundsException(bisq.core.btc.InsufficientFundsException) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) Set(java.util.Set) InsufficientMoneyException(org.bitcoinj.core.InsufficientMoneyException) Collectors(java.util.stream.Collectors) FutureCallback(com.google.common.util.concurrent.FutureCallback) Futures(com.google.common.util.concurrent.Futures) List(java.util.List) AddressEntry(bisq.core.btc.AddressEntry) TransactionInput(org.bitcoinj.core.TransactionInput) Preferences(bisq.core.user.Preferences) TransactionOutput(org.bitcoinj.core.TransactionOutput) Optional(java.util.Optional) FeeService(bisq.core.provider.fee.FeeService) Address(org.bitcoinj.core.Address) Preconditions(com.google.common.base.Preconditions) NotNull(org.jetbrains.annotations.NotNull) Restrictions(bisq.core.btc.Restrictions) Coin(org.bitcoinj.core.Coin) AddressEntryException(bisq.core.btc.AddressEntryException) SendRequest(org.bitcoinj.wallet.SendRequest) Optional(java.util.Optional) Transaction(org.bitcoinj.core.Transaction) AddressEntry(bisq.core.btc.AddressEntry) InsufficientFundsException(bisq.core.btc.InsufficientFundsException) InsufficientMoneyException(org.bitcoinj.core.InsufficientMoneyException) TransactionOutPoint(org.bitcoinj.core.TransactionOutPoint)

Example 4 with AddressEntryException

use of bisq.core.btc.AddressEntryException in project bisq-desktop by bisq-network.

the class BuyerStep4View method reviewWithdrawal.

@SuppressWarnings("PointlessBooleanExpression")
private void reviewWithdrawal() {
    Coin amount = trade.getPayoutAmount();
    BtcWalletService walletService = model.dataModel.btcWalletService;
    AddressEntry fromAddressesEntry = walletService.getOrCreateAddressEntry(trade.getId(), AddressEntry.Context.TRADE_PAYOUT);
    String fromAddresses = fromAddressesEntry.getAddressString();
    String toAddresses = withdrawAddressTextField.getText();
    if (new BtcAddressValidator().validate(toAddresses).isValid) {
        Coin balance = walletService.getBalanceForAddress(fromAddressesEntry.getAddress());
        try {
            Transaction feeEstimationTransaction = walletService.getFeeEstimationTransaction(fromAddresses, toAddresses, amount, AddressEntry.Context.TRADE_PAYOUT);
            Coin fee = feeEstimationTransaction.getFee();
            // noinspection UnusedAssignment
            Coin receiverAmount = amount.subtract(fee);
            if (balance.isZero()) {
                new Popup<>().warning(Res.get("portfolio.pending.step5_buyer.alreadyWithdrawn")).show();
                model.dataModel.tradeManager.addTradeToClosedTrades(trade);
            } else {
                if (toAddresses.isEmpty()) {
                    validateWithdrawAddress();
                } else if (Restrictions.isAboveDust(amount, fee)) {
                    BSFormatter formatter = model.btcFormatter;
                    int txSize = feeEstimationTransaction.bitcoinSerialize().length;
                    double feePerByte = CoinUtil.getFeePerByte(fee, txSize);
                    double kb = txSize / 1000d;
                    String recAmount = formatter.formatCoinWithCode(receiverAmount);
                    new Popup<>().headLine(Res.get("portfolio.pending.step5_buyer.confirmWithdrawal")).confirmation(Res.get("shared.sendFundsDetailsWithFee", formatter.formatCoinWithCode(amount), fromAddresses, toAddresses, formatter.formatCoinWithCode(fee), feePerByte, kb, recAmount)).actionButtonText(Res.get("shared.yes")).onAction(() -> doWithdrawal(amount, fee)).closeButtonText(Res.get("shared.cancel")).onClose(() -> {
                        useSavingsWalletButton.setDisable(false);
                        withdrawToExternalWalletButton.setDisable(false);
                    }).show();
                } else {
                    new Popup<>().warning(Res.get("portfolio.pending.step5_buyer.amountTooLow")).show();
                }
            }
        } catch (AddressFormatException e) {
            validateWithdrawAddress();
        } catch (AddressEntryException e) {
            log.error(e.getMessage());
        } catch (InsufficientFundsException e) {
            log.error(e.getMessage());
            e.printStackTrace();
            new Popup<>().warning(e.getMessage()).show();
        }
    } else {
        new Popup<>().warning(Res.get("validation.btc.invalidAddress")).show();
    }
}
Also used : BtcAddressValidator(bisq.desktop.util.validation.BtcAddressValidator) Coin(org.bitcoinj.core.Coin) AddressFormatException(org.bitcoinj.core.AddressFormatException) AddressEntryException(bisq.core.btc.AddressEntryException) Transaction(org.bitcoinj.core.Transaction) BtcWalletService(bisq.core.btc.wallet.BtcWalletService) AddressEntry(bisq.core.btc.AddressEntry) Popup(bisq.desktop.main.overlays.popups.Popup) InsufficientFundsException(bisq.core.btc.InsufficientFundsException) BSFormatter(bisq.desktop.util.BSFormatter)

Example 5 with AddressEntryException

use of bisq.core.btc.AddressEntryException in project bisq-core by bisq-network.

the class BtcWalletService method getFeeEstimationTransaction.

// /////////////////////////////////////////////////////////////////////////////////////////
// Withdrawal Fee calculation
// /////////////////////////////////////////////////////////////////////////////////////////
public Transaction getFeeEstimationTransaction(String fromAddress, String toAddress, Coin amount, AddressEntry.Context context) throws AddressFormatException, AddressEntryException, InsufficientFundsException {
    Optional<AddressEntry> addressEntry = findAddressEntry(fromAddress, context);
    if (!addressEntry.isPresent())
        throw new AddressEntryException("WithdrawFromAddress is not found in our wallet.");
    checkNotNull(addressEntry.get().getAddress(), "addressEntry.get().getAddress() must nto be null");
    try {
        Coin fee;
        int counter = 0;
        int txSize = 0;
        Transaction tx;
        Coin txFeeForWithdrawalPerByte = getTxFeeForWithdrawalPerByte();
        do {
            counter++;
            fee = txFeeForWithdrawalPerByte.multiply(txSize);
            SendRequest sendRequest = getSendRequest(fromAddress, toAddress, amount, fee, aesKey, context);
            wallet.completeTx(sendRequest);
            tx = sendRequest.tx;
            txSize = tx.bitcoinSerialize().length;
            printTx("FeeEstimationTransaction", tx);
        } while (feeEstimationNotSatisfied(counter, tx));
        if (counter == 10)
            log.error("Could not calculate the fee. Tx=" + tx);
        return tx;
    } catch (InsufficientMoneyException e) {
        throw new InsufficientFundsException("The fees for that transaction exceed the available funds " + "or the resulting output value is below the min. dust value:\n" + "Missing " + (e.missing != null ? e.missing.toFriendlyString() : "null"));
    }
}
Also used : Coin(org.bitcoinj.core.Coin) AddressEntryException(bisq.core.btc.AddressEntryException) SendRequest(org.bitcoinj.wallet.SendRequest) Transaction(org.bitcoinj.core.Transaction) AddressEntry(bisq.core.btc.AddressEntry) InsufficientFundsException(bisq.core.btc.InsufficientFundsException) InsufficientMoneyException(org.bitcoinj.core.InsufficientMoneyException) TransactionOutPoint(org.bitcoinj.core.TransactionOutPoint)

Aggregations

AddressEntry (bisq.core.btc.AddressEntry)5 AddressEntryException (bisq.core.btc.AddressEntryException)5 Transaction (org.bitcoinj.core.Transaction)5 InsufficientFundsException (bisq.core.btc.InsufficientFundsException)4 Coin (org.bitcoinj.core.Coin)4 SendRequest (org.bitcoinj.wallet.SendRequest)4 AddressFormatException (org.bitcoinj.core.AddressFormatException)3 InsufficientMoneyException (org.bitcoinj.core.InsufficientMoneyException)3 TransactionOutPoint (org.bitcoinj.core.TransactionOutPoint)3 ErrorMessageHandler (bisq.common.handlers.ErrorMessageHandler)2 AddressEntryList (bisq.core.btc.AddressEntryList)2 Restrictions (bisq.core.btc.Restrictions)2 TransactionVerificationException (bisq.core.btc.exceptions.TransactionVerificationException)2 WalletException (bisq.core.btc.exceptions.WalletException)2 FeeService (bisq.core.provider.fee.FeeService)2 Preferences (bisq.core.user.Preferences)2 Preconditions (com.google.common.base.Preconditions)2 Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)2 Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)2 ImmutableList (com.google.common.collect.ImmutableList)2