Skip to content

Commit 940d74b

Browse files
afk11sipa
authored andcommitted
bitcoinconsensus: add method that accepts amount, and return error when verify_script receives VERIFY_WITNESS flag
script_tests: always test bitcoinconsensus_verify_script_with_amount if VERIFY_WITNESS isn't set Rename internal method + make it static trim bitcoinconsensus_ prefix Add SERIALIZE_TRANSACTION_WITNESS flag
1 parent 09261e2 commit 940d74b

File tree

3 files changed

+42
-10
lines changed

3 files changed

+42
-10
lines changed

src/script/bitcoinconsensus.cpp

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,29 +69,49 @@ struct ECCryptoClosure
6969
ECCryptoClosure instance_of_eccryptoclosure;
7070
}
7171

72-
int bitcoinconsensus_verify_script(const unsigned char *scriptPubKey, unsigned int scriptPubKeyLen, uint64_t amount,
72+
static int verify_script(const unsigned char *scriptPubKey, unsigned int scriptPubKeyLen, CAmount amount,
7373
const unsigned char *txTo , unsigned int txToLen,
7474
unsigned int nIn, unsigned int flags, bitcoinconsensus_error* err)
7575
{
7676
try {
77-
TxInputStream stream(SER_NETWORK, PROTOCOL_VERSION, txTo, txToLen);
77+
TxInputStream stream(SER_NETWORK, PROTOCOL_VERSION | SERIALIZE_TRANSACTION_WITNESS, txTo, txToLen);
7878
CTransaction tx;
7979
stream >> tx;
8080
if (nIn >= tx.vin.size())
8181
return set_error(err, bitcoinconsensus_ERR_TX_INDEX);
82-
if (tx.GetSerializeSize(SER_NETWORK, PROTOCOL_VERSION) != txToLen)
82+
if (tx.GetSerializeSize(SER_NETWORK, PROTOCOL_VERSION | SERIALIZE_TRANSACTION_WITNESS) != txToLen)
8383
return set_error(err, bitcoinconsensus_ERR_TX_SIZE_MISMATCH);
8484

85-
// Regardless of the verification result, the tx did not error.
86-
set_error(err, bitcoinconsensus_ERR_OK);
85+
// Regardless of the verification result, the tx did not error.
86+
set_error(err, bitcoinconsensus_ERR_OK);
8787

88-
CAmount am(amount);
89-
return VerifyScript(tx.vin[nIn].scriptSig, CScript(scriptPubKey, scriptPubKey + scriptPubKeyLen), nIn < tx.wit.vtxinwit.size() ? &tx.wit.vtxinwit[nIn].scriptWitness : NULL, flags, TransactionSignatureChecker(&tx, nIn, am), NULL);
88+
return VerifyScript(tx.vin[nIn].scriptSig, CScript(scriptPubKey, scriptPubKey + scriptPubKeyLen), nIn < tx.wit.vtxinwit.size() ? &tx.wit.vtxinwit[nIn].scriptWitness : NULL, flags, TransactionSignatureChecker(&tx, nIn, amount), NULL);
9089
} catch (const std::exception&) {
9190
return set_error(err, bitcoinconsensus_ERR_TX_DESERIALIZE); // Error deserializing
9291
}
9392
}
9493

94+
int bitcoinconsensus_verify_script_with_amount(const unsigned char *scriptPubKey, unsigned int scriptPubKeyLen, uint64_t amount,
95+
const unsigned char *txTo , unsigned int txToLen,
96+
unsigned int nIn, unsigned int flags, bitcoinconsensus_error* err)
97+
{
98+
CAmount am(amount);
99+
return ::verify_script(scriptPubKey, scriptPubKeyLen, am, txTo, txToLen, nIn, flags, err);
100+
}
101+
102+
103+
int bitcoinconsensus_verify_script(const unsigned char *scriptPubKey, unsigned int scriptPubKeyLen,
104+
const unsigned char *txTo , unsigned int txToLen,
105+
unsigned int nIn, unsigned int flags, bitcoinconsensus_error* err)
106+
{
107+
if (flags & bitcoinconsensus_SCRIPT_FLAGS_VERIFY_WITNESS) {
108+
return set_error(err, bitcoinconsensus_ERR_AMOUNT_REQUIRED);
109+
}
110+
111+
CAmount am(0);
112+
return ::verify_script(scriptPubKey, scriptPubKeyLen, am, txTo, txToLen, nIn, flags, err);
113+
}
114+
95115
unsigned int bitcoinconsensus_version()
96116
{
97117
// Just use the API version for now

src/script/bitcoinconsensus.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,15 @@
3333
extern "C" {
3434
#endif
3535

36-
#define BITCOINCONSENSUS_API_VER 0
36+
#define BITCOINCONSENSUS_API_VER 1
3737

3838
typedef enum bitcoinconsensus_error_t
3939
{
4040
bitcoinconsensus_ERR_OK = 0,
4141
bitcoinconsensus_ERR_TX_INDEX,
4242
bitcoinconsensus_ERR_TX_SIZE_MISMATCH,
4343
bitcoinconsensus_ERR_TX_DESERIALIZE,
44+
bitcoinconsensus_ERR_AMOUNT_REQUIRED,
4445
} bitcoinconsensus_error;
4546

4647
/** Script verification flags */
@@ -50,13 +51,19 @@ enum
5051
bitcoinconsensus_SCRIPT_FLAGS_VERIFY_P2SH = (1U << 0), // evaluate P2SH (BIP16) subscripts
5152
bitcoinconsensus_SCRIPT_FLAGS_VERIFY_DERSIG = (1U << 2), // enforce strict DER (BIP66) compliance
5253
bitcoinconsensus_SCRIPT_FLAGS_VERIFY_CHECKLOCKTIMEVERIFY = (1U << 9), // enable CHECKLOCKTIMEVERIFY (BIP65)
54+
bitcoinconsensus_SCRIPT_FLAGS_VERIFY_WITNESS = (1U << 10), // enable WITNESS (BIP141)
55+
5356
};
5457

5558
/// Returns 1 if the input nIn of the serialized transaction pointed to by
5659
/// txTo correctly spends the scriptPubKey pointed to by scriptPubKey under
5760
/// the additional constraints specified by flags.
5861
/// If not NULL, err will contain an error/success code for the operation
59-
EXPORT_SYMBOL int bitcoinconsensus_verify_script(const unsigned char *scriptPubKey, unsigned int scriptPubKeyLen, uint64_t amount,
62+
EXPORT_SYMBOL int bitcoinconsensus_verify_script(const unsigned char *scriptPubKey, unsigned int scriptPubKeyLen,
63+
const unsigned char *txTo , unsigned int txToLen,
64+
unsigned int nIn, unsigned int flags, bitcoinconsensus_error* err);
65+
66+
EXPORT_SYMBOL int bitcoinconsensus_verify_script_with_amount(const unsigned char *scriptPubKey, unsigned int scriptPubKeyLen, uint64_t amount,
6067
const unsigned char *txTo , unsigned int txToLen,
6168
unsigned int nIn, unsigned int flags, bitcoinconsensus_error* err);
6269

src/test/script_tests.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,12 @@ void DoTest(const CScript& scriptPubKey, const CScript& scriptSig, int flags, bo
102102
#if defined(HAVE_CONSENSUS_LIB)
103103
CDataStream stream(SER_NETWORK, PROTOCOL_VERSION);
104104
stream << tx2;
105-
BOOST_CHECK_MESSAGE(bitcoinconsensus_verify_script(begin_ptr(scriptPubKey), scriptPubKey.size(), txCredit.vout[0].nValue, (const unsigned char*)&stream[0], stream.size(), 0, flags, NULL) == expect,message);
105+
if (flags & bitcoinconsensus_SCRIPT_FLAGS_VERIFY_WITNESS) {
106+
BOOST_CHECK_MESSAGE(bitcoinconsensus_verify_script_with_amount(begin_ptr(scriptPubKey), scriptPubKey.size(), txCredit.vout[0].nValue, (const unsigned char*)&stream[0], stream.size(), 0, flags, NULL) == expect,message);
107+
} else {
108+
BOOST_CHECK_MESSAGE(bitcoinconsensus_verify_script_with_amount(begin_ptr(scriptPubKey), scriptPubKey.size(), 0, (const unsigned char*)&stream[0], stream.size(), 0, flags, NULL) == expect,message);
109+
BOOST_CHECK_MESSAGE(bitcoinconsensus_verify_script(begin_ptr(scriptPubKey), scriptPubKey.size(), (const unsigned char*)&stream[0], stream.size(), 0, flags, NULL) == expect,message);
110+
}
106111
#endif
107112
}
108113

0 commit comments

Comments
 (0)