diff --git a/ecdsa.c b/ecdsa.c index 1f0e836c95..bbf361053e 100644 --- a/ecdsa.c +++ b/ecdsa.c @@ -930,10 +930,18 @@ void ecdsa_get_wif(const uint8_t *priv_key, uint32_t version, char *wif, int wif MEMSET_BZERO(wif_raw, sizeof(wif_raw)); } -int ecdsa_address_decode(const char *addr, uint8_t *out) +int ecdsa_address_decode(const char *addr, uint32_t version, uint8_t *out) { if (!addr) return 0; - return base58_decode_check(addr, out, 21) == 21; + if (version <= 0xFF) { + return base58_decode_check(addr, out, 21) == 21 && out[0] == (version & 0xFF); + } else if (version <= 0xFFFF) { + return base58_decode_check(addr, out, 22) == 22 && out[0] == ((version >> 8)) && out[1] == (version & 0xFF); + } else if (version <= 0xFFFF) { + return base58_decode_check(addr, out, 23) == 23 && out[0] == ((version >> 16)) && out[1] == ((version >> 8) & 0xFF) && out[2] == (version & 0xFF); + } else { + return base58_decode_check(addr, out, 24) == 24 && out[0] == (version >> 24) && out[1] == ((version >> 16) & 0xFF) && out[2] == ((version >> 8) & 0xFF) && out[3] == (version & 0xFF); + } } void uncompress_coords(const ecdsa_curve *curve, uint8_t odd, const bignum256 *x, bignum256 *y) diff --git a/ecdsa.h b/ecdsa.h index 5b785617ae..f74bf2efcf 100644 --- a/ecdsa.h +++ b/ecdsa.h @@ -78,7 +78,7 @@ void ecdsa_get_address_raw(const uint8_t *pub_key, uint32_t version, uint8_t *ad void ecdsa_get_address(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, uint8_t *out); +int ecdsa_address_decode(const char *addr, uint32_t version, uint8_t *out); int ecdsa_read_pubkey(const ecdsa_curve *curve, const uint8_t *pub_key, curve_point *pub); int ecdsa_validate_pubkey(const ecdsa_curve *curve, const curve_point *pub); int ecdsa_verify(const ecdsa_curve *curve, const uint8_t *pub_key, const uint8_t *sig, const uint8_t *msg, uint32_t msg_len); diff --git a/tests.c b/tests.c index 964e6e11a3..343d3fae1d 100644 --- a/tests.c +++ b/tests.c @@ -2152,44 +2152,48 @@ START_TEST(test_address_decode) int res; uint8_t decode[MAX_ADDR_RAW_SIZE]; - res = ecdsa_address_decode("1JwSSubhmg6iPtRjtyqhUYYH7bZg3Lfy1T", decode); + res = ecdsa_address_decode("1JwSSubhmg6iPtRjtyqhUYYH7bZg3Lfy1T", 0, decode); ck_assert_int_eq(res, 1); ck_assert_mem_eq(decode, fromhex("00c4c5d791fcb4654a1ef5e03fe0ad3d9c598f9827"), 21); - res = ecdsa_address_decode("myTPjxggahXyAzuMcYp5JTkbybANyLsYBW", decode); + res = ecdsa_address_decode("myTPjxggahXyAzuMcYp5JTkbybANyLsYBW", 111, decode); ck_assert_int_eq(res, 1); ck_assert_mem_eq(decode, fromhex("6fc4c5d791fcb4654a1ef5e03fe0ad3d9c598f9827"), 21); - res = ecdsa_address_decode("NEWoeZ6gh4CGvRgFAoAGh4hBqpxizGT6gZ", decode); + res = ecdsa_address_decode("NEWoeZ6gh4CGvRgFAoAGh4hBqpxizGT6gZ", 52, decode); ck_assert_int_eq(res, 1); ck_assert_mem_eq(decode, fromhex("34c4c5d791fcb4654a1ef5e03fe0ad3d9c598f9827"), 21); - res = ecdsa_address_decode("LdAPi7uXrLLmeh7u57pzkZc3KovxEDYRJq", decode); + res = ecdsa_address_decode("LdAPi7uXrLLmeh7u57pzkZc3KovxEDYRJq", 48, decode); ck_assert_int_eq(res, 1); ck_assert_mem_eq(decode, fromhex("30c4c5d791fcb4654a1ef5e03fe0ad3d9c598f9827"), 21); - res = ecdsa_address_decode("1C7zdTfnkzmr13HfA2vNm5SJYRK6nEKyq8", decode); + res = ecdsa_address_decode("1C7zdTfnkzmr13HfA2vNm5SJYRK6nEKyq8", 0, decode); ck_assert_int_eq(res, 1); ck_assert_mem_eq(decode, fromhex("0079fbfc3f34e7745860d76137da68f362380c606c"), 21); - res = ecdsa_address_decode("mrdwvWkma2D6n9mGsbtkazedQQuoksnqJV", decode); + res = ecdsa_address_decode("mrdwvWkma2D6n9mGsbtkazedQQuoksnqJV", 111, decode); ck_assert_int_eq(res, 1); ck_assert_mem_eq(decode, fromhex("6f79fbfc3f34e7745860d76137da68f362380c606c"), 21); - res = ecdsa_address_decode("N7hMq7AmgNsQXaYARrEwybbDGei9mcPNqr", decode); + res = ecdsa_address_decode("N7hMq7AmgNsQXaYARrEwybbDGei9mcPNqr", 52, decode); ck_assert_int_eq(res, 1); ck_assert_mem_eq(decode, fromhex("3479fbfc3f34e7745860d76137da68f362380c606c"), 21); - res = ecdsa_address_decode("LWLwtfycqf1uFqypLAug36W4kdgNwrZdNs", decode); + res = ecdsa_address_decode("LWLwtfycqf1uFqypLAug36W4kdgNwrZdNs", 48, decode); ck_assert_int_eq(res, 1); ck_assert_mem_eq(decode, fromhex("3079fbfc3f34e7745860d76137da68f362380c606c"), 21); // invalid char - res = ecdsa_address_decode("1JwSSubhmg6i000jtyqhUYYH7bZg3Lfy1T", decode); + res = ecdsa_address_decode("1JwSSubhmg6i000jtyqhUYYH7bZg3Lfy1T", 0, decode); ck_assert_int_eq(res, 0); // invalid address - res = ecdsa_address_decode("1111Subhmg6iPtRjtyqhUYYH7bZg3Lfy1T", decode); + res = ecdsa_address_decode("1111Subhmg6iPtRjtyqhUYYH7bZg3Lfy1T", 0, decode); + ck_assert_int_eq(res, 0); + + // invalid version + res = ecdsa_address_decode("LWLwtfycqf1uFqypLAug36W4kdgNwrZdNs", 0, decode); ck_assert_int_eq(res, 0); } END_TEST diff --git a/tools/bip39bruteforce.c b/tools/bip39bruteforce.c index f27007b786..7bb2a33f3c 100644 --- a/tools/bip39bruteforce.c +++ b/tools/bip39bruteforce.c @@ -44,7 +44,7 @@ int main(int argc, char **argv) fprintf(stderr, "\"%s\" is not a valid mnemonic\n", mnemonic); return 2; } - if (!ecdsa_address_decode(address, addr)) { + if (!ecdsa_address_decode(address, 0, addr)) { fprintf(stderr, "\"%s\" is not a valid address\n", address); return 3; }