Add zcash_script V5 functions

Co-authored-by: Jack Grigg <jack@z.cash>
This commit is contained in:
Conrado Gouvea 2022-01-20 18:51:26 -03:00 committed by Jack Grigg
parent 24fb0c97df
commit 3be05964d7
2 changed files with 128 additions and 1 deletions

View File

@ -127,6 +127,39 @@ void* zcash_script_new_precomputed_tx(
}
}
void* zcash_script_new_precomputed_tx_v5(
const unsigned char* txTo,
unsigned int txToLen,
const unsigned char* allPrevOutputs,
unsigned int allPrevOutputsLen,
zcash_script_error* err)
{
try {
TxInputStream stream(SER_NETWORK, PROTOCOL_VERSION, txTo, txToLen);
CTransaction tx;
stream >> tx;
if (GetSerializeSize(tx, SER_NETWORK, PROTOCOL_VERSION) != txToLen) {
set_error(err, zcash_script_ERR_TX_SIZE_MISMATCH);
return nullptr;
}
try {
auto preTx = new PrecomputedTransaction(tx, allPrevOutputs, allPrevOutputsLen);
// Deserializing the tx did not error.
set_error(err, zcash_script_ERR_OK);
return preTx;
} catch (const std::exception&) {
// We had some error when parsing allPrevOutputs inside the
// PrecomputedTransactionData constructor.
set_error(err, zcash_script_ERR_ALL_PREV_OUTPUTS_DESERIALIZE);
return nullptr;
}
} catch (const std::exception&) {
set_error(err, zcash_script_ERR_TX_DESERIALIZE); // Error deserializing
return nullptr;
}
}
void zcash_script_free_precomputed_tx(void* pre_preTx)
{
PrecomputedTransaction* preTx = static_cast<PrecomputedTransaction*>(pre_preTx);
@ -196,6 +229,53 @@ int zcash_script_verify(
}
}
int zcash_script_verify_v5(
const unsigned char* txTo,
unsigned int txToLen,
const unsigned char* allPrevOutputs,
unsigned int allPrevOutputsLen,
unsigned int nIn,
unsigned int flags,
uint32_t consensusBranchId,
zcash_script_error* err)
{
try {
TxInputStream stream(SER_NETWORK, PROTOCOL_VERSION, txTo, txToLen);
CTransaction tx;
stream >> tx;
if (nIn >= tx.vin.size())
return set_error(err, zcash_script_ERR_TX_INDEX);
if (GetSerializeSize(tx, SER_NETWORK, PROTOCOL_VERSION) != txToLen)
return set_error(err, zcash_script_ERR_TX_SIZE_MISMATCH);
// TODO: we can swap this second deserialization for an FFI call by
// fetching this through PrecomputedTransactionData. Simplicity for now.
CDataStream sAllPrevOutputs(
reinterpret_cast<const char*>(allPrevOutputs),
reinterpret_cast<const char*>(allPrevOutputs + allPrevOutputsLen),
SER_NETWORK,
PROTOCOL_VERSION);
std::vector<CTxOut> prevOutputs;
sAllPrevOutputs >> prevOutputs;
if (!(tx.IsCoinBase() ? prevOutputs.empty() : tx.vin.size() == prevOutputs.size())) {
return set_error(err, zcash_script_ERR_ALL_PREV_OUTPUTS_SIZE_MISMATCH);
}
// Regardless of the verification result, the tx did not error.
set_error(err, zcash_script_ERR_OK);
PrecomputedTransactionData txdata(tx, allPrevOutputs, allPrevOutputsLen);
return VerifyScript(
tx.vin[nIn].scriptSig,
prevOutputs[nIn].scriptPubKey,
flags,
TransactionSignatureChecker(&tx, txdata, nIn, prevOutputs[nIn].nValue),
consensusBranchId,
NULL);
} catch (const std::exception&) {
return set_error(err, zcash_script_ERR_TX_DESERIALIZE); // Error deserializing
}
}
unsigned int zcash_script_legacy_sigop_count_precomputed(
const void* pre_preTx,
zcash_script_error* err)

View File

@ -33,7 +33,7 @@
extern "C" {
#endif
#define ZCASH_SCRIPT_API_VER 2
#define ZCASH_SCRIPT_API_VER 3
typedef enum zcash_script_error_t
{
@ -43,6 +43,8 @@ typedef enum zcash_script_error_t
zcash_script_ERR_TX_DESERIALIZE,
// Defined since API version 3.
zcash_script_ERR_TX_VERSION,
zcash_script_ERR_ALL_PREV_OUTPUTS_SIZE_MISMATCH,
zcash_script_ERR_ALL_PREV_OUTPUTS_DESERIALIZE,
} zcash_script_error;
/** Script verification flags */
@ -70,6 +72,27 @@ void* zcash_script_new_precomputed_tx(
unsigned int txToLen,
zcash_script_error* err);
/// Deserializes the given transaction and precomputes values to improve
/// script verification performance. Must be used for V5 transactions;
/// may also be used for previous versions.
///
/// allPrevOutputs must point to the encoding of the vector of all outputs
/// from previous transactions that are spent by the inputs of the given transaction.
/// The outputs must be encoded as specified by Bitcoin. The full encoding will thus
/// consist of a CompactSize prefix containing the number of outputs, followed by
/// the concatenated outputs, each of them encoded as (value, CompactSize, pk_script).
///
/// Returns a pointer to the precomputed transaction. Free this with
/// zcash_script_free_precomputed_tx once you are done.
///
/// If not NULL, err will contain an error/success code for the operation.
void* zcash_script_new_precomputed_tx_v5(
const unsigned char* txTo,
unsigned int txToLen,
const unsigned char* allPrevOutputs,
unsigned int allPrevOutputsLen,
zcash_script_error* err);
/// Frees a precomputed transaction previously created with
/// zcash_script_new_precomputed_tx.
void zcash_script_free_precomputed_tx(void* preTx);
@ -108,6 +131,30 @@ EXPORT_SYMBOL int zcash_script_verify(
uint32_t consensusBranchId,
zcash_script_error* err);
/// Returns 1 if the input nIn of the serialized transaction pointed to by
/// txTo correctly spends the matching output in allPrevOutputs under
/// the additional constraints specified by flags. Must be used for V5 transactions;
/// may also be used for previous versions.
///
/// allPrevOutputs must point to the encoding of the vector of all outputs
/// from previous transactions that are spent by the inputs of the given transaction.
/// The outputs must be encoded as specified by Bitcoin. The full encoding will thus
/// consist of a CompactSize prefix containing the number of outputs, followed by
/// the concatenated outputs, each of them encoded as (value, CompactSize, pk_script).
///
/// If not NULL, err will contain an error/success code for the operation.
/// Note that script verification failure is indicated by err being set to
/// zcash_script_ERR_OK and a return value of 0.
EXPORT_SYMBOL int zcash_script_verify_v5(
const unsigned char* txTo,
unsigned int txToLen,
const unsigned char* allPrevOutputs,
unsigned int allPrevOutputsLen,
unsigned int nIn,
unsigned int flags,
uint32_t consensusBranchId,
zcash_script_error* err);
/// Returns the number of transparent signature operations in the
/// transparent inputs and outputs of the precomputed transaction
/// pointed to by preTx.