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.
|
2015-06-12 04:56:59 +02:00
|
|
|
#include "address.h"
|
|
|
|
#include "base58.h"
|
2016-12-20 14:10:25 +01:00
|
|
|
#include "libbase58/libbase58.h"
|
2015-06-26 04:24:07 +02:00
|
|
|
#include "privkey.h"
|
2015-06-12 04:56:59 +02:00
|
|
|
#include "pubkey.h"
|
|
|
|
#include "shadouble.h"
|
2016-12-02 08:42:58 +01:00
|
|
|
#include "utils.h"
|
2016-01-21 21:08:08 +01:00
|
|
|
#include <assert.h>
|
|
|
|
#include <ccan/build_assert/build_assert.h>
|
|
|
|
#include <ccan/tal/str/str.h>
|
|
|
|
#include <secp256k1.h>
|
|
|
|
#include <string.h>
|
2015-05-26 06:38:12 +02:00
|
|
|
|
2016-07-29 11:36:18 +02:00
|
|
|
static bool my_sha256(void *digest, const void *data, size_t datasz)
|
2015-05-26 06:38:12 +02:00
|
|
|
{
|
2016-07-29 11:36:18 +02:00
|
|
|
sha256(digest, data, datasz);
|
2015-05-26 06:38:12 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2016-07-29 11:36:18 +02:00
|
|
|
char out[BASE58_ADDR_MAX_LEN + 1];
|
|
|
|
size_t outlen = sizeof(out);
|
2015-05-26 06:38:12 +02:00
|
|
|
|
2016-07-29 11:36:18 +02:00
|
|
|
b58_sha256_impl = my_sha256;
|
|
|
|
if (!b58check_enc(out, &outlen, version, rmd, sizeof(*rmd))) {
|
|
|
|
return NULL;
|
|
|
|
}else{
|
|
|
|
return tal_strdup(ctx, out);
|
|
|
|
}
|
2015-05-26 06:38:12 +02:00
|
|
|
}
|
|
|
|
|
2016-01-21 21:11:46 +01:00
|
|
|
char *bitcoin_to_base58(const tal_t *ctx, bool test_net,
|
|
|
|
const struct bitcoin_address *addr)
|
2015-05-26 06:38:12 +02:00
|
|
|
{
|
2016-01-21 21:11:46 +01:00
|
|
|
return to_base58(ctx, test_net ? 111 : 0, &addr->addr);
|
|
|
|
}
|
|
|
|
|
|
|
|
char *p2sh_to_base58(const tal_t *ctx, bool test_net,
|
|
|
|
const struct ripemd160 *p2sh)
|
|
|
|
{
|
|
|
|
return to_base58(ctx, test_net ? 196 : 5, p2sh);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool from_base58(u8 *version,
|
|
|
|
struct ripemd160 *rmd,
|
|
|
|
const char *base58, size_t base58_len)
|
|
|
|
{
|
|
|
|
u8 buf[1 + sizeof(*rmd) + 4];
|
2015-05-26 06:38:12 +02:00
|
|
|
|
2016-07-29 11:36:18 +02:00
|
|
|
b58_sha256_impl = my_sha256;
|
2015-05-26 06:38:12 +02:00
|
|
|
|
2016-07-29 11:36:18 +02:00
|
|
|
size_t buflen = sizeof(buf);
|
|
|
|
b58tobin(buf, &buflen, base58, base58_len);
|
|
|
|
int r = b58check(buf, sizeof(buf), base58, base58_len);
|
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));
|
|
|
|
return r > 0;
|
2016-01-21 21:11:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool bitcoin_from_base58(bool *test_net,
|
|
|
|
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)
|
2015-05-26 06:38:12 +02:00
|
|
|
*test_net = true;
|
2016-01-21 21:11:46 +01:00
|
|
|
else if (version == 0)
|
2015-05-26 06:38:12 +02:00
|
|
|
*test_net = false;
|
|
|
|
else
|
|
|
|
return false;
|
2016-01-21 21:11:46 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool p2sh_from_base58(bool *test_net,
|
|
|
|
struct ripemd160 *p2sh,
|
|
|
|
const char *base58, size_t len)
|
|
|
|
{
|
|
|
|
u8 version;
|
2015-05-26 06:38:12 +02:00
|
|
|
|
2016-01-21 21:11:46 +01:00
|
|
|
if (!from_base58(&version, p2sh, base58, len))
|
2015-05-26 06:38:12 +02:00
|
|
|
return false;
|
|
|
|
|
2016-01-21 21:11:46 +01:00
|
|
|
if (version == 196)
|
|
|
|
*test_net = true;
|
|
|
|
else if (version == 5)
|
|
|
|
*test_net = false;
|
|
|
|
else
|
|
|
|
return false;
|
2015-05-26 06:38:12 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-07-09 08:13:36 +02:00
|
|
|
bool ripemd_from_base58(u8 *version,
|
|
|
|
struct ripemd160 *ripemd160,
|
2015-05-26 06:38:12 +02:00
|
|
|
const char *base58)
|
|
|
|
{
|
2016-07-29 11:36:18 +02:00
|
|
|
return from_base58(version, ripemd160, base58, strlen(base58));
|
2015-05-26 06:38:12 +02:00
|
|
|
}
|
|
|
|
|
2015-06-26 04:24:07 +02:00
|
|
|
char *key_to_base58(const tal_t *ctx, bool test_net, const struct privkey *key)
|
2015-05-26 06:38:12 +02:00
|
|
|
{
|
2016-07-29 11:36:18 +02:00
|
|
|
u8 buf[32 + 1];
|
|
|
|
char out[BASE58_KEY_MAX_LEN + 2];
|
|
|
|
u8 version = test_net ? 239 : 128;
|
|
|
|
size_t outlen = sizeof(out);
|
2015-05-26 06:38:12 +02:00
|
|
|
|
2016-07-29 11:36:18 +02:00
|
|
|
memcpy(buf, key->secret, sizeof(key->secret));
|
2015-05-26 06:38:12 +02:00
|
|
|
/* Mark this as a compressed key. */
|
2016-07-29 11:36:18 +02:00
|
|
|
buf[32] = 1;
|
2015-05-26 06:38:12 +02:00
|
|
|
|
2017-02-07 02:44:21 +01:00
|
|
|
b58_sha256_impl = my_sha256;
|
2016-07-29 11:36:18 +02:00
|
|
|
b58check_enc(out, &outlen, version, buf, sizeof(buf));
|
|
|
|
return tal_strdup(ctx, out);
|
2015-05-26 06:38:12 +02:00
|
|
|
}
|
|
|
|
|
2016-12-02 08:42:58 +01:00
|
|
|
bool key_from_base58(const char *base58, size_t base58_len,
|
2015-06-26 04:24:07 +02:00
|
|
|
bool *test_net, struct privkey *priv, struct pubkey *key)
|
2015-05-26 06:38:12 +02:00
|
|
|
{
|
2016-07-29 11:36:18 +02:00
|
|
|
// 1 byte version, 32 byte private key, 1 byte compressed, 4 byte checksum
|
2015-06-26 04:24:07 +02:00
|
|
|
u8 keybuf[1 + 32 + 1 + 4];
|
2016-07-29 11:36:18 +02:00
|
|
|
size_t keybuflen = sizeof(keybuf);
|
2015-05-26 06:38:12 +02:00
|
|
|
|
2017-02-07 02:44:21 +01:00
|
|
|
b58_sha256_impl = my_sha256;
|
|
|
|
|
2016-07-29 11:36:18 +02:00
|
|
|
b58tobin(keybuf, &keybuflen, base58, base58_len);
|
|
|
|
if (b58check(keybuf, sizeof(keybuf), base58, base58_len) < 0)
|
|
|
|
return false;
|
2015-05-26 06:38:12 +02:00
|
|
|
|
|
|
|
/* Byte after key should be 1 to represent a compressed key. */
|
2016-07-01 03:57:57 +02:00
|
|
|
if (keybuf[1 + 32] != 1)
|
2016-07-29 11:36:18 +02:00
|
|
|
return false;
|
2015-05-26 06:38:12 +02:00
|
|
|
|
|
|
|
if (keybuf[0] == 128)
|
|
|
|
*test_net = false;
|
|
|
|
else if (keybuf[0] == 239)
|
|
|
|
*test_net = true;
|
|
|
|
else
|
2016-07-29 11:36:18 +02:00
|
|
|
return false;
|
2015-05-26 06:38:12 +02:00
|
|
|
|
2015-06-26 04:24:07 +02:00
|
|
|
/* Copy out secret. */
|
|
|
|
memcpy(priv->secret, keybuf + 1, sizeof(priv->secret));
|
2015-05-26 06:38:12 +02:00
|
|
|
|
2016-12-02 08:42:58 +01:00
|
|
|
if (!secp256k1_ec_seckey_verify(secp256k1_ctx, priv->secret))
|
2016-07-29 11:36:18 +02:00
|
|
|
return false;
|
2015-06-26 04:24:07 +02:00
|
|
|
|
2016-07-01 03:57:57 +02:00
|
|
|
/* Get public key, too. */
|
2016-12-02 08:42:58 +01:00
|
|
|
if (!pubkey_from_privkey(priv, key))
|
2016-07-29 11:36:18 +02:00
|
|
|
return false;
|
2015-09-30 03:24:54 +02:00
|
|
|
|
2015-06-26 04:24:07 +02:00
|
|
|
return true;
|
2015-05-26 06:38:12 +02:00
|
|
|
}
|