Search in sources :

Example 6 with WalletService

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

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

the class CreateOfferFeeTx method run.

@Override
protected void run() {
    try {
        runInterceptHook();
        NodeAddress selectedArbitratorNodeAddress = ArbitrationSelectionRule.select(model.user.getAcceptedArbitratorAddresses(), model.offer);
        log.debug("selectedArbitratorAddress " + selectedArbitratorNodeAddress);
        Arbitrator selectedArbitrator = model.user.getAcceptedArbitratorByAddress(selectedArbitratorNodeAddress);
        checkNotNull(selectedArbitrator, "selectedArbitrator must not be null at CreateOfferFeeTx");
        WalletService walletService = model.walletService;
        String id = model.offer.getId();
        Address fundingAddress = walletService.getOrCreateAddressEntry(id, AddressEntry.Context.OFFER_FUNDING).getAddress();
        Address reservedForTradeAddress = walletService.getOrCreateAddressEntry(id, AddressEntry.Context.RESERVED_FOR_TRADE).getAddress();
        Address changeAddress = walletService.getOrCreateAddressEntry(AddressEntry.Context.AVAILABLE).getAddress();
        Transaction transaction = model.tradeWalletService.createTradingFeeTx(fundingAddress, reservedForTradeAddress, changeAddress, model.reservedFundsForOffer, model.useSavingsWallet, FeePolicy.getCreateOfferFee(), selectedArbitrator.getBtcAddress());
        // We assume there will be no tx malleability. We add a check later in case the published offer has a different hash.
        // As the txId is part of the offer and therefore change the hash data we need to be sure to have no
        // tx malleability
        model.offer.setOfferFeePaymentTxID(transaction.getHashAsString());
        model.setTransaction(transaction);
        complete();
    } catch (Throwable t) {
        model.offer.setErrorMessage("An error occurred.\n" + "Error message:\n" + t.getMessage());
        failed(t);
    }
}
Also used : NodeAddress(io.bitsquare.p2p.NodeAddress) Address(org.bitcoinj.core.Address) Transaction(org.bitcoinj.core.Transaction) NodeAddress(io.bitsquare.p2p.NodeAddress) Arbitrator(io.bitsquare.arbitration.Arbitrator) WalletService(io.bitsquare.btc.WalletService)

Example 8 with WalletService

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

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

use of io.bitsquare.btc.WalletService 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)

Aggregations

WalletService (io.bitsquare.btc.WalletService)20 AddressEntry (io.bitsquare.btc.AddressEntry)18 Coin (org.bitcoinj.core.Coin)10 Address (org.bitcoinj.core.Address)8 Transaction (org.bitcoinj.core.Transaction)7 NodeAddress (io.bitsquare.p2p.NodeAddress)4 UserThread (io.bitsquare.common.UserThread)3 Popup (io.bitsquare.gui.main.overlays.popups.Popup)3 Offer (io.bitsquare.trade.offer.Offer)3 TradingPeer (io.bitsquare.trade.protocol.trade.TradingPeer)3 AlertManager (io.bitsquare.alert.AlertManager)2 Arbitrator (io.bitsquare.arbitration.Arbitrator)2 ArbitratorManager (io.bitsquare.arbitration.ArbitratorManager)2 TradeWalletService (io.bitsquare.btc.TradeWalletService)2 InputsAndChangeOutput (io.bitsquare.btc.data.InputsAndChangeOutput)2 PreparedDepositTxAndOffererInputs (io.bitsquare.btc.data.PreparedDepositTxAndOffererInputs)2 RawTransactionInput (io.bitsquare.btc.data.RawTransactionInput)2 BalanceListener (io.bitsquare.btc.listeners.BalanceListener)2 FilterManager (io.bitsquare.filter.FilterManager)2 BSFormatter (io.bitsquare.gui.util.BSFormatter)2