-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use proper error types and silence unused warnings
Define a common field type to be used in other parts of the parser Fix unused warning Add spend action and helper types definitions Add decode function for spend plans add spend error
- Loading branch information
Showing
8 changed files
with
262 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/******************************************************************************* | ||
* (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 "zxformat.h" | ||
#include "parser_pb_utils.h" | ||
|
||
|
||
bool decode_fixed_field(pb_istream_t *stream, const pb_field_t *field, void **arg) | ||
{ | ||
if (stream->bytes_left == 0 || arg == NULL) | ||
return false; | ||
|
||
fixed_size_field_t *decode_arg = (fixed_size_field_t *)*arg; | ||
if (decode_arg == NULL || decode_arg->bytes == NULL) { | ||
return false; | ||
} | ||
|
||
if (decode_arg->check_size && stream->bytes_left != decode_arg->expected_size) { | ||
return false; | ||
} | ||
|
||
const uint8_t *first_byte = stream->state; | ||
uint16_t data_size = stream->bytes_left; | ||
|
||
decode_arg->bytes->ptr = first_byte; | ||
decode_arg->bytes->len = data_size; | ||
|
||
return true; | ||
} | ||
|
||
void setup_decode_fixed_field(pb_callback_t *callback, fixed_size_field_t *arg, Bytes_t *bytes, uint16_t expected_size, bool check_size) { | ||
arg->bytes = bytes; | ||
arg->expected_size = expected_size; | ||
arg->check_size = check_size; | ||
callback->funcs.decode = &decode_fixed_field; | ||
callback->arg = arg; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/******************************************************************************* | ||
* (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 | ||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
|
||
#include <stddef.h> | ||
#include <stdint.h> | ||
|
||
#include <zxmacros.h> | ||
#include <stdbool.h> | ||
|
||
#include "parser_common.h" | ||
#include "parser_txdef.h" | ||
#include "zxtypes.h" | ||
|
||
#include "pb_common.h" | ||
#include "pb_decode.h" | ||
#include "protobuf/penumbra/core/transaction/v1/transaction.pb.h" | ||
|
||
typedef struct { | ||
Bytes_t *bytes; | ||
uint16_t expected_size; | ||
bool check_size; | ||
} fixed_size_field_t; | ||
|
||
// Callback to parse binding fields in spend plans. all those fields are just | ||
// 32-bytes data array that later in rust can be converted into | ||
// Fq, Fr types | ||
bool decode_fixed_field(pb_istream_t *stream, const pb_field_t *field, void **arg); | ||
|
||
void setup_decode_fixed_field(pb_callback_t *callback, fixed_size_field_t *arg, Bytes_t *bytes, uint16_t expected_size, bool check_size); | ||
|
||
|
||
#ifdef __cplusplus | ||
} | ||
#endif | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/******************************************************************************* | ||
* (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 "pb_common.h" | ||
#include "pb_decode.h" | ||
#include "protobuf/penumbra/core/transaction/v1/transaction.pb.h" | ||
|
||
#include "parser_interface.h" | ||
#include "zxformat.h" | ||
#include "parser_pb_utils.h" | ||
|
||
parser_error_t decode_spend_plan(const Bytes_t *data, spend_plan_t *output) { | ||
penumbra_core_component_shielded_pool_v1_SpendPlan spend_plan = penumbra_core_component_shielded_pool_v1_SpendPlan_init_default; | ||
|
||
pb_istream_t spend_stream = pb_istream_from_buffer(data->ptr, data->len); | ||
CHECK_APP_CANARY() | ||
|
||
|
||
// Set up fixed size fields | ||
fixed_size_field_t randomizer_arg, value_blinding_arg, proof_blinding_r_arg, proof_blinding_s_arg; | ||
|
||
setup_decode_fixed_field(&spend_plan.randomizer, &randomizer_arg, &output->randomizer, 32, true); | ||
setup_decode_fixed_field(&spend_plan.value_blinding, &value_blinding_arg, &output->value_blinding, 32, true); | ||
setup_decode_fixed_field(&spend_plan.proof_blinding_r, &proof_blinding_r_arg, &output->proof_blinding_r, 32, true); | ||
setup_decode_fixed_field(&spend_plan.proof_blinding_s, &proof_blinding_s_arg, &output->proof_blinding_s, 32, true); | ||
setup_decode_fixed_field(&spend_plan.proof_blinding_s, &proof_blinding_s_arg, &output->proof_blinding_s, 32, true); | ||
|
||
// asset_id in Note | ||
fixed_size_field_t asset_id_arg; | ||
setup_decode_fixed_field(&spend_plan.note.value.asset_id.inner, &asset_id_arg, &output->note.value.asset_id.inner, ASSET_ID_LEN, true); | ||
// rseed in Note | ||
fixed_size_field_t rseed_arg; | ||
setup_decode_fixed_field(&spend_plan.note.rseed, &rseed_arg, &output->note.rseed, RSEED_LEN, true); | ||
|
||
if (!pb_decode(&spend_stream, penumbra_core_component_shielded_pool_v1_SpendPlan_fields, &spend_plan)) { | ||
return parser_spend_plan_error; | ||
} | ||
|
||
output->note.value.amount.lo = spend_plan.note.value.amount.lo; | ||
output->note.value.amount.hi = spend_plan.note.value.amount.hi; | ||
output->position = spend_plan.position; | ||
|
||
return parser_ok; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/******************************************************************************* | ||
* (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 | ||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
|
||
#include <stddef.h> | ||
#include <stdint.h> | ||
|
||
#include <zxmacros.h> | ||
#include <stdbool.h> | ||
|
||
#include "parser_common.h" | ||
#include "parser_txdef.h" | ||
#include "zxtypes.h" | ||
|
||
#include "pb_common.h" | ||
#include "pb_decode.h" | ||
#include "protobuf/penumbra/core/transaction/v1/transaction.pb.h" | ||
|
||
parser_error_t decode_spend_plan(const Bytes_t *input, spend_plan_t *spend_plan); | ||
|
||
|
||
#ifdef __cplusplus | ||
} | ||
#endif | ||
|