From 3dc1c79b1adadc84bad659e559f85e425ac58b06 Mon Sep 17 00:00:00 2001 From: Jan Pochyla Date: Tue, 5 Jun 2018 16:23:23 +0200 Subject: [PATCH] nem: cleanup modtrezorcrypto bindings --- .../modtrezorcrypto/modtrezorcrypto-bip32.h | 24 +++++++++---------- .../modtrezorcrypto/modtrezorcrypto-nem.h | 15 +++++++----- 2 files changed, 21 insertions(+), 18 deletions(-) diff --git a/embed/extmod/modtrezorcrypto/modtrezorcrypto-bip32.h b/embed/extmod/modtrezorcrypto/modtrezorcrypto-bip32.h index da3fae52f7..1542b21737 100644 --- a/embed/extmod/modtrezorcrypto/modtrezorcrypto-bip32.h +++ b/embed/extmod/modtrezorcrypto/modtrezorcrypto-bip32.h @@ -40,7 +40,6 @@ STATIC const mp_obj_type_t mod_trezorcrypto_HDNode_type; #define XPUB_MAXLEN 128 #define ADDRESS_MAXLEN 36 -#define NEM_ADDRESS_SIZE 40 /// def __init__(self, /// depth: int, @@ -309,6 +308,7 @@ STATIC mp_obj_t mod_trezorcrypto_HDNode_address(mp_obj_t self, mp_obj_t version) mp_obj_HDNode_t *o = MP_OBJ_TO_PTR(self); uint32_t v = trezor_obj_get_uint(version); + char address[ADDRESS_MAXLEN]; hdnode_get_address(&o->hdnode, v, address, ADDRESS_MAXLEN); return mp_obj_new_str(address, strlen(address), false); @@ -322,12 +322,13 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_trezorcrypto_HDNode_address_obj, mod_trezor STATIC mp_obj_t mod_trezorcrypto_HDNode_nem_address(mp_obj_t self, mp_obj_t network) { mp_obj_HDNode_t *o = MP_OBJ_TO_PTR(self); - uint8_t n = mp_obj_get_int_truncated(network); - char address[NEM_ADDRESS_SIZE + 1]; + uint8_t n = trezor_obj_get_uint8(network); + + char address[NEM_ADDRESS_SIZE + 1]; // + 1 for the 0 byte if (!hdnode_get_nem_address(&o->hdnode, n, address)) { mp_raise_ValueError("Failed to compute a NEM address"); } - return mp_obj_new_str(address, strlen(address), false); + return mp_obj_new_str_of_type(&mp_type_str, (const uint8_t *)address, strlen(address)); } STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_trezorcrypto_HDNode_nem_address_obj, mod_trezorcrypto_HDNode_nem_address); @@ -347,29 +348,28 @@ STATIC mp_obj_t mod_trezorcrypto_HDNode_nem_encrypt(size_t n_args, const mp_obj_ mp_buffer_info_t iv; mp_get_buffer_raise(args[2], &iv, MP_BUFFER_READ); if (iv.len != 16) { - mp_raise_ValueError("IV has invalid length"); + mp_raise_ValueError("iv has invalid length"); } mp_buffer_info_t salt; mp_get_buffer_raise(args[3], &salt, MP_BUFFER_READ); if (salt.len != NEM_SALT_SIZE) { - mp_raise_ValueError("Salt has invalid length"); + mp_raise_ValueError("salt has invalid length"); } mp_buffer_info_t payload; mp_get_buffer_raise(args[4], &payload, MP_BUFFER_READ); if (payload.len == 0) { - mp_raise_ValueError("Payload is empty"); + mp_raise_ValueError("payload is empty"); } - uint8_t buffer[NEM_ENCRYPTED_SIZE(payload.len)]; - - if (!hdnode_nem_encrypt(&o->hdnode, *(const ed25519_public_key *)transfer_pk.buf, iv.buf, salt.buf, payload.buf, payload.len, buffer)) { + vstr_t vstr; + vstr_init_len(&vstr, NEM_ENCRYPTED_SIZE(payload.len)); + if (!hdnode_nem_encrypt(&o->hdnode, *(const ed25519_public_key *)transfer_pk.buf, iv.buf, salt.buf, payload.buf, payload.len, (uint8_t *)vstr.buf)) { mp_raise_ValueError("HDNode nem encrypt failed"); } - return mp_obj_new_bytes(buffer, sizeof(buffer)); + return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr); } STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_trezorcrypto_HDNode_nem_encrypt_obj, 5, 5, mod_trezorcrypto_HDNode_nem_encrypt); - /// def ethereum_pubkeyhash(self) -> bytes: /// ''' /// Compute an Ethereum pubkeyhash (aka address) from the HD node. diff --git a/embed/extmod/modtrezorcrypto/modtrezorcrypto-nem.h b/embed/extmod/modtrezorcrypto/modtrezorcrypto-nem.h index d3dfb7dd23..57d1990292 100644 --- a/embed/extmod/modtrezorcrypto/modtrezorcrypto-nem.h +++ b/embed/extmod/modtrezorcrypto/modtrezorcrypto-nem.h @@ -18,6 +18,9 @@ */ #include "py/objstr.h" + +#include "embed/extmod/trezorobj.h" + #include "nem.h" /// def validate_address(address: str, network: int) -> bool: @@ -29,7 +32,7 @@ STATIC mp_obj_t mod_trezorcrypto_nem_validate_address(mp_obj_t address, mp_obj_t mp_buffer_info_t addr; mp_get_buffer_raise(address, &addr, MP_BUFFER_READ); - uint32_t n = mp_obj_get_int_truncated(network); + uint32_t n = trezor_obj_get_uint(network); return mp_obj_new_bool(nem_validate_address(addr.buf, n)); } STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_trezorcrypto_nem_validate_address_obj, mod_trezorcrypto_nem_validate_address); @@ -39,16 +42,16 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_trezorcrypto_nem_validate_address_obj, mod_ /// Compute a NEM address from a public key /// ''' STATIC mp_obj_t mod_trezorcrypto_nem_compute_address(mp_obj_t public_key, mp_obj_t network) { - mp_buffer_info_t p; mp_get_buffer_raise(public_key, &p, MP_BUFFER_READ); - uint32_t n = mp_obj_get_int_truncated(network); - char address[NEM_ADDRESS_SIZE + 1]; + uint32_t n = trezor_obj_get_uint(network); + + char address[NEM_ADDRESS_SIZE + 1]; // + 1 for the 0 byte if (!nem_get_address(p.buf, n, address)) { - mp_raise_ValueError("Failed to compute a NEM address from a provided public key"); + mp_raise_ValueError("Failed to compute a NEM address from provided public key"); } - return mp_obj_new_str(address, strlen(address), false); + return mp_obj_new_str_of_type(&mp_type_str, (const uint8_t *)address, strlen(address)); } STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_trezorcrypto_nem_compute_address_obj, mod_trezorcrypto_nem_compute_address);