nil_zonefile/
wallet.rs

1use serde::{Deserialize, Serialize};
2use nil_slip44::{Coin, Symbol};
3#[allow(unused)]
4use log;
5
6#[derive(Debug, Clone, PartialEq, Eq, Hash)]
7pub enum WalletType {
8    // For on-chain addresses
9    OnChain(Coin),
10    // For Lightning Network addresses
11    Lightning,
12}
13
14// Custom serialization to use coin IDs
15impl Serialize for WalletType {
16    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
17    where
18        S: serde::Serializer,
19    {
20        match self {
21            WalletType::OnChain(coin) => serializer.serialize_u32(coin.id()),
22            WalletType::Lightning => serializer.serialize_str("lightning"),
23        }
24    }
25}
26
27// Custom deserialization to handle both coin IDs and "lightning"
28impl<'de> Deserialize<'de> for WalletType {
29    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
30    where
31        D: serde::Deserializer<'de>,
32    {
33        use serde::de::Error;
34        
35        // Try to deserialize as either a number or string
36        #[derive(Deserialize)]
37        #[serde(untagged)]
38        enum IdOrString {
39            Id(u32),
40            String(String),
41        }
42
43        match IdOrString::deserialize(deserializer)? {
44            IdOrString::Id(id) => {
45                // Convert coin ID back to Coin
46                if let Ok(coin) = Coin::try_from(id) {
47                    Ok(WalletType::OnChain(coin))
48                } else {
49                    Err(Error::custom(format!("Invalid coin ID: {}", id)))
50                }
51            }
52            IdOrString::String(s) => {
53                // Check for Lightning first
54                if s.to_lowercase() == "lightning" {
55                    return Ok(WalletType::Lightning);
56                }
57                
58                // Try parsing string as a number
59                if let Ok(id) = s.parse::<u32>() {
60                    if let Ok(coin) = Coin::try_from(id) {
61                        return Ok(WalletType::OnChain(coin));
62                    }
63                }
64                
65                // Try parsing as Symbol
66                if let Ok(symbol) = s.parse::<Symbol>() {
67                    return Ok(WalletType::OnChain(Coin::from(symbol)));
68                }
69                
70                Err(Error::custom(format!("Invalid wallet type string: {}", s)))
71            }
72        }
73    }
74}
75
76impl WalletType {
77    /// Create a Bitcoin wallet type
78    pub fn bitcoin() -> Self {
79        WalletType::OnChain(Coin::Bitcoin)
80    }
81
82    /// Create a Stacks wallet type
83    pub fn stacks() -> Self {
84        WalletType::OnChain(Coin::Stacks)
85    }
86
87    /// Create a Solana wallet type
88    pub fn solana() -> Self {
89        WalletType::OnChain(Coin::Solana)
90    }
91
92    /// Create an Ethereum wallet type
93    pub fn ethereum() -> Self {
94        WalletType::OnChain(Coin::Ethereum)
95    }
96
97    /// Create a Lightning Network wallet type
98    pub fn lightning() -> Self {
99        WalletType::Lightning
100    }
101
102    pub fn from_str(s: &str) -> Option<Self> {
103        // Try parsing as SLIP44 symbol first for on-chain addresses
104        if let Ok(symbol) = s.parse::<Symbol>() {
105            return Some(WalletType::OnChain(Coin::from(symbol)));
106        }
107
108        // Check for Lightning
109        match s.to_lowercase().as_str() {
110            "lightning" => Some(WalletType::Lightning),
111            _ => None,
112        }
113    }
114
115    pub fn to_string(&self) -> String {
116        match self {
117            WalletType::OnChain(coin) => coin.to_string(),
118            WalletType::Lightning => "lightning".to_string(),
119        }
120    }
121}
122
123#[derive(Debug, Serialize, Deserialize, Clone)]
124pub struct Wallet {
125    pub address: String,
126    pub coin_type: WalletType,
127}
128
129#[cfg(test)]
130mod tests {
131    use serde_json::json;
132    use super::*;
133    use log::{debug};
134
135    #[test]
136    fn test_wallet_type_from_str() {
137        // Test on-chain addresses
138        assert_eq!(WalletType::from_str("BTC"), Some(WalletType::OnChain(Coin::Bitcoin)));
139        assert_eq!(WalletType::from_str("STX"), Some(WalletType::OnChain(Coin::Stacks)));
140        assert_eq!(WalletType::from_str("SOL"), Some(WalletType::OnChain(Coin::Solana)));
141        assert_eq!(WalletType::from_str("ETH"), Some(WalletType::OnChain(Coin::Ethereum)));
142        
143        // Test Lightning Network
144        assert_eq!(WalletType::from_str("lightning"), Some(WalletType::Lightning));
145        assert_eq!(WalletType::from_str("LIGHTNING"), Some(WalletType::Lightning));
146        
147        // Test invalid input
148        assert_eq!(WalletType::from_str("INVALID"), None);
149    }
150
151    #[test]
152    fn test_wallet_type_to_string() {
153        assert_eq!(WalletType::OnChain(Coin::Bitcoin).to_string(), "Bitcoin");
154        assert_eq!(WalletType::OnChain(Coin::Solana).to_string(), "Solana");
155        assert_eq!(WalletType::Lightning.to_string(), "lightning");
156    }
157
158    #[test]
159    fn test_wallet_serialization() {
160        let wallet = Wallet {
161            address: "bc1qxxx...".to_string(),
162            coin_type: WalletType::OnChain(Coin::Bitcoin),
163        };
164        
165        let json = serde_json::to_string(&wallet).unwrap();
166        debug!("Bitcoin wallet JSON: {}", json);
167        let deserialized: Wallet = serde_json::from_str(&json).unwrap();
168        
169        assert_eq!(wallet.address, deserialized.address);
170        assert_eq!(wallet.coin_type, deserialized.coin_type);
171
172        let lightning_wallet = Wallet {
173            address: "[email protected]".to_string(),
174            coin_type: WalletType::Lightning,
175        };
176        
177        let json = serde_json::to_string(&lightning_wallet).unwrap();
178        debug!("Lightning wallet JSON: {}", json);
179        let deserialized: Wallet = serde_json::from_str(&json).unwrap();
180        
181        assert_eq!(lightning_wallet.address, deserialized.address);
182        assert_eq!(lightning_wallet.coin_type, deserialized.coin_type);
183    }
184
185    #[test]
186    fn test_coin_id_serialization() {
187        // Test that Bitcoin serializes to ID 0
188        let btc_wallet = WalletType::OnChain(Coin::Bitcoin);
189        let json = serde_json::to_string(&btc_wallet).unwrap();
190        assert_eq!(json, "0");
191
192        // Test that Stacks serializes to ID 5757
193        let stx_wallet = WalletType::OnChain(Coin::Stacks);
194        let json = serde_json::to_string(&stx_wallet).unwrap();
195        assert_eq!(json, "5757");
196
197        // Test that Solana serializes to ID 501
198        let sol_wallet = WalletType::OnChain(Coin::Solana);
199        let json = serde_json::to_string(&sol_wallet).unwrap();
200        assert_eq!(json, "501");
201
202        // Test that Ethereum serializes to ID 60
203        let eth_wallet = WalletType::OnChain(Coin::Ethereum);
204        let json = serde_json::to_string(&eth_wallet).unwrap();
205        assert_eq!(json, "60");
206
207        // Test deserialization from IDs
208        let deserialized: WalletType = serde_json::from_str("0").unwrap();
209        assert_eq!(deserialized, WalletType::OnChain(Coin::Bitcoin));
210
211        let deserialized: WalletType = serde_json::from_str("5757").unwrap();
212        assert_eq!(deserialized, WalletType::OnChain(Coin::Stacks));
213
214        let deserialized: WalletType = serde_json::from_str("501").unwrap();
215        assert_eq!(deserialized, WalletType::OnChain(Coin::Solana));
216
217        let deserialized: WalletType = serde_json::from_str("60").unwrap();
218        assert_eq!(deserialized, WalletType::OnChain(Coin::Ethereum));
219
220        // Test deserialization from string IDs
221        let deserialized: WalletType = serde_json::from_str("\"0\"").unwrap();
222        assert_eq!(deserialized, WalletType::OnChain(Coin::Bitcoin));
223
224        let deserialized: WalletType = serde_json::from_str("\"5757\"").unwrap();
225        assert_eq!(deserialized, WalletType::OnChain(Coin::Stacks));
226
227        let deserialized: WalletType = serde_json::from_str("\"501\"").unwrap();
228        assert_eq!(deserialized, WalletType::OnChain(Coin::Solana));
229
230        let deserialized: WalletType = serde_json::from_str("\"60\"").unwrap();
231        assert_eq!(deserialized, WalletType::OnChain(Coin::Ethereum));
232
233        // Test deserialization from symbols
234        let deserialized: WalletType = serde_json::from_str("\"BTC\"").unwrap();
235        assert_eq!(deserialized, WalletType::OnChain(Coin::Bitcoin));
236
237        let deserialized: WalletType = serde_json::from_str("\"STX\"").unwrap();
238        assert_eq!(deserialized, WalletType::OnChain(Coin::Stacks));
239
240        let deserialized: WalletType = serde_json::from_str("\"SOL\"").unwrap();
241        assert_eq!(deserialized, WalletType::OnChain(Coin::Solana));
242
243        let deserialized: WalletType = serde_json::from_str("\"ETH\"").unwrap();
244        assert_eq!(deserialized, WalletType::OnChain(Coin::Ethereum));
245    }
246
247    #[test]
248    fn test_onchain_numeric_serialization() {
249        // Test that OnChain types serialize as numbers, not strings
250        let btc_wallet = WalletType::OnChain(Coin::Bitcoin);
251        let stx_wallet = WalletType::OnChain(Coin::Stacks);
252        let sol_wallet = WalletType::OnChain(Coin::Solana);
253
254        // Verify serialization produces numbers
255        assert_eq!(serde_json::to_value(&btc_wallet).unwrap(), json!(0));
256        assert_eq!(serde_json::to_value(&stx_wallet).unwrap(), json!(5757));
257        assert_eq!(serde_json::to_value(&sol_wallet).unwrap(), json!(501));
258
259        // Verify serialization does NOT produce strings
260        assert_ne!(serde_json::to_value(&btc_wallet).unwrap(), json!("0"));
261        assert_ne!(serde_json::to_value(&stx_wallet).unwrap(), json!("5757"));
262        assert_ne!(serde_json::to_value(&sol_wallet).unwrap(), json!("501"));
263
264        // Verify the actual JSON string output
265        assert_eq!(serde_json::to_string(&btc_wallet).unwrap(), "0");
266        assert_eq!(serde_json::to_string(&stx_wallet).unwrap(), "5757");
267        assert_eq!(serde_json::to_string(&sol_wallet).unwrap(), "501");
268        assert_ne!(serde_json::to_string(&btc_wallet).unwrap(), "\"0\"");
269        assert_ne!(serde_json::to_string(&stx_wallet).unwrap(), "\"5757\"");
270        assert_ne!(serde_json::to_string(&sol_wallet).unwrap(), "\"501\"");
271    }
272
273    #[test]
274    fn test_convenience_constructors() {
275        // Test that convenience constructors create the correct types
276        assert_eq!(WalletType::bitcoin(), WalletType::OnChain(Coin::Bitcoin));
277        assert_eq!(WalletType::stacks(), WalletType::OnChain(Coin::Stacks));
278        assert_eq!(WalletType::solana(), WalletType::OnChain(Coin::Solana));
279        assert_eq!(WalletType::ethereum(), WalletType::OnChain(Coin::Ethereum));
280        assert_eq!(WalletType::lightning(), WalletType::Lightning);
281
282        // Test that they serialize to the correct IDs
283        assert_eq!(serde_json::to_string(&WalletType::bitcoin()).unwrap(), "0");
284        assert_eq!(serde_json::to_string(&WalletType::stacks()).unwrap(), "5757");
285        assert_eq!(serde_json::to_string(&WalletType::solana()).unwrap(), "501");
286        assert_eq!(serde_json::to_string(&WalletType::ethereum()).unwrap(), "60");
287        assert_eq!(serde_json::to_string(&WalletType::lightning()).unwrap(), "\"lightning\"");
288    }
289}
290
291
292
OSZAR »