kaccy-bitcoin 0.2.0

Bitcoin integration for Kaccy Protocol - HD wallets, UTXO management, and transaction building
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
//! BIP 21: URI Scheme for Bitcoin Payments
//!
//! This module implements BIP 21, which defines a URI scheme for making Bitcoin
//! payment requests. These URIs are commonly used in QR codes and payment links.
//!
//! # Features
//!
//! - Parse Bitcoin payment URIs
//! - Generate payment URIs with amount, label, and message
//! - Support for additional parameters
//! - Lightning invoice fallback support
//! - Network-aware address validation
//!
//! # URI Format
//!
//! ```text
//! bitcoin:<address>[?amount=<amount>][&label=<label>][&message=<message>]
//! ```
//!
//! # Example
//!
//! ```rust
//! use kaccy_bitcoin::bip21::{BitcoinUri, BitcoinUriBuilder};
//! use bitcoin::Network;
//!
//! # fn example() -> Result<(), Box<dyn std::error::Error>> {
//! // Create a payment URI
//! let uri = BitcoinUriBuilder::new("bc1qxy2kgdygjrsqtzq2n0yrf2493p83kkfjhx0wlh")
//!     .amount(100_000) // 0.001 BTC in satoshis
//!     .label("Donation to Alice")
//!     .message("Thank you for your support")
//!     .build()?;
//!
//! let uri_string = uri.to_string();
//! // bitcoin:bc1qxy2kgdygjrsqtzq2n0yrf2493p83kkfjhx0wlh?amount=0.00100000&label=Donation%20to%20Alice&message=Thank%20you%20for%20your%20support
//!
//! // Parse a URI
//! let parsed = BitcoinUri::parse(&uri_string)?;
//! assert_eq!(parsed.address, "bc1qxy2kgdygjrsqtzq2n0yrf2493p83kkfjhx0wlh");
//! assert_eq!(parsed.amount, Some(100_000));
//! # Ok(())
//! # }
//! ```

use crate::error::BitcoinError;
use bitcoin::{Address, Network};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fmt;
use std::str::FromStr;

/// Bitcoin payment URI according to BIP 21
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct BitcoinUri {
    /// Bitcoin address
    pub address: String,

    /// Amount in satoshis
    pub amount: Option<u64>,

    /// Human-readable label
    pub label: Option<String>,

    /// Human-readable message
    pub message: Option<String>,

    /// Additional parameters (e.g., for extensions)
    pub extras: HashMap<String, String>,
}

impl BitcoinUri {
    /// Create a new Bitcoin URI with just an address
    pub fn new(address: String) -> Self {
        Self {
            address,
            amount: None,
            label: None,
            message: None,
            extras: HashMap::new(),
        }
    }

    /// Parse a Bitcoin URI string
    ///
    /// # Example
    ///
    /// ```rust
    /// use kaccy_bitcoin::bip21::BitcoinUri;
    ///
    /// let uri = BitcoinUri::parse("bitcoin:bc1qxy2kgdygjrsqtzq2n0yrf2493p83kkfjhx0wlh?amount=0.001")?;
    /// # Ok::<(), kaccy_bitcoin::BitcoinError>(())
    /// ```
    pub fn parse(uri: &str) -> Result<Self, BitcoinError> {
        // Remove "bitcoin:" prefix if present
        let uri = if let Some(stripped) = uri.strip_prefix("bitcoin:") {
            stripped
        } else {
            uri
        };

        // Split address and query parameters
        let parts: Vec<&str> = uri.splitn(2, '?').collect();
        let address = parts[0].to_string();

        if address.is_empty() {
            return Err(BitcoinError::InvalidAddress("Empty address in URI".into()));
        }

        let mut parsed = Self::new(address);

        // Parse query parameters if present
        if parts.len() > 1 {
            let query = parts[1];
            for param in query.split('&') {
                let kv: Vec<&str> = param.splitn(2, '=').collect();
                if kv.len() != 2 {
                    continue;
                }

                let key = urlencoding::decode(kv[0])
                    .map_err(|e| BitcoinError::InvalidInput(format!("URL decode error: {}", e)))?
                    .to_string();
                let value = urlencoding::decode(kv[1])
                    .map_err(|e| BitcoinError::InvalidInput(format!("URL decode error: {}", e)))?
                    .to_string();

                match key.as_str() {
                    "amount" => {
                        // Amount is in BTC, convert to satoshis
                        let btc: f64 = value.parse().map_err(|_| {
                            BitcoinError::InvalidInput(format!("Invalid amount: {}", value))
                        })?;
                        parsed.amount = Some((btc * 100_000_000.0) as u64);
                    }
                    "label" => {
                        parsed.label = Some(value);
                    }
                    "message" => {
                        parsed.message = Some(value);
                    }
                    _ => {
                        // Store as extra parameter
                        parsed.extras.insert(key, value);
                    }
                }
            }
        }

        Ok(parsed)
    }

    /// Validate the address for a specific network
    pub fn validate_address(&self, network: Network) -> Result<Address, BitcoinError> {
        let address = Address::from_str(&self.address)
            .map_err(|e| BitcoinError::InvalidAddress(format!("Invalid address: {}", e)))?;

        let validated = address.require_network(network).map_err(|_| {
            BitcoinError::InvalidAddress(format!(
                "Address network does not match expected network: {:?}",
                network
            ))
        })?;

        Ok(validated)
    }

    /// Get the amount in BTC
    pub fn amount_btc(&self) -> Option<f64> {
        self.amount.map(|sats| sats as f64 / 100_000_000.0)
    }

    /// Get an extra parameter value
    pub fn get_extra(&self, key: &str) -> Option<&str> {
        self.extras.get(key).map(|s| s.as_str())
    }

    /// Check if this URI has a Lightning invoice fallback
    pub fn has_lightning_fallback(&self) -> bool {
        self.extras.contains_key("lightning")
    }

    /// Get the Lightning invoice if present
    pub fn lightning_invoice(&self) -> Option<&str> {
        self.get_extra("lightning")
    }
}

impl fmt::Display for BitcoinUri {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "bitcoin:{}", self.address)?;

        let mut params = Vec::new();

        if let Some(amount) = self.amount {
            let btc = amount as f64 / 100_000_000.0;
            params.push(format!("amount={:.8}", btc));
        }

        if let Some(ref label) = self.label {
            params.push(format!("label={}", urlencoding::encode(label)));
        }

        if let Some(ref message) = self.message {
            params.push(format!("message={}", urlencoding::encode(message)));
        }

        // Add extra parameters in sorted order for consistency
        let mut extra_keys: Vec<_> = self.extras.keys().collect();
        extra_keys.sort();
        for key in extra_keys {
            if let Some(value) = self.extras.get(key) {
                params.push(format!(
                    "{}={}",
                    urlencoding::encode(key),
                    urlencoding::encode(value)
                ));
            }
        }

        if !params.is_empty() {
            write!(f, "?{}", params.join("&"))?;
        }

        Ok(())
    }
}

/// Builder for creating Bitcoin URIs
pub struct BitcoinUriBuilder {
    uri: BitcoinUri,
}

impl BitcoinUriBuilder {
    /// Create a new URI builder with an address
    pub fn new(address: impl Into<String>) -> Self {
        Self {
            uri: BitcoinUri::new(address.into()),
        }
    }

    /// Set the payment amount in satoshis
    pub fn amount(mut self, satoshis: u64) -> Self {
        self.uri.amount = Some(satoshis);
        self
    }

    /// Set the payment amount in BTC
    pub fn amount_btc(mut self, btc: f64) -> Self {
        self.uri.amount = Some((btc * 100_000_000.0) as u64);
        self
    }

    /// Set the label
    pub fn label(mut self, label: impl Into<String>) -> Self {
        self.uri.label = Some(label.into());
        self
    }

    /// Set the message
    pub fn message(mut self, message: impl Into<String>) -> Self {
        self.uri.message = Some(message.into());
        self
    }

    /// Add a custom parameter
    pub fn extra(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
        self.uri.extras.insert(key.into(), value.into());
        self
    }

    /// Add a Lightning invoice fallback
    pub fn lightning(mut self, invoice: impl Into<String>) -> Self {
        self.uri
            .extras
            .insert("lightning".to_string(), invoice.into());
        self
    }

    /// Build the URI
    pub fn build(self) -> Result<BitcoinUri, BitcoinError> {
        // Validate that the address is not empty
        if self.uri.address.is_empty() {
            return Err(BitcoinError::InvalidAddress(
                "Address cannot be empty".into(),
            ));
        }

        Ok(self.uri)
    }
}

/// QR code generation helper for Bitcoin URIs
pub struct QrCodeHelper;

impl QrCodeHelper {
    /// Get the recommended QR code error correction level
    ///
    /// BIP 21 recommends using Medium (M) level
    pub fn recommended_error_correction() -> &'static str {
        "M" // Medium: ~15% error correction
    }

    /// Estimate QR code size needed for a URI
    ///
    /// Returns the recommended QR code version (1-40)
    pub fn estimate_qr_version(uri: &BitcoinUri) -> u8 {
        let uri_string = uri.to_string();
        let len = uri_string.len();

        // Rough estimation based on URI length
        // QR version 1 can hold ~25 alphanumeric chars
        // Each version adds ~4 chars capacity
        ((len as f64 / 25.0).ceil() as u8).clamp(1, 40)
    }

    /// Check if a URI is suitable for QR code
    ///
    /// Returns true if the URI can fit in a reasonable QR code size
    pub fn is_qr_friendly(uri: &BitcoinUri) -> bool {
        let uri_string = uri.to_string();
        // QR codes become hard to scan above version 20 (~470 chars)
        uri_string.len() <= 470
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_simple_uri() {
        let uri = BitcoinUri::new("bc1qxy2kgdygjrsqtzq2n0yrf2493p83kkfjhx0wlh".to_string());
        let uri_string = uri.to_string();
        assert_eq!(
            uri_string,
            "bitcoin:bc1qxy2kgdygjrsqtzq2n0yrf2493p83kkfjhx0wlh"
        );
    }

    #[test]
    fn test_uri_with_amount() {
        let uri = BitcoinUriBuilder::new("bc1qxy2kgdygjrsqtzq2n0yrf2493p83kkfjhx0wlh")
            .amount(100_000)
            .build()
            .unwrap();

        let uri_string = uri.to_string();
        assert!(uri_string.contains("amount=0.00100000"));
    }

    #[test]
    fn test_uri_with_all_fields() {
        let uri = BitcoinUriBuilder::new("bc1qxy2kgdygjrsqtzq2n0yrf2493p83kkfjhx0wlh")
            .amount(100_000)
            .label("Donation")
            .message("Thank you")
            .build()
            .unwrap();

        let uri_string = uri.to_string();
        assert!(uri_string.contains("amount=0.00100000"));
        assert!(uri_string.contains("label=Donation"));
        assert!(uri_string.contains("message=Thank%20you"));
    }

    #[test]
    fn test_parse_simple_uri() {
        let uri = BitcoinUri::parse("bitcoin:bc1qxy2kgdygjrsqtzq2n0yrf2493p83kkfjhx0wlh").unwrap();
        assert_eq!(uri.address, "bc1qxy2kgdygjrsqtzq2n0yrf2493p83kkfjhx0wlh");
        assert_eq!(uri.amount, None);
        assert_eq!(uri.label, None);
    }

    #[test]
    fn test_parse_uri_with_amount() {
        let uri =
            BitcoinUri::parse("bitcoin:bc1qxy2kgdygjrsqtzq2n0yrf2493p83kkfjhx0wlh?amount=0.001")
                .unwrap();
        assert_eq!(uri.amount, Some(100_000));
        assert_eq!(uri.amount_btc(), Some(0.001));
    }

    #[test]
    fn test_parse_uri_with_all_fields() {
        let uri = BitcoinUri::parse(
            "bitcoin:bc1qxy2kgdygjrsqtzq2n0yrf2493p83kkfjhx0wlh?amount=0.001&label=Donation&message=Thank%20you",
        )
        .unwrap();
        assert_eq!(uri.address, "bc1qxy2kgdygjrsqtzq2n0yrf2493p83kkfjhx0wlh");
        assert_eq!(uri.amount, Some(100_000));
        assert_eq!(uri.label, Some("Donation".to_string()));
        assert_eq!(uri.message, Some("Thank you".to_string()));
    }

    #[test]
    fn test_parse_without_prefix() {
        let uri =
            BitcoinUri::parse("bc1qxy2kgdygjrsqtzq2n0yrf2493p83kkfjhx0wlh?amount=0.001").unwrap();
        assert_eq!(uri.address, "bc1qxy2kgdygjrsqtzq2n0yrf2493p83kkfjhx0wlh");
        assert_eq!(uri.amount, Some(100_000));
    }

    #[test]
    fn test_extra_parameters() {
        let uri = BitcoinUriBuilder::new("bc1qxy2kgdygjrsqtzq2n0yrf2493p83kkfjhx0wlh")
            .extra("req-payment", "xyz123")
            .build()
            .unwrap();

        assert_eq!(uri.get_extra("req-payment"), Some("xyz123"));
    }

    #[test]
    fn test_lightning_fallback() {
        let uri = BitcoinUriBuilder::new("bc1qxy2kgdygjrsqtzq2n0yrf2493p83kkfjhx0wlh")
            .lightning("lnbc1...")
            .build()
            .unwrap();

        assert!(uri.has_lightning_fallback());
        assert_eq!(uri.lightning_invoice(), Some("lnbc1..."));
    }

    #[test]
    fn test_roundtrip() {
        let original = BitcoinUriBuilder::new("bc1qxy2kgdygjrsqtzq2n0yrf2493p83kkfjhx0wlh")
            .amount(100_000)
            .label("Test Label")
            .message("Test Message")
            .build()
            .unwrap();

        let uri_string = original.to_string();
        let parsed = BitcoinUri::parse(&uri_string).unwrap();

        assert_eq!(parsed.address, original.address);
        assert_eq!(parsed.amount, original.amount);
        assert_eq!(parsed.label, original.label);
        assert_eq!(parsed.message, original.message);
    }

    #[test]
    fn test_qr_helper() {
        let uri = BitcoinUri::new("bc1qxy2kgdygjrsqtzq2n0yrf2493p83kkfjhx0wlh".to_string());
        assert!(QrCodeHelper::is_qr_friendly(&uri));

        let version = QrCodeHelper::estimate_qr_version(&uri);
        assert!(version > 0 && version <= 40);
    }

    #[test]
    fn test_url_encoding() {
        let uri = BitcoinUriBuilder::new("bc1qxy2kgdygjrsqtzq2n0yrf2493p83kkfjhx0wlh")
            .label("Donation & Support")
            .message("Thank you! 🎉")
            .build()
            .unwrap();

        let uri_string = uri.to_string();
        assert!(uri_string.contains("Donation%20%26%20Support"));

        let parsed = BitcoinUri::parse(&uri_string).unwrap();
        assert_eq!(parsed.label, Some("Donation & Support".to_string()));
        assert_eq!(parsed.message, Some("Thank you! 🎉".to_string()));
    }

    #[test]
    fn test_empty_address_error() {
        let result = BitcoinUriBuilder::new("").build();
        assert!(result.is_err());
    }
}