export UTXOs to CSV

This commit is contained in:
Craig Raw 2021-07-29 12:09:54 +02:00
parent 2aa3d83402
commit fc9cdaabb4
4 changed files with 61 additions and 3 deletions

View file

@ -74,7 +74,7 @@ public class UtxosChart extends BarChart<String, Number> {
private String getCategoryName(Entry entry) { private String getCategoryName(Entry entry) {
if(entry.getLabel() != null && !entry.getLabel().isEmpty()) { 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(); return ((UtxoEntry)entry).getDescription();

View file

@ -113,7 +113,7 @@ public class AddressesController extends WalletFormController implements Initial
FileChooser fileChooser = new FileChooser(); FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("Export Addresses to CSV"); 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(); Wallet copy = getWalletForm().getWallet().copy();
WalletNode purposeNode = copy.getNode(keyPurpose); WalletNode purposeNode = copy.getNode(keyPurpose);

View file

@ -1,11 +1,14 @@
package com.sparrowwallet.sparrow.wallet; package com.sparrowwallet.sparrow.wallet;
import com.csvreader.CsvWriter;
import com.google.common.eventbus.Subscribe; import com.google.common.eventbus.Subscribe;
import com.sparrowwallet.drongo.BitcoinUnit; import com.sparrowwallet.drongo.BitcoinUnit;
import com.sparrowwallet.drongo.protocol.Transaction; import com.sparrowwallet.drongo.protocol.Transaction;
import com.sparrowwallet.drongo.wallet.BlockTransactionHashIndex; import com.sparrowwallet.drongo.wallet.BlockTransactionHashIndex;
import com.sparrowwallet.sparrow.AppServices;
import com.sparrowwallet.sparrow.EventManager; import com.sparrowwallet.sparrow.EventManager;
import com.sparrowwallet.sparrow.control.CoinLabel; import com.sparrowwallet.sparrow.control.CoinLabel;
import com.sparrowwallet.sparrow.control.EntryCell;
import com.sparrowwallet.sparrow.control.UtxosChart; import com.sparrowwallet.sparrow.control.UtxosChart;
import com.sparrowwallet.sparrow.control.UtxosTreeTable; import com.sparrowwallet.sparrow.control.UtxosTreeTable;
import com.sparrowwallet.sparrow.event.*; import com.sparrowwallet.sparrow.event.*;
@ -17,14 +20,23 @@ import javafx.fxml.FXML;
import javafx.fxml.Initializable; import javafx.fxml.Initializable;
import javafx.scene.control.Button; import javafx.scene.control.Button;
import javafx.scene.control.Tooltip; 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.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.ResourceBundle; import java.util.ResourceBundle;
import java.util.stream.Collectors; import java.util.stream.Collectors;
public class UtxosController extends WalletFormController implements Initializable { public class UtxosController extends WalletFormController implements Initializable {
private static final Logger log = LoggerFactory.getLogger(UtxosController.class);
@FXML @FXML
private UtxosTreeTable utxosTable; private UtxosTreeTable utxosTable;
@ -96,6 +108,42 @@ public class UtxosController extends WalletFormController implements Initializab
utxosTable.getSelectionModel().clearSelection(); 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 @Subscribe
public void walletNodesChanged(WalletNodesChangedEvent event) { public void walletNodesChanged(WalletNodesChangedEvent event) {
if(event.getWallet().equals(walletForm.getWallet())) { if(event.getWallet().equals(walletForm.getWallet())) {

View file

@ -22,7 +22,17 @@
<VBox spacing="10.0"> <VBox spacing="10.0">
<BorderPane VBox.vgrow="ALWAYS"> <BorderPane VBox.vgrow="ALWAYS">
<top> <top>
<Label styleClass="utxos-treetable-label" text="Unspent Transaction Outputs"/> <HBox alignment="CENTER_LEFT">
<Label styleClass="utxos-treetable-label" text="Unspent Transaction Outputs"/>
<Button onAction="#exportUtxos" styleClass="icon-button">
<graphic>
<Glyph fontFamily="Font Awesome 5 Free Solid" icon="ARROW_CIRCLE_DOWN" fontSize="12" />
</graphic>
<tooltip>
<Tooltip text="Export UTXOs as CSV" />
</tooltip>
</Button>
</HBox>
</top> </top>
<center> <center>
<UtxosTreeTable fx:id="utxosTable" /> <UtxosTreeTable fx:id="utxosTable" />