Skip to content

Commit

Permalink
wip ics
Browse files Browse the repository at this point in the history
  • Loading branch information
abenso committed Dec 10, 2024
1 parent 84d9b5a commit 00026cf
Show file tree
Hide file tree
Showing 8 changed files with 186 additions and 927 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ file(GLOB_RECURSE LIB_SRC
${CMAKE_CURRENT_SOURCE_DIR}/app/src/memo.c
${CMAKE_CURRENT_SOURCE_DIR}/app/src/plan/swap.c
${CMAKE_CURRENT_SOURCE_DIR}/app/src/c_api/rust.c
${CMAKE_CURRENT_SOURCE_DIR}/app/src/plan/ics20_withdrawal.c
)

add_library(app_lib STATIC ${LIB_SRC})
Expand Down
29 changes: 15 additions & 14 deletions app/rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
********************************************************************************/
#![no_std]
// #![no_std]
#![no_builtins]
#![allow(dead_code)]
#![deny(unused_crate_dependencies)]
Expand Down Expand Up @@ -43,14 +43,14 @@ pub(crate) use utils::prf::{expand_fq, expand_fr};

fn debug(_msg: &str) {}

#[cfg(all(not(test), not(feature = "clippy"), not(feature = "fuzzing")))]
use core::panic::PanicInfo;
// #[cfg(all(not(test), not(feature = "clippy"), not(feature = "fuzzing")))]
// use core::panic::PanicInfo;

#[cfg(all(not(test), not(feature = "clippy"), not(feature = "fuzzing")))]
#[panic_handler]
fn panic(_info: &PanicInfo) -> ! {
loop {}
}
// #[cfg(all(not(test), not(feature = "clippy"), not(feature = "fuzzing")))]
// #[panic_handler]
// fn panic(_info: &PanicInfo) -> ! {
// loop {}
// }

extern "C" {
fn check_app_canary();
Expand All @@ -59,12 +59,13 @@ extern "C" {
fn zemu_log_stack(s: *const u8);
}

pub(crate) fn canary() {
#[cfg(not(any(test, fuzzing)))]
unsafe {
check_app_canary();
}
}
// pub(crate) fn canary() {
// let var_name = #[cfg(not(any(test, fuzzing)))]
// unsafe {
// check_app_canary();
// };
// var_name
// }

#[cfg(not(any(test, fuzzing)))]
pub fn is_expert_mode() -> bool {
Expand Down
8 changes: 8 additions & 0 deletions app/src/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "tx_metadata.h"
#include "memo.h"
#include "swap.h"
#include "ics20_withdrawal.h"

static uint8_t action_idx = 0;

Expand Down Expand Up @@ -90,6 +91,9 @@ parser_error_t parser_getNumItems(const parser_context_t *ctx, uint8_t *num_item
case penumbra_core_transaction_v1_ActionPlan_swap_tag:
CHECK_ERROR(swap_getNumItems(ctx, &action_num_items));
break;
case penumbra_core_transaction_v1_ActionPlan_ics20_withdrawal_tag:
CHECK_ERROR(ics20_withdrawal_getNumItems(ctx, &action_num_items));
break;
default:
return parser_unexpected_error;
}
Expand Down Expand Up @@ -159,6 +163,10 @@ parser_error_t parser_getItem(const parser_context_t *ctx, uint8_t displayIdx, c
CHECK_ERROR(swap_getItem(ctx, &ctx->tx_obj->actions_plan[action_idx].action.swap, 0, outKey, outKeyLen,
outVal, outValLen, pageIdx, pageCount))
break;
case penumbra_core_transaction_v1_ActionPlan_ics20_withdrawal_tag:
CHECK_ERROR(ics20_withdrawal_getItem(ctx, &ctx->tx_obj->actions_plan[action_idx].action.ics20_withdrawal, 0, outKey, outKeyLen,
outVal, outValLen, pageIdx, pageCount))
break;
default:
return parser_unexpected_error;
}
Expand Down
24 changes: 24 additions & 0 deletions app/src/parser_impl.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,30 @@ static bool decode_detection_data(pb_istream_t *stream, const pb_field_t *field,
static uint16_t actions_qty = 0;
static uint16_t detection_data_qty = 0;


void print_buffer(bytes_t *buffer, const char *title) {
#if defined(LEDGER_SPECIFIC)
ZEMU_LOGF(50, "%s\n", title);
char print[1000] = {0};
array_to_hexstr(print, sizeof(print), buffer->ptr, buffer->len);
ZEMU_LOGF(1000, "%s\n", print);
#else
printf("%s %d: ", title, buffer->len);
for (uint16_t i = 0; i < buffer->len; i++) {
printf("%02x", buffer->ptr[i]);
}
printf("\n");
#endif
}

void print_string(const char *str) {
#if defined(LEDGER_SPECIFIC)
ZEMU_LOGF(100, "%s\n", str);
#else
printf("%s\n", str);
#endif
}

bool decode_action(pb_istream_t *stream, const pb_field_t *field, void **arg) {
if (arg == NULL || *arg == NULL) {
return false;
Expand Down
87 changes: 87 additions & 0 deletions app/src/plan/ics20_withdrawal.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
********************************************************************************/
#include "ics20_withdrawal.h"

#include "parser_impl.h"
#include "parser_interface.h"
Expand All @@ -21,6 +22,8 @@
#include "pb_decode.h"
#include "protobuf/penumbra/core/transaction/v1/transaction.pb.h"
#include "zxformat.h"
#include "ui_utils.h"
#include "note.h"

parser_error_t decode_ics20_withdrawal_plan(const bytes_t *data, ics20_withdrawal_plan_t *withdrawal) {
penumbra_core_component_ibc_v1_Ics20Withdrawal withdrawal_plan = penumbra_core_component_ibc_v1_Ics20Withdrawal_init_default;
Expand Down Expand Up @@ -59,3 +62,87 @@ parser_error_t decode_ics20_withdrawal_plan(const bytes_t *data, ics20_withdrawa

return parser_ok;
}


parser_error_t ics20_withdrawal_getNumItems(const parser_context_t *ctx, uint8_t *num_items) {
UNUSED(ctx);
*num_items = 1;
return parser_ok;
}

parser_error_t ics20_withdrawal_getItem(const parser_context_t *ctx, const ics20_withdrawal_plan_t *ics20_withdrawal,
uint8_t displayIdx, char *outKey, uint16_t outKeyLen,
char *outVal, uint16_t outValLen, uint8_t pageIdx,
uint8_t *pageCount) {

parser_error_t err = parser_no_data;
if (ics20_withdrawal == NULL || outKey == NULL || outVal == NULL || outKeyLen == 0 || outValLen == 0) {
return err;
}


if (displayIdx != 0) {
return parser_no_data;
}

char bufferUI[SWAP_DISPLAY_MAX_LEN] = {0};

snprintf(outKey, outKeyLen, "Action");
CHECK_ERROR(ics20_withdrawal_printValue(ctx, ics20_withdrawal, bufferUI, sizeof(bufferUI)));
pageString(outVal, outValLen, bufferUI, pageIdx, pageCount);

return parser_ok;

}

parser_error_t ics20_withdrawal_printValue(const parser_context_t *ctx, const ics20_withdrawal_plan_t *ics20_withdrawal, char *outVal, uint16_t outValLen) {
if (ctx == NULL || ics20_withdrawal == NULL || outVal == NULL) {
return parser_no_data;
}

if (outValLen < OUTPUT_DISPLAY_MAX_LEN) {
return parser_unexpected_buffer_end;
}

MEMZERO(outVal, outValLen);

// example: Output 100 USDC to penumbra1k0zzug62gpz60sejdvu9q7mq…

// add action title
uint16_t written_value = snprintf(outVal, outValLen, "ICS20Withdrawal ");

// add "channel"
snprintf(outVal + written_value, outValLen - written_value, "Channel ");
written_value = strlen(outVal);

MEMCPY(outVal + written_value, ics20_withdrawal->source_channel.ptr, ics20_withdrawal->source_channel.len);
written_value += ics20_withdrawal->source_channel.len;

snprintf(outVal + written_value, outValLen - written_value, " Amount ");
written_value = strlen(outVal);

value_t ics20_withdrawal_value = {0};
ics20_withdrawal_value.amount.hi = ics20_withdrawal->amount.hi;
ics20_withdrawal_value.amount.lo = ics20_withdrawal->amount.lo;
ics20_withdrawal_value.asset_id.inner.ptr = ics20_withdrawal->denom.inner.ptr;
ics20_withdrawal_value.asset_id.inner.len = ics20_withdrawal->denom.inner.len;
CHECK_ERROR(printValue(ctx, &ics20_withdrawal_value, &ctx->tx_obj->parameters_plan.chain_id, outVal + written_value, outValLen - written_value));
written_value = strlen(outVal);

snprintf(outVal + written_value, outValLen - written_value, "To ");
written_value = strlen(outVal);

// snprintf(outVal + written_value, outValLen - written_value, " Output Asset ");
// written_value = strlen(outVal);

// CHECK_ERROR(printAssetIdFromValue(ctx, &input_value, &ctx->tx_obj->parameters_plan.chain_id, outVal + written_value, outValLen - written_value));
// written_value = strlen(outVal);

// snprintf(outVal + written_value, outValLen - written_value, " Claim Fee ");
// written_value = strlen(outVal);

// CHECK_ERROR(printValue(ctx, &swap->swap_plaintext.claim_fee, &ctx->tx_obj->parameters_plan.chain_id, outVal + written_value, outValLen - written_value));
// written_value = strlen(outVal);

return parser_ok;
}
7 changes: 7 additions & 0 deletions app/src/plan/ics20_withdrawal.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ extern "C" {
#endif

parser_error_t decode_ics20_withdrawal_plan(const bytes_t *data, ics20_withdrawal_plan_t *withdrawal);
parser_error_t ics20_withdrawal_getNumItems(const parser_context_t *ctx, uint8_t *num_items);
parser_error_t ics20_withdrawal_getItem(const parser_context_t *ctx, const ics20_withdrawal_plan_t *ics20_withdrawal,
uint8_t displayIdx, char *outKey, uint16_t outKeyLen,
char *outVal, uint16_t outValLen, uint8_t pageIdx,
uint8_t *pageCount);

parser_error_t ics20_withdrawal_printValue(const parser_context_t *ctx, const ics20_withdrawal_plan_t *ics20_withdrawal, char *outVal, uint16_t outValLen);

#ifdef __cplusplus
}
Expand Down
1 change: 0 additions & 1 deletion app/src/plan/swap.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ parser_error_t decode_swap_plan(const bytes_t *data, swap_plan_t *swap) {
return parser_ok;
}


parser_error_t swap_getNumItems(const parser_context_t *ctx, uint8_t *num_items) {
UNUSED(ctx);
*num_items = 1;
Expand Down
Loading

0 comments on commit 00026cf

Please sign in to comment.