Skip to content

Commit

Permalink
support for new transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
ftheirs committed Feb 6, 2024
1 parent fbb0953 commit f448a67
Show file tree
Hide file tree
Showing 16 changed files with 1,214 additions and 419 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ file(GLOB_RECURSE LIB_SRC
${CMAKE_CURRENT_SOURCE_DIR}/app/src/parser_impl_txn.c
${CMAKE_CURRENT_SOURCE_DIR}/app/src/crypto_helper.c
${CMAKE_CURRENT_SOURCE_DIR}/app/src/leb128.c
${CMAKE_CURRENT_SOURCE_DIR}/app/src/txn_validator.c
${CMAKE_CURRENT_SOURCE_DIR}/app/src/txn_delegation.c
)

add_library(app_lib STATIC ${LIB_SRC})
Expand Down
54 changes: 54 additions & 0 deletions app/src/allowed_transactions.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*******************************************************************************
* (c) 2018 - 2024 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 "parser_txdef.h"

#include <stdint.h>

static const txn_types_t allowed_txn[] = {
{"tx_bond.wasm", Bond},
{"tx_unbond.wasm", Unbond},
{"tx_redelegate.wasm", Redelegate},
{"tx_init_account.wasm", InitAccount},
{"tx_init_proposal.wasm", InitProposal},
{"tx_vote_proposal.wasm", VoteProposal},
{"tx_become_validator.wasm", BecomeValidator},
{"tx_reveal_pk.wasm", RevealPubkey},
{"tx_transfer.wasm", Transfer},
{"tx_update_account.wasm", UpdateVP},
{"tx_withdraw.wasm", Withdraw},
{"tx_change_validator_commission.wasm", CommissionChange},
{"tx_unjail_validator.wasm", UnjailValidator},
{"tx_ibc.wasm", IBC},
{"tx_deactivate_validator.wasm", DeactivateValidator},
{"tx_reactivate_validator.wasm", ReactivateValidator},
{"tx_claim_rewards.wasm", ClaimRewards},
{"tx_resign_steward.wasm", ResignSteward},
{"tx_change_consensus_key.wasm", ChangeConsensusKey},
{"tx_update_steward_commission.wasm", UpdateStewardCommission},
{"tx_change_validator_metadata.wasm", ChangeValidatorMetadata},
};

static const uint32_t allowed_txn_len = sizeof(allowed_txn) / sizeof(allowed_txn[0]);

#ifdef __cplusplus
}
#endif
1 change: 1 addition & 0 deletions app/src/coin.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ extern "C" {
#define PK_LEN_25519_PLUS_TAG 33u
#define SIG_LEN_25519_PLUS_TAG 65u

#define ADDRESS_LEN_BYTES 21

#define ADDRESS_LEN_MAINNET 42u
#define ADDRESS_LEN_TESTNET 45u
Expand Down
52 changes: 50 additions & 2 deletions app/src/parser_impl.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ parser_error_t getNumItems(const parser_context_t *ctx, uint8_t *numItems) {
break;
}
case InitProposal: {
*numItems = (app_mode_expert() ? INIT_PROPOSAL_EXPERT_PARAMS : INIT_PROPOSAL_NORMAL_PARAMS) + ctx->tx_obj->initProposal.has_id;
*numItems = (app_mode_expert() ? INIT_PROPOSAL_EXPERT_PARAMS : INIT_PROPOSAL_NORMAL_PARAMS);
break;
}
case VoteProposal: {
Expand Down Expand Up @@ -95,10 +95,13 @@ parser_error_t getNumItems(const parser_context_t *ctx, uint8_t *numItems) {
case UpdateVP: {
const uint32_t pubkeys_num = ctx->tx_obj->updateVp.number_of_pubkeys;
const uint8_t has_threshold = ctx->tx_obj->updateVp.has_threshold;
*numItems = (uint8_t) ((app_mode_expert() ? UPDATE_VP_EXPERT_PARAMS : UPDATE_VP_NORMAL_PARAMS) + pubkeys_num + has_threshold);
const uint8_t has_vp_code = ctx->tx_obj->updateVp.has_vp_code;
*numItems = (uint8_t) ((app_mode_expert() ? UPDATE_VP_EXPERT_PARAMS : UPDATE_VP_NORMAL_PARAMS) + pubkeys_num + has_threshold + has_vp_code);
break;
}

case ReactivateValidator:
case DeactivateValidator:
case UnjailValidator:
*numItems = (app_mode_expert() ? UNJAIL_VALIDATOR_EXPERT_PARAMS : UNJAIL_VALIDATOR_NORMAL_PARAMS);
break;
Expand All @@ -107,6 +110,51 @@ parser_error_t getNumItems(const parser_context_t *ctx, uint8_t *numItems) {
*numItems = (app_mode_expert() ? IBC_EXPERT_PARAMS : IBC_NORMAL_PARAMS);
break;

case Redelegate:
*numItems = (app_mode_expert() ? REDELEGATE_EXPERT_PARAMS : REDELEGATE_NORMAL_PARAMS);
break;

case ClaimRewards:
*numItems = (app_mode_expert() ? CLAIM_REWARDS_EXPERT_PARAMS : CLAIM_REWARDS_NORMAL_PARAMS) + ctx->tx_obj->withdraw.has_source;
break;

case ResignSteward:
*numItems = (app_mode_expert() ? RESIGN_STEWARD_EXPERT_PARAMS : RESIGN_STEWARD_NORMAL_PARAMS);
break;

case ChangeConsensusKey:
*numItems = (app_mode_expert() ? CHANGE_CONSENSUS_KEY_EXPERT_PARAMS : CHANGE_CONSENSUS_KEY_NORMAL_PARAMS);
break;

case UpdateStewardCommission:
*numItems = (app_mode_expert() ? UPDATE_STEWARD_COMMISSION_EXPERT_PARAMS : UPDATE_STEWARD_COMMISSION_NORMAL_PARAMS) + 2 * ctx->tx_obj->updateStewardCommission.commissionLen;
break;

case ChangeValidatorMetadata: {
*numItems = app_mode_expert() ? CHANGE_VALIDATOR_METADATA_EXPERT_PARAMS : CHANGE_VALIDATOR_METADATA_NORMAL_PARAMS;

if (ctx->tx_obj->metadataChange.email.ptr != NULL) {
(*numItems)++;
}
if (ctx->tx_obj->metadataChange.description.ptr != NULL) {
(*numItems)++;
}
if (ctx->tx_obj->metadataChange.website.ptr != NULL) {
(*numItems)++;
}
if (ctx->tx_obj->metadataChange.discord_handle.ptr != NULL) {
(*numItems)++;
}
if (ctx->tx_obj->metadataChange.avatar.ptr != NULL) {
(*numItems)++;
}
if (ctx->tx_obj->metadataChange.has_commission_rate) {
(*numItems)++;
}

break;
}

default:
break;
}
Expand Down
74 changes: 16 additions & 58 deletions app/src/parser_impl_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@
#include "parser_impl_common.h"
#include "leb128.h"

#define SIGN_MASK 0x80000000
#define SCALE_SHIFT 16

parser_error_t readByte(parser_context_t *ctx, uint8_t *byte) {
if (byte == NULL || ctx->offset >= ctx->bufferLen) {
return parser_unexpected_error;
Expand Down Expand Up @@ -59,61 +56,6 @@ parser_error_t readUint64(parser_context_t *ctx, uint64_t *value) {
return parser_ok;
}

parser_error_t readUint256(parser_context_t *ctx, uint256_t *value) {
if (value == NULL || ctx->offset + sizeof(uint256_t) > ctx->bufferLen) {
return parser_unexpected_error;
}

MEMCPY(value, ctx->buffer + ctx->offset, sizeof(uint256_t));
ctx->offset += sizeof(uint256_t);
return parser_ok;
}

zxerr_t recover_decimal(const uint8_t* bytes, int64_t* num, uint32_t* scale) {
if (bytes == NULL) {
return zxerr_unknown; // Invalid byte sequence
}

uint32_t flag = 0;
for (int i = 0; i < 4; i++) {
flag |= ((uint32_t)bytes[i]) << (8 * i);
}

*scale = (flag >> SCALE_SHIFT);
uint8_t is_negative = (flag & SIGN_MASK) != 0;

uint32_t hi = 0, lo = 0, mid = 0;
for (int i = 0; i < 4; i++) {
hi |= ((uint32_t)bytes[4 + i]) << (8 * i);
lo |= ((uint32_t)bytes[8 + i]) << (8 * i);
mid |= ((uint32_t)bytes[12 + i]) << (8 * i);
}

uint64_t m = ((uint64_t)hi) << 32 | lo;
m |= ((uint64_t)mid) << 32;

if (is_negative) {
m = ~m + 1; // Two's complement negation
}

*num = (int64_t)m;
return zxerr_ok;
}

parser_error_t readDecimal(parser_context_t *ctx, serialized_decimal *value) {
if (value == NULL || ctx->offset + sizeof(serialized_decimal) > ctx->bufferLen) {
return parser_unexpected_error;
}
uint8_t raw_decimal[sizeof(serialized_decimal)] = {0};
MEMCPY(raw_decimal, ctx->buffer + ctx->offset, sizeof(serialized_decimal));

recover_decimal((const uint8_t *) &raw_decimal, &value->num, &value->scale);

ctx->offset += sizeof(serialized_decimal);
return parser_ok;
}


parser_error_t readBytes(parser_context_t *ctx, const uint8_t **output, uint16_t outputLen) {
if (ctx->offset + outputLen > ctx->bufferLen) {
return parser_unexpected_buffer_end;
Expand Down Expand Up @@ -148,3 +90,19 @@ parser_error_t checkTag(parser_context_t *ctx, uint8_t expectedTag) {
}
return parser_ok;
}

parser_error_t readPubkey(parser_context_t *ctx, bytes_t *pubkey) {
if (ctx == NULL || pubkey == NULL) {
return parser_unexpected_buffer_end;
}

if (ctx->offset >= ctx->bufferLen) {
return parser_unexpected_buffer_end;
}

const uint8_t pkType = *(ctx->buffer + ctx->offset);
//Pubkey must include pkType (needed for encoding)
pubkey->len = 1 + (pkType == key_ed25519 ? PK_LEN_25519 : COMPRESSED_SECP256K1_PK_LEN);
CHECK_ERROR(readBytes(ctx, &pubkey->ptr, pubkey->len))
return parser_ok;
}
30 changes: 24 additions & 6 deletions app/src/parser_impl_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ extern "C" {
#define INIT_ACCOUNT_NORMAL_PARAMS 3
#define INIT_ACCOUNT_EXPERT_PARAMS 8

#define INIT_PROPOSAL_NORMAL_PARAMS 7
#define INIT_PROPOSAL_EXPERT_PARAMS 12
#define INIT_PROPOSAL_NORMAL_PARAMS 8
#define INIT_PROPOSAL_EXPERT_PARAMS 13

#define VOTE_PROPOSAL_NORMAL_PARAMS 4
#define VOTE_PROPOSAL_EXPERT_PARAMS 9
Expand All @@ -48,8 +48,8 @@ extern "C" {
#define TRANSFER_NORMAL_PARAMS 4
#define TRANSFER_EXPERT_PARAMS 9

#define UPDATE_VP_NORMAL_PARAMS 3
#define UPDATE_VP_EXPERT_PARAMS 8
#define UPDATE_VP_NORMAL_PARAMS 2
#define UPDATE_VP_EXPERT_PARAMS 7

#define WITHDRAW_NORMAL_PARAMS 2
#define WITHDRAW_EXPERT_PARAMS 7
Expand All @@ -63,15 +63,33 @@ extern "C" {
#define IBC_NORMAL_PARAMS 8
#define IBC_EXPERT_PARAMS 13

#define REDELEGATE_NORMAL_PARAMS 5
#define REDELEGATE_EXPERT_PARAMS 10

#define CLAIM_REWARDS_NORMAL_PARAMS 2
#define CLAIM_REWARDS_EXPERT_PARAMS 7

#define RESIGN_STEWARD_NORMAL_PARAMS 2
#define RESIGN_STEWARD_EXPERT_PARAMS 7

#define CHANGE_CONSENSUS_KEY_NORMAL_PARAMS 3
#define CHANGE_CONSENSUS_KEY_EXPERT_PARAMS 8

#define UPDATE_STEWARD_COMMISSION_NORMAL_PARAMS 2
#define UPDATE_STEWARD_COMMISSION_EXPERT_PARAMS 7

#define CHANGE_VALIDATOR_METADATA_NORMAL_PARAMS 2
#define CHANGE_VALIDATOR_METADATA_EXPERT_PARAMS 7

parser_error_t readByte(parser_context_t *ctx, uint8_t *byte);
parser_error_t readBytes(parser_context_t *ctx, const uint8_t **output, uint16_t outputLen);
parser_error_t readUint16(parser_context_t *ctx, uint16_t *value);
parser_error_t readUint32(parser_context_t *ctx, uint32_t *value);
parser_error_t readUint64(parser_context_t *ctx, uint64_t *value);
parser_error_t readUint256(parser_context_t *ctx, uint256_t *value);
parser_error_t readDecimal(parser_context_t *ctx, serialized_decimal *value);

parser_error_t readFieldSize(parser_context_t *ctx, uint32_t *size);
parser_error_t checkTag(parser_context_t *ctx, uint8_t expectedTag);
parser_error_t readPubkey(parser_context_t *ctx, bytes_t *pubkey);

parser_error_t readToken(const bytes_t *token, const char **symbol);
parser_error_t readAddress(bytes_t pubkeyHash, char *address, uint16_t addressLen);
Expand Down
Loading

0 comments on commit f448a67

Please sign in to comment.