Search in sources :

Example 1 with TradingPeer

use of bisq.core.trade.protocol.TradingPeer 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 TradingPeer

use of bisq.core.trade.protocol.TradingPeer in project bisq-core by bisq-network.

the class MakerProcessPayDepositRequest method run.

@Override
protected void run() {
    try {
        runInterceptHook();
        log.debug("current trade state " + trade.getState());
        PayDepositRequest payDepositRequest = (PayDepositRequest) processModel.getTradeMessage();
        checkNotNull(payDepositRequest);
        checkTradeId(processModel.getOfferId(), payDepositRequest);
        final TradingPeer tradingPeer = processModel.getTradingPeer();
        tradingPeer.setPaymentAccountPayload(checkNotNull(payDepositRequest.getTakerPaymentAccountPayload()));
        tradingPeer.setRawTransactionInputs(checkNotNull(payDepositRequest.getRawTransactionInputs()));
        checkArgument(payDepositRequest.getRawTransactionInputs().size() > 0);
        tradingPeer.setChangeOutputValue(payDepositRequest.getChangeOutputValue());
        tradingPeer.setChangeOutputAddress(payDepositRequest.getChangeOutputAddress());
        tradingPeer.setMultiSigPubKey(checkNotNull(payDepositRequest.getTakerMultiSigPubKey()));
        tradingPeer.setPayoutAddressString(nonEmptyStringOf(payDepositRequest.getTakerPayoutAddressString()));
        tradingPeer.setPubKeyRing(checkNotNull(payDepositRequest.getTakerPubKeyRing()));
        tradingPeer.setAccountId(nonEmptyStringOf(payDepositRequest.getTakerAccountId()));
        trade.setTakerFeeTxId(nonEmptyStringOf(payDepositRequest.getTakerFeeTxId()));
        processModel.setTakerAcceptedArbitratorNodeAddresses(checkNotNull(payDepositRequest.getAcceptedArbitratorNodeAddresses()));
        processModel.setTakerAcceptedMediatorNodeAddresses(checkNotNull(payDepositRequest.getAcceptedMediatorNodeAddresses()));
        if (payDepositRequest.getAcceptedArbitratorNodeAddresses().isEmpty())
            failed("acceptedArbitratorNodeAddresses must not be empty");
        // Taker has to sign offerId (he cannot manipulate that - so we avoid to have a challenge protocol for passing the nonce we want to get signed)
        tradingPeer.setAccountAgeWitnessNonce(trade.getOffer().getId().getBytes());
        tradingPeer.setAccountAgeWitnessSignature(payDepositRequest.getAccountAgeWitnessSignatureOfOfferId());
        tradingPeer.setCurrentDate(payDepositRequest.getCurrentDate());
        trade.setArbitratorNodeAddress(checkNotNull(payDepositRequest.getArbitratorNodeAddress()));
        trade.setMediatorNodeAddress(checkNotNull(payDepositRequest.getMediatorNodeAddress()));
        try {
            long takersTradePrice = payDepositRequest.getTradePrice();
            trade.getOffer().checkTradePriceTolerance(takersTradePrice);
            trade.setTradePrice(takersTradePrice);
        } catch (TradePriceOutOfToleranceException e) {
            failed(e.getMessage());
        } catch (Throwable e2) {
            failed(e2);
        }
        checkArgument(payDepositRequest.getTradeAmount() > 0);
        trade.setTradeAmount(Coin.valueOf(payDepositRequest.getTradeAmount()));
        trade.setTradingPeerNodeAddress(processModel.getTempTradingPeerNodeAddress());
        processModel.removeMailboxMessageAfterProcessing(trade);
        complete();
    } catch (Throwable t) {
        failed(t);
    }
}
Also used : PayDepositRequest(bisq.core.trade.messages.PayDepositRequest) TradingPeer(bisq.core.trade.protocol.TradingPeer) TradePriceOutOfToleranceException(bisq.core.exceptions.TradePriceOutOfToleranceException)

Example 3 with TradingPeer

use of bisq.core.trade.protocol.TradingPeer 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 4 with TradingPeer

use of bisq.core.trade.protocol.TradingPeer 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 5 with TradingPeer

use of bisq.core.trade.protocol.TradingPeer in project bisq-core by bisq-network.

the class VerifyPeersAccountAgeWitness method run.

@Override
protected void run() {
    try {
        runInterceptHook();
        if (CurrencyUtil.isFiatCurrency(trade.getOffer().getCurrencyCode())) {
            final AccountAgeWitnessService accountAgeWitnessService = processModel.getAccountAgeWitnessService();
            final TradingPeer tradingPeer = processModel.getTradingPeer();
            final PaymentAccountPayload peersPaymentAccountPayload = checkNotNull(tradingPeer.getPaymentAccountPayload(), "Peers peersPaymentAccountPayload must not be null");
            final PubKeyRing peersPubKeyRing = checkNotNull(tradingPeer.getPubKeyRing(), "peersPubKeyRing must not be null");
            byte[] nonce = tradingPeer.getAccountAgeWitnessNonce();
            byte[] signature = tradingPeer.getAccountAgeWitnessSignature();
            if (nonce != null && signature != null) {
                final String[] errorMsg = new String[1];
                long currentDateAsLong = tradingPeer.getCurrentDate();
                // In case the peer has an older version we get 0, so we use our time instead
                final Date peersCurrentDate = currentDateAsLong > 0 ? new Date(currentDateAsLong) : new Date();
                boolean result = accountAgeWitnessService.verifyAccountAgeWitness(trade, peersPaymentAccountPayload, peersCurrentDate, peersPubKeyRing, nonce, signature, errorMessage -> errorMsg[0] = errorMessage);
                if (result)
                    complete();
                else
                    failed(errorMsg[0]);
            } else {
                String msg = "Seems that offer was created with an application before v0.6 which did not support the account age witness verification.";
                msg += "\nTrade ID=" + trade.getId();
                if (new Date().after(AccountAgeWitnessService.FULL_ACTIVATION)) {
                    msg = "The account age witness verification failed.\nReason: " + msg + "\nAfter first of Feb. 2018 we don't support old offers without account age witness verification anymore.";
                    log.error(msg);
                    failed(msg);
                } else {
                    log.warn(msg + "\nWe tolerate offers without account age witness until first of Feb. 2018");
                    complete();
                }
            }
        } else {
            complete();
        }
    } catch (Throwable t) {
        failed(t);
    }
}
Also used : TradingPeer(bisq.core.trade.protocol.TradingPeer) PaymentAccountPayload(bisq.core.payment.payload.PaymentAccountPayload) PubKeyRing(bisq.common.crypto.PubKeyRing) AccountAgeWitnessService(bisq.core.payment.AccountAgeWitnessService) Date(java.util.Date)

Aggregations

TradingPeer (bisq.core.trade.protocol.TradingPeer)10 AddressEntry (bisq.core.btc.AddressEntry)7 BtcWalletService (bisq.core.btc.wallet.BtcWalletService)7 Coin (org.bitcoinj.core.Coin)6 RawTransactionInput (bisq.core.btc.data.RawTransactionInput)4 Offer (bisq.core.offer.Offer)3 PaymentAccountPayload (bisq.core.payment.payload.PaymentAccountPayload)3 Contract (bisq.core.trade.Contract)3 Transaction (org.bitcoinj.core.Transaction)3 PreparedDepositTxAndMakerInputs (bisq.core.btc.data.PreparedDepositTxAndMakerInputs)2 NodeAddress (bisq.network.p2p.NodeAddress)2 Address (org.bitcoinj.core.Address)2 PubKeyRing (bisq.common.crypto.PubKeyRing)1 TradePriceOutOfToleranceException (bisq.core.exceptions.TradePriceOutOfToleranceException)1 AccountAgeWitnessService (bisq.core.payment.AccountAgeWitnessService)1 BuyerAsMakerTrade (bisq.core.trade.BuyerAsMakerTrade)1 SellerAsTakerTrade (bisq.core.trade.SellerAsTakerTrade)1 PayDepositRequest (bisq.core.trade.messages.PayDepositRequest)1 PublishDepositTxRequest (bisq.core.trade.messages.PublishDepositTxRequest)1 Date (java.util.Date)1