From 7915bbfa4778da461cc35f441919b8f2790c41f7 Mon Sep 17 00:00:00 2001 From: Craig Raw Date: Wed, 29 Mar 2023 12:00:04 +0200 Subject: [PATCH] save border wallet pdf when generating a new recovery phrase --- build.gradle | 2 +- .../sparrow/control/MnemonicGridDialog.java | 2 +- .../sparrowwallet/sparrow/io/PdfUtils.java | 74 +++++++++++++++++++ 3 files changed, 76 insertions(+), 2 deletions(-) diff --git a/build.gradle b/build.gradle index 8cb8051b..4f707129 100644 --- a/build.gradle +++ b/build.gradle @@ -126,7 +126,7 @@ dependencies { implementation('io.reactivex.rxjava2:rxjavafx:2.2.2') implementation('org.apache.commons:commons-lang3:3.7') implementation('net.sourceforge.streamsupport:streamsupport:1.7.0') - implementation('com.github.librepdf:openpdf:1.3.27') + implementation('com.github.librepdf:openpdf:1.3.30') implementation('com.googlecode.lanterna:lanterna:3.1.1') implementation('net.coobird:thumbnailator:0.4.18') implementation('com.github.hervegirod:fxsvgimage:1.0b2') diff --git a/src/main/java/com/sparrowwallet/sparrow/control/MnemonicGridDialog.java b/src/main/java/com/sparrowwallet/sparrow/control/MnemonicGridDialog.java index ccbdccb8..9e4a2f6f 100644 --- a/src/main/java/com/sparrowwallet/sparrow/control/MnemonicGridDialog.java +++ b/src/main/java/com/sparrowwallet/sparrow/control/MnemonicGridDialog.java @@ -240,7 +240,7 @@ public class MnemonicGridDialog extends Dialog> { initializedProperty.set(true); if(seedEntryDialog.isGenerated()) { - //TODO: Save grid PDF + PdfUtils.saveWordGrid(wordGrid, mnemonicWords); } } }); diff --git a/src/main/java/com/sparrowwallet/sparrow/io/PdfUtils.java b/src/main/java/com/sparrowwallet/sparrow/io/PdfUtils.java index 636422e9..e738f8b8 100644 --- a/src/main/java/com/sparrowwallet/sparrow/io/PdfUtils.java +++ b/src/main/java/com/sparrowwallet/sparrow/io/PdfUtils.java @@ -9,6 +9,9 @@ import com.google.zxing.qrcode.QRCodeWriter; import com.lowagie.text.*; import com.lowagie.text.Font; import com.lowagie.text.Image; +import com.lowagie.text.Rectangle; +import com.lowagie.text.alignment.HorizontalAlignment; +import com.lowagie.text.alignment.VerticalAlignment; import com.lowagie.text.pdf.PdfReader; import com.lowagie.text.pdf.PdfWriter; import com.lowagie.text.pdf.parser.PdfTextExtractor; @@ -138,4 +141,75 @@ public class PdfUtils { throw new IllegalArgumentException("Not a valid Border Wallets PDF"); } } + + public static void saveWordGrid(String[][] wordGrid, List mnemonicWords) { + Stage window = new Stage(); + FileChooser fileChooser = new FileChooser(); + fileChooser.setTitle("Save Border Wallet Grid"); + fileChooser.setInitialFileName("BorderWalletEntropyGrid.pdf"); + AppServices.moveToActiveWindowScreen(window, 800, 450); + File file = fileChooser.showSaveDialog(window); + if(file != null) { + try(Document document = new Document()) { + document.setMargins(0, 0, 10, 10); + PdfWriter.getInstance(document, new FileOutputStream(file)); + + Font headerFont = new Font(Font.HELVETICA, 8, Font.BOLD, Color.DARK_GRAY); + Font font = new Font(Font.HELVETICA, 8, Font.NORMAL, Color.DARK_GRAY); + + HeaderFooter footer = new HeaderFooter(false, new Phrase("Recovery Phrase: " + String.join(" ", mnemonicWords), font)); + footer.setAlignment(Element.ALIGN_CENTER); + footer.setBorder(Rectangle.NO_BORDER); + footer.setBorderWidth(0); + document.setFooter(footer); + + document.open(); + + int rowCount = wordGrid.length; + int columnCount = wordGrid[0].length; + + Table table = new Table(columnCount + 2); + table.setBorderWidth(0.5f); + table.setBorderColor(new Color(208, 208, 208)); + table.setBorderWidthLeft(0); + table.setBorderWidthRight(0); + + List headers = List.of(" ", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", " "); + for(String header : headers) { + Paragraph paragraph = new Paragraph(header, headerFont); + Cell cell = new Cell(paragraph); + cell.setBackgroundColor(new Color(218, 218, 218)); + cell.setHorizontalAlignment(HorizontalAlignment.CENTER); + cell.setVerticalAlignment(VerticalAlignment.CENTER); + cell.setBorderWidth(0.5f); + cell.setBorderColor(new Color(208, 208, 208)); + cell.setHeader(true); + table.addCell(cell); + } + table.endHeaders(); + + for(int i = 0; i < rowCount; i++) { + for(int j = 0; j < columnCount + 2; j++) { + Cell cell; + if(j == 0 || j == columnCount + 1) { + cell = new Cell(new Paragraph(String.format("%03d", i + 1), headerFont)); + cell.setBackgroundColor(new Color(218, 218, 218)); + } else { + cell = new Cell(new Paragraph(wordGrid[i][j-1], font)); + } + cell.setHorizontalAlignment(HorizontalAlignment.CENTER); + cell.setVerticalAlignment(VerticalAlignment.CENTER); + cell.setBorderWidth(0.5f); + cell.setBorderColor(new Color(208, 208, 208)); + table.addCell(cell); + } + } + + document.add(table); + } catch(Exception e) { + log.error("Error creating word grid PDF", e); + AppServices.showErrorDialog("Error creating word grid PDF", e.getMessage()); + } + } + } }