From af06a997cbb83e733e224be0dff2c8d7d8b49daf Mon Sep 17 00:00:00 2001 From: Pavol Rusnak Date: Tue, 25 Jul 2017 17:59:26 +0200 Subject: [PATCH] refactor ecdsa_get_address_segwit_p2sh{,_raw} --- bip32.c | 2 +- ecdsa.c | 24 ++++++++++++++---------- ecdsa.h | 3 ++- test_check.c | 5 ++++- 4 files changed, 21 insertions(+), 13 deletions(-) diff --git a/bip32.c b/bip32.c index cfa05ed614..6adc7cdf9c 100644 --- a/bip32.c +++ b/bip32.c @@ -298,7 +298,7 @@ void hdnode_public_ckd_address_optimized(const curve_point *pub, const uint8_t * switch (addrformat) { case 1: // Segwit-in-P2SH - ecdsa_get_address_segwit(child_pubkey, version, addr, addrsize); + ecdsa_get_address_segwit_p2sh(child_pubkey, version, addr, addrsize); break; default: // normal address ecdsa_get_address(child_pubkey, version, addr, addrsize); diff --git a/ecdsa.c b/ecdsa.c index 995afc5e73..a2e1f430a7 100644 --- a/ecdsa.c +++ b/ecdsa.c @@ -907,21 +907,25 @@ void ecdsa_get_address(const uint8_t *pub_key, uint32_t version, char *addr, int MEMSET_BZERO(raw, sizeof(raw)); } -void ecdsa_get_address_segwit(const uint8_t *pub_key, uint32_t version, char *addr, int addrsize) +void ecdsa_get_address_segwit_p2sh_raw(const uint8_t *pub_key, uint32_t version, uint8_t *addr_raw) +{ + size_t prefix_len = address_prefix_bytes_len(version); + uint8_t digest[32]; + addr_raw[0] = 0; // version byte + addr_raw[1] = 20; // push 20 bytes + ecdsa_get_pubkeyhash(pub_key, addr_raw + 2); + sha256_Raw(addr_raw, 22, digest); + address_write_prefix_bytes(version, addr_raw); + ripemd160(digest, 32, addr_raw + prefix_len); +} + +void ecdsa_get_address_segwit_p2sh(const uint8_t *pub_key, uint32_t version, char *addr, int addrsize) { uint8_t raw[MAX_ADDR_RAW_SIZE]; size_t prefix_len = address_prefix_bytes_len(version); - uint8_t digest[32]; - raw[0] = 0; // version byte - raw[1] = 20; // push 20 bytes - ecdsa_get_pubkeyhash(pub_key, raw + 2); - sha256_Raw(raw, 22, digest); - address_write_prefix_bytes(version, raw); - ripemd160(digest, 32, raw + prefix_len); + ecdsa_get_address_segwit_p2sh_raw(pub_key, version, raw); base58_encode_check(raw, prefix_len + 20, addr, addrsize); - // not as important to clear these, but we might as well MEMSET_BZERO(raw, sizeof(raw)); - MEMSET_BZERO(digest, sizeof(digest)); } void ecdsa_get_wif(const uint8_t *priv_key, uint32_t version, char *wif, int wifsize) diff --git a/ecdsa.h b/ecdsa.h index bd55451f0d..d21a3855dc 100644 --- a/ecdsa.h +++ b/ecdsa.h @@ -74,7 +74,8 @@ void ecdsa_get_public_key65(const ecdsa_curve *curve, const uint8_t *priv_key, u void ecdsa_get_pubkeyhash(const uint8_t *pub_key, uint8_t *pubkeyhash); void ecdsa_get_address_raw(const uint8_t *pub_key, uint32_t version, uint8_t *addr_raw); void ecdsa_get_address(const uint8_t *pub_key, uint32_t version, char *addr, int addrsize); -void ecdsa_get_address_segwit(const uint8_t *pub_key, uint32_t version, char *addr, int addrsize); +void ecdsa_get_address_segwit_p2sh_raw(const uint8_t *pub_key, uint32_t version, uint8_t *addr_raw); +void ecdsa_get_address_segwit_p2sh(const uint8_t *pub_key, uint32_t version, char *addr, int addrsize); void ecdsa_get_wif(const uint8_t *priv_key, uint32_t version, char *wif, int wifsize); int ecdsa_address_decode(const char *addr, uint32_t version, uint8_t *out); diff --git a/test_check.c b/test_check.c index bd2e2d82ca..9dde969263 100644 --- a/test_check.c +++ b/test_check.c @@ -2317,24 +2317,28 @@ START_TEST(test_address) ecdsa_get_address(pub_key, 111, address, sizeof(address)); ck_assert_str_eq(address, "mhfJsQNnrXB3uuYZqvywARTDfuvyjg4RBh"); ecdsa_get_address(pub_key, 52, address, sizeof(address)); ck_assert_str_eq(address, "MxiimznnxsqMfLKTQBL8Z2PoY9jKpjgkCu"); ecdsa_get_address(pub_key, 48, address, sizeof(address)); ck_assert_str_eq(address, "LMNJqZbe89yrPbm7JVzrcXJf28hZ1rKPaH"); + ecdsa_get_address_segwit_p2sh(pub_key, 5, address, sizeof(address)); ck_assert_str_eq(address, "34PyTHn74syS796eTgsyoLfwoBC3cwLn6p"); memcpy(pub_key, fromhex("025b1654a0e78d28810094f6c5a96b8efb8a65668b578f170ac2b1f83bc63ba856"), 33); ecdsa_get_address(pub_key, 0, address, sizeof(address)); ck_assert_str_eq(address, "19Ywfm3witp6C1yBMy4NRYHY2347WCRBfQ"); ecdsa_get_address(pub_key, 111, address, sizeof(address)); ck_assert_str_eq(address, "mp4txp8vXvFLy8So5Y2kFTVrt2epN6YzdP"); ecdsa_get_address(pub_key, 52, address, sizeof(address)); ck_assert_str_eq(address, "N58JsQYveGueiZDgdnNwe4SSkGTAToutAY"); ecdsa_get_address(pub_key, 48, address, sizeof(address)); ck_assert_str_eq(address, "LTmtvyMmoZ49SpfLY73fhZMJEFRPdyohKh"); + ecdsa_get_address_segwit_p2sh(pub_key, 5, address, sizeof(address)); ck_assert_str_eq(address, "35trq6eeuHf6VL9L8pQv46x3vegHnHoTuB"); memcpy(pub_key, fromhex("03433f246a12e6486a51ff08802228c61cf895175a9b49ed4766ea9a9294a3c7fe"), 33); ecdsa_get_address(pub_key, 0, address, sizeof(address)); ck_assert_str_eq(address, "1FWE2bn3MWhc4QidcF6AvEWpK77sSi2cAP"); ecdsa_get_address(pub_key, 111, address, sizeof(address)); ck_assert_str_eq(address, "mv2BKes2AY8rqXCFKp4Yk9j9B6iaMfWRLN"); ecdsa_get_address(pub_key, 52, address, sizeof(address)); ck_assert_str_eq(address, "NB5bEFH2GtoAawy8t4Qk8kfj3LWvQs3MhB"); ecdsa_get_address(pub_key, 48, address, sizeof(address)); ck_assert_str_eq(address, "LZjBHp5sSAwfKDQnnP5UCFaaXKV9YheGxQ"); + ecdsa_get_address_segwit_p2sh(pub_key, 5, address, sizeof(address)); ck_assert_str_eq(address, "3456DYaKUWuY6RWWw8Hp5CftHLcQN29h9Y"); memcpy(pub_key, fromhex("03aeb03abeee0f0f8b4f7a5d65ce31f9570cef9f72c2dd8a19b4085a30ab033d48"), 33); ecdsa_get_address(pub_key, 0, address, sizeof(address)); ck_assert_str_eq(address, "1yrZb8dhdevoqpUEGi2tUccUEeiMKeLcs"); ecdsa_get_address(pub_key, 111, address, sizeof(address)); ck_assert_str_eq(address, "mgVoreDcWf6BaxJ5wqgQiPpwLEFRLSr8U8"); ecdsa_get_address(pub_key, 52, address, sizeof(address)); ck_assert_str_eq(address, "MwZDmEdcd1kVLP4yW62c6zmXCU3mNbveDo"); ecdsa_get_address(pub_key, 48, address, sizeof(address)); ck_assert_str_eq(address, "LLCopoSTnHtz4eWdQQhLAVgNgT1zTi4QBK"); + ecdsa_get_address_segwit_p2sh(pub_key, 5, address, sizeof(address)); ck_assert_str_eq(address, "3DBU4tJ9tkMR9fnmCtjW48kjvseoNLQZXd"); memcpy(pub_key, fromhex("0496e8f2093f018aff6c2e2da5201ee528e2c8accbf9cac51563d33a7bb74a016054201c025e2a5d96b1629b95194e806c63eb96facaedc733b1a4b70ab3b33e3a"), 65); ecdsa_get_address(pub_key, 0, address, sizeof(address)); ck_assert_str_eq(address, "194SZbL75xCCGBbKtMsyWLE5r9s2V6mhVM"); @@ -3133,7 +3137,6 @@ START_TEST(test_multibyte_address) ecdsa_get_address(pub_key, 0x12345678, address, sizeof(address)); ck_assert_str_eq(address, "BrsGxAHga3VbopvSnb3gmLvMBhJNCGuDxBZL44"); ecdsa_get_address(pub_key, 0xffffffff, address, sizeof(address)); ck_assert_str_eq(address, "3diW7paWGJyZRLGqMJZ55DMfPExob8QxQHkrfYT"); - res = ecdsa_address_decode("1C7zdTfnkzmr13HfA2vNm5SJYRK6nEKyq8", 0, decode); ck_assert_int_eq(res, 1); ck_assert_mem_eq(decode, fromhex("0079fbfc3f34e7745860d76137da68f362380c606c"), 21);