Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Namada v0.30.2 #29

Merged
merged 4 commits into from
Feb 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
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
2 changes: 1 addition & 1 deletion app/Makefile.version
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ APPVERSION_M=0
# This is the `spec_version` field of `Runtime`
APPVERSION_N=0
# This is the patch version of this release
APPVERSION_P=16
APPVERSION_P=17
55 changes: 55 additions & 0 deletions app/src/allowed_transactions.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*******************************************************************************
* (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},
{"tx_bridge_pool.wasm", BridgePoolTransfer},
};

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

#ifdef __cplusplus
}
#endif
2 changes: 2 additions & 0 deletions app/src/coin.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ extern "C" {
#define COMPRESSED_SECP256K1_PK_LEN 33u
#define SECP256K1_SK_LEN 32u
#define SCALAR_LEN_SECP256K1 32u
#define ETH_ADDRESS_LEN 20u

#define SK_LEN_25519 32u
#define SCALAR_LEN_ED25519 32u
Expand All @@ -50,6 +51,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
56 changes: 54 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,55 @@ 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;
}

case BridgePoolTransfer:
*numItems = app_mode_expert() ? BRIDGE_POOL_TRANSFER_EXPERT_PARAMS : BRIDGE_POOL_TRANSFER_NORMAL_PARAMS;
break;

default:
break;
}
Expand Down
89 changes: 31 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 All @@ -139,6 +81,21 @@ parser_error_t readFieldSize(parser_context_t *ctx, uint32_t *size) {
return parser_ok;
}

parser_error_t readFieldSizeU16(parser_context_t *ctx, uint16_t *size) {
uint8_t consumed = 0;
uint64_t tmpSize = 0;

decodeLEB128(ctx->buffer + ctx->offset, 10, &consumed, &tmpSize);
ctx->offset += consumed;

if (tmpSize > UINT16_MAX) {
return parser_value_out_of_range;
}
*size = (uint16_t)tmpSize;

return parser_ok;
}

parser_error_t checkTag(parser_context_t *ctx, uint8_t expectedTag) {
uint8_t tmpTag = 0;
CHECK_ERROR(readByte(ctx, &tmpTag))
Expand All @@ -148,3 +105,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;
}
34 changes: 28 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,37 @@ 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

#define BRIDGE_POOL_TRANSFER_NORMAL_PARAMS 9
#define BRIDGE_POOL_TRANSFER_EXPERT_PARAMS 14

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 readFieldSizeU16(parser_context_t *ctx, uint16_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
Loading