Skip to content

Commit e841fb5

Browse files
committed
Add precomputed txdata support to MutableTransactionSignatureCreator
This provides a means to pass in a PrecomputedTransactionData object to the MutableTransactionSignatureCreator, allowing the prevout data to be passed into the signature hashers. It is also more efficient.
1 parent a91d532 commit e841fb5

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

src/script/sign.cpp

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,19 @@
1414

1515
typedef std::vector<unsigned char> valtype;
1616

17-
MutableTransactionSignatureCreator::MutableTransactionSignatureCreator(const CMutableTransaction* txToIn, unsigned int nInIn, const CAmount& amountIn, int nHashTypeIn) : txTo(txToIn), nIn(nInIn), nHashType(nHashTypeIn), amount(amountIn), checker(txTo, nIn, amountIn, MissingDataBehavior::FAIL) {}
17+
MutableTransactionSignatureCreator::MutableTransactionSignatureCreator(const CMutableTransaction* txToIn, unsigned int nInIn, const CAmount& amountIn, int nHashTypeIn)
18+
: txTo(txToIn), nIn(nInIn), nHashType(nHashTypeIn), amount(amountIn), checker(txTo, nIn, amountIn, MissingDataBehavior::FAIL),
19+
m_txdata(nullptr)
20+
{
21+
}
22+
23+
MutableTransactionSignatureCreator::MutableTransactionSignatureCreator(const CMutableTransaction* txToIn, unsigned int nInIn, const CAmount& amountIn, const PrecomputedTransactionData* txdata, int nHashTypeIn)
24+
: txTo(txToIn), nIn(nInIn), nHashType(nHashTypeIn), amount(amountIn),
25+
checker(txdata ? MutableTransactionSignatureChecker(txTo, nIn, amount, *txdata, MissingDataBehavior::FAIL) :
26+
MutableTransactionSignatureChecker(txTo, nIn, amount, MissingDataBehavior::FAIL)),
27+
m_txdata(txdata)
28+
{
29+
}
1830

1931
bool MutableTransactionSignatureCreator::CreateSig(const SigningProvider& provider, std::vector<unsigned char>& vchSig, const CKeyID& address, const CScript& scriptCode, SigVersion sigversion) const
2032
{
@@ -26,10 +38,10 @@ bool MutableTransactionSignatureCreator::CreateSig(const SigningProvider& provid
2638
if (sigversion == SigVersion::WITNESS_V0 && !key.IsCompressed())
2739
return false;
2840

29-
// Signing for witness scripts needs the amount.
30-
if (sigversion == SigVersion::WITNESS_V0 && amount < 0) return false;
41+
// Signing without known amount does not work in witness scripts.
42+
if (sigversion == SigVersion::WITNESS_V0 && !MoneyRange(amount)) return false;
3143

32-
uint256 hash = SignatureHash(scriptCode, *txTo, nIn, nHashType, amount, sigversion);
44+
uint256 hash = SignatureHash(scriptCode, *txTo, nIn, nHashType, amount, sigversion, m_txdata);
3345
if (!key.Sign(hash, vchSig))
3446
return false;
3547
vchSig.push_back((unsigned char)nHashType);

src/script/sign.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,11 @@ class MutableTransactionSignatureCreator : public BaseSignatureCreator {
4040
int nHashType;
4141
CAmount amount;
4242
const MutableTransactionSignatureChecker checker;
43+
const PrecomputedTransactionData* m_txdata;
4344

4445
public:
4546
MutableTransactionSignatureCreator(const CMutableTransaction* txToIn, unsigned int nInIn, const CAmount& amountIn, int nHashTypeIn = SIGHASH_ALL);
47+
MutableTransactionSignatureCreator(const CMutableTransaction* txToIn, unsigned int nInIn, const CAmount& amountIn, const PrecomputedTransactionData* txdata, int nHashTypeIn = SIGHASH_ALL);
4648
const BaseSignatureChecker& Checker() const override { return checker; }
4749
bool CreateSig(const SigningProvider& provider, std::vector<unsigned char>& vchSig, const CKeyID& keyid, const CScript& scriptCode, SigVersion sigversion) const override;
4850
};

0 commit comments

Comments
 (0)