save border wallet pdf when generating a new recovery phrase

This commit is contained in:
Craig Raw 2023-03-29 12:00:04 +02:00
parent 49e70e8e9b
commit 7915bbfa47
3 changed files with 76 additions and 2 deletions

View file

@ -126,7 +126,7 @@ dependencies {
implementation('io.reactivex.rxjava2:rxjavafx:2.2.2') implementation('io.reactivex.rxjava2:rxjavafx:2.2.2')
implementation('org.apache.commons:commons-lang3:3.7') implementation('org.apache.commons:commons-lang3:3.7')
implementation('net.sourceforge.streamsupport:streamsupport:1.7.0') 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('com.googlecode.lanterna:lanterna:3.1.1')
implementation('net.coobird:thumbnailator:0.4.18') implementation('net.coobird:thumbnailator:0.4.18')
implementation('com.github.hervegirod:fxsvgimage:1.0b2') implementation('com.github.hervegirod:fxsvgimage:1.0b2')

View file

@ -240,7 +240,7 @@ public class MnemonicGridDialog extends Dialog<List<String>> {
initializedProperty.set(true); initializedProperty.set(true);
if(seedEntryDialog.isGenerated()) { if(seedEntryDialog.isGenerated()) {
//TODO: Save grid PDF PdfUtils.saveWordGrid(wordGrid, mnemonicWords);
} }
} }
}); });

View file

@ -9,6 +9,9 @@ import com.google.zxing.qrcode.QRCodeWriter;
import com.lowagie.text.*; import com.lowagie.text.*;
import com.lowagie.text.Font; import com.lowagie.text.Font;
import com.lowagie.text.Image; 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.PdfReader;
import com.lowagie.text.pdf.PdfWriter; import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.pdf.parser.PdfTextExtractor; import com.lowagie.text.pdf.parser.PdfTextExtractor;
@ -138,4 +141,75 @@ public class PdfUtils {
throw new IllegalArgumentException("Not a valid Border Wallets PDF"); throw new IllegalArgumentException("Not a valid Border Wallets PDF");
} }
} }
public static void saveWordGrid(String[][] wordGrid, List<String> 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<String> 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());
}
}
}
} }