From 0d4efce5a7aa83e23894fcfe75d2ddd1984d6d6d Mon Sep 17 00:00:00 2001 From: Tomas Susanka Date: Mon, 14 Jan 2019 16:27:59 +0100 Subject: [PATCH] eth: get public key --- firmware/fsm.h | 1 + firmware/fsm_msg_ethereum.h | 46 +++++++++++++++++++++++ firmware/protob/Makefile | 2 +- firmware/protob/messages-ethereum.options | 2 + firmware/protob/messages_map.py | 3 +- 5 files changed, 51 insertions(+), 3 deletions(-) diff --git a/firmware/fsm.h b/firmware/fsm.h index 75de088c51..0b07d1e1f6 100644 --- a/firmware/fsm.h +++ b/firmware/fsm.h @@ -91,6 +91,7 @@ void fsm_msgDebugLinkFlashErase(const DebugLinkFlashErase *msg); // ethereum void fsm_msgEthereumGetAddress(const EthereumGetAddress *msg); +void fsm_msgEthereumGetPublicKey(const EthereumGetPublicKey *msg); void fsm_msgEthereumSignTx(EthereumSignTx *msg); // not const because we mutate transaction void fsm_msgEthereumTxAck(const EthereumTxAck *msg); void fsm_msgEthereumSignMessage(const EthereumSignMessage *msg); diff --git a/firmware/fsm_msg_ethereum.h b/firmware/fsm_msg_ethereum.h index 7f67de088e..e6cbc452b1 100644 --- a/firmware/fsm_msg_ethereum.h +++ b/firmware/fsm_msg_ethereum.h @@ -17,6 +17,52 @@ * along with this library. If not, see . */ +void fsm_msgEthereumGetPublicKey(const EthereumGetPublicKey *msg) +{ + RESP_INIT(EthereumPublicKey); + + CHECK_INITIALIZED + + CHECK_PIN + + const CoinInfo *coin = fsm_getCoin(true, "Bitcoin"); // we use Bitcoin-like format for ETH + + const char *curve = coin->curve_name; + uint32_t fingerprint; + HDNode *node = node = fsm_getDerivedNode(curve, msg->address_n, msg->address_n_count, &fingerprint); + if (!node) return; + hdnode_fill_public_key(node); + + if (msg->has_show_display && msg->show_display) { + layoutPublicKey(node->public_key); + if (!protectButton(ButtonRequestType_ButtonRequest_PublicKey, true)) { + fsm_sendFailure(FailureType_Failure_ActionCancelled, NULL); + layoutHome(); + return; + } + } + + resp->node.depth = node->depth; + resp->node.fingerprint = fingerprint; + resp->node.child_num = node->child_num; + resp->node.chain_code.size = 32; + memcpy(resp->node.chain_code.bytes, node->chain_code, 32); + resp->node.has_private_key = false; + resp->node.has_public_key = true; + resp->node.public_key.size = 33; + memcpy(resp->node.public_key.bytes, node->public_key, 33); + if (node->public_key[0] == 1) { + /* ed25519 public key */ + resp->node.public_key.bytes[0] = 0; + } + resp->has_xpub = true; + + hdnode_serialize_public(node, fingerprint, coin->xpub_magic, resp->xpub, sizeof(resp->xpub)); + + msg_write(MessageType_MessageType_EthereumPublicKey, resp); + layoutHome(); +} + void fsm_msgEthereumSignTx(EthereumSignTx *msg) { CHECK_INITIALIZED diff --git a/firmware/protob/Makefile b/firmware/protob/Makefile index 6fc525caee..0cf8453c18 100644 --- a/firmware/protob/Makefile +++ b/firmware/protob/Makefile @@ -26,7 +26,7 @@ messages_%_pb2.py: messages-%.proto $(Q)protoc -I/usr/include -I. $< --python_out=. messages_map.h: messages_map.py messages_pb2.py - $(Q)$(PYTHON) $< | grep -v -e MessageType_Cardano -e MessageType_Tezos -e MessageType_Ripple -e MessageType_Monero -e MessageType_DebugMonero -e MessageType_Ontology -e MessageType_Tron -e EthereumGetPublicKey -e EthereumPublicKey > $@ + $(Q)$(PYTHON) $< | grep -v -e MessageType_Cardano -e MessageType_Tezos -e MessageType_Ripple -e MessageType_Monero -e MessageType_DebugMonero -e MessageType_Ontology -e MessageType_Tron > $@ clean: rm -f *.pb *.o *.d *.pb.c *.pb.h *_pb2.py messages_map.h diff --git a/firmware/protob/messages-ethereum.options b/firmware/protob/messages-ethereum.options index c32f6813ed..07b148c97b 100644 --- a/firmware/protob/messages-ethereum.options +++ b/firmware/protob/messages-ethereum.options @@ -22,5 +22,7 @@ EthereumMessageSignature.address max_size:20 EthereumMessageSignature.signature max_size:65 EthereumGetAddress.address_n max_count:8 +EthereumGetPublicKey.address_n max_count:8 EthereumAddress.address max_size:20 +EthereumPublicKey.xpub max_size:113 diff --git a/firmware/protob/messages_map.py b/firmware/protob/messages_map.py index ef98014954..f5ab67d274 100755 --- a/firmware/protob/messages_map.py +++ b/firmware/protob/messages_map.py @@ -49,8 +49,7 @@ def handle_message(message, extension): ) -print("\t// This file is automatically generated" - "by messages_map.py -- DO NOT EDIT!") +print("\t// This file is automatically generated by messages_map.py -- DO NOT EDIT!") messages = defaultdict(list)