Skip to content

Commit 294f881

Browse files
Extract data for external plugins from internal plugin file
1 parent ca9d5c9 commit 294f881

17 files changed

+94
-46
lines changed

src/eth_plugin_handler.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include <string.h>
22
#include "eth_plugin_handler.h"
33
#include "eth_plugin_internal.h"
4+
#include "plugin_utils.h"
45
#include "shared_context.h"
56
#include "network.h"
67

src/eth_plugin_interface.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "os.h"
77
#include "cx.h"
88
#include "tokens.h"
9+
#include "tx_content.h"
910

1011
/*************************************************************************************************
1112
* Comments provided in this file are quick reminders on the usage of the plugin interface *

src/eth_plugin_internal.c

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,11 @@
11
#include <string.h>
22
#include "eth_plugin_internal.h"
3+
#include "plugin_utils.h"
34

45
bool erc20_plugin_available_check(void);
56

67
void erc20_plugin_call(int message, void* parameters);
78

8-
void copy_address(uint8_t* dst, const uint8_t* parameter, uint8_t dst_size) {
9-
uint8_t copy_size = MIN(dst_size, ADDRESS_LENGTH);
10-
memmove(dst, parameter + PARAMETER_LENGTH - copy_size, copy_size);
11-
}
12-
13-
void copy_parameter(uint8_t* dst, const uint8_t* parameter, uint8_t dst_size) {
14-
uint8_t copy_size = MIN(dst_size, PARAMETER_LENGTH);
15-
memmove(dst, parameter, copy_size);
16-
}
17-
18-
bool U2BE_from_parameter(const uint8_t* parameter, uint16_t* value) {
19-
if (allzeroes(parameter, PARAMETER_LENGTH - sizeof(uint16_t))) {
20-
*value = U2BE(parameter, PARAMETER_LENGTH - sizeof(uint16_t));
21-
return true;
22-
}
23-
24-
return false;
25-
}
26-
27-
bool U4BE_from_parameter(const uint8_t* parameter, uint32_t* value) {
28-
if (allzeroes(parameter, PARAMETER_LENGTH - sizeof(uint32_t))) {
29-
*value = U4BE(parameter, PARAMETER_LENGTH - sizeof(uint32_t));
30-
return true;
31-
}
32-
33-
return false;
34-
}
35-
369
#ifdef HAVE_STARKWARE
3710
void starkware_plugin_call(int message, void* parameters);
3811
#endif

src/eth_plugin_internal.h

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,13 @@
1-
#ifndef _ETH_PLUGIN_INTERNAL_H_
2-
#define _ETH_PLUGIN_INTERNAL_H_
1+
#pragma once
32

43
#include <stdint.h>
54
#include <stdbool.h>
5+
#include "shared_context.h"
66
#include "eth_plugin_interface.h"
77

8-
#define SELECTOR_SIZE 4
9-
#define PARAMETER_LENGTH 32
10-
11-
void copy_address(uint8_t* dst, const uint8_t* parameter, uint8_t dst_size);
12-
13-
void copy_parameter(uint8_t* dst, const uint8_t* parameter, uint8_t dst_size);
14-
158
void erc721_plugin_call(int message, void* parameters);
169
void erc1155_plugin_call(int message, void* parameters);
1710

18-
// Get the value from the beginning of the parameter (right to left) and check if the rest of it is
19-
// zero
20-
bool U2BE_from_parameter(const uint8_t* parameter, uint16_t* value);
21-
bool U4BE_from_parameter(const uint8_t* parameter, uint32_t* value);
22-
2311
typedef bool (*PluginAvailableCheck)(void);
2412
typedef void (*PluginCall)(int, void*);
2513

@@ -49,5 +37,3 @@ extern const uint8_t* const STARKWARE_SELECTORS[NUM_STARKWARE_SELECTORS];
4937
#endif
5038

5139
extern internalEthPlugin_t const INTERNAL_ETH_PLUGINS[];
52-
53-
#endif // _ETH_PLUGIN_INTERNAL_H_

src/plugin_utils.c

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#include <string.h>
2+
3+
#include "utils.h"
4+
#include "plugin_utils.h"
5+
6+
void copy_address(uint8_t* dst, const uint8_t* parameter, uint8_t dst_size) {
7+
uint8_t copy_size = MIN(dst_size, ADDRESS_LENGTH);
8+
memmove(dst, parameter + PARAMETER_LENGTH - copy_size, copy_size);
9+
}
10+
11+
void copy_parameter(uint8_t* dst, const uint8_t* parameter, uint8_t dst_size) {
12+
uint8_t copy_size = MIN(dst_size, PARAMETER_LENGTH);
13+
memmove(dst, parameter, copy_size);
14+
}
15+
16+
bool U2BE_from_parameter(const uint8_t* parameter, uint16_t* value) {
17+
if (allzeroes(parameter, PARAMETER_LENGTH - sizeof(uint16_t))) {
18+
*value = U2BE(parameter, PARAMETER_LENGTH - sizeof(uint16_t));
19+
return true;
20+
}
21+
22+
return false;
23+
}
24+
25+
bool U4BE_from_parameter(const uint8_t* parameter, uint32_t* value) {
26+
if (allzeroes(parameter, PARAMETER_LENGTH - sizeof(uint32_t))) {
27+
*value = U4BE(parameter, PARAMETER_LENGTH - sizeof(uint32_t));
28+
return true;
29+
}
30+
31+
return false;
32+
}
33+
34+
bool find_selector(uint32_t selector, const uint32_t* array, size_t size, size_t* idx) {
35+
for (size_t i = 0; i < size; ++i) {
36+
if (selector == array[i]) {
37+
if (idx != NULL) *idx = i;
38+
return true;
39+
}
40+
}
41+
return false;
42+
}

src/plugin_utils.h

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*****************************************************************************
2+
* Ledger
3+
* (c) 2023 Ledger SAS
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*****************************************************************************/
17+
18+
#pragma once
19+
20+
#include <stdint.h>
21+
#include <stdbool.h>
22+
23+
#define SELECTOR_SIZE 4
24+
#define PARAMETER_LENGTH 32
25+
26+
void copy_address(uint8_t* dst, const uint8_t* parameter, uint8_t dst_size);
27+
28+
void copy_parameter(uint8_t* dst, const uint8_t* parameter, uint8_t dst_size);
29+
30+
// Get the value from the beginning of the parameter (right to left) and check if the rest of it is
31+
// zero
32+
bool U2BE_from_parameter(const uint8_t* parameter, uint16_t* value);
33+
bool U4BE_from_parameter(const uint8_t* parameter, uint32_t* value);
34+
35+
bool find_selector(uint32_t selector, const uint32_t* array, size_t size, size_t* idx);

src_features/setExternalPlugin/cmd_setExternalPlugin.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "public_keys.h"
44
#include "eth_plugin_interface.h"
55
#include "eth_plugin_internal.h"
6+
#include "plugin_utils.h"
67
#include "common_ui.h"
78
#include "os_io_seproxyhal.h"
89

src_features/setPlugin/cmd_setPlugin.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "tokens.h"
44
#include "eth_plugin_interface.h"
55
#include "eth_plugin_internal.h"
6+
#include "plugin_utils.h"
67
#include "utils.h"
78
#include "common_ui.h"
89
#include "os_io_seproxyhal.h"

src_plugins/erc1155/erc1155_plugin.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include <string.h>
44
#include "erc1155_plugin.h"
5+
#include "plugin_utils.h"
56
#include "eth_plugin_internal.h"
67
#include "eth_plugin_handler.h"
78

src_plugins/erc1155/erc1155_provide_parameters.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include <string.h>
44
#include "erc1155_plugin.h"
5+
#include "plugin_utils.h"
56
#include "eth_plugin_internal.h"
67
#include "utils.h"
78

0 commit comments

Comments
 (0)