2016-11-29 20:51:50 +01:00
|
|
|
#include "wire.h"
|
2017-03-29 12:56:15 +02:00
|
|
|
#include <bitcoin/preimage.h>
|
2016-11-29 20:51:50 +01:00
|
|
|
#include <bitcoin/pubkey.h>
|
2017-02-21 05:45:28 +01:00
|
|
|
#include <bitcoin/shadouble.h>
|
2017-04-12 08:10:15 +02:00
|
|
|
#include <ccan/build_assert/build_assert.h>
|
2016-11-29 20:51:50 +01:00
|
|
|
#include <ccan/endian/endian.h>
|
|
|
|
#include <ccan/mem/mem.h>
|
2017-01-25 00:32:53 +01:00
|
|
|
#include <ccan/tal/str/str.h>
|
2017-08-28 18:02:01 +02:00
|
|
|
#include <common/type_to_string.h>
|
|
|
|
#include <common/utils.h>
|
2016-11-29 20:51:50 +01:00
|
|
|
|
|
|
|
/* Sets *cursor to NULL and returns NULL when extraction fails. */
|
2017-06-06 05:03:31 +02:00
|
|
|
const void *fromwire_fail(const u8 **cursor, size_t *max)
|
2016-11-29 20:51:50 +01:00
|
|
|
{
|
|
|
|
*cursor = NULL;
|
|
|
|
*max = 0;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
const u8 *fromwire(const u8 **cursor, size_t *max, void *copy, size_t n)
|
|
|
|
{
|
|
|
|
const u8 *p = *cursor;
|
|
|
|
|
|
|
|
if (*max < n) {
|
|
|
|
/* Just make sure we don't leak uninitialized mem! */
|
|
|
|
if (copy)
|
|
|
|
memset(copy, 0, n);
|
2017-06-06 05:03:31 +02:00
|
|
|
return fromwire_fail(cursor, max);
|
2016-11-29 20:51:50 +01:00
|
|
|
}
|
|
|
|
*cursor += n;
|
|
|
|
*max -= n;
|
|
|
|
if (copy)
|
|
|
|
memcpy(copy, p, n);
|
|
|
|
return memcheck(p, n);
|
|
|
|
}
|
|
|
|
|
2017-01-04 04:39:21 +01:00
|
|
|
int fromwire_peektype(const u8 *cursor)
|
|
|
|
{
|
|
|
|
be16 be_type;
|
|
|
|
size_t max = tal_count(cursor);
|
|
|
|
|
|
|
|
fromwire(&cursor, &max, &be_type, sizeof(be_type));
|
|
|
|
if (!cursor)
|
|
|
|
return -1;
|
|
|
|
return be16_to_cpu(be_type);
|
|
|
|
}
|
|
|
|
|
2016-11-29 20:51:50 +01:00
|
|
|
u8 fromwire_u8(const u8 **cursor, size_t *max)
|
|
|
|
{
|
|
|
|
u8 ret;
|
|
|
|
|
|
|
|
if (!fromwire(cursor, max, &ret, sizeof(ret)))
|
|
|
|
return 0;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
u16 fromwire_u16(const u8 **cursor, size_t *max)
|
|
|
|
{
|
|
|
|
be16 ret;
|
|
|
|
|
|
|
|
if (!fromwire(cursor, max, &ret, sizeof(ret)))
|
|
|
|
return 0;
|
|
|
|
return be16_to_cpu(ret);
|
|
|
|
}
|
2017-01-04 04:39:20 +01:00
|
|
|
|
2016-11-29 20:51:50 +01:00
|
|
|
u32 fromwire_u32(const u8 **cursor, size_t *max)
|
|
|
|
{
|
|
|
|
be32 ret;
|
|
|
|
|
|
|
|
if (!fromwire(cursor, max, &ret, sizeof(ret)))
|
|
|
|
return 0;
|
|
|
|
return be32_to_cpu(ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
u64 fromwire_u64(const u8 **cursor, size_t *max)
|
|
|
|
{
|
|
|
|
be64 ret;
|
|
|
|
|
|
|
|
if (!fromwire(cursor, max, &ret, sizeof(ret)))
|
|
|
|
return 0;
|
|
|
|
return be64_to_cpu(ret);
|
|
|
|
}
|
|
|
|
|
2017-01-04 04:39:21 +01:00
|
|
|
bool fromwire_bool(const u8 **cursor, size_t *max)
|
|
|
|
{
|
|
|
|
u8 ret;
|
|
|
|
|
|
|
|
if (!fromwire(cursor, max, &ret, sizeof(ret)))
|
|
|
|
return false;
|
|
|
|
if (ret != 0 && ret != 1)
|
2017-06-06 05:03:31 +02:00
|
|
|
fromwire_fail(cursor, max);
|
2017-01-04 04:39:21 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-12-02 08:41:06 +01:00
|
|
|
void fromwire_pubkey(const u8 **cursor, size_t *max, struct pubkey *pubkey)
|
2016-11-29 20:51:50 +01:00
|
|
|
{
|
|
|
|
u8 der[PUBKEY_DER_LEN];
|
|
|
|
|
|
|
|
if (!fromwire(cursor, max, der, sizeof(der)))
|
|
|
|
return;
|
|
|
|
|
2017-05-05 08:41:44 +02:00
|
|
|
/* FIXME: Handing dummy keys through here is dumb.
|
|
|
|
* See towire_gossip_resolve_channel_reply --RR */
|
|
|
|
if (!memeqzero(der, sizeof(der))
|
|
|
|
&& !pubkey_from_der(der, sizeof(der), pubkey))
|
2017-06-06 05:03:31 +02:00
|
|
|
fromwire_fail(cursor, max);
|
2016-11-29 20:51:50 +01:00
|
|
|
}
|
|
|
|
|
2017-05-06 04:19:44 +02:00
|
|
|
void fromwire_secret(const u8 **cursor, size_t *max, struct secret *secret)
|
|
|
|
{
|
|
|
|
fromwire(cursor, max, secret->data, sizeof(secret->data));
|
|
|
|
}
|
|
|
|
|
2017-02-21 05:45:28 +01:00
|
|
|
void fromwire_privkey(const u8 **cursor, size_t *max, struct privkey *privkey)
|
|
|
|
{
|
2017-05-06 04:19:44 +02:00
|
|
|
fromwire_secret(cursor, max, &privkey->secret);
|
2017-02-21 05:45:28 +01:00
|
|
|
}
|
|
|
|
|
2017-01-25 00:33:42 +01:00
|
|
|
void fromwire_secp256k1_ecdsa_signature(const u8 **cursor,
|
|
|
|
size_t *max, secp256k1_ecdsa_signature *sig)
|
2016-11-29 20:51:50 +01:00
|
|
|
{
|
|
|
|
u8 compact[64];
|
|
|
|
|
|
|
|
if (!fromwire(cursor, max, compact, sizeof(compact)))
|
|
|
|
return;
|
|
|
|
|
2017-01-25 00:33:42 +01:00
|
|
|
if (secp256k1_ecdsa_signature_parse_compact(secp256k1_ctx, sig, compact)
|
2016-11-29 20:51:50 +01:00
|
|
|
!= 1)
|
2017-06-06 05:03:31 +02:00
|
|
|
fromwire_fail(cursor, max);
|
2016-11-29 20:51:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void fromwire_channel_id(const u8 **cursor, size_t *max,
|
|
|
|
struct channel_id *channel_id)
|
2017-03-02 13:21:49 +01:00
|
|
|
{
|
|
|
|
fromwire(cursor, max, channel_id, sizeof(*channel_id));
|
|
|
|
}
|
|
|
|
|
|
|
|
void fromwire_short_channel_id(const u8 **cursor, size_t *max,
|
|
|
|
struct short_channel_id *short_channel_id)
|
2016-11-29 20:51:50 +01:00
|
|
|
{
|
2017-05-19 14:28:39 +02:00
|
|
|
be32 txnum = 0, blocknum = 0;
|
2016-11-29 20:51:50 +01:00
|
|
|
|
|
|
|
/* Pulling 3 bytes off wire is tricky; they're big-endian. */
|
2017-05-19 14:28:39 +02:00
|
|
|
fromwire(cursor, max, (char *)&blocknum + 1, 3);
|
|
|
|
short_channel_id->blocknum = be32_to_cpu(blocknum);
|
2016-11-29 20:51:50 +01:00
|
|
|
fromwire(cursor, max, (char *)&txnum + 1, 3);
|
2017-03-02 13:21:49 +01:00
|
|
|
short_channel_id->txnum = be32_to_cpu(txnum);
|
2017-05-19 14:28:39 +02:00
|
|
|
|
|
|
|
short_channel_id->outnum = fromwire_u16 (cursor, max);
|
2016-11-29 20:51:50 +01:00
|
|
|
}
|
2017-01-04 04:39:20 +01:00
|
|
|
|
2016-11-29 20:51:50 +01:00
|
|
|
void fromwire_sha256(const u8 **cursor, size_t *max, struct sha256 *sha256)
|
|
|
|
{
|
|
|
|
fromwire(cursor, max, sha256, sizeof(*sha256));
|
|
|
|
}
|
|
|
|
|
2017-02-21 05:45:28 +01:00
|
|
|
void fromwire_sha256_double(const u8 **cursor, size_t *max,
|
|
|
|
struct sha256_double *sha256d)
|
|
|
|
{
|
|
|
|
fromwire_sha256(cursor, max, &sha256d->sha);
|
|
|
|
}
|
|
|
|
|
2017-03-29 12:56:15 +02:00
|
|
|
void fromwire_preimage(const u8 **cursor, size_t *max, struct preimage *preimage)
|
|
|
|
{
|
|
|
|
fromwire(cursor, max, preimage, sizeof(*preimage));
|
|
|
|
}
|
|
|
|
|
2017-08-18 06:43:52 +02:00
|
|
|
void fromwire_ripemd160(const u8 **cursor, size_t *max, struct ripemd160 *ripemd)
|
|
|
|
{
|
|
|
|
fromwire(cursor, max, ripemd, sizeof(*ripemd));
|
|
|
|
}
|
|
|
|
|
2017-09-01 06:18:55 +02:00
|
|
|
/* BOLT #7:
|
|
|
|
*
|
|
|
|
* The following `address descriptor` types are defined:
|
|
|
|
*
|
|
|
|
* * `0`: padding. data = none (length 0).
|
|
|
|
* * `1`: ipv4. data = `[4:ipv4_addr][2:port]` (length 6)
|
|
|
|
* * `2`: ipv6. data = `[16:ipv6_addr][2:port]` (length 18)
|
|
|
|
*/
|
|
|
|
/* FIXME: Tor addresses! */
|
|
|
|
|
|
|
|
/* Returns false if we didn't parse it, and *cursor == NULL if malformed. */
|
|
|
|
bool fromwire_ipaddr(const u8 **cursor, size_t *max, struct ipaddr *addr)
|
2016-11-29 20:51:50 +01:00
|
|
|
{
|
2017-09-01 06:18:55 +02:00
|
|
|
addr->type = fromwire_u8(cursor, max);
|
2017-05-08 15:15:29 +02:00
|
|
|
|
|
|
|
switch (addr->type) {
|
2017-09-01 06:18:55 +02:00
|
|
|
case ADDR_TYPE_IPV4:
|
2017-05-08 15:15:29 +02:00
|
|
|
addr->addrlen = 4;
|
|
|
|
break;
|
2017-09-01 06:18:55 +02:00
|
|
|
case ADDR_TYPE_IPV6:
|
2017-05-08 15:15:29 +02:00
|
|
|
addr->addrlen = 16;
|
|
|
|
break;
|
|
|
|
default:
|
2017-09-01 06:18:55 +02:00
|
|
|
return false;
|
2017-05-08 15:15:29 +02:00
|
|
|
}
|
|
|
|
fromwire(cursor, max, addr->addr, addr->addrlen);
|
|
|
|
addr->port = fromwire_u16(cursor, max);
|
2017-09-01 06:18:55 +02:00
|
|
|
|
|
|
|
/* Skip any post-padding */
|
|
|
|
while (*max && (*cursor)[0] == ADDR_TYPE_PADDING)
|
|
|
|
fromwire_u8(cursor, max);
|
|
|
|
|
|
|
|
return *cursor != NULL;
|
2016-11-29 20:51:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void fromwire_u8_array(const u8 **cursor, size_t *max, u8 *arr, size_t num)
|
|
|
|
{
|
|
|
|
fromwire(cursor, max, arr, num);
|
|
|
|
}
|
|
|
|
|
2017-01-04 04:39:20 +01:00
|
|
|
void fromwire_pad(const u8 **cursor, size_t *max, size_t num)
|
2016-11-29 20:51:50 +01:00
|
|
|
{
|
2017-01-04 04:39:20 +01:00
|
|
|
fromwire(cursor, max, NULL, num);
|
2016-11-29 20:51:50 +01:00
|
|
|
}
|
2017-01-04 04:39:20 +01:00
|
|
|
|
2017-08-28 18:04:01 +02:00
|
|
|
REGISTER_TYPE_TO_STRING(short_channel_id, short_channel_id_to_str);
|
2017-03-02 13:21:49 +01:00
|
|
|
REGISTER_TYPE_TO_HEXSTR(channel_id);
|
2017-04-12 08:10:15 +02:00
|
|
|
|
|
|
|
/* BOLT #2:
|
|
|
|
*
|
2017-06-06 01:48:10 +02:00
|
|
|
* This message introduces the `channel_id` to identify the channel, which is
|
|
|
|
* derived from the funding transaction by combining the `funding_txid` and
|
|
|
|
* the `funding_output_index` using big-endian exclusive-OR
|
|
|
|
* (ie. `funding_output_index` alters the last two bytes).
|
2017-04-12 08:10:15 +02:00
|
|
|
*/
|
|
|
|
void derive_channel_id(struct channel_id *channel_id,
|
|
|
|
struct sha256_double *txid, u16 txout)
|
|
|
|
{
|
|
|
|
BUILD_ASSERT(sizeof(*channel_id) == sizeof(*txid));
|
|
|
|
memcpy(channel_id, txid, sizeof(*channel_id));
|
|
|
|
channel_id->id[sizeof(*channel_id)-2] ^= txout >> 8;
|
|
|
|
channel_id->id[sizeof(*channel_id)-1] ^= txout;
|
|
|
|
}
|
|
|
|
|