apply the child derivations of keys in scanned output descriptors, and refuse those that do not derive on the standard chains
What changed, and why it matters
This commit changes how Sparrow Wallet handles QR code and file imports of Bitcoin wallet descriptors. Previously, when scanning a descriptor from a QR code, the wallet might ignore or mishandle 'child derivation' paths that tell it how to generate receiving and change addresses. The fix applies those child derivations consistently and rejects descriptors whose derivation paths are not on the standard receive/change chains. This prevents a malicious or malformed descriptor from silently importing a wallet that derives addresses in an unexpected way, which could lead to funds being sent to addresses the user does not control or cannot easily recover.
Review the verifyStandardChildDerivations() implementation in the OutputDescriptor class to confirm it correctly enforces BIP44/49/84/86 standard external/internal chains. Ensure that the new IllegalStateException paths are surfaced clearly to users and logged. Consider whether hardened child derivations or other non-standard but legitimate paths should be supported with explicit user confirmation rather than outright refusal.
Security signals we found
Input validation added for scanned output descriptor child derivations
Rejection of non-standard child derivation chains
Consistent application of child derivations between QR/file import and text descriptor parsing
Private xprv in QR reduced to public xpub before descriptor creation
Graceful skipping of unconvertible account outputs instead of failing silently or importing incorrectly
Evidence from the diff
The patch updates QRScanDialog and FileImportPane to parse and apply child derivation paths (CryptoHDKey.getChildren / URHDKey.getChildren) when converting scanned output descriptors into OutputDescriptor objects. It adds getChildDerivationPath() to translate CryptoKeypath components into descriptor path suffixes, supports IndexPathComponent, PairPathComponent, and unhardened WildcardPathComponent, and throws IllegalStateException for unsupported components such as RangePathComponent or hardened wildcards. The converted descriptor is then re-parsed from text so child derivations are applied to the keys exactly as for text descriptors. FileImportPane now calls verifyStandardChildDerivations() before importing, and getWallets() skips individual unconvertible outputs while surfacing the error only if none convert. Tests verify that standard paths like /7/<0;1>/* are applied, non-standard paths are rejected, labels are preserved, and private keys are reduced to public keys.
Changed components
QRScanDialog.QRResultListenerFileImportPaneOutputDescriptor parsing and wallet import flowUR/CryptoAccount and CryptoOutput handlingInspect captured patch +290 / −40
### src/main/java/com/sparrowwallet/sparrow/control/FileImportPane.java
@@ -167,9 +167,10 @@ private void importQR() {
}
} else if(result.outputDescriptor != null) {
try {
+ result.outputDescriptor.verifyStandardChildDerivations();
wallets = List.of(result.outputDescriptor.toWallet());
importFile(importer.getName(), null, null);
- } catch(ImportException e) {
+ } catch(ImportException | IllegalStateException e) {
log.error("Error importing QR", e);
setError("Import Error", e.getMessage());
}
### src/main/java/com/sparrowwallet/sparrow/control/QRScanDialog.java
@@ -23,7 +23,9 @@
import com.sparrowwallet.hummingbird.UR;
import com.sparrowwallet.hummingbird.URDecoder;
import com.sparrowwallet.hummingbird.registry.pathcomponent.IndexPathComponent;
+import com.sparrowwallet.hummingbird.registry.pathcomponent.PairPathComponent;
import com.sparrowwallet.hummingbird.registry.pathcomponent.PathComponent;
+import com.sparrowwallet.hummingbird.registry.pathcomponent.WildcardPathComponent;
import com.sparrowwallet.sparrow.AppServices;
import com.sparrowwallet.sparrow.EventManager;
import com.sparrowwallet.sparrow.event.WebcamResolutionChangedEvent;
@@ -233,7 +235,7 @@ public QRScanDialog() {
setResultConverter(dialogButton -> dialogButton != cancelButtonType ? result : null);
}
- private class QRResultListener implements ChangeListener<com.google.zxing.Result> {
+ class QRResultListener implements ChangeListener<com.google.zxing.Result> {
@Override
public void changed(ObservableValue<? extends com.google.zxing.Result> observable, com.google.zxing.Result oldValue, com.google.zxing.Result qrResult) {
if(result != null) {
@@ -556,6 +558,9 @@ private Result extractResultFromUR(UR ur) {
} catch(UR.InvalidCBORException e) {
log.error("Invalid CBOR in UR", e);
return new Result(new URException("Invalid CBOR in UR", e));
+ } catch(IllegalStateException e) {
+ log.error("Error converting UR", e);
+ return new Result(new URException(e.getMessage(), e));
} catch(Exception e) {
log.error("Error parsing UR CBOR", e);
return new Result(new URException("Error parsing UR CBOR", e));
@@ -574,7 +579,7 @@ private Address getAddress(CryptoAddress cryptoAddress) {
return address;
}
- private ExtendedKey getExtendedKey(CryptoHDKey cryptoHDKey) {
+ private static ExtendedKey getExtendedKey(CryptoHDKey cryptoHDKey) {
if(cryptoHDKey.isPrivateKey()) {
DeterministicKey prvKey = HDKeyDerivation.createMasterPrivKeyFromBytes(Arrays.copyOfRange(cryptoHDKey.getKey(), 1, 33), cryptoHDKey.getChainCode(), List.of(ChildNumber.ZERO));
return new ExtendedKey(prvKey, new byte[4], ChildNumber.ZERO);
@@ -583,7 +588,7 @@ private ExtendedKey getExtendedKey(CryptoHDKey cryptoHDKey) {
return getExtendedPublicKey(cryptoHDKey);
}
- private ExtendedKey getExtendedPublicKey(CryptoHDKey cryptoHDKey) {
+ private static ExtendedKey getExtendedPublicKey(CryptoHDKey cryptoHDKey) {
byte[] publicKey = cryptoHDKey.isPrivateKey() ? ECKey.fromPrivate(new BigInteger(1, cryptoHDKey.getKey())).getPubKey() : cryptoHDKey.getKey();
ChildNumber lastChild = ChildNumber.ZERO;
int depth = 1;
@@ -604,59 +609,124 @@ private ExtendedKey getExtendedPublicKey(CryptoHDKey cryptoHDKey) {
return new ExtendedKey(pubKey, parentFingerprint, lastChild);
}
- private ECKey getKey(CryptoHDKey cryptoHDKey) {
+ private static ECKey getKey(CryptoHDKey cryptoHDKey) {
if(cryptoHDKey.isPrivateKey()) {
return ECKey.fromPrivate(new BigInteger(1, cryptoHDKey.getKey()));
} else {
return ECKey.fromPublicOnly(cryptoHDKey.getKey());
}
}
- private OutputDescriptor getOutputDescriptor(CryptoOutput cryptoOutput) {
+ static OutputDescriptor getOutputDescriptor(CryptoOutput cryptoOutput) {
ScriptType scriptType = getScriptType(cryptoOutput.getScriptExpressions());
+ List<CryptoHDKey> cryptoHDKeys;
+ int threshold = 0;
if(cryptoOutput.getMultiKey() != null) {
MultiKey multiKey = cryptoOutput.getMultiKey();
if(multiKey.getEcKeys() != null && !multiKey.getEcKeys().isEmpty()) {
throw new IllegalArgumentException("EC keys are currently unsupported");
}
- Map<ExtendedKey, KeyDerivation> extendedPublicKeys = new LinkedHashMap<>();
- Map<ExtendedKey, String> extendedPublicKeyLabels = new LinkedHashMap<>();
- for(CryptoHDKey cryptoHDKey : multiKey.getHdKeys()) {
- ExtendedKey extendedKey = getExtendedPublicKey(cryptoHDKey);
- KeyDerivation keyDerivation = getKeyDerivation(cryptoHDKey.getOrigin());
- if(extendedPublicKeys.put(extendedKey, keyDerivation) != null) {
- throw new IllegalArgumentException("Duplicate extended keys are not supported");
- }
- if(cryptoHDKey.getName() != null) {
- extendedPublicKeyLabels.put(extendedKey, cryptoHDKey.getName());
- }
- }
- return new OutputDescriptor(scriptType, multiKey.getThreshold(), extendedPublicKeys, new LinkedHashMap<>(), extendedPublicKeyLabels);
+ cryptoHDKeys = multiKey.getHdKeys();
+ threshold = multiKey.getThreshold();
} else if(cryptoOutput.getEcKey() != null) {
throw new IllegalArgumentException("EC keys are currently unsupported");
} else if(cryptoOutput.getHdKey() != null) {
- ExtendedKey extendedKey = getExtendedPublicKey(cryptoOutput.getHdKey());
- KeyDerivation keyDerivation = getKeyDerivation(cryptoOutput.getHdKey().getOrigin());
- return new OutputDescriptor(scriptType, extendedKey, keyDerivation, cryptoOutput.getHdKey().getName());
+ cryptoHDKeys = List.of(cryptoOutput.getHdKey());
+ } else {
+ throw new IllegalStateException("CryptoOutput did not contain sufficient information");
+ }
+
+ Map<ExtendedKey, KeyDerivation> extendedPublicKeys = new LinkedHashMap<>();
+ Map<ExtendedKey, String> childDerivations = new LinkedHashMap<>();
+ Map<ExtendedKey, String> extendedPublicKeyLabels = new LinkedHashMap<>();
+ for(CryptoHDKey cryptoHDKey : cryptoHDKeys) {
+ ExtendedKey extendedKey = getExtendedPublicKey(cryptoHDKey);
+ KeyDerivation keyDerivation = getKeyDerivation(cryptoHDKey.getOrigin());
+ if(extendedPublicKeys.put(extendedKey, keyDerivation) != null) {
+ throw new IllegalArgumentException("Duplicate extended keys are not supported");
+ }
+ String childDerivation = getChildDerivationPath(cryptoHDKey.getChildren());
+ if(childDerivation != null) {
+ childDerivations.put(extendedKey, childDerivation);
+ }
+ if(cryptoHDKey.getName() != null) {
+ extendedPublicKeyLabels.put(extendedKey, cryptoHDKey.getName());
+ }
+ }
+
+ OutputDescriptor outputDescriptor = new OutputDescriptor(scriptType, threshold, extendedPublicKeys, childDerivations, extendedPublicKeyLabels);
+ if(!childDerivations.isEmpty()) {
+ //Parsed from text, so that the child derivations are applied to the keys as they are for a descriptor entered as text. The keys of a
+ //multisig descriptor are written in sorted order, so their labels are listed in that order
+ List<String> labels = outputDescriptor.sortExtendedPubKeys(extendedPublicKeys.keySet()).stream().map(extendedPublicKeyLabels::get).toList();
+ outputDescriptor = getLabelledOutputDescriptor(outputDescriptor.toString(), labels);
+ }
+
+ return outputDescriptor;
+ }
+
+ /**
+ * Parses a descriptor whose keys may be replaced by those derived through their child derivations, labelling them in the order they appear.
+ */
+ private static OutputDescriptor getLabelledOutputDescriptor(String descriptor, List<String> labels) {
+ Map<ExtendedKey, String> extendedPublicKeyLabels = new LinkedHashMap<>();
+ Iterator<String> labelIterator = labels.iterator();
+ for(ExtendedKey extendedKey : OutputDescriptor.getOutputDescriptor(descriptor).getExtendedPublicKeys()) {
+ String label = labelIterator.hasNext() ? labelIterator.next() : null;
+ if(label != null) {
+ extendedPublicKeyLabels.put(extendedKey, label);
+ }
}
- throw new IllegalStateException("CryptoOutput did not contain sufficient information");
+ return OutputDescriptor.getOutputDescriptor(descriptor, extendedPublicKeyLabels);
}
- private List<Wallet> getWallets(CryptoAccount cryptoAccount) {
+ static String getChildDerivationPath(CryptoKeypath children) {
+ if(children == null || children.getComponents() == null || children.getComponents().isEmpty()) {
+ return null;
+ }
+
+ StringBuilder childDerivation = new StringBuilder();
+ for(PathComponent component : children.getComponents()) {
+ childDerivation.append("/");
+ if(component instanceof IndexPathComponent indexPathComponent) {
+ childDerivation.append(indexPathComponent.getIndex()).append(indexPathComponent.isHardened() ? "h" : "");
+ } else if(component instanceof PairPathComponent pair && !pair.getExternal().isHardened() && !pair.getInternal().isHardened()) {
+ childDerivation.append("<").append(pair.getExternal().getIndex()).append(";").append(pair.getInternal().getIndex()).append(">");
+ } else if(component instanceof WildcardPathComponent wildcard && !wildcard.isHardened()) {
+ childDerivation.append("*");
+ } else {
+ throw new IllegalStateException("The child derivation of a key in this QR code is not supported");
+ }
+ }
+
+ return childDerivation.toString();
+ }
+
+ static List<Wallet> getWallets(CryptoAccount cryptoAccount) {
List<Wallet> wallets = new ArrayList<>();
String masterFingerprint = Utils.bytesToHex(cryptoAccount.getMasterFingerprint());
+ IllegalStateException unconverted = null;
for(CryptoOutput cryptoOutput : cryptoAccount.getOutputDescriptors()) {
- OutputDescriptor outputDescriptor = getOutputDescriptor(cryptoOutput);
- Wallet wallet = outputDescriptor.toKeystoreWallet(masterFingerprint);
- wallets.add(wallet);
+ //Only the key and its origin are taken from each output, so one that cannot be converted leaves the others for the user to choose from
+ try {
+ OutputDescriptor outputDescriptor = getOutputDescriptor(cryptoOutput);
+ wallets.add(outputDescriptor.toKeystoreWallet(masterFingerprint));
+ } catch(IllegalStateException e) {
+ log.warn("Could not convert an output descriptor of the account: " + e.getMessage());
+ unconverted = e;
+ }
+ }
+
+ if(wallets.isEmpty() && unconverted != null) {
+ throw unconverted;
}
return wallets;
}
- private ScriptType getScriptType(List<ScriptExpression> scriptExpressions) {
+ private static ScriptType getScriptType(List<ScriptExpression> scriptExpressions) {
List<ScriptExpression> expressions = new ArrayList<>(scriptExpressions);
if(expressions.get(expressions.size() - 1) == ScriptExpression.MULTISIG
|| expressions.get(expressions.size() - 1) == ScriptExpression.SORTED_MULTISIG
@@ -683,7 +753,7 @@ private ScriptType getScriptType(List<ScriptExpression> scriptExpressions) {
throw new IllegalArgumentException("Unknown script of " + expressions);
}
- private KeyDerivation getKeyDerivation(CryptoKeypath cryptoKeypath) {
+ private static KeyDerivation getKeyDerivation(CryptoKeypath cryptoKeypath) {
if(cryptoKeypath != null) {
if(!cryptoKeypath.getComponents().stream().allMatch(pathComponent -> pathComponent instanceof IndexPathComponent)) {
throw new IllegalArgumentException("Only indexed derivation path components are supported");
@@ -706,10 +776,22 @@ private DeterministicSeed getSeed(CryptoBip39 cryptoBip39) {
return new DeterministicSeed(cryptoBip39.getWords(), null, System.currentTimeMillis(), DeterministicSeed.Type.BIP39);
}
- private OutputDescriptor getOutputDescriptor(UROutputDescriptor urOutputDescriptor) {
+ static OutputDescriptor getOutputDescriptor(UROutputDescriptor urOutputDescriptor) {
String source = urOutputDescriptor.getSource();
List<RegistryItem> keys = urOutputDescriptor.getKeys();
- Map<ExtendedKey, String> mapExtendedPublicKeyLabels = new LinkedHashMap<>();
+
+ //Labelled in the order the extended keys first appear in the source, which is the order of the keys of the descriptor parsed from it
+ Set<Integer> placeholderIndexes = new LinkedHashSet<>();
+ Matcher placeholderMatcher = Pattern.compile("@(\\d+)").matcher(source);
+ while(placeholderMatcher.find()) {
+ placeholderIndexes.add(Integer.parseInt(placeholderMatcher.group(1)));
+ }
+ List<String> labels = new ArrayList<>();
+ for(int index : placeholderIndexes) {
+ if(index < keys.size() && keys.get(index) instanceof URHDKey urhdKey && urhdKey.getChainCode() != null) {
+ labels.add(urhdKey.getName());
+ }
+ }
for(int i = 0; i < keys.size(); i++) {
RegistryItem key = keys.get(i);
@@ -720,26 +802,36 @@ private OutputDescriptor getOutputDescriptor(UROutputDescriptor urOutputDescript
source = source.replaceAll("@" + i + "(?!\\d)", Matcher.quoteReplacement(OutputDescriptor.writeKey(ecKey, keyDerivation, true, true)));
} else {
ExtendedKey extendedKey = getExtendedKey(urhdKey);
- source = source.replaceAll("@" + i + "(?!\\d)", Matcher.quoteReplacement(OutputDescriptor.writeKey(extendedKey, keyDerivation, null, true, true)));
- if(urhdKey.getName() != null) {
- mapExtendedPublicKeyLabels.put(extendedKey, urhdKey.getName());
- }
+ String childDerivation = getChildDerivationPath(urhdKey.getChildren());
+ source = source.replaceAll("@" + i + "(?=/)", Matcher.quoteReplacement(OutputDescriptor.writeKey(extendedKey, keyDerivation, null, true, true)));
+ String keyExpression = OutputDescriptor.writeKey(extendedKey, keyDerivation, childDerivation, true, true);
+ source = source.replaceAll("@" + i + "(?![\\d/])", Matcher.quoteReplacement(keyExpression));
}
} else {
throw new IllegalArgumentException("Only extended HD keys are supported in output descriptors");
}
}
- return OutputDescriptor.getOutputDescriptor(source, mapExtendedPublicKeyLabels);
+ return getLabelledOutputDescriptor(source, labels);
}
- private List<Wallet> getWallets(URAccountDescriptor urAccountDescriptor) {
+ static List<Wallet> getWallets(URAccountDescriptor urAccountDescriptor) {
List<Wallet> wallets = new ArrayList<>();
String masterFingerprint = Utils.bytesToHex(urAccountDescriptor.getMasterFingerprint());
+ IllegalStateException unconverted = null;
for(UROutputDescriptor urOutputDescriptor : urAccountDescriptor.getOutputDescriptors()) {
- OutputDescriptor outputDescriptor = getOutputDescriptor(urOutputDescriptor);
- Wallet wallet = outputDescriptor.toKeystoreWallet(masterFingerprint);
- wallets.add(wallet);
+ //Only the key and its origin are taken from each output, so one that cannot be converted leaves the others for the user to choose from
+ try {
+ OutputDescriptor outputDescriptor = getOutputDescriptor(urOutputDescriptor);
+ wallets.add(outputDescriptor.toKeystoreWallet(masterFingerprint));
+ } catch(IllegalStateException e) {
+ log.warn("Could not convert an output descriptor of the account: " + e.getMessage());
+ unconverted = e;
+ }
+ }
+
+ if(wallets.isEmpty() && unconverted != null) {
+ throw unconverted;
}
return wallets;
### src/test/java/com/sparrowwallet/sparrow/control/QRScanDialogTest.java
@@ -0,0 +1,157 @@
+package com.sparrowwallet.sparrow.control;
+
+import com.sparrowwallet.drongo.ExtendedKey;
+import com.sparrowwallet.drongo.OutputDescriptor;
+import com.sparrowwallet.drongo.Utils;
+import com.sparrowwallet.drongo.protocol.ScriptType;
+import com.sparrowwallet.drongo.wallet.Wallet;
+import com.sparrowwallet.hummingbird.registry.*;
+import com.sparrowwallet.hummingbird.registry.pathcomponent.*;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class QRScanDialogTest {
+ //BIP32 test vector 1 at m/0', a child of the master key with fingerprint 3442193e
+ private static final String XPUB = "xpub68Gmy5EdvgibQVfPdqkBBCHxA5htiqg55crXYuXoQRKfDBFA1WEjWgP6LHhwBZeNK1VTsfTFUHCdrfp1bgwQ9xv5ski8PX9rL2dZXvgGDnw";
+ private static final String XPRV = "xprv9uHRZZhk6KAJC1avXpDAp4MDc3sQKNxDiPvvkX8Br5ngLNv1TxvUxt4cV1rGL5hj6KCesnDYUhd7oWgT11eZG7XnxHrnYeSvkzY7d2bhkJ7";
+ private static final String ORIGIN = "[3442193e/0']";
+ //The same vector at m/0'/1
+ private static final String SECOND_XPUB = "xpub6ASuArnXKPbfEwhqN6e3mwBcDTgzisQN1wXN9BJcM47sSikHjJf3UFHKkNAWbWMiGj7Wf5uMash7SyYq527Hqck2AxYysAA7xmALppuCkwQ";
+
+ private static CryptoKeypath origin() {
+ return new CryptoKeypath(List.of(new IndexPathComponent(0, true)), Utils.hexToBytes("3442193e"));
+ }
+
+ private static CryptoKeypath children(PathComponent... components) {
+ return new CryptoKeypath(List.of(components), null);
+ }
+
+ private static PairPathComponent receiveAndChange() {
+ return new PairPathComponent(new IndexPathComponent(0, false), new IndexPathComponent(1, false));
+ }
+
+ private static CryptoHDKey hdKey(CryptoKeypath children) {
+ ExtendedKey xpub = ExtendedKey.fromDescriptor(XPUB);
+ return new CryptoHDKey(false, xpub.getKey().getPubKey(), xpub.getKey().getChainCode(), null, origin(), children, xpub.getParentFingerprint());
+ }
+
+ private static OutputDescriptor fromCryptoOutput(CryptoHDKey hdKey) {
+ return QRScanDialog.QRResultListener.getOutputDescriptor(new CryptoOutput(List.of(ScriptExpression.WITNESS_PUBLIC_KEY_HASH), hdKey));
+ }
+
+ @Test
+ public void cryptoOutputChildrenAreAppliedAsInText() {
+ OutputDescriptor scanned = fromCryptoOutput(hdKey(children(new IndexPathComponent(7, false), receiveAndChange(), new WildcardPathComponent(false))));
+ OutputDescriptor text = OutputDescriptor.getOutputDescriptor("wpkh(" + ORIGIN + XPUB + "/7/<0;1>/*)");
+ Assertions.assertEquals(text.toString(), scanned.toString());
+ Assertions.assertNotEquals(XPUB, scanned.getExtendedPublicKeys().iterator().next().toString());
+ }
+
+ @Test
+ public void cryptoOutputWithoutChildrenIsUnchanged() {
+ OutputDescriptor scanned = fromCryptoOutput(hdKey(null));
+ Assertions.assertEquals(XPUB, scanned.getExtendedPublicKeys().iterator().next().toString());
+ }
+
+ @Test
+ public void cryptoOutputWithNonStandardChildrenIsLeftForTheConsumerToCheck() {
+ OutputDescriptor scanned = fromCryptoOutput(hdKey(children(new IndexPathComponent(2, false), new WildcardPathComponent(false))));
+ Assertions.assertFalse(scanned.hasStandardChildDerivations());
+ Assertions.assertThrows(IllegalStateException.class, () -> fromCryptoOutput(hdKey(children(new RangePathComponent(0, 5, false)))));
+ }
+
+ @Test
+ public void accountOutputThatCannotBeConvertedIsSkipped() {
+ CryptoOutput unconvertible = new CryptoOutput(List.of(ScriptExpression.WITNESS_PUBLIC_KEY_HASH), hdKey(children(new RangePathComponent(0, 5, false))));
+ CryptoOutput lone = new CryptoOutput(List.of(ScriptExpression.PUBLIC_KEY_HASH), hdKey(children(new WildcardPathComponent(false))));
+ CryptoAccount account = new CryptoAccount(Utils.hexToBytes("3442193e"), List.of(unconvertible, lone));
+
+ List<Wallet> wallets = QRScanDialog.QRResultListener.getWallets(account);
+ Assertions.assertEquals(1, wallets.size());
+ Assertions.assertEquals(ScriptType.P2PKH, wallets.getFirst().getScriptType());
+ Assertions.assertEquals(XPUB, wallets.getFirst().getKeystores().getFirst().getExtendedPublicKey().toString());
+
+ CryptoAccount allUnconvertible = new CryptoAccount(Utils.hexToBytes("3442193e"), List.of(unconvertible));
+ Assertions.assertThrows(IllegalStateException.class, () -> QRScanDialog.QRResultListener.getWallets(allUnconvertible));
+ }
+
+ @Test
+ public void cryptoOutputPrivateKeyIsReducedToItsPublicKey() {
+ ExtendedKey xprv = ExtendedKey.fromDescriptor(XPRV);
+ byte[] keyData = new byte[33];
+ System.arraycopy(xprv.getKey().getPrivKeyBytes(), 0, keyData, 1, 32);
+ CryptoHDKey privateHdKey = new CryptoHDKey(true, keyData, xprv.getKey().getChainCode(), null, origin(), null, Utils.hexToBytes("3442193e"));
+
+ OutputDescriptor scanned = fromCryptoOutput(privateHdKey);
+ ExtendedKey key = scanned.getExtendedPublicKeys().iterator().next();
+ Assertions.assertTrue(key.getKey().isPubKeyOnly());
+ Assertions.assertEquals(XPUB, key.toString());
+ }
+
+ @Test
+ public void urOutputDescriptorChildrenAreAppliedAsInText() {
+ ExtendedKey xpub = ExtendedKey.fromDescriptor(XPUB);
+ URKeypath origin = new URKeypath(List.of(new IndexPathComponent(0, true)), Utils.hexToBytes("3442193e"));
+ URKeypath children = new URKeypath(List.of(new IndexPathComponent(7, false), receiveAndChange(), new WildcardPathComponent(false)), null);
+ URHDKey urhdKey = new URHDKey(false, xpub.getKey().getPubKey(), xpub.getKey().getChainCode(), null, origin, children, xpub.getParentFingerprint());
+
+ OutputDescriptor scanned = QRScanDialog.QRResultListener.getOutputDescriptor(new UROutputDescriptor("wpkh(@0)", new ArrayList<>(List.of(urhdKey))));
+ OutputDescriptor text = OutputDescriptor.getOutputDescriptor("wpkh(" + ORIGIN + XPUB + "/7/<0;1>/*)");
+ Assertions.assertEquals(text.toString(), scanned.toString());
+ }
+
+ private static URHDKey urHdKey(String xpubString, List<PathComponent> origin, String name) {
+ ExtendedKey xpub = ExtendedKey.fromDescriptor(xpubString);
+ URKeypath urOrigin = new URKeypath(origin, Utils.hexToBytes("3442193e"));
+ URKeypath children = new URKeypath(List.of(new IndexPathComponent(7, false), receiveAndChange(), new WildcardPathComponent(false)), null);
+ return new URHDKey(false, xpub.getKey().getPubKey(), xpub.getKey().getChainCode(), null, urOrigin, children, xpub.getParentFingerprint(), name, null);
+ }
+
+ @Test
+ public void cryptoOutputKeyKeepsItsNameWhenChildrenAreApplied() {
+ ExtendedKey xpub = ExtendedKey.fromDescriptor(XPUB);
+ CryptoKeypath children = children(new IndexPathComponent(7, false), receiveAndChange(), new WildcardPathComponent(false));
+ CryptoHDKey named = new CryptoHDKey(false, xpub.getKey().getPubKey(), xpub.getKey().getChainCode(), null, origin(), children, xpub.getParentFingerprint(), "Device", null);
+
+ OutputDescriptor scanned = fromCryptoOutput(named);
+ Assertions.assertEquals("Device", scanned.getExtendedPublicKeyLabel(scanned.getExtendedPublicKeys().iterator().next()));
+ }
+
+ @Test
+ public void urOutputDescriptorKeysKeepTheirNamesInSourceOrder() {
+ URHDKey first = urHdKey(XPUB, List.of(new IndexPathComponent(0, true)), "First");
+ URHDKey second = urHdKey(SECOND_XPUB, List.of(new IndexPathComponent(0, true), new IndexPathComponent(1, false)), "Second");
+
+ OutputDescriptor scanned = QRScanDialog.QRResultListener.getOutputDescriptor(new UROutputDescriptor("wsh(sortedmulti(1,@1,@0))", new ArrayList<>(List.of(first, second))));
+ for(ExtendedKey key : scanned.getExtendedPublicKeys()) {
+ String expected = scanned.getKeyDerivation(key).getDerivationPath().startsWith("m/0'/1/") ? "Second" : "First";
+ Assertions.assertEquals(expected, scanned.getExtendedPublicKeyLabel(key));
+ }
+ }
+
+ @Test
+ public void cryptoOutputCosignersKeepTheirNamesInEitherScanOrder() {
+ CryptoHDKey first = namedHdKey(XPUB, List.of(new IndexPathComponent(0, true)), "First");
+ CryptoHDKey second = namedHdKey(SECOND_XPUB, List.of(new IndexPathComponent(0, true), new IndexPathComponent(1, false)), "Second");
+
+ for(List<CryptoHDKey> scanOrder : List.of(List.of(first, second), List.of(second, first))) {
+ CryptoOutput cryptoOutput = new CryptoOutput(List.of(ScriptExpression.WITNESS_SCRIPT_HASH, ScriptExpression.SORTED_MULTISIG), new MultiKey(1, null, scanOrder));
+ OutputDescriptor scanned = QRScanDialog.QRResultListener.getOutputDescriptor(cryptoOutput);
+ Assertions.assertEquals(2, scanned.getExtendedPublicKeys().size());
+ for(ExtendedKey key : scanned.getExtendedPublicKeys()) {
+ String expected = scanned.getKeyDerivation(key).getDerivationPath().startsWith("m/0'/1/") ? "Second" : "First";
+ Assertions.assertEquals(expected, scanned.getExtendedPublicKeyLabel(key));
+ }
+ }
+ }
+
+ private static CryptoHDKey namedHdKey(String xpubString, List<PathComponent> origin, String name) {
+ ExtendedKey xpub = ExtendedKey.fromDescriptor(xpubString);
+ CryptoKeypath cryptoOrigin = new CryptoKeypath(origin, Utils.hexToBytes("3442193e"));
+ CryptoKeypath children = children(new IndexPathComponent(7, false), receiveAndChange(), new WildcardPathComponent(false));
+ return new CryptoHDKey(false, xpub.getKey().getPubKey(), xpub.getKey().getChainCode(), null, cryptoOrigin, children, xpub.getParentFingerprint(), name, null);
+ }
+}Why this scored 58/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.