1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-12-20 21:38:26 +00:00
trezor-firmware/embed/extmod/modtrezorcrypto/modtrezorcrypto-bip32.h

416 lines
17 KiB
C
Raw Normal View History

2016-10-06 12:00:57 +00:00
/*
2018-02-26 13:06:10 +00:00
* This file is part of the TREZOR project, https://trezor.io/
2016-10-06 12:00:57 +00:00
*
2018-02-26 13:06:10 +00:00
* Copyright (c) SatoshiLabs
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
2016-10-06 12:00:57 +00:00
*/
#include "py/objstr.h"
#include "bip32.h"
#include "curves.h"
#include "memzero.h"
2016-10-06 12:00:57 +00:00
/// class HDNode:
/// '''
/// BIP0032 HD node structure.
/// '''
2016-10-06 12:00:57 +00:00
typedef struct _mp_obj_HDNode_t {
mp_obj_base_t base;
uint32_t fingerprint;
HDNode hdnode;
} mp_obj_HDNode_t;
2017-06-14 16:47:38 +00:00
STATIC const mp_obj_type_t mod_trezorcrypto_HDNode_type;
2016-11-06 15:06:11 +00:00
#define XPUB_MAXLEN 128
#define ADDRESS_MAXLEN 36
/// def __init__(self,
/// depth: int,
/// fingerprint: int,
/// child_num: int,
/// chain_code: bytes,
/// private_key: bytes = None,
/// public_key: bytes = None,
/// curve_name: str = None) -> None:
/// '''
/// '''
STATIC mp_obj_t mod_trezorcrypto_HDNode_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
STATIC const mp_arg_t allowed_args[] = {
{ MP_QSTR_depth, MP_ARG_REQUIRED | MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} },
{ MP_QSTR_fingerprint, MP_ARG_REQUIRED | MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} },
{ MP_QSTR_child_num, MP_ARG_REQUIRED | MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} },
{ MP_QSTR_chain_code, MP_ARG_REQUIRED | MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_empty_bytes} },
{ MP_QSTR_private_key, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_empty_bytes} },
{ MP_QSTR_public_key, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_empty_bytes} },
{ MP_QSTR_curve_name, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_empty_bytes} },
};
mp_arg_val_t vals[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all_kw_array(n_args, n_kw, args, MP_ARRAY_SIZE(allowed_args), allowed_args, vals);
mp_buffer_info_t chain_code;
mp_buffer_info_t private_key;
mp_buffer_info_t public_key;
mp_buffer_info_t curve_name;
const uint32_t depth = mp_obj_get_int_truncated(vals[0].u_obj);
const uint32_t fingerprint = mp_obj_get_int_truncated(vals[1].u_obj);
const uint32_t child_num = mp_obj_get_int_truncated(vals[2].u_obj);
mp_get_buffer_raise(vals[3].u_obj, &chain_code, MP_BUFFER_READ);
mp_get_buffer_raise(vals[4].u_obj, &private_key, MP_BUFFER_READ);
mp_get_buffer_raise(vals[5].u_obj, &public_key, MP_BUFFER_READ);
mp_get_buffer_raise(vals[6].u_obj, &curve_name, MP_BUFFER_READ);
2018-01-26 12:51:44 +00:00
if (32 != chain_code.len) {
mp_raise_ValueError("chain_code is invalid");
}
2018-01-26 12:51:44 +00:00
if (0 == public_key.len && 0 == private_key.len) {
mp_raise_ValueError("either public_key or private_key is required");
}
2018-01-26 12:51:44 +00:00
if (0 != private_key.len && 32 != private_key.len) {
mp_raise_ValueError("private_key is invalid");
}
2018-01-26 12:51:44 +00:00
if (0 != public_key.len && 33 != public_key.len) {
mp_raise_ValueError("public_key is invalid");
}
const curve_info *curve = NULL;
2018-01-26 12:51:44 +00:00
if (0 == curve_name.len) {
curve = get_curve_by_name(SECP256K1_NAME);
} else {
curve = get_curve_by_name(curve_name.buf);
}
if (NULL == curve) {
mp_raise_ValueError("curve_name is invalid");
}
mp_obj_HDNode_t *o = m_new_obj(mp_obj_HDNode_t);
o->base.type = type;
o->fingerprint = fingerprint;
o->hdnode.depth = depth;
o->hdnode.child_num = child_num;
2018-01-26 12:51:44 +00:00
if (32 == chain_code.len) {
memcpy(o->hdnode.chain_code, chain_code.buf, 32);
} else {
memzero(o->hdnode.chain_code, 32);
}
2018-01-26 12:51:44 +00:00
if (32 == private_key.len) {
memcpy(o->hdnode.private_key, private_key.buf, 32);
} else {
memzero(o->hdnode.private_key, 32);
}
2018-01-26 12:51:44 +00:00
if (33 == public_key.len) {
memcpy(o->hdnode.public_key, public_key.buf, 33);
} else {
memzero(o->hdnode.public_key, 33);
}
o->hdnode.curve = curve;
return MP_OBJ_FROM_PTR(o);
}
2018-01-26 12:53:32 +00:00
/// def derive(self, index: int, public: bool=False) -> None:
2016-10-06 12:00:57 +00:00
/// '''
/// Derive a BIP0032 child node in place.
/// '''
2018-01-26 12:53:32 +00:00
STATIC mp_obj_t mod_trezorcrypto_HDNode_derive(size_t n_args, const mp_obj_t *args) {
mp_obj_HDNode_t *o = MP_OBJ_TO_PTR(args[0]);
uint32_t i = mp_obj_get_int_truncated(args[1]);
2016-10-06 12:00:57 +00:00
uint32_t fp = hdnode_fingerprint(&o->hdnode);
2018-01-26 12:53:32 +00:00
bool public = n_args > 2 && args[2] == mp_const_true;
2016-10-06 12:00:57 +00:00
2018-01-26 12:53:32 +00:00
int res;
if (public) {
res = hdnode_public_ckd(&o->hdnode, i);
} else {
if (0 == memcmp(o->hdnode.private_key, "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", 32)) {
2018-01-26 12:53:32 +00:00
memzero(&o->hdnode, sizeof(o->hdnode));
mp_raise_ValueError("Failed to derive, private key not set");
}
res = hdnode_private_ckd(&o->hdnode, i);
}
if (!res) {
memzero(&o->hdnode, sizeof(o->hdnode));
mp_raise_ValueError("Failed to derive");
2016-10-06 12:00:57 +00:00
}
o->fingerprint = fp;
return mp_const_none;
}
2018-01-26 12:53:32 +00:00
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_trezorcrypto_HDNode_derive_obj, 2, 3, mod_trezorcrypto_HDNode_derive);
2016-10-06 12:00:57 +00:00
/// def derive_path(self, path: List[int]) -> None:
2016-10-06 12:00:57 +00:00
/// '''
/// Go through a list of indexes and iteratively derive a child node in place.
/// '''
2017-06-14 16:47:38 +00:00
STATIC mp_obj_t mod_trezorcrypto_HDNode_derive_path(mp_obj_t self, mp_obj_t path) {
2016-10-06 12:00:57 +00:00
mp_obj_HDNode_t *o = MP_OBJ_TO_PTR(self);
// get path objects and length
size_t plen;
2016-10-06 12:00:57 +00:00
mp_obj_t *pitems;
mp_obj_get_array(path, &plen, &pitems);
if (plen > 32) {
mp_raise_ValueError("Path cannot be longer than 32 indexes");
}
2016-10-06 12:00:57 +00:00
// convert path to int array
uint32_t pi;
uint32_t pints[plen];
for (pi = 0; pi < plen; pi++) {
pints[pi] = mp_obj_get_int_truncated(pitems[pi]);
}
2016-10-06 12:00:57 +00:00
if (!hdnode_private_ckd_cached(&o->hdnode, pints, plen, &o->fingerprint)) {
// derivation failed, reset the state and raise
o->fingerprint = 0;
memzero(&o->hdnode, sizeof(o->hdnode));
mp_raise_ValueError("Failed to derive path");
2016-10-06 12:00:57 +00:00
}
return mp_const_none;
}
2017-06-14 16:47:38 +00:00
STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_trezorcrypto_HDNode_derive_path_obj, mod_trezorcrypto_HDNode_derive_path);
2016-10-06 12:00:57 +00:00
STATIC mp_obj_t serialize_public_private(mp_obj_t self, bool use_public, uint32_t version) {
2016-10-06 12:00:57 +00:00
mp_obj_HDNode_t *o = MP_OBJ_TO_PTR(self);
char xpub[XPUB_MAXLEN];
int written;
if (use_public) {
hdnode_fill_public_key(&o->hdnode);
written = hdnode_serialize_public(&o->hdnode, o->fingerprint, version, xpub, XPUB_MAXLEN);
} else {
written = hdnode_serialize_private(&o->hdnode, o->fingerprint, version, xpub, XPUB_MAXLEN);
}
2016-10-06 12:00:57 +00:00
if (written <= 0) {
mp_raise_ValueError("Failed to serialize");
2016-10-06 12:00:57 +00:00
}
return mp_obj_new_str(xpub, written - 1, false); // written includes 0 at the end
2016-10-06 12:00:57 +00:00
}
/// def serialize_public(self, version: int) -> str:
2016-10-06 12:00:57 +00:00
/// '''
/// Serialize the public info from HD node to base58 string.
/// '''
2017-06-14 16:47:38 +00:00
STATIC mp_obj_t mod_trezorcrypto_HDNode_serialize_public(mp_obj_t self, mp_obj_t version) {
uint32_t ver = mp_obj_get_int_truncated(version);
return serialize_public_private(self, true, ver);
2016-10-06 12:00:57 +00:00
}
2017-06-14 16:47:38 +00:00
STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_trezorcrypto_HDNode_serialize_public_obj, mod_trezorcrypto_HDNode_serialize_public);
2016-10-06 12:00:57 +00:00
/// def serialize_private(self, version: int) -> str:
2016-10-06 12:00:57 +00:00
/// '''
/// Serialize the private info HD node to base58 string.
/// '''
2017-06-14 16:47:38 +00:00
STATIC mp_obj_t mod_trezorcrypto_HDNode_serialize_private(mp_obj_t self, mp_obj_t version) {
uint32_t ver = mp_obj_get_int_truncated(version);
return serialize_public_private(self, false, ver);
2016-10-06 12:00:57 +00:00
}
2017-06-14 16:47:38 +00:00
STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_trezorcrypto_HDNode_serialize_private_obj, mod_trezorcrypto_HDNode_serialize_private);
2016-10-06 12:00:57 +00:00
/// def clone(self) -> HDNode:
2016-11-06 13:17:05 +00:00
/// '''
/// Returns a copy of the HD node.
/// '''
2017-06-14 16:47:38 +00:00
STATIC mp_obj_t mod_trezorcrypto_HDNode_clone(mp_obj_t self) {
2016-11-06 13:17:05 +00:00
mp_obj_HDNode_t *o = MP_OBJ_TO_PTR(self);
mp_obj_HDNode_t *copy = m_new_obj(mp_obj_HDNode_t);
2017-06-14 16:47:38 +00:00
copy->base.type = &mod_trezorcrypto_HDNode_type;
2016-11-06 13:17:05 +00:00
copy->hdnode = o->hdnode;
copy->fingerprint = o->fingerprint;
return MP_OBJ_FROM_PTR(copy);
}
2017-06-14 16:47:38 +00:00
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorcrypto_HDNode_clone_obj, mod_trezorcrypto_HDNode_clone);
2016-11-06 13:17:05 +00:00
/// def depth(self) -> int:
2016-10-06 12:00:57 +00:00
/// '''
/// Returns a depth of the HD node.
/// '''
2017-06-14 16:47:38 +00:00
STATIC mp_obj_t mod_trezorcrypto_HDNode_depth(mp_obj_t self) {
2016-10-06 12:00:57 +00:00
mp_obj_HDNode_t *o = MP_OBJ_TO_PTR(self);
return mp_obj_new_int_from_uint(o->hdnode.depth);
}
2017-06-14 16:47:38 +00:00
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorcrypto_HDNode_depth_obj, mod_trezorcrypto_HDNode_depth);
2016-10-06 12:00:57 +00:00
/// def fingerprint(self) -> int:
2016-10-06 12:00:57 +00:00
/// '''
/// Returns a fingerprint of the HD node (hash of the parent public key).
/// '''
2017-06-14 16:47:38 +00:00
STATIC mp_obj_t mod_trezorcrypto_HDNode_fingerprint(mp_obj_t self) {
2016-10-06 12:00:57 +00:00
mp_obj_HDNode_t *o = MP_OBJ_TO_PTR(self);
return mp_obj_new_int_from_uint(o->fingerprint);
}
2017-06-14 16:47:38 +00:00
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorcrypto_HDNode_fingerprint_obj, mod_trezorcrypto_HDNode_fingerprint);
2016-10-06 12:00:57 +00:00
/// def child_num(self) -> int:
2016-10-06 12:00:57 +00:00
/// '''
/// Returns a child index of the HD node.
/// '''
2017-06-14 16:47:38 +00:00
STATIC mp_obj_t mod_trezorcrypto_HDNode_child_num(mp_obj_t self) {
2016-10-06 12:00:57 +00:00
mp_obj_HDNode_t *o = MP_OBJ_TO_PTR(self);
return mp_obj_new_int_from_uint(o->hdnode.child_num);
}
2017-06-14 16:47:38 +00:00
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorcrypto_HDNode_child_num_obj, mod_trezorcrypto_HDNode_child_num);
2016-10-06 12:00:57 +00:00
/// def chain_code(self) -> bytes:
2016-10-06 12:00:57 +00:00
/// '''
/// Returns a chain code of the HD node.
/// '''
2017-06-14 16:47:38 +00:00
STATIC mp_obj_t mod_trezorcrypto_HDNode_chain_code(mp_obj_t self) {
2016-10-06 12:00:57 +00:00
mp_obj_HDNode_t *o = MP_OBJ_TO_PTR(self);
return mp_obj_new_bytes(o->hdnode.chain_code, sizeof(o->hdnode.chain_code));
2016-10-06 12:00:57 +00:00
}
2017-06-14 16:47:38 +00:00
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorcrypto_HDNode_chain_code_obj, mod_trezorcrypto_HDNode_chain_code);
2016-10-06 12:00:57 +00:00
/// def private_key(self) -> bytes:
/// '''
/// Returns a private key of the HD node.
/// '''
2017-06-14 16:47:38 +00:00
STATIC mp_obj_t mod_trezorcrypto_HDNode_private_key(mp_obj_t self) {
mp_obj_HDNode_t *o = MP_OBJ_TO_PTR(self);
return mp_obj_new_bytes(o->hdnode.private_key, sizeof(o->hdnode.private_key));
}
2017-06-14 16:47:38 +00:00
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorcrypto_HDNode_private_key_obj, mod_trezorcrypto_HDNode_private_key);
/// def public_key(self) -> bytes:
2016-10-06 12:00:57 +00:00
/// '''
/// Returns a public key of the HD node.
/// '''
2017-06-14 16:47:38 +00:00
STATIC mp_obj_t mod_trezorcrypto_HDNode_public_key(mp_obj_t self) {
2016-10-06 12:00:57 +00:00
mp_obj_HDNode_t *o = MP_OBJ_TO_PTR(self);
hdnode_fill_public_key(&o->hdnode);
return mp_obj_new_bytes(o->hdnode.public_key, sizeof(o->hdnode.public_key));
2016-10-06 12:00:57 +00:00
}
2017-06-14 16:47:38 +00:00
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorcrypto_HDNode_public_key_obj, mod_trezorcrypto_HDNode_public_key);
2016-10-06 12:00:57 +00:00
/// def address(self, version: int) -> str:
2016-10-06 12:00:57 +00:00
/// '''
/// Compute a base58-encoded address string from the HD node.
2016-10-06 12:00:57 +00:00
/// '''
2017-06-14 16:47:38 +00:00
STATIC mp_obj_t mod_trezorcrypto_HDNode_address(mp_obj_t self, mp_obj_t version) {
2016-10-06 12:00:57 +00:00
mp_obj_HDNode_t *o = MP_OBJ_TO_PTR(self);
uint32_t v = mp_obj_get_int_truncated(version);
char address[ADDRESS_MAXLEN];
hdnode_get_address(&o->hdnode, v, address, ADDRESS_MAXLEN);
return mp_obj_new_str(address, strlen(address), false);
2016-10-06 12:00:57 +00:00
}
2017-06-14 16:47:38 +00:00
STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_trezorcrypto_HDNode_address_obj, mod_trezorcrypto_HDNode_address);
/// def ethereum_pubkeyhash(self) -> bytes:
/// '''
/// Compute an Ethereum pubkeyhash (aka address) from the HD node.
/// '''
STATIC mp_obj_t mod_trezorcrypto_HDNode_ethereum_pubkeyhash(mp_obj_t self) {
mp_obj_HDNode_t *o = MP_OBJ_TO_PTR(self);
uint8_t pkh[20];
hdnode_get_ethereum_pubkeyhash(&o->hdnode, pkh);
return mp_obj_new_bytes(pkh, sizeof(pkh));
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorcrypto_HDNode_ethereum_pubkeyhash_obj, mod_trezorcrypto_HDNode_ethereum_pubkeyhash);
2017-06-14 16:47:38 +00:00
STATIC const mp_rom_map_elem_t mod_trezorcrypto_HDNode_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_derive), MP_ROM_PTR(&mod_trezorcrypto_HDNode_derive_obj) },
{ MP_ROM_QSTR(MP_QSTR_derive_path), MP_ROM_PTR(&mod_trezorcrypto_HDNode_derive_path_obj) },
{ MP_ROM_QSTR(MP_QSTR_serialize_private), MP_ROM_PTR(&mod_trezorcrypto_HDNode_serialize_private_obj) },
{ MP_ROM_QSTR(MP_QSTR_serialize_public), MP_ROM_PTR(&mod_trezorcrypto_HDNode_serialize_public_obj) },
{ MP_ROM_QSTR(MP_QSTR_clone), MP_ROM_PTR(&mod_trezorcrypto_HDNode_clone_obj) },
{ MP_ROM_QSTR(MP_QSTR_depth), MP_ROM_PTR(&mod_trezorcrypto_HDNode_depth_obj) },
{ MP_ROM_QSTR(MP_QSTR_fingerprint), MP_ROM_PTR(&mod_trezorcrypto_HDNode_fingerprint_obj) },
{ MP_ROM_QSTR(MP_QSTR_child_num), MP_ROM_PTR(&mod_trezorcrypto_HDNode_child_num_obj) },
{ MP_ROM_QSTR(MP_QSTR_chain_code), MP_ROM_PTR(&mod_trezorcrypto_HDNode_chain_code_obj) },
{ MP_ROM_QSTR(MP_QSTR_private_key), MP_ROM_PTR(&mod_trezorcrypto_HDNode_private_key_obj) },
{ MP_ROM_QSTR(MP_QSTR_public_key), MP_ROM_PTR(&mod_trezorcrypto_HDNode_public_key_obj) },
{ MP_ROM_QSTR(MP_QSTR_address), MP_ROM_PTR(&mod_trezorcrypto_HDNode_address_obj) },
{ MP_ROM_QSTR(MP_QSTR_ethereum_pubkeyhash), MP_ROM_PTR(&mod_trezorcrypto_HDNode_ethereum_pubkeyhash_obj) },
2016-10-06 12:00:57 +00:00
};
2017-06-14 16:47:38 +00:00
STATIC MP_DEFINE_CONST_DICT(mod_trezorcrypto_HDNode_locals_dict, mod_trezorcrypto_HDNode_locals_dict_table);
2016-10-06 12:00:57 +00:00
2017-06-14 16:47:38 +00:00
STATIC const mp_obj_type_t mod_trezorcrypto_HDNode_type = {
2016-10-06 12:00:57 +00:00
{ &mp_type_type },
.name = MP_QSTR_HDNode,
.make_new = mod_trezorcrypto_HDNode_make_new,
2017-06-14 16:47:38 +00:00
.locals_dict = (void*)&mod_trezorcrypto_HDNode_locals_dict,
2016-10-06 12:00:57 +00:00
};
/// def deserialize(self, value: str, version_public: int, version_private: int) -> HDNode:
/// '''
/// Construct a BIP0032 HD node from a base58-serialized value.
/// '''
STATIC mp_obj_t mod_trezorcrypto_bip32_deserialize(mp_obj_t value, mp_obj_t version_public, mp_obj_t version_private) {
mp_buffer_info_t valueb;
mp_get_buffer_raise(value, &valueb, MP_BUFFER_READ);
if (valueb.len == 0) {
mp_raise_ValueError("Invalid value");
}
uint32_t vpub = mp_obj_get_int_truncated(version_public);
uint32_t vpriv = mp_obj_get_int_truncated(version_private);
HDNode hdnode;
uint32_t fingerprint;
if (hdnode_deserialize(valueb.buf, vpub, vpriv, SECP256K1_NAME, &hdnode, &fingerprint) < 0) {
mp_raise_ValueError("Failed to deserialize");
}
mp_obj_HDNode_t *o = m_new_obj(mp_obj_HDNode_t);
2017-06-14 16:47:38 +00:00
o->base.type = &mod_trezorcrypto_HDNode_type;
o->hdnode = hdnode;
o->fingerprint = fingerprint;
return MP_OBJ_FROM_PTR(o);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_3(mod_trezorcrypto_bip32_deserialize_obj, mod_trezorcrypto_bip32_deserialize);
/// def from_seed(seed: bytes, curve_name: str) -> HDNode:
2016-10-06 12:00:57 +00:00
/// '''
/// Construct a BIP0032 HD node from a BIP0039 seed value.
/// '''
STATIC mp_obj_t mod_trezorcrypto_bip32_from_seed(mp_obj_t seed, mp_obj_t curve_name) {
2016-10-06 12:00:57 +00:00
mp_buffer_info_t seedb;
mp_get_buffer_raise(seed, &seedb, MP_BUFFER_READ);
if (seedb.len == 0) {
mp_raise_ValueError("Invalid seed");
2016-10-06 12:00:57 +00:00
}
mp_buffer_info_t curveb;
mp_get_buffer_raise(curve_name, &curveb, MP_BUFFER_READ);
if (curveb.len == 0) {
mp_raise_ValueError("Invalid curve name");
2016-10-06 12:00:57 +00:00
}
HDNode hdnode;
if (!hdnode_from_seed(seedb.buf, seedb.len, curveb.buf, &hdnode)) {
mp_raise_ValueError("Failed to derive the root node");
2016-10-06 12:00:57 +00:00
}
mp_obj_HDNode_t *o = m_new_obj(mp_obj_HDNode_t);
2017-06-14 16:47:38 +00:00
o->base.type = &mod_trezorcrypto_HDNode_type;
2016-10-06 12:00:57 +00:00
o->hdnode = hdnode;
return MP_OBJ_FROM_PTR(o);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_trezorcrypto_bip32_from_seed_obj, mod_trezorcrypto_bip32_from_seed);
2016-10-06 12:00:57 +00:00
STATIC const mp_rom_map_elem_t mod_trezorcrypto_bip32_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_bip32) },
{ MP_ROM_QSTR(MP_QSTR_HDNode), MP_ROM_PTR(&mod_trezorcrypto_HDNode_type) },
{ MP_ROM_QSTR(MP_QSTR_deserialize), MP_ROM_PTR(&mod_trezorcrypto_bip32_deserialize_obj) },
{ MP_ROM_QSTR(MP_QSTR_from_seed), MP_ROM_PTR(&mod_trezorcrypto_bip32_from_seed_obj) },
2016-10-06 12:00:57 +00:00
};
STATIC MP_DEFINE_CONST_DICT(mod_trezorcrypto_bip32_globals, mod_trezorcrypto_bip32_globals_table);
2016-10-06 12:00:57 +00:00
STATIC const mp_obj_module_t mod_trezorcrypto_bip32_module = {
.base = { &mp_type_module },
.globals = (mp_obj_dict_t*)&mod_trezorcrypto_bip32_globals,
2016-10-06 12:00:57 +00:00
};