Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
chcmedeiros committed Jan 31, 2024
1 parent 8975d55 commit 72ec868
Show file tree
Hide file tree
Showing 7 changed files with 119,654 additions and 119,574 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ file(GLOB_RECURSE LIB_SRC
${CMAKE_CURRENT_SOURCE_DIR}/app/src/parser_impl.c
${CMAKE_CURRENT_SOURCE_DIR}/app/src/parser_impl_common.c
${CMAKE_CURRENT_SOURCE_DIR}/app/src/parser_impl_txn.c
${CMAKE_CURRENT_SOURCE_DIR}/app/src/parser_impl_masp.c
${CMAKE_CURRENT_SOURCE_DIR}/app/src/crypto_helper.c
${CMAKE_CURRENT_SOURCE_DIR}/app/src/leb128.c
)
Expand Down
9 changes: 9 additions & 0 deletions app/src/parser_impl_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,15 @@ extern "C" {
#define IBC_NORMAL_PARAMS 8
#define IBC_EXPERT_PARAMS 13

#define DISCRIMINANT_DATA 0x00
#define DISCRIMINANT_EXTRA_DATA 0x01
#define DISCRIMINANT_CODE 0x02
#define DISCRIMINANT_SIGNATURE 0x03
#define DISCRIMINANT_CIPHERTEXT 0x04
#define DISCRIMINANT_MASP_TX 0x05
#define DISCRIMINANT_MASP_BUILDER 0x06


parser_error_t readByte(parser_context_t *ctx, uint8_t *byte);
parser_error_t readBytes(parser_context_t *ctx, const uint8_t **output, uint16_t outputLen);
parser_error_t readUint16(parser_context_t *ctx, uint16_t *value);
Expand Down
63 changes: 63 additions & 0 deletions app/src/parser_impl_masp.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*******************************************************************************
* (c) 2018 - 2023 Zondax AG
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
********************************************************************************/
#include "parser_impl_masp.h"
#include "parser_impl_common.h"
#include "parser_txdef.h"

parser_error_t readMaspTx(parser_context_t *ctx, masp_tx_section_t *maspTx) {
if (ctx == NULL || maspTx == NULL) {
return parser_unexpected_error;
}

uint8_t sectionMaspTx = 0;
CHECK_ERROR(readByte(ctx, &sectionMaspTx))
if (sectionMaspTx != DISCRIMINANT_MASP_TX) {
return parser_unexpected_value;
}

// Read version for now only MaspV5 that translates to 0x02
CHECK_ERROR(readUint32(ctx, &maspTx->data.tx_version))
if (maspTx->data.tx_version != MASPV5_TX_VERSION) {
return parser_unexpected_value;
}
CHECK_ERROR(readUint32(ctx, &maspTx->data.version_group_id))
if (maspTx->data.version_group_id != MASPV5_VERSION_GROUP_ID) {
return parser_unexpected_value;
}

// Read branch id, unique enum for now
CHECK_ERROR(readUint32(ctx, &maspTx->data.consensus_branch_id))
if (maspTx->data.consensus_branch_id != BRANCH_ID_IDENTIFIER) {
return parser_unexpected_value;
}

// Read lock time
CHECK_ERROR(readUint32(ctx, &maspTx->data.lock_time))

// Read expiry_height
CHECK_ERROR(readUint32(ctx, &maspTx->data.expiry_height))

CHECK_ERROR(readByte(ctx, &maspTx->data.has_transparent_bundle))
if(maspTx->data.has_transparent_bundle) {
// funtion to parse transparent bundle
}

CHECK_ERROR(readByte(ctx, &maspTx->data.has_sapling_bundle))
if(maspTx->data.has_sapling_bundle) {
// funtion to parse sapling bundle
}
return parser_ok;
}
34 changes: 34 additions & 0 deletions app/src/parser_impl_masp.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*******************************************************************************
* (c) 2018 - 2022 Zondax AG
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
********************************************************************************/
#pragma once
#include "parser_common.h"
#include <zxmacros.h>
#include "zxtypes.h"
#include "parser_txdef.h"

#ifdef __cplusplus
extern "C" {
#endif

#define MASPV5_TX_VERSION 0x02
#define MASPV5_VERSION_GROUP_ID 0x26A7270A
#define BRANCH_ID_IDENTIFIER 0xE9FF75A6

parser_error_t readMaspTx(parser_context_t *ctx, masp_tx_section_t *maspTx);

#ifdef __cplusplus
}
#endif
36 changes: 4 additions & 32 deletions app/src/parser_impl_txn.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
********************************************************************************/
#include "parser_impl_common.h"
#include "parser_txdef.h"
#include "parser_impl_masp.h"
#include "crypto_helper.h"
#include "leb128.h"
#include "bech32.h"
Expand All @@ -23,14 +24,6 @@

#define ADDRESS_LEN_BYTES 21

#define DISCRIMINANT_DATA 0x00
#define DISCRIMINANT_EXTRA_DATA 0x01
#define DISCRIMINANT_CODE 0x02
#define DISCRIMINANT_SIGNATURE 0x03
#define DISCRIMINANT_CIPHERTEXT 0x04
#define DISCRIMINANT_MASP_TX 0x05
#define DISCRIMINANT_MASP_BUILDER 0x06

static const txn_types_t allowed_txn[] = {
{"tx_bond.wasm", Bond},
{"tx_unbond.wasm", Unbond},
Expand Down Expand Up @@ -1087,26 +1080,6 @@ static parser_error_t readCodeSection(parser_context_t *ctx, section_t *code) {
return parser_ok;
}

#if(0)
static parser_error_t readCiphertext(parser_context_t *ctx, section_t *ciphertext) {
(void) ctx;
(void) ciphertext;
return parser_ok;
}


static parser_error_t readMaspTx(parser_context_t *ctx, section_t *maspTx) {
ctx->offset += 1171; // <- Transfer 2 // Transfer 1 -> 2403;//todo figure out correct number, fix this hack
(void) maspTx;
return parser_ok;
}

static parser_error_t readMaspBuilder(parser_context_t *ctx, section_t *maspBuilder) {
ctx->offset += 941; // <- Transfer 2 // Transfer 1 -> 3060; //todo figure out correct number, fix this hack
(void) maspBuilder;
return parser_ok;
}
#endif
parser_error_t readSections(parser_context_t *ctx, parser_tx_t *v) {
if (ctx == NULL || v == NULL) {
return parser_unexpected_value;
Expand Down Expand Up @@ -1154,19 +1127,18 @@ parser_error_t readSections(parser_context_t *ctx, parser_tx_t *v) {
signature->idx = i+1;
break;
}
#if(0)
case DISCRIMINANT_CIPHERTEXT:
CHECK_ERROR(readCiphertext(ctx, &v->transaction.sections.ciphertext))
//CHECK_ERROR(readCiphertext(ctx, &v->transaction.sections.ciphertext))
break;

case DISCRIMINANT_MASP_TX:
CHECK_ERROR(readMaspTx(ctx, &v->transaction.sections.maspTx))
break;

case DISCRIMINANT_MASP_BUILDER:
CHECK_ERROR(readMaspBuilder(ctx, &v->transaction.sections.maspBuilder))
//CHECK_ERROR(readMaspBuilder(ctx, &v->transaction.sections.maspBuilder))
break;
#endif

default:
return parser_unexpected_field;
}
Expand Down
11 changes: 6 additions & 5 deletions app/src/parser_txdef.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ typedef struct {
bytes_t authorization; // nothing if Auth; for unauth a Vec<TransparentInputInfo>
} masp_transparent_bundle_t;



#endif

// For masp TxData definition, see:
// https://github.com/anoma/masp/blob/0d7dc07d24b878e9162c25260ed744265dd2f748/masp_primitives/src/transaction.rs#L189-L190
typedef struct {
Expand All @@ -118,16 +122,15 @@ typedef struct {
uint32_t lock_time;
uint32_t expiry_height;
uint8_t has_transparent_bundle;
masp_transparent_bundle_t transparent_bundle;
//masp_transparent_bundle_t transparent_bundle;
uint8_t has_sapling_bundle;
masp_sapling_bundle_t sapling_bundle;
//masp_sapling_bundle_t sapling_bundle;
} masp_tx_data_t;

typedef struct {
bytes_t tx_id; // [u8;32]
masp_tx_data_t data;
} masp_tx_section_t;
#endif

typedef struct {
bytes_t extBytes;
Expand Down Expand Up @@ -158,11 +161,9 @@ typedef struct {
section_t data;
section_t extraData[MAX_EXTRA_DATA_SECS];
signature_section_t signatures[MAX_SIGNATURE_SECS];
#if(0)
section_t ciphertext; // todo: if we need to parse this in future, it will not be a section_t
masp_tx_section_t maspTx;
section_t maspBuilder; // todo: if we need to parse this in future, it will not be a section_t
#endif
} sections_t;

typedef struct {
Expand Down
Loading

0 comments on commit 72ec868

Please sign in to comment.