|
| 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 | +} |
0 commit comments