Skip to content

Commit eb9a1a2

Browse files
committed
psbt: Make sighash_type std::optional<int>
It is better to ues an optional to determine whether the sighash type is set rather than using 0 as a magic number.
1 parent 577bd51 commit eb9a1a2

File tree

3 files changed

+10
-8
lines changed

3 files changed

+10
-8
lines changed

src/psbt.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ struct PSBTInput
5757
std::map<CPubKey, KeyOriginInfo> hd_keypaths;
5858
std::map<CKeyID, SigPair> partial_sigs;
5959
std::map<std::vector<unsigned char>, std::vector<unsigned char>> unknown;
60-
int sighash_type = 0;
60+
std::optional<int> sighash_type;
6161

6262
bool IsNull() const;
6363
void FillSignatureData(SignatureData& sigdata) const;
@@ -86,9 +86,9 @@ struct PSBTInput
8686
}
8787

8888
// Write the sighash type
89-
if (sighash_type > 0) {
89+
if (sighash_type != std::nullopt) {
9090
SerializeToVector(s, PSBT_IN_SIGHASH);
91-
SerializeToVector(s, sighash_type);
91+
SerializeToVector(s, *sighash_type);
9292
}
9393

9494
// Write the redeem script
@@ -201,7 +201,9 @@ struct PSBTInput
201201
} else if (key.size() != 1) {
202202
throw std::ios_base::failure("Sighash type key is more than one byte type");
203203
}
204-
UnserializeFromVector(s, sighash_type);
204+
int sighash;
205+
UnserializeFromVector(s, sighash);
206+
sighash_type = sighash;
205207
break;
206208
case PSBT_IN_REDEEMSCRIPT:
207209
{

src/rpc/rawtransaction.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1255,8 +1255,8 @@ static RPCHelpMan decodepsbt()
12551255
}
12561256

12571257
// Sighash
1258-
if (input.sighash_type > 0) {
1259-
in.pushKV("sighash", SighashToStr((unsigned char)input.sighash_type));
1258+
if (input.sighash_type != std::nullopt) {
1259+
in.pushKV("sighash", SighashToStr((unsigned char)*input.sighash_type));
12601260
}
12611261

12621262
// Redeem script and witness script

src/wallet/scriptpubkeyman.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -633,7 +633,7 @@ TransactionError LegacyScriptPubKeyMan::FillPSBT(PartiallySignedTransaction& psb
633633
}
634634

635635
// Get the Sighash type
636-
if (sign && input.sighash_type > 0 && input.sighash_type != sighash_type) {
636+
if (sign && input.sighash_type != std::nullopt && *input.sighash_type != sighash_type) {
637637
return TransactionError::SIGHASH_MISMATCH;
638638
}
639639

@@ -2114,7 +2114,7 @@ TransactionError DescriptorScriptPubKeyMan::FillPSBT(PartiallySignedTransaction&
21142114
}
21152115

21162116
// Get the Sighash type
2117-
if (sign && input.sighash_type > 0 && input.sighash_type != sighash_type) {
2117+
if (sign && input.sighash_type != std::nullopt && *input.sighash_type != sighash_type) {
21182118
return TransactionError::SIGHASH_MISMATCH;
21192119
}
21202120

0 commit comments

Comments
 (0)