Search in sources :

Example 6 with AddressEntry

use of io.bitsquare.btc.AddressEntry 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 7 with AddressEntry

use of io.bitsquare.btc.AddressEntry in project bitsquare by bitsquare.

the class SendFinalizePayoutTxRequest method run.

@Override
protected void run() {
    try {
        runInterceptHook();
        if (trade.getTradingPeerNodeAddress() != null) {
            WalletService walletService = processModel.getWalletService();
            AddressEntry sellerPayoutAddressEntry = walletService.getOrCreateAddressEntry(processModel.getOffer().getId(), AddressEntry.Context.TRADE_PAYOUT);
            FinalizePayoutTxRequest message = new FinalizePayoutTxRequest(processModel.getId(), processModel.getPayoutTxSignature(), sellerPayoutAddressEntry.getAddressString(), trade.getLockTimeAsBlockHeight(), processModel.getMyNodeAddress());
            processModel.getP2PService().sendEncryptedMailboxMessage(trade.getTradingPeerNodeAddress(), processModel.tradingPeer.getPubKeyRing(), message, new SendMailboxMessageListener() {

                @Override
                public void onArrived() {
                    log.trace("Message arrived at peer.");
                    trade.setState(Trade.State.SELLER_SENT_FIAT_PAYMENT_RECEIPT_MSG);
                    complete();
                }

                @Override
                public void onStoredInMailbox() {
                    log.trace("Message stored in mailbox.");
                    trade.setState(Trade.State.SELLER_SENT_FIAT_PAYMENT_RECEIPT_MSG);
                    complete();
                }

                @Override
                public void onFault(String errorMessage) {
                    appendToErrorMessage("FinalizePayoutTxRequest sending failed. errorMessage=" + errorMessage);
                    failed(errorMessage);
                }
            });
        } else {
            log.error("trade.getTradingPeerAddress() = " + trade.getTradingPeerNodeAddress());
            failed("A needed dependency is null");
        }
    } catch (Throwable t) {
        failed(t);
    }
}
Also used : AddressEntry(io.bitsquare.btc.AddressEntry) FinalizePayoutTxRequest(io.bitsquare.trade.protocol.trade.messages.FinalizePayoutTxRequest) SendMailboxMessageListener(io.bitsquare.p2p.messaging.SendMailboxMessageListener) WalletService(io.bitsquare.btc.WalletService)

Example 8 with AddressEntry

use of io.bitsquare.btc.AddressEntry 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 9 with AddressEntry

use of io.bitsquare.btc.AddressEntry in project bitsquare by bitsquare.

the class SignPayoutTx method run.

@Override
protected void run() {
    try {
        runInterceptHook();
        checkNotNull(trade.getTradeAmount(), "trade.getTradeAmount() must not be null");
        checkNotNull(trade.getDepositTx(), "trade.getDepositTx() must not be null");
        Coin sellerPayoutAmount = FeePolicy.getSecurityDeposit(trade.getOffer());
        Coin buyerPayoutAmount = sellerPayoutAmount.add(trade.getTradeAmount());
        // We use the sellers LastBlockSeenHeight, which might be different to the buyers one.
        // If lock time is 0 we set lockTimeAsBlockHeight to 0 to mark it as "not set". 
        // In the tradeWallet we apply the lockTime only if it is set, otherwise we use the default values for 
        // transaction lockTime and sequence number
        long lockTime = trade.getOffer().getPaymentMethod().getLockTime();
        long lockTimeAsBlockHeight = 0;
        if (lockTime > 0)
            lockTimeAsBlockHeight = processModel.getTradeWalletService().getLastBlockSeenHeight() + lockTime;
        trade.setLockTimeAsBlockHeight(lockTimeAsBlockHeight);
        String id = processModel.getOffer().getId();
        WalletService walletService = processModel.getWalletService();
        AddressEntry sellerPayoutAddressEntry = walletService.getOrCreateAddressEntry(id, AddressEntry.Context.TRADE_PAYOUT);
        AddressEntry multiSigAddressEntry = walletService.getOrCreateAddressEntry(id, AddressEntry.Context.MULTI_SIG);
        byte[] payoutTxSignature = processModel.getTradeWalletService().sellerSignsPayoutTx(trade.getDepositTx(), buyerPayoutAmount, sellerPayoutAmount, processModel.tradingPeer.getPayoutAddressString(), sellerPayoutAddressEntry.getAddressString(), multiSigAddressEntry.getKeyPair(), lockTimeAsBlockHeight, processModel.tradingPeer.getMultiSigPubKey(), multiSigAddressEntry.getPubKey(), trade.getArbitratorPubKey());
        processModel.setPayoutTxSignature(payoutTxSignature);
        complete();
    } catch (Throwable t) {
        failed(t);
    }
}
Also used : Coin(org.bitcoinj.core.Coin) AddressEntry(io.bitsquare.btc.AddressEntry) WalletService(io.bitsquare.btc.WalletService)

Example 10 with AddressEntry

use of io.bitsquare.btc.AddressEntry in project bitsquare by bitsquare.

the class SendPublishDepositTxRequest method run.

@Override
protected void run() {
    try {
        runInterceptHook();
        WalletService walletService = processModel.getWalletService();
        AddressEntry offererMultiSigAddressEntry = walletService.getOrCreateAddressEntry(processModel.getOffer().getId(), AddressEntry.Context.MULTI_SIG);
        AddressEntry offererPayoutAddressEntry = walletService.getOrCreateAddressEntry(processModel.getOffer().getId(), AddressEntry.Context.TRADE_PAYOUT);
        PublishDepositTxRequest tradeMessage = new PublishDepositTxRequest(processModel.getId(), processModel.getPaymentAccountContractData(trade), processModel.getAccountId(), offererMultiSigAddressEntry.getPubKey(), trade.getContractAsJson(), trade.getOffererContractSignature(), offererPayoutAddressEntry.getAddressString(), processModel.getPreparedDepositTx(), processModel.getRawTransactionInputs());
        processModel.getP2PService().sendEncryptedDirectMessage(trade.getTradingPeerNodeAddress(), processModel.tradingPeer.getPubKeyRing(), tradeMessage, new SendDirectMessageListener() {

            @Override
            public void onArrived() {
                log.trace("Message arrived at peer.");
                trade.setState(Trade.State.OFFERER_SENT_PUBLISH_DEPOSIT_TX_REQUEST);
                complete();
            }

            @Override
            public void onFault() {
                appendToErrorMessage("PublishDepositTxRequest sending failed");
                failed();
            }
        });
    } catch (Throwable t) {
        failed(t);
    }
}
Also used : AddressEntry(io.bitsquare.btc.AddressEntry) SendDirectMessageListener(io.bitsquare.p2p.messaging.SendDirectMessageListener) WalletService(io.bitsquare.btc.WalletService) PublishDepositTxRequest(io.bitsquare.trade.protocol.trade.messages.PublishDepositTxRequest)

Aggregations

AddressEntry (io.bitsquare.btc.AddressEntry)20 WalletService (io.bitsquare.btc.WalletService)16 Coin (org.bitcoinj.core.Coin)9 Transaction (org.bitcoinj.core.Transaction)6 Address (org.bitcoinj.core.Address)5 Offer (io.bitsquare.trade.offer.Offer)4 TradingPeer (io.bitsquare.trade.protocol.trade.TradingPeer)4 Popup (io.bitsquare.gui.main.overlays.popups.Popup)3 NodeAddress (io.bitsquare.p2p.NodeAddress)3 SendMailboxMessageListener (io.bitsquare.p2p.messaging.SendMailboxMessageListener)3 Contract (io.bitsquare.trade.Contract)3 AddressFormatException (org.bitcoinj.core.AddressFormatException)3 Arbitrator (io.bitsquare.arbitration.Arbitrator)2 InputsAndChangeOutput (io.bitsquare.btc.data.InputsAndChangeOutput)2 PreparedDepositTxAndOffererInputs (io.bitsquare.btc.data.PreparedDepositTxAndOffererInputs)2 RawTransactionInput (io.bitsquare.btc.data.RawTransactionInput)2 TransactionVerificationException (io.bitsquare.btc.exceptions.TransactionVerificationException)2 PaymentAccountContractData (io.bitsquare.payment.PaymentAccountContractData)2 Date (java.util.Date)2 Dispute (io.bitsquare.arbitration.Dispute)1