bsv-rs 0.3.4

BSV blockchain SDK for Rust - primitives, script, transactions, and more
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
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
//! BIP-39 Mnemonic implementation.

use crate::primitives::hash::{pbkdf2_sha512, sha256};
use crate::{Error, Result};

use super::wordlists;

/// Supported word counts for BIP-39 mnemonics.
///
/// Each word count corresponds to a specific entropy size:
/// - 12 words = 128 bits entropy
/// - 15 words = 160 bits entropy
/// - 18 words = 192 bits entropy
/// - 21 words = 224 bits entropy
/// - 24 words = 256 bits entropy
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum WordCount {
    /// 12-word mnemonic (128 bits entropy)
    Words12,
    /// 15-word mnemonic (160 bits entropy)
    Words15,
    /// 18-word mnemonic (192 bits entropy)
    Words18,
    /// 21-word mnemonic (224 bits entropy)
    Words21,
    /// 24-word mnemonic (256 bits entropy)
    Words24,
}

impl WordCount {
    /// Returns the entropy size in bytes for this word count.
    pub fn entropy_bytes(self) -> usize {
        match self {
            WordCount::Words12 => 16, // 128 bits
            WordCount::Words15 => 20, // 160 bits
            WordCount::Words18 => 24, // 192 bits
            WordCount::Words21 => 28, // 224 bits
            WordCount::Words24 => 32, // 256 bits
        }
    }

    /// Returns the number of words for this word count.
    pub fn word_count(self) -> usize {
        match self {
            WordCount::Words12 => 12,
            WordCount::Words15 => 15,
            WordCount::Words18 => 18,
            WordCount::Words21 => 21,
            WordCount::Words24 => 24,
        }
    }

    /// Returns the checksum size in bits for this word count.
    pub fn checksum_bits(self) -> usize {
        self.entropy_bytes() * 8 / 32
    }
}

/// Supported languages for BIP-39 wordlists.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum Language {
    /// Chinese Simplified wordlist
    ChineseSimplified,
    /// Chinese Traditional wordlist
    ChineseTraditional,
    /// Czech wordlist
    Czech,
    /// English wordlist (default)
    #[default]
    English,
    /// French wordlist
    French,
    /// Italian wordlist
    Italian,
    /// Japanese wordlist
    Japanese,
    /// Korean wordlist
    Korean,
    /// Spanish wordlist
    Spanish,
}

impl Language {
    /// Returns the wordlist for this language.
    fn wordlist(&self) -> &'static [&'static str; 2048] {
        match self {
            Language::ChineseSimplified => &wordlists::CHINESE_SIMPLIFIED,
            Language::ChineseTraditional => &wordlists::CHINESE_TRADITIONAL,
            Language::Czech => &wordlists::CZECH,
            Language::English => &wordlists::ENGLISH,
            Language::French => &wordlists::FRENCH,
            Language::Italian => &wordlists::ITALIAN,
            Language::Japanese => &wordlists::JAPANESE,
            Language::Korean => &wordlists::KOREAN,
            Language::Spanish => &wordlists::SPANISH,
        }
    }

    /// Returns the word separator for this language.
    ///
    /// Per BIP-39 specification, Japanese uses the ideographic space (U+3000)
    /// as the word separator. All other languages use a regular ASCII space.
    ///
    /// Note: The Go SDK uses regular space for all languages including Japanese.
    /// This implementation follows the BIP-39 spec, which specifies ideographic
    /// space for Japanese mnemonics.
    fn separator(&self) -> &'static str {
        match self {
            Language::Japanese => "\u{3000}",
            _ => " ",
        }
    }

    /// Finds the index of a word in the wordlist.
    fn word_index(&self, word: &str) -> Option<usize> {
        let wordlist = self.wordlist();
        wordlist.iter().position(|&w| w == word)
    }
}

/// A BIP-39 mnemonic phrase.
///
/// This struct represents a validated mnemonic phrase that can be used to
/// generate deterministic seeds for wallet derivation.
#[derive(Debug, Clone)]
pub struct Mnemonic {
    /// The individual words of the mnemonic.
    words: Vec<String>,
    /// The language of the mnemonic.
    language: Language,
}

impl Mnemonic {
    /// Generates a new random mnemonic with the specified word count.
    ///
    /// # Arguments
    ///
    /// * `word_count` - The number of words in the mnemonic
    ///
    /// # Returns
    ///
    /// A new `Mnemonic` instance with random entropy.
    ///
    /// # Example
    ///
    /// ```rust
    /// use bsv_rs::compat::bip39::{Mnemonic, WordCount};
    ///
    /// let mnemonic = Mnemonic::new(WordCount::Words12).unwrap();
    /// assert_eq!(mnemonic.words().len(), 12);
    /// ```
    pub fn new(word_count: WordCount) -> Result<Self> {
        let entropy_len = word_count.entropy_bytes();
        let mut entropy = vec![0u8; entropy_len];
        getrandom::getrandom(&mut entropy)
            .map_err(|e| Error::CryptoError(format!("failed to generate entropy: {}", e)))?;
        Self::from_entropy(&entropy)
    }

    /// Creates a mnemonic from the given entropy bytes.
    ///
    /// The entropy must be 16, 20, 24, 28, or 32 bytes (128, 160, 192, 224, or 256 bits).
    ///
    /// # Arguments
    ///
    /// * `entropy` - The entropy bytes
    ///
    /// # Returns
    ///
    /// A new `Mnemonic` instance derived from the entropy.
    ///
    /// # Errors
    ///
    /// Returns an error if the entropy length is invalid.
    ///
    /// # Example
    ///
    /// ```rust
    /// use bsv_rs::compat::bip39::Mnemonic;
    ///
    /// let entropy = [0u8; 16]; // 128 bits = 12 words
    /// let mnemonic = Mnemonic::from_entropy(&entropy).unwrap();
    /// assert_eq!(mnemonic.phrase(), "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about");
    /// ```
    pub fn from_entropy(entropy: &[u8]) -> Result<Self> {
        Self::from_entropy_with_language(entropy, Language::English)
    }

    /// Creates a mnemonic from entropy with a specific language.
    pub fn from_entropy_with_language(entropy: &[u8], language: Language) -> Result<Self> {
        // Validate entropy length
        let entropy_bits = entropy.len() * 8;
        #[allow(unknown_lints, clippy::manual_is_multiple_of)]
        if !(128..=256).contains(&entropy_bits) || entropy_bits % 32 != 0 {
            return Err(Error::InvalidEntropyLength {
                expected: "128, 160, 192, 224, or 256 bits".to_string(),
                actual: entropy_bits,
            });
        }

        // Calculate checksum
        let hash = sha256(entropy);
        let checksum_bits = entropy_bits / 32;

        // Convert entropy + checksum to words
        // We need to work with bits, combining entropy and checksum bits
        let total_bits = entropy_bits + checksum_bits;
        let word_count = total_bits / 11;

        let wordlist = language.wordlist();
        let mut words = Vec::with_capacity(word_count);

        // Build a bit stream from entropy + checksum
        // Each word is 11 bits, mapping to an index 0-2047
        for i in 0..word_count {
            let bit_offset = i * 11;
            let mut index: u16 = 0;

            for bit in 0..11 {
                let pos = bit_offset + bit;
                let byte_idx = pos / 8;
                let bit_idx = 7 - (pos % 8);

                let byte = if byte_idx < entropy.len() {
                    entropy[byte_idx]
                } else {
                    // This is in the checksum portion
                    hash[byte_idx - entropy.len()]
                };

                if (byte >> bit_idx) & 1 == 1 {
                    index |= 1 << (10 - bit);
                }
            }

            words.push(wordlist[index as usize].to_string());
        }

        Ok(Self { words, language })
    }

    /// Parses a mnemonic phrase string.
    ///
    /// The phrase is validated for correct word count, valid words, and correct checksum.
    ///
    /// # Arguments
    ///
    /// * `phrase` - The mnemonic phrase as a space-separated string
    ///
    /// # Returns
    ///
    /// A validated `Mnemonic` instance.
    ///
    /// # Errors
    ///
    /// Returns an error if the phrase is invalid.
    ///
    /// # Example
    ///
    /// ```rust
    /// use bsv_rs::compat::bip39::Mnemonic;
    ///
    /// let phrase = "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about";
    /// let mnemonic = Mnemonic::from_phrase(phrase).unwrap();
    /// assert!(mnemonic.is_valid());
    /// ```
    pub fn from_phrase(phrase: &str) -> Result<Self> {
        Self::from_phrase_with_language(phrase, Language::English)
    }

    /// Parses a mnemonic phrase with a specific language.
    pub fn from_phrase_with_language(phrase: &str, language: Language) -> Result<Self> {
        // Split into words, handling multiple spaces
        let words: Vec<String> = phrase
            .split_whitespace()
            .map(|s| s.to_lowercase())
            .collect();

        // Validate word count
        let word_count = words.len();
        if word_count != 12
            && word_count != 15
            && word_count != 18
            && word_count != 21
            && word_count != 24
        {
            return Err(Error::InvalidMnemonic(format!(
                "invalid word count: {}, expected 12, 15, 18, 21, or 24",
                word_count
            )));
        }

        // Validate each word exists in wordlist and get indices
        let mut indices = Vec::with_capacity(word_count);
        for word in &words {
            match language.word_index(word) {
                Some(idx) => indices.push(idx),
                None => return Err(Error::InvalidMnemonicWord(word.clone())),
            }
        }

        let mnemonic = Self { words, language };

        // Validate checksum
        if !mnemonic.validate_checksum(&indices)? {
            return Err(Error::InvalidMnemonic("invalid checksum".to_string()));
        }

        Ok(mnemonic)
    }

    /// Validates the checksum of the mnemonic.
    fn validate_checksum(&self, indices: &[usize]) -> Result<bool> {
        let word_count = self.words.len();
        let total_bits = word_count * 11;
        let checksum_bits = word_count / 3; // CS = ENT / 32, and word_count = (ENT + CS) / 11
        let entropy_bits = total_bits - checksum_bits;
        let entropy_bytes = entropy_bits / 8;

        // Extract entropy and checksum from word indices
        let mut bits = vec![false; total_bits];
        for (i, &index) in indices.iter().enumerate() {
            for bit in 0..11 {
                bits[i * 11 + bit] = (index >> (10 - bit)) & 1 == 1;
            }
        }

        // Convert entropy bits to bytes
        let mut entropy = vec![0u8; entropy_bytes];
        for (i, byte) in entropy.iter_mut().enumerate() {
            for bit in 0..8 {
                if bits[i * 8 + bit] {
                    *byte |= 1 << (7 - bit);
                }
            }
        }

        // Compute expected checksum
        let hash = sha256(&entropy);
        let mut expected_checksum_bits = vec![false; checksum_bits];
        for (i, bit) in expected_checksum_bits.iter_mut().enumerate() {
            let byte_idx = i / 8;
            let bit_idx = 7 - (i % 8);
            *bit = (hash[byte_idx] >> bit_idx) & 1 == 1;
        }

        // Compare with actual checksum bits
        let actual_checksum_bits = &bits[entropy_bits..];
        Ok(actual_checksum_bits == expected_checksum_bits)
    }

    /// Returns the mnemonic phrase as a string.
    ///
    /// # Example
    ///
    /// ```rust
    /// use bsv_rs::compat::bip39::Mnemonic;
    ///
    /// let entropy = [0u8; 16];
    /// let mnemonic = Mnemonic::from_entropy(&entropy).unwrap();
    /// println!("{}", mnemonic.phrase());
    /// ```
    pub fn phrase(&self) -> String {
        self.words.join(self.language.separator())
    }

    /// Returns the individual words of the mnemonic.
    pub fn words(&self) -> &[String] {
        &self.words
    }

    /// Returns the language of the mnemonic.
    pub fn language(&self) -> Language {
        self.language
    }

    /// Converts the mnemonic to a 512-bit (64-byte) seed.
    ///
    /// The seed is derived using PBKDF2-HMAC-SHA512 with 2048 iterations.
    /// The salt is "mnemonic" concatenated with the optional passphrase.
    ///
    /// # Arguments
    ///
    /// * `passphrase` - An optional passphrase for additional security
    ///
    /// # Returns
    ///
    /// A 64-byte seed.
    ///
    /// # Example
    ///
    /// ```rust
    /// use bsv_rs::compat::bip39::Mnemonic;
    ///
    /// let phrase = "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about";
    /// let mnemonic = Mnemonic::from_phrase(phrase).unwrap();
    /// let seed = mnemonic.to_seed("TREZOR");
    /// assert_eq!(seed.len(), 64);
    /// ```
    pub fn to_seed(&self, passphrase: &str) -> [u8; 64] {
        // Normalize the mnemonic phrase using NFKD normalization
        let mnemonic_normalized = self.phrase().nfkd_normalize();
        let passphrase_normalized = passphrase.nfkd_normalize();

        // Salt is "mnemonic" + passphrase
        let salt = format!("mnemonic{}", passphrase_normalized);

        // PBKDF2-HMAC-SHA512 with 2048 iterations
        let seed_vec = pbkdf2_sha512(mnemonic_normalized.as_bytes(), salt.as_bytes(), 2048, 64);

        let mut seed = [0u8; 64];
        seed.copy_from_slice(&seed_vec);
        seed
    }

    /// Converts the mnemonic to a seed with an empty passphrase.
    ///
    /// Equivalent to `to_seed("")`.
    pub fn to_seed_normalized(&self) -> [u8; 64] {
        self.to_seed("")
    }

    /// Returns the entropy bytes that were used to generate this mnemonic.
    ///
    /// # Example
    ///
    /// ```rust
    /// use bsv_rs::compat::bip39::Mnemonic;
    ///
    /// let original_entropy = [0u8; 16];
    /// let mnemonic = Mnemonic::from_entropy(&original_entropy).unwrap();
    /// let extracted = mnemonic.entropy();
    /// assert_eq!(extracted, original_entropy);
    /// ```
    pub fn entropy(&self) -> Vec<u8> {
        self.extract_entropy_internal().0
    }

    /// Returns the entropy bytes with checksum appended.
    ///
    /// This is equivalent to Go SDK's `MnemonicToByteArray` function.
    /// The returned bytes contain the original entropy followed by the
    /// checksum bits (padded to a full byte if necessary).
    ///
    /// # Example
    ///
    /// ```rust
    /// use bsv_rs::compat::bip39::Mnemonic;
    ///
    /// let entropy = [0u8; 16]; // 128 bits
    /// let mnemonic = Mnemonic::from_entropy(&entropy).unwrap();
    /// let with_checksum = mnemonic.entropy_with_checksum();
    /// // 128 bits entropy + 4 bits checksum = 132 bits = 17 bytes
    /// assert_eq!(with_checksum.len(), 17);
    /// ```
    pub fn entropy_with_checksum(&self) -> Vec<u8> {
        // Get word indices to extract the full bit stream
        let indices: Vec<usize> = self
            .words
            .iter()
            .map(|w| self.language.word_index(w).unwrap_or(0))
            .collect();

        let word_count = self.words.len();
        let total_bits = word_count * 11;
        let checksum_bit_count = word_count / 3;
        let entropy_bits = total_bits - checksum_bit_count;

        // Calculate full byte size needed
        let full_byte_size = (entropy_bits + checksum_bit_count).div_ceil(8);

        // Extract all bits from word indices
        let mut bits = vec![false; total_bits];
        for (i, &index) in indices.iter().enumerate() {
            for bit in 0..11 {
                bits[i * 11 + bit] = (index >> (10 - bit)) & 1 == 1;
            }
        }

        // Convert to bytes
        let mut result = vec![0u8; full_byte_size];
        for (i, bit) in bits.iter().enumerate() {
            if *bit {
                result[i / 8] |= 1 << (7 - (i % 8));
            }
        }

        result
    }

    /// Internal method to extract entropy from words.
    /// Returns (entropy_bytes, checksum_bit_count).
    fn extract_entropy_internal(&self) -> (Vec<u8>, usize) {
        let word_count = self.words.len();
        let total_bits = word_count * 11;
        let checksum_bits = word_count / 3;
        let entropy_bits = total_bits - checksum_bits;
        let entropy_bytes = entropy_bits / 8;

        // Get word indices
        let indices: Vec<usize> = self
            .words
            .iter()
            .map(|w| self.language.word_index(w).unwrap_or(0))
            .collect();

        // Extract bits from word indices
        let mut bits = vec![false; total_bits];
        for (i, &index) in indices.iter().enumerate() {
            for bit in 0..11 {
                bits[i * 11 + bit] = (index >> (10 - bit)) & 1 == 1;
            }
        }

        // Convert to bytes (excluding checksum)
        let mut entropy = vec![0u8; entropy_bytes];
        for (i, byte) in entropy.iter_mut().enumerate() {
            for bit in 0..8 {
                if bits[i * 8 + bit] {
                    *byte |= 1 << (7 - bit);
                }
            }
        }

        (entropy, checksum_bits)
    }

    /// Validates the mnemonic checksum.
    ///
    /// Returns `true` if the mnemonic has a valid checksum.
    pub fn is_valid(&self) -> bool {
        let indices: Vec<usize> = self
            .words
            .iter()
            .filter_map(|w| self.language.word_index(w))
            .collect();

        if indices.len() != self.words.len() {
            return false;
        }

        self.validate_checksum(&indices).unwrap_or(false)
    }

    /// Serialize mnemonic to binary format (UTF-8 encoded phrase).
    ///
    /// This returns the mnemonic phrase as UTF-8 bytes, which can be used
    /// for storage or transmission.
    ///
    /// # Example
    ///
    /// ```rust
    /// use bsv_rs::compat::bip39::Mnemonic;
    ///
    /// let entropy = [0u8; 16];
    /// let mnemonic = Mnemonic::from_entropy(&entropy).unwrap();
    /// let binary = mnemonic.to_binary();
    /// let restored = Mnemonic::from_binary(&binary).unwrap();
    /// assert_eq!(mnemonic.phrase(), restored.phrase());
    /// ```
    pub fn to_binary(&self) -> Vec<u8> {
        self.phrase().into_bytes()
    }

    /// Deserialize mnemonic from binary format (UTF-8 encoded phrase).
    ///
    /// This parses a mnemonic from UTF-8 bytes that were produced by `to_binary()`.
    ///
    /// # Arguments
    ///
    /// * `data` - The UTF-8 encoded mnemonic phrase
    ///
    /// # Returns
    ///
    /// A validated `Mnemonic` instance.
    ///
    /// # Errors
    ///
    /// Returns an error if the data is not valid UTF-8 or if the phrase is invalid.
    ///
    /// # Example
    ///
    /// ```rust
    /// use bsv_rs::compat::bip39::Mnemonic;
    ///
    /// let phrase = b"abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about";
    /// let mnemonic = Mnemonic::from_binary(phrase).unwrap();
    /// assert!(mnemonic.is_valid());
    /// ```
    pub fn from_binary(data: &[u8]) -> Result<Self> {
        let phrase = std::str::from_utf8(data)
            .map_err(|e| Error::InvalidMnemonic(format!("invalid UTF-8: {}", e)))?;
        Self::from_phrase(phrase)
    }
}

impl std::fmt::Display for Mnemonic {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.phrase())
    }
}

/// Extension trait for NFKD normalization of strings.
///
/// BIP-39 requires NFKD normalization for both the mnemonic and passphrase.
/// This implementation passes through strings unchanged, matching the Go SDK behavior.
///
/// ## Compatibility Notes
///
/// - **Go SDK**: Does NOT perform NFKD normalization (passes raw UTF-8 bytes)
/// - **TypeScript SDK**: DOES perform NFKD normalization via `String.normalize('NFKD')`
/// - **This implementation**: Matches Go SDK (no normalization)
///
/// For ASCII passphrases (recommended), all SDKs produce identical seeds.
/// For non-ASCII passphrases, this implementation matches Go SDK but may
/// differ from TypeScript SDK.
///
/// ## Recommendation
///
/// For maximum cross-SDK compatibility, use only ASCII characters in passphrases.
trait NfkdNormalize {
    fn nfkd_normalize(&self) -> String;
}

impl NfkdNormalize for str {
    fn nfkd_normalize(&self) -> String {
        // Pass through unchanged to match Go SDK behavior.
        // The BIP-39 English wordlist contains only ASCII characters, so the
        // mnemonic phrase itself doesn't need normalization.
        //
        // For full NFKD Unicode normalization, add the `unicode-normalization` crate:
        // ```
        // use unicode_normalization::UnicodeNormalization;
        // self.nfkd().collect()
        // ```
        self.to_string()
    }
}

impl NfkdNormalize for String {
    fn nfkd_normalize(&self) -> String {
        self.as_str().nfkd_normalize()
    }
}

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

    #[test]
    fn test_word_count_entropy_bytes() {
        assert_eq!(WordCount::Words12.entropy_bytes(), 16);
        assert_eq!(WordCount::Words15.entropy_bytes(), 20);
        assert_eq!(WordCount::Words18.entropy_bytes(), 24);
        assert_eq!(WordCount::Words21.entropy_bytes(), 28);
        assert_eq!(WordCount::Words24.entropy_bytes(), 32);
    }

    #[test]
    fn test_word_count_word_count() {
        assert_eq!(WordCount::Words12.word_count(), 12);
        assert_eq!(WordCount::Words15.word_count(), 15);
        assert_eq!(WordCount::Words18.word_count(), 18);
        assert_eq!(WordCount::Words21.word_count(), 21);
        assert_eq!(WordCount::Words24.word_count(), 24);
    }

    #[test]
    fn test_from_entropy_all_zeros_12_words() {
        let entropy = [0u8; 16];
        let mnemonic = Mnemonic::from_entropy(&entropy).unwrap();
        assert_eq!(
            mnemonic.phrase(),
            "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about"
        );
    }

    #[test]
    fn test_from_entropy_all_ones_12_words() {
        let entropy = [0xffu8; 16];
        let mnemonic = Mnemonic::from_entropy(&entropy).unwrap();
        assert_eq!(
            mnemonic.phrase(),
            "zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo wrong"
        );
    }

    #[test]
    fn test_from_phrase_valid() {
        let phrase = "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about";
        let mnemonic = Mnemonic::from_phrase(phrase).unwrap();
        assert!(mnemonic.is_valid());
        assert_eq!(mnemonic.words().len(), 12);
    }

    #[test]
    fn test_from_phrase_invalid_word() {
        let phrase = "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon notaword";
        let result = Mnemonic::from_phrase(phrase);
        assert!(result.is_err());
    }

    #[test]
    fn test_from_phrase_invalid_checksum() {
        let phrase = "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon";
        let result = Mnemonic::from_phrase(phrase);
        assert!(result.is_err());
    }

    #[test]
    fn test_entropy_roundtrip() {
        let original = hex::decode("7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f").unwrap();
        let mnemonic = Mnemonic::from_entropy(&original).unwrap();
        let extracted = mnemonic.entropy();
        assert_eq!(original, extracted);
    }

    #[test]
    fn test_to_seed_trezor() {
        let phrase = "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about";
        let mnemonic = Mnemonic::from_phrase(phrase).unwrap();
        let seed = mnemonic.to_seed("TREZOR");
        assert_eq!(
            hex::encode(seed),
            "c55257c360c07c72029aebc1b53c05ed0362ada38ead3e3e9efa3708e53495531f09a6987599d18264c1e1c92f2cf141630c7a3c4ab7c81b2f001698e7463b04"
        );
    }

    #[test]
    fn test_new_generates_valid_mnemonic() {
        let mnemonic = Mnemonic::new(WordCount::Words12).unwrap();
        assert!(mnemonic.is_valid());
        assert_eq!(mnemonic.words().len(), 12);

        let mnemonic = Mnemonic::new(WordCount::Words24).unwrap();
        assert!(mnemonic.is_valid());
        assert_eq!(mnemonic.words().len(), 24);
    }

    #[test]
    fn test_invalid_entropy_length() {
        let result = Mnemonic::from_entropy(&[0u8; 15]); // 120 bits - invalid
        assert!(result.is_err());

        let result = Mnemonic::from_entropy(&[0u8; 33]); // 264 bits - invalid
        assert!(result.is_err());
    }

    #[test]
    fn test_to_binary_from_binary_roundtrip() {
        let entropy = [0u8; 16];
        let mnemonic = Mnemonic::from_entropy(&entropy).unwrap();
        let binary = mnemonic.to_binary();
        let restored = Mnemonic::from_binary(&binary).unwrap();
        assert_eq!(mnemonic.phrase(), restored.phrase());
    }

    #[test]
    fn test_to_binary_format() {
        let entropy = [0u8; 16];
        let mnemonic = Mnemonic::from_entropy(&entropy).unwrap();
        let binary = mnemonic.to_binary();
        // Binary should be UTF-8 encoded phrase
        let phrase = mnemonic.phrase();
        assert_eq!(binary, phrase.as_bytes());
    }

    #[test]
    fn test_from_binary_valid() {
        let phrase = b"abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about";
        let mnemonic = Mnemonic::from_binary(phrase).unwrap();
        assert!(mnemonic.is_valid());
        assert_eq!(mnemonic.words().len(), 12);
    }

    #[test]
    fn test_from_binary_invalid_utf8() {
        // Invalid UTF-8 sequence
        let invalid_utf8 = [0xff, 0xfe, 0xfd];
        let result = Mnemonic::from_binary(&invalid_utf8);
        assert!(result.is_err());
    }

    #[test]
    fn test_from_binary_invalid_phrase() {
        let invalid_phrase = b"not a valid mnemonic phrase";
        let result = Mnemonic::from_binary(invalid_phrase);
        assert!(result.is_err());
    }

    #[test]
    fn test_binary_roundtrip_all_word_counts() {
        for word_count in [
            WordCount::Words12,
            WordCount::Words15,
            WordCount::Words18,
            WordCount::Words21,
            WordCount::Words24,
        ] {
            let mnemonic = Mnemonic::new(word_count).unwrap();
            let binary = mnemonic.to_binary();
            let restored = Mnemonic::from_binary(&binary).unwrap();
            assert_eq!(mnemonic.phrase(), restored.phrase());
            assert_eq!(
                mnemonic.words().len(),
                restored.words().len(),
                "Word count mismatch for {:?}",
                word_count
            );
        }
    }

    #[test]
    fn test_generate_mnemonic_each_language() {
        let languages = [
            Language::ChineseSimplified,
            Language::ChineseTraditional,
            Language::Czech,
            Language::English,
            Language::French,
            Language::Italian,
            Language::Japanese,
            Language::Korean,
            Language::Spanish,
        ];

        for lang in &languages {
            let entropy = [0u8; 16]; // 128 bits = 12 words
            let mnemonic = Mnemonic::from_entropy_with_language(&entropy, *lang).unwrap();
            assert_eq!(
                mnemonic.words().len(),
                12,
                "Expected 12 words for {:?}",
                lang
            );
            assert_eq!(mnemonic.language(), *lang);
            // Verify the phrase is non-empty
            assert!(!mnemonic.phrase().is_empty(), "Empty phrase for {:?}", lang);
        }
    }

    #[test]
    fn test_roundtrip_seed_non_english() {
        let languages = [
            Language::ChineseSimplified,
            Language::ChineseTraditional,
            Language::Czech,
            Language::French,
            Language::Italian,
            Language::Japanese,
            Language::Korean,
            Language::Spanish,
        ];

        for lang in &languages {
            let entropy = [0xABu8; 16]; // 128 bits
            let mnemonic = Mnemonic::from_entropy_with_language(&entropy, *lang).unwrap();

            // Generate seed
            let seed1 = mnemonic.to_seed("");
            assert_eq!(seed1.len(), 64, "Seed length wrong for {:?}", lang);

            // Generate seed with passphrase
            let seed2 = mnemonic.to_seed("test passphrase");
            assert_eq!(seed2.len(), 64);

            // Different passphrases should produce different seeds
            assert_ne!(
                seed1, seed2,
                "Seeds should differ with different passphrases for {:?}",
                lang
            );

            // Same mnemonic + same passphrase should produce same seed
            let seed3 = mnemonic.to_seed("");
            assert_eq!(seed1, seed3, "Seeds should be deterministic for {:?}", lang);
        }
    }

    #[test]
    fn test_japanese_mnemonic_with_ideographic_space() {
        // BIP-39 specifies that Japanese mnemonics use ideographic space (U+3000)
        // as the word separator.
        let entropy = [0u8; 16]; // 128 bits = 12 words
        let mnemonic = Mnemonic::from_entropy_with_language(&entropy, Language::Japanese).unwrap();

        let phrase = mnemonic.phrase();

        // The phrase should contain ideographic spaces (U+3000), not regular spaces
        assert!(
            phrase.contains('\u{3000}'),
            "Japanese mnemonic should use ideographic space separator"
        );
        assert!(
            !phrase.contains(' '),
            "Japanese mnemonic should not contain regular spaces"
        );

        // Verify word count by splitting on ideographic space
        let words: Vec<&str> = phrase.split('\u{3000}').collect();
        assert_eq!(words.len(), 12);

        // Each word should be a valid Japanese BIP-39 word
        let wordlist = &wordlists::JAPANESE;
        for word in &words {
            assert!(
                wordlist.contains(word),
                "Word '{}' not found in Japanese wordlist",
                word
            );
        }
    }

    #[test]
    fn test_language_word_count() {
        // Verify each wordlist has exactly 2048 words
        assert_eq!(wordlists::CHINESE_SIMPLIFIED.len(), 2048);
        assert_eq!(wordlists::CHINESE_TRADITIONAL.len(), 2048);
        assert_eq!(wordlists::CZECH.len(), 2048);
        assert_eq!(wordlists::ENGLISH.len(), 2048);
        assert_eq!(wordlists::FRENCH.len(), 2048);
        assert_eq!(wordlists::ITALIAN.len(), 2048);
        assert_eq!(wordlists::JAPANESE.len(), 2048);
        assert_eq!(wordlists::KOREAN.len(), 2048);
        assert_eq!(wordlists::SPANISH.len(), 2048);
    }

    #[test]
    fn test_language_default_is_english() {
        let default_lang = Language::default();
        assert_eq!(default_lang, Language::English);
    }

    #[test]
    fn test_non_english_from_phrase_roundtrip() {
        // Test that from_phrase_with_language can parse back what from_entropy generates
        let languages = [
            Language::ChineseSimplified,
            Language::ChineseTraditional,
            Language::Czech,
            Language::French,
            Language::Italian,
            Language::Japanese,
            Language::Korean,
            Language::Spanish,
        ];

        for lang in &languages {
            let entropy = [0x42u8; 16];
            let mnemonic = Mnemonic::from_entropy_with_language(&entropy, *lang).unwrap();
            let phrase = mnemonic.phrase();

            // Parse the phrase back
            let restored = Mnemonic::from_phrase_with_language(&phrase, *lang).unwrap();
            assert_eq!(
                mnemonic.entropy(),
                restored.entropy(),
                "Entropy mismatch for {:?}",
                lang
            );
        }
    }
}