anya_core/bitcoin/protocol/
mod.rs1use serde::{Deserialize, Serialize};
7use std::fmt;
8
9pub mod testing;
10
11#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
13pub enum BPCLevel {
14 None = 0,
16 Basic = 1,
18 Enhanced = 2,
20 #[default]
22 Full = 3,
23 BPC3 = 4,
25}
26
27impl fmt::Display for BPCLevel {
28 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
29 match self {
30 BPCLevel::None => write!(f, "None"),
31 BPCLevel::Basic => write!(f, "Basic"),
32 BPCLevel::Enhanced => write!(f, "Enhanced"),
33 BPCLevel::Full => write!(f, "Full"),
34 BPCLevel::BPC3 => write!(f, "BPC3"),
35 }
36 }
37}
38
39#[derive(Debug, Default, Clone)]
41pub struct BitcoinProtocol {
42 pub level: BPCLevel,
44 pub supported_bips: Vec<u32>,
46}
47
48impl BitcoinProtocol {
49 pub fn new() -> Self {
51 Self {
52 level: BPCLevel::Full,
53 supported_bips: vec![341, 342, 174, 370, 340], }
55 }
56
57 pub fn validate_compliance(&self) -> Result<bool, Box<dyn std::error::Error>> {
59 let required_bips = vec![341, 342, 174, 340]; for bip in required_bips {
63 if !self.supported_bips.contains(&bip) {
64 return Err(format!("Missing required BIP-{bip}").into());
65 }
66 }
67
68 Ok(true)
69 }
70
71 pub fn get_info(&self) -> ProtocolInfo {
73 ProtocolInfo {
74 level: self.level,
75 supported_bips: self.supported_bips.clone(),
76 features: vec![
77 "Taproot (BIP-341)".to_string(),
78 "Tapscript (BIP-342)".to_string(),
79 "PSBT v1/v2 (BIP-174/370)".to_string(),
80 "Schnorr Signatures (BIP-340)".to_string(),
81 ],
82 }
83 }
84
85 pub fn validate_transaction(
87 &self,
88 tx: &bitcoin::Transaction,
89 ) -> Result<(), crate::bitcoin::error::BitcoinError> {
90 if tx.output.is_empty() {
92 return Err(crate::bitcoin::error::BitcoinError::ValidationError(
93 "Transaction has no outputs".to_string(),
94 ));
95 }
96
97 Ok(())
99 }
100
101 pub fn is_taproot_enabled(&self) -> bool {
103 self.supported_bips.contains(&341) }
105
106 pub fn get_level(&self) -> BPCLevel {
108 self.level
109 }
110}
111
112#[derive(Debug, Clone, Serialize, Deserialize)]
114pub struct ProtocolInfo {
115 pub level: BPCLevel,
116 pub supported_bips: Vec<u32>,
117 pub features: Vec<String>,
118}