diff --git a/lightningd/cryptomsg.c b/lightningd/cryptomsg.c index a9a1cf77f..49986c2b1 100644 --- a/lightningd/cryptomsg.c +++ b/lightningd/cryptomsg.c @@ -9,6 +9,7 @@ #include #include #include +#include #include struct crypto_state { @@ -320,3 +321,31 @@ struct crypto_state *crypto_state(struct peer *peer, return cs; } + +void towire_crypto_state(u8 **ptr, const struct crypto_state *cs) +{ + towire_u64(ptr, cs->rn); + towire_u64(ptr, cs->sn); + towire_sha256(ptr, &cs->sk); + towire_sha256(ptr, &cs->rk); + towire_sha256(ptr, &cs->s_ck); + towire_sha256(ptr, &cs->r_ck); +} + +struct crypto_state *fromwire_crypto_state(const tal_t *ctx, + const u8 **ptr, size_t *max) +{ + struct crypto_state *cs = tal(ctx, struct crypto_state); + + cs->rn = fromwire_u64(ptr, max); + cs->sn = fromwire_u64(ptr, max); + fromwire_sha256(ptr, max, &cs->sk); + fromwire_sha256(ptr, max, &cs->rk); + fromwire_sha256(ptr, max, &cs->s_ck); + fromwire_sha256(ptr, max, &cs->r_ck); + cs->peer = (struct peer *)ctx; + + if (!*ptr) + return tal_free(cs); + return cs; +} diff --git a/lightningd/cryptomsg.h b/lightningd/cryptomsg.h index 47ae10b59..9fbecf81c 100644 --- a/lightningd/cryptomsg.h +++ b/lightningd/cryptomsg.h @@ -2,6 +2,7 @@ #define LIGHTNING_LIGHTNINGD_CRYPTOMSG_H #include "config.h" #include +#include struct io_conn; struct peer; @@ -29,4 +30,8 @@ struct io_plan *peer_write_message(struct io_conn *conn, struct io_plan *(*next)(struct io_conn *, struct peer *)); +void towire_crypto_state(u8 **pptr, const struct crypto_state *cs); +struct crypto_state *fromwire_crypto_state(const tal_t *ctx, + const u8 **ptr, size_t *max); + #endif /* LIGHTNING_LIGHTNINGD_CRYPTOMSG_H */