|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +''' |
| 4 | +This script extract a few specific definitions from app-ethereum that are |
| 5 | +required to exchange information with ethereum external plugins. |
| 6 | +It should always be launched from app-ethereum: |
| 7 | +
|
| 8 | +python3 ethereum-plugin-sdk/build_sdk.py |
| 9 | +
|
| 10 | +''' |
| 11 | + |
| 12 | +import os |
| 13 | + |
| 14 | + |
| 15 | +def extract_from_headers(sources, nodes_to_extract): |
| 16 | + cat_sources = [] |
| 17 | + for source in sources: |
| 18 | + with open(source, 'r') as f: |
| 19 | + cat_sources += f.readlines() |
| 20 | + |
| 21 | + sdk_body = [] |
| 22 | + for key, values in nodes_to_extract.items(): |
| 23 | + for value in values: |
| 24 | + node = [] |
| 25 | + unclosed_curvy_brackets = False |
| 26 | + unclosed_parantheses = False |
| 27 | + for line in cat_sources: |
| 28 | + if key in line and value in line: |
| 29 | + node += [line] |
| 30 | + unclosed_curvy_brackets = line.count('{') - line.count('}') |
| 31 | + if unclosed_curvy_brackets == False: |
| 32 | + break |
| 33 | + elif (key == "fn" and value in line) or unclosed_parantheses: |
| 34 | + node += [line] |
| 35 | + unclosed_parantheses = line.find(")") == -1 |
| 36 | + if unclosed_parantheses == False: |
| 37 | + break |
| 38 | + elif unclosed_curvy_brackets: |
| 39 | + node += [line] |
| 40 | + unclosed_curvy_brackets += line.count( |
| 41 | + '{') - line.count('}') |
| 42 | + if unclosed_curvy_brackets: |
| 43 | + continue |
| 44 | + else: |
| 45 | + break |
| 46 | + |
| 47 | + sdk_body += [''.join(node)] |
| 48 | + |
| 49 | + return '\n'.join(sdk_body) |
| 50 | + |
| 51 | + |
| 52 | +def extract_from_c_files(sources, nodes_to_extract): |
| 53 | + cat_sources = [] |
| 54 | + for source in sources: |
| 55 | + with open(source, 'r') as f: |
| 56 | + cat_sources += f.readlines() |
| 57 | + |
| 58 | + sdk_body = [] |
| 59 | + for node_name in nodes_to_extract: |
| 60 | + node = [] |
| 61 | + copying = False |
| 62 | + wait_curvy_bracket = True |
| 63 | + for line in cat_sources: |
| 64 | + if node_name in line: |
| 65 | + copying = True |
| 66 | + node += [line] |
| 67 | + unclosed_curvy_brackets = line.count('{') - line.count('}') |
| 68 | + elif copying: |
| 69 | + node += [line] |
| 70 | + unclosed_curvy_brackets += line.count('{') - line.count('}') |
| 71 | + if wait_curvy_bracket: |
| 72 | + wait_curvy_bracket = line.count('}') == 0 |
| 73 | + if unclosed_curvy_brackets != 0 or wait_curvy_bracket: |
| 74 | + continue |
| 75 | + else: |
| 76 | + break |
| 77 | + |
| 78 | + sdk_body += [''.join(node)] |
| 79 | + |
| 80 | + return '\n'.join(sdk_body) |
| 81 | + |
| 82 | + |
| 83 | +def merge_headers(sources, nodes_to_extract): |
| 84 | + includes = [ |
| 85 | + '#include "os.h"', |
| 86 | + '#include "cx.h"', |
| 87 | + '#include <stdbool.h>', |
| 88 | + '#include <string.h>' |
| 89 | + ] |
| 90 | + |
| 91 | + body = extract_from_headers(sources, nodes_to_extract) |
| 92 | + |
| 93 | + eth_internals_h = '\n\n'.join([ |
| 94 | + "/* This file is auto-generated, don't edit it */", |
| 95 | + "#pragma once", |
| 96 | + '\n'.join(includes), |
| 97 | + body |
| 98 | + ]) |
| 99 | + |
| 100 | + with open("ethereum-plugin-sdk/include/eth_internals.h", 'w') as f: |
| 101 | + f.write(eth_internals_h) |
| 102 | + |
| 103 | + |
| 104 | +def copy_header(header_to_copy, merged_headers): |
| 105 | + |
| 106 | + merged_headers = [os.path.basename(path) for path in merged_headers] |
| 107 | + |
| 108 | + with open(header_to_copy, 'r') as f: |
| 109 | + source = f.readlines() |
| 110 | + |
| 111 | + eth_plugin_interface_h = [ |
| 112 | + "/* This file is auto-generated, don't edit it */\n"] |
| 113 | + for line in source: |
| 114 | + eth_plugin_interface_h += [line] |
| 115 | + for header in merged_headers: |
| 116 | + if header in line: |
| 117 | + del eth_plugin_interface_h[-1] |
| 118 | + break |
| 119 | + |
| 120 | + # add '#include "eth_internals.h"' |
| 121 | + include_index = eth_plugin_interface_h.index('#include "cx.h"\n') |
| 122 | + eth_plugin_interface_h.insert( |
| 123 | + include_index+1, '#include "eth_internals.h"\n') |
| 124 | + |
| 125 | + # dump to file |
| 126 | + with open("ethereum-plugin-sdk/include/eth_plugin_interface.h", 'w') as f: |
| 127 | + f.writelines(eth_plugin_interface_h) |
| 128 | + |
| 129 | + |
| 130 | +def merge_c_files(sources, nodes_to_extract): |
| 131 | + includes = [ |
| 132 | + '#include "eth_internals.h"' |
| 133 | + ] |
| 134 | + |
| 135 | + body = extract_from_c_files(sources, nodes_to_extract) |
| 136 | + |
| 137 | + eth_internals_h = '\n\n'.join([ |
| 138 | + "/* This file is auto-generated, don't edit it */", |
| 139 | + '\n'.join(includes), |
| 140 | + body |
| 141 | + ]) |
| 142 | + |
| 143 | + with open("ethereum-plugin-sdk/include/eth_internals.c", 'w') as f: |
| 144 | + f.write(eth_internals_h) |
| 145 | + |
| 146 | + |
| 147 | +if __name__ == "__main__": |
| 148 | + |
| 149 | + # some nodes will be extracted from these headers and merged into a new one, copied to sdk |
| 150 | + headers_to_merge = [ |
| 151 | + "src/tokens.h", |
| 152 | + "src/chainConfig.h", |
| 153 | + "src/utils.h", |
| 154 | + "src_common/ethUstream.h", |
| 155 | + "src_common/ethUtils.h", |
| 156 | + "src/shared_context.h", |
| 157 | + "src/eth_plugin_internal.h", |
| 158 | + "src/nft.h", |
| 159 | + ] |
| 160 | + nodes_to_extract = { |
| 161 | + "#define": ["MAX_TICKER_LEN", "ADDRESS_LENGTH", "INT256_LENGTH", "WEI_TO_ETHER", "SELECTOR_SIZE", "PARAMETER_LENGTH", "RUN_APPLICATION", "COLLECTION_NAME_MAX_LEN"], |
| 162 | + "typedef enum": [], |
| 163 | + "typedef struct": ["tokenDefinition_t", "txInt256_t", "txContent_t", "nftInfo_t"], |
| 164 | + "typedef union": ["extraInfo_t"], |
| 165 | + "__attribute__((no_instrument_function)) inline": ["int allzeroes"], |
| 166 | + "const": ["HEXDIGITS"], |
| 167 | + "fn": ["void getEthAddressStringFromBinary", "void getEthAddressFromKey", "void getEthDisplayableAddress", "bool adjustDecimals", "bool uint256_to_decimal", "void amountToString", "void u64_to_string", "void copy_address", "void copy_parameter"] |
| 168 | + } |
| 169 | + merge_headers(headers_to_merge, nodes_to_extract) |
| 170 | + |
| 171 | + # this header will be stripped from all #include related to previously merged headers, then copied to sdk |
| 172 | + copy_header("src/eth_plugin_interface.h", headers_to_merge) |
| 173 | + |
| 174 | + # extract and merge function bodies |
| 175 | + c_files_to_merge = [ |
| 176 | + "src/utils.c", |
| 177 | + "src_common/ethUtils.c", |
| 178 | + "src/eth_plugin_internal.c" |
| 179 | + ] |
| 180 | + merge_c_files(c_files_to_merge, nodes_to_extract["fn"]) |
0 commit comments