Search in sources :

Example 1 with TradingPeer

use of io.bitsquare.trade.protocol.trade.TradingPeer in project bitsquare by bitsquare.

the class VerifyAndSignContract method run.

@Override
protected void run() {
    try {
        runInterceptHook();
        checkNotNull(trade.getTakeOfferFeeTxId(), "TakeOfferFeeTxId must not be null");
        TradingPeer offerer = processModel.tradingPeer;
        PaymentAccountContractData offererPaymentAccountContractData = offerer.getPaymentAccountContractData();
        PaymentAccountContractData takerPaymentAccountContractData = processModel.getPaymentAccountContractData(trade);
        boolean isBuyerOffererAndSellerTaker = trade instanceof SellerAsTakerTrade;
        NodeAddress buyerNodeAddress = isBuyerOffererAndSellerTaker ? processModel.getTempTradingPeerNodeAddress() : processModel.getMyNodeAddress();
        NodeAddress sellerNodeAddress = isBuyerOffererAndSellerTaker ? processModel.getMyNodeAddress() : processModel.getTempTradingPeerNodeAddress();
        log.debug("isBuyerOffererAndSellerTaker " + isBuyerOffererAndSellerTaker);
        log.debug("buyerAddress " + buyerNodeAddress);
        log.debug("sellerAddress " + sellerNodeAddress);
        WalletService walletService = processModel.getWalletService();
        AddressEntry takerPayoutAddressEntry = walletService.getOrCreateAddressEntry(processModel.getOffer().getId(), AddressEntry.Context.TRADE_PAYOUT);
        String takerPayoutAddressString = takerPayoutAddressEntry.getAddressString();
        AddressEntry takerMultiSigAddressEntry = walletService.getOrCreateAddressEntry(processModel.getOffer().getId(), AddressEntry.Context.MULTI_SIG);
        byte[] takerMultiSigPubKey = takerMultiSigAddressEntry.getPubKey();
        Contract contract = new Contract(processModel.getOffer(), trade.getTradeAmount(), trade.getTradePrice(), trade.getTakeOfferFeeTxId(), buyerNodeAddress, sellerNodeAddress, trade.getArbitratorNodeAddress(), isBuyerOffererAndSellerTaker, offerer.getAccountId(), processModel.getAccountId(), offererPaymentAccountContractData, takerPaymentAccountContractData, offerer.getPubKeyRing(), processModel.getPubKeyRing(), offerer.getPayoutAddressString(), takerPayoutAddressString, offerer.getMultiSigPubKey(), takerMultiSigPubKey);
        String contractAsJson = Utilities.objectToJson(contract);
        String signature = Sig.sign(processModel.getKeyRing().getSignatureKeyPair().getPrivate(), contractAsJson);
        trade.setContract(contract);
        trade.setContractAsJson(contractAsJson);
        trade.setTakerContractSignature(signature);
        try {
            Sig.verify(offerer.getPubKeyRing().getSignaturePubKey(), contractAsJson, offerer.getContractSignature());
        } catch (Throwable t) {
            failed("Signature verification failed. " + t.getMessage());
        }
        complete();
    } catch (Throwable t) {
        failed(t);
    }
}
Also used : PaymentAccountContractData(io.bitsquare.payment.PaymentAccountContractData) TradingPeer(io.bitsquare.trade.protocol.trade.TradingPeer) AddressEntry(io.bitsquare.btc.AddressEntry) SellerAsTakerTrade(io.bitsquare.trade.SellerAsTakerTrade) NodeAddress(io.bitsquare.p2p.NodeAddress) Contract(io.bitsquare.trade.Contract) WalletService(io.bitsquare.btc.WalletService)

Example 2 with TradingPeer

use of io.bitsquare.trade.protocol.trade.TradingPeer in project bitsquare by bitsquare.

the class CreateAndSignContract method run.

@Override
protected void run() {
    try {
        runInterceptHook();
        checkNotNull(trade.getTakeOfferFeeTxId(), "trade.getTakeOfferFeeTxId() must not be null");
        TradingPeer taker = processModel.tradingPeer;
        PaymentAccountContractData offererPaymentAccountContractData = processModel.getPaymentAccountContractData(trade);
        checkNotNull(offererPaymentAccountContractData, "offererPaymentAccountContractData must not be null");
        PaymentAccountContractData takerPaymentAccountContractData = taker.getPaymentAccountContractData();
        boolean isBuyerOffererAndSellerTaker = trade instanceof BuyerAsOffererTrade;
        NodeAddress buyerNodeAddress = isBuyerOffererAndSellerTaker ? processModel.getMyNodeAddress() : processModel.getTempTradingPeerNodeAddress();
        NodeAddress sellerNodeAddress = isBuyerOffererAndSellerTaker ? processModel.getTempTradingPeerNodeAddress() : processModel.getMyNodeAddress();
        log.debug("isBuyerOffererAndSellerTaker " + isBuyerOffererAndSellerTaker);
        log.debug("buyerAddress " + buyerNodeAddress);
        log.debug("sellerAddress " + sellerNodeAddress);
        WalletService walletService = processModel.getWalletService();
        AddressEntry takerAddressEntry = walletService.getOrCreateAddressEntry(processModel.getOffer().getId(), AddressEntry.Context.TRADE_PAYOUT);
        AddressEntry offererAddressEntry = walletService.getOrCreateAddressEntry(processModel.getOffer().getId(), AddressEntry.Context.MULTI_SIG);
        Contract contract = new Contract(processModel.getOffer(), trade.getTradeAmount(), trade.getTradePrice(), trade.getTakeOfferFeeTxId(), buyerNodeAddress, sellerNodeAddress, trade.getArbitratorNodeAddress(), isBuyerOffererAndSellerTaker, processModel.getAccountId(), taker.getAccountId(), offererPaymentAccountContractData, takerPaymentAccountContractData, processModel.getPubKeyRing(), taker.getPubKeyRing(), takerAddressEntry.getAddressString(), taker.getPayoutAddressString(), offererAddressEntry.getPubKey(), taker.getMultiSigPubKey());
        String contractAsJson = Utilities.objectToJson(contract);
        String signature = Sig.sign(processModel.getKeyRing().getSignatureKeyPair().getPrivate(), contractAsJson);
        trade.setContract(contract);
        trade.setContractAsJson(contractAsJson);
        trade.setOffererContractSignature(signature);
        complete();
    } catch (Throwable t) {
        failed(t);
    }
}
Also used : PaymentAccountContractData(io.bitsquare.payment.PaymentAccountContractData) TradingPeer(io.bitsquare.trade.protocol.trade.TradingPeer) AddressEntry(io.bitsquare.btc.AddressEntry) NodeAddress(io.bitsquare.p2p.NodeAddress) Contract(io.bitsquare.trade.Contract) BuyerAsOffererTrade(io.bitsquare.trade.BuyerAsOffererTrade) WalletService(io.bitsquare.btc.WalletService)

Example 3 with TradingPeer

use of io.bitsquare.trade.protocol.trade.TradingPeer in project bitsquare by bitsquare.

the class SignAndPublishDepositTxAsSeller method run.

@Override
protected void run() {
    try {
        runInterceptHook();
        log.debug("\n\n------------------------------------------------------------\n" + "Contract as json\n" + trade.getContractAsJson() + "\n------------------------------------------------------------\n");
        byte[] contractHash = Hash.getHash(trade.getContractAsJson());
        trade.setContractHash(contractHash);
        ArrayList<RawTransactionInput> sellerInputs = processModel.getRawTransactionInputs();
        WalletService walletService = processModel.getWalletService();
        AddressEntry sellerMultiSigAddressEntry = walletService.getOrCreateAddressEntry(processModel.getOffer().getId(), AddressEntry.Context.MULTI_SIG);
        sellerMultiSigAddressEntry.setCoinLockedInMultiSig(Coin.valueOf(sellerInputs.stream().mapToLong(input -> input.value).sum()).subtract(FeePolicy.getFixedTxFeeForTrades(trade.getOffer())));
        TradingPeer tradingPeer = processModel.tradingPeer;
        Transaction depositTx = processModel.getTradeWalletService().takerSignsAndPublishesDepositTx(true, contractHash, processModel.getPreparedDepositTx(), tradingPeer.getRawTransactionInputs(), sellerInputs, tradingPeer.getMultiSigPubKey(), sellerMultiSigAddressEntry.getPubKey(), trade.getArbitratorPubKey(), new FutureCallback<Transaction>() {

            @Override
            public void onSuccess(Transaction transaction) {
                log.trace("takerSignAndPublishTx succeeded " + transaction);
                trade.setDepositTx(transaction);
                trade.setState(Trade.State.TAKER_PUBLISHED_DEPOSIT_TX);
                complete();
            }

            @Override
            public void onFailure(@NotNull Throwable t) {
                failed(t);
            }
        });
        trade.setDepositTx(depositTx);
    } catch (Throwable t) {
        failed(t);
    }
}
Also used : TradingPeer(io.bitsquare.trade.protocol.trade.TradingPeer) Transaction(org.bitcoinj.core.Transaction) AddressEntry(io.bitsquare.btc.AddressEntry) RawTransactionInput(io.bitsquare.btc.data.RawTransactionInput) WalletService(io.bitsquare.btc.WalletService)

Example 4 with TradingPeer

use of io.bitsquare.trade.protocol.trade.TradingPeer in project bitsquare by bitsquare.

the class SignAndPublishDepositTxAsBuyer method run.

@Override
protected void run() {
    try {
        runInterceptHook();
        log.debug("\n\n------------------------------------------------------------\n" + "Contract as json\n" + trade.getContractAsJson() + "\n------------------------------------------------------------\n");
        byte[] contractHash = Hash.getHash(trade.getContractAsJson());
        trade.setContractHash(contractHash);
        ArrayList<RawTransactionInput> buyerInputs = processModel.getRawTransactionInputs();
        WalletService walletService = processModel.getWalletService();
        AddressEntry buyerMultiSigAddressEntry = walletService.getOrCreateAddressEntry(processModel.getOffer().getId(), AddressEntry.Context.MULTI_SIG);
        Coin buyerInput = Coin.valueOf(buyerInputs.stream().mapToLong(input -> input.value).sum());
        buyerMultiSigAddressEntry.setCoinLockedInMultiSig(buyerInput.subtract(FeePolicy.getFixedTxFeeForTrades(trade.getOffer())));
        TradingPeer tradingPeer = processModel.tradingPeer;
        Transaction depositTx = processModel.getTradeWalletService().takerSignsAndPublishesDepositTx(false, contractHash, processModel.getPreparedDepositTx(), buyerInputs, tradingPeer.getRawTransactionInputs(), buyerMultiSigAddressEntry.getPubKey(), tradingPeer.getMultiSigPubKey(), trade.getArbitratorPubKey(), new FutureCallback<Transaction>() {

            @Override
            public void onSuccess(Transaction transaction) {
                log.trace("takerSignAndPublishTx succeeded " + transaction);
                trade.setDepositTx(transaction);
                trade.setState(Trade.State.TAKER_PUBLISHED_DEPOSIT_TX);
                complete();
            }

            @Override
            public void onFailure(@NotNull Throwable t) {
                failed(t);
            }
        });
        trade.setDepositTx(depositTx);
    } catch (Throwable t) {
        failed(t);
    }
}
Also used : Coin(org.bitcoinj.core.Coin) TradingPeer(io.bitsquare.trade.protocol.trade.TradingPeer) Transaction(org.bitcoinj.core.Transaction) AddressEntry(io.bitsquare.btc.AddressEntry) RawTransactionInput(io.bitsquare.btc.data.RawTransactionInput) WalletService(io.bitsquare.btc.WalletService)

Aggregations

AddressEntry (io.bitsquare.btc.AddressEntry)4 WalletService (io.bitsquare.btc.WalletService)4 TradingPeer (io.bitsquare.trade.protocol.trade.TradingPeer)4 RawTransactionInput (io.bitsquare.btc.data.RawTransactionInput)2 NodeAddress (io.bitsquare.p2p.NodeAddress)2 PaymentAccountContractData (io.bitsquare.payment.PaymentAccountContractData)2 Contract (io.bitsquare.trade.Contract)2 Transaction (org.bitcoinj.core.Transaction)2 BuyerAsOffererTrade (io.bitsquare.trade.BuyerAsOffererTrade)1 SellerAsTakerTrade (io.bitsquare.trade.SellerAsTakerTrade)1 Coin (org.bitcoinj.core.Coin)1