Skip to content

Commit

Permalink
Merge pull request #520 from LedgerHQ/fbe/redo_file_architecture_clean
Browse files Browse the repository at this point in the history
Fbe/redo file architecture clean
  • Loading branch information
fbeutin-ledger authored Feb 2, 2024
2 parents df6a781 + bc76a31 commit 4d6feea
Show file tree
Hide file tree
Showing 99 changed files with 1,124 additions and 1,023 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/sdk-generation.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:
persist-credentials: false

- name: Build new SDK
run: python tools/build_sdk.py
run: ./tools/build_sdk.sh

- name: Extract branch name
shell: bash
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ ifeq ($(CHAIN),ethereum)
endif

# rebuild SDK
$(shell python3 tools/build_sdk.py)
$(shell ./tools/build_sdk.sh)

# check if a difference is noticed (fail if it happens in CI build)
ifneq ($(shell git status | grep 'ethereum-plugin-sdk'),)
Expand Down
4 changes: 2 additions & 2 deletions src_common/ethUstream.c → src/ethUstream.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
#include <string.h>

#include "ethUstream.h"
#include "ethUtils.h"
#include "utils.h"
#include "rlp_utils.h"
#include "common_utils.h"

#define MAX_INT256 32
#define MAX_ADDRESS 20
Expand Down
30 changes: 4 additions & 26 deletions src_common/ethUstream.h → src/ethUstream.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@
* limitations under the License.
********************************************************************************/

#ifndef _ETHUSTREAM_H_
#define _ETHUSTREAM_H_
#pragma once

#include <stdbool.h>
#include <stdint.h>

#include "os.h"
#include "cx.h"
#include "common_utils.h"
#include "tx_content.h"

struct txContext_t;

Expand All @@ -35,10 +36,7 @@ typedef enum customStatus_e {

typedef customStatus_e (*ustreamProcess_t)(struct txContext_t *context);

#define TX_FLAG_TYPE 0x01
#define ADDRESS_LENGTH 20
#define INT128_LENGTH 16
#define INT256_LENGTH 32
#define TX_FLAG_TYPE 0x01

// First variant of every Tx enum.
#define RLP_NONE 0
Expand Down Expand Up @@ -114,24 +112,6 @@ typedef enum parserStatus_e {
USTREAM_CONTINUE // Used internally to signify we can keep on parsing
} parserStatus_e;

typedef struct txInt256_t {
uint8_t value[INT256_LENGTH];
uint8_t length;
} txInt256_t;

typedef struct txContent_t {
txInt256_t gasprice; // Used as MaxFeePerGas when dealing with EIP1559 transactions.
txInt256_t startgas; // Also known as `gasLimit`.
txInt256_t value;
txInt256_t nonce;
txInt256_t chainID;
uint8_t destination[ADDRESS_LENGTH];
uint8_t destinationLength;
uint8_t v[8];
uint8_t vLength;
bool dataPresent;
} txContent_t;

typedef struct txContext_t {
uint8_t currentField;
cx_sha3_t *sha3;
Expand Down Expand Up @@ -164,5 +144,3 @@ parserStatus_e processTx(txContext_t *context,
parserStatus_e continueTx(txContext_t *context);
void copyTxData(txContext_t *context, uint8_t *out, uint32_t length);
uint8_t readTxByte(txContext_t *context);

#endif // _ETHUSTREAM_H_
2 changes: 1 addition & 1 deletion src/eth_plugin_handler.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#include <string.h>
#include "eth_plugin_handler.h"
#include "eth_plugin_internal.h"
#include "plugin_utils.h"
#include "shared_context.h"
#include "network.h"
#include "ethUtils.h"

void eth_plugin_prepare_init(ethPluginInitContract_t *init,
const uint8_t *selector,
Expand Down
2 changes: 2 additions & 0 deletions src/eth_plugin_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#define NO_EXTRA_INFO(ctx, idx) \
(allzeroes(&(ctx.transactionContext.extraInfo[idx]), sizeof(extraInfo_t)))

#define NO_NFT_METADATA (NO_EXTRA_INFO(tmpCtx, 1))

void eth_plugin_prepare_init(ethPluginInitContract_t *init,
const uint8_t *selector,
uint32_t dataSize);
Expand Down
30 changes: 1 addition & 29 deletions src/eth_plugin_internal.c
Original file line number Diff line number Diff line change
@@ -1,39 +1,11 @@
#include <string.h>
#include "eth_plugin_internal.h"
#include "ethUtils.h" // allzeroes
#include "plugin_utils.h"

bool erc20_plugin_available_check(void);

void erc20_plugin_call(int message, void* parameters);

void copy_address(uint8_t* dst, const uint8_t* parameter, uint8_t dst_size) {
uint8_t copy_size = MIN(dst_size, ADDRESS_LENGTH);
memmove(dst, parameter + PARAMETER_LENGTH - copy_size, copy_size);
}

void copy_parameter(uint8_t* dst, const uint8_t* parameter, uint8_t dst_size) {
uint8_t copy_size = MIN(dst_size, PARAMETER_LENGTH);
memmove(dst, parameter, copy_size);
}

bool U2BE_from_parameter(const uint8_t* parameter, uint16_t* value) {
if (allzeroes(parameter, PARAMETER_LENGTH - sizeof(uint16_t))) {
*value = U2BE(parameter, PARAMETER_LENGTH - sizeof(uint16_t));
return true;
}

return false;
}

bool U4BE_from_parameter(const uint8_t* parameter, uint32_t* value) {
if (allzeroes(parameter, PARAMETER_LENGTH - sizeof(uint32_t))) {
*value = U4BE(parameter, PARAMETER_LENGTH - sizeof(uint32_t));
return true;
}

return false;
}

#ifdef HAVE_STARKWARE
void starkware_plugin_call(int message, void* parameters);
#endif
Expand Down
18 changes: 2 additions & 16 deletions src/eth_plugin_internal.h
Original file line number Diff line number Diff line change
@@ -1,25 +1,13 @@
#ifndef _ETH_PLUGIN_INTERNAL_H_
#define _ETH_PLUGIN_INTERNAL_H_
#pragma once

#include <stdint.h>
#include <stdbool.h>
#include "shared_context.h"
#include "eth_plugin_interface.h"

#define SELECTOR_SIZE 4
#define PARAMETER_LENGTH 32

void copy_address(uint8_t* dst, const uint8_t* parameter, uint8_t dst_size);

void copy_parameter(uint8_t* dst, const uint8_t* parameter, uint8_t dst_size);

void erc721_plugin_call(int message, void* parameters);
void erc1155_plugin_call(int message, void* parameters);

// Get the value from the beginning of the parameter (right to left) and check if the rest of it is
// zero
bool U2BE_from_parameter(const uint8_t* parameter, uint16_t* value);
bool U4BE_from_parameter(const uint8_t* parameter, uint32_t* value);

typedef bool (*PluginAvailableCheck)(void);
typedef void (*PluginCall)(int, void*);

Expand Down Expand Up @@ -49,5 +37,3 @@ extern const uint8_t* const STARKWARE_SELECTORS[NUM_STARKWARE_SELECTORS];
#endif

extern internalEthPlugin_t const INTERNAL_ETH_PLUGINS[];

#endif // _ETH_PLUGIN_INTERNAL_H_
2 changes: 1 addition & 1 deletion src/tokens.c → src/extra_tokens.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

#ifdef HAVE_TOKENS_EXTRA_LIST

#include "tokens.h"
#include "extra_tokens.h"

const tokenDefinition_t TOKENS_EXTRA[NUM_TOKENS_EXTRA] = {

Expand Down
26 changes: 12 additions & 14 deletions src_plugin_sdk/plugin_utils.c → src/extra_tokens.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*****************************************************************************
* Ledger Plugin SDK
* (c) 2023 Ledger SAS
/*******************************************************************************
* Ledger Ethereum App
* (c) 2016-2019 Ledger
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -13,16 +13,14 @@
* 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 "plugin_utils.h"
#pragma once

bool find_selector(uint32_t selector, const uint32_t *array, size_t size, size_t *idx) {
for (size_t i = 0; i < size; ++i) {
if (selector == array[i]) {
if (idx != NULL) *idx = i;
return true;
}
}
return false;
}
#ifdef HAVE_TOKENS_EXTRA_LIST

#define NUM_TOKENS_EXTRA 8

extern tokenDefinition_t const TOKENS_EXTRA[NUM_TOKENS_EXTRA];

#endif
1 change: 0 additions & 1 deletion src/handle_check_address.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include "handle_check_address.h"
#include "os.h"
#include "shared_context.h"
#include "ethUtils.h"
#include "string.h"

#define ZERO(x) explicit_bzero(&x, sizeof(x))
Expand Down
9 changes: 5 additions & 4 deletions src/handle_get_printable_amount.c
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
#include <stdint.h>
#include <os.h>

#include "swap_utils.h"
#include "handle_get_printable_amount.h"
#include "shared_context.h"
#include "ethUtils.h"
#include "utils.h"
#include "common_utils.h"
#include "uint256.h"
#include "string.h"
#include <stdint.h>
#include <os.h>

void handle_get_printable_amount(get_printable_amount_parameters_t* params,
chain_config_t* config) {
Expand Down
3 changes: 2 additions & 1 deletion src/handle_swap_sign_transaction.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#include "os_io_seproxyhal.h"
#include "os.h"
#include "ux.h"
#include "swap_utils.h"
#include "handle_swap_sign_transaction.h"
#include "shared_context.h"
#include "utils.h"
#include "common_utils.h"
#ifdef HAVE_NBGL
#include "nbgl_use_case.h"
#endif // HAVE_NBGL
Expand Down
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
#include "os_io_seproxyhal.h"

#include "glyphs.h"
#include "utils.h"
#include "common_utils.h"

#include "swap_lib_calls.h"
#include "handle_swap_sign_transaction.h"
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
31 changes: 31 additions & 0 deletions src_common/network.c → src/network.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#include "os_utils.h"
#include "os_pic.h"
#include "network.h"
#include "shared_context.h"
#include "common_utils.h"

typedef struct network_info_s {
const char *name;
Expand Down Expand Up @@ -113,3 +115,32 @@ const char *get_network_ticker_from_chain_id(const uint64_t *chain_id) {
bool chain_is_ethereum_compatible(const uint64_t *chain_id) {
return get_network_from_chain_id(chain_id) != NULL;
}

// Returns the chain ID. Defaults to 0 if txType was not found (For TX).
uint64_t get_tx_chain_id(void) {
uint64_t chain_id = 0;

switch (txContext.txType) {
case LEGACY:
chain_id = u64_from_BE(txContext.content->v, txContext.content->vLength);
break;
case EIP2930:
case EIP1559:
chain_id = u64_from_BE(tmpContent.txContent.chainID.value,
tmpContent.txContent.chainID.length);
break;
default:
PRINTF("Txtype `%d` not supported while generating chainID\n", txContext.txType);
break;
}
return chain_id;
}

const char *get_displayable_ticker(const uint64_t *chain_id) {
const char *ticker = get_network_ticker_from_chain_id(chain_id);

if (ticker == NULL) {
ticker = chainConfig->coinName;
}
return ticker;
}
7 changes: 4 additions & 3 deletions src_common/network.h → src/network.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#ifndef _NETWORK_H_
#define _NETWORK_H_
#pragma once

#include <stdint.h>
#include <stdbool.h>
Expand All @@ -9,4 +8,6 @@ const char *get_network_ticker_from_chain_id(const uint64_t *chain_id);

bool chain_is_ethereum_compatible(const uint64_t *chain_id);

#endif // _NETWORK_H_
uint64_t get_tx_chain_id(void);

const char *get_displayable_ticker(const uint64_t *chain_id);
14 changes: 0 additions & 14 deletions src/nft.h

This file was deleted.

File renamed without changes.
File renamed without changes.
Loading

0 comments on commit 4d6feea

Please sign in to comment.