From 03e2e76b2432bd05ef73ee90f4209a53e2a483f0 Mon Sep 17 00:00:00 2001 From: Craig Raw Date: Sat, 3 Jan 2026 14:21:01 +0200 Subject: [PATCH] implement method to add missing key path information to psbts --- .../com/sparrowwallet/drongo/psbt/PSBT.java | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/src/main/java/com/sparrowwallet/drongo/psbt/PSBT.java b/src/main/java/com/sparrowwallet/drongo/psbt/PSBT.java index 115ddbf..d25b676 100644 --- a/src/main/java/com/sparrowwallet/drongo/psbt/PSBT.java +++ b/src/main/java/com/sparrowwallet/drongo/psbt/PSBT.java @@ -597,6 +597,37 @@ public class PSBT { return fee; } + public void addKeyPathInformation(Wallet signingWallet) { + List missingKeyPathInputs = new ArrayList<>(); + for(PSBTInput psbtInput : getPsbtInputs()) { + ScriptType scriptType = psbtInput.getScriptType(); + if((scriptType == ScriptType.P2TR && psbtInput.getTapDerivedPublicKeys().isEmpty()) || + (scriptType != null && scriptType != ScriptType.P2TR && psbtInput.getDerivedPublicKeys().isEmpty())) { + missingKeyPathInputs.add(psbtInput); + } + } + + if(!missingKeyPathInputs.isEmpty() && signingWallet != null) { + Map signingNodes = signingWallet.getSigningNodes(this); + for(PSBTInput psbtInput : missingKeyPathInputs) { + WalletNode walletNode = signingNodes.get(psbtInput); + if(walletNode != null && walletNode.getWallet() != null) { + for(Keystore keystore : signingWallet.getKeystores()) { + ScriptType scriptType = walletNode.getWallet().getScriptType(); + ECKey pubKey = keystore.getPubKey(walletNode); + KeyDerivation keyDerivation = keystore.getKeyDerivation().extend(walletNode.getDerivation()); + if(scriptType == ScriptType.P2TR) { + psbtInput.setTapInternalKey(ECKey.fromPublicOnly(pubKey.getPubKeyXCoord())); + psbtInput.getTapDerivedPublicKeys().put(psbtInput.getTapInternalKey(), Map.of(keyDerivation, Collections.emptyList())); + } else { + psbtInput.getDerivedPublicKeys().put(scriptType.getOutputKey(pubKey), keyDerivation); + } + } + } + } + } + } + public void verifySignatures() throws PSBTSignatureException { verifySignatures(getPsbtInputs()); }