Skip to content

Commit e33195a

Browse files
committed
read transaction with bank and mint
1 parent b51efa8 commit e33195a

6 files changed

Lines changed: 315 additions & 131 deletions

File tree

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ file(GLOB_RECURSE LIB_SRC
140140
${CMAKE_CURRENT_SOURCE_DIR}/app/src/crypto_helper.c
141141
${CMAKE_CURRENT_SOURCE_DIR}/app/src/metadata_reader.c
142142
${CMAKE_CURRENT_SOURCE_DIR}/app/src/borsh.c
143+
${CMAKE_CURRENT_SOURCE_DIR}/app/src/unsigned_transaction_reader.c
143144
)
144145

145146
add_library(app_lib STATIC ${LIB_SRC})

app/src/parser_impl.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,14 @@
1717
#include "parser_impl.h"
1818

1919
#include "metadata_reader.h"
20+
#include "unsigned_transaction_reader.h"
2021
#include "zxerror.h"
2122

2223
parser_error_t _read(parser_context_t *c, parser_tx_t *v) {
2324
UNUSED(c);
2425

25-
CHECK_ERROR(metadata_read(c, v));
26+
//CHECK_ERROR(metadata_read(c, v));
27+
CHECK_ERROR(unsigned_transaction_read(c, v));
2628

2729
return parser_ok;
2830
}

app/src/parser_txdef.h

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,102 @@ extern "C" {
2525

2626
#include "parser_txdef_metadata.h"
2727

28+
#define GAS_DIMENSIONS 2
29+
#define TOKEN_ID_SIZE 32
30+
#define ADDRESS_SIZE 29
31+
typedef enum {
32+
RUNTIME_BANK,
33+
RUNTIME_SEQUENCER_REGISTRY,
34+
RUNTIME_VALUE_SETTER,
35+
RUNTIME_ATTESTER_INCENTIVES,
36+
RUNTIME_PROVER_INCENTIVES,
37+
RUNTIME_ACCOUNTS,
38+
RUNTIME_UNIQUENESS,
39+
RUNTIME_CHAIN_STATE,
40+
RUNTIME_BLOB_STORAGE,
41+
RUNTIME_PAYMASTER,
42+
RUNTIME_EVM,
43+
RUNTIME_ACCESS_PATTERN,
44+
} runtime_type_t;
45+
46+
typedef enum {
47+
CALL_MESSAGE_CREATE_TOKEN,
48+
CALL_MESSAGE_TRANSFER,
49+
CALL_MESSAGE_BURN,
50+
CALL_MESSAGE_MINT,
51+
CALL_MESSAGE_FREEZE,
52+
} call_message_type_t;
53+
54+
typedef struct {
55+
uint64_t hi;
56+
uint64_t lo;
57+
} amount_t;
58+
59+
typedef struct {
60+
bytes_t token;
61+
} token_id_t;
62+
63+
typedef struct {
64+
amount_t amount;
65+
token_id_t token_id;
66+
} coins_t;
67+
68+
typedef struct {
69+
bytes_t address;
70+
} address_t;
71+
72+
// CallMessage structs
73+
typedef struct {
74+
coins_t coins;
75+
} burn_t;
76+
77+
typedef struct {
78+
coins_t coins;
79+
address_t mint_to_address;
80+
} mint_t;
81+
82+
typedef struct {
83+
address_t mint_to_address;
84+
} freeze_t;
85+
86+
typedef struct {
87+
call_message_type_t type;
88+
union {
89+
burn_t burn;
90+
mint_t mint;
91+
freeze_t freeze;
92+
};
93+
} call_message_t;
94+
95+
// Runtime structs
96+
typedef struct {
97+
runtime_type_t type;
98+
union {
99+
call_message_t call_message;
100+
};
101+
} runtime_t;
102+
103+
typedef struct {
104+
uint64_t gas[GAS_DIMENSIONS];
105+
} gas_t;
106+
107+
typedef struct {
108+
uint64_t max_priority_fee_bips;
109+
amount_t max_fee;
110+
bool has_gas_limit;
111+
gas_t gas_limit;
112+
uint64_t chain_id;
113+
} tx_details_t;
114+
115+
typedef struct {
116+
runtime_t runtime_call;
117+
uint64_t generation;
118+
tx_details_t tx_details;
119+
} unsigned_transaction_t;
120+
28121
typedef struct {
29122
schema_t schema;
123+
unsigned_transaction_t unsigned_transaction;
30124
} parser_tx_t;
31125

32126
#ifdef __cplusplus
Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
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 "metadata_reader.h"
18+
19+
#include <stdbool.h>
20+
#include <stddef.h>
21+
#include <stdint.h>
22+
23+
#include "borsh.h"
24+
#include "parser_common.h"
25+
#include "parser_impl.h"
26+
#include "parser_txdef.h"
27+
#include "zxmacros.h"
28+
29+
parser_error_t read_amount(parser_context_t *ctx, amount_t *amount) {
30+
CHECK_INPUT(ctx);
31+
CHECK_INPUT(amount);
32+
33+
CHECK_ERROR(read_u64(ctx, &amount->lo));
34+
CHECK_ERROR(read_u64(ctx, &amount->hi));
35+
printf("amount: 0x%llx\n", amount->lo);
36+
printf("amount: 0x%llx\n", amount->hi);
37+
38+
return parser_ok;
39+
}
40+
41+
parser_error_t read_token_id(parser_context_t *ctx, token_id_t *token_id) {
42+
CHECK_INPUT(ctx);
43+
CHECK_INPUT(token_id);
44+
45+
token_id->token.len = TOKEN_ID_SIZE;
46+
token_id->token.ptr = ctx->buffer + ctx->offset;
47+
CTX_CHECK_AND_ADVANCE(ctx, TOKEN_ID_SIZE)
48+
49+
print_buffer(&token_id->token, "token_id");
50+
51+
return parser_ok;
52+
}
53+
54+
parser_error_t read_address(parser_context_t *ctx, address_t *address) {
55+
CHECK_INPUT(ctx);
56+
CHECK_INPUT(address);
57+
58+
// TODO: check if size is correct
59+
address->address.len = ADDRESS_SIZE;
60+
address->address.ptr = ctx->buffer + ctx->offset;
61+
CTX_CHECK_AND_ADVANCE(ctx, ADDRESS_SIZE);
62+
63+
print_buffer(&address->address, "address");
64+
65+
return parser_ok;
66+
}
67+
68+
parser_error_t read_coins(parser_context_t *ctx, coins_t *coins) {
69+
CHECK_INPUT(ctx);
70+
CHECK_INPUT(coins);
71+
72+
CHECK_ERROR(read_amount(ctx, &coins->amount));
73+
CHECK_ERROR(read_token_id(ctx, &coins->token_id));
74+
75+
return parser_ok;
76+
}
77+
78+
parser_error_t read_mint(parser_context_t *ctx, mint_t *mint) {
79+
CHECK_INPUT(ctx);
80+
CHECK_INPUT(mint);
81+
82+
CHECK_ERROR(read_coins(ctx, &mint->coins));
83+
CHECK_ERROR(read_address(ctx, &mint->mint_to_address));
84+
85+
return parser_ok;
86+
}
87+
88+
parser_error_t read_call_message(parser_context_t *ctx, call_message_t *call_message) {
89+
CHECK_INPUT(ctx);
90+
CHECK_INPUT(call_message);
91+
92+
// read call message type
93+
CHECK_ERROR(read_u8(ctx, (uint8_t *)&call_message->type));
94+
switch (call_message->type) {
95+
case CALL_MESSAGE_CREATE_TOKEN:
96+
print_string("CALL_MESSAGE_CREATE_TOKEN NOT IMPLEMENTED");
97+
return parser_unexpected_error;
98+
case CALL_MESSAGE_TRANSFER:
99+
print_string("CALL_MESSAGE_TRANSFER NOT IMPLEMENTED");
100+
return parser_unexpected_error;
101+
case CALL_MESSAGE_BURN:
102+
print_string("CALL_MESSAGE_BURN NOT IMPLEMENTED");
103+
return parser_unexpected_error;
104+
case CALL_MESSAGE_MINT:
105+
print_string("CALL_MESSAGE_MINT");
106+
CHECK_ERROR(read_mint(ctx, &call_message->mint));
107+
break;
108+
default:
109+
printf("Unexpected call message type: %d\n", call_message->type);
110+
return parser_unexpected_error;
111+
}
112+
return parser_ok;
113+
}
114+
115+
parser_error_t read_runtime(parser_context_t *ctx, runtime_t *runtime) {
116+
CHECK_INPUT(ctx);
117+
CHECK_INPUT(runtime);
118+
119+
// read runtime type
120+
CHECK_ERROR(read_u8(ctx, (uint8_t *)&runtime->type));
121+
printf("runtime type: %d\n", runtime->type);
122+
switch (runtime->type) {
123+
case RUNTIME_BANK:
124+
print_string("RUNTIME_BANK");
125+
CHECK_ERROR(read_call_message(ctx, &runtime->call_message));
126+
break;
127+
default:
128+
printf("Unexpected runtime type: %d\n", runtime->type);
129+
return parser_unexpected_error;
130+
}
131+
132+
return parser_ok;
133+
}
134+
135+
parser_error_t read_tx_details(parser_context_t *ctx, tx_details_t *tx_details) {
136+
CHECK_INPUT(ctx);
137+
CHECK_INPUT(tx_details);
138+
// read max priority fee bips
139+
CHECK_ERROR(read_u64(ctx, &tx_details->max_priority_fee_bips));
140+
printf("max priority fee bips: 0x%llx\n", tx_details->max_priority_fee_bips);
141+
142+
// read max fee
143+
CHECK_ERROR(read_amount(ctx, &tx_details->max_fee));
144+
printf("max fee: %llu\n", tx_details->max_fee.lo);
145+
printf("max fee: %llu\n", tx_details->max_fee.hi);
146+
147+
// read gas limit
148+
CHECK_ERROR(read_u8(ctx, (uint8_t *)&tx_details->has_gas_limit));
149+
printf("has gas limit: %d\n", tx_details->has_gas_limit);
150+
if (tx_details->has_gas_limit) {
151+
for (int i = 0; i < GAS_DIMENSIONS; i++) {
152+
CHECK_ERROR(read_u64(ctx, &tx_details->gas_limit.gas[i]));
153+
printf("gas[%d]: %llu\n", i, tx_details->gas_limit.gas[i]);
154+
}
155+
}
156+
157+
// read chain id
158+
CHECK_ERROR(read_u64(ctx, &tx_details->chain_id));
159+
printf("chain id: 0x%llx\n", tx_details->chain_id);
160+
161+
return parser_ok;
162+
}
163+
164+
parser_error_t unsigned_transaction_read(parser_context_t *ctx, parser_tx_t *txObj) {
165+
CHECK_INPUT(ctx);
166+
CHECK_INPUT(txObj);
167+
168+
// read runtime
169+
CHECK_ERROR(read_runtime(ctx, &txObj->unsigned_transaction.runtime_call));
170+
//read generation
171+
CHECK_ERROR(read_u64(ctx, &txObj->unsigned_transaction.generation));
172+
printf("generation: 0x%llx\n", txObj->unsigned_transaction.generation);
173+
174+
// read tx details
175+
CHECK_ERROR(read_tx_details(ctx, &txObj->unsigned_transaction.tx_details));
176+
177+
// TODO: check that we have consumed all data
178+
// if (ctx->offset != ctx->bufferLen) {
179+
// return parser_unexpected_error;
180+
// }
181+
182+
return parser_ok;
183+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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 <stdint.h>
24+
25+
#include "parser_common.h"
26+
27+
parser_error_t unsigned_transaction_read(parser_context_t *ctx, parser_tx_t *txObj);
28+
29+
#ifdef __cplusplus
30+
}
31+
#endif

0 commit comments

Comments
 (0)