#include <bitcoin/signature.h>
#include <bitcoin/tx_parts.h>
#include <common/bip32.h>
#include <common/coin_mvt.h>
#include <common/derive_basepoints.h>
#include <common/htlc_wire.h>
#include <common/wallet.h>

# Begin!  Here's the onchain tx which spends funding tx, followed by all HTLCs.
msgtype,onchaind_init,5001
msgdata,onchaind_init,shachain,shachain,
# This needs to be set explicitly since the same message also contains a
# transaction that we need to parse correctly.
msgdata,onchaind_init,chainparams,chainparams,
msgdata,onchaind_init,funding_amount_satoshi,amount_sat,
# Our current balance (of funding amount, not counting any pending htlcs)
msgdata,onchaind_init,our_msat,amount_msat,
# Remote per commit point for committed tx.
msgdata,onchaind_init,old_remote_per_commitment_point,pubkey,
# Remote per commit point for current tx (needed if we haven't got revoke_and_ack yet).
msgdata,onchaind_init,remote_per_commitment_point,pubkey,
msgdata,onchaind_init,local_to_self_delay,u32,
msgdata,onchaind_init,remote_to_self_delay,u32,
msgdata,onchaind_init,local_dust_limit_satoshi,amount_sat,
# Gives an easy way to tell if it's our unilateral close or theirs...
msgdata,onchaind_init,our_broadcast_txid,bitcoin_txid,
msgdata,onchaind_init,local_scriptpubkey_len,u16,
msgdata,onchaind_init,local_scriptpubkey,u8,local_scriptpubkey_len
msgdata,onchaind_init,remote_scriptpubkey_len,u16,
msgdata,onchaind_init,remote_scriptpubkey,u8,remote_scriptpubkey_len
# We need these two for commit number obscurer
msgdata,onchaind_init,opener,enum side,
msgdata,onchaind_init,local_basepoints,basepoints,
msgdata,onchaind_init,remote_basepoints,basepoints,
msgdata,onchaind_init,tx_parts,tx_parts,
msgdata,onchaind_init,locktime,u32,
msgdata,onchaind_init,tx_blockheight,u32,
msgdata,onchaind_init,reasonable_depth,u32,
msgdata,onchaind_init,num_htlc_sigs,u16,
msgdata,onchaind_init,htlc_signature,bitcoin_signature,num_htlc_sigs
msgdata,onchaind_init,min_possible_feerate,u32,
msgdata,onchaind_init,max_possible_feerate,u32,
msgdata,onchaind_init,local_funding_pubkey,pubkey,
msgdata,onchaind_init,remote_funding_pubkey,pubkey,
msgdata,onchaind_init,local_static_remotekey_start,u64,
msgdata,onchaind_init,remote_static_remotekey_start,u64,
msgdata,onchaind_init,option_anchor_outputs,bool,
msgdata,onchaind_init,option_anchors_zero_fee_htlc_tx,bool,
# We need this for BIP125 rule 4
msgdata,onchaind_init,min_relay_feerate,u32,

# This says we're ready; give us htlcs and preimages.
msgtype,onchaind_init_reply,5101
msgdata,onchaind_init_reply,commit_num,u64,

#include <onchaind/onchaind_wire.h>
msgtype,onchaind_htlcs,5002
msgdata,onchaind_htlcs,num_htlcs,u32,
msgdata,onchaind_htlcs,htlc,htlc_stub,num_htlcs
# If it's not in the commitment tx, tell us (immediately or htlc_missing_depth)
msgdata,onchaind_htlcs,tell_if_missing,bool,num_htlcs
msgdata,onchaind_htlcs,tell_immediately,bool,num_htlcs

# master->onchaind: Notifier that an output has been spent by input_num of tx.
msgtype,onchaind_spent,5004
msgdata,onchaind_spent,tx,tx_parts,
msgdata,onchaind_spent,input_num,u32,
msgdata,onchaind_spent,blockheight,u32,

# onchaind->master: do we want to continue watching this?
msgtype,onchaind_spent_reply,5104
msgdata,onchaind_spent_reply,interested,bool,

# master->onchaind: We will receive more than one of these, as depth changes.
msgtype,onchaind_depth,5005
msgdata,onchaind_depth,txid,bitcoin_txid,
msgdata,onchaind_depth,depth,u32,

# master->onchaind: We know HTLC preimage
msgtype,onchaind_known_preimage,5007
msgdata,onchaind_known_preimage,preimage,preimage,

# onchaind->master: We discovered HTLC preimage
msgtype,onchaind_extracted_preimage,5008
msgdata,onchaind_extracted_preimage,preimage,preimage,

# onchaind->master: this HTLC was missing from commit tx.
msgtype,onchaind_missing_htlc_output,5009
msgdata,onchaind_missing_htlc_output,htlc,htlc_stub,

# onchaind->master: this HTLC has timed out (after reasonable_depth)
msgtype,onchaind_htlc_timeout,5010
msgdata,onchaind_htlc_timeout,htlc,htlc_stub,

# onchaind->master: this peer can be forgotten
msgtype,onchaind_all_irrevocably_resolved,5011

# onchaind->master: hey, I identified an UTXO you'll want to track
msgtype,onchaind_add_utxo,5012
msgdata,onchaind_add_utxo,prev_out,bitcoin_outpoint,
msgdata,onchaind_add_utxo,per_commit_point,?pubkey,
msgdata,onchaind_add_utxo,value,amount_sat,
msgdata,onchaind_add_utxo,blockheight,u32,
msgdata,onchaind_add_utxo,len,u16,
msgdata,onchaind_add_utxo,scriptpubkey,u8,len
msgdata,onchaind_add_utxo,csv_lock,u32,

# master -> onchaind: do you have a memleak?
msgtype,onchaind_dev_memleak,5033

msgtype,onchaind_dev_memleak_reply,5133
msgdata,onchaind_dev_memleak_reply,leak,bool,

# Tell the main daemon what we've been watching, mainly used for transactions
# that we tracked automatically but only onchaind knows how to classify their
# transactions.
msgtype,onchaind_annotate_txout,5035
msgdata,onchaind_annotate_txout,outpoint,bitcoin_outpoint,
msgdata,onchaind_annotate_txout,type,enum wallet_tx_type,

msgtype,onchaind_annotate_txin,5036
msgdata,onchaind_annotate_txin,txid,bitcoin_txid,
msgdata,onchaind_annotate_txin,innum,u32,
msgdata,onchaind_annotate_txin,type,enum wallet_tx_type,

msgtype,onchaind_notify_coin_mvt,5037
msgdata,onchaind_notify_coin_mvt,mvt,chain_coin_mvt,

msgtype,onchaind_notify_penalty_adj,5038
msgdata,onchaind_notify_penalty_adj,amount,amount_msat,

# We tell lightningd to create, sign and broadcast this tx: 
msgtype,onchaind_spend_to_us,5040
msgdata,onchaind_spend_to_us,outpoint,bitcoin_outpoint,
msgdata,onchaind_spend_to_us,outpoint_amount,amount_sat,
msgdata,onchaind_spend_to_us,sequence,u32,
msgdata,onchaind_spend_to_us,minblock,u32,
msgdata,onchaind_spend_to_us,commit_num,u64,
msgdata,onchaind_spend_to_us,wscript_len,u32,
msgdata,onchaind_spend_to_us,wscript,u8,wscript_len

# We tell lightningd to create, sign and broadcast this penalty tx: 
msgtype,onchaind_spend_penalty,5041
msgdata,onchaind_spend_penalty,outpoint,bitcoin_outpoint,
msgdata,onchaind_spend_penalty,outpoint_amount,amount_sat,
msgdata,onchaind_spend_penalty,remote_per_commitment_secret,secret,
msgdata,onchaind_spend_penalty,stack_elem_len,u16,
msgdata,onchaind_spend_penalty,stack_elem,u8,stack_elem_len
msgdata,onchaind_spend_penalty,wscript_len,u32,
msgdata,onchaind_spend_penalty,wscript,u8,wscript_len

# We tell lightningd to create, sign and broadcast this htlc_success tx: 
msgtype,onchaind_spend_htlc_success,5042
msgdata,onchaind_spend_htlc_success,outpoint,bitcoin_outpoint,
msgdata,onchaind_spend_htlc_success,outpoint_amount,amount_sat,
msgdata,onchaind_spend_htlc_success,fee,amount_sat,
msgdata,onchaind_spend_htlc_success,htlc_id,u64,
msgdata,onchaind_spend_htlc_success,commit_num,u64,
msgdata,onchaind_spend_htlc_success,remote_htlc_sig,bitcoin_signature,
msgdata,onchaind_spend_htlc_success,preimage,preimage,
msgdata,onchaind_spend_htlc_success,wscript_len,u32,
msgdata,onchaind_spend_htlc_success,wscript,u8,wscript_len
msgdata,onchaind_spend_htlc_success,htlc_wscript_len,u32,
msgdata,onchaind_spend_htlc_success,htlc_wscript,u8,htlc_wscript_len

# We tell lightningd to create, sign and broadcast this HTLC redepmtion: 
msgtype,onchaind_spend_fulfill,5043
msgdata,onchaind_spend_fulfill,outpoint,bitcoin_outpoint,
msgdata,onchaind_spend_fulfill,outpoint_amount,amount_sat,
msgdata,onchaind_spend_fulfill,htlc_id,u64,
msgdata,onchaind_spend_fulfill,remote_per_commitment_point,pubkey,
msgdata,onchaind_spend_fulfill,preimage,preimage,
msgdata,onchaind_spend_fulfill,wscript_len,u32,
msgdata,onchaind_spend_fulfill,wscript,u8,wscript_len

# We tell lightningd to create, sign and broadcast this htlc_timeout tx: 
msgtype,onchaind_spend_htlc_timeout,5044
msgdata,onchaind_spend_htlc_timeout,outpoint,bitcoin_outpoint,
msgdata,onchaind_spend_htlc_timeout,outpoint_amount,amount_sat,
msgdata,onchaind_spend_htlc_timeout,fee,amount_sat,
msgdata,onchaind_spend_htlc_timeout,htlc_id,u64,
msgdata,onchaind_spend_htlc_timeout,cltv_expiry,u32,
msgdata,onchaind_spend_htlc_timeout,commit_num,u64,
msgdata,onchaind_spend_htlc_timeout,remote_htlc_sig,bitcoin_signature,
msgdata,onchaind_spend_htlc_timeout,wscript_len,u32,
msgdata,onchaind_spend_htlc_timeout,wscript,u8,wscript_len
msgdata,onchaind_spend_htlc_timeout,htlc_wscript_len,u32,
msgdata,onchaind_spend_htlc_timeout,htlc_wscript,u8,htlc_wscript_len

# We tell lightningd to create, sign and broadcast this tx to collect our
# expired htlc in their unilateral close: 
msgtype,onchaind_spend_htlc_expired,5045
msgdata,onchaind_spend_htlc_expired,outpoint,bitcoin_outpoint,
msgdata,onchaind_spend_htlc_expired,outpoint_amount,amount_sat,
msgdata,onchaind_spend_htlc_expired,htlc_id,u64,
msgdata,onchaind_spend_htlc_expired,cltv_expiry,u32,
msgdata,onchaind_spend_htlc_expired,remote_per_commitment_point,pubkey,
msgdata,onchaind_spend_htlc_expired,wscript_len,u32,
msgdata,onchaind_spend_htlc_expired,wscript,u8,wscript_len

subtype,onchain_witness_element
subtypedata,onchain_witness_element,is_signature,bool,
subtypedata,onchain_witness_element,len,u32,
subtypedata,onchain_witness_element,witness,u8,len

# lightningd replies; if it considers it uneconomic, it tells onchaind
# so it doesn't wait forever!
msgtype,onchaind_spend_created,5140
msgdata,onchaind_spend_created,expect_to_succeed,bool,
msgdata,onchaind_spend_created,num_witnesses,u32,
msgdata,onchaind_spend_created,witness,onchain_witness_element,num_witnesses
