mirror of
https://github.com/ElementsProject/lightning.git
synced 2025-03-03 10:46:58 +01:00
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>
This commit is contained in:
parent
ff5dfb1cc4
commit
0d19d04def
5 changed files with 12 additions and 11 deletions
|
@ -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,
|
||||||
|
|
|
@ -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);
|
||||||
|
|
|
@ -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)];
|
||||||
|
|
|
@ -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");
|
||||||
|
|
|
@ -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…
Add table
Reference in a new issue