use std::fmt::Debug;
use std::io;
use std::marker::PhantomData;
use ecdsa_fun::adaptor::EncryptedSignature;
use crate::blockchain::Network;
use crate::consensus::{self, CanonicalBytes, Decodable, Encodable};
pub(crate) mod address;
pub(crate) mod amount;
pub mod fee;
#[cfg(feature = "experimental")]
#[cfg_attr(docsrs, doc(cfg(feature = "experimental")))]
pub mod segwitv0;
#[cfg(all(feature = "experimental", feature = "taproot"))]
#[cfg_attr(docsrs, doc(cfg(all(feature = "experimental", feature = "taproot"))))]
pub mod taproot;
pub mod timelock;
pub mod transaction;
#[cfg(feature = "experimental")]
#[cfg_attr(docsrs, doc(cfg(feature = "experimental")))]
pub type BitcoinSegwitV0 = Bitcoin<segwitv0::SegwitV0>;
#[cfg(feature = "experimental")]
#[cfg_attr(docsrs, doc(cfg(feature = "experimental")))]
impl Decodable for BitcoinSegwitV0 {
fn consensus_decode<D: io::Read>(d: &mut D) -> Result<Self, consensus::Error> {
match Decodable::consensus_decode(d)? {
0x80000000u32 => Ok(Self::new()),
_ => Err(consensus::Error::UnknownType),
}
}
}
#[cfg(feature = "experimental")]
#[cfg_attr(docsrs, doc(cfg(feature = "experimental")))]
impl Encodable for BitcoinSegwitV0 {
fn consensus_encode<W: io::Write>(&self, writer: &mut W) -> Result<usize, io::Error> {
0x80000000u32.consensus_encode(writer)
}
}
#[cfg(all(feature = "experimental", feature = "taproot"))]
#[cfg_attr(docsrs, doc(cfg(all(feature = "experimental", feature = "taproot"))))]
pub type BitcoinTaproot = Bitcoin<taproot::Taproot>;
#[non_exhaustive]
pub enum Btc {
#[cfg(feature = "experimental")]
#[cfg_attr(docsrs, doc(cfg(feature = "experimental")))]
SegwitV0(BitcoinSegwitV0),
#[cfg(all(feature = "experimental", feature = "taproot"))]
#[cfg_attr(docsrs, doc(cfg(all(feature = "experimental", feature = "taproot"))))]
Taproot(BitcoinTaproot),
}
pub trait Strategy: Clone + Copy + Debug {}
#[derive(Clone, Debug, Copy, Eq, PartialEq)]
pub struct Bitcoin<S: Strategy> {
_e: PhantomData<S>,
}
impl<S: Strategy> Bitcoin<S> {
pub fn new() -> Self {
Self { _e: PhantomData }
}
}
impl<S: Strategy> Default for Bitcoin<S> {
fn default() -> Self {
Self::new()
}
}
impl From<Network> for bitcoin::Network {
fn from(network: Network) -> Self {
match network {
Network::Mainnet => Self::Bitcoin,
Network::Testnet => Self::Testnet,
Network::Local => Self::Regtest,
}
}
}
impl From<bitcoin::Network> for Network {
fn from(network: bitcoin::Network) -> Self {
match network {
bitcoin::Network::Bitcoin => Self::Mainnet,
bitcoin::Network::Testnet => Self::Testnet,
bitcoin::Network::Signet => Self::Testnet,
bitcoin::Network::Regtest => Self::Local,
}
}
}
impl Encodable for EncryptedSignature {
fn consensus_encode<W: io::Write>(&self, writer: &mut W) -> Result<usize, io::Error> {
self.as_canonical_bytes().consensus_encode(writer)
}
}
impl Decodable for EncryptedSignature {
fn consensus_decode<D: io::Read>(d: &mut D) -> Result<Self, consensus::Error> {
Self::from_canonical_bytes(unwrap_vec_ref!(d).as_ref())
}
}