Search in sources :

Example 6 with Address

use of com.sparrowwallet.drongo.address.Address in project sparrow by sparrowwallet.

the class SendToManyDialog method getPayments.

private List<Payment> getPayments() {
    List<Payment> payments = new ArrayList<>();
    Grid grid = spreadsheetView.getGrid();
    String firstLabel = null;
    for (int row = 0; row < grid.getRowCount(); row++) {
        ObservableList<SpreadsheetCell> rowCells = spreadsheetView.getItems().get(row);
        Address address = (Address) rowCells.get(0).getItem();
        Double value = (Double) rowCells.get(1).getItem();
        String label = (String) rowCells.get(2).getItem();
        if (firstLabel == null) {
            firstLabel = label;
        }
        if (label == null || label.isEmpty()) {
            label = firstLabel;
        }
        if (address != null && value != null) {
            if (bitcoinUnit == BitcoinUnit.BTC) {
                value = value * Transaction.SATOSHIS_PER_BITCOIN;
            }
            payments.add(new Payment(address, label, value.longValue(), false));
        }
    }
    return payments;
}
Also used : Payment(com.sparrowwallet.drongo.wallet.Payment) Address(com.sparrowwallet.drongo.address.Address) ArrayList(java.util.ArrayList)

Example 7 with Address

use of com.sparrowwallet.drongo.address.Address in project sparrow by sparrowwallet.

the class PaymentController method initializeView.

@Override
public void initializeView() {
    updateOpenWallets();
    openWallets.prefWidthProperty().bind(address.widthProperty());
    openWallets.valueProperty().addListener((observable, oldValue, newValue) -> {
        if (newValue == payNymWallet) {
            boolean selectLinkedOnly = sendController.getPaymentTabs().getTabs().size() > 1 || !SorobanServices.canWalletMix(sendController.getWalletForm().getWallet());
            PayNymDialog payNymDialog = new PayNymDialog(sendController.getWalletForm().getWalletId(), true, selectLinkedOnly);
            Optional<PayNym> optPayNym = payNymDialog.showAndWait();
            optPayNym.ifPresent(this::setPayNym);
        } else if (newValue != null) {
            WalletNode freshNode = newValue.getFreshNode(KeyPurpose.RECEIVE);
            Address freshAddress = freshNode.getAddress();
            address.setText(freshAddress.toString());
            label.requestFocus();
        }
    });
    openWallets.setCellFactory(c -> new ListCell<>() {

        @Override
        protected void updateItem(Wallet wallet, boolean empty) {
            super.updateItem(wallet, empty);
            if (empty || wallet == null) {
                setText(null);
                setGraphic(null);
            } else {
                setText(wallet.getFullDisplayName() + (wallet == sendController.getWalletForm().getWallet() ? " (Consolidation)" : ""));
                setGraphic(wallet == payNymWallet ? getPayNymGlyph() : null);
            }
        }
    });
    payNymProperty.addListener((observable, oldValue, payNym) -> {
        updateMixOnlyStatus(payNym);
        revalidateAmount();
    });
    address.textProperty().addListener((observable, oldValue, newValue) -> {
        address.leftProperty().set(null);
        if (payNymProperty.get() != null && !newValue.equals(payNymProperty.get().nymName())) {
            payNymProperty.set(null);
        }
        try {
            BitcoinURI bitcoinURI = new BitcoinURI(newValue);
            Platform.runLater(() -> updateFromURI(bitcoinURI));
            return;
        } catch (Exception e) {
        // ignore, not a URI
        }
        if (sendController.getWalletForm().getWallet().hasPaymentCode()) {
            try {
                PaymentCode paymentCode = new PaymentCode(newValue);
                Wallet recipientBip47Wallet = sendController.getWalletForm().getWallet().getChildWallet(paymentCode, sendController.getWalletForm().getWallet().getScriptType());
                if (recipientBip47Wallet == null && sendController.getWalletForm().getWallet().getScriptType() != ScriptType.P2PKH) {
                    recipientBip47Wallet = sendController.getWalletForm().getWallet().getChildWallet(paymentCode, ScriptType.P2PKH);
                }
                if (recipientBip47Wallet != null) {
                    PayNym payNym = PayNym.fromWallet(recipientBip47Wallet);
                    Platform.runLater(() -> setPayNym(payNym));
                } else if (!paymentCode.equals(sendController.getWalletForm().getWallet().getPaymentCode())) {
                    ButtonType previewType = new ButtonType("Preview Transaction", ButtonBar.ButtonData.YES);
                    Optional<ButtonType> optButton = AppServices.showAlertDialog("Send notification transaction?", "This payment code is not yet linked with a notification transaction. Send a notification transaction?", Alert.AlertType.CONFIRMATION, ButtonType.CANCEL, previewType);
                    if (optButton.isPresent() && optButton.get() == previewType) {
                        Payment payment = new Payment(paymentCode.getNotificationAddress(), "Link " + paymentCode.toAbbreviatedString(), MINIMUM_P2PKH_OUTPUT_SATS, false);
                        Platform.runLater(() -> EventManager.get().post(new SpendUtxoEvent(sendController.getWalletForm().getWallet(), List.of(payment), List.of(new byte[80]), paymentCode)));
                    } else {
                        Platform.runLater(() -> address.setText(""));
                    }
                }
            } catch (Exception e) {
            // ignore, not a payment code
            }
        }
        revalidateAmount();
        maxButton.setDisable(!isMaxButtonEnabled());
        sendController.updateTransaction();
        if (validationSupport != null) {
            validationSupport.setErrorDecorationEnabled(true);
        }
    });
    label.textProperty().addListener((observable, oldValue, newValue) -> {
        maxButton.setDisable(!isMaxButtonEnabled());
        sendController.getCreateButton().setDisable(sendController.getWalletTransaction() == null || newValue == null || newValue.isEmpty() || sendController.isInsufficientFeeRate());
        sendController.updateTransaction();
    });
    amount.setTextFormatter(new CoinTextFormatter());
    amount.textProperty().addListener(amountListener);
    amountUnit.getSelectionModel().select(BitcoinUnit.BTC.equals(sendController.getBitcoinUnit(Config.get().getBitcoinUnit())) ? 0 : 1);
    amountUnit.valueProperty().addListener((observable, oldValue, newValue) -> {
        Long value = getRecipientValueSats(oldValue);
        if (value != null) {
            DecimalFormat df = new DecimalFormat("#.#", DecimalFormatSymbols.getInstance(Locale.ENGLISH));
            df.setMaximumFractionDigits(8);
            amount.setText(df.format(newValue.getValue(value)));
        }
    });
    maxButton.setDisable(!isMaxButtonEnabled());
    sendController.utxoSelectorProperty().addListener((observable, oldValue, newValue) -> {
        maxButton.setDisable(!isMaxButtonEnabled());
    });
    sendController.getPaymentTabs().getTabs().addListener((ListChangeListener<Tab>) c -> {
        maxButton.setDisable(!isMaxButtonEnabled());
    });
    sendController.utxoLabelSelectionProperty().addListener((observable, oldValue, newValue) -> {
        maxButton.setText("Max" + newValue);
    });
    amountStatus.managedProperty().bind(amountStatus.visibleProperty());
    amountStatus.visibleProperty().bind(sendController.insufficientInputsProperty().and(dustAmountProperty.not()).and(emptyAmountProperty.not()));
    dustStatus.managedProperty().bind(dustStatus.visibleProperty());
    dustStatus.visibleProperty().bind(dustAmountProperty);
    Optional<Tab> firstTab = sendController.getPaymentTabs().getTabs().stream().findFirst();
    if (firstTab.isPresent()) {
        PaymentController controller = (PaymentController) firstTab.get().getUserData();
        String firstLabel = controller.label.getText();
        label.setText(firstLabel);
    }
    addValidation(validationSupport);
}
Also used : Initializable(javafx.fxml.Initializable) PayNym(com.sparrowwallet.sparrow.paynym.PayNym) com.sparrowwallet.drongo.wallet(com.sparrowwallet.drongo.wallet) javafx.scene.control(javafx.scene.control) URL(java.net.URL) DecimalFormatSymbols(java.text.DecimalFormatSymbols) LoggerFactory(org.slf4j.LoggerFactory) ValidationSupport(org.controlsfx.validation.ValidationSupport) Config(com.sparrowwallet.sparrow.io.Config) PayNymAddress(com.sparrowwallet.sparrow.paynym.PayNymAddress) ExchangeSource(com.sparrowwallet.sparrow.net.ExchangeSource) ListChangeListener(javafx.collections.ListChangeListener) CurrencyRate(com.sparrowwallet.sparrow.CurrencyRate) FontAwesome5(com.sparrowwallet.sparrow.glyphfont.FontAwesome5) InvalidPaymentCodeException(com.sparrowwallet.drongo.bip47.InvalidPaymentCodeException) Collectors(java.util.stream.Collectors) BitcoinURI(com.sparrowwallet.drongo.uri.BitcoinURI) Platform(javafx.application.Platform) FXML(javafx.fxml.FXML) BooleanProperty(javafx.beans.property.BooleanProperty) KeyPurpose(com.sparrowwallet.drongo.KeyPurpose) ScriptType(com.sparrowwallet.drongo.protocol.ScriptType) PaymentCode(com.sparrowwallet.drongo.bip47.PaymentCode) Address(com.sparrowwallet.drongo.address.Address) java.util(java.util) FXCollections(javafx.collections.FXCollections) InvalidAddressException(com.sparrowwallet.drongo.address.InvalidAddressException) AppServices.showErrorDialog(com.sparrowwallet.sparrow.AppServices.showErrorDialog) Glyph(org.controlsfx.glyphfont.Glyph) TransactionOutput(com.sparrowwallet.drongo.protocol.TransactionOutput) ValidationResult(org.controlsfx.validation.ValidationResult) Subscribe(com.google.common.eventbus.Subscribe) Transaction(com.sparrowwallet.drongo.protocol.Transaction) ObjectProperty(javafx.beans.property.ObjectProperty) Logger(org.slf4j.Logger) P2PKHAddress(com.sparrowwallet.drongo.address.P2PKHAddress) com.sparrowwallet.sparrow.event(com.sparrowwallet.sparrow.event) DecimalFormat(java.text.DecimalFormat) com.sparrowwallet.sparrow.soroban(com.sparrowwallet.sparrow.soroban) BitcoinUnit(com.sparrowwallet.drongo.BitcoinUnit) PayNymDialog(com.sparrowwallet.sparrow.paynym.PayNymDialog) ActionEvent(javafx.event.ActionEvent) AppServices(com.sparrowwallet.sparrow.AppServices) SimpleBooleanProperty(javafx.beans.property.SimpleBooleanProperty) SimpleObjectProperty(javafx.beans.property.SimpleObjectProperty) EventManager(com.sparrowwallet.sparrow.EventManager) ECKey(com.sparrowwallet.drongo.crypto.ECKey) ObservableValue(javafx.beans.value.ObservableValue) Validator(org.controlsfx.validation.Validator) ChangeListener(javafx.beans.value.ChangeListener) com.sparrowwallet.sparrow.control(com.sparrowwallet.sparrow.control) PaymentCode(com.sparrowwallet.drongo.bip47.PaymentCode) PayNymAddress(com.sparrowwallet.sparrow.paynym.PayNymAddress) Address(com.sparrowwallet.drongo.address.Address) P2PKHAddress(com.sparrowwallet.drongo.address.P2PKHAddress) DecimalFormat(java.text.DecimalFormat) PayNym(com.sparrowwallet.sparrow.paynym.PayNym) InvalidPaymentCodeException(com.sparrowwallet.drongo.bip47.InvalidPaymentCodeException) InvalidAddressException(com.sparrowwallet.drongo.address.InvalidAddressException) PayNymDialog(com.sparrowwallet.sparrow.paynym.PayNymDialog) BitcoinURI(com.sparrowwallet.drongo.uri.BitcoinURI)

Example 8 with Address

use of com.sparrowwallet.drongo.address.Address in project sparrow by sparrowwallet.

the class PaymentController method getRecipientAddress.

private Address getRecipientAddress() throws InvalidAddressException {
    if (payNymProperty.get() == null) {
        return Address.fromString(address.getText());
    }
    try {
        Wallet recipientBip47Wallet = getWalletForPayNym(payNymProperty.get());
        if (recipientBip47Wallet != null) {
            WalletNode sendNode = recipientBip47Wallet.getFreshNode(KeyPurpose.SEND);
            ECKey pubKey = sendNode.getPubKey();
            Address address = recipientBip47Wallet.getScriptType().getAddress(pubKey);
            if (sendController.getPaymentTabs().getTabs().size() > 1 || (getRecipientValueSats() != null && getRecipientValueSats() > getRecipientDustThreshold(address)) || maxButton.isSelected()) {
                return address;
            }
        }
    } catch (InvalidPaymentCodeException e) {
        log.error("Error creating payment code from PayNym", e);
    }
    return new PayNymAddress(payNymProperty.get());
}
Also used : InvalidPaymentCodeException(com.sparrowwallet.drongo.bip47.InvalidPaymentCodeException) PayNymAddress(com.sparrowwallet.sparrow.paynym.PayNymAddress) Address(com.sparrowwallet.drongo.address.Address) P2PKHAddress(com.sparrowwallet.drongo.address.P2PKHAddress) PayNymAddress(com.sparrowwallet.sparrow.paynym.PayNymAddress) ECKey(com.sparrowwallet.drongo.crypto.ECKey)

Example 9 with Address

use of com.sparrowwallet.drongo.address.Address in project sparrow by sparrowwallet.

the class PaymentController method getPayment.

public Payment getPayment(boolean sendAll) {
    try {
        Address recipientAddress = getRecipientAddress();
        Long value = sendAll ? Long.valueOf(getRecipientDustThreshold() + 1) : getRecipientValueSats();
        if (!label.getText().isEmpty() && value != null && value >= getRecipientDustThreshold()) {
            Payment payment = new Payment(recipientAddress, label.getText(), value, sendAll);
            if (address.getUserData() != null) {
                payment.setType((Payment.Type) address.getUserData());
            }
            return payment;
        }
    } catch (InvalidAddressException e) {
    // ignore
    }
    throw new IllegalStateException("Invalid payment specified");
}
Also used : PayNymAddress(com.sparrowwallet.sparrow.paynym.PayNymAddress) Address(com.sparrowwallet.drongo.address.Address) P2PKHAddress(com.sparrowwallet.drongo.address.P2PKHAddress) InvalidAddressException(com.sparrowwallet.drongo.address.InvalidAddressException)

Example 10 with Address

use of com.sparrowwallet.drongo.address.Address in project sparrow by sparrowwallet.

the class TransactionFormController method addPieData.

protected void addPieData(PieChart pie, List<TransactionOutput> outputs) {
    ObservableList<PieChart.Data> outputsPieData = FXCollections.observableArrayList();
    long totalAmt = 0;
    for (int i = 0; i < outputs.size(); i++) {
        TransactionOutput output = outputs.get(i);
        String name = "#" + i;
        try {
            Address[] addresses = output.getScript().getToAddresses();
            if (addresses.length == 1) {
                name = name + " " + addresses[0].getAddress();
            } else {
                name = name + " [" + addresses[0].getAddress() + ",...]";
            }
        } catch (NonStandardScriptException e) {
        // ignore
        }
        totalAmt += output.getValue();
        outputsPieData.add(new PieChart.Data(name, output.getValue()));
    }
    addPieData(pie, outputsPieData);
}
Also used : NonStandardScriptException(com.sparrowwallet.drongo.protocol.NonStandardScriptException) TransactionOutput(com.sparrowwallet.drongo.protocol.TransactionOutput) Address(com.sparrowwallet.drongo.address.Address) PieChart(javafx.scene.chart.PieChart) TransactionTabData(com.sparrowwallet.sparrow.TransactionTabData)

Aggregations

Address (com.sparrowwallet.drongo.address.Address)26 Test (org.junit.Test)6 InvalidAddressException (com.sparrowwallet.drongo.address.InvalidAddressException)5 ECKey (com.sparrowwallet.drongo.crypto.ECKey)5 KeyPurpose (com.sparrowwallet.drongo.KeyPurpose)4 TransactionOutput (com.sparrowwallet.drongo.protocol.TransactionOutput)4 P2PKHAddress (com.sparrowwallet.drongo.address.P2PKHAddress)3 com.sparrowwallet.drongo.wallet (com.sparrowwallet.drongo.wallet)3 PayNymAddress (com.sparrowwallet.sparrow.paynym.PayNymAddress)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 java.util (java.util)3 Collectors (java.util.stream.Collectors)3 InvalidPaymentCodeException (com.sparrowwallet.drongo.bip47.InvalidPaymentCodeException)2 DumpedPrivateKey (com.sparrowwallet.drongo.crypto.DumpedPrivateKey)2 NonStandardScriptException (com.sparrowwallet.drongo.protocol.NonStandardScriptException)2 ScriptType (com.sparrowwallet.drongo.protocol.ScriptType)2 Transaction (com.sparrowwallet.drongo.protocol.Transaction)2 BitcoinURI (com.sparrowwallet.drongo.uri.BitcoinURI)2 AppServices (com.sparrowwallet.sparrow.AppServices)2 EventManager (com.sparrowwallet.sparrow.EventManager)2