From fc9cdaabb4b7b602dfd93b26d1fc53a2e92144b2 Mon Sep 17 00:00:00 2001 From: Craig Raw Date: Thu, 29 Jul 2021 12:09:54 +0200 Subject: [PATCH] export UTXOs to CSV --- .../sparrow/control/UtxosChart.java | 2 +- .../sparrow/wallet/AddressesController.java | 2 +- .../sparrow/wallet/UtxosController.java | 48 +++++++++++++++++++ .../sparrowwallet/sparrow/wallet/utxos.fxml | 12 ++++- 4 files changed, 61 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/sparrowwallet/sparrow/control/UtxosChart.java b/src/main/java/com/sparrowwallet/sparrow/control/UtxosChart.java index 2cdab12a..10d60016 100644 --- a/src/main/java/com/sparrowwallet/sparrow/control/UtxosChart.java +++ b/src/main/java/com/sparrowwallet/sparrow/control/UtxosChart.java @@ -74,7 +74,7 @@ public class UtxosChart extends BarChart { private String getCategoryName(Entry entry) { if(entry.getLabel() != null && !entry.getLabel().isEmpty()) { - return entry.getLabel().length() > 15 ? entry.getLabel().substring(0, 15) + "..." : entry.getLabel() + "\n" + ((UtxoEntry)entry).getDescription(); + return entry.getLabel().length() > 15 ? entry.getLabel().substring(0, 15) + "..." + "\n" + ((UtxoEntry)entry).getDescription() : entry.getLabel() + "\n" + ((UtxoEntry)entry).getDescription(); } return ((UtxoEntry)entry).getDescription(); diff --git a/src/main/java/com/sparrowwallet/sparrow/wallet/AddressesController.java b/src/main/java/com/sparrowwallet/sparrow/wallet/AddressesController.java index 90586971..be767cd8 100644 --- a/src/main/java/com/sparrowwallet/sparrow/wallet/AddressesController.java +++ b/src/main/java/com/sparrowwallet/sparrow/wallet/AddressesController.java @@ -113,7 +113,7 @@ public class AddressesController extends WalletFormController implements Initial FileChooser fileChooser = new FileChooser(); fileChooser.setTitle("Export Addresses to CSV"); - fileChooser.setInitialFileName(getWalletForm().getWallet().getName() + "-" + keyPurpose.name().toLowerCase() + "-addresses.txt"); + fileChooser.setInitialFileName(getWalletForm().getWallet().getName() + "-" + keyPurpose.name().toLowerCase() + "-addresses.csv"); Wallet copy = getWalletForm().getWallet().copy(); WalletNode purposeNode = copy.getNode(keyPurpose); diff --git a/src/main/java/com/sparrowwallet/sparrow/wallet/UtxosController.java b/src/main/java/com/sparrowwallet/sparrow/wallet/UtxosController.java index b9eed10b..f8e0df7c 100644 --- a/src/main/java/com/sparrowwallet/sparrow/wallet/UtxosController.java +++ b/src/main/java/com/sparrowwallet/sparrow/wallet/UtxosController.java @@ -1,11 +1,14 @@ package com.sparrowwallet.sparrow.wallet; +import com.csvreader.CsvWriter; import com.google.common.eventbus.Subscribe; import com.sparrowwallet.drongo.BitcoinUnit; import com.sparrowwallet.drongo.protocol.Transaction; import com.sparrowwallet.drongo.wallet.BlockTransactionHashIndex; +import com.sparrowwallet.sparrow.AppServices; import com.sparrowwallet.sparrow.EventManager; import com.sparrowwallet.sparrow.control.CoinLabel; +import com.sparrowwallet.sparrow.control.EntryCell; import com.sparrowwallet.sparrow.control.UtxosChart; import com.sparrowwallet.sparrow.control.UtxosTreeTable; import com.sparrowwallet.sparrow.event.*; @@ -17,14 +20,23 @@ import javafx.fxml.FXML; import javafx.fxml.Initializable; import javafx.scene.control.Button; import javafx.scene.control.Tooltip; +import javafx.stage.FileChooser; +import javafx.stage.Stage; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; import java.net.URL; +import java.nio.charset.StandardCharsets; import java.util.List; import java.util.Locale; import java.util.ResourceBundle; import java.util.stream.Collectors; public class UtxosController extends WalletFormController implements Initializable { + private static final Logger log = LoggerFactory.getLogger(UtxosController.class); @FXML private UtxosTreeTable utxosTable; @@ -96,6 +108,42 @@ public class UtxosController extends WalletFormController implements Initializab utxosTable.getSelectionModel().clearSelection(); } + public void exportUtxos(ActionEvent event) { + Stage window = new Stage(); + + FileChooser fileChooser = new FileChooser(); + fileChooser.setTitle("Export UTXOs to CSV"); + fileChooser.setInitialFileName(getWalletForm().getWallet().getName() + "-utxos.csv"); + + 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[] {"Date", "Output", "Address", "Label", "Value"}); + for(Entry entry : getWalletForm().getWalletUtxosEntry().getChildren()) { + UtxoEntry utxoEntry = (UtxoEntry)entry; + writer.write(utxoEntry.getBlockTransaction().getDate() == null ? "Unconfirmed" : EntryCell.DATE_FORMAT.format(utxoEntry.getBlockTransaction().getDate())); + writer.write(utxoEntry.getHashIndex().toString()); + writer.write(utxoEntry.getAddress().getAddress()); + writer.write(utxoEntry.getLabel()); + writer.write(getCoinValue(utxoEntry.getValue())); + writer.endRecord(); + } + writer.close(); + } catch(IOException e) { + log.error("Error exporting UTXOs as CSV", e); + AppServices.showErrorDialog("Error exporting UTXOs as CSV", e.getMessage()); + } + } + } + + private String getCoinValue(Long value) { + return BitcoinUnit.BTC.equals(utxosTable.getBitcoinUnit()) ? + CoinLabel.getBTCFormat().format(value.doubleValue() / Transaction.SATOSHIS_PER_BITCOIN) : + String.format(Locale.ENGLISH, "%d", value); + } + @Subscribe public void walletNodesChanged(WalletNodesChangedEvent event) { if(event.getWallet().equals(walletForm.getWallet())) { diff --git a/src/main/resources/com/sparrowwallet/sparrow/wallet/utxos.fxml b/src/main/resources/com/sparrowwallet/sparrow/wallet/utxos.fxml index cd235f7f..df8cc4d1 100644 --- a/src/main/resources/com/sparrowwallet/sparrow/wallet/utxos.fxml +++ b/src/main/resources/com/sparrowwallet/sparrow/wallet/utxos.fxml @@ -22,7 +22,17 @@ -