Search in sources :

Example 6 with WalletNode

use of com.sparrowwallet.drongo.wallet.WalletNode in project sparrow by sparrowwallet.

the class CounterpartyController method startCounterpartyCollaboration.

private void startCounterpartyCollaboration(SparrowCahootsWallet counterpartyCahootsWallet, PaymentCode initiatorPaymentCode, CahootsType cahootsType) {
    sorobanProgressLabel.setText("Creating mix transaction...");
    Soroban soroban = AppServices.getSorobanServices().getSoroban(walletId);
    Map<BlockTransactionHashIndex, WalletNode> walletUtxos = wallet.getWalletUtxos();
    for (Map.Entry<BlockTransactionHashIndex, WalletNode> entry : walletUtxos.entrySet()) {
        if (entry.getKey().getStatus() != Status.FROZEN) {
            counterpartyCahootsWallet.addUtxo(entry.getValue(), wallet.getWalletTransaction(entry.getKey().getHash()), (int) entry.getKey().getIndex());
        }
    }
    try {
        SorobanCahootsService sorobanCahootsService = soroban.getSorobanCahootsService(counterpartyCahootsWallet);
        CahootsContext cahootsContext = cahootsType == CahootsType.STONEWALLX2 ? CahootsContext.newCounterpartyStonewallx2() : CahootsContext.newCounterpartyStowaway();
        sorobanCahootsService.contributor(counterpartyCahootsWallet.getAccount(), cahootsContext, initiatorPaymentCode, TIMEOUT_MS).subscribeOn(Schedulers.io()).observeOn(JavaFxScheduler.platform()).subscribe(sorobanMessage -> {
            OnlineCahootsMessage cahootsMessage = (OnlineCahootsMessage) sorobanMessage;
            if (cahootsMessage != null) {
                Cahoots cahoots = cahootsMessage.getCahoots();
                sorobanProgressBar.setProgress((double) (cahoots.getStep() + 1) / 5);
                if (cahoots.getStep() == 3) {
                    sorobanProgressLabel.setText("Your mix partner is reviewing the transaction...");
                    step3Timer.start();
                } else if (cahoots.getStep() >= 4) {
                    try {
                        Transaction transaction = getTransaction(cahoots);
                        if (transaction != null) {
                            transactionProperty.set(transaction);
                            updateTransactionDiagram(transactionDiagram, wallet, null, transaction);
                            next();
                        }
                    } catch (PSBTParseException e) {
                        log.error("Invalid collaborative PSBT created", e);
                        step3Desc.setText("Invalid transaction created.");
                        sorobanProgressLabel.setVisible(false);
                    }
                }
            }
        }, error -> {
            log.error("Error creating mix transaction", error);
            String cutFrom = "Exception: ";
            int index = error.getMessage().lastIndexOf(cutFrom);
            String msg = index < 0 ? error.getMessage() : error.getMessage().substring(index + cutFrom.length());
            msg = msg.replace("#Cahoots", "mix transaction");
            step3Desc.setText(msg);
            sorobanProgressLabel.setVisible(false);
        });
    } catch (Exception e) {
        log.error("Error creating mix transaction", e);
        sorobanProgressLabel.setText(e.getMessage());
    }
}
Also used : BlockTransactionHashIndex(com.sparrowwallet.drongo.wallet.BlockTransactionHashIndex) PSBTParseException(com.sparrowwallet.drongo.psbt.PSBTParseException) CahootsContext(com.samourai.soroban.cahoots.CahootsContext) Transaction(com.sparrowwallet.drongo.protocol.Transaction) OnlineCahootsMessage(com.samourai.soroban.client.cahoots.OnlineCahootsMessage) SorobanCahootsService(com.samourai.soroban.client.cahoots.SorobanCahootsService) PSBTParseException(com.sparrowwallet.drongo.psbt.PSBTParseException) Cahoots(com.samourai.wallet.cahoots.Cahoots) HashMap(java.util.HashMap) Map(java.util.Map) WalletNode(com.sparrowwallet.drongo.wallet.WalletNode)

Example 7 with WalletNode

use of com.sparrowwallet.drongo.wallet.WalletNode in project sparrow by sparrowwallet.

the class AddressesController method exportAddresses.

private void exportAddresses(KeyPurpose keyPurpose) {
    Stage window = new Stage();
    FileChooser fileChooser = new FileChooser();
    fileChooser.setTitle("Export Addresses to CSV");
    fileChooser.setInitialFileName(getWalletForm().getWallet().getFullName() + "-" + keyPurpose.name().toLowerCase() + "-addresses.csv");
    Wallet copy = getWalletForm().getWallet().copy();
    WalletNode purposeNode = copy.getNode(keyPurpose);
    purposeNode.fillToIndex(Math.max(purposeNode.getChildren().size(), DEFAULT_EXPORT_ADDRESSES_LENGTH));
    AppServices.moveToActiveWindowScreen(window, 800, 450);
    File file = fileChooser.showSaveDialog(window);
    if (file != null) {
        try (FileOutputStream outputStream = new FileOutputStream(file)) {
            CsvWriter writer = new CsvWriter(outputStream, ',', StandardCharsets.UTF_8);
            writer.writeRecord(new String[] { "Index", "Payment Address", "Derivation", "Label" });
            for (WalletNode indexNode : purposeNode.getChildren()) {
                writer.write(Integer.toString(indexNode.getIndex()));
                writer.write(indexNode.getAddress().toString());
                writer.write(getDerivationPath(indexNode));
                Optional<Entry> optLabelEntry = getWalletForm().getNodeEntry(keyPurpose).getChildren().stream().filter(entry -> ((NodeEntry) entry).getNode().getIndex() == indexNode.getIndex()).findFirst();
                writer.write(optLabelEntry.isPresent() ? optLabelEntry.get().getLabel() : "");
                writer.endRecord();
            }
            writer.close();
        } catch (IOException e) {
            log.error("Error exporting addresses as CSV", e);
            AppServices.showErrorDialog("Error exporting addresses as CSV", e.getMessage());
        }
    }
}
Also used : Button(javafx.scene.control.Button) Initializable(javafx.fxml.Initializable) Logger(org.slf4j.Logger) Wallet(com.sparrowwallet.drongo.wallet.Wallet) URL(java.net.URL) com.sparrowwallet.sparrow.event(com.sparrowwallet.sparrow.event) LoggerFactory(org.slf4j.LoggerFactory) AddressTreeTable(com.sparrowwallet.sparrow.control.AddressTreeTable) StandardCharsets(java.nio.charset.StandardCharsets) FXML(javafx.fxml.FXML) FileChooser(javafx.stage.FileChooser) ActionEvent(javafx.event.ActionEvent) List(java.util.List) AppServices(com.sparrowwallet.sparrow.AppServices) KeyPurpose(com.sparrowwallet.drongo.KeyPurpose) Stage(javafx.stage.Stage) java.io(java.io) ResourceBundle(java.util.ResourceBundle) CsvWriter(com.csvreader.CsvWriter) EventManager(com.sparrowwallet.sparrow.EventManager) Optional(java.util.Optional) Subscribe(com.google.common.eventbus.Subscribe) WalletNode(com.sparrowwallet.drongo.wallet.WalletNode) PayNymAddressesDialog(com.sparrowwallet.sparrow.paynym.PayNymAddressesDialog) CsvWriter(com.csvreader.CsvWriter) Wallet(com.sparrowwallet.drongo.wallet.Wallet) FileChooser(javafx.stage.FileChooser) Stage(javafx.stage.Stage) WalletNode(com.sparrowwallet.drongo.wallet.WalletNode)

Example 8 with WalletNode

use of com.sparrowwallet.drongo.wallet.WalletNode in project sparrow by sparrowwallet.

the class WalletTransactionsEntry method getWalletTransactions.

private static void getWalletTransactions(Wallet wallet, Map<BlockTransaction, WalletTransaction> walletTransactionMap, WalletNode purposeNode) {
    KeyPurpose keyPurpose = purposeNode.getKeyPurpose();
    List<WalletNode> childNodes = new ArrayList<>(purposeNode.getChildren());
    for (WalletNode addressNode : childNodes) {
        for (BlockTransactionHashIndex hashIndex : addressNode.getTransactionOutputs()) {
            BlockTransaction inputTx = wallet.getWalletTransaction(hashIndex.getHash());
            // A null inputTx here means the wallet is still updating - ignore as the WalletHistoryChangedEvent will run this again
            if (inputTx != null) {
                WalletTransaction inputWalletTx = walletTransactionMap.get(inputTx);
                if (inputWalletTx == null) {
                    inputWalletTx = new WalletTransaction(wallet, inputTx);
                    walletTransactionMap.put(inputTx, inputWalletTx);
                }
                inputWalletTx.incoming.put(hashIndex, keyPurpose);
                if (hashIndex.getSpentBy() != null) {
                    BlockTransaction outputTx = wallet.getWalletTransaction(hashIndex.getSpentBy().getHash());
                    if (outputTx != null) {
                        WalletTransaction outputWalletTx = walletTransactionMap.get(outputTx);
                        if (outputWalletTx == null) {
                            outputWalletTx = new WalletTransaction(wallet, outputTx);
                            walletTransactionMap.put(outputTx, outputWalletTx);
                        }
                        outputWalletTx.outgoing.put(hashIndex.getSpentBy(), keyPurpose);
                    }
                }
            }
        }
    }
}
Also used : BlockTransactionHashIndex(com.sparrowwallet.drongo.wallet.BlockTransactionHashIndex) KeyPurpose(com.sparrowwallet.drongo.KeyPurpose) BlockTransaction(com.sparrowwallet.drongo.wallet.BlockTransaction) WalletNode(com.sparrowwallet.drongo.wallet.WalletNode)

Example 9 with WalletNode

use of com.sparrowwallet.drongo.wallet.WalletNode in project sparrow by sparrowwallet.

the class SparrowPostmixHandler method computeNextDestination.

protected MixDestination computeNextDestination() throws Exception {
    // index
    int index = Math.max(getIndexHandler().getAndIncrementUnconfirmed(), startIndex);
    // address
    WalletNode node = new WalletNode(wallet, keyPurpose, index);
    Address address = node.getAddress();
    String path = XPubUtil.getInstance().getPath(index, keyPurpose.getPathIndex().num());
    log.info("Mixing to external xPub -> receiveAddress=" + address + ", path=" + path);
    return new MixDestination(DestinationType.XPUB, index, address.toString(), path);
}
Also used : Address(com.sparrowwallet.drongo.address.Address) MixDestination(com.samourai.whirlpool.client.mix.handler.MixDestination) WalletNode(com.sparrowwallet.drongo.wallet.WalletNode)

Example 10 with WalletNode

use of com.sparrowwallet.drongo.wallet.WalletNode in project sparrow by sparrowwallet.

the class AddressTreeTable method updateHistory.

public void updateHistory(List<WalletNode> updatedNodes) {
    // We only ever add child nodes - never remove in order to keep a full sequence
    NodeEntry rootEntry = (NodeEntry) getRoot().getValue();
    for (WalletNode updatedNode : updatedNodes) {
        Optional<Entry> optEntry = rootEntry.getChildren().stream().filter(childEntry -> ((NodeEntry) childEntry).getNode().equals(updatedNode)).findFirst();
        if (optEntry.isPresent()) {
            NodeEntry existingEntry = (NodeEntry) optEntry.get();
            existingEntry.refreshChildren();
        } else {
            NodeEntry nodeEntry = new NodeEntry(rootEntry.getWallet(), updatedNode);
            rootEntry.getChildren().add(nodeEntry);
        }
    }
    refresh();
}
Also used : MouseButton(javafx.scene.input.MouseButton) javafx.scene.control(javafx.scene.control) NodeEntry(com.sparrowwallet.sparrow.wallet.NodeEntry) OptionalInt(java.util.OptionalInt) Entry(com.sparrowwallet.sparrow.wallet.Entry) Platform(javafx.application.Platform) ReceiveActionEvent(com.sparrowwallet.sparrow.event.ReceiveActionEvent) List(java.util.List) AppServices(com.sparrowwallet.sparrow.AppServices) ReadOnlyObjectWrapper(javafx.beans.property.ReadOnlyObjectWrapper) ListChangeListener(javafx.collections.ListChangeListener) ReceiveToEvent(com.sparrowwallet.sparrow.event.ReceiveToEvent) EventManager(com.sparrowwallet.sparrow.EventManager) Optional(java.util.Optional) WalletNode(com.sparrowwallet.drongo.wallet.WalletNode) NodeEntry(com.sparrowwallet.sparrow.wallet.NodeEntry) Entry(com.sparrowwallet.sparrow.wallet.Entry) NodeEntry(com.sparrowwallet.sparrow.wallet.NodeEntry) WalletNode(com.sparrowwallet.drongo.wallet.WalletNode)

Aggregations

WalletNode (com.sparrowwallet.drongo.wallet.WalletNode)10 BlockTransactionHashIndex (com.sparrowwallet.drongo.wallet.BlockTransactionHashIndex)3 KeyPurpose (com.sparrowwallet.drongo.KeyPurpose)2 Wallet (com.sparrowwallet.drongo.wallet.Wallet)2 AppServices (com.sparrowwallet.sparrow.AppServices)2 EventManager (com.sparrowwallet.sparrow.EventManager)2 List (java.util.List)2 Optional (java.util.Optional)2 CsvWriter (com.csvreader.CsvWriter)1 Subscribe (com.google.common.eventbus.Subscribe)1 CahootsContext (com.samourai.soroban.cahoots.CahootsContext)1 OnlineCahootsMessage (com.samourai.soroban.client.cahoots.OnlineCahootsMessage)1 SorobanCahootsService (com.samourai.soroban.client.cahoots.SorobanCahootsService)1 Cahoots (com.samourai.wallet.cahoots.Cahoots)1 MixDestination (com.samourai.whirlpool.client.mix.handler.MixDestination)1 Address (com.sparrowwallet.drongo.address.Address)1 Transaction (com.sparrowwallet.drongo.protocol.Transaction)1 PSBTParseException (com.sparrowwallet.drongo.psbt.PSBTParseException)1 BlockTransaction (com.sparrowwallet.drongo.wallet.BlockTransaction)1 AddressTreeTable (com.sparrowwallet.sparrow.control.AddressTreeTable)1