From b1706561cd330005ff1ad72bb2a917f78b1c0ff6 Mon Sep 17 00:00:00 2001 From: Andrew Kozlik Date: Tue, 8 Feb 2022 18:57:05 +0100 Subject: [PATCH] fix(legacy): Remove Bitcoin Cash from BITCOIN_ONLY firmware. --- legacy/firmware/Makefile | 3 ++- legacy/firmware/crypto.c | 10 ++++++-- legacy/firmware/transaction.c | 46 +++++++++++++++++++++++------------ 3 files changed, 41 insertions(+), 18 deletions(-) diff --git a/legacy/firmware/Makefile b/legacy/firmware/Makefile index 6ded8c075..c48a2e65a 100644 --- a/legacy/firmware/Makefile +++ b/legacy/firmware/Makefile @@ -101,7 +101,6 @@ OBJS += ../vendor/trezor-crypto/pbkdf2.o OBJS += ../vendor/trezor-crypto/base32.o OBJS += ../vendor/trezor-crypto/base58.o OBJS += ../vendor/trezor-crypto/segwit_addr.o -OBJS += ../vendor/trezor-crypto/cash_addr.o OBJS += ../vendor/trezor-crypto/ripemd160.o OBJS += ../vendor/trezor-crypto/sha2.o @@ -138,7 +137,9 @@ OBJS += protob/messages-common.pb.o OBJS += protob/messages-crypto.pb.o OBJS += protob/messages-debug.pb.o OBJS += protob/messages-management.pb.o + ifneq ($(BITCOIN_ONLY),1) +OBJS += ../vendor/trezor-crypto/cash_addr.o OBJS += protob/messages-ethereum.pb.o OBJS += protob/messages-nem.pb.o OBJS += protob/messages-stellar.pb.o diff --git a/legacy/firmware/crypto.c b/legacy/firmware/crypto.c index d5314de27..faca68e9c 100644 --- a/legacy/firmware/crypto.c +++ b/legacy/firmware/crypto.c @@ -23,7 +23,6 @@ #include "aes/aes.h" #include "base58.h" #include "bip32.h" -#include "cash_addr.h" #include "coins.h" #include "curves.h" #include "hmac.h" @@ -33,6 +32,10 @@ #include "segwit_addr.h" #include "sha2.h" +#if !BITCOIN_ONLY +#include "cash_addr.h" +#endif + #define PATH_MAX_ACCOUNT 100 #define PATH_MAX_CHANGE 1 #define PATH_MAX_ADDRESS_INDEX 1000000 @@ -275,11 +278,14 @@ int cryptoMessageVerify(const CoinInfo *coin, const uint8_t *message, if (script_type == InputScriptType_SPENDADDRESS) { // p2pkh size_t len = 0; +#if !BITCOIN_ONLY if (coin->cashaddr_prefix) { if (!cash_addr_decode(addr_raw, &len, coin->cashaddr_prefix, address)) { return 1; // invalid address } - } else { + } else +#endif + { len = base58_decode_check(address, coin->curve->hasher_base58, addr_raw, MAX_ADDR_RAW_SIZE); } diff --git a/legacy/firmware/transaction.c b/legacy/firmware/transaction.c index 259ed9343..074c13121 100644 --- a/legacy/firmware/transaction.c +++ b/legacy/firmware/transaction.c @@ -21,7 +21,6 @@ #include #include "address.h" #include "base58.h" -#include "cash_addr.h" #include "coins.h" #include "crypto.h" #include "debug.h" @@ -35,6 +34,10 @@ #include "util.h" #include "zkp_bip340.h" +#if !BITCOIN_ONLY +#include "cash_addr.h" +#endif + #define SEGWIT_VERSION_0 0 #define SEGWIT_VERSION_1 1 @@ -151,13 +154,16 @@ bool compute_address(const CoinInfo *coin, InputScriptType script_type, } } else if (script_type == InputScriptType_SPENDADDRESS || script_type == InputScriptType_SPENDMULTISIG) { +#if !BITCOIN_ONLY if (coin->cashaddr_prefix) { raw[0] = CASHADDR_P2SH | CASHADDR_160; ripemd160(digest, 32, raw + 1); if (!cash_addr_encode(address, coin->cashaddr_prefix, raw, 21)) { return 0; } - } else { + } else +#endif + { // non-segwit p2sh multisig prelen = address_prefix_bytes_len(coin->address_type_p2sh); address_write_prefix_bytes(coin->address_type_p2sh, raw); @@ -202,13 +208,16 @@ bool compute_address(const CoinInfo *coin, InputScriptType script_type, coin->curve->hasher_base58, address, MAX_ADDR_SIZE); } else if (script_type == InputScriptType_SPENDADDRESS || script_type == InputScriptType_SPENDMULTISIG) { +#if !BITCOIN_ONLY if (coin->cashaddr_prefix) { ecdsa_get_address_raw(node->public_key, CASHADDR_P2KH | CASHADDR_160, coin->curve->hasher_pubkey, raw); if (!cash_addr_encode(address, coin->cashaddr_prefix, raw, 21)) { return 0; } - } else { + } else +#endif + { ecdsa_get_address(node->public_key, coin->address_type, coin->curve->hasher_pubkey, coin->curve->hasher_base58, address, MAX_ADDR_SIZE); @@ -252,6 +261,7 @@ static int address_to_script_pubkey(const CoinInfo *coin, const char *address, return 1; } +#if !BITCOIN_ONLY if (coin->cashaddr_prefix && cash_addr_decode(addr_raw, &addr_raw_len, coin->cashaddr_prefix, address)) { @@ -276,6 +286,7 @@ static int address_to_script_pubkey(const CoinInfo *coin, const char *address, return 0; } } +#endif // SegWit if (coin->bech32_prefix) { @@ -991,6 +1002,7 @@ uint32_t tx_output_weight(const CoinInfo *coin, const TxOutputType *txoutput) { uint8_t addr_raw[MAX_ADDR_RAW_SIZE] = {0}; int witver = 0; size_t addr_raw_len = 0; +#if !BITCOIN_ONLY if (coin->cashaddr_prefix && cash_addr_decode(addr_raw, &addr_raw_len, coin->cashaddr_prefix, txoutput->address)) { @@ -1000,18 +1012,22 @@ uint32_t tx_output_weight(const CoinInfo *coin, const TxOutputType *txoutput) { addr_raw[0] == (CASHADDR_P2SH | CASHADDR_160)) { output_script_size = TXSIZE_P2SCRIPT; } - } else if (coin->bech32_prefix && - segwit_addr_decode(&witver, addr_raw, &addr_raw_len, - coin->bech32_prefix, txoutput->address)) { - output_script_size = 2 + addr_raw_len; - } else { - addr_raw_len = - base58_decode_check(txoutput->address, coin->curve->hasher_base58, - addr_raw, MAX_ADDR_RAW_SIZE); - if (address_check_prefix(addr_raw, coin->address_type)) { - output_script_size = TXSIZE_P2PKHASH; - } else if (address_check_prefix(addr_raw, coin->address_type_p2sh)) { - output_script_size = TXSIZE_P2SCRIPT; + } else +#endif + { + if (coin->bech32_prefix && + segwit_addr_decode(&witver, addr_raw, &addr_raw_len, + coin->bech32_prefix, txoutput->address)) { + output_script_size = 2 + addr_raw_len; + } else { + addr_raw_len = + base58_decode_check(txoutput->address, coin->curve->hasher_base58, + addr_raw, MAX_ADDR_RAW_SIZE); + if (address_check_prefix(addr_raw, coin->address_type)) { + output_script_size = TXSIZE_P2PKHASH; + } else if (address_check_prefix(addr_raw, coin->address_type_p2sh)) { + output_script_size = TXSIZE_P2SCRIPT; + } } } }