Skip to content

Commit bd19cc7

Browse files
committed
Serialize non-witness utxo as a non-witness tx but always deserialize as witness
Strip out the witnesses when serializing the non-witness utxo. However witness serializations are allowed, so make sure we always deserialize as witness.
1 parent 43811e6 commit bd19cc7

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

src/script/sign.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,8 @@ struct PSBTInput
223223
// If there is a non-witness utxo, then don't add the witness one.
224224
if (non_witness_utxo) {
225225
SerializeToVector(s, PSBT_IN_NON_WITNESS_UTXO);
226-
SerializeToVector(s, non_witness_utxo);
226+
OverrideStream<Stream> os(&s, s.GetType(), s.GetVersion() | SERIALIZE_TRANSACTION_NO_WITNESS);
227+
SerializeToVector(os, non_witness_utxo);
227228
} else if (!witness_utxo.IsNull()) {
228229
SerializeToVector(s, PSBT_IN_WITNESS_UTXO);
229230
SerializeToVector(s, witness_utxo);
@@ -297,13 +298,17 @@ struct PSBTInput
297298
// Do stuff based on type
298299
switch(type) {
299300
case PSBT_IN_NON_WITNESS_UTXO:
301+
{
300302
if (non_witness_utxo) {
301303
throw std::ios_base::failure("Duplicate Key, input non-witness utxo already provided");
302304
} else if (key.size() != 1) {
303305
throw std::ios_base::failure("Non-witness utxo key is more than one byte type");
304306
}
305-
UnserializeFromVector(s, non_witness_utxo);
307+
// Set the stream to unserialize with witness since this is always a valid network transaction
308+
OverrideStream<Stream> os(&s, s.GetType(), s.GetVersion() & ~SERIALIZE_TRANSACTION_NO_WITNESS);
309+
UnserializeFromVector(os, non_witness_utxo);
306310
break;
311+
}
307312
case PSBT_IN_WITNESS_UTXO:
308313
if (!witness_utxo.IsNull()) {
309314
throw std::ios_base::failure("Duplicate Key, input witness utxo already provided");

0 commit comments

Comments
 (0)