use std::collections::{BTreeMap, BTreeSet};
use bitcoin::hashes::sha256;
use bitcoin::secp256k1::schnorr::Signature;
use bitcoin::secp256k1::XOnlyPublicKey;
use bitcoin::{Amount, OutPoint, Txid, Witness};
use miniscript::Descriptor;
use crate::bifrost::ChannelId;
#[derive(Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Debug)]
#[derive(NetworkEncode, NetworkDecode)]
pub struct SegwitDescriptor(u8);
#[derive(Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Debug)]
#[derive(NetworkEncode, NetworkDecode)]
pub struct TaprootDescriptor(u8);
#[derive(Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Debug)]
#[derive(NetworkEncode, NetworkDecode)]
pub struct TaprootWitness(u8);
pub type ProtocolId = sha256::Hash;
pub type TxRole = u8;
pub type LinkId = u16;
pub const LN_TX_ROLE_FUNDING: u8 = 0x00;
pub const LN_TX_ROLE_REFUND: u8 = 0x02;
pub const LN_TX_ROLE_COMMITMENT: u8 = 0x02;
#[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Debug)]
#[derive(NetworkEncode, NetworkDecode)]
pub struct NodeSignature(pub XOnlyPublicKey, pub Signature);
#[derive(Clone, Ord, PartialOrd, Eq, PartialEq, Debug)]
#[derive(NetworkEncode, NetworkDecode)]
pub struct NodeSignatureMap(pub BTreeMap<XOnlyPublicKey, Signature>);
#[derive(Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Debug)]
#[derive(NetworkEncode, NetworkDecode)]
pub struct ChannelInput {
pub prev_outpoint: OutPoint,
pub sequence_no: u32,
pub descriptor: SegwitDescriptor,
pub witness: Option<Witness>,
}
#[derive(Clone, Ord, PartialOrd, Eq, PartialEq, Debug)]
#[derive(NetworkEncode, NetworkDecode)]
pub struct ChannelOutput {
pub output: Descriptor<secp256k1::PublicKey>,
pub p2c_tweaks: BTreeMap<ProtocolId, sha256::Hash>,
pub signature: NodeSignature,
}
#[derive(Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Debug)]
#[derive(NetworkEncode, NetworkDecode)]
pub enum ChannelFunding {
Blockchain(BTreeSet<ChannelInput>),
Channel {
channel_id: ChannelId,
actual_state: Txid,
funding: BTreeMap<u16, Option<TaprootWitness>>,
},
}
#[derive(Clone, Ord, PartialOrd, Eq, PartialEq, Debug)]
#[derive(NetworkEncode, NetworkDecode)]
pub struct ChannelLink {
pub amount: Amount,
pub link_id: LinkId,
pub descriptor: TaprootDescriptor,
pub sequence_no: u32,
pub witness: Option<TaprootWitness>,
pub p2c_tweaks: BTreeMap<ProtocolId, sha256::Hash>,
}
#[derive(Clone, Ord, PartialOrd, Eq, PartialEq, Debug)]
#[derive(NetworkEncode, NetworkDecode)]
pub struct FundingTx {
pub locktime: u32,
pub funding: ChannelFunding,
pub external_outputs: Vec<ChannelOutput>,
pub channel_output: ChannelLink,
}
#[derive(Clone, Ord, PartialOrd, Eq, PartialEq, Debug)]
#[derive(NetworkEncode, NetworkDecode)]
pub struct ChannelTx {
pub link_id: LinkId,
pub locktime: u32,
pub external_outputs: Vec<ChannelOutput>,
pub children: BTreeMap<TxRole, ChannelLink>,
}
pub struct RoleIter {}
pub struct ChannelIter {}
pub trait ChannelGraph {
fn tx_by_role(&self, role: TxRole) -> RoleIter;
fn iter(&self) -> ChannelIter;
fn funding_tx(&self) -> &ChannelTx;
fn refund_tx(&self) -> &ChannelTx;
}
#[derive(Clone, Ord, PartialOrd, Eq, PartialEq, Debug)]
#[derive(NetworkEncode, NetworkDecode)]
pub struct ChannelProposal {
channel: FundingTx,
pub signatures: NodeSignatureMap, index: Option<BTreeMap<TxRole, Vec<ChannelTx>>>,
}