Search in sources :

Example 6 with BtcWalletService

use of bisq.core.btc.wallet.BtcWalletService in project bisq-core by bisq-network.

the class SellerAsTakerSignAndPublishDepositTx method run.

@Override
protected void run() {
    try {
        runInterceptHook();
        log.debug("\n\n------------------------------------------------------------\n" + "Contract as json\n" + trade.getContractAsJson() + "\n------------------------------------------------------------\n");
        byte[] contractHash = Hash.getSha256Hash(trade.getContractAsJson());
        trade.setContractHash(contractHash);
        List<RawTransactionInput> sellerInputs = checkNotNull(processModel.getRawTransactionInputs(), "sellerInputs must not be null");
        BtcWalletService walletService = processModel.getBtcWalletService();
        String id = processModel.getOffer().getId();
        Optional<AddressEntry> addressEntryOptional = walletService.getAddressEntry(id, AddressEntry.Context.MULTI_SIG);
        checkArgument(addressEntryOptional.isPresent(), "addressEntryOptional must be present");
        AddressEntry sellerMultiSigAddressEntry = addressEntryOptional.get();
        byte[] sellerMultiSigPubKey = processModel.getMyMultiSigPubKey();
        checkArgument(Arrays.equals(sellerMultiSigPubKey, sellerMultiSigAddressEntry.getPubKey()), "sellerMultiSigPubKey from AddressEntry must match the one from the trade data. trade id =" + id);
        Coin sellerInput = Coin.valueOf(sellerInputs.stream().mapToLong(input -> input.value).sum());
        sellerMultiSigAddressEntry.setCoinLockedInMultiSig(sellerInput.subtract(trade.getTxFee().multiply(2)));
        walletService.saveAddressEntryList();
        TradingPeer tradingPeer = processModel.getTradingPeer();
        Transaction depositTx = processModel.getTradeWalletService().takerSignsAndPublishesDepositTx(true, contractHash, processModel.getPreparedDepositTx(), tradingPeer.getRawTransactionInputs(), sellerInputs, tradingPeer.getMultiSigPubKey(), sellerMultiSigPubKey, trade.getArbitratorBtcPubKey(), new FutureCallback<Transaction>() {

            @Override
            public void onSuccess(Transaction transaction) {
                if (!completed) {
                    log.trace("takerSignAndPublishTx succeeded " + transaction);
                    trade.setState(Trade.State.TAKER_PUBLISHED_DEPOSIT_TX);
                    walletService.swapTradeEntryToAvailableEntry(id, AddressEntry.Context.RESERVED_FOR_TRADE);
                    complete();
                } else {
                    log.warn("We got the onSuccess callback called after the timeout has been triggered a complete().");
                }
            }

            @Override
            public void onFailure(@NotNull Throwable t) {
                if (!completed) {
                    failed(t);
                } else {
                    log.warn("We got the onFailure callback called after the timeout has been triggered a complete().");
                }
            }
        });
        // We set the deposit tx in case we get the onFailure called.
        trade.setDepositTx(depositTx);
    } catch (Throwable t) {
        final Contract contract = trade.getContract();
        if (contract != null)
            contract.printDiff(processModel.getTradingPeer().getContractAsJson());
        failed(t);
    }
}
Also used : AddressEntry(bisq.core.btc.AddressEntry) RawTransactionInput(bisq.core.btc.data.RawTransactionInput) Coin(org.bitcoinj.core.Coin) TradingPeer(bisq.core.trade.protocol.TradingPeer) Transaction(org.bitcoinj.core.Transaction) BtcWalletService(bisq.core.btc.wallet.BtcWalletService) Contract(bisq.core.trade.Contract)

Example 7 with BtcWalletService

use of bisq.core.btc.wallet.BtcWalletService in project bisq-core by bisq-network.

the class TakerSendPayDepositRequest method run.

@Override
protected void run() {
    try {
        runInterceptHook();
        checkNotNull(trade.getTradeAmount(), "TradeAmount must not be null");
        checkNotNull(trade.getTakerFeeTxId(), "TakeOfferFeeTxId must not be null");
        final User user = processModel.getUser();
        checkNotNull(user, "User must not be null");
        final List<NodeAddress> acceptedArbitratorAddresses = user.getAcceptedArbitratorAddresses();
        final List<NodeAddress> acceptedMediatorAddresses = user.getAcceptedMediatorAddresses();
        checkNotNull(acceptedArbitratorAddresses, "acceptedArbitratorAddresses must not be null");
        checkNotNull(acceptedMediatorAddresses, "acceptedMediatorAddresses must not be null");
        BtcWalletService walletService = processModel.getBtcWalletService();
        String id = processModel.getOffer().getId();
        checkArgument(!walletService.getAddressEntry(id, AddressEntry.Context.MULTI_SIG).isPresent(), "addressEntry must not be set here.");
        AddressEntry addressEntry = walletService.getOrCreateAddressEntry(id, AddressEntry.Context.MULTI_SIG);
        byte[] takerMultiSigPubKey = addressEntry.getPubKey();
        processModel.setMyMultiSigPubKey(takerMultiSigPubKey);
        AddressEntry takerPayoutAddressEntry = walletService.getOrCreateAddressEntry(id, AddressEntry.Context.TRADE_PAYOUT);
        String takerPayoutAddressString = takerPayoutAddressEntry.getAddressString();
        final String offerId = processModel.getOfferId();
        // Taker has to use offerId as nonce (he cannot manipulate that - so we avoid to have a challenge protocol for passing the nonce we want to get signed)
        // He cannot manipulate the offerId - so we avoid to have a challenge protocol for passing the nonce we want to get signed.
        final PaymentAccountPayload paymentAccountPayload = checkNotNull(processModel.getPaymentAccountPayload(trade), "processModel.getPaymentAccountPayload(trade) must not be null");
        byte[] sig = Sig.sign(processModel.getKeyRing().getSignatureKeyPair().getPrivate(), offerId.getBytes());
        PayDepositRequest message = new PayDepositRequest(offerId, processModel.getMyNodeAddress(), trade.getTradeAmount().value, trade.getTradePrice().getValue(), trade.getTxFee().getValue(), trade.getTakerFee().getValue(), trade.isCurrencyForTakerFeeBtc(), processModel.getRawTransactionInputs(), processModel.getChangeOutputValue(), processModel.getChangeOutputAddress(), takerMultiSigPubKey, takerPayoutAddressString, processModel.getPubKeyRing(), paymentAccountPayload, processModel.getAccountId(), trade.getTakerFeeTxId(), new ArrayList<>(acceptedArbitratorAddresses), new ArrayList<>(acceptedMediatorAddresses), trade.getArbitratorNodeAddress(), trade.getMediatorNodeAddress(), UUID.randomUUID().toString(), Version.getP2PMessageVersion(), sig, new Date().getTime());
        processModel.getP2PService().sendEncryptedDirectMessage(trade.getTradingPeerNodeAddress(), processModel.getTradingPeer().getPubKeyRing(), message, new SendDirectMessageListener() {

            @Override
            public void onArrived() {
                log.debug("Message arrived at peer. tradeId={}, message{}", id, message);
                complete();
            }

            @Override
            public void onFault() {
                appendToErrorMessage("Sending message failed: message=" + message + "\nerrorMessage=" + errorMessage);
                failed();
            }
        });
    } catch (Throwable t) {
        failed(t);
    }
}
Also used : User(bisq.core.user.User) AddressEntry(bisq.core.btc.AddressEntry) PaymentAccountPayload(bisq.core.payment.payload.PaymentAccountPayload) SendDirectMessageListener(bisq.network.p2p.SendDirectMessageListener) Date(java.util.Date) PayDepositRequest(bisq.core.trade.messages.PayDepositRequest) BtcWalletService(bisq.core.btc.wallet.BtcWalletService) NodeAddress(bisq.network.p2p.NodeAddress)

Example 8 with BtcWalletService

use of bisq.core.btc.wallet.BtcWalletService in project bisq-core by bisq-network.

the class TradableList method fromProto.

@Nullable
public static TradableList fromProto(PB.TradableList proto, CoreProtoResolver coreProtoResolver, Storage<TradableList<Tradable>> storage, BtcWalletService btcWalletService) {
    log.debug("TradableList fromProto of {} ", proto);
    List<Tradable> list = proto.getTradableList().stream().map(tradable -> {
        switch(tradable.getMessageCase()) {
            case OPEN_OFFER:
                return OpenOffer.fromProto(tradable.getOpenOffer());
            case BUYER_AS_MAKER_TRADE:
                return BuyerAsMakerTrade.fromProto(tradable.getBuyerAsMakerTrade(), storage, btcWalletService, coreProtoResolver);
            case BUYER_AS_TAKER_TRADE:
                return BuyerAsTakerTrade.fromProto(tradable.getBuyerAsTakerTrade(), storage, btcWalletService, coreProtoResolver);
            case SELLER_AS_MAKER_TRADE:
                return SellerAsMakerTrade.fromProto(tradable.getSellerAsMakerTrade(), storage, btcWalletService, coreProtoResolver);
            case SELLER_AS_TAKER_TRADE:
                return SellerAsTakerTrade.fromProto(tradable.getSellerAsTakerTrade(), storage, btcWalletService, coreProtoResolver);
            default:
                log.error("Unknown messageCase. tradable.getMessageCase() = " + tradable.getMessageCase());
                throw new ProtobufferException("Unknown messageCase. tradable.getMessageCase() = " + tradable.getMessageCase());
        }
    }).collect(Collectors.toList());
    return new TradableList<>(storage, list);
}
Also used : BtcWalletService(bisq.core.btc.wallet.BtcWalletService) ProtoUtil(bisq.common.proto.ProtoUtil) PersistableEnvelope(bisq.common.proto.persistable.PersistableEnvelope) Getter(lombok.Getter) OpenOffer(bisq.core.offer.OpenOffer) FXCollections(javafx.collections.FXCollections) Collectors(java.util.stream.Collectors) ProtobufferException(bisq.common.proto.ProtobufferException) ArrayList(java.util.ArrayList) CoreProtoResolver(bisq.core.proto.CoreProtoResolver) Consumer(java.util.function.Consumer) PB(io.bisq.generated.protobuffer.PB) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) Stream(java.util.stream.Stream) Storage(bisq.common.storage.Storage) Message(com.google.protobuf.Message) ObservableList(javafx.collections.ObservableList) Nullable(javax.annotation.Nullable) ProtobufferException(bisq.common.proto.ProtobufferException) Nullable(javax.annotation.Nullable)

Example 9 with BtcWalletService

use of bisq.core.btc.wallet.BtcWalletService 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.getOrCreateAddressEntry(AddressEntry.Context.AVAILABLE).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 10 with BtcWalletService

use of bisq.core.btc.wallet.BtcWalletService 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.getOrCreateAddressEntry(AddressEntry.Context.AVAILABLE).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)

Aggregations

BtcWalletService (bisq.core.btc.wallet.BtcWalletService)23 AddressEntry (bisq.core.btc.AddressEntry)15 Coin (org.bitcoinj.core.Coin)11 Transaction (org.bitcoinj.core.Transaction)10 Address (org.bitcoinj.core.Address)8 TradingPeer (bisq.core.trade.protocol.TradingPeer)7 NodeAddress (bisq.network.p2p.NodeAddress)5 RawTransactionInput (bisq.core.btc.data.RawTransactionInput)4 BsqWalletService (bisq.core.btc.wallet.BsqWalletService)4 Offer (bisq.core.offer.Offer)4 PaymentAccountPayload (bisq.core.payment.payload.PaymentAccountPayload)4 Contract (bisq.core.trade.Contract)3 Date (java.util.Date)3 Slf4j (lombok.extern.slf4j.Slf4j)3 UserThread (bisq.common.UserThread)2 TaskRunner (bisq.common.taskrunner.TaskRunner)2 Arbitrator (bisq.core.arbitration.Arbitrator)2 InputsAndChangeOutput (bisq.core.btc.data.InputsAndChangeOutput)2 PreparedDepositTxAndMakerInputs (bisq.core.btc.data.PreparedDepositTxAndMakerInputs)2 AddressConfidenceListener (bisq.core.btc.listeners.AddressConfidenceListener)2