Skip to content

Instantly share code, notes, and snippets.

@dgpv
Created November 3, 2021 19:10
Show Gist options
  • Save dgpv/7bf8787ecb093e12c862287f4b86c3f4 to your computer and use it in GitHub Desktop.
Save dgpv/7bf8787ecb093e12c862287f4b86c3f4 to your computer and use it in GitHub Desktop.
Ad-hoc patch for libbitcoinconsensus that adds taproot (only for testing)
diff --git a/src/script/bitcoinconsensus.cpp b/src/script/bitcoinconsensus.cpp
index a9aa6a006..e0a79058e 100644
--- a/src/script/bitcoinconsensus.cpp
+++ b/src/script/bitcoinconsensus.cpp
@@ -9,6 +9,7 @@
#include <pubkey.h>
#include <script/interpreter.h>
#include <version.h>
+#include <streams.h>
namespace {
@@ -75,6 +76,7 @@ static bool verify_flags(unsigned int flags)
static int verify_script(const unsigned char *scriptPubKey, unsigned int scriptPubKeyLen, CAmount amount,
const unsigned char *txTo , unsigned int txToLen,
+ const unsigned char *spentOutputs , unsigned int spentOutputsLen,
unsigned int nIn, unsigned int flags, bitcoinconsensus_error* err)
{
if (!verify_flags(flags)) {
@@ -88,22 +90,49 @@ static int verify_script(const unsigned char *scriptPubKey, unsigned int scriptP
if (GetSerializeSize(tx, PROTOCOL_VERSION) != txToLen)
return set_error(err, bitcoinconsensus_ERR_TX_SIZE_MISMATCH);
+ PrecomputedTransactionData txdata;
+
+ if (spentOutputsLen == 0) {
+ txdata.Init(tx, {});
+ }
+ else {
+ int numSpentOutputs = tx.vin.size();
+ std::vector<CTxOut> spent_outputs(numSpentOutputs);
+ std::vector<unsigned char> outs_data(spentOutputs, spentOutputs+spentOutputsLen);
+ CDataStream ss_data(MakeUCharSpan(outs_data), SER_NETWORK, PROTOCOL_VERSION);
+ //TODO catch exception if data is short for the number of outputs provided
+ for(int i = 0; i < numSpentOutputs; i++) {
+ spent_outputs[i].Unserialize(ss_data);
+ }
+ //TODO check that the number of outputs match the length (that all data was consumed)
+
+ txdata.Init(tx, std::move(spent_outputs));
+ }
+
// Regardless of the verification result, the tx did not error.
set_error(err, bitcoinconsensus_ERR_OK);
- PrecomputedTransactionData txdata(tx);
return VerifyScript(tx.vin[nIn].scriptSig, CScript(scriptPubKey, scriptPubKey + scriptPubKeyLen), &tx.vin[nIn].scriptWitness, flags, TransactionSignatureChecker(&tx, nIn, amount, txdata, MissingDataBehavior::FAIL), nullptr);
} catch (const std::exception&) {
return set_error(err, bitcoinconsensus_ERR_TX_DESERIALIZE); // Error deserializing
}
}
+int bitcoinconsensus_verify_script_taproot(const unsigned char *scriptPubKey, unsigned int scriptPubKeyLen, int64_t amount,
+ const unsigned char *txTo , unsigned int txToLen,
+ const unsigned char *spentOutputs , unsigned int spentOutputsLen,
+ unsigned int nIn, unsigned int flags, bitcoinconsensus_error* err)
+{
+ CAmount am(amount);
+ return ::verify_script(scriptPubKey, scriptPubKeyLen, am, txTo, txToLen, spentOutputs, spentOutputsLen, nIn, flags, err);
+}
+
int bitcoinconsensus_verify_script_with_amount(const unsigned char *scriptPubKey, unsigned int scriptPubKeyLen, int64_t amount,
const unsigned char *txTo , unsigned int txToLen,
unsigned int nIn, unsigned int flags, bitcoinconsensus_error* err)
{
CAmount am(amount);
- return ::verify_script(scriptPubKey, scriptPubKeyLen, am, txTo, txToLen, nIn, flags, err);
+ return ::verify_script(scriptPubKey, scriptPubKeyLen, am, txTo, txToLen, 0, 0, nIn, flags, err);
}
@@ -116,7 +145,7 @@ int bitcoinconsensus_verify_script(const unsigned char *scriptPubKey, unsigned i
}
CAmount am(0);
- return ::verify_script(scriptPubKey, scriptPubKeyLen, am, txTo, txToLen, nIn, flags, err);
+ return ::verify_script(scriptPubKey, scriptPubKeyLen, am, txTo, txToLen, 0, 0, nIn, flags, err);
}
unsigned int bitcoinconsensus_version()
diff --git a/src/script/bitcoinconsensus.h b/src/script/bitcoinconsensus.h
index b6939127e..9f9c6f101 100644
--- a/src/script/bitcoinconsensus.h
+++ b/src/script/bitcoinconsensus.h
@@ -53,9 +53,10 @@ enum
bitcoinconsensus_SCRIPT_FLAGS_VERIFY_CHECKLOCKTIMEVERIFY = (1U << 9), // enable CHECKLOCKTIMEVERIFY (BIP65)
bitcoinconsensus_SCRIPT_FLAGS_VERIFY_CHECKSEQUENCEVERIFY = (1U << 10), // enable CHECKSEQUENCEVERIFY (BIP112)
bitcoinconsensus_SCRIPT_FLAGS_VERIFY_WITNESS = (1U << 11), // enable WITNESS (BIP141)
+ bitcoinconsensus_SCRIPT_VERIFY_TAPROOT = (1U << 17),
bitcoinconsensus_SCRIPT_FLAGS_VERIFY_ALL = bitcoinconsensus_SCRIPT_FLAGS_VERIFY_P2SH | bitcoinconsensus_SCRIPT_FLAGS_VERIFY_DERSIG |
bitcoinconsensus_SCRIPT_FLAGS_VERIFY_NULLDUMMY | bitcoinconsensus_SCRIPT_FLAGS_VERIFY_CHECKLOCKTIMEVERIFY |
- bitcoinconsensus_SCRIPT_FLAGS_VERIFY_CHECKSEQUENCEVERIFY | bitcoinconsensus_SCRIPT_FLAGS_VERIFY_WITNESS
+ bitcoinconsensus_SCRIPT_FLAGS_VERIFY_CHECKSEQUENCEVERIFY | bitcoinconsensus_SCRIPT_FLAGS_VERIFY_WITNESS | bitcoinconsensus_SCRIPT_VERIFY_TAPROOT
};
/// Returns 1 if the input nIn of the serialized transaction pointed to by
@@ -70,6 +71,11 @@ EXPORT_SYMBOL int bitcoinconsensus_verify_script_with_amount(const unsigned char
const unsigned char *txTo , unsigned int txToLen,
unsigned int nIn, unsigned int flags, bitcoinconsensus_error* err);
+EXPORT_SYMBOL int bitcoinconsensus_verify_script_taproot(const unsigned char *scriptPubKey, unsigned int scriptPubKeyLen, int64_t amount,
+ const unsigned char *txTo , unsigned int txToLen,
+ const unsigned char *spentOutputs , unsigned int spentOutputsLen,
+ unsigned int nIn, unsigned int flags, bitcoinconsensus_error* err);
+
EXPORT_SYMBOL unsigned int bitcoinconsensus_version();
#ifdef __cplusplus
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment