ensure consistent xpub ordering when copying output descriptors without child derivations

This commit is contained in:
Craig Raw 2024-12-02 10:07:55 +02:00
parent 3cb3d322a0
commit 64a3f1c00b

View file

@ -15,6 +15,7 @@ import java.math.BigInteger;
import java.util.*; import java.util.*;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import java.util.stream.Collectors;
import static com.sparrowwallet.drongo.KeyDerivation.parsePath; import static com.sparrowwallet.drongo.KeyDerivation.parsePath;
@ -681,7 +682,19 @@ public class OutputDescriptor {
} }
public OutputDescriptor copy(boolean includeChildDerivations) { public OutputDescriptor copy(boolean includeChildDerivations) {
return new OutputDescriptor(scriptType, multisigThreshold, extendedPublicKeys, Map<ExtendedKey, KeyDerivation> copyExtendedPublicKeys = new LinkedHashMap<>(extendedPublicKeys);
includeChildDerivations ? mapChildrenDerivations : Collections.emptyMap(), mapExtendedPublicKeyLabels, extendedMasterPrivateKeys); Map<ExtendedKey, String> copyChildDerivations = new LinkedHashMap<>(mapChildrenDerivations);
Map<ExtendedKey, String> copyExtendedPublicKeyLabels = new LinkedHashMap<>(mapExtendedPublicKeyLabels);
Map<ExtendedKey, ExtendedKey> copyExtendedMasterPrivateKeys = new LinkedHashMap<>(extendedMasterPrivateKeys);
if(!includeChildDerivations) {
//Ensure consistent xpub order by sorting on the first receive address
Map<ExtendedKey, String> childDerivations = copyExtendedPublicKeys.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, _ -> "/0/0"));
OutputDescriptor copyFirstReceive = new OutputDescriptor(scriptType, multisigThreshold, copyExtendedPublicKeys, childDerivations);
OutputDescriptor copySortedXpubs = OutputDescriptor.getOutputDescriptor(copyFirstReceive.toString());
return new OutputDescriptor(scriptType, multisigThreshold, copySortedXpubs.extendedPublicKeys, Collections.emptyMap(), copyExtendedPublicKeyLabels, copyExtendedMasterPrivateKeys);
}
return new OutputDescriptor(scriptType, multisigThreshold, copyExtendedPublicKeys, copyChildDerivations, copyExtendedPublicKeyLabels, copyExtendedMasterPrivateKeys);
} }
} }