mirror of
https://github.com/trezor/trezor-firmware.git
synced 2024-11-22 23:48:12 +00:00
extract address related stuff into trezor-crypto
This commit is contained in:
parent
e70900d49e
commit
c0181b1aec
@ -21,6 +21,7 @@ OBJS += ethereum.o
|
||||
|
||||
OBJS += debug.o
|
||||
|
||||
OBJS += ../vendor/trezor-crypto/address.o
|
||||
OBJS += ../vendor/trezor-crypto/bignum.o
|
||||
OBJS += ../vendor/trezor-crypto/ecdsa.o
|
||||
OBJS += ../vendor/trezor-crypto/curves.o
|
||||
|
@ -19,6 +19,7 @@
|
||||
|
||||
#include <string.h>
|
||||
#include "coins.h"
|
||||
#include "address.h"
|
||||
|
||||
// filled CoinType Protobuf structure defined in https://github.com/trezor/trezor-common/blob/master/protob/types.proto#L133
|
||||
// address types > 0xFF represent a two-byte prefix in big-endian order
|
||||
@ -68,51 +69,21 @@ const CoinType *coinByAddressType(uint32_t address_type)
|
||||
return 0;
|
||||
}
|
||||
|
||||
size_t prefixBytesByAddressType(uint32_t address_type)
|
||||
bool coinExtractAddressType(const CoinType *coin, const uint8_t *addr, uint32_t *address_type)
|
||||
{
|
||||
if (address_type <= 0xFF) return 1;
|
||||
if (address_type <= 0xFFFF) return 2;
|
||||
if (address_type <= 0xFFFFFF) return 3;
|
||||
return 4;
|
||||
}
|
||||
|
||||
bool addressHasExpectedPrefix(const uint8_t *addr, uint32_t address_type)
|
||||
{
|
||||
if (address_type <= 0xFF) {
|
||||
return address_type == (uint32_t)(addr[0]);
|
||||
}
|
||||
if (address_type <= 0xFFFF) {
|
||||
return address_type == ((uint32_t)(addr[0] << 8) | (uint32_t)(addr[1]));
|
||||
}
|
||||
if (address_type <= 0xFFFFFF) {
|
||||
return address_type == ((uint32_t)(addr[0] << 16) | (uint32_t)(addr[1] << 8) | (uint32_t)(addr[2]));
|
||||
}
|
||||
return address_type == ((uint32_t)(addr[0] << 24) | (uint32_t)(addr[1] << 16) | (uint32_t)(addr[2] << 8) | (uint32_t)(addr[3]));
|
||||
}
|
||||
|
||||
void writeAddressPrefix(uint8_t *addr, uint32_t address_type)
|
||||
{
|
||||
if (address_type > 0xFFFFFF) *(addr++) = address_type >> 24;
|
||||
if (address_type > 0xFFFF) *(addr++) = (address_type >> 16) & 0xFF;
|
||||
if (address_type > 0xFF) *(addr++) = (address_type >> 8) & 0xFF;
|
||||
*(addr++) = address_type & 0xFF;
|
||||
}
|
||||
|
||||
bool getAddressType(const CoinType *coin, const uint8_t *addr, uint32_t *address_type)
|
||||
{
|
||||
if (coin->has_address_type && addressHasExpectedPrefix(addr, coin->address_type)) {
|
||||
if (coin->has_address_type && address_check_prefix(addr, coin->address_type)) {
|
||||
*address_type = coin->address_type;
|
||||
return true;
|
||||
}
|
||||
if (coin->has_address_type_p2sh && addressHasExpectedPrefix(addr, coin->address_type_p2sh)) {
|
||||
if (coin->has_address_type_p2sh && address_check_prefix(addr, coin->address_type_p2sh)) {
|
||||
*address_type = coin->address_type_p2sh;
|
||||
return true;
|
||||
}
|
||||
if (coin->has_address_type_p2wpkh && addressHasExpectedPrefix(addr, coin->address_type_p2wpkh)) {
|
||||
if (coin->has_address_type_p2wpkh && address_check_prefix(addr, coin->address_type_p2wpkh)) {
|
||||
*address_type = coin->address_type_p2wpkh;
|
||||
return true;
|
||||
}
|
||||
if (coin->has_address_type_p2wsh && addressHasExpectedPrefix(addr, coin->address_type_p2wsh)) {
|
||||
if (coin->has_address_type_p2wsh && address_check_prefix(addr, coin->address_type_p2wsh)) {
|
||||
*address_type = coin->address_type_p2wsh;
|
||||
return true;
|
||||
}
|
||||
|
@ -29,9 +29,6 @@ extern const CoinType coins[COINS_COUNT];
|
||||
const CoinType *coinByShortcut(const char *shortcut);
|
||||
const CoinType *coinByName(const char *name);
|
||||
const CoinType *coinByAddressType(uint32_t address_type);
|
||||
size_t prefixBytesByAddressType(uint32_t address_type);
|
||||
bool addressHasExpectedPrefix(const uint8_t *addr, uint32_t address_type);
|
||||
void writeAddressPrefix(uint8_t *addr, uint32_t address_type);
|
||||
bool getAddressType(const CoinType *coin, const uint8_t *addr, uint32_t *address_type);
|
||||
bool coinExtractAddressType(const CoinType *coin, const uint8_t *addr, uint32_t *address_type);
|
||||
|
||||
#endif
|
||||
|
@ -27,6 +27,7 @@
|
||||
#include "layout.h"
|
||||
#include "curves.h"
|
||||
#include "secp256k1.h"
|
||||
#include "address.h"
|
||||
#include "macros.h"
|
||||
#include "coins.h"
|
||||
|
||||
@ -173,11 +174,11 @@ int cryptoMessageVerify(const CoinType *coin, const uint8_t *message, size_t mes
|
||||
}
|
||||
// check if the address is correct
|
||||
uint32_t address_type;
|
||||
if (!getAddressType(coin, address_raw, &address_type)) {
|
||||
if (!coinExtractAddressType(coin, address_raw, &address_type)) {
|
||||
return 2;
|
||||
}
|
||||
ecdsa_get_address_raw(pubkey, address_type, addr_raw);
|
||||
if (memcmp(addr_raw, address_raw, prefixBytesByAddressType(address_type) + 20) != 0) {
|
||||
if (memcmp(addr_raw, address_raw, address_prefix_bytes_len(address_type) + 20) != 0) {
|
||||
return 2;
|
||||
}
|
||||
return 0;
|
||||
|
@ -28,6 +28,7 @@
|
||||
#include "crypto.h"
|
||||
#include "ripemd160.h"
|
||||
#include "base58.h"
|
||||
#include "address.h"
|
||||
#include "messages.pb.h"
|
||||
#include "types.pb.h"
|
||||
|
||||
@ -91,7 +92,7 @@ int compile_output(const CoinType *coin, const HDNode *root, TxOutputType *in, T
|
||||
out->script_pubkey.bytes[0] = 0x76; // OP_DUP
|
||||
out->script_pubkey.bytes[1] = 0xA9; // OP_HASH_160
|
||||
out->script_pubkey.bytes[2] = 0x14; // pushing 20 bytes
|
||||
memcpy(out->script_pubkey.bytes + 3, addr_raw + prefixBytesByAddressType(coin->address_type), 20);
|
||||
memcpy(out->script_pubkey.bytes + 3, addr_raw + address_prefix_bytes_len(coin->address_type), 20);
|
||||
out->script_pubkey.bytes[23] = 0x88; // OP_EQUALVERIFY
|
||||
out->script_pubkey.bytes[24] = 0xAC; // OP_CHECKSIG
|
||||
out->script_pubkey.size = 25;
|
||||
@ -110,7 +111,7 @@ int compile_output(const CoinType *coin, const HDNode *root, TxOutputType *in, T
|
||||
}
|
||||
out->script_pubkey.bytes[0] = 0xA9; // OP_HASH_160
|
||||
out->script_pubkey.bytes[1] = 0x14; // pushing 20 bytes
|
||||
memcpy(out->script_pubkey.bytes + 2, addr_raw + prefixBytesByAddressType(coin->address_type_p2sh), 20);
|
||||
memcpy(out->script_pubkey.bytes + 2, addr_raw + address_prefix_bytes_len(coin->address_type_p2sh), 20);
|
||||
out->script_pubkey.bytes[22] = 0x87; // OP_EQUAL
|
||||
out->script_pubkey.size = 23;
|
||||
return 23;
|
||||
@ -118,14 +119,14 @@ int compile_output(const CoinType *coin, const HDNode *root, TxOutputType *in, T
|
||||
|
||||
if (in->script_type == OutputScriptType_PAYTOMULTISIG) {
|
||||
uint8_t buf[32];
|
||||
size_t prefix_bytes = prefixBytesByAddressType(coin->address_type_p2sh);
|
||||
size_t prefix_bytes = address_prefix_bytes_len(coin->address_type_p2sh);
|
||||
if (!in->has_multisig) {
|
||||
return 0;
|
||||
}
|
||||
if (compile_script_multisig_hash(&(in->multisig), buf) == 0) {
|
||||
return 0;
|
||||
}
|
||||
writeAddressPrefix(addr_raw, coin->address_type_p2sh);
|
||||
address_write_prefix_bytes(coin->address_type_p2sh, addr_raw);
|
||||
ripemd160(buf, 32, addr_raw + prefix_bytes);
|
||||
if (needs_confirm) {
|
||||
base58_encode_check(addr_raw, prefix_bytes + 20, in->address, sizeof(in->address));
|
||||
|
2
vendor/trezor-crypto
vendored
2
vendor/trezor-crypto
vendored
@ -1 +1 @@
|
||||
Subproject commit ad73c0d4e73fe138ebbbc39d6a335167ba7c9923
|
||||
Subproject commit b05776be77168738d94ef9963019abb4d80a5356
|
Loading…
Reference in New Issue
Block a user