Skip to content

Commit

Permalink
move parameters decode
Browse files Browse the repository at this point in the history
  • Loading branch information
abenso committed Dec 2, 2024
1 parent b4a2f96 commit b2aa47c
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 17 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Expand Down
45 changes: 45 additions & 0 deletions app/src/parameters.c
Original file line number Diff line number Diff line change
@@ -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, &parameters->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;
}
39 changes: 39 additions & 0 deletions app/src/parameters.h
Original file line number Diff line number Diff line change
@@ -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 <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <zxmacros.h>

#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
17 changes: 2 additions & 15 deletions app/src/parser_impl.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion app/src/parser_pb_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion app/src/parser_pb_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit b2aa47c

Please sign in to comment.