From aa9284eaa302b56ebc0001b6733d1dc86555a315 Mon Sep 17 00:00:00 2001 From: Christian Decker Date: Tue, 30 Apr 2019 22:10:50 +0200 Subject: [PATCH] base58: Simplified the address parsing We were deciding whether an address is a testnet address or not in the parser, and then checking whether it matches our expectation outside as well. This just returns the address version instead, and still checks it against our expectation, but without having the parser need to know about address types. Signed-off-by: Christian Decker --- bitcoin/base58.c | 34 ++++++---------------------------- bitcoin/base58.h | 8 +++----- lightningd/jsonrpc.c | 32 ++++++++++++++------------------ 3 files changed, 23 insertions(+), 51 deletions(-) diff --git a/bitcoin/base58.c b/bitcoin/base58.c index 056739871..fe1226867 100644 --- a/bitcoin/base58.c +++ b/bitcoin/base58.c @@ -67,40 +67,18 @@ static bool from_base58(u8 *version, return true; } -bool bitcoin_from_base58(bool *test_net, - struct bitcoin_address *addr, +bool bitcoin_from_base58(u8 *version, struct bitcoin_address *addr, const char *base58, size_t len) { - u8 version; - - if (!from_base58(&version, &addr->addr, base58, len)) - return false; - - if (version == 111) - *test_net = true; - else if (version == 0) - *test_net = false; - else - return false; - return true; + return from_base58(version, &addr->addr, base58, len); } -bool p2sh_from_base58(bool *test_net, - struct ripemd160 *p2sh, - const char *base58, size_t len) + +bool p2sh_from_base58(u8 *version, struct ripemd160 *p2sh, const char *base58, + size_t len) { - u8 version; - if (!from_base58(&version, p2sh, base58, len)) - return false; - - if (version == 196) - *test_net = true; - else if (version == 5) - *test_net = false; - else - return false; - return true; + return from_base58(version, p2sh, base58, len); } bool ripemd160_from_base58(u8 *version, struct ripemd160 *rmd, diff --git a/bitcoin/base58.h b/bitcoin/base58.h index 939eac27b..4154b6a3d 100644 --- a/bitcoin/base58.h +++ b/bitcoin/base58.h @@ -15,16 +15,14 @@ struct bitcoin_address; /* 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); -bool bitcoin_from_base58(bool *test_net, - struct bitcoin_address *addr, +bool bitcoin_from_base58(u8 *version, struct bitcoin_address *addr, const char *base58, size_t len); /* P2SH address encoded as base58, with version and checksum */ char *p2sh_to_base58(const tal_t *ctx, bool test_net, const struct ripemd160 *p2sh); -bool p2sh_from_base58(bool *test_net, - struct ripemd160 *p2sh, - const char *base58, size_t len); +bool p2sh_from_base58(u8 *version, struct ripemd160 *p2sh, const char *base58, + size_t len); bool key_from_base58(const char *base58, size_t base58_len, bool *test_net, struct privkey *priv, struct pubkey *key); diff --git a/lightningd/jsonrpc.c b/lightningd/jsonrpc.c index 7e9af5c8d..283fed0ca 100644 --- a/lightningd/jsonrpc.c +++ b/lightningd/jsonrpc.c @@ -957,8 +957,7 @@ json_tok_address_scriptpubkey(const tal_t *cxt, const char *buffer, const jsmntok_t *tok, const u8 **scriptpubkey) { - struct bitcoin_address p2pkh_destination; - struct ripemd160 p2sh_destination; + struct bitcoin_address destination; int witness_version; /* segwit_addr_net_decode requires a buffer of size 40, and will * not write to the buffer if the address is too long, so a buffer @@ -971,27 +970,24 @@ json_tok_address_scriptpubkey(const tal_t *cxt, bool parsed; bool right_network; - bool testnet; + u8 addr_version; - parsed = false; - if (bitcoin_from_base58(&testnet, &p2pkh_destination, - buffer + tok->start, tok->end - tok->start)) { - *scriptpubkey = scriptpubkey_p2pkh(cxt, &p2pkh_destination); - parsed = true; - right_network = (testnet == chainparams->testnet); - } else if (p2sh_from_base58(&testnet, &p2sh_destination, - buffer + tok->start, tok->end - tok->start)) { - *scriptpubkey = scriptpubkey_p2sh_hash(cxt, &p2sh_destination); - parsed = true; - right_network = (testnet == chainparams->testnet); - } - /* Insert other parsers that accept pointer+len here. */ + parsed = + ripemd160_from_base58(&addr_version, &destination.addr, + buffer + tok->start, tok->end - tok->start); if (parsed) { - if (right_network) + if (addr_version == chainparams->p2pkh_version) { + *scriptpubkey = scriptpubkey_p2pkh(cxt, &destination); return ADDRESS_PARSE_SUCCESS; - else + } else if (addr_version == chainparams->p2sh_version) { + *scriptpubkey = + scriptpubkey_p2sh_hash(cxt, &destination.addr); + return ADDRESS_PARSE_SUCCESS; + } else { return ADDRESS_PARSE_WRONG_NETWORK; + } + /* Insert other parsers that accept pointer+len here. */ } /* Generate null-terminated address. */