Skip to content

Commit 011d6e4

Browse files
author
MarcoFalke
committed
Merge bitcoin/bitcoin#22514: psbt: Actually use SIGHASH_DEFAULT for PSBT signing
c0405ee rpc: Document that DEFAULT is for Taproot, ALL for everything else (Andrew Chow) d399266 psbt: Actually use SIGHASH_DEFAULT (Andrew Chow) eb9a1a2 psbt: Make sighash_type std::optional<int> (Andrew Chow) Pull request description: Make the behavior align with the help text by actually using SIGHASH_DEFAULT as the default sighash for signing PSBTs. ACKs for top commit: Sjors: re-utACK c0405ee Tree-SHA512: 5199fb41de416b2f10ac451f824e7c94b428ba11fdb9e50f0027c692e959ce5813a340c34a4e52d7aa128e12008303d80939a693eff36a869720e45442119828
2 parents 9f7661c + c0405ee commit 011d6e4

File tree

8 files changed

+19
-17
lines changed

8 files changed

+19
-17
lines changed

src/core_read.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ std::vector<unsigned char> ParseHexUV(const UniValue& v, const std::string& strN
248248

249249
int ParseSighashString(const UniValue& sighash)
250250
{
251-
int hash_type = SIGHASH_ALL;
251+
int hash_type = SIGHASH_DEFAULT;
252252
if (!sighash.isNull()) {
253253
static std::map<std::string, int> map_sighash_values = {
254254
{std::string("DEFAULT"), int(SIGHASH_DEFAULT)},

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: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -785,7 +785,7 @@ static RPCHelpMan signrawtransactionwithkey()
785785
},
786786
},
787787
},
788-
{"sighashtype", RPCArg::Type::STR, RPCArg::Default{"DEFAULT"}, "The signature hash type. Must be one of:\n"
788+
{"sighashtype", RPCArg::Type::STR, RPCArg::Default{"DEFAULT for Taproot, ALL otherwise"}, "The signature hash type. Must be one of:\n"
789789
" \"DEFAULT\"\n"
790790
" \"ALL\"\n"
791791
" \"NONE\"\n"
@@ -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/rpc/spend.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -709,7 +709,7 @@ RPCHelpMan signrawtransactionwithwallet()
709709
},
710710
},
711711
},
712-
{"sighashtype", RPCArg::Type::STR, RPCArg::Default{"DEFAULT"}, "The signature hash type. Must be one of\n"
712+
{"sighashtype", RPCArg::Type::STR, RPCArg::Default{"DEFAULT for Taproot, ALL otherwise"}, "The signature hash type. Must be one of\n"
713713
" \"DEFAULT\"\n"
714714
" \"ALL\"\n"
715715
" \"NONE\"\n"
@@ -1167,7 +1167,7 @@ RPCHelpMan walletprocesspsbt()
11671167
{
11681168
{"psbt", RPCArg::Type::STR, RPCArg::Optional::NO, "The transaction base64 string"},
11691169
{"sign", RPCArg::Type::BOOL, RPCArg::Default{true}, "Also sign the transaction when updating (requires wallet to be unlocked)"},
1170-
{"sighashtype", RPCArg::Type::STR, RPCArg::Default{"DEFAULT"}, "The signature hash type to sign with if not specified by the PSBT. Must be one of\n"
1170+
{"sighashtype", RPCArg::Type::STR, RPCArg::Default{"DEFAULT for Taproot, ALL otherwise"}, "The signature hash type to sign with if not specified by the PSBT. Must be one of\n"
11711171
" \"DEFAULT\"\n"
11721172
" \"ALL\"\n"
11731173
" \"NONE\"\n"

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

src/wallet/scriptpubkeyman.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ class ScriptPubKeyMan
236236
/** Sign a message with the given script */
237237
virtual SigningResult SignMessage(const std::string& message, const PKHash& pkhash, std::string& str_sig) const { return SigningResult::SIGNING_FAILED; };
238238
/** Adds script and derivation path information to a PSBT, and optionally signs it. */
239-
virtual TransactionError FillPSBT(PartiallySignedTransaction& psbt, const PrecomputedTransactionData& txdata, int sighash_type = 1 /* SIGHASH_ALL */, bool sign = true, bool bip32derivs = false, int* n_signed = nullptr, bool finalize = true) const { return TransactionError::INVALID_PSBT; }
239+
virtual TransactionError FillPSBT(PartiallySignedTransaction& psbt, const PrecomputedTransactionData& txdata, int sighash_type = SIGHASH_DEFAULT, bool sign = true, bool bip32derivs = false, int* n_signed = nullptr, bool finalize = true) const { return TransactionError::INVALID_PSBT; }
240240

241241
virtual uint256 GetID() const { return uint256(); }
242242

@@ -400,7 +400,7 @@ class LegacyScriptPubKeyMan : public ScriptPubKeyMan, public FillableSigningProv
400400

401401
bool SignTransaction(CMutableTransaction& tx, const std::map<COutPoint, Coin>& coins, int sighash, std::map<int, bilingual_str>& input_errors) const override;
402402
SigningResult SignMessage(const std::string& message, const PKHash& pkhash, std::string& str_sig) const override;
403-
TransactionError FillPSBT(PartiallySignedTransaction& psbt, const PrecomputedTransactionData& txdata, int sighash_type = 1 /* SIGHASH_ALL */, bool sign = true, bool bip32derivs = false, int* n_signed = nullptr, bool finalize = true) const override;
403+
TransactionError FillPSBT(PartiallySignedTransaction& psbt, const PrecomputedTransactionData& txdata, int sighash_type = SIGHASH_DEFAULT, bool sign = true, bool bip32derivs = false, int* n_signed = nullptr, bool finalize = true) const override;
404404

405405
uint256 GetID() const override;
406406

@@ -609,7 +609,7 @@ class DescriptorScriptPubKeyMan : public ScriptPubKeyMan
609609

610610
bool SignTransaction(CMutableTransaction& tx, const std::map<COutPoint, Coin>& coins, int sighash, std::map<int, bilingual_str>& input_errors) const override;
611611
SigningResult SignMessage(const std::string& message, const PKHash& pkhash, std::string& str_sig) const override;
612-
TransactionError FillPSBT(PartiallySignedTransaction& psbt, const PrecomputedTransactionData& txdata, int sighash_type = 1 /* SIGHASH_ALL */, bool sign = true, bool bip32derivs = false, int* n_signed = nullptr, bool finalize = true) const override;
612+
TransactionError FillPSBT(PartiallySignedTransaction& psbt, const PrecomputedTransactionData& txdata, int sighash_type = SIGHASH_DEFAULT, bool sign = true, bool bip32derivs = false, int* n_signed = nullptr, bool finalize = true) const override;
613613

614614
uint256 GetID() const override;
615615

src/wallet/wallet.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -561,7 +561,7 @@ class CWallet final : public WalletStorage, public interfaces::Chain::Notificati
561561
*/
562562
TransactionError FillPSBT(PartiallySignedTransaction& psbtx,
563563
bool& complete,
564-
int sighash_type = 1 /* SIGHASH_ALL */,
564+
int sighash_type = SIGHASH_DEFAULT,
565565
bool sign = true,
566566
bool bip32derivs = true,
567567
size_t* n_signed = nullptr,

test/functional/rpc_psbt.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@ def run_test(self):
457457
wrpc = self.nodes[2].get_wallet_rpc("wallet{}".format(i))
458458
for key in signer['privkeys']:
459459
wrpc.importprivkey(key)
460-
signed_tx = wrpc.walletprocesspsbt(signer['psbt'])['psbt']
460+
signed_tx = wrpc.walletprocesspsbt(signer['psbt'], True, "ALL")['psbt']
461461
assert_equal(signed_tx, signer['result'])
462462

463463
# Combiner test

0 commit comments

Comments
 (0)