Skip to content

Commit

Permalink
format files
Browse files Browse the repository at this point in the history
  • Loading branch information
abenso committed Nov 20, 2024
1 parent 0e6ce5e commit 635eaed
Show file tree
Hide file tree
Showing 11 changed files with 55 additions and 52 deletions.
3 changes: 2 additions & 1 deletion app/rust/include/rslib.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ int32_t rs_bech32_encode(const uint8_t *hrp_ptr, size_t hrp_len, const uint8_t *
parser_error_t rs_compute_transaction_plan(transaction_plan_t *plan, uint8_t *output, size_t output_len);

parser_error_t rs_spend_action_hash(spend_key_bytes_t *sk, spend_plan_t *plan, uint8_t *output, size_t output_len);
parser_error_t rs_output_action_hash(spend_key_bytes_t *sk, output_plan_t *plan, bytes_t *memo_key, uint8_t *output, size_t output_len);
parser_error_t rs_output_action_hash(spend_key_bytes_t *sk, output_plan_t *plan, bytes_t *memo_key, uint8_t *output,
size_t output_len);
parser_error_t rs_generic_action_hash(bytes_t *data, uint8_t action_type, uint8_t *output, size_t output_len);

#ifdef __cplusplus
Expand Down
2 changes: 1 addition & 1 deletion app/src/common/parser_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ extern "C" {
#include <stddef.h>
#include <stdint.h>

#include "parser_txdef.h"
#include "keys_def.h"
#include "parser_txdef.h"

#define CHECK_ERROR(__CALL) \
{ \
Expand Down
4 changes: 1 addition & 3 deletions app/src/common/tx.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,7 @@ const char *tx_parse() {

void tx_parse_reset() { MEMZERO(&tx_obj, sizeof(tx_obj)); }

parser_tx_t* tx_get_txObject() {
return &tx_obj;
}
parser_tx_t *tx_get_txObject() { return &tx_obj; }

zxerr_t tx_getNumItems(uint8_t *num_items) {
parser_error_t err = parser_getNumItems(&ctx_parsed_tx, num_items);
Expand Down
4 changes: 2 additions & 2 deletions app/src/common/tx.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@

#include "coin.h"
#include "os.h"
#include "zxerror.h"
#include "parser_txdef.h"
#include "zxerror.h"

void tx_initialize();

/// Clears the transaction buffer
void tx_reset();

/// Returns the transaction object
parser_tx_t* tx_get_txObject();
parser_tx_t *tx_get_txObject();

/// Appends buffer to the end of the current transaction buffer
/// Transaction buffer will grow until it reaches the maximum allowed size
Expand Down
7 changes: 4 additions & 3 deletions app/src/crypto.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
#include "crypto_helper.h"
#include "cx.h"
#include "keys_def.h"
#include "zxmacros.h"
#include "parser_interface.h"
#include "zxformat.h"
#include "zxmacros.h"

uint32_t hdPath[HDPATH_LEN_DEFAULT];

Expand Down Expand Up @@ -143,7 +143,8 @@ zxerr_t crypto_sign(parser_tx_t *tx_obj, uint8_t *signature, uint16_t signatureM
CATCH_ZX_ERROR(computeSpendKey(&keys));

for (uint16_t i = 0; i < tx_obj->plan.actions.qty; i++) {
CATCH_ZX_ERROR(compute_action_hash(&tx_obj->actions_plan[i], &keys.skb, &tx_obj->plan.memo.key, &tx_obj->plan.actions.hashes[i]));
CATCH_ZX_ERROR(compute_action_hash(&tx_obj->actions_plan[i], &keys.skb, &tx_obj->plan.memo.key,
&tx_obj->plan.actions.hashes[i]));
}

CATCH_ZX_ERROR(compute_transaction_plan(&tx_obj->plan, tx_obj->effect_hash, sizeof(tx_obj->effect_hash)));
Expand All @@ -152,7 +153,7 @@ zxerr_t crypto_sign(parser_tx_t *tx_obj, uint8_t *signature, uint16_t signatureM

return zxerr_ok;

catch_zx_error:
catch_zx_error:
MEMZERO(&keys, sizeof(keys));
MEMZERO(signature, signatureMaxlen);

Expand Down
2 changes: 1 addition & 1 deletion app/src/crypto.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ extern "C" {

#include "coin.h"
#include "keys_def.h"
#include "zxerror.h"
#include "parser_txdef.h"
#include "zxerror.h"

extern uint32_t hdPath[HDPATH_LEN_DEFAULT];

Expand Down
1 change: 0 additions & 1 deletion app/src/crypto_helper.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,3 @@ zxerr_t compute_keys(keys_t *keys) {

return zxerr_ok;
}

26 changes: 13 additions & 13 deletions app/src/parser_impl.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,15 @@ parser_error_t decode_output_plan(const bytes_t *data, output_plan_t *output) {
}

parser_error_t decode_delegate_plan(const bytes_t *data, delegate_plan_t *delegate) {
penumbra_core_component_stake_v1_Delegate delegate_plan =
penumbra_core_component_stake_v1_Delegate_init_default;
penumbra_core_component_stake_v1_Delegate delegate_plan = penumbra_core_component_stake_v1_Delegate_init_default;

pb_istream_t spend_stream = pb_istream_from_buffer(data->ptr, data->len);
CHECK_APP_CANARY()

// Set up fixed size fields
fixed_size_field_t validator_identity_arg;
setup_decode_fixed_field(&delegate_plan.validator_identity.ik, &validator_identity_arg, &delegate->validator_identity.ik, 32);
setup_decode_fixed_field(&delegate_plan.validator_identity.ik, &validator_identity_arg, &delegate->validator_identity.ik,
32);

if (!pb_decode(&spend_stream, penumbra_core_component_stake_v1_Delegate_fields, &delegate_plan)) {
return parser_delegate_plan_error;
Expand All @@ -116,15 +116,15 @@ parser_error_t decode_delegate_plan(const bytes_t *data, delegate_plan_t *delega
}

parser_error_t decode_undelegate_plan(const bytes_t *data, undelegate_plan_t *undelegate) {
penumbra_core_component_stake_v1_Undelegate undelegate_plan =
penumbra_core_component_stake_v1_Undelegate_init_default;
penumbra_core_component_stake_v1_Undelegate undelegate_plan = penumbra_core_component_stake_v1_Undelegate_init_default;

pb_istream_t spend_stream = pb_istream_from_buffer(data->ptr, data->len);
CHECK_APP_CANARY()

// Set up fixed size fields
fixed_size_field_t validator_identity_arg;
setup_decode_fixed_field(&undelegate_plan.validator_identity.ik, &validator_identity_arg, &undelegate->validator_identity.ik, 32);
setup_decode_fixed_field(&undelegate_plan.validator_identity.ik, &validator_identity_arg,
&undelegate->validator_identity.ik, 32);

if (!pb_decode(&spend_stream, penumbra_core_component_stake_v1_Undelegate_fields, &undelegate_plan)) {
return parser_undelegate_plan_error;
Expand Down Expand Up @@ -160,10 +160,7 @@ bool decode_action(pb_istream_t *stream, const pb_field_t *field, void **arg) {
return false;
}

bytes_t action_data = {
.ptr = stream->state + 3,
.len = stream->bytes_left - 3
};
bytes_t action_data = {.ptr = stream->state + 3, .len = stream->bytes_left - 3};

decode_arg[actions_qty].action_data = action_data;

Expand Down Expand Up @@ -279,7 +276,8 @@ parser_error_t _read(parser_context_t *c, parser_tx_t *v) {
switch (v->actions_plan[i].action_type) {
case penumbra_core_transaction_v1_ActionPlan_spend_tag:
print_buffer(&v->actions_plan[i].action.spend.note.address.inner, "real spend action note address inner");
print_buffer(&v->actions_plan[i].action.spend.note.value.asset_id.inner, "real spend action note value asset id inner");
print_buffer(&v->actions_plan[i].action.spend.note.value.asset_id.inner,
"real spend action note value asset id inner");
print_buffer(&v->actions_plan[i].action.spend.note.rseed, "real spend action note rseed");
print_buffer(&v->actions_plan[i].action.spend.randomizer, "real spend action proof randomizer");
print_buffer(&v->actions_plan[i].action.spend.value_blinding, "real spend action proof value_blinding");
Expand All @@ -291,10 +289,12 @@ parser_error_t _read(parser_context_t *c, parser_tx_t *v) {
// printf("amount lo: %lu\n", actions_plan[i].action.spend.note.value.amount.lo);
break;
case penumbra_core_transaction_v1_ActionPlan_output_tag:
print_buffer(&v->actions_plan[i].action.output.value.asset_id.inner, "real output action note value asset id inner");
print_buffer(&v->actions_plan[i].action.output.value.asset_id.inner,
"real output action note value asset id inner");
// printf("output value amount hi: %lu\n", actions_plan[i].action.output.value.amount.hi);
// printf("output value amount lo: %lu\n", actions_plan[i].action.output.value.amount.lo);
print_buffer(&v->actions_plan[i].action.output.dest_address.inner, "real output action note dest address inner");
print_buffer(&v->actions_plan[i].action.output.dest_address.inner,
"real output action note dest address inner");
print_buffer(&v->actions_plan[i].action.output.rseed, "real output action note rseed");
print_buffer(&v->actions_plan[i].action.output.value_blinding, "real output action note value_blinding");
print_buffer(&v->actions_plan[i].action.output.proof_blinding_r, "real output action note proof_blinding_r");
Expand Down
24 changes: 14 additions & 10 deletions app/src/parser_interface.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
#include <string.h>

#include "keys_def.h"
#include "protobuf/penumbra/core/transaction/v1/transaction.pb.h"
#include "rslib.h"
#include "zxformat.h"
#include "protobuf/penumbra/core/transaction/v1/transaction.pb.h"

void print_buffer_interface(uint8_t *buffer, size_t len, const char *title) {
#if defined(LEDGER_SPECIFIC)
Expand All @@ -47,27 +47,31 @@ zxerr_t compute_transaction_plan(transaction_plan_t *plan, uint8_t *effect_hash,
return zxerr_ok;
}

zxerr_t compute_action_hash(action_t *action, spend_key_bytes_t *sk_bytes, bytes_t *memo_key,action_hash_t *output) {
if (action == NULL || output == NULL)
return zxerr_unknown;
zxerr_t compute_action_hash(action_t *action, spend_key_bytes_t *sk_bytes, bytes_t *memo_key, action_hash_t *output) {
if (action == NULL || output == NULL) return zxerr_unknown;

switch (action->action_type) {
case penumbra_core_transaction_v1_ActionPlan_spend_tag:
if (sk_bytes == NULL)
if (sk_bytes == NULL) {
return zxerr_unknown;
if (rs_spend_action_hash(sk_bytes, &action->action.spend, (uint8_t *)output, 64) != parser_ok)
}
if (rs_spend_action_hash(sk_bytes, &action->action.spend, (uint8_t *)output, 64) != parser_ok) {
return zxerr_encoding_failed;
}
break;
case penumbra_core_transaction_v1_ActionPlan_output_tag:
if (sk_bytes == NULL)
return zxerr_unknown;
if (rs_output_action_hash(sk_bytes, &action->action.output, memo_key, (uint8_t *)output, 64) != parser_ok)
if (sk_bytes == NULL) {
return zxerr_unknown;
}
if (rs_output_action_hash(sk_bytes, &action->action.output, memo_key, (uint8_t *)output, 64) != parser_ok) {
return zxerr_encoding_failed;
}
break;
case penumbra_core_transaction_v1_ActionPlan_delegate_tag:
case penumbra_core_transaction_v1_ActionPlan_undelegate_tag:
if (rs_generic_action_hash(&action->action_data, action->action_type, (uint8_t *)output, 64) != parser_ok)
if (rs_generic_action_hash(&action->action_data, action->action_type, (uint8_t *)output, 64) != parser_ok) {
return zxerr_encoding_failed;
}
break;
default:
return zxerr_unknown;
Expand Down
22 changes: 12 additions & 10 deletions tests/parser_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,32 +28,34 @@
#include "parser_txdef.h"
#include "zxformat.h"


using namespace std;

TEST(SCALE, ReadBytes) {
parser_context_t ctx = {0};
parser_tx_t tx_obj = {0};
parser_error_t err;
uint8_t buffer[6000];
auto bufferLen = parseHexString(
buffer, sizeof(buffer),
"0a47ca02440a220a20f9b7b3723506cca29bdfb54b409d7ffff96f23aaa8998101f23ff9ec4c955c861a0a08b9a1f789a28c94de02220a08b5a8ba94d2b2ed8d032a0608d10910d109123808e0d6e5821b1223706e7964772d31383432333733323334373436313838363932363135333332333035341a0b0a0908c6e09fded6c3cb37");
auto bufferLen =
parseHexString(buffer, sizeof(buffer),
"0a47ca02440a220a20f9b7b3723506cca29bdfb54b409d7ffff96f23aaa8998101f23ff9ec4c955c861a0a08b9a1f789a28c"
"94de02220a08b5a8ba94d2b2ed8d032a0608d10910d109123808e0d6e5821b1223706e7964772d3138343233373332333437"
"3436313838363932363135333332333035341a0b0a0908c6e09fded6c3cb37");

spend_key_bytes_t sk_bytes = {0};
std::array<uint8_t, 32> sk_bytes_raw = {
0xa1, 0xff, 0xba, 0x0c, 0x37, 0x93, 0x1f, 0x0a, 0x62, 0x61, 0x37, 0x52, 0x0d, 0xa6, 0x50, 0x63,
0x2d, 0x35, 0x85, 0x3b, 0xf5, 0x91, 0xb3, 0x6b, 0xb4, 0x28, 0x63, 0x0a, 0x4d, 0x87, 0xc4, 0xdc
};
std::array<uint8_t, 32> sk_bytes_raw = {0xa1, 0xff, 0xba, 0x0c, 0x37, 0x93, 0x1f, 0x0a, 0x62, 0x61, 0x37,
0x52, 0x0d, 0xa6, 0x50, 0x63, 0x2d, 0x35, 0x85, 0x3b, 0xf5, 0x91,
0xb3, 0x6b, 0xb4, 0x28, 0x63, 0x0a, 0x4d, 0x87, 0xc4, 0xdc};
std::copy(sk_bytes_raw.begin(), sk_bytes_raw.end(), sk_bytes);
ctx.sk_bytes = &sk_bytes;

err = parser_parse(&ctx, buffer, bufferLen, &tx_obj);
ASSERT_EQ(err, parser_ok) << parser_getErrorDescription(err);

std::string expected = "09cb80cda44cd051971bcc9fa1acc71a4d4366d52b2f4ee49fdc15eb18d30dbde75373d5cfbadf467277da61907a21ec8ce3d5dd9c04af70ee67ce4beff7bcae";
std::string expected =
"09cb80cda44cd051971bcc9fa1acc71a4d4366d52b2f4ee49fdc15eb18d30dbde75373d5cfbadf467277da61907a21ec8ce3d5dd9c04af70ee6"
"7ce4beff7bcae";
char actual[129];
array_to_hexstr(actual, sizeof(actual), tx_obj.effect_hash, sizeof(tx_obj.effect_hash));

EXPECT_EQ(std::string(actual), expected);
}
12 changes: 5 additions & 7 deletions tests/plan_effect_hash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,10 @@ void check_testcase(const testcase_t &tc, bool expert_mode) {
parser_context_t ctx = {0};
parser_error_t err;

spend_key_bytes_t sk_bytes = {0};
std::array<uint8_t, 32> sk_bytes_raw = {
0xa1, 0xff, 0xba, 0x0c, 0x37, 0x93, 0x1f, 0x0a, 0x62, 0x61, 0x37, 0x52, 0x0d, 0xa6, 0x50, 0x63,
0x2d, 0x35, 0x85, 0x3b, 0xf5, 0x91, 0xb3, 0x6b, 0xb4, 0x28, 0x63, 0x0a, 0x4d, 0x87, 0xc4, 0xdc
};
spend_key_bytes_t sk_bytes = {0};
std::array<uint8_t, 32> sk_bytes_raw = {0xa1, 0xff, 0xba, 0x0c, 0x37, 0x93, 0x1f, 0x0a, 0x62, 0x61, 0x37,
0x52, 0x0d, 0xa6, 0x50, 0x63, 0x2d, 0x35, 0x85, 0x3b, 0xf5, 0x91,
0xb3, 0x6b, 0xb4, 0x28, 0x63, 0x0a, 0x4d, 0x87, 0xc4, 0xdc};
std::copy(sk_bytes_raw.begin(), sk_bytes_raw.end(), sk_bytes);
ctx.sk_bytes = &sk_bytes;

Expand All @@ -102,11 +101,10 @@ void check_testcase(const testcase_t &tc, bool expert_mode) {
std::string expected = tc.hash;
char actual[129];
array_to_hexstr(actual, sizeof(actual), tx_obj.effect_hash, sizeof(tx_obj.effect_hash));

EXPECT_EQ(std::string(actual), expected);
}


INSTANTIATE_TEST_SUITE_P

(JsonTestCasesCurrentTxVer, JsonTestsA, ::testing::ValuesIn(GetJsonTestCases("plan_effect_hash_testcases.json")));
Expand Down

0 comments on commit 635eaed

Please sign in to comment.