Search in sources :

Example 11 with WalletService

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

Example 12 with WalletService

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

the class OffererCreatesAndSignsDepositTxAsBuyer method run.

@Override
protected void run() {
    try {
        runInterceptHook();
        checkNotNull(trade.getTradeAmount(), "trade.getTradeAmount() must not be null");
        Offer offer = trade.getOffer();
        Coin securityDeposit = FeePolicy.getSecurityDeposit(offer);
        Coin buyerInputAmount = securityDeposit.add(FeePolicy.getFixedTxFeeForTrades(offer));
        Coin msOutputAmount = buyerInputAmount.add(securityDeposit).add(trade.getTradeAmount());
        log.debug("\n\n------------------------------------------------------------\n" + "Contract as json\n" + trade.getContractAsJson() + "\n------------------------------------------------------------\n");
        byte[] contractHash = Hash.getHash(trade.getContractAsJson());
        trade.setContractHash(contractHash);
        WalletService walletService = processModel.getWalletService();
        String id = processModel.getOffer().getId();
        AddressEntry offererAddressEntry = walletService.getOrCreateAddressEntry(id, AddressEntry.Context.RESERVED_FOR_TRADE);
        AddressEntry buyerMultiSigAddressEntry = walletService.getOrCreateAddressEntry(id, AddressEntry.Context.MULTI_SIG);
        buyerMultiSigAddressEntry.setCoinLockedInMultiSig(buyerInputAmount.subtract(FeePolicy.getFixedTxFeeForTrades(offer)));
        Address changeAddress = walletService.getOrCreateAddressEntry(AddressEntry.Context.AVAILABLE).getAddress();
        PreparedDepositTxAndOffererInputs result = processModel.getTradeWalletService().offererCreatesAndSignsDepositTx(true, contractHash, buyerInputAmount, msOutputAmount, processModel.tradingPeer.getRawTransactionInputs(), processModel.tradingPeer.getChangeOutputValue(), processModel.tradingPeer.getChangeOutputAddress(), offererAddressEntry.getAddress(), changeAddress, buyerMultiSigAddressEntry.getPubKey(), processModel.tradingPeer.getMultiSigPubKey(), trade.getArbitratorPubKey());
        processModel.setPreparedDepositTx(result.depositTransaction);
        processModel.setRawTransactionInputs(result.rawOffererInputs);
        complete();
    } catch (Throwable t) {
        failed(t);
    }
}
Also used : Coin(org.bitcoinj.core.Coin) Address(org.bitcoinj.core.Address) Offer(io.bitsquare.trade.offer.Offer) AddressEntry(io.bitsquare.btc.AddressEntry) PreparedDepositTxAndOffererInputs(io.bitsquare.btc.data.PreparedDepositTxAndOffererInputs) WalletService(io.bitsquare.btc.WalletService)

Example 13 with WalletService

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

the class SendFiatTransferStartedMessage method run.

@Override
protected void run() {
    try {
        runInterceptHook();
        WalletService walletService = processModel.getWalletService();
        AddressEntry payoutAddressEntry = walletService.getOrCreateAddressEntry(processModel.getOffer().getId(), AddressEntry.Context.TRADE_PAYOUT);
        processModel.getP2PService().sendEncryptedMailboxMessage(trade.getTradingPeerNodeAddress(), processModel.tradingPeer.getPubKeyRing(), new FiatTransferStartedMessage(processModel.getId(), payoutAddressEntry.getAddressString(), processModel.getMyNodeAddress()), new SendMailboxMessageListener() {

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

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

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

Example 14 with WalletService

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

Example 15 with WalletService

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

the class SetupDepositBalanceListener method run.

@Override
protected void run() {
    try {
        runInterceptHook();
        WalletService walletService = processModel.getWalletService();
        Address address = walletService.getOrCreateAddressEntry(trade.getId(), AddressEntry.Context.RESERVED_FOR_TRADE).getAddress();
        balanceListener = new BalanceListener(address) {

            @Override
            public void onBalanceChanged(Coin balance, Transaction tx) {
                updateBalance(balance);
            }
        };
        walletService.addBalanceListener(balanceListener);
        tradeStateSubscription = EasyBind.subscribe(trade.stateProperty(), newValue -> {
            log.debug("tradeStateSubscription newValue " + newValue);
            if (newValue == Trade.State.OFFERER_RECEIVED_DEPOSIT_TX_PUBLISHED_MSG || newValue == Trade.State.DEPOSIT_SEEN_IN_NETWORK) {
                walletService.removeBalanceListener(balanceListener);
                UserThread.execute(this::unSubscribe);
            }
        });
        updateBalance(walletService.getBalanceForAddress(address));
        // we complete immediately, our object stays alive because the balanceListener is stored in the WalletService
        complete();
    } catch (Throwable t) {
        failed(t);
    }
}
Also used : OffererTrade(io.bitsquare.trade.OffererTrade) Transaction(org.bitcoinj.core.Transaction) Logger(org.slf4j.Logger) Coin(org.bitcoinj.core.Coin) UserThread(io.bitsquare.common.UserThread) TaskRunner(io.bitsquare.common.taskrunner.TaskRunner) LoggerFactory(org.slf4j.LoggerFactory) Trade(io.bitsquare.trade.Trade) TradeTask(io.bitsquare.trade.protocol.trade.tasks.TradeTask) Subscription(org.fxmisc.easybind.Subscription) BalanceListener(io.bitsquare.btc.listeners.BalanceListener) EasyBind(org.fxmisc.easybind.EasyBind) WalletService(io.bitsquare.btc.WalletService) Address(org.bitcoinj.core.Address) AddressEntry(io.bitsquare.btc.AddressEntry) Coin(org.bitcoinj.core.Coin) BalanceListener(io.bitsquare.btc.listeners.BalanceListener) Address(org.bitcoinj.core.Address) Transaction(org.bitcoinj.core.Transaction) 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