Skip to content

Commit

Permalink
add base tendermint support
Browse files Browse the repository at this point in the history
  • Loading branch information
markrypto committed Jun 14, 2021
1 parent 6c19f74 commit fad542c
Show file tree
Hide file tree
Showing 13 changed files with 389 additions and 54 deletions.
3 changes: 3 additions & 0 deletions include/keepkey/firmware/coins.def
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,8 @@ X(true, "Binance", true, "BEP2", false, NA, false, NA, false, N
X(true, "Cosmos", true, "ATOM", false, NA, false, NA, false, NA, false, {0}, true, 0x80000076, false, 0, true, 6, false, NO_CONTRACT, false, 0, false, false, false, false, true, SECP256K1_STRING, false, "", false, "cosmos", false, false, false, 0, false, 0, false, "" )
X(true, "Ripple", true, "Ripple",false, 0, false, 0, false, 0, false, {0}, true, 0x80000090, false, 0, false, 0, false, NO_CONTRACT, false, 0, false, false, false, false, true, SECP256K1_STRING, false, "", false, "", false, false, true, 77429938, true, 78792518, false, "" )
X(true, "THORChain", true, "RUNE", false, NA, false, NA, false, NA, false, {0}, true, 0x800003a3, false, 0, true, 8, false, NO_CONTRACT, false, 0, false, false, false, false, true, SECP256K1_STRING, false, "", false, "thor", false, false, false, 0, false, 0, false, "" )
X(true, "Terra", true, "LUNA", false, NA, false, NA, false, NA, false, {0}, true, 0x8000014a, false, 0, true, 6, false, NO_CONTRACT, false, 0, false, false, false, false, true, SECP256K1_STRING, false, "", false, "terra", false, false, false, 0, false, 0, false, "" )
X(true, "Kava", true, "KAVA", false, NA, false, NA, false, NA, false, {0}, true, 0x800001cb, false, 0, true, 6, false, NO_CONTRACT, false, 0, false, false, false, false, true, SECP256K1_STRING, false, "", false, "kava", false, false, false, 0, false, 0, false, "" )
X(true, "Secret", true, "SCRT", false, NA, false, NA, false, NA, false, {0}, true, 0x80000211, false, 0, true, 6, false, NO_CONTRACT, false, 0, false, false, false, false, true, SECP256K1_STRING, false, "", false, "secret", false, false, false, 0, false, 0, false, "" )
#undef X
#undef NO_CONTRACT
8 changes: 0 additions & 8 deletions include/keepkey/firmware/cosmos.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,4 @@

typedef struct _CosmosSignTx CosmosSignTx;

bool cosmos_signTxInit(const HDNode *_node, const CosmosSignTx *_msg);
bool cosmos_signTxUpdateMsgSend(const uint64_t amount, const char *to_address);
bool cosmos_signTxFinalize(uint8_t *public_key, uint8_t *signature);
bool cosmos_signingIsInited(void);
bool cosmos_signingIsFinished(void);
void cosmos_signAbort(void);
const CosmosSignTx *cosmos_getCosmosSignTx(void);

#endif
20 changes: 20 additions & 0 deletions include/keepkey/firmware/signtx_tendermint.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#ifndef KEEPKEY_FIRMWARE_SIGNTXTENDERMINT_H
#define KEEPKEY_FIRMWARE_SIGNTXTENDERMINT_H

#include "messages.pb.h"
#include "trezor/crypto/bip32.h"

#include <stdbool.h>
#include <stdint.h>

typedef struct _TendermintSignTx TendermintSignTx;

bool tendermint_signTxInit(const HDNode *_node, const void *_msg, const size_t msgsize, const char *denom);
bool tendermint_signTxUpdateMsgSend(const uint64_t amount, const char *to_address, const char *chainstr, const char *denom, const char *msgTypePrefix);
bool tendermint_signTxFinalize(uint8_t *public_key, uint8_t *signature);
bool tendermint_signingIsInited(void);
bool tendermint_signingIsFinished(void);
void tendermint_signAbort(void);
const void *tendermint_getSignTx(void);

#endif
3 changes: 2 additions & 1 deletion include/keepkey/transport/interface.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* This file is part of the KeepKey project.
*
* Copyright (C) 2015 KeepKey LLC
* Copyright (C) 2021 KeepKey LLC
*
* This library is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
Expand Down Expand Up @@ -30,6 +30,7 @@
#include "messages-cosmos.pb.h"
#include "messages-eos.pb.h"
#include "messages-ripple.pb.h"
#include "messages-tendermint.pb.h"
#include "messages-thorchain.pb.h"

#include "types.pb.h"
Expand Down
22 changes: 22 additions & 0 deletions include/keepkey/transport/messages-tendermint.options
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
TendermintGetAddress.address_n max_count:10
TendermintGetAddress.address_prefix max_size:10
TendermintGetAddress.chain_name max_size:15

TendermintAddress.address max_size:46

TendermintMsgAck.denom max_size:10
TendermintMsgAck.chain_name max_size:15
TendermintMsgAck.message_type_prefix max_size:25

TendermintSignTx.address_n max_count:10
TendermintSignTx.chain_id max_size:32
TendermintSignTx.memo max_size:256
TendermintSignTx.denom max_size:10
TendermintSignTx.chain_name max_size:15
TendermintSignTx.message_type_prefix max_size:25

TendermintMsgSend.from_address max_size:46
TendermintMsgSend.to_address max_size:46

TendermintSignedTx.public_key max_size:33
TendermintSignedTx.signature max_size:64
2 changes: 1 addition & 1 deletion lib/firmware/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ set(sources
app_layout.c
binance.c
coins.c
cosmos.c
crypto.c
eos.c
eos-contracts/eosio.system.c
Expand All @@ -29,6 +28,7 @@ set(sources
ripple.c
ripple_base58.c
signing.c
signtx_tendermint.c
storage.c
tendermint.c
thorchain.c
Expand Down
2 changes: 2 additions & 0 deletions lib/firmware/fsm.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
#include "keepkey/firmware/reset.h"
#include "keepkey/firmware/ripple.h"
#include "keepkey/firmware/signing.h"
#include "keepkey/firmware/signtx_tendermint.h"
#include "keepkey/firmware/storage.h"
#include "keepkey/firmware/tendermint.h"
#include "keepkey/firmware/thorchain.h"
Expand Down Expand Up @@ -288,4 +289,5 @@ void fsm_msgClearSession(ClearSession *msg) {
#include "fsm_msg_cosmos.h"
#include "fsm_msg_binance.h"
#include "fsm_msg_ripple.h"
#include "fsm_msg_tendermint.h"
#include "fsm_msg_thorchain.h"
4 changes: 2 additions & 2 deletions lib/firmware/fsm_msg_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ void fsm_msgInitialize(Initialize *msg) {
recovery_cipher_abort();
signing_abort();
ethereum_signing_abort();
cosmos_signAbort();
tendermint_signAbort();
eos_signingAbort();
session_clear(false); // do not clear PIN
layoutHome();
Expand Down Expand Up @@ -433,7 +433,7 @@ void fsm_msgCancel(Cancel *msg) {
recovery_cipher_abort();
signing_abort();
ethereum_signing_abort();
cosmos_signAbort();
tendermint_signAbort();
eos_signingAbort();
fsm_sendFailure(FailureType_Failure_ActionCancelled, "Aborted");
}
Expand Down
34 changes: 17 additions & 17 deletions lib/firmware/fsm_msg_cosmos.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ void fsm_msgCosmosSignTx(const CosmosSignTx *msg) {

if (!msg->has_account_number || !msg->has_chain_id || !msg->has_fee_amount ||
!msg->has_gas || !msg->has_sequence) {
cosmos_signAbort();
tendermint_signAbort();
fsm_sendFailure(FailureType_Failure_SyntaxError,
"Missing Fields On Message");
layoutHome();
Expand All @@ -92,8 +92,8 @@ void fsm_msgCosmosSignTx(const CosmosSignTx *msg) {

RESP_INIT(CosmosMsgRequest);

if (!cosmos_signTxInit(node, msg)) {
cosmos_signAbort();
if (!tendermint_signTxInit(node, (void *)msg, sizeof(CosmosSignTx), "uatom")) {
tendermint_signAbort();
memzero(node, sizeof(*node));
fsm_sendFailure(FailureType_Failure_FirmwareError,
_("Failed to initialize transaction signing"));
Expand All @@ -108,9 +108,9 @@ void fsm_msgCosmosSignTx(const CosmosSignTx *msg) {

void fsm_msgCosmosMsgAck(const CosmosMsgAck *msg) {
// Confirm transaction basics
CHECK_PARAM(cosmos_signingIsInited(), "Signing not in progress");
CHECK_PARAM(tendermint_signingIsInited(), "Signing not in progress");
if (!msg->has_send || !msg->send.has_to_address || !msg->send.has_amount) {
cosmos_signAbort();
tendermint_signAbort();
fsm_sendFailure(FailureType_Failure_FirmwareError,
_("Invalid Cosmos Message Type"));
layoutHome();
Expand All @@ -122,13 +122,13 @@ void fsm_msgCosmosMsgAck(const CosmosMsgAck *msg) {
return;
}

const CosmosSignTx *sign_tx = cosmos_getCosmosSignTx();
const CosmosSignTx *sign_tx = (CosmosSignTx *)tendermint_getSignTx();

switch (msg->send.address_type) {
case OutputAddressType_EXCHANGE: {
HDNode *root_node = fsm_getDerivedNode(SECP256K1_NAME, 0, 0, NULL);
if (!root_node) {
cosmos_signAbort();
tendermint_signAbort();
fsm_sendFailure(FailureType_Failure_FirmwareError, NULL);
layoutHome();
return;
Expand All @@ -138,7 +138,7 @@ void fsm_msgCosmosMsgAck(const CosmosMsgAck *msg) {
(void *)NULL, true);
if (ret < TXOUT_OK) {
memzero((void *)root_node, sizeof(*root_node));
cosmos_signAbort();
tendermint_signAbort();
send_fsm_co_error_message(ret);
layoutHome();
return;
Expand All @@ -154,7 +154,7 @@ void fsm_msgCosmosMsgAck(const CosmosMsgAck *msg) {
if (!confirm_transaction_output(
ButtonRequestType_ButtonRequest_ConfirmOutput, amount_str,
msg->send.to_address)) {
cosmos_signAbort();
tendermint_signAbort();
fsm_sendFailure(FailureType_Failure_ActionCancelled, NULL);
layoutHome();
return;
Expand All @@ -164,23 +164,23 @@ void fsm_msgCosmosMsgAck(const CosmosMsgAck *msg) {
}
}

if (!cosmos_signTxUpdateMsgSend(msg->send.amount, msg->send.to_address)) {
cosmos_signAbort();
if (!tendermint_signTxUpdateMsgSend(msg->send.amount, msg->send.to_address, "cosmos", "uatom", "cosmos-sdk")) {
tendermint_signAbort();
fsm_sendFailure(FailureType_Failure_SyntaxError,
"Failed to include send message in transaction");
layoutHome();
return;
}

if (!cosmos_signingIsFinished()) {
if (!tendermint_signingIsFinished()) {
RESP_INIT(CosmosMsgRequest);
msg_write(MessageType_MessageType_CosmosMsgRequest, resp);
return;
}

if (sign_tx->has_memo && !confirm(ButtonRequestType_ButtonRequest_ConfirmMemo,
_("Memo"), "%s", sign_tx->memo)) {
cosmos_signAbort();
tendermint_signAbort();
fsm_sendFailure(FailureType_Failure_ActionCancelled, NULL);
layoutHome();
return;
Expand All @@ -200,16 +200,16 @@ void fsm_msgCosmosMsgAck(const CosmosMsgAck *msg) {
"Sign this Cosmos transaction on %s? "
"It includes a fee of %" PRIu32 " uATOM and %" PRIu32 " gas.",
sign_tx->chain_id, sign_tx->fee_amount, sign_tx->gas)) {
cosmos_signAbort();
tendermint_signAbort();
fsm_sendFailure(FailureType_Failure_ActionCancelled, NULL);
layoutHome();
return;
}

RESP_INIT(CosmosSignedTx);

if (!cosmos_signTxFinalize(resp->public_key.bytes, resp->signature.bytes)) {
cosmos_signAbort();
if (!tendermint_signTxFinalize(resp->public_key.bytes, resp->signature.bytes)) {
tendermint_signAbort();
fsm_sendFailure(FailureType_Failure_SyntaxError,
"Failed to finalize signature");
layoutHome();
Expand All @@ -220,7 +220,7 @@ void fsm_msgCosmosMsgAck(const CosmosMsgAck *msg) {
resp->has_public_key = true;
resp->signature.size = 64;
resp->has_signature = true;
cosmos_signAbort();
tendermint_signAbort();
layoutHome();
msg_write(MessageType_MessageType_CosmosSignedTx, resp);
}
Loading

0 comments on commit fad542c

Please sign in to comment.