Skip to content

Commit

Permalink
wallet: Pass chainparams to address serialization
Browse files Browse the repository at this point in the history
The chainparams are needed to know the prefixes, so instead of passing down
the testnet, we pass the entire params struct.

Signed-off-by: Christian Decker <[email protected]>
  • Loading branch information
cdecker authored and rustyrussell committed May 10, 2019
1 parent ff5dfb1 commit 0d19d04
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 11 deletions.
8 changes: 4 additions & 4 deletions bitcoin/base58.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,16 @@ static char *to_base58(const tal_t *ctx, u8 version,
}
}

char *bitcoin_to_base58(const tal_t *ctx, bool test_net,
char *bitcoin_to_base58(const tal_t *ctx, const struct chainparams *chainparams,
const struct bitcoin_address *addr)
{
return to_base58(ctx, test_net ? 111 : 0, &addr->addr);
return to_base58(ctx, chainparams->p2pkh_version, &addr->addr);
}

char *p2sh_to_base58(const tal_t *ctx, bool test_net,
char *p2sh_to_base58(const tal_t *ctx, const struct chainparams *chainparams,
const struct ripemd160 *p2sh)
{
return to_base58(ctx, test_net ? 196 : 5, p2sh);
return to_base58(ctx, chainparams->p2sh_version, p2sh);
}

static bool from_base58(u8 *version,
Expand Down
5 changes: 3 additions & 2 deletions bitcoin/base58.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define LIGHTNING_BITCOIN_BASE58_H
#include "config.h"

#include <bitcoin/chainparams.h>
#include <ccan/crypto/ripemd160/ripemd160.h>
#include <ccan/short_types/short_types.h>
#include <ccan/tal/tal.h>
Expand All @@ -13,13 +14,13 @@ struct privkey;
struct bitcoin_address;

/* 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, const struct chainparams *chainparams,
const 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,
char *p2sh_to_base58(const tal_t *ctx, const struct chainparams *chainparams,
const struct ripemd160 *p2sh);
bool p2sh_from_base58(u8 *version, struct ripemd160 *p2sh, const char *base58,
size_t len);
Expand Down
4 changes: 2 additions & 2 deletions devtools/bolt11-cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,12 @@ int main(int argc, char *argv[])
printf("fallback: %s\n", tal_hex(ctx, b11->fallbacks[i]));
if (is_p2pkh(b11->fallbacks[i], &pkh)) {
printf("fallback-P2PKH: %s\n",
bitcoin_to_base58(ctx, b11->chain->testnet,
bitcoin_to_base58(ctx, b11->chain,
&pkh));
} else if (is_p2sh(b11->fallbacks[i], &sh)) {
printf("fallback-P2SH: %s\n",
p2sh_to_base58(ctx,
b11->chain->testnet,
b11->chain,
&sh));
} else if (is_p2wpkh(b11->fallbacks[i], &pkh)) {
char out[73 + strlen(b11->chain->bip173_name)];
Expand Down
4 changes: 2 additions & 2 deletions lightningd/invoice.c
Original file line number Diff line number Diff line change
Expand Up @@ -1041,11 +1041,11 @@ static void json_add_fallback(struct json_stream *response,
if (is_p2pkh(fallback, &pkh)) {
json_add_string(response, "type", "P2PKH");
json_add_string(response, "addr",
bitcoin_to_base58(tmpctx, chain->testnet, &pkh));
bitcoin_to_base58(tmpctx, chain, &pkh));
} else if (is_p2sh(fallback, &sh)) {
json_add_string(response, "type", "P2SH");
json_add_string(response, "addr",
p2sh_to_base58(tmpctx, chain->testnet, &sh));
p2sh_to_base58(tmpctx, chain, &sh));
} else if (is_p2wpkh(fallback, &pkh)) {
char out[73 + strlen(chain->bip173_name)];
json_add_string(response, "type", "P2WPKH");
Expand Down
2 changes: 1 addition & 1 deletion wallet/walletrpc.c
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ encode_pubkey_to_addr(const tal_t *ctx,
sha256(&h, redeemscript, tal_count(redeemscript));
ripemd160(&h160, h.u.u8, sizeof(h));
out = p2sh_to_base58(ctx,
get_chainparams(ld)->testnet,
get_chainparams(ld),
&h160);
} else {
hrp = get_chainparams(ld)->bip173_name;
Expand Down

0 comments on commit 0d19d04

Please sign in to comment.