From 3251bb2452b243c04e949f5038dc5acd969f42c9 Mon Sep 17 00:00:00 2001 From: TejasvOnly Date: Sat, 30 Nov 2024 07:09:41 -0800 Subject: [PATCH] chore: Remove obsolete bitcoin input parser and its test --- apps/btc_family/btc_txn.c | 3 - apps/btc_family/btc_txn_helpers.c | 268 ------------------ apps/btc_family/btc_txn_helpers.h | 63 ----- tests/apps/btc_app/btc_txn_helpers_tests.c | 309 --------------------- tests/unit_test_lists.c | 7 - 5 files changed, 650 deletions(-) diff --git a/apps/btc_family/btc_txn.c b/apps/btc_family/btc_txn.c index ed3fb0f76..9622d1746 100644 --- a/apps/btc_family/btc_txn.c +++ b/apps/btc_family/btc_txn.c @@ -446,9 +446,6 @@ static bool fetch_valid_input(btc_query_t *query) { // verify transaction details and discard the raw-transaction (prev_txn) const btc_prev_txn_chunk_t *prev_txn = &(query->sign_txn.prev_txn_chunk); - btc_verify_input_t verify_input_data; - memzero(&(verify_input_data), sizeof(btc_verify_input_t)); - // req prev txn chunk from host if (!btc_get_query(query, BTC_QUERY_SIGN_TXN_TAG) || !check_which_request(query, BTC_SIGN_TXN_REQUEST_PREV_TXN_CHUNK_TAG)) { diff --git a/apps/btc_family/btc_txn_helpers.c b/apps/btc_family/btc_txn_helpers.c index 772cf2acb..47f793907 100644 --- a/apps/btc_family/btc_txn_helpers.c +++ b/apps/btc_family/btc_txn_helpers.c @@ -65,7 +65,6 @@ #include #include "bignum.h" -#include "btc_helpers.h" #include "btc_script.h" #include "utils.h" @@ -328,277 +327,10 @@ STATIC bool calculate_p2wpkh_digest(const btc_txn_context_t *context, return true; } -static void update_hash(btc_verify_input_t *verify_input_data, - const uint8_t *raw_txn_chunk, - int chunk_index, - int32_t offset) { - hash_case update = DEFAULT; - - if (0 == chunk_index) { - update = FIRST_CHUNK_HASH; - } - switch (update) { - case FIRST_CHUNK_HASH: { - if (verify_input_data->is_segwit) { - sha256_Update(&(verify_input_data->sha_256_ctx), raw_txn_chunk, 4); - // skip marker and flag - sha256_Update( - &(verify_input_data->sha_256_ctx), raw_txn_chunk + 6, offset - 6); - } else { - sha256_Update(&(verify_input_data->sha_256_ctx), raw_txn_chunk, offset); - } - return; - break; - } - - default: { - sha256_Update(&(verify_input_data->sha_256_ctx), raw_txn_chunk, offset); - break; - } - } -} - -static void update_locktime(btc_verify_input_t *verify_input_data, - const uint8_t *raw_txn_chunk, - int chunk_index) { - if (verify_input_data->is_locktime_split) { - // last second chunk - if (chunk_index + 2 == verify_input_data->chunk_total) { - memcpy( - verify_input_data->locktime, - raw_txn_chunk + (CHUNK_SIZE - 4 - verify_input_data->size_last_chunk), - 4 - verify_input_data->size_last_chunk); - return; - } else if (chunk_index + 1 == verify_input_data->chunk_total) { - memcpy( - verify_input_data->locktime + 4 - verify_input_data->size_last_chunk, - raw_txn_chunk, - verify_input_data->size_last_chunk); - verify_input_data->has_locktime = true; - return; - } else { - // wait for subsequent chunks - return; - } - } else if (chunk_index + 1 == verify_input_data->chunk_total) { - memcpy(verify_input_data->locktime, - raw_txn_chunk + verify_input_data->size_last_chunk - 4, - 4); - verify_input_data->has_locktime = true; - } else { - // wait for subsequent chunks - return; - } -} - -// TODO: Add chunking condition for varint decode -// refer: https://app.clickup.com/t/9002019994/PRF-7288 -static int64_t varint_decode(const uint8_t *raw_txn_chunk, int32_t *offset) { - uint8_t first_byte = raw_txn_chunk[*offset]; - if (first_byte < 0xFD) { - return first_byte; - } else { - // TODO: var-int varies between 1-9 bytes - // current implementation supports decoding - // upto 3 bytes only - uint8_t result[2]; - memcpy(result, raw_txn_chunk + *offset + 1, 2); - *offset += 2; - return U16_READ_LE_ARRAY(result); - } -} /***************************************************************************** * GLOBAL FUNCTIONS *****************************************************************************/ -int btc_verify_input(const uint8_t *raw_txn_chunk, - const uint32_t chunk_index, - btc_verify_input_t *verify_input_data, - const btc_sign_txn_input_t *input) { - if (NULL == input || NULL == raw_txn_chunk || - 0 == verify_input_data->chunk_total) { - return -1; - } - - int32_t offset = 0; - if (chunk_index == 0) { - // ignore network version (4-bytes), skip marker & flag (in segwit) - offset += (raw_txn_chunk[4] == 0 ? 6 : 4); - if (6 == offset) { - verify_input_data->is_segwit = true; - } - - // TODO: Improve varint decode. - // size of variable containing script size and ip-count/op-count - // varies (1-9 Bytes) depending on its value. - // refer: - // https://en.bitcoin.it/wiki/Protocol_documentation#Variable_length_integer - verify_input_data->count = - raw_txn_chunk[offset++]; ///< store the number of inputs in the - ///< raw_txn - - verify_input_data->parsetype = INPUT; - sha256_Init(&(verify_input_data->sha_256_ctx)); - } else { - offset += verify_input_data->prev_offset; - } - switch (verify_input_data->parsetype) { - case INPUT: { - while (verify_input_data->input_index < (verify_input_data->count) && - INPUT == verify_input_data->parsetype) { - input_case ip_case = verify_input_data->input_parse; - switch (ip_case) { - case PREVIOUS_TX_HASH_PLUS_OP_INDEX_CASE: { - if (offset + 36 >= CHUNK_SIZE) { - verify_input_data->prev_offset = (offset + 36) - CHUNK_SIZE; - update_hash( - verify_input_data, raw_txn_chunk, chunk_index, CHUNK_SIZE); - verify_input_data->input_parse = SCRIPT_LENGTH_CASE; - return 4; - } else { - offset += 36; - } - } - - case SCRIPT_LENGTH_CASE: { - int64_t script_length = varint_decode(raw_txn_chunk, &offset); - if (offset + script_length + 1 + 4 >= CHUNK_SIZE) { - verify_input_data->prev_offset = - (offset + script_length + 1 + 4) - CHUNK_SIZE; - update_hash( - verify_input_data, raw_txn_chunk, chunk_index, CHUNK_SIZE); - verify_input_data->input_parse = - PREVIOUS_TX_HASH_PLUS_OP_INDEX_CASE; - verify_input_data->input_index++; - return 4; - } else { - offset += (script_length + 1 + 4); - } - break; - } - - default: - break; - } - verify_input_data->input_parse = PREVIOUS_TX_HASH_PLUS_OP_INDEX_CASE; - verify_input_data->input_index++; - } - verify_input_data->parsetype = OP_COUNT; - } - - case OP_COUNT: { - if (offset + 1 >= CHUNK_SIZE) { - // reset prev offset - verify_input_data->prev_offset = - offset - CHUNK_SIZE; ///< Did not add +1 as returning back to - ///< this stage to read op count - update_hash(verify_input_data, raw_txn_chunk, chunk_index, CHUNK_SIZE); - return 4; - } else { - verify_input_data->count = raw_txn_chunk[offset++]; - } - verify_input_data->parsetype = OUTPUT; - verify_input_data->output_parse = VALUE_CASE; - } - - case OUTPUT: { - while (verify_input_data->output_index < verify_input_data->count) { - output_case op_case = verify_input_data->output_parse; - switch (op_case) { - case VALUE_CASE: { - if (verify_input_data->output_index == input->prev_output_index) { - if (offset + 8 >= CHUNK_SIZE) { - verify_input_data->prev_offset = (offset + 8) - CHUNK_SIZE; - memcpy(verify_input_data->value, - raw_txn_chunk + offset, - CHUNK_SIZE - offset); - update_hash( - verify_input_data, raw_txn_chunk, chunk_index, CHUNK_SIZE); - verify_input_data->output_parse = VALUE_SPLIT_CASE; - verify_input_data->is_split = 1; - return 4; - } else { - memcpy(verify_input_data->value, raw_txn_chunk + offset, 8); - } - } - if (offset + 8 >= CHUNK_SIZE) { - verify_input_data->prev_offset = (offset + 8) - CHUNK_SIZE; - update_hash( - verify_input_data, raw_txn_chunk, chunk_index, CHUNK_SIZE); - verify_input_data->output_parse = SCRIPT_PUBKEY_CASE; - return 4; - } else { - offset += 8; - } - } - - case VALUE_SPLIT_CASE: { - if (verify_input_data->is_split) { - memcpy(verify_input_data->value + (8 - offset), - raw_txn_chunk, - offset); - verify_input_data->output_parse = SCRIPT_PUBKEY_CASE; - verify_input_data->is_split = 0; - } - } - - case SCRIPT_PUBKEY_CASE: { - if (offset + raw_txn_chunk[offset] + 1 >= CHUNK_SIZE) { - verify_input_data->prev_offset = - (offset + raw_txn_chunk[offset] + 1) - CHUNK_SIZE; - update_hash( - verify_input_data, raw_txn_chunk, chunk_index, CHUNK_SIZE); - verify_input_data->output_parse = VALUE_CASE; - verify_input_data->output_index++; - return 4; - } else { - offset += (raw_txn_chunk[offset] + 1); - } - break; - } - default: - break; - } - verify_input_data->output_parse = VALUE_CASE; - verify_input_data->output_index++; - } - - verify_input_data->parsetype = LOCK_TIME; - update_hash(verify_input_data, raw_txn_chunk, chunk_index, offset); - } - - case LOCK_TIME: { - update_locktime(verify_input_data, raw_txn_chunk, chunk_index); - if (false == verify_input_data->has_locktime) { - return 4; - } - sha256_Update( - &(verify_input_data->sha_256_ctx), verify_input_data->locktime, 4); - } - default: - break; - } - - verify_input_data->parsetype = END; - - // Finalize hashing - uint8_t hash[SHA256_DIGEST_LENGTH] = {0}; - sha256_Final(&(verify_input_data->sha_256_ctx), hash); - - if (U64_READ_LE_ARRAY(verify_input_data->value) == 0) { - return 1; - } - sha256_Raw(hash, sizeof(hash), hash); - // verify input txn hash - if (memcmp(hash, input->prev_txn_hash, sizeof(input->prev_txn_hash)) != 0) { - return 2; - } - if (U64_READ_LE_ARRAY(verify_input_data->value) != input->value) { - return 3; - } - return 0; -} - uint64_t get_transaction_fee_threshold(const btc_txn_context_t *txn_ctx) { return (g_btc_app->max_fee / 1000) * (get_transaction_weight(txn_ctx) / 4); } diff --git a/apps/btc_family/btc_txn_helpers.h b/apps/btc_family/btc_txn_helpers.h index 3555b0139..fb4893ffc 100644 --- a/apps/btc_family/btc_txn_helpers.h +++ b/apps/btc_family/btc_txn_helpers.h @@ -13,9 +13,7 @@ * INCLUDES *****************************************************************************/ -#include "btc/sign_txn.pb.h" #include "btc_priv.h" -#include "sha2.h" /***************************************************************************** * MACROS AND DEFINES @@ -26,39 +24,6 @@ /***************************************************************************** * TYPEDEFS *****************************************************************************/ -typedef enum parse_type { INPUT, OP_COUNT, OUTPUT, LOCK_TIME, END } parse_type; - -typedef enum input_case { - PREVIOUS_TX_HASH_PLUS_OP_INDEX_CASE, - SCRIPT_LENGTH_CASE, - SEQ_CASE -} input_case; - -typedef enum output_case { - VALUE_CASE, - VALUE_SPLIT_CASE, - SCRIPT_PUBKEY_CASE -} output_case; - -typedef enum hash_case { FIRST_CHUNK_HASH, DEFAULT } hash_case; -typedef struct btc_verify_input { - int32_t chunk_total; - int32_t count; // count of ip/op - int32_t prev_offset; // offset to remember from prev chunk - int32_t input_index; - int32_t output_index; - SHA256_CTX sha_256_ctx; - parse_type parsetype; - input_case input_parse; - output_case output_parse; - bool is_segwit; - bool is_split; - bool has_locktime; - bool is_locktime_split; - int32_t size_last_chunk; - uint8_t value[8]; - uint8_t locktime[4]; -} btc_verify_input_t; /***************************************************************************** * EXPORTED VARIABLES @@ -68,34 +33,6 @@ typedef struct btc_verify_input { * GLOBAL FUNCTION PROTOTYPES *****************************************************************************/ -/** - * @brief Verifies the provided input with its related raw transaction byte - * @details The function verifies if the input details match with the details in - * the raw transaction. This is done by checking the output value against the - * specified output index in the raw transaction and then finally matching the - * specified hash with the calculated hash from the raw transactions bytes. - * To remove size limitations, the function requests the prev_txn from host - * in chunks of size CHUNK_SIZE. - * - * @param [in] raw_txn_chunk current chunk of transaction. - * @param [in] chunk_index index of current chunk. - * @param [in] verify_input_data struct to hold data and flags required by - * parser. - * @param [in] input Immutable reference to the btc_txn_input_t. - * - * @return int Result of verification, 0 if verified otherwise error status. - * @retval 0 Input verified successfully. - * @retval -1 If function parameters are invalid - * @retval 1 If specified output index (input->prev_output_index) is not present - * @retval 2 If there is a hash (input->prev_txn_hash) mismatch - * @retval 3 If there is a value (input->value) mismatch - * @retval 4 If in processing state, not all chunks parsed - */ -int btc_verify_input(const uint8_t *raw_txn_chunk, - const uint32_t chunk_index, - btc_verify_input_t *verify_input_data, - const btc_sign_txn_input_t *input); - /** * @brief Calculates an estimated upper cap on the transaction fee. * @details The function calculates the fee according to the assumed upper cap diff --git a/tests/apps/btc_app/btc_txn_helpers_tests.c b/tests/apps/btc_app/btc_txn_helpers_tests.c index 18d6a6bb9..95ef429d0 100644 --- a/tests/apps/btc_app/btc_txn_helpers_tests.c +++ b/tests/apps/btc_app/btc_txn_helpers_tests.c @@ -69,39 +69,6 @@ uint32_t get_transaction_weight(const btc_txn_context_t *txn_ctx); -// wrapper function to call 'btc_verify_input' -// The function takes the entire 'raw_txn' and feeds it to -// 'btc_verify_input' in chunks of size CHUNK_SIZE, simulating the sdk -int btc_verify_input_test(btc_sign_txn_input_t *input, - uint8_t *raw_txn, - int ip_txn_bytes_size) { - btc_verify_input_t verify_input_data; - int status = 4; - memzero(&(verify_input_data), sizeof(btc_verify_input_t)); - verify_input_data.chunk_total = (ip_txn_bytes_size % CHUNK_SIZE == 0) - ? (ip_txn_bytes_size / CHUNK_SIZE) - : (ip_txn_bytes_size / CHUNK_SIZE) + 1; - - uint8_t txn_chunk[CHUNK_SIZE] = {0}; - verify_input_data.size_last_chunk = ip_txn_bytes_size % CHUNK_SIZE; - int index = 0; - for (int chunk_var = ip_txn_bytes_size; chunk_var > 0; - chunk_var -= CHUNK_SIZE) { - int chvar = (chunk_var >= CHUNK_SIZE) ? CHUNK_SIZE : chunk_var % CHUNK_SIZE; - - for (int i = 0; i < (chvar); i++) { - txn_chunk[i] = raw_txn[i + (CHUNK_SIZE * index)]; - } - - status = btc_verify_input(txn_chunk, index, &verify_input_data, input); - if (status != 4) { - break; - } - index++; - } - return status; -} - TEST_GROUP(btc_txn_helper_test); /** @@ -124,282 +91,6 @@ TEST_TEAR_DOWN(btc_txn_helper_test) { g_btc_app = NULL; } -TEST(btc_txn_helper_test, btc_txn_helper_verify_input_p2pk) { - /* Test data source: rawTxn - - * https://blockchain.info/rawtx/0437cd7f8525ceed2324359c2d0ba26006d92d856a9c20fa0241106ee5a597c9?format=hex - * txnElements - - * https://blockchain.info/rawtx/f4184fc596403b9d638783cf57adfe4c75c605f6356fbc91338530e9831e9e16?format=json - * Code reference - https://www.blockchain.com/explorer/api/blockchain_api - */ - uint8_t raw_txn[300] = {0}; - hex_string_to_byte_array( - "010000000100000000000000000000000000000000000000000000000000000000000000" - "00ffffffff0704ffff001d0134ffffffff0100f2052a0100000043410411db93e1dcdb8a" - "016b49840f8c53bc1eb68a382e97b1482ecad7b148a6909a5cb2e0eaddfb84ccf9744464" - "f82e160bfa9b8b64f9d4c03f999b8643f656b412a3ac00000000", - 268, - raw_txn); - // only fill necessary values - btc_sign_txn_input_t input = {.prev_output_index = 0, - .value = 5000000000, - .script_pub_key = { - .size = 67, - }}; - hex_string_to_byte_array( - "0437cd7f8525ceed2324359c2d0ba26006d92d856a9c20fa0241106ee5a597c9", - 64, - input.prev_txn_hash); - // revere order of txn-id: - cy_reverse_byte_array(input.prev_txn_hash, sizeof(input.prev_txn_hash)); - - hex_string_to_byte_array( - "410411db93e1dcdb8a016b49840f8c53bc1eb68a382e97b1482ecad7b148a6909a5cb2e0" - "eaddfb84ccf9744464f82e160bfa9b8b64f9d4c03f999b8643f656b412a3ac", - 134, - input.script_pub_key.bytes); - - int status = btc_verify_input_test(&input, raw_txn, 134); - - TEST_ASSERT_EQUAL_INT(0, status); -} - -TEST(btc_txn_helper_test, btc_txn_helper_verify_input_p2pk_fail) { - /* Test data source: rawTxn - - * https://blockchain.info/rawtx/0437cd7f8525ceed2324359c2d0ba26006d92d856a9c20fa0241106ee5a597c9?format=hex - * txnElements - - * https://blockchain.info/rawtx/f4184fc596403b9d638783cf57adfe4c75c605f6356fbc91338530e9831e9e16?format=json - * Code reference - https://www.blockchain.com/explorer/api/blockchain_api - */ - uint8_t raw_txn[300] = {0}; - hex_string_to_byte_array( - "010000000100000000000000000000000000000000000000000000000000000000000000" - "00ffffffff0704ffff001d0134ffffffff0100f2052a0100000043410411db93e1dcdb8a" - "016b49840f8c53bc1eb68a382e97b1482ecad7b148a6909a5cb2e0eaddfb84ccf9744464" - "f82e160bfa9b8b64f9d4c03f999b8643f656b412a3ac00000000", - 268, - raw_txn); - // only fill necessary values - btc_sign_txn_input_t input = { - .prev_output_index = 1, // incorrect index; correct is '0' - .value = 5000000000, - .script_pub_key = { - .size = 67, - }}; - hex_string_to_byte_array( - "0437cd7f8525ceed2324359c2d0ba26006d92d856a9c20fa0241106ee5a597c9", - 64, - input.prev_txn_hash); - // revere order of txn-id: - // 0437cd7f8525ceed2324359c2d0ba26006d92d856a9c20fa0241106ee5a597c9 - cy_reverse_byte_array(input.prev_txn_hash, sizeof(input.prev_txn_hash)); - hex_string_to_byte_array( - "410411db93e1dcdb8a016b49840f8c53bc1eb68a382e97b1482ecad7b148a6909a5cb2e0" - "eaddfb84ccf9744464f82e160bfa9b8b64f9d4c03f999b8643f656b412a3ac", - 134, - input.script_pub_key.bytes); - - int status = btc_verify_input_test(&input, raw_txn, 134); - - TEST_ASSERT_EQUAL_INT(1, status); -} - -TEST(btc_txn_helper_test, btc_txn_helper_verify_input_p2pkh) { - /* Test data source: rawTxn - - * https://blockchain.info/rawtx/eb0e2029310edade8e2a034aea4f0c4a1e243fe2dce67d05f95fddb7ac11bfbe?format=hex - * txnElements - - * https://blockchain.info/rawtx/16fbc39570ac5f16c103a39da1920ab3b77ad4f21f3c6d415c745bd6a37097e1?format=json - * Code reference - https://www.blockchain.com/explorer/api/blockchain_api - */ - uint8_t raw_txn[300] = {0}; - hex_string_to_byte_array( - "01000000014da2d059c1c6eb1c66884643f3bfa917cdb182273bf9dd2361db0c1c6bc706" - "61000000008b483045022100f2522df4a0d2193ee53ad95b698bf502e5874d340b4bb4f8" - "0720015f2ce87296022061d33d02d6f4a3b131a18328c3b1249e53fa115735c2597e29b0" - "738a1d5f3f8801410445bd85326dabc1772b4b319e0dc924ef93caf2360a033941e427f0" - "397b265f3c46e805be40904034880e781ab758a9e67518c624393e2ac14339faa45fafc5" - "deffffffff0100e1f505000000001976a91412ab8dc588ca9d5787dde7eb29569da63c3a" - "238c88ac00000000", - 448, - raw_txn); - // only fill necessary values - btc_sign_txn_input_t input = {.prev_output_index = 0, - .value = 100000000, - .script_pub_key = { - .size = 25, - }}; - hex_string_to_byte_array( - "eb0e2029310edade8e2a034aea4f0c4a1e243fe2dce67d05f95fddb7ac11bfbe", - 64, - input.prev_txn_hash); - // revere order of txn-id: - // eb0e2029310edade8e2a034aea4f0c4a1e243fe2dce67d05f95fddb7ac11bfbe - cy_reverse_byte_array(input.prev_txn_hash, sizeof(input.prev_txn_hash)); - hex_string_to_byte_array("76a91412ab8dc588ca9d5787dde7eb29569da63c3a238c88ac", - 50, - input.script_pub_key.bytes); - - int status = btc_verify_input_test(&input, raw_txn, 224); - TEST_ASSERT_EQUAL_INT(0, status); -} - -TEST(btc_txn_helper_test, btc_txn_helper_verify_input_p2pkh_fail) { - /* Test data source: rawTxn - - * https://blockchain.info/rawtx/eb0e2029310edade8e2a034aea4f0c4a1e243fe2dce67d05f95fddb7ac11bfbe?format=hex - * txnElements - - * https://blockchain.info/rawtx/16fbc39570ac5f16c103a39da1920ab3b77ad4f21f3c6d415c745bd6a37097e1?format=json - * Code reference - https://www.blockchain.com/explorer/api/blockchain_api - */ - uint8_t raw_txn[300] = {0}; - hex_string_to_byte_array( - "01000000014da2d059c1c6eb1c66884643f3bfa917cdb182273bf9dd2361db0c1c6bc706" - "61000000008b483045022100f2522df4a0d2193ee53ad95b698bf502e5874d340b4bb4f8" - "0720015f2ce87296022061d33d02d6f4a3b131a18328c3b1249e53fa115735c2597e29b0" - "738a1d5f3f8801410445bd85326dabc1772b4b319e0dc924ef93caf2360a033941e427f0" - "397b265f3c46e805be40904034880e781ab758a9e67518c624393e2ac14339faa45fafc5" - "deffffffff0100e1f505000000001976a91412ab8dc588ca9d5787dde7eb29569da63c3a" - "238c88ac00000000", - 448, - raw_txn); - // only fill necessary values - btc_sign_txn_input_t input = { - .prev_output_index = 0, - .value = 1000000000, // invalid value; more by a factor of 10 - .script_pub_key = { - .size = 25, - }}; - hex_string_to_byte_array( - "eb0e2029310edade8e2a034aea4f0c4a1e243fe2dce67d05f95fddb7ac11bfbe", - 64, - input.prev_txn_hash); - // revere order of txn-id: - // eb0e2029310edade8e2a034aea4f0c4a1e243fe2dce67d05f95fddb7ac11bfbe - cy_reverse_byte_array(input.prev_txn_hash, sizeof(input.prev_txn_hash)); - hex_string_to_byte_array("76a91412ab8dc588ca9d5787dde7eb29569da63c3a238c88ac", - 50, - input.script_pub_key.bytes); - - int status = btc_verify_input_test(&input, raw_txn, 224); - TEST_ASSERT_EQUAL_INT(3, status); -} - -TEST(btc_txn_helper_test, btc_txn_helper_verify_input_p2wpkh) { - /* Test data source: rawTxn - - * https://blockchain.info/rawtx/21706dfac590a74e7d083ad60e790c3a1775a4818afd7aa3ddf1a3d76dc16b03?format=hex - * txnElements - - * https://blockchain.info/rawtx/fcb26cf6235d591b89494398b51746610917fd49c376994fa0ce24fcc383eac0?format=json - * Code reference - https://www.blockchain.com/explorer/api/blockchain_api - */ - uint8_t raw_txn[2000] = {0}; - hex_string_to_byte_array( - "0100000006d572e075a155d3fa334230a691cf085d463215aead3c417f08f931ccbbe6a1" - "321b0000006b483045022100cdeb3add0650fc8b8351512d6df17fbfc602ec484d8c5c6a" - "7800d3cd5e71fac60220481616b00629c8c7a81a10397db2942ce21676eb5fd4be20bc44" - "31754c657f6001210217630c3bfab894e6e7322ae6d104d9b9aff58ae8ce4c30f45e9903" - "51ba3668a4ffffffff9b4139cb7a02366cd228514a89b46cb55fc8183b03c3f9b8fe583e" - "fe93f28958420000006b483045022100a19519b0d4af6a5f50301c36d620c7c2b5fea705" - "7874a184c03db88196933fd4022070e559e34fe80e17a6d9e4b400d183c22af094684004" - "1220a0965487964b46f301210334f414ce378f5f24a128dbf34a1be8d8a1b863e665f782" - "99a52f06991be8406dffffffff4b98c9a61ad1b2070ebbcfc2d616824ff3259c5c0d09b8" - "1f38975446f12e57cb3a0000006b483045022100e9b10baf0226b7394474142b87edcd26" - "fef74f6fcb6b82833559e95481a51f5802204db8a3f7962f1e731b815f01807ef16b6d48" - "bd73a40e6534d8e4abc1d72a2554012103e7dcb93f93afdf17b60e00b21f8a283e8140f5" - "3619970daf7d3b5934230c420dfffffffff63acaa7893f29aa069dbbd6c74bdb9f597d70" - "3e0f9a99d52b5930edd568ff54000000006b483045022100a3b9578999c1fb5d07c047c9" - "6569476a73cc8684b1b6f1461cdeaa81de58e631022049cb3e73977605aad472e470a995" - "9ab913f2b101c268b8cddadd4bca16db4bf10121033011c8839eb82d1c37596bee611cf7" - "0564d21fb38ce0f4535c4d1d5e53dad958ffffffffd5c0e501e2045ba6fed26aba945e0d" - "6333d2ca555a60e4c1c920f3e27030bc17400000006b483045022100bbf7e176a402c223" - "32ab21c3aa0a5ca05bb9efe0d2a3f3797d5174873d4fc5e8022061aba78025eb56e7a8e1" - "663c68bb63ff0d0d7db0b6fc5064e021994a59cf8c0c012103ca9827e71289cfc3581340" - "ae413ae7860af88829b05e30083cee496cb9cdff39ffffffff64cca9bfba56c7f2dfb497" - "53a7f90fe8e2b2121f3d1cd9221a1032425095ed3a8f0000006b483045022100b275537b" - "a5e33b3511925e68df84d62487640dd813cf179cf65abd2920f4c29f022072523568c14f" - "50881bd527d352847a343d65f5620a0df6e6683c8a793ff77a240121030ab067dab80cd5" - "89f30e36d45902e933abc270304829b4e7323ba51695f8d331ffffffff01f0270f000000" - "0000160014854fe623a8a6a4c76779b57c3895ed2e0962647400000000", - 1858, - raw_txn); - // only fill necessary values - btc_sign_txn_input_t input = {.prev_output_index = 0, - .value = 993264, - .script_pub_key = { - .size = 22, - }}; - hex_string_to_byte_array( - "21706dfac590a74e7d083ad60e790c3a1775a4818afd7aa3ddf1a3d76dc16b03", - 64, - input.prev_txn_hash); - // revere order of txn-id: - // 21706dfac590a74e7d083ad60e790c3a1775a4818afd7aa3ddf1a3d76dc16b03 - cy_reverse_byte_array(input.prev_txn_hash, sizeof(input.prev_txn_hash)); - hex_string_to_byte_array("0014854fe623a8a6a4c76779b57c3895ed2e09626474", - 44, - input.script_pub_key.bytes); - - int status = btc_verify_input_test(&input, raw_txn, 929); - TEST_ASSERT_EQUAL_INT(0, status); -} - -TEST(btc_txn_helper_test, btc_txn_helper_verify_input_p2wpkh_fail) { - /* Test data source: rawTxn - - * https://blockchain.info/rawtx/21706dfac590a74e7d083ad60e790c3a1775a4818afd7aa3ddf1a3d76dc16b03?format=hex - * txnElements - - * https://blockchain.info/rawtx/fcb26cf6235d591b89494398b51746610917fd49c376994fa0ce24fcc383eac0?format=json - * Code reference - https://www.blockchain.com/explorer/api/blockchain_api - */ - uint8_t raw_txn[2000] = {0}; - hex_string_to_byte_array( - "0100000006d572e075a155d3fa334230a691cf085d463215aead3c417f08f931ccbbe6a1" - "321b0000006b483045022100cdeb3add0650fc8b8351512d6df17fbfc602ec484d8c5c6a" - "7800d3cd5e71fac60220481616b00629c8c7a81a10397db2942ce21676eb5fd4be20bc44" - "31754c657f6001210217630c3bfab894e6e7322ae6d104d9b9aff58ae8ce4c30f45e9903" - "51ba3668a4ffffffff9b4139cb7a02366cd228514a89b46cb55fc8183b03c3f9b8fe583e" - "fe93f28958420000006b483045022100a19519b0d4af6a5f50301c36d620c7c2b5fea705" - "7874a184c03db88196933fd4022070e559e34fe80e17a6d9e4b400d183c22af094684004" - "1220a0965487964b46f301210334f414ce378f5f24a128dbf34a1be8d8a1b863e665f782" - "99a52f06991be8406dffffffff4b98c9a61ad1b2070ebbcfc2d616824ff3259c5c0d09b8" - "1f38975446f12e57cb3a0000006b483045022100e9b10baf0226b7394474142b87edcd26" - "fef74f6fcb6b82833559e95481a51f5802204db8a3f7962f1e731b815f01807ef16b6d48" - "bd73a40e6534d8e4abc1d72a2554012103e7dcb93f93afdf17b60e00b21f8a283e8140f5" - "3619970daf7d3b5934230c420dfffffffff63acaa7893f29aa069dbbd6c74bdb9f597d70" - "3e0f9a99d52b5930edd568ff54000000006b483045022100a3b9578999c1fb5d07c047c9" - "6569476a73cc8684b1b6f1461cdeaa81de58e631022049cb3e73977605aad472e470a995" - "9ab913f2b101c268b8cddadd4bca16db4bf10121033011c8839eb82d1c37596bee611cf7" - "0564d21fb38ce0f4535c4d1d5e53dad958ffffffffd5c0e501e2045ba6fed26aba945e0d" - "6333d2ca555a60e4c1c920f3e27030bc17400000006b483045022100bbf7e176a402c223" - "32ab21c3aa0a5ca05bb9efe0d2a3f3797d5174873d4fc5e8022061aba78025eb56e7a8e1" - "663c68bb63ff0d0d7db0b6fc5064e021994a59cf8c0c012103ca9827e71289cfc3581340" - "ae413ae7860af88829b05e30083cee496cb9cdff39ffffffff64cca9bfba56c7f2dfb497" - "53a7f90fe8e2b2121f3d1cd9221a1032425095ed3a8f0000006b483045022100b275537b" - "a5e33b3511925e68df84d62487640dd813cf179cf65abd2920f4c29f022072523568c14f" - "50881bd527d352847a343d65f5620a0df6e6683c8a793ff77a240121030ab067dab80cd5" - "89f30e36d45902e933abc270304829b4e7323ba51695f8d331ffffffff01f0270f000000" - "0000160014854fe623a8a6a4c76779b57c3895ed2e0962647400000000", - 1858, - raw_txn); - // only fill necessary values - btc_sign_txn_input_t input = {.prev_output_index = 0, - .value = 993264, - .script_pub_key = { - .size = 22, - }}; - hex_string_to_byte_array( - // invalid txn hash test. valid txn hash/id: - // 21706dfac590a74e7d083ad60e790c3a1775a4818afd7aa3ddf1a3d76dc16b03 - "21706dfac590a74e7d083ad60e790c3a1775a4818afd7aa3ddf1a3d76dc16b04", - 64, - input.prev_txn_hash); - // revere order of txn-id: - // 21706dfac590a74e7d083ad60e790c3a1775a4818afd7aa3ddf1a3d76dc16b04 - cy_reverse_byte_array(input.prev_txn_hash, sizeof(input.prev_txn_hash)); - hex_string_to_byte_array("0014854fe623a8a6a4c76779b57c3895ed2e09626474", - 44, - input.script_pub_key.bytes); - - int status = btc_verify_input_test(&input, raw_txn, 929); - TEST_ASSERT_EQUAL_INT(2, status); -} - /* FIX: Required to fix the hardcoded value of 106 (2 + 33 + 71) since the * signature part of the script can vary (71 bytes | 72 bytes | 73 bytes). * Check the get_transaction_weight function. */ diff --git a/tests/unit_test_lists.c b/tests/unit_test_lists.c index 572649652..4b0f93f24 100644 --- a/tests/unit_test_lists.c +++ b/tests/unit_test_lists.c @@ -145,13 +145,6 @@ TEST_GROUP_RUNNER(manager_api_test) { } TEST_GROUP_RUNNER(btc_txn_helper_test) { - RUN_TEST_CASE(btc_txn_helper_test, btc_txn_helper_verify_input_p2pk); - RUN_TEST_CASE(btc_txn_helper_test, btc_txn_helper_verify_input_p2pk_fail); - RUN_TEST_CASE(btc_txn_helper_test, btc_txn_helper_verify_input_p2pkh); - RUN_TEST_CASE(btc_txn_helper_test, btc_txn_helper_verify_input_p2pkh_fail); - RUN_TEST_CASE(btc_txn_helper_test, btc_txn_helper_verify_input_p2wpkh); - RUN_TEST_CASE(btc_txn_helper_test, btc_txn_helper_verify_input_p2wpkh_fail); - RUN_TEST_CASE(btc_txn_helper_test, btc_txn_helper_transaction_weight_legacy1); RUN_TEST_CASE(btc_txn_helper_test, btc_txn_helper_transaction_weight_legacy2); RUN_TEST_CASE(btc_txn_helper_test, btc_txn_helper_transaction_weight_segwit1);