Merge pull request #89 from jimbojw/export-addresses

Export Addresses to CSV File
This commit is contained in:
craigraw 2021-04-06 08:40:43 +02:00 committed by GitHub
commit 6785bccf0e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 50 additions and 1 deletions

View file

@ -1,19 +1,30 @@
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.KeyPurpose; import com.sparrowwallet.drongo.KeyPurpose;
import com.sparrowwallet.drongo.wallet.WalletNode; import com.sparrowwallet.drongo.wallet.WalletNode;
import com.sparrowwallet.sparrow.AppServices;
import com.sparrowwallet.sparrow.EventManager; import com.sparrowwallet.sparrow.EventManager;
import com.sparrowwallet.sparrow.control.AddressTreeTable; import com.sparrowwallet.sparrow.control.AddressTreeTable;
import com.sparrowwallet.sparrow.event.*; import com.sparrowwallet.sparrow.event.*;
import javafx.event.ActionEvent;
import javafx.fxml.FXML; import javafx.fxml.FXML;
import javafx.fxml.Initializable; import javafx.fxml.Initializable;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.*;
import java.net.URL; import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.List; import java.util.List;
import java.util.ResourceBundle; import java.util.ResourceBundle;
public class AddressesController extends WalletFormController implements Initializable { public class AddressesController extends WalletFormController implements Initializable {
private static final Logger log = LoggerFactory.getLogger(AddressesController.class);
@FXML @FXML
private AddressTreeTable receiveTable; private AddressTreeTable receiveTable;
@ -83,4 +94,35 @@ public class AddressesController extends WalletFormController implements Initial
changeTable.updateAll(getWalletForm().getNodeEntry(KeyPurpose.CHANGE)); changeTable.updateAll(getWalletForm().getNodeEntry(KeyPurpose.CHANGE));
} }
} }
public void exportReceiveAddresses(ActionEvent event) {
exportFile();
}
private void exportFile() {
Stage window = new Stage();
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("Export Addresses File");
String extension = "txt";
fileChooser.setInitialFileName(getWalletForm().getWallet().getName() + "-addresses.txt");
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"});
for(Entry entry : getWalletForm().getNodeEntry(KeyPurpose.RECEIVE).getChildren()) {
NodeEntry childEntry = (NodeEntry)entry;
writer.write(childEntry.getNode().getIndex() + "");
writer.write(childEntry.getAddress().toString());
writer.endRecord();
}
writer.close();
} catch(IOException e) {
log.error("Error exporting addresses as CSV", e);
AppServices.showErrorDialog("Error exporting addresses as CSV", e.getMessage());
}
}
}
} }

View file

@ -21,7 +21,14 @@
</rowConstraints> </rowConstraints>
<BorderPane GridPane.columnIndex="0" GridPane.rowIndex="0"> <BorderPane GridPane.columnIndex="0" GridPane.rowIndex="0">
<top> <top>
<BorderPane GridPane.columnIndex="0" GridPane.rowIndex="0">
<left>
<Label styleClass="addresses-treetable-label" text="Receiving Addresses"/> <Label styleClass="addresses-treetable-label" text="Receiving Addresses"/>
</left>
<right>
<Button fx:id="receiveExportButton" text="Export File..." onAction="#exportReceiveAddresses" />
</right>
</BorderPane>
</top> </top>
<center> <center>
<AddressTreeTable fx:id="receiveTable" /> <AddressTreeTable fx:id="receiveTable" />