coin moves: add wire handlers for chain coin moves

onchaind is the only daemon that emits coin events, and those are all
onchain (ha!), so the only 'wire' facility we need for coin moves are
for the 'chain' type.
This commit is contained in:
lisa neigut 2020-04-01 20:54:06 -05:00 committed by Rusty Russell
parent 3a4a3597a3
commit dcde37581e
2 changed files with 61 additions and 0 deletions

View file

@ -180,3 +180,61 @@ struct coin_mvt *finalize_channel_mvt(const tal_t *ctx,
return mvt;
}
void towire_chain_coin_mvt(u8 **pptr, const struct chain_coin_mvt *mvt)
{
if (mvt->account_name) {
towire_u16(pptr, strlen(mvt->account_name));
towire_u8_array(pptr, (u8 *)mvt->account_name, strlen(mvt->account_name));
} else
towire_u16(pptr, 0);
towire_bitcoin_txid(pptr, cast_const(struct bitcoin_txid *, mvt->tx_txid));
if (mvt->output_txid) {
towire_bool(pptr, true);
towire_bitcoin_txid(pptr, cast_const(struct bitcoin_txid *, mvt->output_txid));
} else
towire_bool(pptr, false);
towire_u32(pptr, mvt->vout);
if (mvt->payment_hash) {
towire_bool(pptr, true);
towire_sha256(pptr, mvt->payment_hash);
} else
towire_bool(pptr, false);
towire_u8(pptr, mvt->tag);
towire_amount_msat(pptr, mvt->credit);
towire_amount_msat(pptr, mvt->debit);
towire_u8(pptr, mvt->unit);
}
void fromwire_chain_coin_mvt(const u8 **cursor, size_t *max, struct chain_coin_mvt *mvt)
{
u16 account_name_len;
account_name_len = fromwire_u16(cursor, max);
if (account_name_len) {
mvt->account_name = tal_arr(mvt, char, account_name_len);
fromwire_u8_array(cursor, max, (u8 *)mvt->account_name, account_name_len);
} else
mvt->account_name = NULL;
mvt->tx_txid = tal(mvt, struct bitcoin_txid);
fromwire_bitcoin_txid(cursor, max,
cast_const(struct bitcoin_txid *, mvt->tx_txid));
if (fromwire_bool(cursor, max)) {
mvt->output_txid = tal(mvt, struct bitcoin_txid);
fromwire_bitcoin_txid(cursor, max,
cast_const(struct bitcoin_txid *, mvt->output_txid));
} else
mvt->output_txid = NULL;
mvt->vout = fromwire_u32(cursor, max);
if (fromwire_bool(cursor, max)) {
mvt->payment_hash = tal(mvt, struct sha256);
fromwire_sha256(cursor, max, mvt->payment_hash);
} else
mvt->payment_hash = NULL;
mvt->tag = fromwire_u8(cursor, max);
mvt->credit = fromwire_amount_msat(cursor, max);
mvt->debit = fromwire_amount_msat(cursor, max);
mvt->unit = fromwire_u8(cursor, max);
}

View file

@ -165,4 +165,7 @@ const char *mvt_type_str(enum mvt_type type);
const char *mvt_tag_str(enum mvt_tag tag);
const char *mvt_unit_str(enum mvt_unit_type unit);
void towire_chain_coin_mvt(u8 **pptr, const struct chain_coin_mvt *mvt);
void fromwire_chain_coin_mvt(const u8 **cursor, size_t *max, struct chain_coin_mvt *mvt);
#endif /* LIGHTNING_COMMON_COIN_MVT_H */