Search in sources :

Example 6 with Transaction

use of org.bitcoinj.core.Transaction in project bitsquare by bitsquare.

the class SignAndFinalizePayoutTx method run.

@Override
protected void run() {
    try {
        runInterceptHook();
        checkNotNull(trade.getTradeAmount(), "trade.getTradeAmount() must not be null");
        Coin sellerPayoutAmount = FeePolicy.getSecurityDeposit(trade.getOffer());
        Coin buyerPayoutAmount = sellerPayoutAmount.add(trade.getTradeAmount());
        WalletService walletService = processModel.getWalletService();
        AddressEntry buyerAddressEntry = walletService.getOrCreateAddressEntry(processModel.getOffer().getId(), AddressEntry.Context.TRADE_PAYOUT);
        AddressEntry multiSigAddressEntry = walletService.getOrCreateAddressEntry(processModel.getOffer().getId(), AddressEntry.Context.MULTI_SIG);
        Transaction transaction = processModel.getTradeWalletService().buyerSignsAndFinalizesPayoutTx(trade.getDepositTx(), processModel.tradingPeer.getSignature(), buyerPayoutAmount, sellerPayoutAmount, buyerAddressEntry.getAddressString(), processModel.tradingPeer.getPayoutAddressString(), multiSigAddressEntry.getKeyPair(), trade.getLockTimeAsBlockHeight(), multiSigAddressEntry.getPubKey(), processModel.tradingPeer.getMultiSigPubKey(), trade.getArbitratorPubKey());
        trade.setPayoutTx(transaction);
        trade.setState(Trade.State.BUYER_COMMITTED_PAYOUT_TX);
        complete();
    } catch (Throwable t) {
        failed(t);
    }
}
Also used : Coin(org.bitcoinj.core.Coin) Transaction(org.bitcoinj.core.Transaction) AddressEntry(io.bitsquare.btc.AddressEntry) WalletService(io.bitsquare.btc.WalletService)

Example 7 with Transaction

use of org.bitcoinj.core.Transaction in project bitsquare by bitsquare.

the class ProcessDepositTxPublishedMessage method run.

@Override
protected void run() {
    try {
        runInterceptHook();
        log.debug("current trade state " + trade.getState());
        DepositTxPublishedMessage message = (DepositTxPublishedMessage) processModel.getTradeMessage();
        checkTradeId(processModel.getId(), message);
        checkNotNull(message);
        checkArgument(message.depositTx != null);
        // To access tx confidence we need to add that tx into our wallet.
        Transaction transactionFromSerializedTx = processModel.getWalletService().getTransactionFromSerializedTx(message.depositTx);
        // update with full tx
        trade.setDepositTx(processModel.getTradeWalletService().addTransactionToWallet(transactionFromSerializedTx));
        if (trade instanceof OffererTrade)
            processModel.getOpenOfferManager().closeOpenOffer(trade.getOffer());
        // update to the latest peer address of our peer if the message is correct
        trade.setTradingPeerNodeAddress(processModel.getTempTradingPeerNodeAddress());
        removeMailboxMessageAfterProcessing();
        trade.setState(Trade.State.OFFERER_RECEIVED_DEPOSIT_TX_PUBLISHED_MSG);
        complete();
    } catch (Throwable t) {
        failed(t);
    }
}
Also used : Transaction(org.bitcoinj.core.Transaction) DepositTxPublishedMessage(io.bitsquare.trade.protocol.trade.messages.DepositTxPublishedMessage) OffererTrade(io.bitsquare.trade.OffererTrade)

Example 8 with Transaction

use of org.bitcoinj.core.Transaction 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 9 with Transaction

use of org.bitcoinj.core.Transaction 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 Transaction

use of org.bitcoinj.core.Transaction in project bitsquare by bitsquare.

the class SpendFromDepositTxWindow method addContent.

private void addContent() {
    InputTextField depositTxHex = addLabelInputTextField(gridPane, ++rowIndex, "depositTxHex:").second;
    InputTextField buyerPayoutAmount = addLabelInputTextField(gridPane, ++rowIndex, "buyerPayoutAmount:").second;
    InputTextField sellerPayoutAmount = addLabelInputTextField(gridPane, ++rowIndex, "sellerPayoutAmount:").second;
    InputTextField arbitratorPayoutAmount = addLabelInputTextField(gridPane, ++rowIndex, "arbitratorPayoutAmount:").second;
    InputTextField buyerAddressString = addLabelInputTextField(gridPane, ++rowIndex, "buyerAddressString:").second;
    InputTextField sellerAddressString = addLabelInputTextField(gridPane, ++rowIndex, "sellerAddressString:").second;
    InputTextField arbitratorAddressString = addLabelInputTextField(gridPane, ++rowIndex, "arbitratorAddressString:").second;
    InputTextField buyerPrivateKeyAsHex = addLabelInputTextField(gridPane, ++rowIndex, "buyerPrivateKeyAsHex:").second;
    InputTextField sellerPrivateKeyAsHex = addLabelInputTextField(gridPane, ++rowIndex, "sellerPrivateKeyAsHex:").second;
    InputTextField arbitratorPrivateKeyAsHex = addLabelInputTextField(gridPane, ++rowIndex, "arbitratorPrivateKeyAsHex:").second;
    InputTextField buyerPubKeyAsHex = addLabelInputTextField(gridPane, ++rowIndex, "buyerPubKeyAsHex:").second;
    InputTextField sellerPubKeyAsHex = addLabelInputTextField(gridPane, ++rowIndex, "sellerPubKeyAsHex:").second;
    InputTextField arbitratorPubKeyAsHex = addLabelInputTextField(gridPane, ++rowIndex, "arbitratorPubKeyAsHex:").second;
    InputTextField P2SHMultiSigOutputScript = addLabelInputTextField(gridPane, ++rowIndex, "P2SHMultiSigOutputScript:").second;
    // Notes: 
    // Open with alt+g and enable DEV mode
    // Priv key is only visible if pw protection is removed (wallet details data (alt+j))
    // Take P2SHMultiSigOutputScript from depositTx in blockexplorer 
    // Take missing buyerPubKeyAsHex and sellerPubKeyAsHex from contract data!
    // Lookup sellerPrivateKeyAsHex associated with sellerPubKeyAsHex (or buyers) in wallet details data 
    // sellerPubKeys/buyerPubKeys are auto generated if used the fields below
    // Never set the priv arbitr. key here!
    depositTxHex.setText("");
    P2SHMultiSigOutputScript.setText("");
    buyerPayoutAmount.setText("1.03");
    sellerPayoutAmount.setText("0.03");
    arbitratorPayoutAmount.setText("0");
    buyerAddressString.setText("");
    buyerPubKeyAsHex.setText("");
    buyerPrivateKeyAsHex.setText("");
    sellerAddressString.setText("");
    sellerPubKeyAsHex.setText("");
    sellerPrivateKeyAsHex.setText("");
    //4.9 (pkfcmj42c6es6tjt.onion)
    // arbitratorAddressString.setText("19xdeiQM2Hn2M2wbpT5imcYWzqhiSDHPy4");
    // arbitratorPubKeyAsHex.setText("02c62e794fe67f3a2115e2de4757143ff7f27bdf38aa4ae58a3595baa6d676875b");
    // 4.2 (ntjhaj27rylxwvnp.onion)
    arbitratorAddressString.setText("1FdFzBazmHQxbUbdCUJwuCtR37DrZrEobu");
    arbitratorPubKeyAsHex.setText("030fdc2ebc297df4047442f6079f1ce3b7d1938a41f88bd11497545cc94fcfd315");
    actionButtonText("Sign and publish transaction");
    FutureCallback<Transaction> callback = new FutureCallback<Transaction>() {

        @Override
        public void onSuccess(@Nullable Transaction result) {
            log.error("onSuccess");
            UserThread.execute(() -> {
                String txId = result != null ? result.getHashAsString() : "null";
                new Popup<>().information("Transaction successful published. Transaction ID: " + txId).show();
            });
        }

        @Override
        public void onFailure(Throwable t) {
            log.error(t.toString());
            log.error("onFailure");
            UserThread.execute(() -> new Popup<>().warning(t.toString()).show());
        }
    };
    onAction(() -> {
        try {
            tradeWalletService.emergencySignAndPublishPayoutTx(depositTxHex.getText(), Coin.parseCoin(buyerPayoutAmount.getText()), Coin.parseCoin(sellerPayoutAmount.getText()), Coin.parseCoin(arbitratorPayoutAmount.getText()), buyerAddressString.getText(), sellerAddressString.getText(), arbitratorAddressString.getText(), buyerPrivateKeyAsHex.getText(), sellerPrivateKeyAsHex.getText(), arbitratorPrivateKeyAsHex.getText(), buyerPubKeyAsHex.getText(), sellerPubKeyAsHex.getText(), arbitratorPubKeyAsHex.getText(), P2SHMultiSigOutputScript.getText(), callback);
        } catch (AddressFormatException | WalletException | TransactionVerificationException e) {
            log.error(e.toString());
            e.printStackTrace();
            UserThread.execute(() -> new Popup<>().warning(e.toString()).show());
        }
    });
}
Also used : WalletException(io.bitsquare.btc.exceptions.WalletException) AddressFormatException(org.bitcoinj.core.AddressFormatException) TransactionVerificationException(io.bitsquare.btc.exceptions.TransactionVerificationException) FormBuilder.addLabelInputTextField(io.bitsquare.gui.util.FormBuilder.addLabelInputTextField) InputTextField(io.bitsquare.gui.components.InputTextField) Transaction(org.bitcoinj.core.Transaction) Popup(io.bitsquare.gui.main.overlays.popups.Popup) FutureCallback(com.google.common.util.concurrent.FutureCallback) Nullable(javax.annotation.Nullable)

Aggregations

Transaction (org.bitcoinj.core.Transaction)20 Coin (org.bitcoinj.core.Coin)10 AddressEntry (io.bitsquare.btc.AddressEntry)8 WalletService (io.bitsquare.btc.WalletService)8 BalanceListener (io.bitsquare.btc.listeners.BalanceListener)7 Popup (io.bitsquare.gui.main.overlays.popups.Popup)5 Arbitrator (io.bitsquare.arbitration.Arbitrator)4 Trade (io.bitsquare.trade.Trade)4 Address (org.bitcoinj.core.Address)4 Dispute (io.bitsquare.arbitration.Dispute)3 Timer (io.bitsquare.common.Timer)3 Inject (com.google.inject.Inject)2 Log (io.bitsquare.app.Log)2 DisputeManager (io.bitsquare.arbitration.DisputeManager)2 TradeWalletService (io.bitsquare.btc.TradeWalletService)2 RawTransactionInput (io.bitsquare.btc.data.RawTransactionInput)2 TransactionVerificationException (io.bitsquare.btc.exceptions.TransactionVerificationException)2 WalletException (io.bitsquare.btc.exceptions.WalletException)2 UserThread (io.bitsquare.common.UserThread)2 Navigation (io.bitsquare.gui.Navigation)2