Search in sources :

Example 16 with AddressEntry

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

the class OffererCreatesAndSignsDepositTxAsSeller method run.

@Override
protected void run() {
    try {
        runInterceptHook();
        checkNotNull(trade.getTradeAmount(), "trade.getTradeAmount() must not be null");
        Offer offer = trade.getOffer();
        Coin sellerInputAmount = FeePolicy.getSecurityDeposit(offer).add(FeePolicy.getFixedTxFeeForTrades(offer)).add(trade.getTradeAmount());
        Coin msOutputAmount = sellerInputAmount.add(FeePolicy.getSecurityDeposit(offer));
        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 sellerMultiSigAddressEntry = walletService.getOrCreateAddressEntry(id, AddressEntry.Context.MULTI_SIG);
        sellerMultiSigAddressEntry.setCoinLockedInMultiSig(sellerInputAmount.subtract(FeePolicy.getFixedTxFeeForTrades(offer)));
        Address changeAddress = walletService.getOrCreateAddressEntry(AddressEntry.Context.AVAILABLE).getAddress();
        PreparedDepositTxAndOffererInputs result = processModel.getTradeWalletService().offererCreatesAndSignsDepositTx(false, contractHash, sellerInputAmount, msOutputAmount, processModel.tradingPeer.getRawTransactionInputs(), processModel.tradingPeer.getChangeOutputValue(), processModel.tradingPeer.getChangeOutputAddress(), offererAddressEntry.getAddress(), changeAddress, processModel.tradingPeer.getMultiSigPubKey(), sellerMultiSigAddressEntry.getPubKey(), 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 17 with AddressEntry

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

the class SendPayDepositRequest method run.

@Override
protected void run() {
    try {
        runInterceptHook();
        checkNotNull(trade.getTradeAmount(), "TradeAmount must not be null");
        checkNotNull(trade.getTakeOfferFeeTxId(), "TakeOfferFeeTxId must not be null");
        WalletService walletService = processModel.getWalletService();
        AddressEntry takerMultiSigPubKeyAddressEntry = walletService.getOrCreateAddressEntry(processModel.getOffer().getId(), AddressEntry.Context.MULTI_SIG);
        AddressEntry takerPayoutAddressEntry = walletService.getOrCreateAddressEntry(processModel.getOffer().getId(), AddressEntry.Context.TRADE_PAYOUT);
        byte[] takerMultiSigPubKey = takerMultiSigPubKeyAddressEntry.getPubKey();
        String takerPayoutAddressString = takerPayoutAddressEntry.getAddressString();
        PayDepositRequest payDepositRequest = new PayDepositRequest(processModel.getMyNodeAddress(), processModel.getId(), trade.getTradeAmount().value, trade.getTradePrice().value, processModel.getRawTransactionInputs(), processModel.getChangeOutputValue(), processModel.getChangeOutputAddress(), takerMultiSigPubKey, takerPayoutAddressString, processModel.getPubKeyRing(), processModel.getPaymentAccountContractData(trade), processModel.getAccountId(), trade.getTakeOfferFeeTxId(), new ArrayList<>(processModel.getUser().getAcceptedArbitratorAddresses()), trade.getArbitratorNodeAddress());
        processModel.getP2PService().sendEncryptedMailboxMessage(trade.getTradingPeerNodeAddress(), processModel.tradingPeer.getPubKeyRing(), payDepositRequest, new SendMailboxMessageListener() {

            @Override
            public void onArrived() {
                log.trace("Message arrived at peer.");
                complete();
            }

            @Override
            public void onStoredInMailbox() {
                log.trace("Message stored in mailbox.");
                complete();
            }

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

Example 18 with AddressEntry

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

the class TakerCreatesDepositTxInputsAsSeller method run.

@Override
protected void run() {
    try {
        runInterceptHook();
        if (trade.getTradeAmount() != null) {
            Offer offer = trade.getOffer();
            Coin takerInputAmount = FeePolicy.getSecurityDeposit(offer).add(FeePolicy.getFixedTxFeeForTrades(offer)).add(trade.getTradeAmount());
            WalletService walletService = processModel.getWalletService();
            AddressEntry takersAddressEntry = walletService.getOrCreateAddressEntry(processModel.getOffer().getId(), AddressEntry.Context.RESERVED_FOR_TRADE);
            Address changeAddress = walletService.getOrCreateAddressEntry(AddressEntry.Context.AVAILABLE).getAddress();
            InputsAndChangeOutput result = processModel.getTradeWalletService().takerCreatesDepositsTxInputs(takerInputAmount, takersAddressEntry.getAddress(), changeAddress);
            processModel.setRawTransactionInputs(result.rawTransactionInputs);
            processModel.setChangeOutputValue(result.changeOutputValue);
            processModel.setChangeOutputAddress(result.changeOutputAddress);
            complete();
        } else {
            failed("trade.getTradeAmount() = null");
        }
    } 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) InputsAndChangeOutput(io.bitsquare.btc.data.InputsAndChangeOutput) WalletService(io.bitsquare.btc.WalletService)

Example 19 with AddressEntry

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

the class DepositView method initialize.

@Override
public void initialize() {
    // trigger creation of at least 1 savings address
    walletService.getOrCreateAddressEntry(AddressEntry.Context.AVAILABLE);
    tableView.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);
    tableView.setPlaceholder(new Label("No deposit addresses have been generated yet"));
    tableViewSelectionListener = (observableValue, oldValue, newValue) -> {
        if (newValue != null)
            fillForm(newValue.getAddressString());
    };
    setSelectColumnCellFactory();
    setAddressColumnCellFactory();
    setBalanceColumnCellFactory();
    setUsageColumnCellFactory();
    setConfidenceColumnCellFactory();
    addressColumn.setComparator((o1, o2) -> o1.getAddressString().compareTo(o2.getAddressString()));
    balanceColumn.setComparator((o1, o2) -> o1.getBalanceAsCoin().compareTo(o2.getBalanceAsCoin()));
    confidenceColumn.setComparator((o1, o2) -> Double.valueOf(o1.getTxConfidenceIndicator().getProgress()).compareTo(o2.getTxConfidenceIndicator().getProgress()));
    usageColumn.setComparator((a, b) -> (a.getNumTxOutputs() < b.getNumTxOutputs()) ? -1 : ((a.getNumTxOutputs() == b.getNumTxOutputs()) ? 0 : 1));
    tableView.getSortOrder().add(usageColumn);
    tableView.setItems(sortedList);
    titledGroupBg = addTitledGroupBg(gridPane, gridRow, 3, "Fund your wallet");
    qrCodeLabel = addLabel(gridPane, gridRow, "", 0);
    //GridPane.setMargin(qrCodeLabel, new Insets(Layout.FIRST_ROW_DISTANCE - 9, 0, 0, 5));
    qrCodeImageView = new ImageView();
    qrCodeImageView.setStyle("-fx-cursor: hand;");
    Tooltip.install(qrCodeImageView, new Tooltip("Open large QR-Code window"));
    qrCodeImageView.setOnMouseClicked(e -> GUIUtil.showFeeInfoBeforeExecute(() -> UserThread.runAfter(() -> new QRCodeWindow(getBitcoinURI()).show(), 200, TimeUnit.MILLISECONDS)));
    GridPane.setRowIndex(qrCodeImageView, gridRow);
    GridPane.setColumnIndex(qrCodeImageView, 1);
    GridPane.setMargin(qrCodeImageView, new Insets(Layout.FIRST_ROW_DISTANCE, 0, 0, 0));
    gridPane.getChildren().add(qrCodeImageView);
    Tuple2<Label, AddressTextField> addressTuple = addLabelAddressTextField(gridPane, ++gridRow, "Address:");
    addressLabel = addressTuple.first;
    //GridPane.setValignment(addressLabel, VPos.TOP);
    //GridPane.setMargin(addressLabel, new Insets(3, 0, 0, 0));
    addressTextField = addressTuple.second;
    addressTextField.setPaymentLabel(paymentLabelString);
    Tuple2<Label, InputTextField> amountTuple = addLabelInputTextField(gridPane, ++gridRow, "Amount in BTC (optional):");
    amountLabel = amountTuple.first;
    amountTextField = amountTuple.second;
    if (DevFlags.DEV_MODE)
        amountTextField.setText("10");
    titledGroupBg.setVisible(false);
    titledGroupBg.setManaged(false);
    qrCodeLabel.setVisible(false);
    qrCodeLabel.setManaged(false);
    qrCodeImageView.setVisible(false);
    qrCodeImageView.setManaged(false);
    addressLabel.setVisible(false);
    addressLabel.setManaged(false);
    addressTextField.setVisible(false);
    addressTextField.setManaged(false);
    amountLabel.setVisible(false);
    amountTextField.setManaged(false);
    generateNewAddressButton = addButton(gridPane, ++gridRow, "Generate new address", -20);
    GridPane.setColumnIndex(generateNewAddressButton, 0);
    GridPane.setHalignment(generateNewAddressButton, HPos.LEFT);
    generateNewAddressButton.setOnAction(event -> {
        boolean hasUnUsedAddress = observableList.stream().filter(e -> e.getNumTxOutputs() == 0).findAny().isPresent();
        if (hasUnUsedAddress) {
            new Popup().warning("Please select an unused address from the table above rather than generating a new one.").show();
        } else {
            AddressEntry newSavingsAddressEntry = walletService.getOrCreateUnusedAddressEntry(AddressEntry.Context.AVAILABLE);
            updateList();
            observableList.stream().filter(depositListItem -> depositListItem.getAddressString().equals(newSavingsAddressEntry.getAddressString())).findAny().ifPresent(depositListItem -> tableView.getSelectionModel().select(depositListItem));
        }
    });
    balanceListener = new BalanceListener() {

        @Override
        public void onBalanceChanged(Coin balance, Transaction tx) {
            updateList();
        }
    };
}
Also used : Insets(javafx.geometry.Insets) BalanceListener(io.bitsquare.btc.listeners.BalanceListener) AddressEntry(io.bitsquare.btc.AddressEntry) AddressTextField(io.bitsquare.gui.components.AddressTextField) InputTextField(io.bitsquare.gui.components.InputTextField) QRCodeWindow(io.bitsquare.gui.main.overlays.windows.QRCodeWindow) Coin(org.bitcoinj.core.Coin) Transaction(org.bitcoinj.core.Transaction) Popup(io.bitsquare.gui.main.overlays.popups.Popup) ImageView(javafx.scene.image.ImageView)

Example 20 with AddressEntry

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

the class BuyerStep5View method reviewWithdrawal.

private void reviewWithdrawal() {
    Coin senderAmount = trade.getPayoutAmount();
    WalletService walletService = model.dataModel.walletService;
    AddressEntry fromAddressesEntry = walletService.getOrCreateAddressEntry(trade.getId(), AddressEntry.Context.TRADE_PAYOUT);
    String fromAddresses = fromAddressesEntry.getAddressString();
    String toAddresses = withdrawAddressTextField.getText();
    // TODO at some error situation it can be tha the funds are already paid out and we get stuck here
    // need handling to remove the trade (planned for next release)
    Coin balance = walletService.getBalanceForAddress(fromAddressesEntry.getAddress());
    try {
        Coin requiredFee = walletService.getRequiredFee(fromAddresses, toAddresses, senderAmount, AddressEntry.Context.TRADE_PAYOUT);
        Coin receiverAmount = senderAmount.subtract(requiredFee);
        if (balance.isZero()) {
            new Popup().warning("Your funds have already been withdrawn.\nPlease check the transaction history.").show();
            model.dataModel.tradeManager.addTradeToClosedTrades(trade);
        } else {
            if (toAddresses.isEmpty()) {
                validateWithdrawAddress();
            } else if (Restrictions.isAboveFixedTxFeeForTradesAndDust(senderAmount)) {
                if (DevFlags.DEV_MODE) {
                    doWithdrawal(receiverAmount);
                } else {
                    BSFormatter formatter = model.formatter;
                    String key = "reviewWithdrawalAtTradeComplete";
                    if (!DevFlags.DEV_MODE && preferences.showAgain(key)) {
                        new Popup().headLine("Confirm withdrawal request").confirmation("Sending: " + formatter.formatCoinWithCode(senderAmount) + "\n" + "From address: " + fromAddresses + "\n" + "To receiving address: " + toAddresses + ".\n" + "Required transaction fee is: " + formatter.formatCoinWithCode(requiredFee) + "\n\n" + "The recipient will receive: " + formatter.formatCoinWithCode(receiverAmount) + "\n\n" + "Are you sure you want to proceed with the withdrawal?").closeButtonText("Cancel").onClose(() -> {
                            useSavingsWalletButton.setDisable(false);
                            withdrawToExternalWalletButton.setDisable(false);
                        }).actionButtonText("Yes").onAction(() -> doWithdrawal(receiverAmount)).dontShowAgainId(key, preferences).show();
                    } else {
                        doWithdrawal(receiverAmount);
                    }
                }
            } else {
                new Popup().warning("The amount to transfer is lower than the transaction fee and the min. possible tx value (dust).").show();
            }
        }
    } catch (AddressFormatException e) {
        validateWithdrawAddress();
    } catch (AddressEntryException e) {
        log.error(e.getMessage());
    }
}
Also used : Coin(org.bitcoinj.core.Coin) AddressFormatException(org.bitcoinj.core.AddressFormatException) AddressEntryException(io.bitsquare.btc.AddressEntryException) AddressEntry(io.bitsquare.btc.AddressEntry) Popup(io.bitsquare.gui.main.overlays.popups.Popup) BSFormatter(io.bitsquare.gui.util.BSFormatter) WalletService(io.bitsquare.btc.WalletService)

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