verify payjoin receiver input signatures against their declared utxos before checking the proposal fee
What changed, and why it matters
This commit fixes a security gap in Sparrow Wallet's Payjoin handling. Payjoin lets a receiver add their own bitcoin inputs to a transaction you are sending. Before this change, Sparrow could be tricked into accepting a Payjoin proposal where the receiver's claimed coins were not properly signed, or where the receiver hid the real previous transaction for old-style (non-Segwit) inputs. The change makes Sparrow verify the receiver's signatures against the exact coins they declared, and it rejects proposals that omit required data for non-Segwit inputs. This closes a path where a malicious or buggy payjoin server could make the sender pay more fees or include invalid inputs.
Treat this as a security fix and include it in the next release. Users who initiate Payjoin transactions should upgrade. Review the drongo submodule changelog for related PSBT verification changes. Consider whether any prior release allowed Payjoin proposals to bypass these checks and assess if a security advisory is warranted.
Security signals we found
Payjoin receiver input signature verification added before fee check
Mandatory non_witness_utxo enforcement for non-Segwit receiver inputs
New test: receiver input signed for different amount is rejected
New test: non-Segwit receiver input missing non_witness_utxo is rejected
New test: Taproot receiver input verified after original UTXOs filled
drongo submodule updated, likely providing signature verification primitives
Evidence from the diff
The patch moves and strengthens validation in Payjoin.checkProposal(). It now: (1) collects the receiver-added PSBT inputs, (2) requires non_witness_utxo for non-Segwit receiver inputs, and (3) calls verifyFinalizedSignatures() on each receiver input after the original sender inputs’ UTXOs have been populated. Previously, signature/fee checks appear to have happened in an order that could let a receiver-signed-for-different-amount or missing-utxo proposal pass. The drongo submodule bump likely carries the underlying PSBT signature verification support. New unit tests confirm valid signatures are accepted, signatures over a different amount are rejected, missing non_witness_utxo for P2PKH is rejected, and Taproot receiver inputs are verified even when the proposal omits the original input’s UTXO.
Changed components
src/main/java/com/sparrowwallet/sparrow/payjoin/Payjoin.javadrongo submodulePayjoin receiver input validation flowInspect captured patch +113 / −22
### drongo
@@ -1 +1 @@
-Subproject commit d31b0249542d4616187e01466a81f98f1d70c935
+Subproject commit 6236bf0eedeeac6cdb004c9df45c633b2dc7b080
### src/main/java/com/sparrowwallet/sparrow/payjoin/Payjoin.java
@@ -4,6 +4,7 @@
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.sparrowwallet.drongo.protocol.Script;
+import com.sparrowwallet.drongo.protocol.ScriptType;
import com.sparrowwallet.drongo.protocol.Transaction;
import com.sparrowwallet.drongo.protocol.TransactionInput;
import com.sparrowwallet.drongo.protocol.TransactionOutput;
@@ -153,6 +154,7 @@ void checkProposal(PSBT original, PSBT proposal, int changeOutputIndex, long max
}
Set<Long> sequences = new HashSet<>();
+ List<PSBTInput> receiverInputs = new ArrayList<>();
// For each inputs in the proposal:
for(PSBTInput proposedPSBTInput : proposal.getPsbtInputs()) {
if(!proposedPSBTInput.getDerivedPublicKeys().isEmpty() || !proposedPSBTInput.getTapDerivedPublicKeys().isEmpty() || proposedPSBTInput.getTapInternalKey() != null) {
@@ -204,6 +206,11 @@ void checkProposal(PSBT original, PSBT proposal, int changeOutputIndex, long max
if(wallet.getScriptType() != proposedPSBTInput.getScriptType()) {
throw new PayjoinReceiverException("Proposal script type of " + proposedPSBTInput.getScriptType() + " did not match wallet script type of " + wallet.getScriptType());
}
+ // Verify that non_witness_utxo is filled in for non segwit inputs.
+ if(Arrays.asList(ScriptType.NON_WITNESS_TYPES).contains(proposedPSBTInput.getScriptType()) && proposedPSBTInput.getNonWitnessUtxo() == null) {
+ throw new PayjoinReceiverException("The receiver did not specify non_witness_utxo for one of their non segwit inputs");
+ }
+ receiverInputs.add(proposedPSBTInput);
}
}
@@ -212,6 +219,16 @@ void checkProposal(PSBT original, PSBT proposal, int changeOutputIndex, long max
throw new PayjoinReceiverException("Some of the original inputs are not included in the proposal");
}
+ // Verify the receiver's finalized signatures against the utxos it provided, once the utxos of the original inputs have been filled up.
+ for(PSBTInput receiverInput : receiverInputs) {
+ try {
+ receiverInput.verifyFinalizedSignatures();
+ } catch(PSBTSignatureException e) {
+ log.warn("Payjoin receiver input failed verification: " + e.getMessage());
+ throw new PayjoinReceiverException("The receiver did not provide valid signatures for one of their inputs");
+ }
+ }
+
// Verify that the payjoin proposal did not introduced mixed inputs' sequence.
if(sequences.size() != 1) {
throw new PayjoinReceiverException("Mixed sequences detected in the proposal");
### src/test/java/com/sparrowwallet/sparrow/payjoin/PayjoinTest.java
@@ -1,11 +1,13 @@
package com.sparrowwallet.sparrow.payjoin;
import com.sparrowwallet.drongo.crypto.ECKey;
+import com.sparrowwallet.drongo.policy.PolicyType;
import com.sparrowwallet.drongo.protocol.Script;
import com.sparrowwallet.drongo.protocol.ScriptType;
import com.sparrowwallet.drongo.protocol.Sha256Hash;
import com.sparrowwallet.drongo.protocol.Transaction;
import com.sparrowwallet.drongo.protocol.TransactionOutput;
+import com.sparrowwallet.drongo.protocol.TransactionSignature;
import com.sparrowwallet.drongo.protocol.TransactionWitness;
import com.sparrowwallet.drongo.psbt.PSBT;
import com.sparrowwallet.drongo.psbt.PSBTInput;
@@ -115,15 +117,79 @@ public void originalWithSilentPaymentOutputIsRejected() throws Exception {
Assertions.assertEquals("Original PSBT for payjoin transaction cannot contain silent payment outputs", e.getMessage());
}
+ @Test
+ public void receiverInputWithValidSignatureIsAccepted() throws Exception {
+ PSBT original = getOriginalPSBT();
+ Payjoin payjoin = getPayjoin(original);
+ PSBT proposal = getProposalPSBT(getPaymentScript(), PAYMENT_VALUE + RECEIVER_UTXO_VALUE, CHANGE_VALUE);
+
+ payjoin.checkProposal(original, proposal, CHANGE_OUTPUT_INDEX, MAX_ADDITIONAL_FEE_CONTRIBUTION, MIN_FEE_RATE, true);
+ Assertions.assertEquals(SENDER_UTXO_VALUE + RECEIVER_UTXO_VALUE - PAYMENT_VALUE - RECEIVER_UTXO_VALUE - CHANGE_VALUE, proposal.getFee());
+ }
+
+ @Test
+ public void receiverInputSignedForDifferentAmountIsRejected() throws Exception {
+ PSBT original = getOriginalPSBT();
+ Payjoin payjoin = getPayjoin(original);
+ long difference = 5000L;
+ PSBT proposal = getProposalPSBT(ScriptType.P2WPKH, getPaymentScript(), PAYMENT_VALUE + RECEIVER_UTXO_VALUE + difference, CHANGE_VALUE, 0, RECEIVER_UTXO_VALUE + difference);
+
+ PayjoinReceiverException e = Assertions.assertThrows(PayjoinReceiverException.class,
+ () -> payjoin.checkProposal(original, proposal, CHANGE_OUTPUT_INDEX, MAX_ADDITIONAL_FEE_CONTRIBUTION, MIN_FEE_RATE, true));
+ Assertions.assertEquals("The receiver did not provide valid signatures for one of their inputs", e.getMessage());
+ }
+
+ @Test
+ public void nonSegwitReceiverInputWithoutNonWitnessUtxoIsRejected() throws Exception {
+ PSBT original = getOriginalPSBT(ScriptType.P2PKH);
+ Payjoin payjoin = getPayjoin(original, ScriptType.P2PKH);
+ PSBT proposal = getProposalPSBT(ScriptType.P2PKH, getPaymentScript(), PAYMENT_VALUE + RECEIVER_UTXO_VALUE, CHANGE_VALUE, 0, RECEIVER_UTXO_VALUE);
+
+ PayjoinReceiverException e = Assertions.assertThrows(PayjoinReceiverException.class,
+ () -> payjoin.checkProposal(original, proposal, CHANGE_OUTPUT_INDEX, MAX_ADDITIONAL_FEE_CONTRIBUTION, MIN_FEE_RATE, true));
+ Assertions.assertEquals("The receiver did not specify non_witness_utxo for one of their non segwit inputs", e.getMessage());
+ }
+
+ @Test
+ public void taprootReceiverInputIsVerifiedWithOriginalUtxosFilledUp() throws Exception {
+ PSBT original = getOriginalPSBT(ScriptType.P2TR);
+ Payjoin payjoin = getPayjoin(original, ScriptType.P2TR);
+
+ //The receiver input comes first and the proposal omits the utxo of the original input, which the taproot sighash of the receiver input commits to
+ Transaction transaction = new Transaction();
+ transaction.setVersion(2);
+ transaction.addInput(RECEIVER_UTXO_HASH, 0, new Script(new byte[0]));
+ transaction.addInput(SENDER_UTXO_HASH, 0, new Script(new byte[0]));
+ transaction.addOutput(PAYMENT_VALUE + RECEIVER_UTXO_VALUE, getPaymentScript());
+ transaction.addOutput(CHANGE_VALUE, getChangeScript());
+
+ PSBT proposal = new PSBT(transaction);
+ proposal.convertVersion(0);
+ PSBTInput senderInput = proposal.getPsbtInputs().get(1);
+ senderInput.setWitnessUtxo(original.getPsbtInputs().get(0).getWitnessUtxo());
+ finalise(proposal.getPsbtInputs().get(0), proposal.getTransaction(), ScriptType.P2TR, RECEIVER_KEY, RECEIVER_UTXO_VALUE, RECEIVER_UTXO_VALUE, 0);
+ senderInput.setWitnessUtxo(null);
+
+ payjoin.checkProposal(original, proposal, CHANGE_OUTPUT_INDEX, MAX_ADDITIONAL_FEE_CONTRIBUTION, MIN_FEE_RATE, true);
+ }
+
private Payjoin getPayjoin(PSBT original) throws Exception {
+ return getPayjoin(original, ScriptType.P2WPKH);
+ }
+
+ private Payjoin getPayjoin(PSBT original, ScriptType scriptType) throws Exception {
Wallet wallet = new Wallet();
- wallet.setScriptType(ScriptType.P2WPKH);
+ wallet.setScriptType(scriptType);
BitcoinURI payjoinURI = new BitcoinURI("bitcoin:" + ScriptType.P2WPKH.getAddress(PAYMENT_KEY.getPubKeyHash()) + "?pj=https://payjoin.example.com/pj");
return new Payjoin(payjoinURI, wallet, original);
}
private PSBT getOriginalPSBT() {
+ return getOriginalPSBT(ScriptType.P2WPKH);
+ }
+
+ private PSBT getOriginalPSBT(ScriptType scriptType) {
Transaction transaction = new Transaction();
transaction.setVersion(2);
transaction.addInput(SENDER_UTXO_HASH, 0, new Script(new byte[0]));
@@ -132,16 +198,20 @@ private PSBT getOriginalPSBT() {
PSBT psbt = new PSBT(transaction);
psbt.convertVersion(0);
- finalise(psbt.getPsbtInputs().get(0), psbt.getTransaction(), SENDER_UTXO_VALUE, getSenderScript(), SENDER_KEY);
+ finalise(psbt.getPsbtInputs().get(0), psbt.getTransaction(), scriptType, SENDER_KEY, SENDER_UTXO_VALUE, SENDER_UTXO_VALUE, 0);
return psbt;
}
private PSBT getProposalPSBT(Script paymentScript, long paymentValue, long changeValue) {
- return getProposalPSBT(paymentScript, paymentValue, changeValue, 71);
+ return getProposalPSBT(paymentScript, paymentValue, changeValue, 0);
}
- private PSBT getProposalPSBT(Script paymentScript, long paymentValue, long changeValue, int receiverSignatureLength) {
+ private PSBT getProposalPSBT(Script paymentScript, long paymentValue, long changeValue, int receiverWitnessPadding) {
+ return getProposalPSBT(ScriptType.P2WPKH, paymentScript, paymentValue, changeValue, receiverWitnessPadding, RECEIVER_UTXO_VALUE);
+ }
+
+ private PSBT getProposalPSBT(ScriptType scriptType, Script paymentScript, long paymentValue, long changeValue, int receiverWitnessPadding, long receiverUtxoValue) {
Transaction transaction = new Transaction();
transaction.setVersion(2);
transaction.addInput(SENDER_UTXO_HASH, 0, new Script(new byte[0]));
@@ -151,23 +221,31 @@ private PSBT getProposalPSBT(Script paymentScript, long paymentValue, long chang
PSBT psbt = new PSBT(transaction);
psbt.convertVersion(0);
- finalise(psbt.getPsbtInputs().get(1), psbt.getTransaction(), RECEIVER_UTXO_VALUE, getReceiverScript(), RECEIVER_KEY, receiverSignatureLength);
+ finalise(psbt.getPsbtInputs().get(1), psbt.getTransaction(), scriptType, RECEIVER_KEY, RECEIVER_UTXO_VALUE, receiverUtxoValue, receiverWitnessPadding);
return psbt;
}
- private void finalise(PSBTInput psbtInput, Transaction transaction, long value, Script script, ECKey key) {
- finalise(psbtInput, transaction, value, script, key, 71);
- }
-
- private void finalise(PSBTInput psbtInput, Transaction transaction, long value, Script script, ECKey key, int signatureLength) {
- psbtInput.setWitnessUtxo(new TransactionOutput(null, value, script));
- psbtInput.setFinalScriptSig(new Script(new byte[0]));
- psbtInput.setFinalScriptWitness(new TransactionWitness(transaction, List.of(new byte[signatureLength], key.getPubKey())));
- }
-
- private Script getSenderScript() {
- return ScriptType.P2WPKH.getOutputScript(SENDER_KEY.getPubKeyHash());
+ private void finalise(PSBTInput psbtInput, Transaction transaction, ScriptType scriptType, ECKey key, long signedValue, long utxoValue, int witnessPadding) {
+ ECKey signingKey = scriptType == ScriptType.P2TR ? key.getTweakedOutputKey() : key;
+ Script script = scriptType == ScriptType.P2TR ? ScriptType.P2TR.getOutputScript(signingKey.getPubKeyXCoord()) : scriptType.getOutputScript(key.getPubKeyHash());
+ psbtInput.setWitnessUtxo(new TransactionOutput(null, signedValue, script));
+ psbtInput.sign(signingKey);
+ TransactionSignature signature = scriptType == ScriptType.P2TR ? psbtInput.getTapKeyPathSignature() : psbtInput.getPartialSignatures().values().iterator().next();
+ psbtInput.setTapKeyPathSignature(null);
+ psbtInput.getPartialSignatures().clear();
+
+ if(scriptType == ScriptType.P2PKH) {
+ psbtInput.setFinalScriptSig(ScriptType.P2PKH.getScriptSig(PolicyType.SINGLE_HD, script, key, signature));
+ } else if(scriptType == ScriptType.P2TR) {
+ psbtInput.setFinalScriptWitness(new TransactionWitness(transaction, List.of(signature.encodeToBitcoin())));
+ } else {
+ List<byte[]> pushes = witnessPadding > 0 ? List.of(signature.encodeToBitcoin(), key.getPubKey(), new byte[witnessPadding]) : List.of(signature.encodeToBitcoin(), key.getPubKey());
+ psbtInput.setFinalScriptSig(new Script(new byte[0]));
+ psbtInput.setFinalScriptWitness(new TransactionWitness(transaction, pushes));
+ }
+
+ psbtInput.setWitnessUtxo(new TransactionOutput(null, utxoValue, script));
}
private Script getChangeScript() {
@@ -181,8 +259,4 @@ private Script getPaymentScript() {
private Script getSubstituteScript() {
return ScriptType.P2WPKH.getOutputScript(SUBSTITUTE_KEY.getPubKeyHash());
}
-
- private Script getReceiverScript() {
- return ScriptType.P2WPKH.getOutputScript(RECEIVER_KEY.getPubKeyHash());
- }
}Why this scored 76/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.