2021-12-04 12:23:56 +01:00
|
|
|
#include "config.h"
|
2016-11-29 20:51:50 +01:00
|
|
|
#include "wire.h"
|
2018-12-03 00:15:06 +01:00
|
|
|
#include <assert.h>
|
2018-02-26 03:32:58 +01:00
|
|
|
#include <ccan/crypto/siphash24/siphash24.h>
|
2016-11-29 20:51:50 +01:00
|
|
|
#include <ccan/endian/endian.h>
|
|
|
|
#include <ccan/mem/mem.h>
|
2017-08-28 18:02:01 +02:00
|
|
|
#include <common/utils.h>
|
2016-11-29 20:51:50 +01:00
|
|
|
|
|
|
|
void towire(u8 **pptr, const void *data, size_t len)
|
|
|
|
{
|
|
|
|
size_t oldsize = tal_count(*pptr);
|
|
|
|
|
|
|
|
tal_resize(pptr, oldsize + len);
|
|
|
|
memcpy(*pptr + oldsize, memcheck(data, len), len);
|
|
|
|
}
|
|
|
|
|
|
|
|
void towire_u8(u8 **pptr, u8 v)
|
|
|
|
{
|
|
|
|
towire(pptr, &v, sizeof(v));
|
|
|
|
}
|
|
|
|
|
|
|
|
void towire_u16(u8 **pptr, u16 v)
|
|
|
|
{
|
|
|
|
be16 l = cpu_to_be16(v);
|
|
|
|
towire(pptr, &l, sizeof(l));
|
|
|
|
}
|
|
|
|
|
|
|
|
void towire_u32(u8 **pptr, u32 v)
|
|
|
|
{
|
|
|
|
be32 l = cpu_to_be32(v);
|
|
|
|
towire(pptr, &l, sizeof(l));
|
|
|
|
}
|
|
|
|
|
|
|
|
void towire_u64(u8 **pptr, u64 v)
|
|
|
|
{
|
|
|
|
be64 l = cpu_to_be64(v);
|
|
|
|
towire(pptr, &l, sizeof(l));
|
|
|
|
}
|
|
|
|
|
2019-07-18 07:17:16 +02:00
|
|
|
static void towire_tlv_uint(u8 **pptr, u64 v)
|
|
|
|
{
|
|
|
|
u8 bytes[8];
|
|
|
|
size_t num_zeroes;
|
|
|
|
be64 val;
|
|
|
|
|
|
|
|
val = cpu_to_be64(v);
|
|
|
|
BUILD_ASSERT(sizeof(val) == sizeof(bytes));
|
|
|
|
memcpy(bytes, &val, sizeof(bytes));
|
|
|
|
|
|
|
|
for (num_zeroes = 0; num_zeroes < sizeof(bytes); num_zeroes++)
|
|
|
|
if (bytes[num_zeroes] != 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
towire(pptr, bytes + num_zeroes, sizeof(bytes) - num_zeroes);
|
|
|
|
}
|
|
|
|
|
|
|
|
void towire_tu16(u8 **pptr, u16 v)
|
|
|
|
{
|
|
|
|
return towire_tlv_uint(pptr, v);
|
|
|
|
}
|
|
|
|
|
|
|
|
void towire_tu32(u8 **pptr, u32 v)
|
|
|
|
{
|
|
|
|
return towire_tlv_uint(pptr, v);
|
|
|
|
}
|
|
|
|
|
|
|
|
void towire_tu64(u8 **pptr, u64 v)
|
|
|
|
{
|
|
|
|
return towire_tlv_uint(pptr, v);
|
|
|
|
}
|
|
|
|
|
2017-01-04 04:39:21 +01:00
|
|
|
void towire_bool(u8 **pptr, bool v)
|
|
|
|
{
|
2017-09-26 23:02:48 +02:00
|
|
|
u8 val = v;
|
2017-01-04 04:39:21 +01:00
|
|
|
towire(pptr, &val, sizeof(val));
|
|
|
|
}
|
|
|
|
|
2020-01-26 13:52:29 +01:00
|
|
|
void towire_errcode_t(u8 **pptr, errcode_t v)
|
2020-01-12 15:33:59 +01:00
|
|
|
{
|
2020-01-26 13:52:29 +01:00
|
|
|
towire_u32(pptr, (u32)v);
|
2020-01-12 15:33:59 +01:00
|
|
|
}
|
|
|
|
|
2017-01-25 00:33:42 +01:00
|
|
|
void towire_secp256k1_ecdsa_signature(u8 **pptr,
|
|
|
|
const secp256k1_ecdsa_signature *sig)
|
2016-11-29 20:51:50 +01:00
|
|
|
{
|
|
|
|
u8 compact[64];
|
|
|
|
|
2016-12-02 08:41:06 +01:00
|
|
|
secp256k1_ecdsa_signature_serialize_compact(secp256k1_ctx,
|
2017-01-25 00:33:42 +01:00
|
|
|
compact, sig);
|
2016-11-29 20:51:50 +01:00
|
|
|
towire(pptr, compact, sizeof(compact));
|
|
|
|
}
|
|
|
|
|
2017-10-26 05:01:19 +02:00
|
|
|
void towire_secp256k1_ecdsa_recoverable_signature(u8 **pptr,
|
|
|
|
const secp256k1_ecdsa_recoverable_signature *rsig)
|
|
|
|
{
|
|
|
|
u8 compact[64];
|
|
|
|
int recid;
|
|
|
|
|
|
|
|
secp256k1_ecdsa_recoverable_signature_serialize_compact(secp256k1_ctx,
|
|
|
|
compact,
|
|
|
|
&recid,
|
|
|
|
rsig);
|
|
|
|
towire(pptr, compact, sizeof(compact));
|
|
|
|
towire_u8(pptr, recid);
|
|
|
|
}
|
|
|
|
|
2016-11-29 20:51:50 +01:00
|
|
|
void towire_sha256(u8 **pptr, const struct sha256 *sha256)
|
|
|
|
{
|
|
|
|
towire(pptr, sha256, sizeof(*sha256));
|
|
|
|
}
|
|
|
|
|
2017-08-18 06:43:52 +02:00
|
|
|
void towire_ripemd160(u8 **pptr, const struct ripemd160 *ripemd)
|
|
|
|
{
|
|
|
|
towire(pptr, ripemd, sizeof(*ripemd));
|
|
|
|
}
|
|
|
|
|
2016-11-29 20:51:50 +01:00
|
|
|
void towire_u8_array(u8 **pptr, const u8 *arr, size_t num)
|
|
|
|
{
|
|
|
|
towire(pptr, arr, num);
|
|
|
|
}
|
|
|
|
|
2020-12-05 03:19:54 +01:00
|
|
|
void towire_utf8_array(u8 **pptr, const char *arr, size_t num)
|
|
|
|
{
|
|
|
|
assert(utf8_check(arr, num));
|
|
|
|
towire(pptr, arr, num);
|
|
|
|
}
|
|
|
|
|
2017-01-04 04:39:20 +01:00
|
|
|
void towire_pad(u8 **pptr, size_t num)
|
2016-11-29 20:51:50 +01:00
|
|
|
{
|
|
|
|
/* Simply insert zeros. */
|
|
|
|
size_t oldsize = tal_count(*pptr);
|
|
|
|
|
|
|
|
tal_resize(pptr, oldsize + num);
|
|
|
|
memset(*pptr + oldsize, 0, num);
|
|
|
|
}
|
2018-01-04 18:19:02 +01:00
|
|
|
|
2018-02-08 02:25:12 +01:00
|
|
|
void towire_wirestring(u8 **pptr, const char *str)
|
|
|
|
{
|
|
|
|
towire(pptr, str, strlen(str) + 1);
|
|
|
|
}
|
|
|
|
|
2018-02-26 03:32:58 +01:00
|
|
|
void towire_siphash_seed(u8 **pptr, const struct siphash_seed *seed)
|
|
|
|
{
|
|
|
|
towire(pptr, seed, sizeof(*seed));
|
|
|
|
}
|