2015-05-26 06:38:12 +02:00
|
|
|
/* Converted to C by Rusty Russell, based on bitcoin source: */
|
|
|
|
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
|
|
|
// Copyright (c) 2009-2012 The Bitcoin Developers
|
|
|
|
// Distributed under the MIT/X11 software license, see the accompanying
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
2021-12-04 12:23:56 +01:00
|
|
|
#include "config.h"
|
|
|
|
#include <bitcoin/address.h>
|
|
|
|
#include <bitcoin/base58.h>
|
|
|
|
#include <bitcoin/privkey.h>
|
|
|
|
#include <bitcoin/pubkey.h>
|
|
|
|
#include <bitcoin/shadouble.h>
|
2017-08-28 18:02:01 +02:00
|
|
|
#include <common/utils.h>
|
2019-04-30 23:07:31 +02:00
|
|
|
#include <wally_core.h>
|
2015-05-26 06:38:12 +02:00
|
|
|
|
2016-01-21 21:11:46 +01:00
|
|
|
static char *to_base58(const tal_t *ctx, u8 version,
|
|
|
|
const struct ripemd160 *rmd)
|
2015-05-26 06:38:12 +02:00
|
|
|
{
|
2019-04-30 23:07:31 +02:00
|
|
|
char *out;
|
|
|
|
size_t total_length = sizeof(*rmd) + 1;
|
|
|
|
u8 buf[total_length];
|
|
|
|
buf[0] = version;
|
|
|
|
memcpy(buf + 1, rmd, sizeof(*rmd));
|
2015-05-26 06:38:12 +02:00
|
|
|
|
2020-09-23 12:43:28 +02:00
|
|
|
tal_wally_start();
|
2020-09-23 12:36:46 +02:00
|
|
|
if (wally_base58_from_bytes((const unsigned char *) buf,
|
|
|
|
total_length, BASE58_FLAG_CHECKSUM, &out)
|
|
|
|
!= WALLY_OK)
|
|
|
|
out = NULL;
|
2020-09-23 12:43:28 +02:00
|
|
|
tal_wally_end(tal_steal(ctx, out));
|
2020-09-23 12:36:46 +02:00
|
|
|
|
2020-09-23 12:43:28 +02:00
|
|
|
return out;
|
2015-05-26 06:38:12 +02:00
|
|
|
}
|
|
|
|
|
2019-05-06 23:17:15 +02:00
|
|
|
char *bitcoin_to_base58(const tal_t *ctx, const struct chainparams *chainparams,
|
2016-01-21 21:11:46 +01:00
|
|
|
const struct bitcoin_address *addr)
|
2015-05-26 06:38:12 +02:00
|
|
|
{
|
2019-05-06 23:17:15 +02:00
|
|
|
return to_base58(ctx, chainparams->p2pkh_version, &addr->addr);
|
2016-01-21 21:11:46 +01:00
|
|
|
}
|
|
|
|
|
2019-05-06 23:17:15 +02:00
|
|
|
char *p2sh_to_base58(const tal_t *ctx, const struct chainparams *chainparams,
|
2016-01-21 21:11:46 +01:00
|
|
|
const struct ripemd160 *p2sh)
|
|
|
|
{
|
2019-05-06 23:17:15 +02:00
|
|
|
return to_base58(ctx, chainparams->p2sh_version, p2sh);
|
2016-01-21 21:11:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool from_base58(u8 *version,
|
|
|
|
struct ripemd160 *rmd,
|
|
|
|
const char *base58, size_t base58_len)
|
|
|
|
{
|
|
|
|
u8 buf[1 + sizeof(*rmd) + 4];
|
2017-06-14 11:11:17 +02:00
|
|
|
/* Avoid memcheck complaining if decoding resulted in a short value */
|
2016-07-29 11:36:18 +02:00
|
|
|
size_t buflen = sizeof(buf);
|
2019-04-30 23:07:31 +02:00
|
|
|
memset(buf, 0, buflen);
|
|
|
|
char *terminated_base58 = tal_dup_arr(NULL, char, base58, base58_len, 1);
|
|
|
|
terminated_base58[base58_len] = '\0';
|
|
|
|
|
|
|
|
size_t written = 0;
|
|
|
|
int r = wally_base58_to_bytes(terminated_base58, BASE58_FLAG_CHECKSUM, buf, buflen, &written);
|
|
|
|
tal_free(terminated_base58);
|
|
|
|
if (r != WALLY_OK || written > buflen) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-01-21 21:11:46 +01:00
|
|
|
*version = buf[0];
|
2016-07-29 11:36:18 +02:00
|
|
|
memcpy(rmd, buf + 1, sizeof(*rmd));
|
2019-04-30 23:07:31 +02:00
|
|
|
return true;
|
2016-01-21 21:11:46 +01:00
|
|
|
}
|
|
|
|
|
2019-04-30 20:43:18 +02:00
|
|
|
bool ripemd160_from_base58(u8 *version, struct ripemd160 *rmd,
|
|
|
|
const char *base58, size_t base58_len)
|
|
|
|
{
|
|
|
|
return from_base58(version, rmd, base58, base58_len);
|
|
|
|
}
|