support converting xprvs to xpubs in output descriptors

This commit is contained in:
Craig Raw 2023-02-28 09:55:51 +02:00
parent d0da764aad
commit 0f78efc373
2 changed files with 10 additions and 3 deletions

View file

@ -90,8 +90,8 @@ public class ExtendedKey {
ChildNumber childNumber; ChildNumber childNumber;
List<ChildNumber> path; List<ChildNumber> path;
if(depth == 0) { if(depth == 0 && !header.isPrivateKey()) {
//Poorly formatted extended key, add first child path element //Poorly formatted public extended key, add first child path element
childNumber = new ChildNumber(0, false); childNumber = new ChildNumber(0, false);
} else if ((i & ChildNumber.HARDENED_BIT) != 0) { } else if ((i & ChildNumber.HARDENED_BIT) != 0) {
childNumber = new ChildNumber(i ^ ChildNumber.HARDENED_BIT, true); //already hardened childNumber = new ChildNumber(i ^ ChildNumber.HARDENED_BIT, true); //already hardened

View file

@ -22,7 +22,7 @@ public class OutputDescriptor {
private static final String INPUT_CHARSET = "0123456789()[],'/*abcdefgh@:$%{}IJKLMNOPQRSTUVWXYZ&+-.;<=>?!^_|~ijklmnopqrstuvwxyzABCDEFGH`#\"\\ "; private static final String INPUT_CHARSET = "0123456789()[],'/*abcdefgh@:$%{}IJKLMNOPQRSTUVWXYZ&+-.;<=>?!^_|~ijklmnopqrstuvwxyzABCDEFGH`#\"\\ ";
private static final String CHECKSUM_CHARSET = "qpzry9x8gf2tvdw0s3jn54khce6mua7l"; 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 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 MULTI_PATTERN = Pattern.compile("multi\\(([\\d+])");
private static final Pattern KEY_ORIGIN_PATTERN = Pattern.compile("\\[([A-Fa-f0-9]{8})([/\\d'hH]+)\\]"); 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); KeyDerivation keyDerivation = new KeyDerivation(masterFingerprint, keyDerivationPath);
try { try {
ExtendedKey extendedPublicKey = ExtendedKey.fromDescriptor(extPubKey); ExtendedKey extendedPublicKey = ExtendedKey.fromDescriptor(extPubKey);
if(extendedPublicKey.getKey().hasPrivKey()) {
List<ChildNumber> 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); keyDerivationMap.put(extendedPublicKey, keyDerivation);
keyChildDerivationMap.put(extendedPublicKey, childDerivationPath); keyChildDerivationMap.put(extendedPublicKey, childDerivationPath);
} catch(ProtocolException e) { } catch(ProtocolException e) {