rustywallet-batch 0.3.0

High-performance batch key and address generation for cryptocurrency wallets
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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
//! Batch address generation.
//!
//! This module provides [`BatchAddressGenerator`] for generating large batches
//! of addresses efficiently with parallel processing and streaming output.
//!
//! ## Supported Address Types
//!
//! - **P2PKH** - Legacy Bitcoin addresses (1...)
//! - **P2WPKH** - SegWit Bitcoin addresses (bc1q...)
//! - **P2TR** - Taproot Bitcoin addresses (bc1p...)
//!
//! ## Example
//!
//! ```rust
//! use rustywallet_batch::address::{BatchAddressGenerator, BatchAddressType};
//! use rustywallet_address::Network;
//!
//! // Generate 1000 P2WPKH addresses
//! let generator = BatchAddressGenerator::new(BatchAddressType::P2WPKH, Network::BitcoinMainnet);
//! let addresses: Vec<_> = generator.generate_stream(1000).take(10).collect();
//!
//! for (key, addr) in addresses {
//!     println!("{}: {}", key.to_hex(), addr);
//! }
//! ```

use crate::error::BatchError;
use rayon::prelude::*;
use rustywallet_address::{Network, P2PKHAddress, P2TRAddress, P2WPKHAddress};
use rustywallet_keys::private_key::PrivateKey;

/// Supported address types for batch generation.
///
/// This enum defines the Bitcoin address types that can be generated
/// in batch operations.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum BatchAddressType {
    /// Pay-to-Public-Key-Hash (Legacy) - addresses starting with `1` (mainnet)
    P2PKH,
    /// Pay-to-Witness-Public-Key-Hash (SegWit) - addresses starting with `bc1q` (mainnet)
    P2WPKH,
    /// Pay-to-Taproot - addresses starting with `bc1p` (mainnet)
    P2TR,
}

impl BatchAddressType {
    /// Returns the address prefix for this type on mainnet.
    pub fn mainnet_prefix(&self) -> &'static str {
        match self {
            BatchAddressType::P2PKH => "1",
            BatchAddressType::P2WPKH => "bc1q",
            BatchAddressType::P2TR => "bc1p",
        }
    }

    /// Returns the address prefix for this type on testnet.
    pub fn testnet_prefix(&self) -> &'static str {
        match self {
            BatchAddressType::P2PKH => "m/n",
            BatchAddressType::P2WPKH => "tb1q",
            BatchAddressType::P2TR => "tb1p",
        }
    }
}

impl std::fmt::Display for BatchAddressType {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            BatchAddressType::P2PKH => write!(f, "P2PKH"),
            BatchAddressType::P2WPKH => write!(f, "P2WPKH"),
            BatchAddressType::P2TR => write!(f, "P2TR"),
        }
    }
}


/// Batch address generator for efficient address generation.
///
/// `BatchAddressGenerator` provides a high-performance API for generating
/// large batches of addresses with parallel processing and streaming output.
///
/// # Example
///
/// ```rust
/// use rustywallet_batch::address::{BatchAddressGenerator, BatchAddressType};
/// use rustywallet_address::Network;
///
/// // Create generator for P2WPKH addresses on mainnet
/// let generator = BatchAddressGenerator::new(BatchAddressType::P2WPKH, Network::BitcoinMainnet);
///
/// // Generate 100 addresses as a vector
/// let addresses = generator.generate_vec(100).unwrap();
/// assert_eq!(addresses.len(), 100);
///
/// // Or stream addresses for memory efficiency
/// for (key, addr) in generator.generate_stream(1000).take(10) {
///     println!("{}: {}", key.to_hex(), addr);
/// }
/// ```
#[derive(Debug, Clone)]
pub struct BatchAddressGenerator {
    address_type: BatchAddressType,
    network: Network,
    parallel: bool,
    chunk_size: usize,
}

impl BatchAddressGenerator {
    /// Create a new batch address generator.
    ///
    /// # Arguments
    ///
    /// * `address_type` - The type of addresses to generate (P2PKH, P2WPKH, P2TR)
    /// * `network` - The Bitcoin network (mainnet or testnet)
    ///
    /// # Example
    ///
    /// ```rust
    /// use rustywallet_batch::address::{BatchAddressGenerator, BatchAddressType};
    /// use rustywallet_address::Network;
    ///
    /// let generator = BatchAddressGenerator::new(BatchAddressType::P2TR, Network::BitcoinMainnet);
    /// ```
    pub fn new(address_type: BatchAddressType, network: Network) -> Self {
        Self {
            address_type,
            network,
            parallel: true, // Default to parallel for performance
            chunk_size: 1000,
        }
    }

    /// Enable or disable parallel processing.
    ///
    /// Parallel processing is enabled by default for better performance.
    pub fn parallel(mut self, enabled: bool) -> Self {
        self.parallel = enabled;
        self
    }

    /// Set the chunk size for streaming operations.
    ///
    /// Larger chunks improve throughput but use more memory.
    /// Default is 1000.
    pub fn chunk_size(mut self, size: usize) -> Self {
        self.chunk_size = size;
        self
    }

    /// Get the address type.
    #[inline]
    pub fn address_type(&self) -> BatchAddressType {
        self.address_type
    }

    /// Get the network.
    #[inline]
    pub fn network(&self) -> Network {
        self.network
    }

    /// Generate addresses as a streaming iterator.
    ///
    /// This method returns an iterator that generates key-address pairs on-demand,
    /// allowing processing of millions of addresses without memory exhaustion.
    ///
    /// # Arguments
    ///
    /// * `count` - The number of addresses to generate
    ///
    /// # Example
    ///
    /// ```rust
    /// use rustywallet_batch::address::{BatchAddressGenerator, BatchAddressType};
    /// use rustywallet_address::Network;
    ///
    /// let generator = BatchAddressGenerator::new(BatchAddressType::P2WPKH, Network::BitcoinMainnet);
    ///
    /// // Stream 1 million addresses without storing all in memory
    /// for (key, addr) in generator.generate_stream(1_000_000).take(100) {
    ///     println!("{}", addr);
    /// }
    /// ```
    pub fn generate_stream(&self, count: usize) -> AddressStream {
        AddressStream::new(
            self.address_type,
            self.network,
            count,
            self.parallel,
            self.chunk_size,
        )
    }

    /// Generate addresses and collect them into a vector.
    ///
    /// This method generates all addresses and stores them in memory.
    /// For large batches, consider using `generate_stream()` for streaming.
    ///
    /// # Arguments
    ///
    /// * `count` - The number of addresses to generate
    ///
    /// # Example
    ///
    /// ```rust
    /// use rustywallet_batch::address::{BatchAddressGenerator, BatchAddressType};
    /// use rustywallet_address::Network;
    ///
    /// let generator = BatchAddressGenerator::new(BatchAddressType::P2PKH, Network::BitcoinMainnet);
    /// let addresses = generator.generate_vec(1000).unwrap();
    ///
    /// for (key, addr) in &addresses {
    ///     assert!(addr.starts_with('1'));
    /// }
    /// ```
    pub fn generate_vec(&self, count: usize) -> Result<Vec<(PrivateKey, String)>, BatchError> {
        if !self.network.is_bitcoin() {
            return Err(BatchError::invalid_config(format!(
                "Network {} is not supported for Bitcoin address generation",
                self.network
            )));
        }

        if self.parallel {
            self.generate_parallel_vec(count)
        } else {
            self.generate_sequential_vec(count)
        }
    }

    /// Generate a single key-address pair.
    fn generate_single(&self) -> Result<(PrivateKey, String), BatchError> {
        let key = PrivateKey::random();
        let pubkey = key.public_key();

        let address = match self.address_type {
            BatchAddressType::P2PKH => {
                P2PKHAddress::from_public_key(&pubkey, self.network)
                    .map_err(|e| BatchError::generation_error(e.to_string()))?
                    .to_string()
            }
            BatchAddressType::P2WPKH => {
                P2WPKHAddress::from_public_key(&pubkey, self.network)
                    .map_err(|e| BatchError::generation_error(e.to_string()))?
                    .to_string()
            }
            BatchAddressType::P2TR => {
                P2TRAddress::from_public_key(&pubkey, self.network)
                    .map_err(|e| BatchError::generation_error(e.to_string()))?
                    .to_string()
            }
        };

        Ok((key, address))
    }

    /// Generate addresses sequentially.
    fn generate_sequential_vec(&self, count: usize) -> Result<Vec<(PrivateKey, String)>, BatchError> {
        (0..count)
            .map(|_| self.generate_single())
            .collect()
    }

    /// Generate addresses in parallel.
    fn generate_parallel_vec(&self, count: usize) -> Result<Vec<(PrivateKey, String)>, BatchError> {
        let address_type = self.address_type;
        let network = self.network;

        let results: Vec<_> = (0..count)
            .into_par_iter()
            .map(|_| generate_address_pair(address_type, network))
            .collect();

        // Check for errors
        results.into_iter().collect()
    }
}

/// Generate a single address pair (used in parallel context).
fn generate_address_pair(
    address_type: BatchAddressType,
    network: Network,
) -> Result<(PrivateKey, String), BatchError> {
    let key = PrivateKey::random();
    let pubkey = key.public_key();

    let address = match address_type {
        BatchAddressType::P2PKH => {
            P2PKHAddress::from_public_key(&pubkey, network)
                .map_err(|e| BatchError::generation_error(e.to_string()))?
                .to_string()
        }
        BatchAddressType::P2WPKH => {
            P2WPKHAddress::from_public_key(&pubkey, network)
                .map_err(|e| BatchError::generation_error(e.to_string()))?
                .to_string()
        }
        BatchAddressType::P2TR => {
            P2TRAddress::from_public_key(&pubkey, network)
                .map_err(|e| BatchError::generation_error(e.to_string()))?
                .to_string()
        }
    };

    Ok((key, address))
}


/// Streaming iterator for batch address generation.
///
/// `AddressStream` generates key-address pairs on-demand, allowing
/// processing of large batches without memory exhaustion.
pub struct AddressStream {
    address_type: BatchAddressType,
    network: Network,
    remaining: usize,
    parallel: bool,
    chunk_size: usize,
    current_chunk: std::vec::IntoIter<(PrivateKey, String)>,
}

impl AddressStream {
    /// Create a new address stream.
    fn new(
        address_type: BatchAddressType,
        network: Network,
        count: usize,
        parallel: bool,
        chunk_size: usize,
    ) -> Self {
        Self {
            address_type,
            network,
            remaining: count,
            parallel,
            chunk_size,
            current_chunk: Vec::new().into_iter(),
        }
    }

    /// Generate a chunk of addresses.
    fn generate_chunk(&mut self) -> Vec<(PrivateKey, String)> {
        let chunk_count = self.remaining.min(self.chunk_size);
        self.remaining -= chunk_count;

        let address_type = self.address_type;
        let network = self.network;

        if self.parallel {
            (0..chunk_count)
                .into_par_iter()
                .filter_map(|_| generate_address_pair(address_type, network).ok())
                .collect()
        } else {
            (0..chunk_count)
                .filter_map(|_| generate_address_pair(address_type, network).ok())
                .collect()
        }
    }

    /// Get the number of remaining addresses to generate.
    #[inline]
    pub fn remaining(&self) -> usize {
        self.remaining + self.current_chunk.len()
    }
}

impl Iterator for AddressStream {
    type Item = (PrivateKey, String);

    fn next(&mut self) -> Option<Self::Item> {
        // Try to get from current chunk
        if let Some(pair) = self.current_chunk.next() {
            return Some(pair);
        }

        // Generate new chunk if there are remaining addresses
        if self.remaining > 0 {
            let chunk = self.generate_chunk();
            self.current_chunk = chunk.into_iter();
            self.current_chunk.next()
        } else {
            None
        }
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        let remaining = self.remaining();
        (remaining, Some(remaining))
    }
}

impl ExactSizeIterator for AddressStream {}

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

    #[test]
    fn test_batch_address_type_display() {
        assert_eq!(BatchAddressType::P2PKH.to_string(), "P2PKH");
        assert_eq!(BatchAddressType::P2WPKH.to_string(), "P2WPKH");
        assert_eq!(BatchAddressType::P2TR.to_string(), "P2TR");
    }

    #[test]
    fn test_batch_address_type_prefixes() {
        assert_eq!(BatchAddressType::P2PKH.mainnet_prefix(), "1");
        assert_eq!(BatchAddressType::P2WPKH.mainnet_prefix(), "bc1q");
        assert_eq!(BatchAddressType::P2TR.mainnet_prefix(), "bc1p");
    }

    #[test]
    fn test_generate_p2pkh_addresses() {
        let generator = BatchAddressGenerator::new(BatchAddressType::P2PKH, Network::BitcoinMainnet);
        let addresses = generator.generate_vec(10).unwrap();

        assert_eq!(addresses.len(), 10);
        for (_, addr) in &addresses {
            assert!(addr.starts_with('1'), "P2PKH address should start with '1': {}", addr);
        }
    }

    #[test]
    fn test_generate_p2wpkh_addresses() {
        let generator = BatchAddressGenerator::new(BatchAddressType::P2WPKH, Network::BitcoinMainnet);
        let addresses = generator.generate_vec(10).unwrap();

        assert_eq!(addresses.len(), 10);
        for (_, addr) in &addresses {
            assert!(addr.starts_with("bc1q"), "P2WPKH address should start with 'bc1q': {}", addr);
        }
    }

    #[test]
    fn test_generate_p2tr_addresses() {
        let generator = BatchAddressGenerator::new(BatchAddressType::P2TR, Network::BitcoinMainnet);
        let addresses = generator.generate_vec(10).unwrap();

        assert_eq!(addresses.len(), 10);
        for (_, addr) in &addresses {
            assert!(addr.starts_with("bc1p"), "P2TR address should start with 'bc1p': {}", addr);
        }
    }

    #[test]
    fn test_generate_testnet_addresses() {
        let generator = BatchAddressGenerator::new(BatchAddressType::P2WPKH, Network::BitcoinTestnet);
        let addresses = generator.generate_vec(10).unwrap();

        assert_eq!(addresses.len(), 10);
        for (_, addr) in &addresses {
            assert!(addr.starts_with("tb1q"), "Testnet P2WPKH should start with 'tb1q': {}", addr);
        }
    }

    #[test]
    fn test_generate_stream() {
        let generator = BatchAddressGenerator::new(BatchAddressType::P2WPKH, Network::BitcoinMainnet);
        let stream = generator.generate_stream(100);

        let addresses: Vec<_> = stream.collect();
        assert_eq!(addresses.len(), 100);
    }

    #[test]
    fn test_generate_stream_parallel() {
        let generator = BatchAddressGenerator::new(BatchAddressType::P2TR, Network::BitcoinMainnet)
            .parallel(true)
            .chunk_size(50);

        let stream = generator.generate_stream(200);
        let addresses: Vec<_> = stream.collect();

        assert_eq!(addresses.len(), 200);
        for (_, addr) in &addresses {
            assert!(addr.starts_with("bc1p"));
        }
    }

    #[test]
    fn test_generate_sequential() {
        let generator = BatchAddressGenerator::new(BatchAddressType::P2PKH, Network::BitcoinMainnet)
            .parallel(false);

        let addresses = generator.generate_vec(50).unwrap();
        assert_eq!(addresses.len(), 50);
    }

    #[test]
    fn test_addresses_are_unique() {
        let generator = BatchAddressGenerator::new(BatchAddressType::P2WPKH, Network::BitcoinMainnet);
        let addresses = generator.generate_vec(100).unwrap();

        let unique_addrs: std::collections::HashSet<_> = addresses.iter().map(|(_, a)| a.clone()).collect();
        assert_eq!(unique_addrs.len(), addresses.len(), "All addresses should be unique");
    }

    #[test]
    fn test_key_derives_to_address() {
        let generator = BatchAddressGenerator::new(BatchAddressType::P2WPKH, Network::BitcoinMainnet);
        let addresses = generator.generate_vec(10).unwrap();

        for (key, addr) in addresses {
            // Manually derive address from key and verify it matches
            let pubkey = key.public_key();
            let derived_addr = P2WPKHAddress::from_public_key(&pubkey, Network::BitcoinMainnet)
                .unwrap()
                .to_string();
            assert_eq!(addr, derived_addr, "Address should match derived address");
        }
    }
}