22import assert from "node:assert" ;
33import { describe , it } from "node:test" ;
44import { wordlists } from "bip39" ;
5- import fetch from "node-fetch" ;
65
76import { calculateBip39Mnemonic } from "./calculateBip39Mnemonic.js" ;
87
@@ -12,16 +11,6 @@ if (!wordlist) {
1211 throw new Error ( "Missing english wordlist." ) ;
1312}
1413
15- const expectWords = async ( entropy : string , words : string ) => {
16- const wordCount = words . length === 0 ? 0 : words . split ( " " ) . length ;
17-
18- await it (
19- entropy ,
20- async ( ) => assert ( ( await calculateBip39Mnemonic ( entropy , wordCount , wordlist ) ) . join ( " " ) === words ) ,
21- ) ;
22- } ;
23-
24-
2514const expectError = async ( entropy : string , newWordlist : readonly string [ ] , errorMessage : string ) => await it (
2615 entropy ,
2716 async ( ) => {
@@ -34,58 +23,8 @@ const expectError = async (entropy: string, newWordlist: readonly string[], erro
3423 } ,
3524) ;
3625
37-
38- const response = await fetch ( "https://raw.githubusercontent.com/trezor/python-mnemonic/master/vectors.json" ) ;
39-
40- if ( ! response . ok ) {
41- throw new Error ( "Unexpected response" ) ;
42- }
43-
44- const vectors = JSON . parse ( await response . text ( ) ) as Record < string , unknown > ;
45-
4626await describe ( calculateBip39Mnemonic . name , async ( ) => {
47- await describe ( "should calculate the expected words" , async ( ) => {
48- if ( ! ( "english" in vectors ) || ! Array . isArray ( vectors [ "english" ] ) ) {
49- throw new Error ( "Unexpected response" ) ;
50- }
51-
52- for ( const vector of vectors [ "english" ] ) {
53- if ( ! Array . isArray ( vector ) || ( vector . length < 2 ) ||
54- ( typeof vector [ 0 ] !== "string" ) || ( typeof vector [ 1 ] !== "string" ) ) {
55- throw new Error ( "Unexpected response" ) ;
56- }
57-
58- // https://github.com/typescript-eslint/typescript-eslint/issues/7464
59- // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
60- const [ entropy , words ] = vector ;
61- // eslint-disable-next-line no-await-in-loop
62- await expectWords ( entropy , words ) ;
63- }
64-
65- await expectWords ( "" , "" ) ;
66- await expectWords ( "00000000" , "abandon abandon ability" ) ;
67- await expectWords ( "ffffffff" , "zoo zoo zoo" ) ;
68- } ) ;
69-
7027 await describe ( "should throw the expected exception" , async ( ) => {
71- await it ( "ffffffff" , async ( ) => {
72- try {
73- await calculateBip39Mnemonic ( "ffffffff" , 2 , wordlist ) ;
74- assert ( false , "Expected error to be thrown!" ) ;
75- } catch ( error : unknown ) {
76- assert ( error instanceof RangeError && error . message === "wordCount must be a multiple of 3" ) ;
77- }
78- } ) ;
79-
80- await it ( "fffffff" , async ( ) => {
81- try {
82- await calculateBip39Mnemonic ( "fffffff" , 3 , wordlist ) ;
83- assert ( false , "Expected error to be thrown!" ) ;
84- } catch ( error : unknown ) {
85- assert ( error instanceof RangeError && error . message === "hexEntropy length must be >= 8" ) ;
86- }
87- } ) ;
88-
8928 await expectError ( "ffffffff" , wordlist . slice ( 1 ) , "wordlist.length is invalid: 2047" ) ;
9029 await expectError ( "ffffffff" , wordlist . slice ( 1024 ) , "wordlist.length is invalid: 1024" ) ;
9130 const invalidWordlist = wordlist . slice ( - 1 ) ;
0 commit comments