Skip to content

Commit fb0db07

Browse files
jrawsthornebrunoerg
andcommitted
lib: add Taproot support to libconsensus
Co-authored-by: Bruno Garcia <[email protected]>
1 parent 73dfa6d commit fb0db07

File tree

3 files changed

+122
-4
lines changed

3 files changed

+122
-4
lines changed

src/script/bitcoinconsensus.cpp

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,34 @@ static bool verify_flags(unsigned int flags)
7272

7373
static int verify_script(const unsigned char *scriptPubKey, unsigned int scriptPubKeyLen, CAmount amount,
7474
const unsigned char *txTo , unsigned int txToLen,
75+
const UTXO *spentOutputs, unsigned int spentOutputsLen,
7576
unsigned int nIn, unsigned int flags, bitcoinconsensus_error* err)
7677
{
7778
if (!verify_flags(flags)) {
7879
return set_error(err, bitcoinconsensus_ERR_INVALID_FLAGS);
7980
}
81+
82+
if (flags & bitcoinconsensus_SCRIPT_FLAGS_VERIFY_TAPROOT && spentOutputs == nullptr) {
83+
return set_error(err, bitcoinconsensus_ERR_SPENT_OUTPUTS_REQUIRED);
84+
}
85+
8086
try {
8187
TxInputStream stream(PROTOCOL_VERSION, txTo, txToLen);
8288
CTransaction tx(deserialize, stream);
89+
90+
std::vector<CTxOut> spent_outputs;
91+
if (spentOutputs != nullptr) {
92+
if (spentOutputsLen != tx.vin.size()) {
93+
return set_error(err, bitcoinconsensus_ERR_SPENT_OUTPUTS_MISMATCH);
94+
}
95+
for (size_t i = 0; i < spentOutputsLen; i++) {
96+
CScript spk = CScript(spentOutputs[i].scriptPubKey, spentOutputs[i].scriptPubKey + spentOutputs[i].scriptPubKeySize);
97+
const CAmount& value = spentOutputs[i].value;
98+
CTxOut tx_out = CTxOut(value, spk);
99+
spent_outputs.push_back(tx_out);
100+
}
101+
}
102+
83103
if (nIn >= tx.vin.size())
84104
return set_error(err, bitcoinconsensus_ERR_TX_INDEX);
85105
if (GetSerializeSize(tx, PROTOCOL_VERSION) != txToLen)
@@ -89,18 +109,34 @@ static int verify_script(const unsigned char *scriptPubKey, unsigned int scriptP
89109
set_error(err, bitcoinconsensus_ERR_OK);
90110

91111
PrecomputedTransactionData txdata(tx);
112+
113+
if (spentOutputs != nullptr && flags & bitcoinconsensus_SCRIPT_FLAGS_VERIFY_TAPROOT) {
114+
txdata.Init(tx, std::move(spent_outputs));
115+
}
116+
92117
return VerifyScript(tx.vin[nIn].scriptSig, CScript(scriptPubKey, scriptPubKey + scriptPubKeyLen), &tx.vin[nIn].scriptWitness, flags, TransactionSignatureChecker(&tx, nIn, amount, txdata, MissingDataBehavior::FAIL), nullptr);
93118
} catch (const std::exception&) {
94119
return set_error(err, bitcoinconsensus_ERR_TX_DESERIALIZE); // Error deserializing
95120
}
96121
}
97122

123+
int bitcoinconsensus_verify_script_with_spent_outputs(const unsigned char *scriptPubKey, unsigned int scriptPubKeyLen, int64_t amount,
124+
const unsigned char *txTo , unsigned int txToLen,
125+
const UTXO *spentOutputs, unsigned int spentOutputsLen,
126+
unsigned int nIn, unsigned int flags, bitcoinconsensus_error* err)
127+
{
128+
CAmount am(amount);
129+
return ::verify_script(scriptPubKey, scriptPubKeyLen, am, txTo, txToLen, spentOutputs, spentOutputsLen, nIn, flags, err);
130+
}
131+
98132
int bitcoinconsensus_verify_script_with_amount(const unsigned char *scriptPubKey, unsigned int scriptPubKeyLen, int64_t amount,
99133
const unsigned char *txTo , unsigned int txToLen,
100134
unsigned int nIn, unsigned int flags, bitcoinconsensus_error* err)
101135
{
102136
CAmount am(amount);
103-
return ::verify_script(scriptPubKey, scriptPubKeyLen, am, txTo, txToLen, nIn, flags, err);
137+
UTXO *spentOutputs = nullptr;
138+
unsigned int spentOutputsLen = 0;
139+
return ::verify_script(scriptPubKey, scriptPubKeyLen, am, txTo, txToLen, spentOutputs, spentOutputsLen, nIn, flags, err);
104140
}
105141

106142

@@ -113,7 +149,9 @@ int bitcoinconsensus_verify_script(const unsigned char *scriptPubKey, unsigned i
113149
}
114150

115151
CAmount am(0);
116-
return ::verify_script(scriptPubKey, scriptPubKeyLen, am, txTo, txToLen, nIn, flags, err);
152+
UTXO *spentOutputs = nullptr;
153+
unsigned int spentOutputsLen = 0;
154+
return ::verify_script(scriptPubKey, scriptPubKeyLen, am, txTo, txToLen, spentOutputs, spentOutputsLen, nIn, flags, err);
117155
}
118156

119157
unsigned int bitcoinconsensus_version()

src/script/bitcoinconsensus.h

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
extern "C" {
3232
#endif
3333

34-
#define BITCOINCONSENSUS_API_VER 1
34+
#define BITCOINCONSENSUS_API_VER 2
3535

3636
typedef enum bitcoinconsensus_error_t
3737
{
@@ -41,6 +41,8 @@ typedef enum bitcoinconsensus_error_t
4141
bitcoinconsensus_ERR_TX_DESERIALIZE,
4242
bitcoinconsensus_ERR_AMOUNT_REQUIRED,
4343
bitcoinconsensus_ERR_INVALID_FLAGS,
44+
bitcoinconsensus_ERR_SPENT_OUTPUTS_REQUIRED,
45+
bitcoinconsensus_ERR_SPENT_OUTPUTS_MISMATCH
4446
} bitcoinconsensus_error;
4547

4648
/** Script verification flags */
@@ -53,11 +55,19 @@ enum
5355
bitcoinconsensus_SCRIPT_FLAGS_VERIFY_CHECKLOCKTIMEVERIFY = (1U << 9), // enable CHECKLOCKTIMEVERIFY (BIP65)
5456
bitcoinconsensus_SCRIPT_FLAGS_VERIFY_CHECKSEQUENCEVERIFY = (1U << 10), // enable CHECKSEQUENCEVERIFY (BIP112)
5557
bitcoinconsensus_SCRIPT_FLAGS_VERIFY_WITNESS = (1U << 11), // enable WITNESS (BIP141)
58+
bitcoinconsensus_SCRIPT_FLAGS_VERIFY_TAPROOT = (1U << 17), // enable TAPROOT (BIPs 341 & 342)
5659
bitcoinconsensus_SCRIPT_FLAGS_VERIFY_ALL = bitcoinconsensus_SCRIPT_FLAGS_VERIFY_P2SH | bitcoinconsensus_SCRIPT_FLAGS_VERIFY_DERSIG |
5760
bitcoinconsensus_SCRIPT_FLAGS_VERIFY_NULLDUMMY | bitcoinconsensus_SCRIPT_FLAGS_VERIFY_CHECKLOCKTIMEVERIFY |
58-
bitcoinconsensus_SCRIPT_FLAGS_VERIFY_CHECKSEQUENCEVERIFY | bitcoinconsensus_SCRIPT_FLAGS_VERIFY_WITNESS
61+
bitcoinconsensus_SCRIPT_FLAGS_VERIFY_CHECKSEQUENCEVERIFY | bitcoinconsensus_SCRIPT_FLAGS_VERIFY_WITNESS |
62+
bitcoinconsensus_SCRIPT_FLAGS_VERIFY_TAPROOT
5963
};
6064

65+
typedef struct {
66+
const unsigned char *scriptPubKey;
67+
unsigned int scriptPubKeySize;
68+
int64_t value;
69+
} UTXO;
70+
6171
/// Returns 1 if the input nIn of the serialized transaction pointed to by
6272
/// txTo correctly spends the scriptPubKey pointed to by scriptPubKey under
6373
/// the additional constraints specified by flags.
@@ -70,6 +80,11 @@ EXPORT_SYMBOL int bitcoinconsensus_verify_script_with_amount(const unsigned char
7080
const unsigned char *txTo , unsigned int txToLen,
7181
unsigned int nIn, unsigned int flags, bitcoinconsensus_error* err);
7282

83+
EXPORT_SYMBOL int bitcoinconsensus_verify_script_with_spent_outputs(const unsigned char *scriptPubKey, unsigned int scriptPubKeyLen, int64_t amount,
84+
const unsigned char *txTo , unsigned int txToLen,
85+
const UTXO *spentOutputs, unsigned int spentOutputsLen,
86+
unsigned int nIn, unsigned int flags, bitcoinconsensus_error* err);
87+
7388
EXPORT_SYMBOL unsigned int bitcoinconsensus_version();
7489

7590
#ifdef __cplusplus

src/test/script_tests.cpp

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1637,6 +1637,37 @@ BOOST_AUTO_TEST_CASE(bitcoinconsensus_verify_script_invalid_flags)
16371637
BOOST_CHECK_EQUAL(err, bitcoinconsensus_ERR_INVALID_FLAGS);
16381638
}
16391639

1640+
/* Test bitcoinconsensus_verify_script returns spent outputs required err */
1641+
BOOST_AUTO_TEST_CASE(bitcoinconsensus_verify_script_spent_outputs_required_err)
1642+
{
1643+
unsigned int libconsensus_flags{bitcoinconsensus_SCRIPT_FLAGS_VERIFY_TAPROOT};
1644+
const int nIn{0};
1645+
1646+
CScript scriptPubKey;
1647+
CScript scriptSig;
1648+
CScriptWitness wit;
1649+
1650+
scriptPubKey << OP_EQUAL;
1651+
CTransaction creditTx{BuildCreditingTransaction(scriptPubKey, 1)};
1652+
CTransaction spendTx{BuildSpendingTransaction(scriptSig, wit, creditTx)};
1653+
1654+
CDataStream stream(SER_NETWORK, PROTOCOL_VERSION);
1655+
stream << spendTx;
1656+
1657+
bitcoinconsensus_error err;
1658+
int result{bitcoinconsensus_verify_script_with_spent_outputs(scriptPubKey.data(), scriptPubKey.size(), creditTx.vout[0].nValue, UCharCast(stream.data()), stream.size(), nullptr, 0, nIn, libconsensus_flags, &err)};
1659+
BOOST_CHECK_EQUAL(result, 0);
1660+
BOOST_CHECK_EQUAL(err, bitcoinconsensus_ERR_SPENT_OUTPUTS_REQUIRED);
1661+
1662+
result = bitcoinconsensus_verify_script_with_amount(scriptPubKey.data(), scriptPubKey.size(), creditTx.vout[0].nValue, UCharCast(stream.data()), stream.size(), nIn, libconsensus_flags, &err);
1663+
BOOST_CHECK_EQUAL(result, 0);
1664+
BOOST_CHECK_EQUAL(err, bitcoinconsensus_ERR_SPENT_OUTPUTS_REQUIRED);
1665+
1666+
result = bitcoinconsensus_verify_script(scriptPubKey.data(), scriptPubKey.size(), UCharCast(stream.data()), stream.size(), nIn, libconsensus_flags, &err);
1667+
BOOST_CHECK_EQUAL(result, 0);
1668+
BOOST_CHECK_EQUAL(err, bitcoinconsensus_ERR_SPENT_OUTPUTS_REQUIRED);
1669+
}
1670+
16401671
#endif // defined(HAVE_CONSENSUS_LIB)
16411672

16421673
static std::vector<unsigned int> AllConsensusFlags()
@@ -1685,12 +1716,29 @@ static void AssetTest(const UniValue& test)
16851716
PrecomputedTransactionData txdata;
16861717
txdata.Init(tx, std::vector<CTxOut>(prevouts));
16871718
CachingTransactionSignatureChecker txcheck(&tx, idx, prevouts[idx].nValue, true, txdata);
1719+
1720+
#if defined(HAVE_CONSENSUS_LIB)
1721+
CDataStream stream(SER_NETWORK, PROTOCOL_VERSION);
1722+
stream << tx;
1723+
std::vector<UTXO> utxos;
1724+
utxos.resize(prevouts.size());
1725+
for (size_t i = 0; i < prevouts.size(); i++) {
1726+
utxos[i].scriptPubKey = prevouts[i].scriptPubKey.data();
1727+
utxos[i].scriptPubKeySize = prevouts[i].scriptPubKey.size();
1728+
utxos[i].value = prevouts[i].nValue;
1729+
}
1730+
#endif
1731+
16881732
for (const auto flags : ALL_CONSENSUS_FLAGS) {
16891733
// "final": true tests are valid for all flags. Others are only valid with flags that are
16901734
// a subset of test_flags.
16911735
if (fin || ((flags & test_flags) == flags)) {
16921736
bool ret = VerifyScript(tx.vin[idx].scriptSig, prevouts[idx].scriptPubKey, &tx.vin[idx].scriptWitness, flags, txcheck, nullptr);
16931737
BOOST_CHECK(ret);
1738+
#if defined(HAVE_CONSENSUS_LIB)
1739+
int lib_ret = bitcoinconsensus_verify_script_with_spent_outputs(prevouts[idx].scriptPubKey.data(), prevouts[idx].scriptPubKey.size(), prevouts[idx].nValue, UCharCast(stream.data()), stream.size(), utxos.data(), utxos.size(), idx, flags, nullptr);
1740+
BOOST_CHECK(lib_ret == 1);
1741+
#endif
16941742
}
16951743
}
16961744
}
@@ -1702,11 +1750,28 @@ static void AssetTest(const UniValue& test)
17021750
PrecomputedTransactionData txdata;
17031751
txdata.Init(tx, std::vector<CTxOut>(prevouts));
17041752
CachingTransactionSignatureChecker txcheck(&tx, idx, prevouts[idx].nValue, true, txdata);
1753+
1754+
#if defined(HAVE_CONSENSUS_LIB)
1755+
CDataStream stream(SER_NETWORK, PROTOCOL_VERSION);
1756+
stream << tx;
1757+
std::vector<UTXO> utxos;
1758+
utxos.resize(prevouts.size());
1759+
for (size_t i = 0; i < prevouts.size(); i++) {
1760+
utxos[i].scriptPubKey = prevouts[i].scriptPubKey.data();
1761+
utxos[i].scriptPubKeySize = prevouts[i].scriptPubKey.size();
1762+
utxos[i].value = prevouts[i].nValue;
1763+
}
1764+
#endif
1765+
17051766
for (const auto flags : ALL_CONSENSUS_FLAGS) {
17061767
// If a test is supposed to fail with test_flags, it should also fail with any superset thereof.
17071768
if ((flags & test_flags) == test_flags) {
17081769
bool ret = VerifyScript(tx.vin[idx].scriptSig, prevouts[idx].scriptPubKey, &tx.vin[idx].scriptWitness, flags, txcheck, nullptr);
17091770
BOOST_CHECK(!ret);
1771+
#if defined(HAVE_CONSENSUS_LIB)
1772+
int lib_ret = bitcoinconsensus_verify_script_with_spent_outputs(prevouts[idx].scriptPubKey.data(), prevouts[idx].scriptPubKey.size(), prevouts[idx].nValue, UCharCast(stream.data()), stream.size(), utxos.data(), utxos.size(), idx, flags, nullptr);
1773+
BOOST_CHECK(lib_ret == 0);
1774+
#endif
17101775
}
17111776
}
17121777
}

0 commit comments

Comments
 (0)