mirror of
https://github.com/aljazceru/lightning.git
synced 2026-02-23 06:54:30 +01:00
Move pkt.c into test-cli.
We use cryptopkt for normal cases. Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
227
test-cli/pkt.c
Normal file
227
test-cli/pkt.c
Normal file
@@ -0,0 +1,227 @@
|
||||
#include "bitcoin/address.h"
|
||||
#include "bitcoin/pubkey.h"
|
||||
#include "bitcoin/signature.h"
|
||||
#include "bitcoin/tx.h"
|
||||
#include "pkt.h"
|
||||
#include "protobuf_convert.h"
|
||||
#include <ccan/crypto/sha256/sha256.h>
|
||||
#include <ccan/err/err.h>
|
||||
#include <ccan/tal/grab_file/grab_file.h>
|
||||
|
||||
size_t pkt_totlen(const struct pkt *pkt)
|
||||
{
|
||||
return sizeof(pkt->len) + pkt->len;
|
||||
}
|
||||
|
||||
static struct pkt *to_pkt(const tal_t *ctx, Pkt__PktCase type, const void *msg)
|
||||
{
|
||||
struct pkt *ret;
|
||||
size_t len;
|
||||
Pkt p = PKT__INIT;
|
||||
|
||||
p.pkt_case = type;
|
||||
/* This is a union, so doesn't matter which we assign. */
|
||||
p.error = (Error *)msg;
|
||||
|
||||
len = pkt__get_packed_size(&p);
|
||||
ret = (struct pkt *)tal_arr(ctx, u8, sizeof(ret->len) + len);
|
||||
ret->len = len;
|
||||
|
||||
pkt__pack(&p, ret->data);
|
||||
return ret;
|
||||
}
|
||||
|
||||
struct pkt *open_channel_pkt(const tal_t *ctx,
|
||||
const struct sha256 *revocation_hash,
|
||||
const struct pubkey *commit,
|
||||
const struct pubkey *final,
|
||||
u32 rel_locktime_seconds,
|
||||
bool offer_anchor,
|
||||
u32 min_depth,
|
||||
u64 commitment_fee)
|
||||
{
|
||||
OpenChannel o = OPEN_CHANNEL__INIT;
|
||||
Locktime lt = LOCKTIME__INIT;
|
||||
|
||||
o.revocation_hash = sha256_to_proto(ctx, revocation_hash);
|
||||
o.commit_key = pubkey_to_proto(ctx, commit);
|
||||
o.final_key = pubkey_to_proto(ctx, final);
|
||||
lt.locktime_case = LOCKTIME__LOCKTIME_SECONDS;
|
||||
lt.seconds = rel_locktime_seconds;
|
||||
o.delay = <
|
||||
o.commitment_fee = commitment_fee;
|
||||
if (offer_anchor)
|
||||
o.anch = OPEN_CHANNEL__ANCHOR_OFFER__WILL_CREATE_ANCHOR;
|
||||
else
|
||||
o.anch = OPEN_CHANNEL__ANCHOR_OFFER__WONT_CREATE_ANCHOR;
|
||||
|
||||
o.min_depth = min_depth;
|
||||
|
||||
{
|
||||
size_t len = open_channel__get_packed_size(&o);
|
||||
unsigned char *pb = malloc(len);
|
||||
open_channel__pack(&o, pb);
|
||||
assert(open_channel__unpack(NULL, len, pb));
|
||||
free(pb);
|
||||
}
|
||||
|
||||
return to_pkt(ctx, PKT__PKT_OPEN, &o);
|
||||
}
|
||||
|
||||
struct pkt *open_anchor_pkt(const tal_t *ctx, const OpenAnchor *oa_msg)
|
||||
{
|
||||
return to_pkt(ctx, PKT__PKT_OPEN_ANCHOR, oa_msg);
|
||||
}
|
||||
|
||||
Pkt *any_pkt_from_file(const char *filename)
|
||||
{
|
||||
struct pkt *pkt;
|
||||
Pkt *ret;
|
||||
size_t len;
|
||||
|
||||
pkt = grab_file(NULL, filename);
|
||||
if (!pkt)
|
||||
err(1, "Opening %s", filename);
|
||||
|
||||
len = tal_count(pkt) - 1;
|
||||
if (len < sizeof(pkt->len) || len != sizeof(pkt->len) + pkt->len)
|
||||
errx(1, "%s length is wrong", filename);
|
||||
len -= sizeof(pkt->len);
|
||||
|
||||
ret = pkt__unpack(NULL, len, pkt->data);
|
||||
if (!ret)
|
||||
errx(1, "Unpack failed for %s", filename);
|
||||
return ret;
|
||||
}
|
||||
|
||||
Pkt *pkt_from_file(const char *filename, Pkt__PktCase expect)
|
||||
{
|
||||
Pkt *ret = any_pkt_from_file(filename);
|
||||
|
||||
if (ret->pkt_case != expect)
|
||||
errx(1, "Unexpected type %i in %s", ret->pkt_case, filename);
|
||||
return ret;
|
||||
}
|
||||
|
||||
struct pkt *open_commit_sig_pkt(const tal_t *ctx, const struct signature *sig)
|
||||
{
|
||||
OpenCommitSig o = OPEN_COMMIT_SIG__INIT;
|
||||
|
||||
o.sig = signature_to_proto(ctx, sig);
|
||||
return to_pkt(ctx, PKT__PKT_OPEN_COMMIT_SIG, &o);
|
||||
}
|
||||
|
||||
struct pkt *close_channel_pkt(const tal_t *ctx,
|
||||
uint64_t fee,
|
||||
const struct signature *sig)
|
||||
{
|
||||
CloseChannel c = CLOSE_CHANNEL__INIT;
|
||||
c.close_fee = fee;
|
||||
c.sig = signature_to_proto(ctx, sig);
|
||||
return to_pkt(ctx, PKT__PKT_CLOSE, &c);
|
||||
}
|
||||
|
||||
struct pkt *close_channel_complete_pkt(const tal_t *ctx,
|
||||
const struct signature *sig)
|
||||
{
|
||||
CloseChannelComplete c = CLOSE_CHANNEL_COMPLETE__INIT;
|
||||
c.sig = signature_to_proto(ctx, sig);
|
||||
return to_pkt(ctx, PKT__PKT_CLOSE_COMPLETE, &c);
|
||||
}
|
||||
|
||||
struct pkt *update_pkt(const tal_t *ctx,
|
||||
const struct sha256 *revocation_hash,
|
||||
s64 delta)
|
||||
{
|
||||
Update u = UPDATE__INIT;
|
||||
u.revocation_hash = sha256_to_proto(ctx, revocation_hash);
|
||||
u.delta_msat = delta * 1000;
|
||||
return to_pkt(ctx, PKT__PKT_UPDATE, &u);
|
||||
}
|
||||
|
||||
struct pkt *update_htlc_add_pkt(const tal_t *ctx,
|
||||
const struct sha256 *revocation_hash,
|
||||
u32 value,
|
||||
const struct sha256 *htlc_rhash,
|
||||
u32 abs_locktime_seconds)
|
||||
{
|
||||
UpdateAddHtlc u = UPDATE_ADD_HTLC__INIT;
|
||||
Locktime l = LOCKTIME__INIT;
|
||||
|
||||
/* HTLC total must fit in 32 bits. */
|
||||
if (value > (1ULL << 32) / 1000)
|
||||
return NULL;
|
||||
|
||||
u.revocation_hash = sha256_to_proto(ctx, revocation_hash);
|
||||
u.amount_msat = value * 1000;
|
||||
u.r_hash = sha256_to_proto(ctx, htlc_rhash);
|
||||
l.locktime_case = LOCKTIME__LOCKTIME_SECONDS;
|
||||
l.seconds = abs_locktime_seconds;
|
||||
u.expiry = &l;
|
||||
|
||||
return to_pkt(ctx, PKT__PKT_UPDATE_ADD_HTLC, &u);
|
||||
}
|
||||
|
||||
struct pkt *update_htlc_complete_pkt(const tal_t *ctx,
|
||||
const struct sha256 *revocation_hash,
|
||||
const struct sha256 *rval)
|
||||
{
|
||||
UpdateFulfillHtlc u = UPDATE_FULFILL_HTLC__INIT;
|
||||
|
||||
u.revocation_hash = sha256_to_proto(ctx, revocation_hash);
|
||||
u.r = sha256_to_proto(ctx, rval);
|
||||
|
||||
return to_pkt(ctx, PKT__PKT_UPDATE_FULFILL_HTLC, &u);
|
||||
}
|
||||
|
||||
struct pkt *update_htlc_timedout_pkt(const tal_t *ctx,
|
||||
const struct sha256 *revocation_hash,
|
||||
const struct sha256 *htlc_rhash)
|
||||
{
|
||||
UpdateTimedoutHtlc u = UPDATE_TIMEDOUT_HTLC__INIT;
|
||||
|
||||
u.revocation_hash = sha256_to_proto(ctx, revocation_hash);
|
||||
u.r_hash = sha256_to_proto(ctx, htlc_rhash);
|
||||
|
||||
return to_pkt(ctx, PKT__PKT_UPDATE_TIMEDOUT_HTLC, &u);
|
||||
}
|
||||
|
||||
struct pkt *update_htlc_routefail_pkt(const tal_t *ctx,
|
||||
const struct sha256 *revocation_hash,
|
||||
const struct sha256 *htlc_rhash)
|
||||
{
|
||||
UpdateRoutefailHtlc u = UPDATE_ROUTEFAIL_HTLC__INIT;
|
||||
|
||||
u.revocation_hash = sha256_to_proto(ctx, revocation_hash);
|
||||
u.r_hash = sha256_to_proto(ctx, htlc_rhash);
|
||||
|
||||
return to_pkt(ctx, PKT__PKT_UPDATE_ROUTEFAIL_HTLC, &u);
|
||||
}
|
||||
|
||||
struct pkt *update_accept_pkt(const tal_t *ctx,
|
||||
struct signature *sig,
|
||||
const struct sha256 *revocation_hash)
|
||||
{
|
||||
UpdateAccept ua = UPDATE_ACCEPT__INIT;
|
||||
ua.sig = signature_to_proto(ctx, sig);
|
||||
ua.revocation_hash = sha256_to_proto(ctx, revocation_hash);
|
||||
return to_pkt(ctx, PKT__PKT_UPDATE_ACCEPT, &ua);
|
||||
}
|
||||
|
||||
struct pkt *update_signature_pkt(const tal_t *ctx,
|
||||
const struct signature *sig,
|
||||
const struct sha256 *revocation_preimage)
|
||||
{
|
||||
UpdateSignature us = UPDATE_SIGNATURE__INIT;
|
||||
us.sig = signature_to_proto(ctx, sig);
|
||||
us.revocation_preimage = sha256_to_proto(ctx, revocation_preimage);
|
||||
return to_pkt(ctx, PKT__PKT_UPDATE_SIGNATURE, &us);
|
||||
}
|
||||
|
||||
struct pkt *update_complete_pkt(const tal_t *ctx,
|
||||
const struct sha256 *revocation_preimage)
|
||||
{
|
||||
UpdateComplete uc = UPDATE_COMPLETE__INIT;
|
||||
uc.revocation_preimage = sha256_to_proto(ctx, revocation_preimage);
|
||||
return to_pkt(ctx, PKT__PKT_UPDATE_COMPLETE, &uc);
|
||||
}
|
||||
165
test-cli/pkt.h
Normal file
165
test-cli/pkt.h
Normal file
@@ -0,0 +1,165 @@
|
||||
#ifndef LIGHTNING_PKT_H
|
||||
#define LIGHTNING_PKT_H
|
||||
#include "config.h"
|
||||
|
||||
/* Simple (non-threadsafe!) wrapper for protobufs.
|
||||
*
|
||||
* This could be a simple set of macros, if the protobuf-c people hadn't
|
||||
* insisted on "prettifing" the names they generate into CamelCase.
|
||||
*/
|
||||
#include "lightning.pb-c.h"
|
||||
#include <ccan/short_types/short_types.h>
|
||||
#include <ccan/tal/tal.h>
|
||||
|
||||
/* A packet, ready to be de-protobuf'ed. */
|
||||
struct pkt {
|
||||
u32 len;
|
||||
u8 data[];
|
||||
};
|
||||
|
||||
/* Utility helper: dies if there's a problem. */
|
||||
Pkt *pkt_from_file(const char *filename, Pkt__PktCase expect);
|
||||
Pkt *any_pkt_from_file(const char *filename);
|
||||
|
||||
/* Total length of packet, including header. */
|
||||
size_t pkt_totlen(const struct pkt *pkt);
|
||||
|
||||
struct sha256;
|
||||
struct bitcoin_compressed_pubkey;
|
||||
struct signature;
|
||||
struct pubkey;
|
||||
|
||||
/**
|
||||
* open_channel_pkt - create an openchannel message
|
||||
* @ctx: tal context to allocate off.
|
||||
* @revocation_hash: first hash value generated from seed.
|
||||
* @commit: the pubkey for the anchor transactions' P2SH output.
|
||||
* @final: the pubkey for the commit transactions' P2SH output.
|
||||
* @rel_locktime_seconds: relative seconds for commitment locktime.
|
||||
* @offer_anchor: whether we will offer anchor.
|
||||
* @min_depth: minimum depth to insist on (if non-zero)
|
||||
* @commitment_fee: fee we would like for commitment txs.
|
||||
*/
|
||||
struct pkt *open_channel_pkt(const tal_t *ctx,
|
||||
const struct sha256 *revocation_hash,
|
||||
const struct pubkey *commit,
|
||||
const struct pubkey *final,
|
||||
u32 rel_locktime_seconds,
|
||||
bool offer_anchor,
|
||||
u32 min_depth,
|
||||
u64 commitment_fee);
|
||||
|
||||
/**
|
||||
* open_anchor_pkt - create an open_anchor message packet
|
||||
* @ctx: tal context to allocate off.
|
||||
* @oa_msg: the OpenAnchor message.
|
||||
*/
|
||||
struct pkt *open_anchor_pkt(const tal_t *ctx, const OpenAnchor *oa_msg);
|
||||
|
||||
/**
|
||||
* open_commit_sig_pkt - create an open_commit_sig message
|
||||
* @ctx: tal context to allocate off.
|
||||
* @sig: the signature for the commit transaction input.
|
||||
*/
|
||||
struct pkt *open_commit_sig_pkt(const tal_t *ctx, const struct signature *sig);
|
||||
|
||||
/**
|
||||
* close_channel_pkt - create an close_channel message
|
||||
* @ctx: tal context to allocate off.
|
||||
* @fee: the fee for the transaction.
|
||||
* @sig: the signature for the close transaction input.
|
||||
*/
|
||||
struct pkt *close_channel_pkt(const tal_t *ctx,
|
||||
uint64_t fee,
|
||||
const struct signature *sig);
|
||||
|
||||
/**
|
||||
* close_channel_complete_pkt - create an close_channel_complete message
|
||||
* @ctx: tal context to allocate off.
|
||||
* @sig: the signature for the close transaction input.
|
||||
*/
|
||||
struct pkt *close_channel_complete_pkt(const tal_t *ctx,
|
||||
const struct signature *sig);
|
||||
|
||||
/**
|
||||
* update_pkt - create an update message
|
||||
* @ctx: tal context to allocate off.
|
||||
* @revocation_hash: the revocation hash for the next tx.
|
||||
* @delta: the change in satoshis (to me).
|
||||
*/
|
||||
struct pkt *update_pkt(const tal_t *ctx,
|
||||
const struct sha256 *revocation_hash,
|
||||
s64 delta);
|
||||
|
||||
/**
|
||||
* update_htlc_add_pkt - create an update message adding a HTLC
|
||||
* @ctx: tal context to allocate off.
|
||||
* @revocation_hash: the revocation hash for the next commitment tx.
|
||||
* @val: the change in satoshis (from me).
|
||||
* @htlc_rhash: the hash of the htlc secret.
|
||||
* @abs_locktime_seconds: the HTLC timeout.
|
||||
*/
|
||||
struct pkt *update_htlc_add_pkt(const tal_t *ctx,
|
||||
const struct sha256 *revocation_hash,
|
||||
u32 value,
|
||||
const struct sha256 *htlc_rhash,
|
||||
u32 abs_locktime_seconds);
|
||||
|
||||
/**
|
||||
* update_htlc_complete_pkt - create an update message completing a HTLC
|
||||
* @ctx: tal context to allocate off.
|
||||
* @revocation_hash: the revocation hash for the next commitment tx.
|
||||
* @rval: the r value for the HTLC
|
||||
*/
|
||||
struct pkt *update_htlc_complete_pkt(const tal_t *ctx,
|
||||
const struct sha256 *revocation_hash,
|
||||
const struct sha256 *rval);
|
||||
|
||||
/**
|
||||
* update_htlc_timedout_pkt - create an update message removing a HTLC
|
||||
* @ctx: tal context to allocate off.
|
||||
* @revocation_hash: the revocation hash for the next commitment tx.
|
||||
* @htlc_rhash: the hash of the htlc secret.
|
||||
*/
|
||||
struct pkt *update_htlc_timedout_pkt(const tal_t *ctx,
|
||||
const struct sha256 *revocation_hash,
|
||||
const struct sha256 *htlc_rhash);
|
||||
|
||||
/**
|
||||
* update_htlc_routefail_pkt - create an update message removing a HTLC
|
||||
* @ctx: tal context to allocate off.
|
||||
* @revocation_hash: the revocation hash for the next commitment tx.
|
||||
* @htlc_rhash: the hash of the htlc secret.
|
||||
*/
|
||||
struct pkt *update_htlc_routefail_pkt(const tal_t *ctx,
|
||||
const struct sha256 *revocation_hash,
|
||||
const struct sha256 *htlc_rhash);
|
||||
|
||||
/**
|
||||
* update_accept_pkt - create an update_accept message
|
||||
* @ctx: tal context to allocate off.
|
||||
* @sig: the signature for the close transaction input.
|
||||
* @revocation_hash: hash to revoke the next tx.
|
||||
*/
|
||||
struct pkt *update_accept_pkt(const tal_t *ctx,
|
||||
struct signature *sig,
|
||||
const struct sha256 *revocation_hash);
|
||||
|
||||
/**
|
||||
* update_signature_pkt - create an update_signature message
|
||||
* @ctx: tal context to allocate off.
|
||||
* @sig: the signature for the close transaction input.
|
||||
* @revocation_preimage: preimage to revoke existing (now-obsolete) tx.
|
||||
*/
|
||||
struct pkt *update_signature_pkt(const tal_t *ctx,
|
||||
const struct signature *sig,
|
||||
const struct sha256 *revocation_preimage);
|
||||
/**
|
||||
* update_complete_pkt - create an update_accept message
|
||||
* @ctx: tal context to allocate off.
|
||||
* @revocation_preimage: preimage to revoke existing (now-obsolete) tx.
|
||||
*/
|
||||
struct pkt *update_complete_pkt(const tal_t *ctx,
|
||||
const struct sha256 *revocation_preimage);
|
||||
|
||||
#endif /* LIGHTNING_PKT_H */
|
||||
Reference in New Issue
Block a user