From b2aa47cc37b8514fc7844b4aa5991be16ca66caf Mon Sep 17 00:00:00 2001 From: abenso Date: Mon, 2 Dec 2024 17:06:45 -0300 Subject: [PATCH] move parameters decode --- CMakeLists.txt | 1 + app/src/parameters.c | 45 +++++++++++++++++++++++++++++++++++++++ app/src/parameters.h | 39 +++++++++++++++++++++++++++++++++ app/src/parser_impl.c | 17 ++------------- app/src/parser_pb_utils.c | 2 +- app/src/parser_pb_utils.h | 2 +- 6 files changed, 89 insertions(+), 17 deletions(-) create mode 100644 app/src/parameters.c create mode 100644 app/src/parameters.h diff --git a/CMakeLists.txt b/CMakeLists.txt index df4c8d0..071e8e8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -151,6 +151,7 @@ file(GLOB_RECURSE LIB_SRC ${CMAKE_CURRENT_SOURCE_DIR}/app/src/ui/note.c ${CMAKE_CURRENT_SOURCE_DIR}/app/src/known_assets.c ${CMAKE_CURRENT_SOURCE_DIR}/app/src/ui_utils.c + ${CMAKE_CURRENT_SOURCE_DIR}/app/src/parameters.c ) add_library(app_lib STATIC ${LIB_SRC}) diff --git a/app/src/parameters.c b/app/src/parameters.c new file mode 100644 index 0000000..5dee899 --- /dev/null +++ b/app/src/parameters.c @@ -0,0 +1,45 @@ +/******************************************************************************* + * (c) 2018 - 2023 Zondax AG + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + ********************************************************************************/ + +#include "parser_impl.h" +#include "parser_interface.h" +#include "parser_pb_utils.h" +#include "pb_common.h" +#include "pb_decode.h" +#include "protobuf/penumbra/core/transaction/v1/transaction.pb.h" +#include "zxformat.h" + +parser_error_t decode_parameters(const bytes_t *data, + const penumbra_core_transaction_v1_TransactionParameters *transaction_parameters, + parameters_t *parameters) { + // get transaction parameters + CHECK_ERROR(extract_data_from_tag(data, ¶meters->data_bytes, + penumbra_core_transaction_v1_TransactionPlan_transaction_parameters_tag)); + + // copy parameters + parameters->expiry_height = transaction_parameters->expiry_height; + parameters->has_fee = transaction_parameters->has_fee; + if (parameters->has_fee) { + parameters->fee.has_amount = transaction_parameters->fee.has_amount; + if (parameters->fee.has_amount) { + parameters->fee.amount.lo = transaction_parameters->fee.amount.lo; + parameters->fee.amount.hi = transaction_parameters->fee.amount.hi; + } + parameters->fee.has_asset_id = transaction_parameters->fee.has_asset_id; + } + + return parser_ok; +} diff --git a/app/src/parameters.h b/app/src/parameters.h new file mode 100644 index 0000000..5bfebf8 --- /dev/null +++ b/app/src/parameters.h @@ -0,0 +1,39 @@ +/******************************************************************************* + * (c) 2018 - 2023 Zondax AG + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + ********************************************************************************/ +#pragma once + +#include +#include +#include +#include + +#include "parser_common.h" +#include "parser_txdef.h" +#include "pb_common.h" +#include "pb_decode.h" +#include "zxtypes.h" + +#ifdef __cplusplus +extern "C" { +#endif + +parser_error_t decode_parameters(const bytes_t *data, + const penumbra_core_transaction_v1_TransactionParameters *transaction_parameters, + parameters_t *parameters); + +#ifdef __cplusplus +} +#endif diff --git a/app/src/parser_impl.c b/app/src/parser_impl.c index 4231ea0..aadedaf 100644 --- a/app/src/parser_impl.c +++ b/app/src/parser_impl.c @@ -26,6 +26,7 @@ #include "delegate_plan.h" #include "undelegate_plan.h" #include "ics20_withdrawal.h" +#include "parameters.h" #include "swap.h" #include "zxformat.h" @@ -166,22 +167,8 @@ parser_error_t _read(parser_context_t *c, parser_tx_t *v) { return parser_unexpected_error; } - // get transaction parameters - extract_data_from_tag(&data, &v->parameters_plan.data_bytes, - penumbra_core_transaction_v1_TransactionPlan_transaction_parameters_tag); v->plan.actions.qty = actions_qty; - - // copy parameters - v->parameters_plan.expiry_height = request.transaction_parameters.expiry_height; - v->parameters_plan.has_fee = request.transaction_parameters.has_fee; - if (v->parameters_plan.has_fee) { - v->parameters_plan.fee.has_amount = request.transaction_parameters.fee.has_amount; - if (v->parameters_plan.fee.has_amount) { - v->parameters_plan.fee.amount.lo = request.transaction_parameters.fee.amount.lo; - v->parameters_plan.fee.amount.hi = request.transaction_parameters.fee.amount.hi; - } - v->parameters_plan.fee.has_asset_id = request.transaction_parameters.fee.has_asset_id; - } + CHECK_ERROR(decode_parameters(&data, &request.transaction_parameters, &v->parameters_plan)); return parser_ok; } diff --git a/app/src/parser_pb_utils.c b/app/src/parser_pb_utils.c index ae02ead..f1d90e5 100644 --- a/app/src/parser_pb_utils.c +++ b/app/src/parser_pb_utils.c @@ -65,7 +65,7 @@ void setup_decode_variable_field(pb_callback_t *callback, variable_size_field_t callback->arg = arg; } -parser_error_t extract_data_from_tag(bytes_t *in, bytes_t *out, uint32_t tag) { +parser_error_t extract_data_from_tag(const bytes_t *in, bytes_t *out, uint32_t tag) { const uint8_t *start = NULL; const uint8_t *end = NULL; bool eof = false; diff --git a/app/src/parser_pb_utils.h b/app/src/parser_pb_utils.h index 9cafec0..f24e453 100644 --- a/app/src/parser_pb_utils.h +++ b/app/src/parser_pb_utils.h @@ -48,7 +48,7 @@ bool decode_variable_field(pb_istream_t *stream, const pb_field_t *field, void * void setup_decode_fixed_field(pb_callback_t *callback, fixed_size_field_t *arg, bytes_t *bytes, uint16_t expected_size); void setup_decode_variable_field(pb_callback_t *callback, variable_size_field_t *arg, bytes_t *bytes); -parser_error_t extract_data_from_tag(bytes_t *in, bytes_t *out, uint32_t tag); +parser_error_t extract_data_from_tag(const bytes_t *in, bytes_t *out, uint32_t tag); #ifdef __cplusplus }