From 7acd13643553560ce73ca40b7bc000dcca13d05e Mon Sep 17 00:00:00 2001 From: Michele Balistreri Date: Wed, 5 Jul 2023 07:56:48 +0200 Subject: [PATCH] add erc20 db lookup --- app/ethereum/eth_db.c | 80 +++++++++++++++++++++++++++++++++++++++++++ app/ethereum/eth_db.h | 24 +++++++++++++ tools/token-db.py | 4 ++- 3 files changed, 107 insertions(+), 1 deletion(-) create mode 100644 app/ethereum/eth_db.c create mode 100644 app/ethereum/eth_db.h diff --git a/app/ethereum/eth_db.c b/app/ethereum/eth_db.c new file mode 100644 index 0000000..79be0fe --- /dev/null +++ b/app/ethereum/eth_db.c @@ -0,0 +1,80 @@ +#include "eth_db.h" +#include + +#define FS_PAIRING_CHAIN 0x4348 +#define FS_PAIRING_ERC20 0x3020 + +#define ERC20_NET_LEN 24 + +struct __attribute__((packed)) chain_raw_desc { + fs_entry_t _entry; + uint32_t chain_id; + char data[]; +}; + +struct __attribute__((packed)) erc20_raw_desc { + fs_entry_t _entry; + uint8_t net_count; + uint8_t data[]; +}; + +fs_action_t _eth_db_match_chain(void* ctx, fs_entry_t* entry) { + if (entry->magic != FS_PAIRING_CHAIN) { + return FS_REJECT; + } + + chain_desc_t* chain = (chain_desc_t*) ctx; + struct chain_raw_desc* chain_data = (struct chain_raw_desc*) entry; + + return chain_data->chain_id == chain->chain_id ? FS_ACCEPT : FS_REJECT; +} + +fs_action_t _eth_db_match_erc20(void* ctx, fs_entry_t* entry) { + if (entry->magic != FS_PAIRING_ERC20) { + return FS_REJECT; + } + + erc20_desc_t* erc20 = (erc20_desc_t*) ctx; + struct erc20_raw_desc* erc20_data = (struct erc20_raw_desc*) entry; + + int count = erc20_data->net_count; + uint8_t* data = erc20_data->data; + + while (count--) { + if (!memcmp(&erc20->chain, data, 4) && !memcmp(erc20->addr, &data[4], 20)) { + return FS_ACCEPT; + } + + data += ERC20_NET_LEN; + } + + return FS_REJECT; +} + +app_err_t eth_db_lookup_chain(chain_desc_t* chain) { + struct chain_raw_desc* chain_data = (struct chain_raw_desc*) fs_find(_eth_db_match_chain, chain); + + if (!chain_data) { + return ERR_DATA; + } + + chain->ticker = chain_data->data; + chain->name = chain->ticker + strlen(chain->ticker); + chain->short_name = chain->name + strlen(chain->name); + + return ERR_OK; +} + +app_err_t eth_db_lookup_erc20(erc20_desc_t* erc20) { + struct erc20_raw_desc* erc20_data = (struct erc20_raw_desc*) fs_find(_eth_db_match_erc20, erc20); + + if (!erc20_data) { + return ERR_DATA; + } + + uint8_t* data = erc20_data->data + (erc20_data->net_count * ERC20_NET_LEN); + erc20->decimals = *(data++); + erc20->ticker = (char *) data; + + return ERR_OK; +} diff --git a/app/ethereum/eth_db.h b/app/ethereum/eth_db.h new file mode 100644 index 0000000..e73d62e --- /dev/null +++ b/app/ethereum/eth_db.h @@ -0,0 +1,24 @@ +#ifndef _ETH_DB_H_ +#define _ETH_DB_H_ + +#include "storage/fs.h" +#include "error.h" + +typedef struct { + uint32_t chain_id; + const char *ticker; + const char *name; + const char *short_name; +} chain_desc_t; + +typedef struct { + uint32_t chain; + const uint8_t* addr; + const char *ticker; + uint8_t decimals; +} erc20_desc_t; + +app_err_t eth_db_lookup_chain(chain_desc_t* chain); +app_err_t eth_db_lookup_erc20(erc20_desc_t* erc20); + +#endif diff --git a/tools/token-db.py b/tools/token-db.py index 65aaf99..3b00005 100644 --- a/tools/token-db.py +++ b/tools/token-db.py @@ -16,10 +16,11 @@ def serialize_addresses(addresses): def serialize_db(f, chains, tokens): for chain in chains.values(): - chain_len = 4 + len(chain["ticker"]) + 1 + len(chain["name"]) + 1 + chain_len = 4 + len(chain["ticker"]) + 1 + len(chain["name"]) + 1 + len(chain["shortName"]) + 1 f.write(struct.pack("