Search in sources :

Example 1 with BtcWalletService

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

the class MakerCreateAndSignContract method run.

@Override
protected void run() {
    try {
        runInterceptHook();
        Preconditions.checkNotNull(trade.getTakerFeeTxId(), "trade.getTakeOfferFeeTxId() must not be null");
        TradingPeer taker = processModel.getTradingPeer();
        PaymentAccountPayload makerPaymentAccountPayload = processModel.getPaymentAccountPayload(trade);
        checkNotNull(makerPaymentAccountPayload, "makerPaymentAccountPayload must not be null");
        PaymentAccountPayload takerPaymentAccountPayload = taker.getPaymentAccountPayload();
        boolean isBuyerMakerAndSellerTaker = trade instanceof BuyerAsMakerTrade;
        NodeAddress buyerNodeAddress = isBuyerMakerAndSellerTaker ? processModel.getMyNodeAddress() : processModel.getTempTradingPeerNodeAddress();
        NodeAddress sellerNodeAddress = isBuyerMakerAndSellerTaker ? processModel.getTempTradingPeerNodeAddress() : processModel.getMyNodeAddress();
        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 makerAddressEntry = walletService.getOrCreateAddressEntry(id, AddressEntry.Context.MULTI_SIG);
        byte[] makerMultiSigPubKey = makerAddressEntry.getPubKey();
        AddressEntry takerAddressEntry = walletService.getOrCreateAddressEntry(id, AddressEntry.Context.TRADE_PAYOUT);
        checkNotNull(trade.getTradeAmount(), "trade.getTradeAmount() must not be null");
        Contract contract = new Contract(processModel.getOffer().getOfferPayload(), trade.getTradeAmount().value, trade.getTradePrice().getValue(), trade.getTakerFeeTxId(), buyerNodeAddress, sellerNodeAddress, trade.getArbitratorNodeAddress(), trade.getMediatorNodeAddress(), isBuyerMakerAndSellerTaker, processModel.getAccountId(), taker.getAccountId(), makerPaymentAccountPayload, takerPaymentAccountPayload, processModel.getPubKeyRing(), taker.getPubKeyRing(), takerAddressEntry.getAddressString(), taker.getPayoutAddressString(), makerMultiSigPubKey, taker.getMultiSigPubKey());
        String contractAsJson = Utilities.objectToJson(contract);
        log.trace("Contract as json:{}", contractAsJson);
        String signature = Sig.sign(processModel.getKeyRing().getSignatureKeyPair().getPrivate(), contractAsJson);
        trade.setContract(contract);
        trade.setContractAsJson(contractAsJson);
        trade.setMakerContractSignature(signature);
        processModel.setMyMultiSigPubKey(makerMultiSigPubKey);
        complete();
    } catch (Throwable t) {
        failed(t);
    }
}
Also used : TradingPeer(bisq.core.trade.protocol.TradingPeer) BuyerAsMakerTrade(bisq.core.trade.BuyerAsMakerTrade) BtcWalletService(bisq.core.btc.wallet.BtcWalletService) AddressEntry(bisq.core.btc.AddressEntry) PaymentAccountPayload(bisq.core.payment.payload.PaymentAccountPayload) NodeAddress(bisq.network.p2p.NodeAddress) Contract(bisq.core.trade.Contract)

Example 2 with BtcWalletService

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

the class MakerSendPublishDepositTxRequest method run.

@Override
protected void run() {
    try {
        runInterceptHook();
        BtcWalletService walletService = processModel.getBtcWalletService();
        String id = processModel.getOffer().getId();
        Optional<AddressEntry> addressEntryOptional = walletService.getAddressEntry(id, AddressEntry.Context.MULTI_SIG);
        checkArgument(addressEntryOptional.isPresent(), "addressEntry must be set here.");
        AddressEntry makerPayoutAddressEntry = walletService.getOrCreateAddressEntry(id, AddressEntry.Context.TRADE_PAYOUT);
        byte[] makerMultiSigPubKey = processModel.getMyMultiSigPubKey();
        checkArgument(Arrays.equals(makerMultiSigPubKey, addressEntryOptional.get().getPubKey()), "makerMultiSigPubKey from AddressEntry must match the one from the trade data. trade id =" + id);
        final byte[] preparedDepositTx = processModel.getPreparedDepositTx();
        // Maker has to use preparedDepositTx as nonce.
        // He cannot manipulate the preparedDepositTx - 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(), preparedDepositTx);
        PublishDepositTxRequest message = new PublishDepositTxRequest(processModel.getOfferId(), paymentAccountPayload, processModel.getAccountId(), makerMultiSigPubKey, trade.getContractAsJson(), trade.getMakerContractSignature(), makerPayoutAddressEntry.getAddressString(), preparedDepositTx, processModel.getRawTransactionInputs(), processModel.getMyNodeAddress(), UUID.randomUUID().toString(), sig, new Date().getTime());
        trade.setState(Trade.State.MAKER_SENT_PUBLISH_DEPOSIT_TX_REQUEST);
        processModel.getP2PService().sendEncryptedMailboxMessage(trade.getTradingPeerNodeAddress(), processModel.getTradingPeer().getPubKeyRing(), message, new SendMailboxMessageListener() {

            @Override
            public void onArrived() {
                log.info("Message arrived at peer. tradeId={}", id);
                trade.setState(Trade.State.MAKER_SAW_ARRIVED_PUBLISH_DEPOSIT_TX_REQUEST);
                complete();
            }

            @Override
            public void onStoredInMailbox() {
                log.info("Message stored in mailbox. tradeId={}", id);
                trade.setState(Trade.State.MAKER_STORED_IN_MAILBOX_PUBLISH_DEPOSIT_TX_REQUEST);
                complete();
            }

            @Override
            public void onFault(String errorMessage) {
                log.error("sendEncryptedMailboxMessage failed. message=" + message);
                trade.setState(Trade.State.MAKER_SEND_FAILED_PUBLISH_DEPOSIT_TX_REQUEST);
                appendToErrorMessage("Sending message failed: message=" + message + "\nerrorMessage=" + errorMessage);
                failed(errorMessage);
            }
        });
    } catch (Throwable t) {
        failed(t);
    }
}
Also used : BtcWalletService(bisq.core.btc.wallet.BtcWalletService) AddressEntry(bisq.core.btc.AddressEntry) PaymentAccountPayload(bisq.core.payment.payload.PaymentAccountPayload) SendMailboxMessageListener(bisq.network.p2p.SendMailboxMessageListener) PublishDepositTxRequest(bisq.core.trade.messages.PublishDepositTxRequest) Date(java.util.Date)

Example 3 with BtcWalletService

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

the class MakerSetupDepositTxListener method run.

@Override
protected void run() {
    try {
        runInterceptHook();
        if (trade.getDepositTx() == null && processModel.getPreparedDepositTx() != null) {
            BtcWalletService walletService = processModel.getBtcWalletService();
            final NetworkParameters params = walletService.getParams();
            Transaction preparedDepositTx = new Transaction(params, processModel.getPreparedDepositTx());
            checkArgument(!preparedDepositTx.getOutputs().isEmpty(), "preparedDepositTx.getOutputs() must not be empty");
            Address depositTxAddress = preparedDepositTx.getOutput(0).getAddressFromP2SH(params);
            final TransactionConfidence confidence = walletService.getConfidenceForAddress(depositTxAddress);
            if (isInNetwork(confidence)) {
                applyConfidence(confidence);
            } else {
                confidenceListener = new AddressConfidenceListener(depositTxAddress) {

                    @Override
                    public void onTransactionConfidenceChanged(TransactionConfidence confidence) {
                        if (isInNetwork(confidence))
                            applyConfidence(confidence);
                    }
                };
                walletService.addAddressConfidenceListener(confidenceListener);
                tradeStateSubscription = EasyBind.subscribe(trade.stateProperty(), newValue -> {
                    if (trade.isDepositPublished()) {
                        swapReservedForTradeEntry();
                        // hack to remove tradeStateSubscription at callback
                        UserThread.execute(this::unSubscribe);
                    }
                });
            }
        }
        // we complete immediately, our object stays alive because the balanceListener is stored in the WalletService
        complete();
    } catch (Throwable t) {
        failed(t);
    }
}
Also used : BtcWalletService(bisq.core.btc.wallet.BtcWalletService) Transaction(org.bitcoinj.core.Transaction) TransactionConfidence(org.bitcoinj.core.TransactionConfidence) Trade(bisq.core.trade.Trade) Subscription(org.fxmisc.easybind.Subscription) NetworkParameters(org.bitcoinj.core.NetworkParameters) Slf4j(lombok.extern.slf4j.Slf4j) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) TaskRunner(bisq.common.taskrunner.TaskRunner) AddressEntry(bisq.core.btc.AddressEntry) EasyBind(org.fxmisc.easybind.EasyBind) UserThread(bisq.common.UserThread) Address(org.bitcoinj.core.Address) AddressConfidenceListener(bisq.core.btc.listeners.AddressConfidenceListener) TradeTask(bisq.core.trade.protocol.tasks.TradeTask) AddressConfidenceListener(bisq.core.btc.listeners.AddressConfidenceListener) Transaction(org.bitcoinj.core.Transaction) Address(org.bitcoinj.core.Address) BtcWalletService(bisq.core.btc.wallet.BtcWalletService) NetworkParameters(org.bitcoinj.core.NetworkParameters) TransactionConfidence(org.bitcoinj.core.TransactionConfidence)

Example 4 with BtcWalletService

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

the class SellerSignAndFinalizePayoutTx method run.

@Override
protected void run() {
    try {
        runInterceptHook();
        checkNotNull(trade.getTradeAmount(), "trade.getTradeAmount() must not be null");
        Offer offer = trade.getOffer();
        TradingPeer tradingPeer = processModel.getTradingPeer();
        BtcWalletService walletService = processModel.getBtcWalletService();
        String id = processModel.getOffer().getId();
        final byte[] buyerSignature = tradingPeer.getSignature();
        Coin buyerPayoutAmount = offer.getBuyerSecurityDeposit().add(trade.getTradeAmount());
        Coin sellerPayoutAmount = offer.getSellerSecurityDeposit();
        final String buyerPayoutAddressString = tradingPeer.getPayoutAddressString();
        String sellerPayoutAddressString = walletService.getOrCreateAddressEntry(id, AddressEntry.Context.TRADE_PAYOUT).getAddressString();
        final byte[] buyerMultiSigPubKey = tradingPeer.getMultiSigPubKey();
        byte[] sellerMultiSigPubKey = processModel.getMyMultiSigPubKey();
        Optional<AddressEntry> MultiSigAddressEntryOptional = walletService.getAddressEntry(id, AddressEntry.Context.MULTI_SIG);
        checkArgument(MultiSigAddressEntryOptional.isPresent() && Arrays.equals(sellerMultiSigPubKey, MultiSigAddressEntryOptional.get().getPubKey()), "sellerMultiSigPubKey from AddressEntry must match the one from the trade data. trade id =" + id);
        DeterministicKey multiSigKeyPair = walletService.getMultiSigKeyPair(id, sellerMultiSigPubKey);
        Transaction transaction = processModel.getTradeWalletService().sellerSignsAndFinalizesPayoutTx(trade.getDepositTx(), buyerSignature, buyerPayoutAmount, sellerPayoutAmount, buyerPayoutAddressString, sellerPayoutAddressString, multiSigKeyPair, buyerMultiSigPubKey, sellerMultiSigPubKey, trade.getArbitratorBtcPubKey());
        trade.setPayoutTx(transaction);
        walletService.swapTradeEntryToAvailableEntry(id, AddressEntry.Context.MULTI_SIG);
        complete();
    } catch (Throwable t) {
        failed(t);
    }
}
Also used : Coin(org.bitcoinj.core.Coin) TradingPeer(bisq.core.trade.protocol.TradingPeer) Transaction(org.bitcoinj.core.Transaction) Offer(bisq.core.offer.Offer) BtcWalletService(bisq.core.btc.wallet.BtcWalletService) AddressEntry(bisq.core.btc.AddressEntry) DeterministicKey(org.bitcoinj.crypto.DeterministicKey)

Example 5 with BtcWalletService

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

the class SellerAsTakerCreatesDepositTxInputs method run.

@Override
protected void run() {
    try {
        runInterceptHook();
        if (trade.getTradeAmount() != null) {
            Coin txFee = trade.getTxFee();
            Coin takerInputAmount = trade.getOffer().getSellerSecurityDeposit().add(txFee).add(txFee).add(trade.getTradeAmount());
            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, takersAddress, takersChangeAddress);
            processModel.setRawTransactionInputs(result.rawTransactionInputs);
            processModel.setChangeOutputValue(result.changeOutputValue);
            processModel.setChangeOutputAddress(result.changeOutputAddress);
            complete();
        } else {
            failed("trade.getTradeAmount() = null");
        }
    } 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)24 AddressEntry (bisq.core.btc.AddressEntry)16 Coin (org.bitcoinj.core.Coin)12 Transaction (org.bitcoinj.core.Transaction)11 Address (org.bitcoinj.core.Address)9 TradingPeer (bisq.core.trade.protocol.TradingPeer)7 NodeAddress (bisq.network.p2p.NodeAddress)6 BsqWalletService (bisq.core.btc.wallet.BsqWalletService)5 Offer (bisq.core.offer.Offer)5 RawTransactionInput (bisq.core.btc.data.RawTransactionInput)4 Slf4j (lombok.extern.slf4j.Slf4j)4 Arbitrator (bisq.core.arbitration.Arbitrator)3 PaymentAccountPayload (bisq.core.payment.payload.PaymentAccountPayload)3 User (bisq.core.user.User)3 Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)3 FutureCallback (com.google.common.util.concurrent.FutureCallback)3 Date (java.util.Date)3 NotNull (org.jetbrains.annotations.NotNull)3 UserThread (bisq.common.UserThread)2 Storage (bisq.common.storage.Storage)2