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

Murisi/ledger v0.30.2 #28

Closed
wants to merge 21 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
2033fe4
Added missing fields.
murisi Jan 31, 2024
6e244a5
Added support for redelegate transactions.
murisi Feb 3, 2024
0ecc2d5
Added support for reactivate validator transactions.
murisi Feb 3, 2024
4fe08d1
Implemented change metadata transaction.
murisi Feb 3, 2024
54d183b
Added support for the claim rewards transaction.
murisi Feb 4, 2024
e494b5a
Added support for deactivate validator transactions.
murisi Feb 4, 2024
46382d0
Added support for change consensus key transactions.
murisi Feb 4, 2024
c760d49
Added support for resign steward transactions.
murisi Feb 4, 2024
6b89a8b
Fixed the support for vote proposal transactions.
murisi Feb 4, 2024
6109f18
Added support for update steward commission transactions.
murisi Feb 4, 2024
3a92811
Fixed expert mode printing of update account and adjusted test vectors.
murisi Feb 4, 2024
4d944ce
Fixed expert mode printing update steward commission.
murisi Feb 4, 2024
22ce62f
Updated some signing test vectors.
murisi Feb 5, 2024
6fe0687
Support extra data sections with original bytes instead of just their…
murisi Feb 6, 2024
315d01a
Now printing out the memo field.
murisi Feb 7, 2024
80fad5c
Now successfully parsing init proposal transactions.
murisi Feb 8, 2024
4330e37
Now printing out more details for init proposal transactions.
murisi Feb 8, 2024
f5ab5a3
Now propagate a yield return code for proposal type parsing and print…
murisi Feb 8, 2024
f2c8b20
Include the memo section hash in the signature if its there. Updated …
murisi Feb 8, 2024
7cf3c72
Now print the missing case of PGF IBC targets.
murisi Feb 9, 2024
e2f78ff
Modified the test vectors to also include pre-signed transactions.
murisi Feb 9, 2024
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 app/src/common/parser_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ typedef enum {
paser_unknown_transaction,
parser_decimal_too_big,
parser_invalid_output_buffer,

parser_yield,
} parser_error_t;

typedef struct {
Expand Down
10 changes: 9 additions & 1 deletion app/src/crypto.c
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,15 @@ zxerr_t crypto_sign(const parser_tx_t *txObj, uint8_t *output, uint16_t outputLe
CHECK_ZXERR(crypto_hashDataSection(data, dataHash, HASH_LEN))
section_hashes.hashesLen += 2;
signature_section.hashes.hashesLen += 2;

// Include the memo seection hash in the siggnature if it's there
if (txObj->transaction.header.memoSection) {
const section_t *memo = txObj->transaction.header.memoSection;
uint8_t *memoHash = section_hashes.hashes.ptr + (section_hashes.hashesLen * HASH_LEN);
section_hashes.indices.ptr[section_hashes.hashesLen] = memo->idx;
CHECK_ZXERR(crypto_hashExtraDataSection(memo, memoHash, HASH_LEN))
section_hashes.hashesLen++;
signature_section.hashes.hashesLen++;
}

// Hash the eligible signature sections
for (uint32_t i = 0; i < txObj->transaction.sections.signaturesLen; i++) {
Expand Down
29 changes: 25 additions & 4 deletions app/src/crypto_helper.c
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,27 @@ zxerr_t crypto_sha256(const uint8_t *input, uint16_t inputLen, uint8_t *output,
return zxerr_ok;
}

zxerr_t crypto_computeCodeHash(section_t *extraData) {
if (extraData == NULL) {
return zxerr_invalid_crypto_settings;
}

if (extraData->commitmentDiscriminant) {
#if defined(TARGET_NANOS) || defined(TARGET_NANOS2) || defined(TARGET_NANOX) || defined(TARGET_STAX)
cx_sha256_t sha256 = {0};
cx_sha256_init(&sha256);
CHECK_CX_OK(cx_sha256_update(&sha256, extraData->bytes.ptr, extraData->bytes.len));
CHECK_CX_OK(cx_sha256_final(&sha256, extraData->bytes_hash));
#else
picohash_ctx_t sha256 = {0};
picohash_init_sha256(&sha256);
picohash_update(&sha256, extraData->bytes.ptr, extraData->bytes.len);
picohash_final(&sha256, extraData->bytes_hash);
#endif
}
return zxerr_ok;
}

zxerr_t crypto_hashExtraDataSection(const section_t *extraData, uint8_t *output, uint32_t outputLen) {
if (extraData == NULL || output == NULL || outputLen < CX_SHA256_SIZE) {
return zxerr_invalid_crypto_settings;
Expand All @@ -131,7 +152,7 @@ zxerr_t crypto_hashExtraDataSection(const section_t *extraData, uint8_t *output,
cx_sha256_init(&sha256);
CHECK_CX_OK(cx_sha256_update(&sha256, &extraData->discriminant, 1));
CHECK_CX_OK(cx_sha256_update(&sha256, extraData->salt.ptr, extraData->salt.len));
CHECK_CX_OK(cx_sha256_update(&sha256, extraData->bytes.ptr, extraData->bytes.len));
CHECK_CX_OK(cx_sha256_update(&sha256, extraData->bytes_hash, sizeof(extraData->bytes_hash)));
uint8_t has_tag = (extraData->tag.ptr == NULL) ? 0 : 1;
CHECK_CX_OK(cx_sha256_update(&sha256, &has_tag, 1));
CHECK_CX_OK(cx_sha256_update(&sha256, (uint8_t*) &extraDataTagLen, has_tag*sizeof(extraDataTagLen)));
Expand All @@ -142,7 +163,7 @@ zxerr_t crypto_hashExtraDataSection(const section_t *extraData, uint8_t *output,
picohash_init_sha256(&sha256);
picohash_update(&sha256, &extraData->discriminant, 1);
picohash_update(&sha256, extraData->salt.ptr, extraData->salt.len);
picohash_update(&sha256, extraData->bytes.ptr, extraData->bytes.len);
picohash_update(&sha256, extraData->bytes_hash, sizeof(extraData->bytes_hash));
uint8_t has_tag = (extraData->tag.ptr == NULL) ? 0 : 1;
picohash_update(&sha256, &has_tag, 1);
picohash_update(&sha256, (uint8_t*) &extraDataTagLen, has_tag*sizeof(extraDataTagLen));
Expand Down Expand Up @@ -191,7 +212,7 @@ zxerr_t crypto_hashCodeSection(const section_t *code, uint8_t *output, uint32_t
cx_sha256_init(&sha256);
CHECK_CX_OK(cx_sha256_update(&sha256, &code->discriminant, 1));
CHECK_CX_OK(cx_sha256_update(&sha256, code->salt.ptr, code->salt.len));
CHECK_CX_OK(cx_sha256_update(&sha256, code->bytes.ptr, code->bytes.len));
CHECK_CX_OK(cx_sha256_update(&sha256, code->bytes_hash, sizeof(code->bytes_hash)));
uint8_t has_tag = (code->tag.ptr == NULL) ? 0 : 1;
CHECK_CX_OK(cx_sha256_update(&sha256, &has_tag, 1));
CHECK_CX_OK(cx_sha256_update(&sha256, (uint8_t*) &codeTagLen, has_tag*sizeof(codeTagLen)));
Expand All @@ -202,7 +223,7 @@ zxerr_t crypto_hashCodeSection(const section_t *code, uint8_t *output, uint32_t
picohash_init_sha256(&sha256);
picohash_update(&sha256, &code->discriminant, 1);
picohash_update(&sha256, code->salt.ptr, code->salt.len);
picohash_update(&sha256, code->bytes.ptr, code->bytes.len);
picohash_update(&sha256, code->bytes_hash, sizeof(code->bytes_hash));
uint8_t has_tag = (code->tag.ptr == NULL) ? 0 : 1;
picohash_update(&sha256, &has_tag, 1);
picohash_update(&sha256, (uint8_t*) &codeTagLen, has_tag*sizeof(codeTagLen));
Expand Down
1 change: 1 addition & 0 deletions app/src/crypto_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ uint8_t crypto_encodePubkey_ed25519(uint8_t *buffer, uint16_t bufferLen, const u
zxerr_t crypto_sha256(const uint8_t *input, uint16_t inputLen,
uint8_t *output, uint16_t outputLen);

zxerr_t crypto_computeCodeHash(section_t *extraData);
zxerr_t crypto_hashDataSection(const section_t *data, uint8_t *output, uint32_t outputLen);
zxerr_t crypto_hashCodeSection(const section_t *section, uint8_t *output, uint32_t outputLen);
zxerr_t crypto_hashExtraDataSection(const section_t *section, uint8_t *output, uint32_t outputLen);
Expand Down
38 changes: 35 additions & 3 deletions app/src/parser_impl.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@ parser_error_t getNumItems(const parser_context_t *ctx, uint8_t *numItems) {
switch (ctx->tx_obj->typeTx) {
case Unbond:
case Bond:
*numItems = (app_mode_expert() ? BOND_EXPERT_PARAMS : BOND_NORMAL_PARAMS) + ctx->tx_obj->bond.has_source;
*numItems = (app_mode_expert() ? BOND_EXPERT_PARAMS : BOND_NORMAL_PARAMS) + ctx->tx_obj->bond.has_source;
break;

case ChangeValidatorMetadata:
*numItems = (app_mode_expert() ? CHANGE_VALIDATOR_METADATA_EXPERT_PARAMS : CHANGE_VALIDATOR_METADATA_NORMAL_PARAMS) + (ctx->tx_obj->metadataChange.email.ptr != NULL) + (ctx->tx_obj->metadataChange.description.ptr != NULL) + (ctx->tx_obj->metadataChange.website.ptr != NULL) + (ctx->tx_obj->metadataChange.discord_handle.ptr != NULL) + (ctx->tx_obj->metadataChange.avatar.ptr != NULL) + ctx->tx_obj->metadataChange.has_commission_rate;
break;

case Custom:
Expand All @@ -53,13 +57,17 @@ parser_error_t getNumItems(const parser_context_t *ctx, uint8_t *numItems) {
}
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 InitAccount: {
const uint32_t pubkeys_num = ctx->tx_obj->initAccount.number_of_pubkeys;
*numItems = (uint8_t)((app_mode_expert() ? INIT_ACCOUNT_EXPERT_PARAMS : INIT_ACCOUNT_NORMAL_PARAMS) + pubkeys_num);
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) + ctx->tx_obj->initProposal.proposal_type_entries;
break;
}
case VoteProposal: {
Expand All @@ -70,11 +78,30 @@ parser_error_t getNumItems(const parser_context_t *ctx, uint8_t *numItems) {
case RevealPubkey:
*numItems = (app_mode_expert() ? REVEAL_PUBKEY_EXPERT_PARAMS : REVEAL_PUBKEY_NORMAL_PARAMS);
break;
case Redelegate:
*numItems = (app_mode_expert() ? REDELEGATE_EXPERT_PARAMS : REDELEGATE_NORMAL_PARAMS);
break;
case ReactivateValidator:
*numItems = (app_mode_expert() ? REACTIVATE_VALIDATOR_EXPERT_PARAMS : REACTIVATE_VALIDATOR_NORMAL_PARAMS);
break;
case DeactivateValidator:
*numItems = (app_mode_expert() ? DEACTIVATE_VALIDATOR_EXPERT_PARAMS : DEACTIVATE_VALIDATOR_NORMAL_PARAMS);
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 Withdraw:
*numItems = (app_mode_expert() ? WITHDRAW_EXPERT_PARAMS : WITHDRAW_NORMAL_PARAMS) + ctx->tx_obj->withdraw.has_source;
break;

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

case CommissionChange:
*numItems = (app_mode_expert() ? COMMISSION_CHANGE_EXPERT_PARAMS : COMMISSION_CHANGE_NORMAL_PARAMS);
break;
Expand All @@ -95,7 +122,8 @@ 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;
}

Expand All @@ -111,6 +139,10 @@ parser_error_t getNumItems(const parser_context_t *ctx, uint8_t *numItems) {
break;
}

if (ctx->tx_obj->transaction.header.memoSection != NULL) {
(*numItems)++;
}

if(app_mode_expert() && ctx->tx_obj->transaction.header.fees.symbol == NULL) {
(*numItems)++;
}
Expand Down
20 changes: 20 additions & 0 deletions app/src/parser_impl_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,16 @@
#define SIGN_MASK 0x80000000
#define SCALE_SHIFT 16

bool isAllZeroes(const void *buf, size_t n) {
uint8_t *p = (uint8_t *) buf;
for (size_t i = 0; i < n; ++i) {
if (p[i]) {
return false;
}
}
return true;
}

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 @@ -69,6 +79,16 @@ parser_error_t readUint256(parser_context_t *ctx, uint256_t *value) {
return parser_ok;
}

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

MEMCPY(value, ctx->buffer + ctx->offset, sizeof(int256_t));
ctx->offset += sizeof(int256_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
Expand Down
36 changes: 34 additions & 2 deletions app/src/parser_impl_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,29 @@
extern "C" {
#endif

#define ADDRESS_LEN_BYTES 21

#define BOND_NORMAL_PARAMS 3
#define BOND_EXPERT_PARAMS 8

#define CHANGE_VALIDATOR_METADATA_NORMAL_PARAMS 2
#define CHANGE_VALIDATOR_METADATA_EXPERT_PARAMS 7

#define UPDATE_STEWARD_COMMISSION_NORMAL_PARAMS 2
#define UPDATE_STEWARD_COMMISSION_EXPERT_PARAMS 7

#define REACTIVATE_VALIDATOR_NORMAL_PARAMS 2
#define REACTIVATE_VALIDATOR_EXPERT_PARAMS 7

#define DEACTIVATE_VALIDATOR_NORMAL_PARAMS 2
#define DEACTIVATE_VALIDATOR_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 CUSTOM_NORMAL_PARAMS 1
#define CUSTOM_EXPERT_PARAMS 6

Expand All @@ -45,15 +65,21 @@ extern "C" {
#define REVEAL_PUBKEY_NORMAL_PARAMS 2
#define REVEAL_PUBKEY_EXPERT_PARAMS 7

#define REDELEGATE_NORMAL_PARAMS 5
#define REDELEGATE_EXPERT_PARAMS 10

#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

#define CLAIM_REWARDS_NORMAL_PARAMS 2
#define CLAIM_REWARDS_EXPERT_PARAMS 7

#define COMMISSION_CHANGE_NORMAL_PARAMS 3
#define COMMISSION_CHANGE_EXPERT_PARAMS 8

Expand All @@ -63,23 +89,29 @@ extern "C" {
#define IBC_NORMAL_PARAMS 8
#define IBC_EXPERT_PARAMS 13

bool isAllZeroes(const void *buf, size_t n);
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 readInt256(parser_context_t *ctx, int256_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 readToken(const bytes_t *token, const char **symbol);
parser_error_t readAddress(bytes_t pubkeyHash, char *address, uint16_t addressLen);
parser_error_t readAddressBytes(parser_context_t *ctx, bytes_t *address);
parser_error_t readVote(bytes_t *vote, yay_vote_type_e type, char *strVote, uint16_t strVoteLen);

parser_error_t readHeader(parser_context_t *ctx, parser_tx_t *v);
parser_error_t readSections(parser_context_t *ctx, parser_tx_t *v);
parser_error_t validateTransactionParams(parser_tx_t *txObj);
parser_error_t readProposalType(parser_context_t *ctx, parser_tx_t *v, uint16_t *pos, uint16_t target,
char *outKey, uint16_t outKeyLen, char *outVal, uint16_t outValLen,
uint8_t pageIdx, uint8_t *pageCount);

#ifdef __cplusplus
}
Expand Down
Loading