Browse Source

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>
htlc_accepted_hook
Christian Decker 6 years ago
parent
commit
aa9284eaa3
  1. 34
      bitcoin/base58.c
  2. 8
      bitcoin/base58.h
  3. 34
      lightningd/jsonrpc.c

34
bitcoin/base58.c

@ -67,40 +67,18 @@ static bool from_base58(u8 *version,
return true; return true;
} }
bool bitcoin_from_base58(bool *test_net, bool bitcoin_from_base58(u8 *version, struct bitcoin_address *addr,
struct bitcoin_address *addr,
const char *base58, size_t len) const char *base58, size_t len)
{ {
u8 version; return from_base58(version, &addr->addr, base58, len);
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;
} }
bool p2sh_from_base58(bool *test_net,
struct ripemd160 *p2sh,
const char *base58, size_t len)
{
u8 version;
if (!from_base58(&version, p2sh, base58, len)) bool p2sh_from_base58(u8 *version, struct ripemd160 *p2sh, const char *base58,
return false; size_t len)
{
if (version == 196) return from_base58(version, p2sh, base58, len);
*test_net = true;
else if (version == 5)
*test_net = false;
else
return false;
return true;
} }
bool ripemd160_from_base58(u8 *version, struct ripemd160 *rmd, bool ripemd160_from_base58(u8 *version, struct ripemd160 *rmd,

8
bitcoin/base58.h

@ -15,16 +15,14 @@ struct bitcoin_address;
/* Bitcoin address encoded in base58, with version and checksum */ /* Bitcoin address encoded in base58, with version and checksum */
char *bitcoin_to_base58(const tal_t *ctx, bool test_net, char *bitcoin_to_base58(const tal_t *ctx, bool test_net,
const struct bitcoin_address *addr); const struct bitcoin_address *addr);
bool bitcoin_from_base58(bool *test_net, bool bitcoin_from_base58(u8 *version, struct bitcoin_address *addr,
struct bitcoin_address *addr,
const char *base58, size_t len); const char *base58, size_t len);
/* P2SH address encoded as base58, with version and checksum */ /* P2SH address encoded as base58, with version and checksum */
char *p2sh_to_base58(const tal_t *ctx, bool test_net, char *p2sh_to_base58(const tal_t *ctx, bool test_net,
const struct ripemd160 *p2sh); const struct ripemd160 *p2sh);
bool p2sh_from_base58(bool *test_net, bool p2sh_from_base58(u8 *version, struct ripemd160 *p2sh, const char *base58,
struct ripemd160 *p2sh, size_t len);
const char *base58, size_t len);
bool key_from_base58(const char *base58, size_t base58_len, bool key_from_base58(const char *base58, size_t base58_len,
bool *test_net, struct privkey *priv, struct pubkey *key); bool *test_net, struct privkey *priv, struct pubkey *key);

34
lightningd/jsonrpc.c

@ -957,8 +957,7 @@ json_tok_address_scriptpubkey(const tal_t *cxt,
const char *buffer, const char *buffer,
const jsmntok_t *tok, const u8 **scriptpubkey) const jsmntok_t *tok, const u8 **scriptpubkey)
{ {
struct bitcoin_address p2pkh_destination; struct bitcoin_address destination;
struct ripemd160 p2sh_destination;
int witness_version; int witness_version;
/* segwit_addr_net_decode requires a buffer of size 40, and will /* 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 * 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 parsed;
bool right_network; bool right_network;
bool testnet; u8 addr_version;
parsed = false; parsed =
if (bitcoin_from_base58(&testnet, &p2pkh_destination, ripemd160_from_base58(&addr_version, &destination.addr,
buffer + tok->start, tok->end - tok->start)) { 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. */
if (parsed) { if (parsed) {
if (right_network) if (addr_version == chainparams->p2pkh_version) {
*scriptpubkey = scriptpubkey_p2pkh(cxt, &destination);
return ADDRESS_PARSE_SUCCESS; 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; return ADDRESS_PARSE_WRONG_NETWORK;
}
/* Insert other parsers that accept pointer+len here. */
} }
/* Generate null-terminated address. */ /* Generate null-terminated address. */

Loading…
Cancel
Save