1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2025-01-07 05:51:38 +00:00

seed: add progress callback to bip39.from_seed, draw loader

This commit is contained in:
Jan Pochyla 2019-01-14 14:54:48 +01:00 committed by Pavol Rusnak
parent a0c8e977a2
commit d2aef38bd8
No known key found for this signature in database
GPG Key ID: 91F3B339B9A02A3D
2 changed files with 33 additions and 14 deletions

View File

@ -17,6 +17,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include "py/runtime.h"
#include "py/objstr.h" #include "py/objstr.h"
#include "bip39.h" #include "bip39.h"
@ -110,22 +111,38 @@ STATIC mp_obj_t mod_trezorcrypto_bip39_check(mp_obj_t mnemonic) {
} }
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorcrypto_bip39_check_obj, mod_trezorcrypto_bip39_check); STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorcrypto_bip39_check_obj, mod_trezorcrypto_bip39_check);
/// def seed(mnemonic: str, passphrase: str) -> bytes: STATIC mp_obj_t ui_wait_callback = mp_const_none;
STATIC void wrapped_ui_wait_callback(uint32_t current, uint32_t total) {
if (mp_obj_is_callable(ui_wait_callback)) {
mp_call_function_2_protected(ui_wait_callback, mp_obj_new_int(current), mp_obj_new_int(total));
}
}
/// def seed(mnemonic: str, passphrase: str, callback: (int, int -> None)=None) -> bytes:
/// ''' /// '''
/// Generate seed from mnemonic and passphrase. /// Generate seed from mnemonic and passphrase.
/// ''' /// '''
STATIC mp_obj_t mod_trezorcrypto_bip39_seed(mp_obj_t mnemonic, mp_obj_t passphrase) { STATIC mp_obj_t mod_trezorcrypto_bip39_seed(size_t n_args, const mp_obj_t *args) {
mp_buffer_info_t mnemo; mp_buffer_info_t mnemo;
mp_buffer_info_t phrase; mp_buffer_info_t phrase;
mp_get_buffer_raise(mnemonic, &mnemo, MP_BUFFER_READ); mp_get_buffer_raise(args[0], &mnemo, MP_BUFFER_READ);
mp_get_buffer_raise(passphrase, &phrase, MP_BUFFER_READ); mp_get_buffer_raise(args[1], &phrase, MP_BUFFER_READ);
uint8_t seed[64]; uint8_t seed[64];
const char *pmnemonic = mnemo.len > 0 ? mnemo.buf : ""; const char *pmnemonic = mnemo.len > 0 ? mnemo.buf : "";
const char *ppassphrase = phrase.len > 0 ? phrase.buf : ""; const char *ppassphrase = phrase.len > 0 ? phrase.buf : "";
mnemonic_to_seed(pmnemonic, ppassphrase, seed, NULL); // no callback for now if (n_args > 2) {
// generate with a progress callback
ui_wait_callback = args[2];
mnemonic_to_seed(pmnemonic, ppassphrase, seed, wrapped_ui_wait_callback);
ui_wait_callback = mp_const_none;
} else {
// generate without callback
mnemonic_to_seed(pmnemonic, ppassphrase, seed, NULL);
}
return mp_obj_new_bytes(seed, sizeof(seed)); return mp_obj_new_bytes(seed, sizeof(seed));
} }
STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_trezorcrypto_bip39_seed_obj, mod_trezorcrypto_bip39_seed); STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_trezorcrypto_bip39_seed_obj, 2, 3, mod_trezorcrypto_bip39_seed);
STATIC const mp_rom_map_elem_t mod_trezorcrypto_bip39_globals_table[] = { STATIC const mp_rom_map_elem_t mod_trezorcrypto_bip39_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_bip39) }, { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_bip39) },

View File

@ -1,6 +1,5 @@
from trezor import ui, wire from trezor import ui, wire
from trezor.crypto import bip32, bip39 from trezor.crypto import bip32, bip39
from trezor.ui.text import Text
from apps.common import cache, storage from apps.common import cache, storage
from apps.common.request_passphrase import protect_by_passphrase from apps.common.request_passphrase import protect_by_passphrase
@ -62,19 +61,22 @@ async def get_keychain(ctx: wire.Context, namespaces: list) -> Keychain:
if passphrase is None: if passphrase is None:
passphrase = await protect_by_passphrase(ctx) passphrase = await protect_by_passphrase(ctx)
cache.set_passphrase(passphrase) cache.set_passphrase(passphrase)
await layout_waiting_screen() _start_bip39_progress()
seed = bip39.seed(storage.get_mnemonic(), passphrase) seed = bip39.seed(storage.get_mnemonic(), passphrase, _render_bip39_progress)
cache.set_seed(seed) cache.set_seed(seed)
keychain = Keychain(seed, namespaces) keychain = Keychain(seed, namespaces)
return keychain return keychain
@ui.layout def _start_bip39_progress():
async def layout_waiting_screen(): ui.display.clear()
text = Text("Deriving seed") ui.header("Please wait")
text.bold("Please wait...")
text.render()
def _render_bip39_progress(progress: int, total: int):
p = int(1000 * progress / total)
ui.display.loader(p, 18, ui.WHITE, ui.BG)
def derive_node_without_passphrase( def derive_node_without_passphrase(