From e7563f27aeebe1ec6d2aa4f9270af9967a3be9e5 Mon Sep 17 00:00:00 2001 From: "Peter D. Gray" Date: Sat, 17 Feb 2018 00:30:43 -0500 Subject: [PATCH] Add simple BIP39 lookup function --- Makefile | 10 +++++++--- modtcc-bip39.c | 21 +++++++++++++++++++++ 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index 18e165b..6a94f61 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ # -# TODO: Make this useful... it should compile against microptyhon unix port +# TODO: Make this useful... it should compile against microptyhon unix port or something # MPY_TOP ?= ../micropython @@ -11,10 +11,14 @@ CFLAGS += -DMICROPY_PY_TREZORCRYPTO=1 -Itrezor-crypto # Include these files into your project. C_FILES = crc.c modtcc.c -OBJ_FILES = $(C_FILES:%.c=%.o) +# and this includes lots of other stuff +# default target is here +modtcc.o: modtcc-*.c all: $(OBJ_FILES) - @echo "syntax ok" + @echo syntax ok: $(OBJ_FILES) + +OBJ_FILES = $(C_FILES:%.c=%.o) TC_LIB = trezor-crypto/libtrezor-crypto.so diff --git a/modtcc-bip39.c b/modtcc-bip39.c index 357bc22..caa78f2 100644 --- a/modtcc-bip39.c +++ b/modtcc-bip39.c @@ -9,6 +9,26 @@ #include "bip39.h" +/// def lookup(idx: int) -> str: +/// ''' +/// Return the n-th word from the wordlist. +/// ''' +STATIC mp_obj_t mod_tcc_bip39_lookup(mp_obj_t idx_obj) +{ + int idx = mp_obj_get_int(idx_obj); + + // we've all read the BIP, so obviously 12 bits only here. + if(idx < 0 || idx > 0x7ff) { + mp_raise_ValueError("wordlist range"); + } + + const char * const *wordlist = mnemonic_wordlist(); + const char *w = wordlist[idx]; + + return mp_obj_new_str(w, strlen(w)); +} +STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_tcc_bip39_lookup_obj, mod_tcc_bip39_lookup); + /// def find_word(prefix: str) -> Optional[str]: /// ''' /// Return the first word from the wordlist starting with prefix. @@ -121,6 +141,7 @@ STATIC const mp_rom_map_elem_t mod_trezorcrypto_bip39_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_from_data), MP_ROM_PTR(&mod_trezorcrypto_bip39_from_data_obj) }, { MP_ROM_QSTR(MP_QSTR_check), MP_ROM_PTR(&mod_trezorcrypto_bip39_check_obj) }, { MP_ROM_QSTR(MP_QSTR_seed), MP_ROM_PTR(&mod_trezorcrypto_bip39_seed_obj) }, + { MP_ROM_QSTR(MP_QSTR_lookup), MP_ROM_PTR(&mod_tcc_bip39_lookup_obj) }, }; STATIC MP_DEFINE_CONST_DICT(mod_trezorcrypto_bip39_globals, mod_trezorcrypto_bip39_globals_table);