Browse Source

wallet: Pass chainparams to address serialization

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 <decker.christian@gmail.com>
htlc_accepted_hook
Christian Decker 6 years ago
committed by Rusty Russell
parent
commit
0d19d04def
  1. 8
      bitcoin/base58.c
  2. 5
      bitcoin/base58.h
  3. 4
      devtools/bolt11-cli.c
  4. 4
      lightningd/invoice.c
  5. 2
      wallet/walletrpc.c

8
bitcoin/base58.c

@ -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) 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) 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, static bool from_base58(u8 *version,

5
bitcoin/base58.h

@ -2,6 +2,7 @@
#define LIGHTNING_BITCOIN_BASE58_H #define LIGHTNING_BITCOIN_BASE58_H
#include "config.h" #include "config.h"
#include <bitcoin/chainparams.h>
#include <ccan/crypto/ripemd160/ripemd160.h> #include <ccan/crypto/ripemd160/ripemd160.h>
#include <ccan/short_types/short_types.h> #include <ccan/short_types/short_types.h>
#include <ccan/tal/tal.h> #include <ccan/tal/tal.h>
@ -13,13 +14,13 @@ struct privkey;
struct bitcoin_address; 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, const struct chainparams *chainparams,
const struct bitcoin_address *addr); const struct bitcoin_address *addr);
bool bitcoin_from_base58(u8 *version, struct bitcoin_address *addr, bool bitcoin_from_base58(u8 *version, 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, const struct chainparams *chainparams,
const struct ripemd160 *p2sh); const struct ripemd160 *p2sh);
bool p2sh_from_base58(u8 *version, struct ripemd160 *p2sh, const char *base58, bool p2sh_from_base58(u8 *version, struct ripemd160 *p2sh, const char *base58,
size_t len); size_t len);

4
devtools/bolt11-cli.c

@ -125,12 +125,12 @@ int main(int argc, char *argv[])
printf("fallback: %s\n", tal_hex(ctx, b11->fallbacks[i])); printf("fallback: %s\n", tal_hex(ctx, b11->fallbacks[i]));
if (is_p2pkh(b11->fallbacks[i], &pkh)) { if (is_p2pkh(b11->fallbacks[i], &pkh)) {
printf("fallback-P2PKH: %s\n", printf("fallback-P2PKH: %s\n",
bitcoin_to_base58(ctx, b11->chain->testnet, bitcoin_to_base58(ctx, b11->chain,
&pkh)); &pkh));
} else if (is_p2sh(b11->fallbacks[i], &sh)) { } else if (is_p2sh(b11->fallbacks[i], &sh)) {
printf("fallback-P2SH: %s\n", printf("fallback-P2SH: %s\n",
p2sh_to_base58(ctx, p2sh_to_base58(ctx,
b11->chain->testnet, b11->chain,
&sh)); &sh));
} else if (is_p2wpkh(b11->fallbacks[i], &pkh)) { } else if (is_p2wpkh(b11->fallbacks[i], &pkh)) {
char out[73 + strlen(b11->chain->bip173_name)]; char out[73 + strlen(b11->chain->bip173_name)];

4
lightningd/invoice.c

@ -1041,11 +1041,11 @@ static void json_add_fallback(struct json_stream *response,
if (is_p2pkh(fallback, &pkh)) { if (is_p2pkh(fallback, &pkh)) {
json_add_string(response, "type", "P2PKH"); json_add_string(response, "type", "P2PKH");
json_add_string(response, "addr", json_add_string(response, "addr",
bitcoin_to_base58(tmpctx, chain->testnet, &pkh)); bitcoin_to_base58(tmpctx, chain, &pkh));
} else if (is_p2sh(fallback, &sh)) { } else if (is_p2sh(fallback, &sh)) {
json_add_string(response, "type", "P2SH"); json_add_string(response, "type", "P2SH");
json_add_string(response, "addr", json_add_string(response, "addr",
p2sh_to_base58(tmpctx, chain->testnet, &sh)); p2sh_to_base58(tmpctx, chain, &sh));
} else if (is_p2wpkh(fallback, &pkh)) { } else if (is_p2wpkh(fallback, &pkh)) {
char out[73 + strlen(chain->bip173_name)]; char out[73 + strlen(chain->bip173_name)];
json_add_string(response, "type", "P2WPKH"); json_add_string(response, "type", "P2WPKH");

2
wallet/walletrpc.c

@ -218,7 +218,7 @@ encode_pubkey_to_addr(const tal_t *ctx,
sha256(&h, redeemscript, tal_count(redeemscript)); sha256(&h, redeemscript, tal_count(redeemscript));
ripemd160(&h160, h.u.u8, sizeof(h)); ripemd160(&h160, h.u.u8, sizeof(h));
out = p2sh_to_base58(ctx, out = p2sh_to_base58(ctx,
get_chainparams(ld)->testnet, get_chainparams(ld),
&h160); &h160);
} else { } else {
hrp = get_chainparams(ld)->bip173_name; hrp = get_chainparams(ld)->bip173_name;

Loading…
Cancel
Save