From 0f78efc373e99cf48a749ced25cd112ca083b88f Mon Sep 17 00:00:00 2001 From: Craig Raw Date: Tue, 28 Feb 2023 09:55:51 +0200 Subject: [PATCH] support converting xprvs to xpubs in output descriptors --- src/main/java/com/sparrowwallet/drongo/ExtendedKey.java | 4 ++-- .../java/com/sparrowwallet/drongo/OutputDescriptor.java | 9 ++++++++- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/sparrowwallet/drongo/ExtendedKey.java b/src/main/java/com/sparrowwallet/drongo/ExtendedKey.java index 37efd09..67b1d50 100644 --- a/src/main/java/com/sparrowwallet/drongo/ExtendedKey.java +++ b/src/main/java/com/sparrowwallet/drongo/ExtendedKey.java @@ -90,8 +90,8 @@ public class ExtendedKey { ChildNumber childNumber; List path; - if(depth == 0) { - //Poorly formatted extended key, add first child path element + if(depth == 0 && !header.isPrivateKey()) { + //Poorly formatted public extended key, add first child path element childNumber = new ChildNumber(0, false); } else if ((i & ChildNumber.HARDENED_BIT) != 0) { childNumber = new ChildNumber(i ^ ChildNumber.HARDENED_BIT, true); //already hardened diff --git a/src/main/java/com/sparrowwallet/drongo/OutputDescriptor.java b/src/main/java/com/sparrowwallet/drongo/OutputDescriptor.java index cf4defe..6698930 100644 --- a/src/main/java/com/sparrowwallet/drongo/OutputDescriptor.java +++ b/src/main/java/com/sparrowwallet/drongo/OutputDescriptor.java @@ -22,7 +22,7 @@ public class OutputDescriptor { private static final String INPUT_CHARSET = "0123456789()[],'/*abcdefgh@:$%{}IJKLMNOPQRSTUVWXYZ&+-.;<=>?!^_|~ijklmnopqrstuvwxyzABCDEFGH`#\"\\ "; private static final String CHECKSUM_CHARSET = "qpzry9x8gf2tvdw0s3jn54khce6mua7l"; - private static final Pattern XPUB_PATTERN = Pattern.compile("(\\[[^\\]]+\\])?(.pub[^/\\,)]{100,112})(/[/\\d*'hH<>;]+)?"); + private static final Pattern XPUB_PATTERN = Pattern.compile("(\\[[^\\]]+\\])?(.(?:pub|prv)[^/\\,)]{100,112})(/[/\\d*'hH<>;]+)?"); private static final Pattern PUBKEY_PATTERN = Pattern.compile("(\\[[^\\]]+\\])?(0[23][0-9a-fA-F]{32})"); private static final Pattern MULTI_PATTERN = Pattern.compile("multi\\(([\\d+])"); private static final Pattern KEY_ORIGIN_PATTERN = Pattern.compile("\\[([A-Fa-f0-9]{8})([/\\d'hH]+)\\]"); @@ -379,6 +379,13 @@ public class OutputDescriptor { KeyDerivation keyDerivation = new KeyDerivation(masterFingerprint, keyDerivationPath); try { ExtendedKey extendedPublicKey = ExtendedKey.fromDescriptor(extPubKey); + if(extendedPublicKey.getKey().hasPrivKey()) { + List derivation = keyDerivation.getDerivation(); + int depth = derivation.size() == 0 ? scriptType.getDefaultDerivation().size() : derivation.size(); + DeterministicKey prvKey = extendedPublicKey.getKey(); + DeterministicKey pubKey = new DeterministicKey(prvKey.getPath(), prvKey.getChainCode(), prvKey.getPubKey(), depth, extendedPublicKey.getParentFingerprint()); + extendedPublicKey = new ExtendedKey(pubKey, pubKey.getParentFingerprint(), extendedPublicKey.getKeyChildNumber()); + } keyDerivationMap.put(extendedPublicKey, keyDerivation); keyChildDerivationMap.put(extendedPublicKey, childDerivationPath); } catch(ProtocolException e) {