Search in sources :

Example 1 with AddressEntry

use of bisq.core.btc.AddressEntry 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 AddressEntry

use of bisq.core.btc.AddressEntry 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 AddressEntry

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

the class BtcWalletService method getMultiSigKeyPair.

public DeterministicKey getMultiSigKeyPair(String tradeId, byte[] pubKey) {
    Optional<AddressEntry> multiSigAddressEntryOptional = getAddressEntry(tradeId, AddressEntry.Context.MULTI_SIG);
    DeterministicKey multiSigKeyPair;
    if (multiSigAddressEntryOptional.isPresent()) {
        AddressEntry multiSigAddressEntry = multiSigAddressEntryOptional.get();
        multiSigKeyPair = multiSigAddressEntry.getKeyPair();
        if (!Arrays.equals(pubKey, multiSigAddressEntry.getPubKey())) {
            log.error("Pub Key from AddressEntry does not match key pair from trade data. Trade ID={}\n" + "We try to find the keypair in the wallet with the pubKey we found in the trade data.", tradeId);
            multiSigKeyPair = findKeyFromPubKey(pubKey);
        }
    } else {
        log.error("multiSigAddressEntry not found for trade ID={}.\n" + "We try to find the keypair in the wallet with the pubKey we found in the trade data.", tradeId);
        multiSigKeyPair = findKeyFromPubKey(pubKey);
    }
    return multiSigKeyPair;
}
Also used : AddressEntry(bisq.core.btc.AddressEntry) DeterministicKey(org.bitcoinj.crypto.DeterministicKey)

Example 4 with AddressEntry

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

the class BtcWalletService method getOrCreateAddressEntry.

public AddressEntry getOrCreateAddressEntry(String offerId, AddressEntry.Context context) {
    Optional<AddressEntry> addressEntry = getAddressEntryListAsImmutableList().stream().filter(e -> offerId.equals(e.getOfferId())).filter(e -> context == e.getContext()).findAny();
    if (addressEntry.isPresent()) {
        return addressEntry.get();
    } else {
        AddressEntry entry = addressEntryList.addAddressEntry(new AddressEntry(wallet.freshReceiveKey(), context, offerId));
        saveAddressEntryList();
        return entry;
    }
}
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) AddressEntry(bisq.core.btc.AddressEntry)

Example 5 with AddressEntry

use of bisq.core.btc.AddressEntry 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)

Aggregations

AddressEntry (bisq.core.btc.AddressEntry)27 Coin (org.bitcoinj.core.Coin)16 BtcWalletService (bisq.core.btc.wallet.BtcWalletService)15 Transaction (org.bitcoinj.core.Transaction)14 Address (org.bitcoinj.core.Address)8 AddressEntryException (bisq.core.btc.AddressEntryException)7 TradingPeer (bisq.core.trade.protocol.TradingPeer)7 InsufficientFundsException (bisq.core.btc.InsufficientFundsException)6 FeeService (bisq.core.provider.fee.FeeService)6 Preferences (bisq.core.user.Preferences)6 Optional (java.util.Optional)6 Collectors (java.util.stream.Collectors)6 TransactionVerificationException (bisq.core.btc.exceptions.TransactionVerificationException)5 Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)5 FutureCallback (com.google.common.util.concurrent.FutureCallback)5 List (java.util.List)5 Set (java.util.Set)5 DeterministicKey (org.bitcoinj.crypto.DeterministicKey)5 AddressFormatException (org.bitcoinj.core.AddressFormatException)4 SendRequest (org.bitcoinj.wallet.SendRequest)4