import wallet from output descriptor pdf, ignore newline characters in output descriptor dialog

This commit is contained in:
Craig Raw 2022-09-26 13:48:49 +02:00
parent 8fb6de85f1
commit 52696b014f
4 changed files with 43 additions and 2 deletions

2
drongo

@ -1 +1 @@
Subproject commit 189ef88b08e4aa430315e1e312046c833331bd91 Subproject commit 60cb3ed85f46c3f1b7ddd071e6b02eb1ac334bb6

View file

@ -66,6 +66,12 @@ public class Descriptor implements WalletImport, WalletExport {
@Override @Override
public Wallet importWallet(InputStream inputStream, String password) throws ImportException { public Wallet importWallet(InputStream inputStream, String password) throws ImportException {
try { try {
try {
return PdfUtils.getOutputDescriptor(inputStream).toWallet();
} catch(Exception e) {
//ignore
}
String outputDescriptor = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8)).lines().collect(Collectors.joining("\n")); String outputDescriptor = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8)).lines().collect(Collectors.joining("\n"));
OutputDescriptor descriptor = OutputDescriptor.getOutputDescriptor(outputDescriptor.trim()); OutputDescriptor descriptor = OutputDescriptor.getOutputDescriptor(outputDescriptor.trim());
return descriptor.toWallet(); return descriptor.toWallet();

View file

@ -9,7 +9,11 @@ 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.pdf.PdfReader;
import com.lowagie.text.pdf.PdfWriter; import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.pdf.parser.PdfTextExtractor;
import com.sparrowwallet.drongo.OutputDescriptor;
import com.sparrowwallet.drongo.protocol.ScriptType;
import com.sparrowwallet.hummingbird.UR; import com.sparrowwallet.hummingbird.UR;
import com.sparrowwallet.hummingbird.UREncoder; import com.sparrowwallet.hummingbird.UREncoder;
import com.sparrowwallet.sparrow.AppServices; import com.sparrowwallet.sparrow.AppServices;
@ -21,6 +25,7 @@ import org.slf4j.LoggerFactory;
import java.awt.*; import java.awt.*;
import java.io.*; import java.io.*;
import java.util.Scanner;
public class PdfUtils { public class PdfUtils {
private static final Logger log = LoggerFactory.getLogger(PdfUtils.class); private static final Logger log = LoggerFactory.getLogger(PdfUtils.class);
@ -62,6 +67,36 @@ public class PdfUtils {
} }
} }
public static OutputDescriptor getOutputDescriptor(InputStream inputStream) throws IOException {
try {
PdfReader pdfReader = new PdfReader(inputStream);
String allText = "";
for(int page = 1; page <= pdfReader.getNumberOfPages(); page++) {
PdfTextExtractor textExtractor = new PdfTextExtractor(pdfReader);
allText += textExtractor.getTextFromPage(page) + "\n";
}
String descriptor = null;
Scanner scanner = new Scanner(allText);
while(scanner.hasNextLine()) {
String line = scanner.nextLine().trim();
if(descriptor != null) {
descriptor += line;
} else if(ScriptType.fromDescriptor(line) != null) {
descriptor = line;
}
}
if(descriptor != null) {
return OutputDescriptor.getOutputDescriptor(descriptor);
}
} catch(Exception e) {
throw new IllegalArgumentException("Not a valid PDF or output descriptor");
}
throw new IllegalArgumentException("Output descriptor could not be found");
}
private static javafx.scene.image.Image getQrCode(String fragment) throws IOException, WriterException { private static javafx.scene.image.Image getQrCode(String fragment) throws IOException, WriterException {
QRCodeWriter qrCodeWriter = new QRCodeWriter(); QRCodeWriter qrCodeWriter = new QRCodeWriter();
BitMatrix qrMatrix = qrCodeWriter.encode(fragment, BarcodeFormat.QR_CODE, QR_WIDTH, QR_HEIGHT); BitMatrix qrMatrix = qrCodeWriter.encode(fragment, BarcodeFormat.QR_CODE, QR_WIDTH, QR_HEIGHT);

View file

@ -402,7 +402,7 @@ public class SettingsController extends WalletFormController implements Initiali
(walletForm.getWallet().getPolicyType() == PolicyType.MULTI ? "\nKey expressions are shown in canonical order." : "")); (walletForm.getWallet().getPolicyType() == PolicyType.MULTI ? "\nKey expressions are shown in canonical order." : ""));
Optional<String> text = dialog.showAndWait(); Optional<String> text = dialog.showAndWait();
if(text.isPresent() && !text.get().isEmpty() && !text.get().equals(outputDescriptorString)) { if(text.isPresent() && !text.get().isEmpty() && !text.get().equals(outputDescriptorString)) {
setDescriptorText(text.get()); setDescriptorText(text.get().replace("\n", ""));
} }
} }