mirror of
https://github.com/ElementsProject/lightning.git
synced 2025-02-22 14:42:40 +01:00
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>
This commit is contained in:
parent
5d185f43a8
commit
aa9284eaa3
3 changed files with 23 additions and 51 deletions
|
@ -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,
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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,28 +970,25 @@ 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. */
|
||||
addrz = tal_dup_arr(cxt, char, buffer + tok->start, tok->end - tok->start, 1);
|
||||
|
|
Loading…
Add table
Reference in a new issue