c32address 0.1.0

Crockford Base32Check encoding for Stacks addresses
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
use c32address::*;
use std::fs;
use rayon::prelude::*;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Arc;
use std::time::Instant;
use env_logger;

// Initialize logger for tests
fn setup() {
    let _ = env_logger::builder()
        .is_test(true)
        .try_init();
}

const TEST_HEX_STRINGS: &[&str] = &[
    "a46ff88886c2ef9762d970b4d2c63678835bd39d",
    "0000000000000000000000000000000000000000",
    "0000000000000000000000000000000000000001",
    "1000000000000000000000000000000000000001",
    "1000000000000000000000000000000000000000",
];

const TEST_VERSIONS: &[u8] = &[22, 0, 31, 20, 26, 21];

const TEST_C32_ADDRESSES: &[&[&str]] = &[
    &[
        "SP2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKNRV9EJ7",
        "SP000000000000000000002Q6VF78",
        "SP00000000000000000005JA84HQ",
        "SP80000000000000000000000000000004R0CMNV",
        "SP800000000000000000000000000000033H8YKK",
    ],
    &[
        "S02J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKPVKG2CE",
        "S0000000000000000000002AA028H",
        "S000000000000000000006EKBDDS",
        "S080000000000000000000000000000007R1QC00",
        "S080000000000000000000000000000003ENTGCQ",
    ],
    &[
        "SZ2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQ9H6DPR",
        "SZ000000000000000000002ZE1VMN",
        "SZ00000000000000000005HZ3DVN",
        "SZ80000000000000000000000000000004XBV6MS",
        "SZ800000000000000000000000000000007VF5G0",
    ],
    &[
        "SM2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQVX8X0G",
        "SM0000000000000000000062QV6X",
        "SM00000000000000000005VR75B2",
        "SM80000000000000000000000000000004WBEWKC",
        "SM80000000000000000000000000000000JGSYGV",
    ],
    &[
        "ST2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQYAC0RQ",
        "ST000000000000000000002AMW42H",
        "ST000000000000000000042DB08Y",
        "ST80000000000000000000000000000006BYJ4R4",
        "ST80000000000000000000000000000002YBNPV3",
    ],
    &[
        "SN2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKP6D2ZK9",
        "SN000000000000000000003YDHWKJ",
        "SN00000000000000000005341MC8",
        "SN800000000000000000000000000000066KZWY0",
        "SN800000000000000000000000000000006H75AK",
    ],
];

#[test]
fn test_c32address_encode_decode() {
    setup();
    for hex_str in TEST_HEX_STRINGS {
        if hex_str.is_empty() {
            continue;
        }
        let bytes = hex::decode(hex_str).unwrap();
        
        // Test without min_length
        let encoded = c32::encode(&bytes);
        let decoded = c32::decode(&encoded).expect("Failed to decode c32 string");
        assert_eq!(
            decoded, bytes,
            "c32_decode(c32_encode({})): expected {}, got {}",
            hex_str,
            hex::encode(&bytes),
            hex::encode(&decoded)
        );
    }
}

#[test]
fn test_c32check_encode_decode() {
    setup();
    for (_i, hex_str) in TEST_HEX_STRINGS.iter().enumerate() {
        if hex_str.is_empty() {
            continue;
        }
        for &version in TEST_VERSIONS {
            let bytes = hex::decode(hex_str).unwrap();
            let encoded = c32::encode_check(&bytes, version)
                .expect("Failed to encode");
            
            let (decoded_version, decoded_bytes) = c32::decode_check(&encoded)
                .expect("Failed to decode");
            
            assert_eq!(
                decoded_version, version,
                "c32::encode_check version: expected {}, got {}",
                version, decoded_version
            );
            assert_eq!(
                decoded_bytes, bytes,
                "c32::decode_check bytes: expected {}, got {}",
                hex::encode(&bytes),
                hex::encode(&decoded_bytes)
            );
        }
    }
}

#[test]
fn test_encode_address() {
    setup();
    for (i, hex_str) in TEST_HEX_STRINGS.iter().enumerate() {
        if hex_str.is_empty() {
            continue;
        }
        for (j, &version) in TEST_VERSIONS.iter().enumerate() {
            let bytes = hex::decode(hex_str).unwrap();
            let address = encode_address(version, &bytes).expect("Failed to encode address");
            assert_eq!(
                address,
                TEST_C32_ADDRESSES[j][i],
                "encode_address version={} {}: expected {}, got {}",
                version,
                hex_str,
                TEST_C32_ADDRESSES[j][i],
                address
            );

            let (decoded_version, decoded_bytes) = decode_address(&address)
                .expect("Failed to decode address");
            assert_eq!(
                decoded_version, version,
                "decode_address {}: expected version {}, got {}",
                address, version, decoded_version
            );

            let expected_hex = if hex_str.len() % 2 != 0 {
                format!("0{}", hex_str)
            } else {
                hex_str.to_string()
            };
            let decoded_hex = hex::encode(&decoded_bytes);
            assert_eq!(
                decoded_hex,
                expected_hex,
                "decode_address {}: expected hex {}, got {}",
                address,
                expected_hex,
                decoded_hex
            );
        }
    }
}

#[test]
fn test_invalid_version() {
    setup();
    let hex_str = "a46ff88886c2ef9762d970b4d2c63678835bd39d";
    let bytes = hex::decode(hex_str).unwrap();

    // Test invalid versions
    let invalid_versions = [-1i8 as u8, 32];
    for &version in &invalid_versions {
        let result = encode_address(version, &bytes);
        assert!(
            matches!(result, Err(AddressError::InvalidVersion(_))),
            "encode_address with invalid version {} should return InvalidVersion error",
            version
        );
    }
}

#[test]
fn test_invalid_length() {
    setup();
    let invalid_hex = [
        "a46ff88886c2ef9762d970b4d2c63678835bd39d00", // too long
        "a46ff88886c2ef9762d970b4d2c63678835bd3",     // too short
    ];

    for hex_str in &invalid_hex {
        let bytes = hex::decode(hex_str).unwrap();
        let result = encode_address(22, &bytes);
        assert!(
            matches!(result, Err(AddressError::InvalidLength(_))),
            "encode_address with invalid length should return InvalidLength error"
        );
    }
}

#[test]
fn test_decode_invalid_address() {
    setup();
    let invalid_addresses = [
        "ST2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQYAC0RQ0", // too long
        "ST2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQYAC0R",   // too short
        "ST2J",                                        // way too short
        "XP2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKNRV9EJ7", // invalid prefix
    ];

    for address in &invalid_addresses {
        let result = decode_address(address);
        assert!(
            matches!(result, 
                Err(AddressError::InvalidFormat(_))
            ),
            "decode_address with invalid address {} should return InvalidFormat, got {:?}",
            address,
            result
        );
    }
}

#[test]
fn test_simple() {
    setup();
    let hex_strings = &[
        "a46ff88886c2ef9762d970b4d2c63678835bd39d",
        "",
        "0000000000000000000000000000000000000000",
        "0000000000000000000000000000000000000001",
        "1000000000000000000000000000000000000001",
        "1000000000000000000000000000000000000000",
        "01",
        "22",
        "0001",
        "000001",
        "00000001",
        "10",
        "0100",
        "1000",
        "010000",
        "100000",
        "01000000",
        "10000000",
        "0100000000",
    ];
    let c32_strs = [
        "MHQZH246RBQSERPSE2TD5HHPF21NQMWX",
        "",
        "00000000000000000000",
        "00000000000000000001",
        "20000000000000000000000000000001",
        "20000000000000000000000000000000",
        "1",
        "12",
        "01",
        "001",
        "0001",
        "G",
        "80",
        "400",
        "2000",
        "10000",
        "G0000",
        "800000",
        "4000000",
    ];

    let results: Vec<_> = hex_strings
        .iter()
        .zip(c32_strs.iter())
        .map(|(hex_str, expected)| {
            let bytes = hex::decode(hex_str).unwrap();
            let c32_encoded = c32::encode(&bytes);
            let decoded_bytes = c32::decode(&c32_encoded).unwrap();
            let result = (bytes, c32_encoded, decoded_bytes, expected);
            println!("{:?}", result);
            result
        })
        .collect();
    for (bytes, c32_encoded, decoded_bytes, expected_c32) in results.iter() {
        assert_eq!(bytes, decoded_bytes);
        assert_eq!(c32_encoded, *expected_c32);
    }
}

    #[test]
    fn test_addresses() {
        setup();
        let hex_strs = [
            "a46ff88886c2ef9762d970b4d2c63678835bd39d",
            "0000000000000000000000000000000000000000",
            "0000000000000000000000000000000000000001",
            "1000000000000000000000000000000000000001",
            "1000000000000000000000000000000000000000",
        ];

        let versions = [22, 0, 31, 20, 26, 21];

        let c32_addrs = [
            [
                "SP2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKNRV9EJ7",
                "SP000000000000000000002Q6VF78",
                "SP00000000000000000005JA84HQ",
                "SP80000000000000000000000000000004R0CMNV",
                "SP800000000000000000000000000000033H8YKK",
            ],
            [
                "S02J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKPVKG2CE",
                "S0000000000000000000002AA028H",
                "S000000000000000000006EKBDDS",
                "S080000000000000000000000000000007R1QC00",
                "S080000000000000000000000000000003ENTGCQ",
            ],
            [
                "SZ2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQ9H6DPR",
                "SZ000000000000000000002ZE1VMN",
                "SZ00000000000000000005HZ3DVN",
                "SZ80000000000000000000000000000004XBV6MS",
                "SZ800000000000000000000000000000007VF5G0",
            ],
            [
                "SM2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQVX8X0G",
                "SM0000000000000000000062QV6X",
                "SM00000000000000000005VR75B2",
                "SM80000000000000000000000000000004WBEWKC",
                "SM80000000000000000000000000000000JGSYGV",
            ],
            [
                "ST2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQYAC0RQ",
                "ST000000000000000000002AMW42H",
                "ST000000000000000000042DB08Y",
                "ST80000000000000000000000000000006BYJ4R4",
                "ST80000000000000000000000000000002YBNPV3",
            ],
            [
                "SN2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKP6D2ZK9",
                "SN000000000000000000003YDHWKJ",
                "SN00000000000000000005341MC8",
                "SN800000000000000000000000000000066KZWY0",
                "SN800000000000000000000000000000006H75AK",
            ],
        ];

        for (i, h) in hex_strs.iter().enumerate() {
            for (j, v) in versions.iter().enumerate() {
                let b = hex::decode(h).unwrap();
                let z = encode_address(*v, &b).unwrap();

                assert_eq!(z, c32_addrs[j][i]);

                let (decoded_version, decoded_bytes) = decode_address(&z).unwrap();
                assert_eq!(decoded_version, *v);
                assert_eq!(decoded_bytes, b);
            }
        }
    }


    #[test]
fn test_random_c32_encoding() {
    setup();
    // Check if FULL_TEST environment variable is set
    let run_full_test = std::env::var("FULL_TEST").is_ok();
    
    println!("\nRunning c32 encoding/decoding verification test");
    println!("----------------------------------------------");
    
    // Read test data from JSON file
    let data = fs::read_to_string("tests/random-data.json")
        .expect("Failed to read random-data.json");
    let test_data: Vec<serde_json::Value> = serde_json::from_str(&data)
        .expect("Failed to parse random-data.json");
    
    // Determine number of test cases
    let num_cases = if run_full_test {
        println!("FULL_TEST flag detected - running all available test cases");
        println!("Note: Full test takes ~90-100 seconds on a MacBook Pro M3 Max using parallel execution");
        test_data.len()
    } else {
        println!("Running first 3000 test cases in parallel (fast mode, ~1 second on MacBook Pro M3 Max)");
        println!("To run all test cases, use:");
        println!("    FULL_TEST=1 cargo test test_random_c32_encoding");
        std::cmp::min(3000, test_data.len())
    };
    
    let test_subset: Vec<_> = test_data.iter().take(num_cases).collect();
    println!("\nTesting {} out of {} total test cases", test_subset.len(), test_data.len());
    
    // Setup progress tracking
    let processed = Arc::new(AtomicUsize::new(0));
    let total_cases = test_subset.len();
    let start_time = Instant::now();

    // Spawn progress reporting thread
    let processed_clone = Arc::clone(&processed);
    let progress_thread = std::thread::spawn(move || {
        while processed_clone.load(Ordering::Relaxed) < total_cases {
            let current = processed_clone.load(Ordering::Relaxed);
            let elapsed = start_time.elapsed();
            println!(
                "Progress: {}/{} ({:.1}%) - {:.2}s elapsed", 
                current,
                total_cases,
                (current as f64 / total_cases as f64) * 100.0,
                elapsed.as_secs_f64()
            );
            std::thread::sleep(std::time::Duration::from_secs(1));
        }
    });

    // Process test cases in parallel
    test_subset.par_iter().try_for_each(|test_case| -> Result<(), String> {
        let expected_hex = test_case["hex"].as_str()
            .ok_or("Test case missing hex field")?;
        let expected_c32 = test_case["c32"].as_str()
            .ok_or("Test case missing c32 field")?;
        
        // Test c32encode
        let bytes = hex::decode(expected_hex).map_err(|e| format!("Failed to decode hex: {}", e))?;
        let actual_c32 = c32::encode(&bytes); 
        
        if actual_c32.len() == expected_c32.len() + 1 && actual_c32.starts_with('0') {
            let actual_without_zero = &actual_c32[1..];
            if actual_without_zero != expected_c32 {
                return Err(format!(
                    "c32encode failed - hex {} - expected {}, got {} (without leading zero: {})",
                    expected_hex, expected_c32, actual_c32, actual_without_zero
                ));
            }
        } else if actual_c32 != expected_c32 {
            return Err(format!(
                "c32encode failed - hex {} - expected {}, got {}",
                expected_hex, expected_c32, actual_c32
            ));
        }
        
        // Test c32decode
        let expected_bytes_len = expected_hex.len() / 2;
        let decoded_bytes = match c32::decode(expected_c32) {
            Ok(mut bytes) => {
                // Pad with leading zeros if necessary to match expected length
                while bytes.len() < expected_bytes_len {
                    bytes.insert(0, 0);
                }
                bytes
            },
            Err(e) => return Err(format!("Failed to decode c32: {}", e))
        };
        
        let actual_hex = hex::encode(&decoded_bytes);
        
        if actual_hex != expected_hex {
            return Err(format!(
                "c32decode failed - c32 {} - expected {}, got {} (decoded bytes len: {}, expected len: {})",
                expected_c32, expected_hex, actual_hex, decoded_bytes.len(), expected_bytes_len
            ));
        }

        // Update progress counter
        processed.fetch_add(1, Ordering::Relaxed);
        Ok(())
    }).expect("Test failed");

    // Wait for progress thread to finish
    progress_thread.join().unwrap();

    let total_time = start_time.elapsed();
    println!("\nAll {} test cases passed in {:.2}s!", total_cases, total_time.as_secs_f64());
    
    if !run_full_test {
        println!("\nNote: Only ran the first 3000 test cases");
        println!("To run all test cases (~90-100 seconds on MacBook Pro M3 Max), use:");
        println!("    FULL_TEST=1 cargo test test_random_c32_encoding");
    }
}

// Add a test that explains how to run all test cases
#[test]
fn test_random_c32_encoding_info() {
    setup();
    println!("\nNote: The random c32 encoding test runs 3000 test cases by default in parallel (~1 second)");
    println!("To run all test cases (~90-100 seconds on MacBook Pro M3 Max), use:");
    println!("    FULL_TEST=1 cargo test test_random_c32_encoding\n");
}