From fb0726566349105dde0be4183ea02dfb649cba68 Mon Sep 17 00:00:00 2001 From: GreenAddress Date: Tue, 30 Apr 2019 23:07:31 +0200 Subject: [PATCH] remove libbase58, use base58 from libwally (#2594) * remove libbase58, use base58 from libwally This removes libbase58 and uses libwally instead. It allocates and then frees some memory, we may want to add a function in wally that doesn't or override wally_operations to use tal. Signed-off-by: Lawrence Nahum lawrence@greenaddress.it --- .gitmodules | 3 --- bitcoin/base58.c | 50 ++++++++++++++++++++++------------------ bitcoin/base58.h | 17 -------------- channeld/test/Makefile | 2 +- cli/test/Makefile | 2 +- configure | 14 ----------- doc/HACKING.md | 1 - doc/INSTALL.md | 3 +-- external/.gitignore | 1 - external/Makefile | 24 +------------------ external/libbase58 | 1 - lightningd/test/Makefile | 2 +- tools/repro-build.sh | 4 +--- 13 files changed, 34 insertions(+), 90 deletions(-) delete mode 160000 external/libbase58 diff --git a/.gitmodules b/.gitmodules index cada63324..841626523 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,9 +1,6 @@ [submodule "daemon/jsmn"] path = external/jsmn url = https://github.com/zserge/jsmn -[submodule "bitcoin/libbase58"] - path = external/libbase58 - url = https://github.com/bitcoin/libbase58.git [submodule "libsodium"] path = external/libsodium url = https://github.com/jedisct1/libsodium.git diff --git a/bitcoin/base58.c b/bitcoin/base58.c index e99ecea8e..65281af02 100644 --- a/bitcoin/base58.c +++ b/bitcoin/base58.c @@ -12,26 +12,24 @@ #include #include #include -#include #include - -static bool my_sha256(void *digest, const void *data, size_t datasz) -{ - sha256(digest, data, datasz); - return true; -} +#include static char *to_base58(const tal_t *ctx, u8 version, const struct ripemd160 *rmd) { - char out[BASE58_ADDR_MAX_LEN + 1]; - size_t outlen = sizeof(out); + char *out; + size_t total_length = sizeof(*rmd) + 1; + u8 buf[total_length]; + buf[0] = version; + memcpy(buf + 1, rmd, sizeof(*rmd)); - b58_sha256_impl = my_sha256; - if (!b58check_enc(out, &outlen, version, rmd, sizeof(*rmd))) { + if (wally_base58_from_bytes((const unsigned char *) buf, total_length, BASE58_FLAG_CHECKSUM, &out) != WALLY_OK) { return NULL; }else{ - return tal_strdup(ctx, out); + char *res = tal_strdup(ctx, out); + wally_free_string(out); + return res; } } @@ -53,16 +51,20 @@ static bool from_base58(u8 *version, { u8 buf[1 + sizeof(*rmd) + 4]; /* Avoid memcheck complaining if decoding resulted in a short value */ - memset(buf, 0, sizeof(buf)); - b58_sha256_impl = my_sha256; - size_t buflen = sizeof(buf); - b58tobin(buf, &buflen, base58, base58_len); - - int r = b58check(buf, buflen, base58, base58_len); + memset(buf, 0, buflen); + char *terminated_base58 = tal_dup_arr(NULL, char, base58, base58_len, 1); + terminated_base58[base58_len] = '\0'; + + size_t written = 0; + int r = wally_base58_to_bytes(terminated_base58, BASE58_FLAG_CHECKSUM, buf, buflen, &written); + tal_free(terminated_base58); + if (r != WALLY_OK || written > buflen) { + return false; + } *version = buf[0]; memcpy(rmd, buf + 1, sizeof(*rmd)); - return r >= 0; + return true; } bool bitcoin_from_base58(bool *test_net, @@ -106,12 +108,16 @@ bool key_from_base58(const char *base58, size_t base58_len, { // 1 byte version, 32 byte private key, 1 byte compressed, 4 byte checksum u8 keybuf[1 + 32 + 1 + 4]; + char *terminated_base58 = tal_dup_arr(NULL, char, base58, base58_len, 1); + terminated_base58[base58_len] = '\0'; size_t keybuflen = sizeof(keybuf); - b58_sha256_impl = my_sha256; - b58tobin(keybuf, &keybuflen, base58, base58_len); - if (b58check(keybuf, sizeof(keybuf), base58, base58_len) < 0) + size_t written = 0; + int r = wally_base58_to_bytes(terminated_base58, BASE58_FLAG_CHECKSUM, keybuf, keybuflen, &written); + wally_bzero(terminated_base58, base58_len + 1); + tal_free(terminated_base58); + if (r != WALLY_OK || written > keybuflen) return false; /* Byte after key should be 1 to represent a compressed key. */ diff --git a/bitcoin/base58.h b/bitcoin/base58.h index 514ea5106..cff8f85e9 100644 --- a/bitcoin/base58.h +++ b/bitcoin/base58.h @@ -5,7 +5,6 @@ #include #include #include -#include #include #include @@ -13,17 +12,6 @@ struct pubkey; struct privkey; struct bitcoin_address; -/* Encoding is version byte + ripemd160 + 4-byte checksum == 200 bits => 2^200. - * - * Now, 58^34 < 2^200, but 58^35 > 2^200. So 35 digits is sufficient, - * plus 1 terminator. - */ -#define BASE58_ADDR_MAX_LEN 36 - -/* For encoding private keys, it's 302 bits. - * 58^51 < 2^302, but 58^52 > 2^302. So 52 digits, plus one terminator. */ -#define BASE58_KEY_MAX_LEN 53 - /* Bitcoin address encoded in base58, with version and checksum */ char *bitcoin_to_base58(const tal_t *ctx, bool test_net, const struct bitcoin_address *addr); @@ -38,12 +26,7 @@ bool p2sh_from_base58(bool *test_net, struct ripemd160 *p2sh, const char *base58, size_t len); -char *base58_with_check(char dest[BASE58_ADDR_MAX_LEN], - u8 buf[1 + sizeof(struct ripemd160) + 4]); - bool key_from_base58(const char *base58, size_t base58_len, bool *test_net, struct privkey *priv, struct pubkey *key); -void base58_get_checksum(u8 csum[4], const u8 buf[], size_t buflen); - #endif /* LIGHTNING_BITCOIN_BASE58_H */ diff --git a/channeld/test/Makefile b/channeld/test/Makefile index 623aa6403..55403a1fc 100644 --- a/channeld/test/Makefile +++ b/channeld/test/Makefile @@ -22,7 +22,7 @@ CHANNELD_TEST_COMMON_OBJS := \ update-mocks: $(CHANNELD_TEST_SRC:%=update-mocks/%) -$(CHANNELD_TEST_PROGRAMS): $(CCAN_OBJS) $(BITCOIN_OBJS) $(WIRE_OBJS) $(LIBBASE58_OBJS) $(CHANNELD_TEST_COMMON_OBJS) +$(CHANNELD_TEST_PROGRAMS): $(CCAN_OBJS) $(BITCOIN_OBJS) $(WIRE_OBJS) $(CHANNELD_TEST_COMMON_OBJS) $(CHANNELD_TEST_OBJS): $(LIGHTNING_CHANNELD_HEADERS) $(LIGHTNING_CHANNELD_SRC) diff --git a/cli/test/Makefile b/cli/test/Makefile index abae776a8..6758a6992 100644 --- a/cli/test/Makefile +++ b/cli/test/Makefile @@ -22,7 +22,7 @@ CLI_TEST_COMMON_OBJS := \ update-mocks: $(CLI_TEST_SRC:%=update-mocks/%) -$(CLI_TEST_PROGRAMS): $(CCAN_OBJS) $(BITCOIN_OBJS) $(WIRE_OBJS) $(LIBBASE58_OBJS) $(CLI_TEST_COMMON_OBJS) +$(CLI_TEST_PROGRAMS): $(CCAN_OBJS) $(BITCOIN_OBJS) $(WIRE_OBJS) $(CLI_TEST_COMMON_OBJS) $(CLI_TEST_OBJS): $(LIGHTNING_CLI_HEADERS) $(LIGHTNING_CLI_SRC) diff --git a/configure b/configure index 6de24775f..b220cde05 100755 --- a/configure +++ b/configure @@ -172,20 +172,6 @@ int main(void) return 0; } /*END*/ -var=HAVE_SYSTEM_LIBBASE58 -desc=libbase58 -style=DEFINES_EVERYTHING|EXECUTE|MAY_NOT_COMPILE -link=-lbase58 -code= -#include -#include - -int main(void) -{ - printf("%p\n", b58check); - return 0; -} -/*END*/ var=HAVE_SQLITE3_EXPANDED_SQL desc=sqlite3_expanded_sql style=DEFINES_EVERYTHING|EXECUTE|MAY_NOT_COMPILE diff --git a/doc/HACKING.md b/doc/HACKING.md index 2a88e1c53..7aa7ec1d4 100644 --- a/doc/HACKING.md +++ b/doc/HACKING.md @@ -40,7 +40,6 @@ Here's a list of parts, with notes: - libwally-core - bitcoin helper library - secp256k1 - bitcoin curve encryption library within libwally-core - jsmn - tiny JSON parsing helper - - libbase58 - base58 address encoding/decoding library. * tools/ - tools for building - check-bolt.c: check the source code contains correct BOLT quotes diff --git a/doc/INSTALL.md b/doc/INSTALL.md index fb7d8fea2..e590b06d6 100644 --- a/doc/INSTALL.md +++ b/doc/INSTALL.md @@ -38,8 +38,7 @@ Get dependencies: sudo apt-get update sudo apt-get install -y \ autoconf automake build-essential git libtool libgmp-dev \ - libsqlite3-dev python python3 net-tools zlib1g-dev libsodium-dev \ - libbase58-dev + libsqlite3-dev python python3 net-tools zlib1g-dev libsodium-dev If you don't have Bitcoin installed locally you'll need to install that as well: diff --git a/external/.gitignore b/external/.gitignore index fbd137f4b..5029d6105 100644 --- a/external/.gitignore +++ b/external/.gitignore @@ -1,4 +1,3 @@ -libbase58.a libjsmn.a libsecp256k1.a libsecp256k1.la diff --git a/external/Makefile b/external/Makefile index 816b95fb1..fc0088ba7 100644 --- a/external/Makefile +++ b/external/Makefile @@ -2,7 +2,6 @@ SUBMODULES = \ external/libsodium \ external/libwally-core \ external/jsmn \ - external/libbase58 \ external/libbacktrace LIBSODIUM_HEADERS := external/libsodium/src/libsodium/include/sodium.h @@ -12,9 +11,8 @@ LIBWALLY_HEADERS := external/libwally-core/include/wally_bip32.h \ LIBSECP_HEADERS := external/libwally-core/src/secp256k1/include/secp256k1_ecdh.h \ external/libwally-core/src/secp256k1/include/secp256k1.h JSMN_HEADERS := external/jsmn/jsmn.h -LIBBASE58_HEADERS := external/libbase58/libbase58.h -EXTERNAL_HEADERS := $(LIBSODIUM_HEADERS) $(LIBWALLY_HEADERS) $(LIBSECP_HEADERS) $(JSMN_HEADERS) $(LIBBASE58_HEADERS) +EXTERNAL_HEADERS := $(LIBSODIUM_HEADERS) $(LIBWALLY_HEADERS) $(LIBSECP_HEADERS) $(JSMN_HEADERS) EXTERNAL_LIBS := external/libwallycore.a external/libsecp256k1.a external/libjsmn.a external/libbacktrace.a EXTERNAL_INCLUDE_FLAGS := \ @@ -31,13 +29,6 @@ else LDLIBS += -lsodium endif -ifneq ($(HAVE_SYSTEM_LIBBASE58),1) -EXTERNAL_INCLUDE_FLAGS += -I external/libbase58/ -EXTERNAL_LIBS += external/libbase58.a -else -LDLIBS += -lbase58 -endif - EXTERNAL_LDLIBS := -Lexternal $(patsubst lib%.a,-l%,$(notdir $(EXTERNAL_LIBS))) submodcheck: FORCE @@ -75,19 +66,6 @@ external/jsmn.o: external/jsmn/jsmn.c Makefile external/libjsmn.a: external/jsmn.o $(AR) rc $@ $< -LIBBASE58_SRC := external/libbase58/base58.c - -$(LIBBASE58_SRC): $(LIBBASE58_HEADERS) - -$(LIBBASE58_HEADERS): submodcheck - -# Can't be inside submodule, as that makes git think it's dirty. -external/base58.o: $(LIBBASE58_SRC) Makefile - $(COMPILE.c) $(OUTPUT_OPTION) $< - -external/libbase58.a: external/base58.o - $(AR) rc $@ $< - external/libbacktrace/backtrace.h: submodcheck # Need separate build dir: changes inside submodule make git think it's dirty. diff --git a/external/libbase58 b/external/libbase58 deleted file mode 160000 index 16c252760..000000000 --- a/external/libbase58 +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 16c2527608053d2cc2fa05b2e3b5ae96065d1410 diff --git a/lightningd/test/Makefile b/lightningd/test/Makefile index 17e2fc539..c64f70e8b 100644 --- a/lightningd/test/Makefile +++ b/lightningd/test/Makefile @@ -26,7 +26,7 @@ LIGHTNINGD_TEST_COMMON_OBJS := \ update-mocks: $(LIGHTNINGD_TEST_SRC:%=update-mocks/%) -$(LIGHTNINGD_TEST_PROGRAMS): $(CCAN_OBJS) $(BITCOIN_OBJS) $(WIRE_OBJS) $(LIBBASE58_OBJS) $(LIGHTNINGD_TEST_COMMON_OBJS) +$(LIGHTNINGD_TEST_PROGRAMS): $(CCAN_OBJS) $(BITCOIN_OBJS) $(WIRE_OBJS) $(LIGHTNINGD_TEST_COMMON_OBJS) $(LIGHTNINGD_TEST_OBJS): $(LIGHTNINGD_HEADERS) $(LIGHTNINGD_SRC) diff --git a/tools/repro-build.sh b/tools/repro-build.sh index af503a9b2..f7fadf7a4 100755 --- a/tools/repro-build.sh +++ b/tools/repro-build.sh @@ -70,7 +70,7 @@ case "$PLATFORM" in exit 1 fi DOWNLOAD='sudo apt --no-install-recommends --reinstall -d install' - PKGS='autoconf automake libtool make gcc libgmp-dev libsqlite3-dev zlib1g-dev libsodium-dev libbase58-dev' + PKGS='autoconf automake libtool make gcc libgmp-dev libsqlite3-dev zlib1g-dev libsodium-dev' INST='sudo dpkg -i' cat > /tmp/SHASUMS <