Skip to content

Commit 80bfa67

Browse files
committed
refurbish readers
1 parent ced71c8 commit 80bfa67

8 files changed

Lines changed: 435 additions & 298 deletions

File tree

CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,9 @@ file(GLOB_RECURSE LIB_SRC
141141
${CMAKE_CURRENT_SOURCE_DIR}/app/src/metadata_reader.c
142142
${CMAKE_CURRENT_SOURCE_DIR}/app/src/borsh.c
143143
${CMAKE_CURRENT_SOURCE_DIR}/app/src/unsigned_transaction_reader.c
144+
${CMAKE_CURRENT_SOURCE_DIR}/app/src/modules/common_reader.c
145+
${CMAKE_CURRENT_SOURCE_DIR}/app/src/modules/bank_reader.c
146+
${CMAKE_CURRENT_SOURCE_DIR}/app/src/modules/paymaster_reader.c
144147
)
145148

146149
add_library(app_lib STATIC ${LIB_SRC})
@@ -151,6 +154,7 @@ target_include_directories(app_lib PUBLIC
151154
${CMAKE_CURRENT_SOURCE_DIR}/app/src/lib
152155
${CMAKE_CURRENT_SOURCE_DIR}/app/src/common
153156
${CMAKE_CURRENT_SOURCE_DIR}/app/src/txdefs
157+
${CMAKE_CURRENT_SOURCE_DIR}/app/src/modules
154158
${CMAKE_CURRENT_SOURCE_DIR}/deps/picohash
155159
)
156160

app/src/modules/bank_reader.c

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*******************************************************************************
2+
* (c) 2018 - 2025 Zondax AG
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
********************************************************************************/
16+
17+
#include "common_reader.h"
18+
19+
parser_error_t read_bank_transfer(parser_context_t *ctx, bank_transfer_t *transfer) {
20+
CHECK_INPUT(ctx);
21+
CHECK_INPUT(transfer);
22+
23+
CHECK_ERROR(read_address(ctx, &transfer->to));
24+
CHECK_ERROR(read_coins(ctx, &transfer->coins));
25+
26+
return parser_ok;
27+
}
28+
29+
parser_error_t read_bank_mint(parser_context_t *ctx, bank_mint_t *mint) {
30+
CHECK_INPUT(ctx);
31+
CHECK_INPUT(mint);
32+
33+
CHECK_ERROR(read_coins(ctx, &mint->coins));
34+
CHECK_ERROR(read_address(ctx, &mint->mint_to_address));
35+
36+
return parser_ok;
37+
}
38+
39+
parser_error_t read_bank_burn(parser_context_t *ctx, bank_burn_t *burn) {
40+
CHECK_INPUT(ctx);
41+
CHECK_INPUT(burn);
42+
43+
CHECK_ERROR(read_coins(ctx, &burn->coins));
44+
return parser_ok;
45+
}
46+
47+
parser_error_t read_bank_freeze(parser_context_t *ctx, bank_freeze_t *freeze) {
48+
CHECK_INPUT(ctx);
49+
CHECK_INPUT(freeze);
50+
51+
CHECK_ERROR(read_token_id(ctx, &freeze->token_id));
52+
return parser_ok;
53+
}
54+
55+
parser_error_t read_bank_call_message(parser_context_t *ctx, bank_call_message_t *bank) {
56+
CHECK_INPUT(ctx);
57+
CHECK_INPUT(bank);
58+
59+
// read call message type
60+
CHECK_ERROR(read_u8(ctx, (uint8_t *)&bank->type));
61+
switch (bank->type) {
62+
case BANK_CALL_MESSAGE_CREATE_TOKEN:
63+
print_string("BANK_CALL_MESSAGE_CREATE_TOKEN NOT IMPLEMENTED");
64+
return parser_unexpected_error;
65+
case BANK_CALL_MESSAGE_TRANSFER:
66+
print_string("CALL_MESSAGE_TRANSFER");
67+
CHECK_ERROR(read_bank_transfer(ctx, &bank->transfer));
68+
break;
69+
case BANK_CALL_MESSAGE_BURN:
70+
print_string("BANK_CALL_MESSAGE_BURN");
71+
CHECK_ERROR(read_bank_burn(ctx, &bank->burn));
72+
break;
73+
case BANK_CALL_MESSAGE_MINT:
74+
print_string("BANK_CALL_MESSAGE_MINT");
75+
CHECK_ERROR(read_bank_mint(ctx, &bank->mint));
76+
break;
77+
case BANK_CALL_MESSAGE_FREEZE:
78+
print_string("BANK_CALL_MESSAGE_FREEZE");
79+
CHECK_ERROR(read_bank_freeze(ctx, &bank->freeze));
80+
break;
81+
default:
82+
print_u8("Unexpected bank call message type:", bank->type);
83+
return parser_unexpected_error;
84+
}
85+
return parser_ok;
86+
}

app/src/modules/bank_reader.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*******************************************************************************
2+
* (c) 2018 - 2025 Zondax AG
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
********************************************************************************/
16+
17+
#pragma once
18+
19+
#ifdef __cplusplus
20+
extern "C" {
21+
#endif
22+
23+
parser_error_t read_bank_call_message(parser_context_t *ctx, bank_call_message_t *bank);
24+
25+
#ifdef __cplusplus
26+
}
27+
#endif

app/src/modules/common_reader.c

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/*******************************************************************************
2+
* (c) 2018 - 2025 Zondax AG
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
********************************************************************************/
16+
17+
#include "common_reader.h"
18+
19+
parser_error_t read_amount(parser_context_t *ctx, amount_t *amount) {
20+
CHECK_INPUT(ctx);
21+
CHECK_INPUT(amount);
22+
23+
CHECK_ERROR(read_u64(ctx, &amount->lo));
24+
CHECK_ERROR(read_u64(ctx, &amount->hi));
25+
print_u64_hex("amount:", amount->lo);
26+
print_u64_hex("amount:", amount->hi);
27+
28+
print_u64("amount:", amount->lo);
29+
print_u64("amount:", amount->hi);
30+
31+
return parser_ok;
32+
}
33+
34+
parser_error_t read_token_id(parser_context_t *ctx, token_id_t *token_id) {
35+
CHECK_INPUT(ctx);
36+
CHECK_INPUT(token_id);
37+
38+
token_id->token.len = TOKEN_ID_SIZE;
39+
token_id->token.ptr = ctx->buffer + ctx->offset;
40+
CTX_CHECK_AND_ADVANCE(ctx, TOKEN_ID_SIZE)
41+
42+
print_buffer(&token_id->token, "token_id");
43+
44+
return parser_ok;
45+
}
46+
47+
parser_error_t read_address(parser_context_t *ctx, address_t *address) {
48+
CHECK_INPUT(ctx);
49+
CHECK_INPUT(address);
50+
51+
// TODO: check this value, MultiAddress enum?
52+
uint8_t unknown = 0;
53+
CHECK_ERROR(read_u8(ctx, &unknown));
54+
print_u8("unknown", unknown);
55+
56+
address->address.len = ADDRESS_SIZE;
57+
address->address.ptr = ctx->buffer + ctx->offset;
58+
CTX_CHECK_AND_ADVANCE(ctx, ADDRESS_SIZE);
59+
60+
print_buffer(&address->address, "address");
61+
62+
return parser_ok;
63+
}
64+
65+
parser_error_t read_coins(parser_context_t *ctx, coins_t *coins) {
66+
CHECK_INPUT(ctx);
67+
CHECK_INPUT(coins);
68+
69+
CHECK_ERROR(read_amount(ctx, &coins->amount));
70+
CHECK_ERROR(read_token_id(ctx, &coins->token_id));
71+
72+
return parser_ok;
73+
}
74+
75+
parser_error_t read_gas(parser_context_t *ctx, gas_t *gas) {
76+
CHECK_INPUT(ctx);
77+
CHECK_INPUT(gas);
78+
79+
for (int i = 0; i < GAS_DIMENSIONS; i++) {
80+
CHECK_ERROR(read_u64(ctx, &gas->gas[i]));
81+
print_u64_hex("gas[%d]:", gas->gas[i]);
82+
print_u64("gas[%d]:", gas->gas[i]);
83+
}
84+
85+
return parser_ok;
86+
}
87+
88+
parser_error_t read_gas_u128(parser_context_t *ctx, gas_u128_t *gas) {
89+
CHECK_INPUT(ctx);
90+
CHECK_INPUT(gas);
91+
92+
for (int i = 0; i < GAS_DIMENSIONS; i++) {
93+
CHECK_ERROR(read_amount(ctx, &gas->gas[i]));
94+
}
95+
96+
return parser_ok;
97+
}

app/src/modules/common_reader.h

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*******************************************************************************
2+
* (c) 2018 - 2025 Zondax AG
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
********************************************************************************/
16+
17+
#pragma once
18+
19+
#ifdef __cplusplus
20+
extern "C" {
21+
#endif
22+
23+
#include "borsh.h"
24+
#include "parser_common.h"
25+
26+
parser_error_t read_amount(parser_context_t *ctx, amount_t *amount);
27+
parser_error_t read_token_id(parser_context_t *ctx, token_id_t *token_id);
28+
parser_error_t read_address(parser_context_t *ctx, address_t *address);
29+
parser_error_t read_coins(parser_context_t *ctx, coins_t *coins);
30+
parser_error_t read_gas(parser_context_t *ctx, gas_t *gas);
31+
parser_error_t read_gas_u128(parser_context_t *ctx, gas_u128_t *gas);
32+
33+
#ifdef __cplusplus
34+
}
35+
#endif

0 commit comments

Comments
 (0)