Skip to content

Commit 7092541

Browse files
committed
refactor: use SignOptions for MutableTransactionSignatureCreator
1 parent 7b6efb3 commit 7092541

File tree

11 files changed

+23
-23
lines changed

11 files changed

+23
-23
lines changed

src/bitcoin-tx.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -681,7 +681,7 @@ static void MutateTxSign(CMutableTransaction& tx, const std::string& flagStr)
681681
SignatureData sigdata = DataFromTransaction(mergedTx, i, coin.out);
682682
// Only sign SIGHASH_SINGLE if there's a corresponding output:
683683
if (!fHashSingle || (i < mergedTx.vout.size()))
684-
ProduceSignature(keystore, MutableTransactionSignatureCreator(mergedTx, i, amount, nHashType), prevPubKey, sigdata);
684+
ProduceSignature(keystore, MutableTransactionSignatureCreator(mergedTx, i, amount, {.sighash_type = nHashType}), prevPubKey, sigdata);
685685

686686
if (amount == MAX_MONEY && !sigdata.scriptWitness.IsNull()) {
687687
throw std::runtime_error(strprintf("Missing amount for CTxOut with scriptPubKey=%s", HexStr(prevPubKey)));

src/psbt.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ void UpdatePSBTOutput(const SigningProvider& provider, PartiallySignedTransactio
351351
// Construct a would-be spend of this output, to update sigdata with.
352352
// Note that ProduceSignature is used to fill in metadata (not actual signatures),
353353
// so provider does not need to provide any private keys (it can be a HidingSigningProvider).
354-
MutableTransactionSignatureCreator creator(tx, /*input_idx=*/0, out.nValue, SIGHASH_ALL);
354+
MutableTransactionSignatureCreator creator(tx, /*input_idx=*/0, out.nValue, {.sighash_type = SIGHASH_ALL});
355355
ProduceSignature(provider, creator, out.scriptPubKey, sigdata);
356356

357357
// Put redeem_script, witness_script, key paths, into PSBTOutput.
@@ -457,7 +457,7 @@ PSBTError SignPSBTInput(const SigningProvider& provider, PartiallySignedTransact
457457
if (txdata == nullptr) {
458458
sig_complete = ProduceSignature(provider, DUMMY_SIGNATURE_CREATOR, utxo.scriptPubKey, sigdata);
459459
} else {
460-
MutableTransactionSignatureCreator creator(tx, index, utxo.nValue, txdata, sighash);
460+
MutableTransactionSignatureCreator creator(tx, index, utxo.nValue, txdata, {.sighash_type = sighash});
461461
sig_complete = ProduceSignature(provider, creator, utxo.scriptPubKey, sigdata);
462462
}
463463
// Verify that a witness signature was produced in case one was required.

src/rpc/rawtransaction.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -697,7 +697,7 @@ static RPCHelpMan combinerawtransaction()
697697
sigdata.MergeSignatureData(DataFromTransaction(txv, i, coin.out));
698698
}
699699
}
700-
ProduceSignature(DUMMY_SIGNING_PROVIDER, MutableTransactionSignatureCreator(mergedTx, i, coin.out.nValue, 1), coin.out.scriptPubKey, sigdata);
700+
ProduceSignature(DUMMY_SIGNING_PROVIDER, MutableTransactionSignatureCreator(mergedTx, i, coin.out.nValue, {.sighash_type = SIGHASH_ALL}), coin.out.scriptPubKey, sigdata);
701701

702702
UpdateInput(txin, sigdata);
703703
}

src/script/sign.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@
2020

2121
typedef std::vector<unsigned char> valtype;
2222

23-
MutableTransactionSignatureCreator::MutableTransactionSignatureCreator(const CMutableTransaction& tx, unsigned int input_idx, const CAmount& amount, int hash_type)
24-
: m_txto{tx}, nIn{input_idx}, nHashType{hash_type}, amount{amount}, checker{&m_txto, nIn, amount, MissingDataBehavior::FAIL},
23+
MutableTransactionSignatureCreator::MutableTransactionSignatureCreator(const CMutableTransaction& tx, unsigned int input_idx, const CAmount& amount, const SignOptions options)
24+
: m_txto{tx}, nIn{input_idx}, m_options{options}, amount{amount}, checker{&m_txto, nIn, amount, MissingDataBehavior::FAIL},
2525
m_txdata(nullptr)
2626
{
2727
}
2828

29-
MutableTransactionSignatureCreator::MutableTransactionSignatureCreator(const CMutableTransaction& tx, unsigned int input_idx, const CAmount& amount, const PrecomputedTransactionData* txdata, int hash_type)
30-
: m_txto{tx}, nIn{input_idx}, nHashType{hash_type}, amount{amount},
29+
MutableTransactionSignatureCreator::MutableTransactionSignatureCreator(const CMutableTransaction& tx, unsigned int input_idx, const CAmount& amount, const PrecomputedTransactionData* txdata, const SignOptions options)
30+
: m_txto{tx}, nIn{input_idx}, m_options{options}, amount{amount},
3131
checker{txdata ? MutableTransactionSignatureChecker{&m_txto, nIn, amount, *txdata, MissingDataBehavior::FAIL} :
3232
MutableTransactionSignatureChecker{&m_txto, nIn, amount, MissingDataBehavior::FAIL}},
3333
m_txdata(txdata)
@@ -50,7 +50,7 @@ bool MutableTransactionSignatureCreator::CreateSig(const SigningProvider& provid
5050
if (sigversion == SigVersion::WITNESS_V0 && !MoneyRange(amount)) return false;
5151

5252
// BASE/WITNESS_V0 signatures don't support explicit SIGHASH_DEFAULT, use SIGHASH_ALL instead.
53-
const int hashtype = nHashType == SIGHASH_DEFAULT ? SIGHASH_ALL : nHashType;
53+
const int hashtype = m_options.sighash_type == SIGHASH_DEFAULT ? SIGHASH_ALL : m_options.sighash_type;
5454

5555
uint256 hash = SignatureHash(scriptCode, m_txto, nIn, hashtype, amount, sigversion, m_txdata);
5656
if (!key.Sign(hash, vchSig))
@@ -82,11 +82,11 @@ bool MutableTransactionSignatureCreator::CreateSchnorrSig(const SigningProvider&
8282
execdata.m_tapleaf_hash = *leaf_hash;
8383
}
8484
uint256 hash;
85-
if (!SignatureHashSchnorr(hash, execdata, m_txto, nIn, nHashType, sigversion, *m_txdata, MissingDataBehavior::FAIL)) return false;
85+
if (!SignatureHashSchnorr(hash, execdata, m_txto, nIn, m_options.sighash_type, sigversion, *m_txdata, MissingDataBehavior::FAIL)) return false;
8686
sig.resize(64);
8787
// Use uint256{} as aux_rnd for now.
8888
if (!key.SignSchnorr(hash, sig, merkle_root, {})) return false;
89-
if (nHashType) sig.push_back(nHashType);
89+
if (m_options.sighash_type) sig.push_back(m_options.sighash_type);
9090
return true;
9191
}
9292

@@ -801,7 +801,7 @@ bool SignTransaction(CMutableTransaction& mtx, const SigningProvider* keystore,
801801
SignatureData sigdata = DataFromTransaction(mtx, i, coin->second.out);
802802
// Only sign SIGHASH_SINGLE if there's a corresponding output:
803803
if (!fHashSingle || (i < mtx.vout.size())) {
804-
ProduceSignature(*keystore, MutableTransactionSignatureCreator(mtx, i, amount, &txdata, options.sighash_type), prevPubKey, sigdata);
804+
ProduceSignature(*keystore, MutableTransactionSignatureCreator(mtx, i, amount, &txdata, options), prevPubKey, sigdata);
805805
}
806806

807807
UpdateInput(txin, sigdata);

src/script/sign.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,14 @@ class MutableTransactionSignatureCreator : public BaseSignatureCreator
4444
{
4545
const CMutableTransaction& m_txto;
4646
unsigned int nIn;
47-
int nHashType;
47+
SignOptions m_options;
4848
CAmount amount;
4949
const MutableTransactionSignatureChecker checker;
5050
const PrecomputedTransactionData* m_txdata;
5151

5252
public:
53-
MutableTransactionSignatureCreator(const CMutableTransaction& tx LIFETIMEBOUND, unsigned int input_idx, const CAmount& amount, int hash_type);
54-
MutableTransactionSignatureCreator(const CMutableTransaction& tx LIFETIMEBOUND, unsigned int input_idx, const CAmount& amount, const PrecomputedTransactionData* txdata, int hash_type);
53+
MutableTransactionSignatureCreator(const CMutableTransaction& tx LIFETIMEBOUND, unsigned int input_idx, const CAmount& amount, const SignOptions options);
54+
MutableTransactionSignatureCreator(const CMutableTransaction& tx LIFETIMEBOUND, unsigned int input_idx, const CAmount& amount, const PrecomputedTransactionData* txdata, const SignOptions options);
5555
const BaseSignatureChecker& Checker() const override { return checker; }
5656
bool CreateSig(const SigningProvider& provider, std::vector<unsigned char>& vchSig, const CKeyID& keyid, const CScript& scriptCode, SigVersion sigversion) const override;
5757
bool CreateSchnorrSig(const SigningProvider& provider, std::vector<unsigned char>& sig, const XOnlyPubKey& pubkey, const uint256* leaf_hash, const uint256* merkle_root, SigVersion sigversion) const override;

src/test/descriptor_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ void DoCheck(std::string prv, std::string pub, const std::string& norm_pub, int
373373
std::vector<CTxOut> utxos(1);
374374
PrecomputedTransactionData txdata;
375375
txdata.Init(spend, std::move(utxos), /*force=*/true);
376-
MutableTransactionSignatureCreator creator{spend, 0, CAmount{0}, &txdata, SIGHASH_DEFAULT};
376+
MutableTransactionSignatureCreator creator{spend, 0, CAmount{0}, &txdata, {.sighash_type = SIGHASH_DEFAULT}};
377377
SignatureData sigdata;
378378
// We assume there is no collision between the hashes (eg h1=SHA256(SHA256(x)) and h2=SHA256(x))
379379
sigdata.sha256_preimages = preimages;

src/test/fuzz/script_sign.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ FUZZ_TARGET(script_sign, .init = initialize_script_sign)
116116
auto amount = ConsumeMoney(fuzzed_data_provider);
117117
auto n_hash_type = fuzzed_data_provider.ConsumeIntegral<int>();
118118
(void)SignSignature(provider, from_pub_key, script_tx_to, n_in, amount, n_hash_type, empty);
119-
MutableTransactionSignatureCreator signature_creator{tx_to, n_in, ConsumeMoney(fuzzed_data_provider), fuzzed_data_provider.ConsumeIntegral<int>()};
119+
MutableTransactionSignatureCreator signature_creator{tx_to, n_in, ConsumeMoney(fuzzed_data_provider), {.sighash_type = fuzzed_data_provider.ConsumeIntegral<int>()}};
120120
std::vector<unsigned char> vch_sig;
121121
CKeyID address;
122122
if (fuzzed_data_provider.ConsumeBool()) {

src/test/script_tests.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1157,7 +1157,7 @@ SignatureData CombineSignatures(const CTxOut& txout, const CMutableTransaction&
11571157
SignatureData data;
11581158
data.MergeSignatureData(scriptSig1);
11591159
data.MergeSignatureData(scriptSig2);
1160-
ProduceSignature(DUMMY_SIGNING_PROVIDER, MutableTransactionSignatureCreator(tx, 0, txout.nValue, SIGHASH_DEFAULT), txout.scriptPubKey, data);
1160+
ProduceSignature(DUMMY_SIGNING_PROVIDER, MutableTransactionSignatureCreator(tx, 0, txout.nValue, {.sighash_type = SIGHASH_DEFAULT}), txout.scriptPubKey, data);
11611161
return data;
11621162
}
11631163

@@ -1705,7 +1705,7 @@ BOOST_AUTO_TEST_CASE(bip341_keypath_test_vectors)
17051705
// Sign and verify signature.
17061706
FlatSigningProvider provider;
17071707
provider.keys[key.GetPubKey().GetID()] = key;
1708-
MutableTransactionSignatureCreator creator(tx, txinpos, utxos[txinpos].nValue, &txdata, hashtype);
1708+
MutableTransactionSignatureCreator creator(tx, txinpos, utxos[txinpos].nValue, &txdata, {.sighash_type = hashtype});
17091709
std::vector<unsigned char> signature;
17101710
BOOST_CHECK(creator.CreateSchnorrSig(provider, signature, pubkey, nullptr, &merkle_root, SigVersion::TAPROOT));
17111711
BOOST_CHECK_EQUAL(HexStr(signature), input["expected"]["witness"][0].get_str());

src/test/transaction_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -597,7 +597,7 @@ SignatureData CombineSignatures(const CMutableTransaction& input1, const CMutabl
597597
SignatureData sigdata;
598598
sigdata = DataFromTransaction(input1, 0, tx->vout[0]);
599599
sigdata.MergeSignatureData(DataFromTransaction(input2, 0, tx->vout[0]));
600-
ProduceSignature(DUMMY_SIGNING_PROVIDER, MutableTransactionSignatureCreator(input1, 0, tx->vout[0].nValue, SIGHASH_ALL), tx->vout[0].scriptPubKey, sigdata);
600+
ProduceSignature(DUMMY_SIGNING_PROVIDER, MutableTransactionSignatureCreator(input1, 0, tx->vout[0].nValue, {.sighash_type = SIGHASH_DEFAULT}), tx->vout[0].scriptPubKey, sigdata);
601601
return sigdata;
602602
}
603603

src/test/txvalidationcache_tests.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ BOOST_FIXTURE_TEST_CASE(checkinputs_test, Dersig100Setup)
331331

332332
// Sign
333333
SignatureData sigdata;
334-
BOOST_CHECK(ProduceSignature(keystore, MutableTransactionSignatureCreator(valid_with_witness_tx, 0, 11 * CENT, SIGHASH_ALL), spend_tx.vout[1].scriptPubKey, sigdata));
334+
BOOST_CHECK(ProduceSignature(keystore, MutableTransactionSignatureCreator(valid_with_witness_tx, 0, 11 * CENT, {.sighash_type = SIGHASH_DEFAULT}), spend_tx.vout[1].scriptPubKey, sigdata));
335335
UpdateInput(valid_with_witness_tx.vin[0], sigdata);
336336

337337
// This should be valid under all script flags.
@@ -359,7 +359,7 @@ BOOST_FIXTURE_TEST_CASE(checkinputs_test, Dersig100Setup)
359359
// Sign
360360
for (int i = 0; i < 2; ++i) {
361361
SignatureData sigdata;
362-
BOOST_CHECK(ProduceSignature(keystore, MutableTransactionSignatureCreator(tx, i, 11 * CENT, SIGHASH_ALL), spend_tx.vout[i].scriptPubKey, sigdata));
362+
BOOST_CHECK(ProduceSignature(keystore, MutableTransactionSignatureCreator(tx, i, 11 * CENT, {.sighash_type = SIGHASH_DEFAULT}), spend_tx.vout[i].scriptPubKey, sigdata));
363363
UpdateInput(tx.vin[i], sigdata);
364364
}
365365

0 commit comments

Comments
 (0)