Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/ldg 416 migrate msgsend txtype logic #2

Merged
12 commits merged into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
20 changes: 20 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
BasedOnStyle: Google
IndentWidth: 4
---
Language: Cpp
ColumnLimit: 100
PointerAlignment: Right
AlignAfterOpenBracket: Align
AlignConsecutiveMacros: true
AllowAllParametersOfDeclarationOnNextLine: false
SortIncludes: false
SpaceAfterCStyleCast: true
AllowShortCaseLabelsOnASingleLine: false
AllowAllArgumentsOnNextLine: false
AllowAllParametersOfDeclarationOnNextLine: false
AllowShortBlocksOnASingleLine: Never
AllowShortFunctionsOnASingleLine: None
BinPackArguments: false
BinPackParameters: false
---
8 changes: 8 additions & 0 deletions .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Checklist
<!-- Put an `x` in each box when you have completed the items. -->
- [ ] App update process has been followed <!-- See comment below -->
- [ ] Target branch is `develop` <!-- unless you have a very good reason -->
- [ ] Application version has been bumped <!-- required if your changes are to be deployed -->

<!-- Make sure you followed the process described in https://developers.ledger.com/docs/embedded-app/maintenance/ before opening your Pull Request.
Don't hesitate to contact us directly on Discord if you have any questions ! https://developers.ledger.com/discord -->
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#* limitations under the License.
#********************************************************************************
cmake_minimum_required(VERSION 3.0)
project(ledger-cosmos VERSION 0.0.0)
project(ledger-thorchain VERSION 0.0.0)
enable_testing()

cmake_policy(SET CMP0025 NEW)
Expand Down
14 changes: 9 additions & 5 deletions app/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ $(info ************ TARGET_NAME = [$(TARGET_NAME)])
include $(CURDIR)/../deps/ledger-zxlib/makefiles/Makefile.app_testing

ifndef COIN
COIN=ATOM
COIN=RUNE
endif

$(info COIN = [$(COIN)])

ifeq ($(COIN),ATOM)
ifeq ($(COIN),RUNE)
# Main app configuration
APPNAME = "Cosmos"
APPPATH = "44'/118'" --path "44'/60'"
APPNAME = "THORChain"
APPPATH = "44'/931'"
else
define error_message

Expand All @@ -52,6 +52,8 @@ endif

APP_LOAD_PARAMS = --curve secp256k1 $(COMMON_LOAD_PARAMS) --path $(APPPATH)

ENABLE_PENDING_REVIEW_SCREEN ?= 1

include $(CURDIR)/../deps/ledger-zxlib/makefiles/Makefile.devices

$(info TARGET_NAME = [$(TARGET_NAME)])
Expand All @@ -64,9 +66,11 @@ endif
include $(CURDIR)/../deps/ledger-zxlib/makefiles/Makefile.platform
DEFINES += HAVE_HASH HAVE_BLAKE2 HAVE_SHA256 HAVE_SHA512
CFLAGS += -I$(MY_DIR)/../deps/tinycbor/src
CFLAGS += -g3 -ggdb3 -O3
APP_SOURCE_PATH += $(MY_DIR)/../deps/tinycbor-ledger
APP_SOURCE_PATH += $(MY_DIR)/../deps/jsmn/src


.PHONY: rust
rust:
@echo "No rust code"
Expand All @@ -87,7 +91,7 @@ dep/%.d: %.c Makefile

.PHONY: listvariants
listvariants:
@echo VARIANTS COIN ATOM
@echo VARIANTS COIN RUNE

.PHONY: version
version:
Expand Down
4 changes: 2 additions & 2 deletions app/Makefile.version
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This is the `transaction_version` field of `Runtime`
APPVERSION_M=2
# This is the `spec_version` field of `Runtime`
APPVERSION_N=35
APPVERSION_N=3
# This is the patch version of this release
APPVERSION_P=18
APPVERSION_P=0
Binary file modified app/glyphs/icon_app.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 1 addition & 12 deletions app/src/apdu_handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,6 @@

#include "chain_config.h"

static const char *msg_error1 = "Expert Mode";
static const char *msg_error2 = "Required";

__Z_INLINE void handle_getversion(__Z_UNUSED volatile uint32_t *flags, volatile uint32_t *tx, __Z_UNUSED uint32_t rx) {
#ifdef DEBUG
G_io_apdu_buffer[0] = 0xFF;
Expand Down Expand Up @@ -95,7 +92,7 @@ __Z_INLINE void extractHDPath(uint32_t rx, uint32_t offset) {

// Check values
if (hdPath[0] != HDPATH_0_DEFAULT ||
((hdPath[1] != HDPATH_1_DEFAULT) && (hdPath[1] != HDPATH_ETH_1_DEFAULT)) ||
(hdPath[1] != HDPATH_1_DEFAULT) ||
hdPath[3] != HDPATH_3_DEFAULT) {
THROW(APDU_CODE_DATA_INVALID);
}
Expand All @@ -122,8 +119,6 @@ static void extractHDPath_HRP(uint32_t rx, uint32_t offset) {
ZEMU_LOGF(50, "Chain config not supported for: %s\n", bech32_hrp)
THROW(APDU_CODE_COMMAND_NOT_ALLOWED);
}
} else if (hdPath[1] == HDPATH_ETH_1_DEFAULT) {
THROW(APDU_CODE_COMMAND_NOT_ALLOWED);
}
}

Expand Down Expand Up @@ -197,12 +192,6 @@ __Z_INLINE void handleSign(volatile uint32_t *flags, volatile uint32_t *tx, uint
// Let grab P2 value and if it's not valid, the parser should reject it
const tx_type_e sign_type = (tx_type_e) G_io_apdu_buffer[OFFSET_P2];

if ((hdPath[1] == HDPATH_ETH_1_DEFAULT) && !app_mode_expert()) {
*flags |= IO_ASYNCH_REPLY;
view_custom_error_show(PIC(msg_error1),PIC(msg_error2));
THROW(APDU_CODE_DATA_INVALID);
}

parser_tx_obj.tx_json.own_addr = (const char *) (G_io_apdu_buffer + VIEW_ADDRESS_OFFSET_SECP256K1);
const char *error_msg = tx_parse(sign_type);
if (error_msg != NULL) {
Expand Down
15 changes: 7 additions & 8 deletions app/src/coin.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ extern "C" {
#define HDPATH_LEN_DEFAULT 5

#define HDPATH_0_DEFAULT (0x80000000u | 0x2cu)
#define HDPATH_1_DEFAULT (0x80000000u | 0x76u)
#define HDPATH_ETH_1_DEFAULT (0x80000000u | 0x3cu)
#define HDPATH_1_DEFAULT (0x80000000u | 0x3a3u)
#define HDPATH_2_DEFAULT (0x80000000u | 0u)
#define HDPATH_3_DEFAULT (0u)

Expand All @@ -50,18 +49,18 @@ typedef enum {
#define VIEW_ADDRESS_OFFSET_SECP256K1 PK_LEN_SECP256K1
#define VIEW_ADDRESS_LAST_PAGE_DEFAULT 0

#define MENU_MAIN_APP_LINE1 "Cosmos"
#define MENU_MAIN_APP_LINE1 "THORChain"
#define MENU_MAIN_APP_LINE2 "Ready"
#define APPVERSION_LINE1 "Version:"
#define APPVERSION_LINE2 ("v" APPVERSION)

#define COIN_DEFAULT_CHAINID "cosmoshub-4"
#define COIN_DEFAULT_CHAINID "thorchain"

// In non-expert mode, the app will convert from uatom to ATOM
#define COIN_DEFAULT_DENOM_BASE "uatom"
#define COIN_DEFAULT_DENOM_REPR "ATOM"
#define COIN_DEFAULT_DENOM_FACTOR 6u
#define COIN_DEFAULT_DENOM_TRIMMING 6u
#define COIN_DEFAULT_DENOM_BASE "rune"
#define COIN_DEFAULT_DENOM_REPR "RUNE"
#define COIN_DEFAULT_DENOM_FACTOR 8u
#define COIN_DEFAULT_DENOM_TRIMMING 0u

// Coin denoms may be up to 128 characters long
// https://github.com/cosmos/cosmos-sdk/blob/master/types/coin.go#L780
Expand Down
20 changes: 2 additions & 18 deletions app/src/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -115,34 +115,18 @@ __Z_INLINE bool parser_areEqual(uint16_t tokenIdx, const char *expected) {
}

__Z_INLINE bool parser_isAmount(char *key) {
if (strcmp(key, "fee/amount") == 0) {
return true;
}

if (strcmp(key, "msgs/inputs/coins") == 0) {
return true;
}

if (strcmp(key, "msgs/outputs/coins") == 0) {
if (strcmp(key, "msgs/value/coins") == 0){
return true;
}

if (strcmp(key, "msgs/value/inputs/coins") == 0) {
return true;
}

if (strcmp(key, "msgs/value/outputs/coins") == 0) {
if (strcmp(key, "fee/amount") == 0) {
return true;
}

if (strcmp(key, "msgs/value/amount") == 0) {
return true;
}

if (strcmp(key, "tip/amount") == 0) {
return true;
}

return false;
}

Expand Down
39 changes: 7 additions & 32 deletions app/src/tx_display.c
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ __Z_INLINE parser_error_t get_subitem_count(root_item_e root_item, uint8_t *num_
case root_item_fee:
CHECK_PARSER_ERR(tx_is_expert_mode_or_not_default_chainid(&is_expert_or_default))
if (!is_expert_or_default) {
tmp_num_items = 1; // Only Amount
tmp_num_items = 0;
}
break;
case root_item_tip:
Expand Down Expand Up @@ -508,42 +508,17 @@ static const key_subst_t key_substitutions[] = {
{"memo", "Memo"},
{"fee/amount", "Fee"},
{"fee/gas", "Gas"},
{"fee/gas_limit", "Gas Limit"},
{"fee/granter", "Granter"},
{"fee/payer", "Payer"},
{"msgs/type", "Type"},

{"tip/amount", "Tip"},
{"tip/tipper", "Tipper"},

{"msgs/inputs/address", "Source Address"},
{"msgs/inputs/coins", "Source Coins"},
{"msgs/outputs/address", "Dest Address"},
{"msgs/outputs/coins", "Dest Coins"},

{"msgs/value/inputs/address", "Source Address"},
{"msgs/value/inputs/coins", "Source Coins"},
{"msgs/value/outputs/address", "Dest Address"},
{"msgs/value/outputs/coins", "Dest Coins"},

// MsgSend
{"msgs/value/from_address", "From"},
{"msgs/value/to_address", "To"},
{"msgs/value/amount", "Amount"},
{"msgs/value/delegator_address", "Delegator"},
{"msgs/value/validator_address", "Validator"},
{"msgs/value/withdraw_address", "Withdraw Address"},
{"msgs/value/validator_src_address", "Validator Source"},
{"msgs/value/validator_dst_address", "Validator Dest"},
{"msgs/value/description", "Description"},
{"msgs/value/initial_deposit/amount", "Deposit Amount"},
{"msgs/value/initial_deposit/denom", "Deposit Denom"},
{"msgs/value/proposal_type", "Proposal"},
{"msgs/value/proposer", "Proposer"},
{"msgs/value/title", "Title"},
{"msgs/value/depositor", "Sender"},
{"msgs/value/proposal_id", "Proposal ID"},
{"msgs/value/voter", "Description"},
{"msgs/value/option", "Option"},

// MsgDeposit
{"msgs/value/signer", "Sender"},
{"msgs/value/memo", "Memo"},
{"msgs/value/coins", "Amount"},
};

parser_error_t tx_display_make_friendly() {
Expand Down
14 changes: 3 additions & 11 deletions app/src/tx_parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,9 @@ __Z_INLINE void strcat_chunk_s(char *dst, uint16_t dst_max, const char *src_chun
///////////////////////////

static const key_subst_t value_substitutions[] = {
{"cosmos-sdk/MsgSend", "Send"},
{"cosmos-sdk/MsgDelegate", "Delegate"},
{"cosmos-sdk/MsgUndelegate", "Undelegate"},
{"cosmos-sdk/MsgBeginRedelegate", "Redelegate"},
{"cosmos-sdk/MsgSubmitProposal", "Propose"},
{"cosmos-sdk/MsgDeposit", "Deposit"},
{"cosmos-sdk/MsgVote", "Vote"},
{"cosmos-sdk/MsgWithdrawDelegationReward", "Withdraw Reward"},
{"cosmos-sdk/MsgWithdrawValidatorCommission", "Withdraw Val. Commission"},
{"cosmos-sdk/MsgSetWithdrawAddress", "Withdraw Set Address"},
{"cosmos-sdk/MsgMultiSend", "Multi Send"},
{"thorchain/MsgSend", "Send"},
{"thorchain/MsgDeposit", "Deposit"},
{"[]", "Empty"},

};

Expand Down
17 changes: 17 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
version: "3.7"

services:
nanosp:
# platform: linux/arm64/v8
image: ghcr.io/blooo-io/speculos:latest
volumes:
- ./app/build/nanos2/bin:/speculos/apps
- ./app/src:/app/app/src
- ./deps:/app/deps
ports:
- "5000:5000" # api
- "40000:40000" # apdu
environment:
- GDB_DIRECTORY_LIST="/app/app/src:/app/deps"
command: "-d --model nanosp apps/app.elf --display headless --apdu-port 40000 "
# Add `--vnc-password "<password>"` for macos users to use built-in vnc client.
12 changes: 12 additions & 0 deletions gdb.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/bash

docker compose up -d

# Wait for the container to start
until docker inspect -f '{{.State.Status}}' app-thorchain-nanosp-1 | grep -q "running"; do
sleep 1
done

docker exec -it app-thorchain-nanosp-1 ./tools/debug.sh apps/app.elf

docker compose down
6 changes: 3 additions & 3 deletions tests/json_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ namespace {

TEST(TxValidationTest, ObjectGetValueCorrectFormat) {
auto transaction =
R"({"account_number":"0","chain_id":"test-chain-1","fee":{"amount":[{"amount":"5","denom":"photon"}],"gas":"10000"},"memo":"testmemo","msgs":[{"inputs":[{"address":"cosmosaccaddr1d9h8qat5e4ehc5","coins":[{"amount":"10","denom":"atom"}]}],"outputs":[{"address":"cosmosaccaddr1da6hgur4wse3jx32","coins":[{"amount":"10","denom":"atom"}]}]}],"sequence":"1"})";
R"({"account_number":"588","chain_id":"thorchain","fee":{"amount":[],"gas":"2000000"},"memo":"TestMemo","msgs":[{"type":"thorchain/MsgSend","value":{"amount":[{"amount":"150000000","denom":"rune"}],"from_address":"tthor1c648xgpter9xffhmcqvs7lzd7hxh0prgv5t5gp","to_address":"tthor10xgrknu44d83qr4s4uw56cqxg0hsev5e68lc9z","test":"test"}}],"sequence":"5"})";
parsed_json_t parsed_json;
JSON_PARSE(&parsed_json, transaction);

Expand All @@ -383,9 +383,9 @@ namespace {
EXPECT_EQ(token_index, 6) << "Wrong token index";

EXPECT_EQ(object_get_value(&parsed_json, 0, "msgs", &token_index), parser_ok);
EXPECT_EQ(token_index, 19) << "Wrong token index";
EXPECT_EQ(token_index, 14) << "Wrong token index";

EXPECT_EQ(object_get_value(&parsed_json, 0, "sequence", &token_index), parser_ok);
EXPECT_EQ(token_index, 46) << "Wrong token index";
EXPECT_EQ(token_index, 34) << "Wrong token index";
}
}
Loading
Loading