Skip to content

Commit 458a345

Browse files
committed
Add support for SIGHASH_DEFAULT in RPCs, and make it default
For non-Taproot signatures, this is interpreted as SIGHASH_ALL.
1 parent c0f0c8e commit 458a345

File tree

6 files changed

+18
-10
lines changed

6 files changed

+18
-10
lines changed

src/bitcoin-tx.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -506,11 +506,12 @@ static void MutateTxDelOutput(CMutableTransaction& tx, const std::string& strOut
506506
tx.vout.erase(tx.vout.begin() + outIdx);
507507
}
508508

509-
static const unsigned int N_SIGHASH_OPTS = 6;
509+
static const unsigned int N_SIGHASH_OPTS = 7;
510510
static const struct {
511511
const char *flagStr;
512512
int flags;
513513
} sighashOptions[N_SIGHASH_OPTS] = {
514+
{"DEFAULT", SIGHASH_DEFAULT},
514515
{"ALL", SIGHASH_ALL},
515516
{"NONE", SIGHASH_NONE},
516517
{"SINGLE", SIGHASH_SINGLE},

src/core_read.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,7 @@ int ParseSighashString(const UniValue& sighash)
260260
int hash_type = SIGHASH_ALL;
261261
if (!sighash.isNull()) {
262262
static std::map<std::string, int> map_sighash_values = {
263+
{std::string("DEFAULT"), int(SIGHASH_DEFAULT)},
263264
{std::string("ALL"), int(SIGHASH_ALL)},
264265
{std::string("ALL|ANYONECANPAY"), int(SIGHASH_ALL|SIGHASH_ANYONECANPAY)},
265266
{std::string("NONE"), int(SIGHASH_NONE)},

src/rpc/rawtransaction.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -753,7 +753,8 @@ static RPCHelpMan signrawtransactionwithkey()
753753
},
754754
},
755755
},
756-
{"sighashtype", RPCArg::Type::STR, RPCArg::Default{"ALL"}, "The signature hash type. Must be one of:\n"
756+
{"sighashtype", RPCArg::Type::STR, RPCArg::Default{"DEFAULT"}, "The signature hash type. Must be one of:\n"
757+
" \"DEFAULT\"\n"
757758
" \"ALL\"\n"
758759
" \"NONE\"\n"
759760
" \"SINGLE\"\n"

src/script/sign.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,13 @@ bool MutableTransactionSignatureCreator::CreateSig(const SigningProvider& provid
4444
// Signing without known amount does not work in witness scripts.
4545
if (sigversion == SigVersion::WITNESS_V0 && !MoneyRange(amount)) return false;
4646

47-
uint256 hash = SignatureHash(scriptCode, *txTo, nIn, nHashType, amount, sigversion, m_txdata);
47+
// BASE/WITNESS_V0 signatures don't support explicit SIGHASH_DEFAULT, use SIGHASH_ALL instead.
48+
const int hashtype = nHashType == SIGHASH_DEFAULT ? SIGHASH_ALL : nHashType;
49+
50+
uint256 hash = SignatureHash(scriptCode, *txTo, nIn, hashtype, amount, sigversion, m_txdata);
4851
if (!key.Sign(hash, vchSig))
4952
return false;
50-
vchSig.push_back((unsigned char)nHashType);
53+
vchSig.push_back((unsigned char)hashtype);
5154
return true;
5255
}
5356

src/wallet/rpcwallet.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3320,7 +3320,8 @@ RPCHelpMan signrawtransactionwithwallet()
33203320
},
33213321
},
33223322
},
3323-
{"sighashtype", RPCArg::Type::STR, RPCArg::Default{"ALL"}, "The signature hash type. Must be one of\n"
3323+
{"sighashtype", RPCArg::Type::STR, RPCArg::Default{"DEFAULT"}, "The signature hash type. Must be one of\n"
3324+
" \"DEFAULT\"\n"
33243325
" \"ALL\"\n"
33253326
" \"NONE\"\n"
33263327
" \"SINGLE\"\n"
@@ -3542,7 +3543,7 @@ static RPCHelpMan bumpfee_helper(std::string method_name)
35423543
} else {
35433544
PartiallySignedTransaction psbtx(mtx);
35443545
bool complete = false;
3545-
const TransactionError err = pwallet->FillPSBT(psbtx, complete, SIGHASH_ALL, false /* sign */, true /* bip32derivs */);
3546+
const TransactionError err = pwallet->FillPSBT(psbtx, complete, SIGHASH_DEFAULT, false /* sign */, true /* bip32derivs */);
35463547
CHECK_NONFATAL(err == TransactionError::OK);
35473548
CHECK_NONFATAL(!complete);
35483549
CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION);
@@ -4175,8 +4176,8 @@ static RPCHelpMan send()
41754176
// First fill transaction with our data without signing,
41764177
// so external signers are not asked sign more than once.
41774178
bool complete;
4178-
pwallet->FillPSBT(psbtx, complete, SIGHASH_ALL, false, true);
4179-
const TransactionError err = pwallet->FillPSBT(psbtx, complete, SIGHASH_ALL, true, false);
4179+
pwallet->FillPSBT(psbtx, complete, SIGHASH_DEFAULT, false, true);
4180+
const TransactionError err = pwallet->FillPSBT(psbtx, complete, SIGHASH_DEFAULT, true, false);
41804181
if (err != TransactionError::OK) {
41814182
throw JSONRPCTransactionError(err);
41824183
}
@@ -4291,7 +4292,8 @@ static RPCHelpMan walletprocesspsbt()
42914292
{
42924293
{"psbt", RPCArg::Type::STR, RPCArg::Optional::NO, "The transaction base64 string"},
42934294
{"sign", RPCArg::Type::BOOL, RPCArg::Default{true}, "Also sign the transaction when updating"},
4294-
{"sighashtype", RPCArg::Type::STR, RPCArg::Default{"ALL"}, "The signature hash type to sign with if not specified by the PSBT. Must be one of\n"
4295+
{"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"
4296+
" \"DEFAULT\"\n"
42954297
" \"ALL\"\n"
42964298
" \"NONE\"\n"
42974299
" \"SINGLE\"\n"

src/wallet/wallet.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1807,7 +1807,7 @@ bool CWallet::SignTransaction(CMutableTransaction& tx) const
18071807
coins[input.prevout] = Coin(wtx.tx->vout[input.prevout.n], wtx.m_confirm.block_height, wtx.IsCoinBase());
18081808
}
18091809
std::map<int, std::string> input_errors;
1810-
return SignTransaction(tx, coins, SIGHASH_ALL, input_errors);
1810+
return SignTransaction(tx, coins, SIGHASH_DEFAULT, input_errors);
18111811
}
18121812

18131813
bool CWallet::SignTransaction(CMutableTransaction& tx, const std::map<COutPoint, Coin>& coins, int sighash, std::map<int, std::string>& input_errors) const

0 commit comments

Comments
 (0)