Extract data for external plugins from internal plugin file

This commit is contained in:
Francois Beutin
2024-01-15 19:25:44 +01:00
parent ca9d5c9c77
commit 294f88195b
17 changed files with 94 additions and 46 deletions

View File

@@ -1,6 +1,7 @@
#include <string.h>
#include "eth_plugin_handler.h"
#include "eth_plugin_internal.h"
#include "plugin_utils.h"
#include "shared_context.h"
#include "network.h"

View File

@@ -6,6 +6,7 @@
#include "os.h"
#include "cx.h"
#include "tokens.h"
#include "tx_content.h"
/*************************************************************************************************
* Comments provided in this file are quick reminders on the usage of the plugin interface *

View File

@@ -1,38 +1,11 @@
#include <string.h>
#include "eth_plugin_internal.h"
#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

View File

@@ -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*);
@@ -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_

42
src/plugin_utils.c Normal file
View File

@@ -0,0 +1,42 @@
#include <string.h>
#include "utils.h"
#include "plugin_utils.h"
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;
}
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;
}

35
src/plugin_utils.h Normal file
View File

@@ -0,0 +1,35 @@
/*****************************************************************************
* Ledger
* (c) 2023 Ledger SAS
*
* 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 <stdint.h>
#include <stdbool.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);
// 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);
bool find_selector(uint32_t selector, const uint32_t* array, size_t size, size_t* idx);

View File

@@ -3,6 +3,7 @@
#include "public_keys.h"
#include "eth_plugin_interface.h"
#include "eth_plugin_internal.h"
#include "plugin_utils.h"
#include "common_ui.h"
#include "os_io_seproxyhal.h"

View File

@@ -3,6 +3,7 @@
#include "tokens.h"
#include "eth_plugin_interface.h"
#include "eth_plugin_internal.h"
#include "plugin_utils.h"
#include "utils.h"
#include "common_ui.h"
#include "os_io_seproxyhal.h"

View File

@@ -2,6 +2,7 @@
#include <string.h>
#include "erc1155_plugin.h"
#include "plugin_utils.h"
#include "eth_plugin_internal.h"
#include "eth_plugin_handler.h"

View File

@@ -2,6 +2,7 @@
#include <string.h>
#include "erc1155_plugin.h"
#include "plugin_utils.h"
#include "eth_plugin_internal.h"
#include "utils.h"

View File

@@ -2,6 +2,7 @@
#include <string.h>
#include "erc1155_plugin.h"
#include "eth_plugin_internal.h"
#include "eth_plugin_interface.h"
#include "utils.h"

View File

@@ -2,6 +2,7 @@
#include "eth_plugin_internal.h"
#include "eth_plugin_handler.h"
#include "shared_context.h"
#include "plugin_utils.h"
#include "ethUstream.h"
#include "utils.h"

View File

@@ -1,6 +1,7 @@
#ifdef HAVE_NFT_SUPPORT
#include <string.h>
#include "plugin_utils.h"
#include "erc721_plugin.h"
#include "eth_plugin_internal.h"
#include "eth_plugin_interface.h"

View File

@@ -1,6 +1,7 @@
#ifdef HAVE_NFT_SUPPORT
#include "erc721_plugin.h"
#include "plugin_utils.h"
#include "eth_plugin_internal.h"
static void handle_approve(ethPluginProvideParameter_t *msg, erc721_context_t *context) {

View File

@@ -2,6 +2,7 @@
#include <string.h>
#include "erc721_plugin.h"
#include "eth_plugin_internal.h"
#include "eth_plugin_interface.h"
#include "utils.h"

View File

@@ -4,6 +4,7 @@
#include "shared_context.h" // TODO : rewrite as independant code
#include "eth_plugin_internal.h" // TODO : rewrite as independant code
#include "stark_utils.h"
#include "plugin_utils.h"
#include "utils.h"
#include "apdu_constants.h"

View File

@@ -157,7 +157,7 @@ if __name__ == "__main__":
"src/tokens.h",
"src/utils.h",
"src/tx_content.h",
"src/eth_plugin_internal.h",
"src/plugin_utils.h",
"src/nft.h",
"src/extra_info.h",
"src/caller_api.h",
@@ -200,7 +200,7 @@ if __name__ == "__main__":
# extract and merge function bodies
c_files_to_merge = [
"src/utils.c",
"src/eth_plugin_internal.c",
"src/plugin_utils.c",
]
merge_c_files(c_files_to_merge, nodes_to_extract["fn"])