Search in sources :

Example 31 with Address

use of org.bitcoinj.core.Address in project bisq-core by bisq-network.

the class BuyerAsMakerCreatesAndSignsDepositTx method run.

@Override
protected void run() {
    try {
        runInterceptHook();
        checkNotNull(trade.getTradeAmount(), "trade.getTradeAmount() must not be null");
        BtcWalletService walletService = processModel.getBtcWalletService();
        String id = processModel.getOffer().getId();
        TradingPeer tradingPeer = processModel.getTradingPeer();
        final Offer offer = trade.getOffer();
        // params
        final boolean makerIsBuyer = true;
        final byte[] contractHash = Hash.getSha256Hash(trade.getContractAsJson());
        trade.setContractHash(contractHash);
        log.debug("\n\n------------------------------------------------------------\n" + "Contract as json\n" + trade.getContractAsJson() + "\n------------------------------------------------------------\n");
        final Coin makerInputAmount = offer.getBuyerSecurityDeposit();
        Optional<AddressEntry> addressEntryOptional = walletService.getAddressEntry(id, AddressEntry.Context.MULTI_SIG);
        checkArgument(addressEntryOptional.isPresent(), "addressEntryOptional must be present");
        AddressEntry makerMultiSigAddressEntry = addressEntryOptional.get();
        makerMultiSigAddressEntry.setCoinLockedInMultiSig(makerInputAmount);
        walletService.saveAddressEntryList();
        final Coin msOutputAmount = makerInputAmount.add(trade.getTxFee()).add(offer.getSellerSecurityDeposit()).add(trade.getTradeAmount());
        final List<RawTransactionInput> takerRawTransactionInputs = tradingPeer.getRawTransactionInputs();
        final long takerChangeOutputValue = tradingPeer.getChangeOutputValue();
        final String takerChangeAddressString = tradingPeer.getChangeOutputAddress();
        final Address makerAddress = walletService.getOrCreateAddressEntry(id, AddressEntry.Context.RESERVED_FOR_TRADE).getAddress();
        final Address makerChangeAddress = walletService.getFreshAddressEntry().getAddress();
        final byte[] buyerPubKey = processModel.getMyMultiSigPubKey();
        checkArgument(Arrays.equals(buyerPubKey, makerMultiSigAddressEntry.getPubKey()), "buyerPubKey from AddressEntry must match the one from the trade data. trade id =" + id);
        final byte[] sellerPubKey = tradingPeer.getMultiSigPubKey();
        final byte[] arbitratorBtcPubKey = trade.getArbitratorBtcPubKey();
        PreparedDepositTxAndMakerInputs result = processModel.getTradeWalletService().makerCreatesAndSignsDepositTx(makerIsBuyer, contractHash, makerInputAmount, msOutputAmount, takerRawTransactionInputs, takerChangeOutputValue, takerChangeAddressString, makerAddress, makerChangeAddress, buyerPubKey, sellerPubKey, arbitratorBtcPubKey);
        processModel.setPreparedDepositTx(result.depositTransaction);
        processModel.setRawTransactionInputs(result.rawMakerInputs);
        complete();
    } catch (Throwable t) {
        failed(t);
    }
}
Also used : Address(org.bitcoinj.core.Address) AddressEntry(bisq.core.btc.AddressEntry) RawTransactionInput(bisq.core.btc.data.RawTransactionInput) Coin(org.bitcoinj.core.Coin) TradingPeer(bisq.core.trade.protocol.TradingPeer) BtcWalletService(bisq.core.btc.wallet.BtcWalletService) Offer(bisq.core.offer.Offer) PreparedDepositTxAndMakerInputs(bisq.core.btc.data.PreparedDepositTxAndMakerInputs)

Example 32 with Address

use of org.bitcoinj.core.Address in project bisq-core by bisq-network.

the class BuyerAsTakerCreatesDepositTxInputs method run.

@Override
protected void run() {
    try {
        runInterceptHook();
        // In case we pay the taker fee in bsq we reduce tx fee by that as the burned bsq satoshis goes to miners.
        Coin bsqTakerFee = trade.isCurrencyForTakerFeeBtc() ? Coin.ZERO : trade.getTakerFee();
        Coin txFee = trade.getTxFee();
        Coin takerInputAmount = trade.getOffer().getBuyerSecurityDeposit().add(txFee).add(txFee).subtract(bsqTakerFee);
        BtcWalletService walletService = processModel.getBtcWalletService();
        Address takersAddress = walletService.getOrCreateAddressEntry(processModel.getOffer().getId(), AddressEntry.Context.RESERVED_FOR_TRADE).getAddress();
        Address takersChangeAddress = walletService.getFreshAddressEntry().getAddress();
        InputsAndChangeOutput result = processModel.getTradeWalletService().takerCreatesDepositsTxInputs(takerInputAmount, txFee.subtract(bsqTakerFee), takersAddress, takersChangeAddress);
        processModel.setRawTransactionInputs(result.rawTransactionInputs);
        processModel.setChangeOutputValue(result.changeOutputValue);
        processModel.setChangeOutputAddress(result.changeOutputAddress);
        complete();
    } catch (Throwable t) {
        failed(t);
    }
}
Also used : Coin(org.bitcoinj.core.Coin) Address(org.bitcoinj.core.Address) BtcWalletService(bisq.core.btc.wallet.BtcWalletService) InputsAndChangeOutput(bisq.core.btc.data.InputsAndChangeOutput)

Example 33 with Address

use of org.bitcoinj.core.Address in project bitsquare by bitsquare.

the class MultiAddressesCoinSelector method matchesRequirement.

@Override
protected boolean matchesRequirement(TransactionOutput transactionOutput) {
    if (transactionOutput.getScriptPubKey().isSentToAddress() || transactionOutput.getScriptPubKey().isPayToScriptHash()) {
        boolean confirmationCheck = allowUnconfirmedSpend || false;
        if (!allowUnconfirmedSpend && transactionOutput.getParentTransaction() != null && transactionOutput.getParentTransaction().getConfidence() != null) {
            final TransactionConfidence.ConfidenceType confidenceType = transactionOutput.getParentTransaction().getConfidence().getConfidenceType();
            confirmationCheck = confidenceType == TransactionConfidence.ConfidenceType.BUILDING;
            if (!confirmationCheck)
                log.error("Tx is not in blockchain yet. confidenceType=" + confidenceType);
        }
        Address addressOutput = transactionOutput.getScriptPubKey().getToAddress(params);
        log.trace("matchesRequiredAddress(es)?");
        log.trace(addressOutput.toString());
        log.trace(addressEntries.toString());
        for (AddressEntry entry : addressEntries) {
            if (addressOutput.equals(entry.getAddress()) && confirmationCheck)
                return true;
        }
        log.trace("No match found at matchesRequiredAddress addressOutput / addressEntries " + addressOutput.toString() + " / " + addressEntries.toString());
        return false;
    } else {
        log.warn("transactionOutput.getScriptPubKey() not isSentToAddress or isPayToScriptHash");
        return false;
    }
}
Also used : Address(org.bitcoinj.core.Address) TransactionConfidence(org.bitcoinj.core.TransactionConfidence)

Example 34 with Address

use of org.bitcoinj.core.Address in project bitsquare by bitsquare.

the class TradeWalletCoinSelector method matchesRequirement.

@Override
protected boolean matchesRequirement(TransactionOutput transactionOutput) {
    if (transactionOutput.getScriptPubKey().isSentToAddress() || transactionOutput.getScriptPubKey().isPayToScriptHash()) {
        boolean confirmationCheck = allowUnconfirmedSpend || false;
        if (!allowUnconfirmedSpend && transactionOutput.getParentTransaction() != null && transactionOutput.getParentTransaction().getConfidence() != null) {
            final TransactionConfidence.ConfidenceType confidenceType = transactionOutput.getParentTransaction().getConfidence().getConfidenceType();
            confirmationCheck = confidenceType == TransactionConfidence.ConfidenceType.BUILDING;
            if (!confirmationCheck)
                log.error("Tx is not in blockchain yet. confidenceType=" + confidenceType);
        }
        Address addressOutput = transactionOutput.getScriptPubKey().getToAddress(params);
        log.trace("matchesRequiredAddress?");
        log.trace("addressOutput " + addressOutput.toString());
        log.trace("address " + address.toString());
        boolean matches = addressOutput.equals(address);
        if (!matches)
            log.trace("No match found at matchesRequiredAddress addressOutput / address " + addressOutput.toString() + " / " + address.toString());
        return matches && confirmationCheck;
    } else {
        log.warn("transactionOutput.getScriptPubKey() not isSentToAddress or isPayToScriptHash");
        return false;
    }
}
Also used : Address(org.bitcoinj.core.Address) TransactionConfidence(org.bitcoinj.core.TransactionConfidence)

Example 35 with Address

use of org.bitcoinj.core.Address in project bitsquare by bitsquare.

the class OffererCreatesAndSignsDepositTxAsBuyer method run.

@Override
protected void run() {
    try {
        runInterceptHook();
        checkNotNull(trade.getTradeAmount(), "trade.getTradeAmount() must not be null");
        Offer offer = trade.getOffer();
        Coin securityDeposit = FeePolicy.getSecurityDeposit(offer);
        Coin buyerInputAmount = securityDeposit.add(FeePolicy.getFixedTxFeeForTrades(offer));
        Coin msOutputAmount = buyerInputAmount.add(securityDeposit).add(trade.getTradeAmount());
        log.debug("\n\n------------------------------------------------------------\n" + "Contract as json\n" + trade.getContractAsJson() + "\n------------------------------------------------------------\n");
        byte[] contractHash = Hash.getHash(trade.getContractAsJson());
        trade.setContractHash(contractHash);
        WalletService walletService = processModel.getWalletService();
        String id = processModel.getOffer().getId();
        AddressEntry offererAddressEntry = walletService.getOrCreateAddressEntry(id, AddressEntry.Context.RESERVED_FOR_TRADE);
        AddressEntry buyerMultiSigAddressEntry = walletService.getOrCreateAddressEntry(id, AddressEntry.Context.MULTI_SIG);
        buyerMultiSigAddressEntry.setCoinLockedInMultiSig(buyerInputAmount.subtract(FeePolicy.getFixedTxFeeForTrades(offer)));
        Address changeAddress = walletService.getOrCreateAddressEntry(AddressEntry.Context.AVAILABLE).getAddress();
        PreparedDepositTxAndOffererInputs result = processModel.getTradeWalletService().offererCreatesAndSignsDepositTx(true, contractHash, buyerInputAmount, msOutputAmount, processModel.tradingPeer.getRawTransactionInputs(), processModel.tradingPeer.getChangeOutputValue(), processModel.tradingPeer.getChangeOutputAddress(), offererAddressEntry.getAddress(), changeAddress, buyerMultiSigAddressEntry.getPubKey(), processModel.tradingPeer.getMultiSigPubKey(), trade.getArbitratorPubKey());
        processModel.setPreparedDepositTx(result.depositTransaction);
        processModel.setRawTransactionInputs(result.rawOffererInputs);
        complete();
    } catch (Throwable t) {
        failed(t);
    }
}
Also used : Coin(org.bitcoinj.core.Coin) Address(org.bitcoinj.core.Address) Offer(io.bitsquare.trade.offer.Offer) AddressEntry(io.bitsquare.btc.AddressEntry) PreparedDepositTxAndOffererInputs(io.bitsquare.btc.data.PreparedDepositTxAndOffererInputs) WalletService(io.bitsquare.btc.WalletService)

Aggregations

Address (org.bitcoinj.core.Address)80 Transaction (org.bitcoinj.core.Transaction)33 Coin (org.bitcoinj.core.Coin)30 TransactionOutput (org.bitcoinj.core.TransactionOutput)16 TransactionInput (org.bitcoinj.core.TransactionInput)14 AddressEntry (bisq.core.btc.AddressEntry)11 Wallet (org.bitcoinj.wallet.Wallet)11 BtcWalletService (bisq.core.btc.wallet.BtcWalletService)10 AddressFormatException (org.bitcoinj.core.AddressFormatException)10 TransactionConfidence (org.bitcoinj.core.TransactionConfidence)10 SendRequest (org.bitcoinj.wallet.SendRequest)10 IOException (java.io.IOException)9 TransactionOutPoint (org.bitcoinj.core.TransactionOutPoint)9 WalletService (io.bitsquare.btc.WalletService)8 List (java.util.List)8 Collectors (java.util.stream.Collectors)8 Nullable (javax.annotation.Nullable)8 Script (org.bitcoinj.script.Script)8 Intent (android.content.Intent)7 TextView (android.widget.TextView)7