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
This commit is contained in:
GreenAddress 2019-04-30 23:07:31 +02:00 committed by Christian Decker
parent 890379d8f1
commit fb07265663
13 changed files with 33 additions and 89 deletions

3
.gitmodules vendored
View File

@ -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

View File

@ -12,26 +12,24 @@
#include <ccan/build_assert/build_assert.h>
#include <ccan/tal/str/str.h>
#include <common/utils.h>
#include <libbase58.h>
#include <string.h>
static bool my_sha256(void *digest, const void *data, size_t datasz)
{
sha256(digest, data, datasz);
return true;
}
#include <wally_core.h>
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);
memset(buf, 0, buflen);
char *terminated_base58 = tal_dup_arr(NULL, char, base58, base58_len, 1);
terminated_base58[base58_len] = '\0';
int r = b58check(buf, buflen, base58, base58_len);
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. */

View File

@ -5,7 +5,6 @@
#include <ccan/crypto/ripemd160/ripemd160.h>
#include <ccan/short_types/short_types.h>
#include <ccan/tal/tal.h>
#include <secp256k1.h>
#include <stdbool.h>
#include <stdlib.h>
@ -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 */

View File

@ -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)

View File

@ -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)

14
configure vendored
View File

@ -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 <libbase58.h>
#include <stdio.h>
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

View File

@ -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

View File

@ -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:

1
external/.gitignore vendored
View File

@ -1,4 +1,3 @@
libbase58.a
libjsmn.a
libsecp256k1.a
libsecp256k1.la

24
external/Makefile vendored
View File

@ -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.

1
external/libbase58 vendored

@ -1 +0,0 @@
Subproject commit 16c2527608053d2cc2fa05b2e3b5ae96065d1410

View File

@ -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)

View File

@ -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 <<EOF
a909ad8b2e97f45960a05458140cff737df30bf7c616778a5a0ca74b9d012d93 /var/cache/apt/archives/autoconf_2.69-11_all.deb
@ -80,8 +80,6 @@ abe8f767884414dde79c4c5c4b6b7447ce057a07277a6de24f1b96e7e2b5da5a /var/cache/apt
e8d83c288e08da39c5ccd289b550e2097f562bf848480f71f94cebbd187e60da /var/cache/apt/archives/gcc-7_7.3.0-16ubuntu3_amd64.deb
92f5f15faca8cee48608b58a0300c469c076dd1dd8946b93b8428abd404d54f9 /var/cache/apt/archives/libasan4_7.3.0-16ubuntu3_amd64.deb
fc386b12f324c34e405502767216daef22bf7d2f0e597b1c7ccea5cef1821bd3 /var/cache/apt/archives/libatomic1_8-20180414-1ubuntu2_amd64.deb
76e511f41c4aa7d7683a06d2a76c24df968d5c084a63c9ff27a544de899585a5 /var/cache/apt/archives/libbase58-0_0.1.4-1_amd64.deb
ce48723acc07e67db5da181cd0e4dfecfe50b2147f8de8bc1bab5f15833562cb /var/cache/apt/archives/libbase58-dev_0.1.4-1_amd64.deb
e426c70a940a7d0c5c95823a5fd01f26bd8bcb08d109df2f8c96c439da8dc440 /var/cache/apt/archives/libc6-dev_2.27-3ubuntu1_amd64.deb
69ea1317b37cbd467eb7d216f5d23aa8831d926908e9e12477aa28bdc1d5e62b /var/cache/apt/archives/libc-dev-bin_2.27-3ubuntu1_amd64.deb
357185ad09d689b61efda9576888feea2a0f178ae1422cddc6cd0d48f7c22d50 /var/cache/apt/archives/libcilkrts5_7.3.0-16ubuntu3_amd64.deb