Skip to content

Commit aa9284e

Browse files
committed
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 <decker.christian@gmail.com>
1 parent 5d185f4 commit aa9284e

File tree

3 files changed

+24
-52
lines changed

3 files changed

+24
-52
lines changed

bitcoin/base58.c

Lines changed: 6 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -67,40 +67,18 @@ static bool from_base58(u8 *version,
6767
return true;
6868
}
6969

70-
bool bitcoin_from_base58(bool *test_net,
71-
struct bitcoin_address *addr,
70+
bool bitcoin_from_base58(u8 *version, struct bitcoin_address *addr,
7271
const char *base58, size_t len)
7372
{
74-
u8 version;
75-
76-
if (!from_base58(&version, &addr->addr, base58, len))
77-
return false;
78-
79-
if (version == 111)
80-
*test_net = true;
81-
else if (version == 0)
82-
*test_net = false;
83-
else
84-
return false;
85-
return true;
73+
return from_base58(version, &addr->addr, base58, len);
8674
}
8775

88-
bool p2sh_from_base58(bool *test_net,
89-
struct ripemd160 *p2sh,
90-
const char *base58, size_t len)
91-
{
92-
u8 version;
9376

94-
if (!from_base58(&version, p2sh, base58, len))
95-
return false;
77+
bool p2sh_from_base58(u8 *version, struct ripemd160 *p2sh, const char *base58,
78+
size_t len)
79+
{
9680

97-
if (version == 196)
98-
*test_net = true;
99-
else if (version == 5)
100-
*test_net = false;
101-
else
102-
return false;
103-
return true;
81+
return from_base58(version, p2sh, base58, len);
10482
}
10583

10684
bool ripemd160_from_base58(u8 *version, struct ripemd160 *rmd,

bitcoin/base58.h

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,14 @@ struct bitcoin_address;
1515
/* Bitcoin address encoded in base58, with version and checksum */
1616
char *bitcoin_to_base58(const tal_t *ctx, bool test_net,
1717
const struct bitcoin_address *addr);
18-
bool bitcoin_from_base58(bool *test_net,
19-
struct bitcoin_address *addr,
18+
bool bitcoin_from_base58(u8 *version, struct bitcoin_address *addr,
2019
const char *base58, size_t len);
2120

2221
/* P2SH address encoded as base58, with version and checksum */
2322
char *p2sh_to_base58(const tal_t *ctx, bool test_net,
2423
const struct ripemd160 *p2sh);
25-
bool p2sh_from_base58(bool *test_net,
26-
struct ripemd160 *p2sh,
27-
const char *base58, size_t len);
24+
bool p2sh_from_base58(u8 *version, struct ripemd160 *p2sh, const char *base58,
25+
size_t len);
2826

2927
bool key_from_base58(const char *base58, size_t base58_len,
3028
bool *test_net, struct privkey *priv, struct pubkey *key);

lightningd/jsonrpc.c

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -957,8 +957,7 @@ json_tok_address_scriptpubkey(const tal_t *cxt,
957957
const char *buffer,
958958
const jsmntok_t *tok, const u8 **scriptpubkey)
959959
{
960-
struct bitcoin_address p2pkh_destination;
961-
struct ripemd160 p2sh_destination;
960+
struct bitcoin_address destination;
962961
int witness_version;
963962
/* segwit_addr_net_decode requires a buffer of size 40, and will
964963
* 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,
971970

972971
bool parsed;
973972
bool right_network;
974-
bool testnet;
975-
976-
parsed = false;
977-
if (bitcoin_from_base58(&testnet, &p2pkh_destination,
978-
buffer + tok->start, tok->end - tok->start)) {
979-
*scriptpubkey = scriptpubkey_p2pkh(cxt, &p2pkh_destination);
980-
parsed = true;
981-
right_network = (testnet == chainparams->testnet);
982-
} else if (p2sh_from_base58(&testnet, &p2sh_destination,
983-
buffer + tok->start, tok->end - tok->start)) {
984-
*scriptpubkey = scriptpubkey_p2sh_hash(cxt, &p2sh_destination);
985-
parsed = true;
986-
right_network = (testnet == chainparams->testnet);
987-
}
988-
/* Insert other parsers that accept pointer+len here. */
973+
u8 addr_version;
974+
975+
parsed =
976+
ripemd160_from_base58(&addr_version, &destination.addr,
977+
buffer + tok->start, tok->end - tok->start);
989978

990979
if (parsed) {
991-
if (right_network)
980+
if (addr_version == chainparams->p2pkh_version) {
981+
*scriptpubkey = scriptpubkey_p2pkh(cxt, &destination);
992982
return ADDRESS_PARSE_SUCCESS;
993-
else
983+
} else if (addr_version == chainparams->p2sh_version) {
984+
*scriptpubkey =
985+
scriptpubkey_p2sh_hash(cxt, &destination.addr);
986+
return ADDRESS_PARSE_SUCCESS;
987+
} else {
994988
return ADDRESS_PARSE_WRONG_NETWORK;
989+
}
990+
/* Insert other parsers that accept pointer+len here. */
995991
}
996992

997993
/* Generate null-terminated address. */

0 commit comments

Comments
 (0)