Skip to content

Commit

Permalink
Fully parse Addresses everywhere.
Browse files Browse the repository at this point in the history
  • Loading branch information
murisi committed Feb 19, 2024
1 parent be591fa commit aa5bb4c
Show file tree
Hide file tree
Showing 10 changed files with 202 additions and 223 deletions.
2 changes: 1 addition & 1 deletion app/src/crypto.c
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ zxerr_t crypto_hashSigSection(const signature_section_t *signature_section, cons
break;
}
case Address:
CHECK_CX_OK(cx_sha256_update(&sha256, signature_section->address.ptr, signature_section->address.len));
CHECK_CX_OK(cx_sha256_update(&sha256, signature_section->addressBytes.ptr, signature_section->addressBytes.len));
break;

default:
Expand Down
5 changes: 3 additions & 2 deletions app/src/parser_impl_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,10 @@ 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 readToken(const AddressAlt *token, const char **symbol);
parser_error_t readAddress(bytes_t pubkeyHash, char *address, uint16_t addressLen);
parser_error_t encodeAddress(AddressAlt *addr, char *address, uint16_t addressLen);
parser_error_t readAddressAlt(parser_context_t *ctx, AddressAlt *obj);
parser_error_t encodeAddress(const AddressAlt *addr, char *address, uint16_t addressLen);
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);
Expand Down
80 changes: 34 additions & 46 deletions app/src/parser_impl_txn.c
Original file line number Diff line number Diff line change
Expand Up @@ -910,14 +910,14 @@ parser_error_t readIbcTokenHash(parser_context_t *ctx, IbcTokenHash *obj) {
return parser_ok;
}

parser_error_t readToken(const bytes_t *token, const char **symbol) {
parser_error_t readToken(const AddressAlt *token, const char **symbol) {
if (token == NULL || symbol == NULL) {
return parser_unexpected_value;
}

// Convert token to address
char address[53] = {0};
CHECK_ERROR(readAddress(*token, address, sizeof(address)))
CHECK_ERROR(encodeAddress(token, address, sizeof(address)))

*symbol = NULL;

Expand Down Expand Up @@ -990,7 +990,7 @@ parser_error_t readAddress(bytes_t pubkeyHash, char *address, uint16_t addressLe
return parser_ok;
}

parser_error_t encodeAddress(AddressAlt *addr, char *address, uint16_t addressLen) {
parser_error_t encodeAddress(const AddressAlt *addr, char *address, uint16_t addressLen) {
uint8_t tmpBuffer[ADDRESS_LEN_BYTES] = {0};

switch (addr->tag) {
Expand Down Expand Up @@ -1145,8 +1145,7 @@ static parser_error_t readPGFInternal(parser_context_t *ctx, pgf_payment_action_
}

// Read target
paymentAction->internal.address.len = ADDRESS_LEN_BYTES;
CHECK_ERROR(readBytes(ctx, &paymentAction->internal.address.ptr, paymentAction->internal.address.len))
CHECK_ERROR(readAddressAlt(ctx, &paymentAction->internal.address))
// Read amount
paymentAction->internal.amount.len = 32;
CHECK_ERROR(readBytes(ctx, &paymentAction->internal.amount.ptr, paymentAction->internal.amount.len))
Expand Down Expand Up @@ -1240,9 +1239,8 @@ static parser_error_t readInitProposalTxn(const bytes_t *data, const section_t *
v->initProposal.content_sechash.len = HASH_LEN;
CHECK_ERROR(readBytes(&ctx, &v->initProposal.content_sechash.ptr, v->initProposal.content_sechash.len))

// Author, should be of length ADDRESS_LEN_BYTES
v->initProposal.author.len = ADDRESS_LEN_BYTES;
CHECK_ERROR(readBytes(&ctx, &v->initProposal.author.ptr, v->initProposal.author.len))
// Author
CHECK_ERROR(readAddressAlt(&ctx, &v->initProposal.author))

// Proposal type
v->initProposal.has_proposal_code = 0;
Expand All @@ -1264,11 +1262,11 @@ static parser_error_t readInitProposalTxn(const bytes_t *data, const section_t *
v->initProposal.pgf_steward_actions.len = 0;

uint8_t add_rem_discriminant = 0;
bytes_t tmpBytes = {.ptr = NULL, .len = ADDRESS_LEN_BYTES};
AddressAlt tmpBytes;
for (uint32_t i = 0; i < v->initProposal.pgf_steward_actions_num; i++) {
CHECK_ERROR(readByte(&ctx, &add_rem_discriminant))
CHECK_ERROR(readBytes(&ctx, &tmpBytes.ptr, tmpBytes.len))
v->initProposal.pgf_steward_actions.len += 1 + ADDRESS_LEN_BYTES;
CHECK_ERROR(readAddressAlt(&ctx, &tmpBytes))
v->initProposal.pgf_steward_actions.len = ctx.buffer + ctx.offset - v->initProposal.pgf_steward_actions.ptr;
}
break;
}
Expand Down Expand Up @@ -1353,17 +1351,19 @@ static parser_error_t readVoteProposalTxn(const bytes_t *data, parser_tx_t *v) {
return parser_unexpected_value;
}

// Voter, should be of length ADDRESS_LEN_BYTES
v->voteProposal.voter.len = ADDRESS_LEN_BYTES;
CHECK_ERROR(readBytes(&ctx, &v->voteProposal.voter.ptr, v->voteProposal.voter.len))
CHECK_ERROR(readAddressAlt(&ctx, &v->voteProposal.voter))

// Delegators
v->voteProposal.number_of_delegations = 0;
CHECK_ERROR(readUint32(&ctx, &v->voteProposal.number_of_delegations))
v->voteProposal.delegations.len = 0;
if (v->voteProposal.number_of_delegations > 0 ){
v->voteProposal.delegations.len = ADDRESS_LEN_BYTES*v->voteProposal.number_of_delegations;
CHECK_ERROR(readBytes(&ctx, &v->voteProposal.delegations.ptr, v->voteProposal.delegations.len))
v->voteProposal.delegations.ptr = ctx.buffer + ctx.offset;
for (uint32_t i = 0; i < v->voteProposal.number_of_delegations; i++) {
AddressAlt tmp;
CHECK_ERROR(readAddressAlt(&ctx, &tmp))
}
v->voteProposal.delegations.len = ctx.buffer + ctx.offset - v->voteProposal.delegations.ptr;
}

if ((ctx.offset != ctx.bufferLen)) {
Expand All @@ -1390,16 +1390,14 @@ static parser_error_t readWithdrawTxn(bytes_t *buffer, parser_tx_t *v) {
parser_context_t ctx = {.buffer = buffer->ptr, .bufferLen = buffer->len, .offset = 0, .tx_obj = NULL};

// Validator
v->withdraw.validator.len = ADDRESS_LEN_BYTES;
CHECK_ERROR(readBytes(&ctx, &v->withdraw.validator.ptr, v->withdraw.validator.len))
CHECK_ERROR(readAddressAlt(&ctx, &v->withdraw.validator))

// Does this tx specify the source
CHECK_ERROR(readByte(&ctx, &v->withdraw.has_source))

// Source
if (v->withdraw.has_source != 0) {
v->withdraw.source.len = ADDRESS_LEN_BYTES;
CHECK_ERROR(readBytes(&ctx, &v->withdraw.source.ptr, v->withdraw.source.len))
CHECK_ERROR(readAddressAlt(&ctx, &v->withdraw.source))
}

if (ctx.offset != ctx.bufferLen) {
Expand All @@ -1412,8 +1410,7 @@ static parser_error_t readCommissionChangeTxn(bytes_t *buffer, parser_tx_t *v) {
parser_context_t ctx = {.buffer = buffer->ptr, .bufferLen = buffer->len, .offset = 0, .tx_obj = NULL};

// Validator
v->commissionChange.validator.len = ADDRESS_LEN_BYTES;
CHECK_ERROR(readBytes(&ctx, &v->commissionChange.validator.ptr, v->commissionChange.validator.len))
CHECK_ERROR(readAddressAlt(&ctx, &v->commissionChange.validator))

// Read new commission rate
v->commissionChange.new_rate.len = 32;
Expand All @@ -1434,8 +1431,7 @@ static parser_error_t readUpdateVPTxn(const bytes_t *data, const section_t *extr
parser_context_t ctx = {.buffer = data->ptr, .bufferLen = data->len, .offset = 0, .tx_obj = NULL};

// Address
v->updateVp.address.len = ADDRESS_LEN_BYTES;
CHECK_ERROR(readBytes(&ctx, &v->updateVp.address.ptr, v->updateVp.address.len))
CHECK_ERROR(readAddressAlt(&ctx, &v->updateVp.address))

// VP code hash (optional)
CHECK_ERROR(readByte(&ctx, &v->updateVp.has_vp_code));
Expand Down Expand Up @@ -1498,8 +1494,7 @@ static parser_error_t readTransferTxn(const bytes_t *data, parser_tx_t *v) {
CHECK_ERROR(readAddressAlt(&ctx, &v->transfer.target_address))

// Token
v->transfer.token.len = ADDRESS_LEN_BYTES;
CHECK_ERROR(readBytes(&ctx, &v->transfer.token.ptr, v->transfer.token.len))
CHECK_ERROR(readAddressAlt(&ctx, &v->transfer.token))
// Get symbol from token
CHECK_ERROR(readToken(&v->transfer.token, &v->transfer.symbol))

Expand Down Expand Up @@ -1541,8 +1536,7 @@ static parser_error_t readResignSteward(const bytes_t *data, tx_resign_steward_t
parser_context_t ctx = {.buffer = data->ptr, .bufferLen = data->len, .offset = 0, .tx_obj = NULL};

// Validator
resignSteward->steward.len = ADDRESS_LEN_BYTES;
CHECK_ERROR(readBytes(&ctx, &resignSteward->steward.ptr, resignSteward->steward.len))
CHECK_ERROR(readAddressAlt(&ctx, &resignSteward->steward))
if (ctx.offset != ctx.bufferLen) {
return parser_unexpected_characters;
}
Expand All @@ -1553,8 +1547,7 @@ static parser_error_t readChangeConsensusKey(const bytes_t *data, tx_consensus_k
parser_context_t ctx = {.buffer = data->ptr, .bufferLen = data->len, .offset = 0, .tx_obj = NULL};

// Validator
consensusKeyChange->validator.len = ADDRESS_LEN_BYTES;
CHECK_ERROR(readBytes(&ctx, &consensusKeyChange->validator.ptr, consensusKeyChange->validator.len))
CHECK_ERROR(readAddressAlt(&ctx, &consensusKeyChange->validator))
// Consensus key
CHECK_ERROR(readPubkey(&ctx, &consensusKeyChange->consensus_key))

Expand All @@ -1568,18 +1561,17 @@ static parser_error_t readUpdateStewardCommission(const bytes_t *data, tx_update
parser_context_t ctx = {.buffer = data->ptr, .bufferLen = data->len, .offset = 0, .tx_obj = NULL};

// Address
updateStewardCommission->steward.len = ADDRESS_LEN_BYTES;
CHECK_ERROR(readBytes(&ctx, &updateStewardCommission->steward.ptr, updateStewardCommission->steward.len))
CHECK_ERROR(readAddressAlt(&ctx, &updateStewardCommission->steward))

updateStewardCommission->commissionLen = 0;
CHECK_ERROR(readUint32(&ctx, &updateStewardCommission->commissionLen))

updateStewardCommission->commission.ptr = ctx.buffer + ctx.offset;
const uint16_t startOffset = ctx.offset;
bytes_t address = {.ptr = NULL, .len = ADDRESS_LEN_BYTES};
AddressAlt address;
bytes_t amount = {.ptr = NULL, .len = 32};
for (uint32_t i = 0; i < updateStewardCommission->commissionLen; i++) {
CHECK_ERROR(readBytes(&ctx, &address.ptr, address.len))
CHECK_ERROR(readAddressAlt(&ctx, &address))
CHECK_ERROR(readBytes(&ctx, &amount.ptr, amount.len))
}
updateStewardCommission->commission.len = ctx.offset - startOffset;
Expand All @@ -1594,8 +1586,7 @@ static parser_error_t readChangeValidatorMetadata(const bytes_t *data, tx_metada
parser_context_t ctx = {.buffer = data->ptr, .bufferLen = data->len, .offset = 0, .tx_obj = NULL};

// Validator
metadataChange->validator.len = ADDRESS_LEN_BYTES;
CHECK_ERROR(readBytes(&ctx, &metadataChange->validator.ptr, metadataChange->validator.len))
CHECK_ERROR(readAddressAlt(&ctx, &metadataChange->validator))

uint32_t tmpValue = 0;
// The validator email
Expand Down Expand Up @@ -1701,20 +1692,17 @@ static parser_error_t readBridgePoolTransfer(const bytes_t *data, tx_bridge_pool
bridgePoolTransfer->recipient.len = ETH_ADDRESS_LEN;
CHECK_ERROR(readBytes(&ctx, &bridgePoolTransfer->recipient.ptr, bridgePoolTransfer->recipient.len))

bridgePoolTransfer->sender.len = ADDRESS_LEN_BYTES;
CHECK_ERROR(readBytes(&ctx, &bridgePoolTransfer->sender.ptr, bridgePoolTransfer->sender.len))
CHECK_ERROR(readAddressAlt(&ctx, &bridgePoolTransfer->sender))

bridgePoolTransfer->amount.len = 32;
CHECK_ERROR(readBytes(&ctx, &bridgePoolTransfer->amount.ptr, bridgePoolTransfer->amount.len))

bridgePoolTransfer->gasAmount.len = 32;
CHECK_ERROR(readBytes(&ctx, &bridgePoolTransfer->gasAmount.ptr, bridgePoolTransfer->gasAmount.len))

bridgePoolTransfer->gasPayer.len = ADDRESS_LEN_BYTES;
CHECK_ERROR(readBytes(&ctx, &bridgePoolTransfer->gasPayer.ptr, bridgePoolTransfer->gasPayer.len))
CHECK_ERROR(readAddressAlt(&ctx, &bridgePoolTransfer->gasPayer))

bridgePoolTransfer->gasToken.len = ADDRESS_LEN_BYTES;
CHECK_ERROR(readBytes(&ctx, &bridgePoolTransfer->gasToken.ptr, bridgePoolTransfer->gasToken.len))
CHECK_ERROR(readAddressAlt(&ctx, &bridgePoolTransfer->gasToken))

if (ctx.offset != ctx.bufferLen) {
return parser_unexpected_characters;
Expand Down Expand Up @@ -1890,8 +1878,7 @@ parser_error_t readHeader(parser_context_t *ctx, parser_tx_t *v) {
CHECK_ERROR(readByte(ctx, &v->transaction.header.fees.denom))

// Fee.address
v->transaction.header.fees.address.len = ADDRESS_LEN_BYTES;
CHECK_ERROR(readBytes(ctx, &v->transaction.header.fees.address.ptr, v->transaction.header.fees.address.len))
CHECK_ERROR(readAddressAlt(ctx, &v->transaction.header.fees.address))
// Get symbol from token
CHECK_ERROR(readToken(&v->transaction.header.fees.address, &v->transaction.header.fees.symbol))

Expand Down Expand Up @@ -2022,8 +2009,9 @@ static parser_error_t readSignatureSection(parser_context_t *ctx, signature_sect
break;

case Address:
signature->address.len = ADDRESS_LEN_BYTES;
CHECK_ERROR(readBytes(ctx, &signature->address.ptr, signature->address.len))
signature->addressBytes.ptr = ctx->buffer + ctx->offset;
CHECK_ERROR(readAddressAlt(ctx, &signature->address))
signature->addressBytes.len = ctx->buffer + ctx->offset - signature->addressBytes.ptr;
break;

default:
Expand Down
12 changes: 6 additions & 6 deletions app/src/parser_print_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ parser_error_t printAddress( bytes_t pubkeyHash,
return parser_ok;
}

parser_error_t printAddressAlt( AddressAlt *addr,
parser_error_t printAddressAlt( const AddressAlt *addr,
char *outVal, uint16_t outValLen,
uint8_t pageIdx, uint8_t *pageCount) {

Expand Down Expand Up @@ -288,13 +288,13 @@ parser_error_t printProposal(const tx_init_proposal_t *initProposal, uint8_t dis

} else if (initProposal->proposal_type == PGFSteward) {
uint8_t add_rem_discriminant = 0;
bytes_t tmpBytes = {.ptr = NULL, .len = ADDRESS_LEN_BYTES};
AddressAlt tmpBytes;
parser_context_t tmpCtx = { .buffer = initProposal->pgf_steward_actions.ptr,
.bufferLen = initProposal->pgf_steward_actions.len,
.offset = 0};
for (uint32_t i = 0; i < displayIdx; i++) {
CHECK_ERROR(readByte(&tmpCtx, &add_rem_discriminant))
CHECK_ERROR(readBytes(&tmpCtx, &tmpBytes.ptr, tmpBytes.len))
CHECK_ERROR(readAddressAlt(&tmpCtx, &tmpBytes))
}

// Add = 0 | Remove = 1
Expand All @@ -303,7 +303,7 @@ parser_error_t printProposal(const tx_init_proposal_t *initProposal, uint8_t dis
snprintf(outKey, outKeyLen, "Remove");
}

CHECK_ERROR(printAddress(tmpBytes, outVal, outValLen, pageIdx, pageCount))
CHECK_ERROR(printAddressAlt(&tmpBytes, outVal, outValLen, pageIdx, pageCount))

} else if (initProposal->proposal_type == PGFPayment) {
pgf_payment_action_t pgfPayment = {0};
Expand Down Expand Up @@ -340,7 +340,7 @@ parser_error_t printProposal(const tx_init_proposal_t *initProposal, uint8_t dis

case 1:
snprintf(outKey, outKeyLen, "Target");
CHECK_ERROR(printAddress(pgfPayment.internal.address, outVal, outValLen, pageIdx, pageCount))
CHECK_ERROR(printAddressAlt(&pgfPayment.internal.address, outVal, outValLen, pageIdx, pageCount))
break;

case 0:
Expand Down Expand Up @@ -447,7 +447,7 @@ parser_error_t printExpert( const parser_context_t *ctx,
CHECK_ERROR(printAmount(&ctx->tx_obj->transaction.header.fees.amount, true, ctx->tx_obj->transaction.header.fees.denom, "", outVal, outValLen, pageIdx, pageCount))
} else {
snprintf(outKey, outKeyLen, "Fee token");
CHECK_ERROR(printAddress(ctx->tx_obj->transaction.header.fees.address, outVal, outValLen, pageIdx, pageCount))
CHECK_ERROR(printAddressAlt(&ctx->tx_obj->transaction.header.fees.address, outVal, outValLen, pageIdx, pageCount))
}
break;
}
Expand Down
2 changes: 1 addition & 1 deletion app/src/parser_print_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ parser_error_t printAddress(bytes_t pubkeyHash,
char *outVal, uint16_t outValLen,
uint8_t pageIdx, uint8_t *pageCount);

parser_error_t printAddressAlt(AddressAlt *addr,
parser_error_t printAddressAlt(const AddressAlt *addr,
char *outVal, uint16_t outValLen,
uint8_t pageIdx, uint8_t *pageCount);

Expand Down
Loading

0 comments on commit aa5bb4c

Please sign in to comment.