Skip to content

Commit 462c196

Browse files
committed
chore: Complete verification data all txns
1 parent 266e827 commit 462c196

File tree

2 files changed

+85
-11
lines changed

2 files changed

+85
-11
lines changed

apps/starknet_app/starknet_poseidon.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,7 @@ static void calculate_deploy_transaction_hash(
465465
felt_t hash) {
466466
uint8_t hex[14] = DEPLOY_ACCOUNT_PREFIX;
467467
felt_t transaction_hash_prefix = {0};
468-
hex_to_felt_t(hex, 12, transaction_hash_prefix);
468+
hex_to_felt_t(hex, 14, transaction_hash_prefix);
469469

470470
// prepare additional data array
471471
const uint8_t data_max_count = 3;

apps/starknet_app/starknet_sign_txn.c

Lines changed: 84 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -489,9 +489,13 @@ static void stark_amount_get_decimal_str(const uint8_t *byte_array,
489489
uint8_t i = 2;
490490
for (; i < (18 - len + 2); i++) {
491491
snprintf(amount_str + i, amount_size - i, "0");
492+
// stop if more than 6 zeros (value too low)
493+
if (i == (2 + 6)) {
494+
break;
495+
}
492496
}
493497
uint8_t offset = 0;
494-
for (; i < 6 + 2; i++) {
498+
for (; i < 6 + 2; i++) { ///< append till reach 6th char
495499
snprintf(amount_str + i, amount_size - i, "%c", str[offset++]);
496500
}
497501
return;
@@ -520,7 +524,27 @@ static void stark_amount_get_decimal_str(const uint8_t *byte_array,
520524
}
521525
}
522526

527+
static void starknet_get_max_fee(const uint8_t *max_amount_bytes,
528+
const uint8_t max_amount_size,
529+
const uint8_t *unit_price_bytes,
530+
const uint8_t unit_price_size,
531+
uint8_t max_fee[32]) {
532+
// get max fee = max_amount * unit_price
533+
mpz_t max_amount, unit_price;
534+
mpz_init(max_amount);
535+
mpz_init(unit_price);
536+
537+
byte_array_to_mpz(max_amount, max_amount_bytes, max_amount_size);
538+
byte_array_to_mpz(unit_price, unit_price_bytes, unit_price_size);
539+
mpz_mul(max_amount, max_amount, unit_price);
540+
541+
mpz_to_byte_array(max_amount, max_fee, 32);
542+
mpz_clear(max_amount);
543+
mpz_clear(unit_price);
544+
}
545+
523546
static bool get_invoke_txn_user_verification() {
547+
// verify address
524548
char address[100] = "0x";
525549
if (starknet_txn_context->invoke_txn->calldata.value[4].size != 32) {
526550
return false;
@@ -555,24 +579,75 @@ static bool get_invoke_txn_user_verification() {
555579
return false;
556580
}
557581

582+
// Verify gas fee
583+
memzero(amount_str, sizeof(amount_str));
584+
585+
// calculate l1 max fee
586+
uint8_t max_fee[32] = {0};
587+
starknet_get_max_fee(
588+
starknet_txn_context->invoke_txn->resource_bound.level_1.max_amount.bytes,
589+
starknet_txn_context->invoke_txn->resource_bound.level_1.max_amount.size,
590+
starknet_txn_context->invoke_txn->resource_bound.level_1
591+
.max_price_per_unit.bytes,
592+
starknet_txn_context->invoke_txn->resource_bound.level_1
593+
.max_price_per_unit.size,
594+
max_fee);
595+
stark_amount_get_decimal_str(max_fee, 32, amount_str, sizeof(amount_str));
596+
597+
memzero(display, sizeof(display));
598+
snprintf(display,
599+
sizeof(display),
600+
"Verify Max Fee\n%s\n%s",
601+
amount_str,
602+
starknet_app.lunit1_name);
603+
604+
if (!core_confirmation(display, starknet_send_error)) {
605+
return false;
606+
}
607+
558608
return true;
559609
}
560610

561611
static bool get_deploy_txn_user_verification() {
612+
// verify address
562613
char address[100] = "0x";
563-
if (starknet_txn_context->invoke_txn->calldata.value[4].size != 32) {
614+
615+
byte_array_to_hex_string(starknet_txn_context->deploy_txn->contract_address,
616+
32,
617+
&address[2],
618+
sizeof(address));
619+
620+
if (!core_scroll_page(ui_text_verify_address, address, starknet_send_error)) {
564621
return false;
565622
}
566-
byte_array_to_hex_string(
567-
starknet_txn_context->invoke_txn->calldata.value[4].bytes,
568-
32,
569-
&address[2],
570-
sizeof(address));
571623

572-
if (!core_scroll_page(ui_text_verify_address, address, starknet_send_error)) {
624+
// Verify gas fee
625+
char amount_str[100] = "";
626+
627+
// calculate l1 max fee
628+
uint8_t max_fee[32] = {0};
629+
starknet_get_max_fee(
630+
starknet_txn_context->deploy_txn->resource_bounds.level_1.max_amount
631+
.bytes,
632+
starknet_txn_context->deploy_txn->resource_bounds.level_1.max_amount.size,
633+
starknet_txn_context->deploy_txn->resource_bounds.level_1
634+
.max_price_per_unit.bytes,
635+
starknet_txn_context->deploy_txn->resource_bounds.level_1
636+
.max_price_per_unit.size,
637+
max_fee);
638+
stark_amount_get_decimal_str(max_fee, 32, amount_str, sizeof(amount_str));
639+
640+
char display[200] = {'\0'};
641+
snprintf(display,
642+
sizeof(display),
643+
"Verify Max Fee\n%s\n%s",
644+
amount_str,
645+
starknet_app.lunit1_name);
646+
647+
if (!core_confirmation(display, starknet_send_error)) {
573648
return false;
574649
}
575-
// TODO:Verify Constructor Call Data
650+
576651
return true;
577652
}
578653

@@ -623,7 +698,6 @@ static bool sign_txn(uint8_t *signature_buffer) {
623698
// calculate txn hash
624699
felt_t hash_felt = {0};
625700
switch (starknet_txn_context->which_type) {
626-
// TODO: Remove switch case and pass starknet_txn_context itself
627701
case STARKNET_SIGN_TXN_UNSIGNED_TXN_INVOKE_TXN_TAG: {
628702
calculate_txn_hash((void *)starknet_txn_context->invoke_txn,
629703
STARKNET_SIGN_TXN_UNSIGNED_TXN_INVOKE_TXN_TAG,

0 commit comments

Comments
 (0)