Skip to content

Commit

Permalink
complete support for printing masp tx
Browse files Browse the repository at this point in the history
  • Loading branch information
chcmedeiros committed May 13, 2024
1 parent 9e650b9 commit 8640103
Show file tree
Hide file tree
Showing 7 changed files with 61,080 additions and 31 deletions.
4 changes: 4 additions & 0 deletions app/src/parser_address.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ parser_error_t readAddressInternal(parser_context_t *ctx, InternalAddress *obj)
case 10: //Multitoken
case 11: //Pgf
case 12: //Masp
case 13: //TempStorage
break;
case 4:
CHECK_ERROR(readInternalAddressIbcToken(ctx, &obj->IbcToken))
Expand Down Expand Up @@ -146,6 +147,9 @@ parser_error_t encodeAddress(const AddressAlt *addr, char *address, uint16_t add
case 12:
tmpBuffer[0] = PREFIX_MASP;
break;
case 13:
tmpBuffer[0] = PREFIX_TMP_STORAGE;
break;
}
break;

Expand Down
1 change: 1 addition & 0 deletions app/src/parser_address.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ extern "C" {
#define PREFIX_NUT 12
#define PREFIX_IBC_TOKEN 13
#define PREFIX_MASP 14
#define PREFIX_TMP_STORAGE 15
#define PREFIX_INTERNAL 2

parser_error_t readAddressAlt(parser_context_t *ctx, AddressAlt *obj);
Expand Down
14 changes: 6 additions & 8 deletions app/src/parser_impl.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,16 @@ parser_error_t getNumItems(const parser_context_t *ctx, uint8_t *numItems) {
items += 3; // print only sender from transfer source
if(target_is_masp) {
items += 3 * ctx->tx_obj->transaction.sections.maspBuilder.builder.sapling_builder.n_outputs; // print from outputs
} else {
items += 3; // print only receiver from transfer target
}
} else {
items += 3 * ctx->tx_obj->transaction.sections.maspBuilder.builder.sapling_builder.n_spends; // print from spends
}

if(!target_is_masp) {
items += 3; // print only receiver from transfer target
if(source_is_masp) {
items += 3 * ctx->tx_obj->transaction.sections.maspBuilder.builder.sapling_builder.n_spends; // print from spends
if(!target_is_masp) {
items += 3; // print only receiver from transfer target
} else {
items += 3 * ctx->tx_obj->transaction.sections.maspBuilder.builder.sapling_builder.n_outputs; // print from outputs
}
} else {
items += 3 * ctx->tx_obj->transaction.sections.maspBuilder.builder.sapling_builder.n_outputs; // print from outputs
}

*numItems = (app_mode_expert() ? items + 5 : items);
Expand Down
116 changes: 94 additions & 22 deletions app/src/parser_print_txn.c
Original file line number Diff line number Diff line change
Expand Up @@ -185,28 +185,104 @@ static parser_error_t printTransferTxn( const parser_context_t *ctx,
return parser_ok;
}

static parser_error_t getSpendfromIndex(uint32_t index, bytes_t *spend) {

for (uint32_t i = 0; i < index; i++) {
spend->ptr += EXTENDED_FVK_LEN + DIVERSIFIER_LEN + NOTE_LEN + ALPHA_LEN;
uint8_t tmp_len = spend->ptr[0];
spend->ptr++;
spend->ptr += (tmp_len * (32 + 1)) + sizeof(uint64_t);
}

return parser_ok;
}

static parser_error_t getOutputfromIndex(uint32_t index, bytes_t *out) {

for (uint32_t i = 0; i < index; i++) {
uint8_t has_ovk = out->ptr[0];
if(has_ovk) {
out->ptr += 33;
} else {
out->ptr++;
}
out->ptr += DIVERSIFIER_LEN + PAYMENT_ADDR_LEN + NOTE_LEN + MEMO_LEN;
}

return parser_ok;
}

uint16_t spend_index = 0;
uint16_t out_index = 0;

static parser_error_t printMaspTransferTxn( const parser_context_t *ctx,
uint8_t displayIdx,
char *outKey, uint16_t outKeyLen,
char *outVal, uint16_t outValLen,
uint8_t pageIdx, uint8_t *pageCount) {
uint32_t n_spends = ctx->tx_obj->transaction.sections.maspBuilder.builder.sapling_builder.n_spends;
uint32_t n_outs = ctx->tx_obj->transaction.sections.maspBuilder.builder.sapling_builder.n_outputs;
uint32_t n_send_items = n_spends > 0 ? n_spends * 3 : 3;
uint32_t n_dest_items = n_outs > 0 ? n_outs * 3 : 3;

if(displayIdx >= 6 && ctx->tx_obj->transfer.symbol) {
if(displayIdx >= (6 + (n_send_items - 3) + (n_dest_items - 3)) && ctx->tx_obj->transfer.symbol) {
displayIdx++;
}

if(displayIdx >= 1 && n_spends == 0 && ctx->tx_obj->transfer.source_address.tag == 2) {
displayIdx += 3;
}
if(displayIdx >= 4 && n_outs == 0 && ctx->tx_obj->transfer.target_address.tag == 2) {
displayIdx += 3;
}

if(displayIdx == 0) {
out_index = 0;
spend_index = 0;
}
uint8_t orig_idx = displayIdx;
bytes_t spend = ctx->tx_obj->transaction.sections.maspBuilder.builder.sapling_builder.spends;
if(n_send_items > 3 ) {
if(displayIdx >= 4 && displayIdx <= n_send_items) {
displayIdx = displayIdx % 3;
displayIdx = displayIdx == 0 ? 3 : displayIdx;
if (displayIdx == 1 && pageIdx == 0) {
spend_index ++;
}
} else if(displayIdx > n_send_items) {
displayIdx -= (n_send_items - 3);
spend_index = 0;
}
getSpendfromIndex(spend_index, &spend);
}

uint8_t tmp_idx = 0;
bytes_t out = ctx->tx_obj->transaction.sections.maspBuilder.builder.sapling_builder.outputs;
if(n_dest_items > 3 ) {
if(displayIdx >= 7 && displayIdx <= n_send_items + n_dest_items && orig_idx <= (n_send_items + n_dest_items)) {
tmp_idx = displayIdx;
displayIdx = (displayIdx % 6) + 3;
if (displayIdx == 4 && pageIdx == 0) {
out_index ++;
}
} else if(displayIdx > n_dest_items) {
displayIdx -= (n_dest_items - 3);
out_index = 0;
}
getOutputfromIndex(out_index, &out);
// if(tmp_idx == n_dest_items + n_send_items) {
// out_index = 0;
// }
}

const bool hasMemo = ctx->tx_obj->transaction.header.memoSection != NULL;
if (displayIdx >= 7 && !hasMemo) {
displayIdx++;
}

if(displayIdx >= 1 && ctx->tx_obj->transaction.sections.maspBuilder.builder.sapling_builder.n_spends == 0 && ctx->tx_obj->transfer.source_address.tag == 2) {
displayIdx += 3;
}

char tmp_buf[500] = {0};
uint8_t tmp_amount[32] = {0};
bytes_t amount_bytes = {tmp_amount, 32};
uint8_t has_ovk = 0;
const uint8_t *amount = {0};

switch (displayIdx) {
Expand All @@ -223,29 +299,27 @@ static parser_error_t printMaspTransferTxn( const parser_context_t *ctx,
if (ctx->tx_obj->transfer.source_address.tag != 2) {
CHECK_ERROR(printAddressAlt(&ctx->tx_obj->transfer.source_address, outVal, outValLen, pageIdx, pageCount))
} else {
const uint8_t *vk = ctx->tx_obj->transaction.sections.maspBuilder.builder.sapling_builder.spends.ptr;

const char *hrp = "zvknam";
bech32EncodeFromLargeBytes(tmp_buf,
sizeof(tmp_buf),
hrp,
(uint8_t*) vk,
(uint8_t*) spend.ptr,
EXTENDED_FVK_LEN,
1,
BECH32_ENCODING_BECH32M);
pageString(outVal, outValLen, (const char*) tmp_buf, pageIdx, pageCount);
}
pageString(outVal, outValLen, (const char*) tmp_buf, pageIdx, pageCount);

break;
case 2:
snprintf(outKey, outKeyLen, "Sending Token");
if (ctx->tx_obj->transfer.source_address.tag != 2) {
CHECK_ERROR(printAddressAlt(&ctx->tx_obj->transfer.token, outVal, outValLen, pageIdx, pageCount))
} else {
const uint8_t *token = ctx->tx_obj->transaction.sections.maspBuilder.builder.sapling_builder.spends.ptr + EXTENDED_FVK_LEN + DIVERSIFIER_LEN;
} else {
const uint8_t *token = spend.ptr + EXTENDED_FVK_LEN + DIVERSIFIER_LEN;
array_to_hexstr(tmp_buf, sizeof(tmp_buf), token, ASSET_ID_LEN);
pageString(outVal, outValLen, (const char*) tmp_buf, pageIdx, pageCount);
}

break;
case 3:
snprintf(outKey, outKeyLen, "Sending Amount");
Expand All @@ -254,8 +328,9 @@ static parser_error_t printMaspTransferTxn( const parser_context_t *ctx,
"",
outVal, outValLen, pageIdx, pageCount))
} else {
amount = ctx->tx_obj->transaction.sections.maspBuilder.builder.sapling_builder.spends.ptr + EXTENDED_FVK_LEN + DIVERSIFIER_LEN + ASSET_ID_LEN;
MEMCPY(tmp_amount + sizeof(tmp_amount) - sizeof(uint64_t), amount, sizeof(uint64_t));
amount = spend.ptr + EXTENDED_FVK_LEN + DIVERSIFIER_LEN + ASSET_ID_LEN;
// tmp_amount is a 32 bytes array that represents an uint64[4] array, position will determine amount postion inside the array
MEMCPY(tmp_amount + (ctx->tx_obj->transaction.sections.maspBuilder.asset_data.position * sizeof(uint64_t)), amount, sizeof(uint64_t));
printAmount(&amount_bytes, false, ctx->tx_obj->transaction.sections.maspBuilder.asset_data.denom, "", outVal, outValLen, pageIdx, pageCount);
}
break;
Expand All @@ -264,12 +339,11 @@ static parser_error_t printMaspTransferTxn( const parser_context_t *ctx,
if(ctx->tx_obj->transfer.target_address.tag != 2) {
CHECK_ERROR(printAddressAlt(&ctx->tx_obj->transfer.target_address, outVal, outValLen, pageIdx, pageCount))
} else {
const uint8_t *pa = ctx->tx_obj->transaction.sections.maspBuilder.builder.sapling_builder.outputs.ptr;
const char *hrp = "znam";
bech32EncodeFromLargeBytes(tmp_buf,
sizeof(tmp_buf),
hrp,
(uint8_t*) pa,
(uint8_t*) out.ptr,
PAYMENT_ADDR_LEN + DIVERSIFIER_LEN + 1,
1,
BECH32_ENCODING_BECH32M);
Expand All @@ -281,8 +355,7 @@ static parser_error_t printMaspTransferTxn( const parser_context_t *ctx,
if (ctx->tx_obj->transfer.target_address.tag != 2) {
CHECK_ERROR(printAddressAlt(&ctx->tx_obj->transfer.token, outVal, outValLen, pageIdx, pageCount))
} else {
has_ovk = ctx->tx_obj->transaction.sections.maspBuilder.builder.sapling_builder.outputs.ptr[0];
const uint8_t *rtoken = ctx->tx_obj->transaction.sections.maspBuilder.builder.sapling_builder.outputs.ptr + (has_ovk ? 33 : 1) + PAYMENT_ADDR_LEN + DIVERSIFIER_LEN;
const uint8_t *rtoken = out.ptr + (out.ptr[0] ? 33 : 1) + PAYMENT_ADDR_LEN + DIVERSIFIER_LEN;
array_to_hexstr(tmp_buf, sizeof(tmp_buf), rtoken, ASSET_ID_LEN);
pageString(outVal, outValLen, (const char*) tmp_buf, pageIdx, pageCount);
}
Expand All @@ -294,9 +367,8 @@ static parser_error_t printMaspTransferTxn( const parser_context_t *ctx,
"",
outVal, outValLen, pageIdx, pageCount))
} else {
has_ovk = ctx->tx_obj->transaction.sections.maspBuilder.builder.sapling_builder.outputs.ptr[0];
amount = ctx->tx_obj->transaction.sections.maspBuilder.builder.sapling_builder.outputs.ptr + (has_ovk ? 33 : 1) + PAYMENT_ADDR_LEN + DIVERSIFIER_LEN + ASSET_ID_LEN;
MEMCPY(tmp_amount + sizeof(tmp_amount) - sizeof(uint64_t), amount, sizeof(uint64_t));
amount = out.ptr + (out.ptr[0] ? 33 : 1) + PAYMENT_ADDR_LEN + DIVERSIFIER_LEN + ASSET_ID_LEN;
MEMCPY(tmp_amount + (ctx->tx_obj->transaction.sections.maspBuilder.asset_data.position * sizeof(uint64_t)), amount, sizeof(uint64_t));
printAmount(&amount_bytes, false, ctx->tx_obj->transaction.sections.maspBuilder.asset_data.denom, "", outVal, outValLen, pageIdx, pageCount);
}
break;
Expand Down
Loading

0 comments on commit 8640103

Please sign in to comment.