diff --git a/ecdsa.c b/ecdsa.c index 2394101f4f..3cb52ce44d 100644 --- a/ecdsa.c +++ b/ecdsa.c @@ -36,6 +36,9 @@ #include "base58.h" #include "macros.h" #include "secp256k1.h" +#if USE_ETHEREUM +#include "sha3.h" +#endif // Set cp2 = cp1 void point_copy(const curve_point *cp1, curve_point *cp2) @@ -850,6 +853,27 @@ void ecdsa_get_pubkeyhash(const uint8_t *pub_key, uint8_t *pubkeyhash) MEMSET_BZERO(h, sizeof(h)); } +#if USE_ETHEREUM +int ecdsa_get_ethereum_pubkeyhash(const uint8_t *pub_key, uint8_t *pubkeyhash) +{ + uint8_t h[65]; + SHA3_CTX ctx; + + if (!ecdsa_uncompress_pubkey(&secp256k1, pub_key, h)) { + return 0; + } + + sha3_256_Init(&ctx); + sha3_Update(&ctx, h + 1, 64); + keccak_Final(&ctx, h); + + // least significant 160 bits + memcpy(pubkeyhash, h + 12, 20); + + return 1; +} +#endif + void ecdsa_get_address_raw(const uint8_t *pub_key, uint8_t version, uint8_t *addr_raw) { addr_raw[0] = version; diff --git a/ecdsa.h b/ecdsa.h index b7c22aa4d8..2950a314f7 100644 --- a/ecdsa.h +++ b/ecdsa.h @@ -66,6 +66,7 @@ int ecdsa_sign_digest(const ecdsa_curve *curve, const uint8_t *priv_key, const u void ecdsa_get_public_key33(const ecdsa_curve *curve, const uint8_t *priv_key, uint8_t *pub_key); void ecdsa_get_public_key65(const ecdsa_curve *curve, const uint8_t *priv_key, uint8_t *pub_key); void ecdsa_get_pubkeyhash(const uint8_t *pub_key, uint8_t *pubkeyhash); +int ecdsa_get_ethereum_pubkeyhash(const uint8_t *pub_key, uint8_t *pubkeyhash); void ecdsa_get_address_raw(const uint8_t *pub_key, uint8_t version, uint8_t *addr_raw); void ecdsa_get_address(const uint8_t *pub_key, uint8_t version, char *addr, int addrsize); void ecdsa_get_wif(const uint8_t *priv_key, uint8_t version, char *wif, int wifsize); diff --git a/options.h b/options.h index 4baadf0bd5..fabca9f9b3 100644 --- a/options.h +++ b/options.h @@ -56,9 +56,14 @@ #define BIP39_CACHE_SIZE 4 #endif +// support Ethereum operations +#ifndef USE_ETHEREUM +#define USE_ETHEREUM 0 +#endif + // support Keccak hashing #ifndef USE_KECCAK -#define USE_KECCAK 0 +#define USE_KECCAK USE_ETHEREUM #endif #endif