diff --git a/Makefile b/Makefile index 08aa509c88..a297ebf74a 100644 --- a/Makefile +++ b/Makefile @@ -14,6 +14,9 @@ build_stmhal: ## build stmhal port build_unix: ## build unix port make -C vendor/micropython/unix +run_unix: ## run unix port + ./vendor/micropython/unix/micropython + flash: ## flash firmware using st-flash st-flash write $(STMHAL_BUILD_DIR)/firmware0.bin 0x8000000 sleep 0.1 diff --git a/extmod/modTrezorCrypto/modTrezorCrypto-base58.h b/extmod/modTrezorCrypto/modTrezorCrypto-base58.h index 4d4439501f..0747598f05 100644 --- a/extmod/modTrezorCrypto/modTrezorCrypto-base58.h +++ b/extmod/modTrezorCrypto/modTrezorCrypto-base58.h @@ -5,6 +5,8 @@ * see LICENSE.md file for details */ +#include "trezor-crypto/base58.h" + // class Base58(object): typedef struct _mp_obj_Base58_t { mp_obj_base_t base; @@ -18,24 +20,58 @@ STATIC mp_obj_t mod_TrezorCrypto_Base58_make_new(const mp_obj_type_t *type, size } // def Base58.encode(self, data: bytes) -> str -STATIC mp_obj_t mod_TrezorCrypto_Base58_encode(mp_obj_t self, mp_obj_t callback) { - // TODO - return mp_const_none; +STATIC mp_obj_t mod_TrezorCrypto_Base58_encode(mp_obj_t self, mp_obj_t data) { + mp_buffer_info_t databuf; + mp_get_buffer_raise(data, &databuf, MP_BUFFER_READ); + vstr_t vstr; + vstr_init(&vstr, databuf.len * 8000 / 5857 + 1); // 256 = 2^8 ; 58 > 2^5.857 + b58enc(vstr.buf, &vstr.len, databuf.buf, databuf.len); + vstr.len--; // b58enc returns length including the trailing zero + return mp_obj_new_str_from_vstr(&mp_type_str, &vstr); } MP_DEFINE_CONST_FUN_OBJ_2(mod_TrezorCrypto_Base58_encode_obj, mod_TrezorCrypto_Base58_encode); +// def Base58.encode_check(self, data: bytes) -> str +STATIC mp_obj_t mod_TrezorCrypto_Base58_encode_check(mp_obj_t self, mp_obj_t data) { + mp_buffer_info_t databuf; + mp_get_buffer_raise(data, &databuf, MP_BUFFER_READ); + vstr_t vstr; + vstr_init(&vstr, (databuf.len + 4) * 8000 / 5857 + 1); // 256 = 2^8 ; 58 > 2^5.857 + vstr.len = base58_encode_check(databuf.buf, databuf.len, vstr.buf, vstr.alloc); + vstr.len--; // base58_encode_check returns length including the trailing zero + return mp_obj_new_str_from_vstr(&mp_type_str, &vstr); +} +MP_DEFINE_CONST_FUN_OBJ_2(mod_TrezorCrypto_Base58_encode_check_obj, mod_TrezorCrypto_Base58_encode_check); + // def Base58.decode(self, string: str) -> bytes -STATIC mp_obj_t mod_TrezorCrypto_Base58_decode(mp_obj_t self, mp_obj_t data) { - // TODO - return mp_const_none; +STATIC mp_obj_t mod_TrezorCrypto_Base58_decode(mp_obj_t self, mp_obj_t string) { + mp_buffer_info_t stringbuf; + mp_get_buffer_raise(string, &stringbuf, MP_BUFFER_READ); + vstr_t vstr; + vstr_init(&vstr, stringbuf.len * 5858 / 8000 + 1); // 256 = 2^8 ; 58 < 2^5.858 + b58tobin(vstr.buf, &vstr.len, stringbuf.buf); + return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr); } MP_DEFINE_CONST_FUN_OBJ_2(mod_TrezorCrypto_Base58_decode_obj, mod_TrezorCrypto_Base58_decode); +// def Base58.decode_check(self, string: str) -> bytes +STATIC mp_obj_t mod_TrezorCrypto_Base58_decode_check(mp_obj_t self, mp_obj_t string) { + mp_buffer_info_t stringbuf; + mp_get_buffer_raise(string, &stringbuf, MP_BUFFER_READ); + vstr_t vstr; + vstr_init(&vstr, stringbuf.len * 5858 / 8000 + 1); // 256 = 2^8 ; 58 < 2^5.858 + vstr.len = base58_decode_check(stringbuf.buf, (uint8_t *)vstr.buf, vstr.alloc); + return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr); +} +MP_DEFINE_CONST_FUN_OBJ_2(mod_TrezorCrypto_Base58_decode_check_obj, mod_TrezorCrypto_Base58_decode_check); + // Base58 stuff STATIC const mp_rom_map_elem_t mod_TrezorCrypto_Base58_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_encode), MP_ROM_PTR(&mod_TrezorCrypto_Base58_encode_obj) }, + { MP_ROM_QSTR(MP_QSTR_encode_check), MP_ROM_PTR(&mod_TrezorCrypto_Base58_encode_check_obj) }, { MP_ROM_QSTR(MP_QSTR_decode), MP_ROM_PTR(&mod_TrezorCrypto_Base58_decode_obj) }, + { MP_ROM_QSTR(MP_QSTR_decode_check), MP_ROM_PTR(&mod_TrezorCrypto_Base58_decode_check_obj) }, }; STATIC MP_DEFINE_CONST_DICT(mod_TrezorCrypto_Base58_locals_dict, mod_TrezorCrypto_Base58_locals_dict_table); diff --git a/vendor/micropython b/vendor/micropython index 10261a5c15..fc29254cd7 160000 --- a/vendor/micropython +++ b/vendor/micropython @@ -1 +1 @@ -Subproject commit 10261a5c15abf5fab215e6f539e2fe36d7ac3d75 +Subproject commit fc29254cd7037b817154f9154509018594f77d5c