Add simple BIP39 lookup function

This commit is contained in:
Peter D. Gray 2018-02-17 00:30:43 -05:00
parent df570ce97f
commit e7563f27ae
2 changed files with 28 additions and 3 deletions

View File

@ -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

View File

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