diff --git a/drongo b/drongo index 189ef88b..60cb3ed8 160000 --- a/drongo +++ b/drongo @@ -1 +1 @@ -Subproject commit 189ef88b08e4aa430315e1e312046c833331bd91 +Subproject commit 60cb3ed85f46c3f1b7ddd071e6b02eb1ac334bb6 diff --git a/src/main/java/com/sparrowwallet/sparrow/io/Descriptor.java b/src/main/java/com/sparrowwallet/sparrow/io/Descriptor.java index e65d9f31..44a25fb5 100644 --- a/src/main/java/com/sparrowwallet/sparrow/io/Descriptor.java +++ b/src/main/java/com/sparrowwallet/sparrow/io/Descriptor.java @@ -66,6 +66,12 @@ public class Descriptor implements WalletImport, WalletExport { @Override public Wallet importWallet(InputStream inputStream, String password) throws ImportException { 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")); OutputDescriptor descriptor = OutputDescriptor.getOutputDescriptor(outputDescriptor.trim()); return descriptor.toWallet(); diff --git a/src/main/java/com/sparrowwallet/sparrow/io/PdfUtils.java b/src/main/java/com/sparrowwallet/sparrow/io/PdfUtils.java index c64ae8e2..489afc35 100644 --- a/src/main/java/com/sparrowwallet/sparrow/io/PdfUtils.java +++ b/src/main/java/com/sparrowwallet/sparrow/io/PdfUtils.java @@ -9,7 +9,11 @@ import com.google.zxing.qrcode.QRCodeWriter; import com.lowagie.text.*; import com.lowagie.text.Font; import com.lowagie.text.Image; +import com.lowagie.text.pdf.PdfReader; 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.UREncoder; import com.sparrowwallet.sparrow.AppServices; @@ -21,6 +25,7 @@ import org.slf4j.LoggerFactory; import java.awt.*; import java.io.*; +import java.util.Scanner; public class PdfUtils { 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 { QRCodeWriter qrCodeWriter = new QRCodeWriter(); BitMatrix qrMatrix = qrCodeWriter.encode(fragment, BarcodeFormat.QR_CODE, QR_WIDTH, QR_HEIGHT); diff --git a/src/main/java/com/sparrowwallet/sparrow/wallet/SettingsController.java b/src/main/java/com/sparrowwallet/sparrow/wallet/SettingsController.java index dc08ceee..dd9f8f28 100644 --- a/src/main/java/com/sparrowwallet/sparrow/wallet/SettingsController.java +++ b/src/main/java/com/sparrowwallet/sparrow/wallet/SettingsController.java @@ -402,7 +402,7 @@ public class SettingsController extends WalletFormController implements Initiali (walletForm.getWallet().getPolicyType() == PolicyType.MULTI ? "\nKey expressions are shown in canonical order." : "")); Optional text = dialog.showAndWait(); if(text.isPresent() && !text.get().isEmpty() && !text.get().equals(outputDescriptorString)) { - setDescriptorText(text.get()); + setDescriptorText(text.get().replace("\n", "")); } }