add zcash_script_transparent_output_address() to get address from output

This commit is contained in:
Conrado Gouvea 2022-02-23 17:02:31 -03:00
parent 1c38c9833c
commit 040b8c3739
2 changed files with 123 additions and 1 deletions

View File

@ -232,6 +232,94 @@ unsigned int zcash_script_legacy_sigop_count(
}
}
zcash_script_uint160 zcash_script_transparent_output_address_precomputed(
const void* pre_preTx,
unsigned int nOut,
zcash_script_type* type,
zcash_script_error* err)
{
const PrecomputedTransaction* preTx = static_cast<const PrecomputedTransaction*>(pre_preTx);
zcash_script_uint160 r;
memset(&r, 0, sizeof(r));
if (nOut >= preTx->tx.vout.size()) {
set_error(err, zcash_script_ERR_TX_INDEX);
return r;
}
const CTxOut& out = preTx->tx.vout[nOut];
CScript::ScriptType scriptType = out.scriptPubKey.GetType();
switch (scriptType) {
case CScript::ScriptType::P2PKH:
if (type) {
*type = zcash_script_TYPE_P2PKH;
}
break;
case CScript::ScriptType::P2SH:
if (type) {
*type = zcash_script_TYPE_P2SH;
}
break;
default:
set_error(err, zcash_script_ERR_TX_INVALID_SCRIPT);
return r;
}
const uint160 addr = out.scriptPubKey.AddressHash();
assert(sizeof(r.value) == addr.size());
memcpy(r.value, addr.begin(), addr.size());
set_error(err, zcash_script_ERR_OK);
return r;
}
zcash_script_uint160 zcash_script_transparent_output_address(
const unsigned char *txTo,
unsigned int txToLen,
unsigned int nOut,
zcash_script_type* type,
zcash_script_error* err)
{
TxInputStream stream(SER_NETWORK, PROTOCOL_VERSION, txTo, txToLen);
CTransaction tx;
zcash_script_uint160 r;
memset(&r, 0, sizeof(r));
stream >> tx;
if (nOut >= tx.vout.size()) {
set_error(err, zcash_script_ERR_TX_INDEX);
return r;
}
if (GetSerializeSize(tx, SER_NETWORK, PROTOCOL_VERSION) != txToLen) {
set_error(err, zcash_script_ERR_TX_SIZE_MISMATCH);
return r;
}
const CTxOut& out = tx.vout[nOut];
CScript::ScriptType scriptType = out.scriptPubKey.GetType();
switch (scriptType) {
case CScript::ScriptType::P2PKH:
if (type) {
*type = zcash_script_TYPE_P2PKH;
}
break;
case CScript::ScriptType::P2SH:
if (type) {
*type = zcash_script_TYPE_P2SH;
}
break;
default:
set_error(err, zcash_script_ERR_TX_INVALID_SCRIPT);
return r;
}
const uint160 addr = out.scriptPubKey.AddressHash();
assert(sizeof(r.value) == addr.size());
memcpy(r.value, addr.begin(), addr.size());
set_error(err, zcash_script_ERR_OK);
return r;
}
unsigned int zcash_script_version()
{
// Just use the API version for now

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,7 @@ typedef enum zcash_script_error_t
zcash_script_ERR_TX_DESERIALIZE,
// Defined since API version 3.
zcash_script_ERR_TX_VERSION,
zcash_script_ERR_TX_INVALID_SCRIPT,
} zcash_script_error;
/** Script verification flags */
@ -53,6 +54,19 @@ enum
zcash_script_SCRIPT_FLAGS_VERIFY_CHECKLOCKTIMEVERIFY = (1U << 9), // enable CHECKLOCKTIMEVERIFY (BIP65)
};
/** Address type enumeration */
typedef enum zcash_script_type_t
{
zcash_script_TYPE_P2PKH = 1,
zcash_script_TYPE_P2SH = 2,
} zcash_script_type;
/// A Zcash transparent address encoded as 20 bytes.
typedef struct zcash_script_uint160_t
{
unsigned char value[20];
} zcash_script_uint160;
/// Deserializes the given transaction and precomputes values to improve
/// script verification performance.
///
@ -129,6 +143,26 @@ EXPORT_SYMBOL unsigned int zcash_script_legacy_sigop_count(
unsigned int txToLen,
zcash_script_error* err);
/// Return the destination address for transparent output nOut of the precomputed transaction
/// pointed to by preTx.
/// If not NULL, type will contain the address type.
/// If not NULL, err will contain an error/success code for the operation.
EXPORT_SYMBOL zcash_script_uint160 zcash_script_transparent_output_address_precomputed(
const void* pre_preTx,
unsigned int nOut,
zcash_script_type* type,
zcash_script_error* err);
/// Return the destination address for transparent output nOut of the transaction txTo.
/// If not NULL, type will contain the address type.
/// If not NULL, err will contain an error/success code for the operation.
EXPORT_SYMBOL zcash_script_uint160 zcash_script_transparent_output_address(
const unsigned char *txTo,
unsigned int txToLen,
unsigned int nOut,
zcash_script_type* type,
zcash_script_error* err);
/// Returns the current version of the zcash_script library.
EXPORT_SYMBOL unsigned int zcash_script_version();