import { describe, it, expect } from 'vitest'; import { Bytes } from '~/src/bytes'; import { Mnemonic, MnemonicRaw } from '~/src/mnemonic'; describe('Success cases', () => { // Simple array of cases const testCases: Array<{ fromPhrase: string; fromWords: Array; passphrase?: string; raw: MnemonicRaw; toSeed: Uint8Array; toPhrase: string; toPhraseWithoutPassphrase: string; toWords: Array; toWordsWithoutPassphrase: Array; }> = [ // No pass-phrase { fromPhrase: 'goose just fan kidney barrel album wave crowd movie flame cousin exhaust', fromWords: [ 'goose', 'just', 'fan', 'kidney', 'barrel', 'album', 'wave', 'crowd', 'movie', 'flame', 'cousin', 'exhaust', ], raw: { words: [ 'goose', 'just', 'fan', 'kidney', 'barrel', 'album', 'wave', 'crowd', 'movie', 'flame', 'cousin', 'exhaust', ], }, toSeed: Bytes.fromHex( '7066f43a742829f0edce7d8e3c31651df320f13913995a6500f4e515db372b35202227f923ff181defc35cea02f8468df39f8fa9d97a202d252e61562e4bbfb5', ).toBytes(), toPhrase: 'goose just fan kidney barrel album wave crowd movie flame cousin exhaust', toPhraseWithoutPassphrase: 'goose just fan kidney barrel album wave crowd movie flame cousin exhaust', toWords: [ 'goose', 'just', 'fan', 'kidney', 'barrel', 'album', 'wave', 'crowd', 'movie', 'flame', 'cousin', 'exhaust', ], toWordsWithoutPassphrase: [ 'goose', 'just', 'fan', 'kidney', 'barrel', 'album', 'wave', 'crowd', 'movie', 'flame', 'cousin', 'exhaust', ], }, // Passphrase { fromPhrase: 'goose just fan kidney barrel album wave crowd movie flame cousin exhaust GeneralProtocols!', fromWords: [ 'goose', 'just', 'fan', 'kidney', 'barrel', 'album', 'wave', 'crowd', 'movie', 'flame', 'cousin', 'exhaust', 'GeneralProtocols!', ], passphrase: 'GeneralProtocols!', raw: { words: [ 'goose', 'just', 'fan', 'kidney', 'barrel', 'album', 'wave', 'crowd', 'movie', 'flame', 'cousin', 'exhaust', ], passphrase: 'GeneralProtocols!', }, toSeed: Bytes.fromHex( '2f706756ab2083f7ce07c0c0fcde8f14f062da5de839f42763262b25e0d871f03dd2b561d55eaacc471c94da656ee7b0007fb48a64e31ec91a1a2ba706b46b89', ).toBytes(), toPhrase: 'goose just fan kidney barrel album wave crowd movie flame cousin exhaust GeneralProtocols!', toPhraseWithoutPassphrase: 'goose just fan kidney barrel album wave crowd movie flame cousin exhaust', toWords: [ 'goose', 'just', 'fan', 'kidney', 'barrel', 'album', 'wave', 'crowd', 'movie', 'flame', 'cousin', 'exhaust', 'GeneralProtocols!', ], toWordsWithoutPassphrase: [ 'goose', 'just', 'fan', 'kidney', 'barrel', 'album', 'wave', 'crowd', 'movie', 'flame', 'cousin', 'exhaust', ], }, ]; it.each(testCases)('from(string)', ({ fromPhrase: phrase, raw }) => { const mnemonic = Mnemonic.from(phrase); expect(mnemonic.toRaw()).toStrictEqual(raw); }); it.each(testCases)('from(words)', ({ fromWords, raw }) => { const mnemonic = Mnemonic.from(fromWords); expect(mnemonic.toRaw()).toStrictEqual(raw); }); it.each(testCases)('fromPhrase', ({ fromPhrase, raw }) => { const mnemonic = Mnemonic.fromPhrase(fromPhrase); expect(mnemonic.toRaw()).toStrictEqual(raw); }); it.each(testCases)('fromWords', ({ fromWords, raw }) => { const mnemonic = Mnemonic.fromWords(fromWords); expect(mnemonic.toRaw()).toStrictEqual(raw); }); it.each(testCases)('fromWords', ({ fromWords, raw }) => { const mnemonic = Mnemonic.fromWords(fromWords); expect(mnemonic.toRaw()).toStrictEqual(raw); }); it.each(testCases)('toSeed', ({ toSeed, raw }) => { const mnemonic = Mnemonic.fromRaw(raw); expect(mnemonic.toSeed()).toStrictEqual(toSeed); }); it.each(testCases)('toPhrase', ({ toPhrase, raw }) => { const mnemonic = Mnemonic.fromRaw(raw); expect(mnemonic.toPhrase()).toStrictEqual(toPhrase); }); it.each(testCases)( 'toPhrase(withoutPassphrase)', ({ toPhraseWithoutPassphrase, raw }) => { const mnemonic = Mnemonic.fromRaw(raw); expect(mnemonic.toPhrase(false)).toStrictEqual(toPhraseWithoutPassphrase); }, ); it.each(testCases)('toWords', ({ toWords, raw }) => { const mnemonic = Mnemonic.fromRaw(raw); expect(mnemonic.toWords()).toStrictEqual(toWords); }); it.each(testCases)( 'toWords(withoutPassphrase)', ({ toWordsWithoutPassphrase, passphrase, raw }) => { const mnemonic = Mnemonic.fromRaw(raw); expect(mnemonic.toWords(false)).toStrictEqual(toWordsWithoutPassphrase); }, ); it.each(testCases)('getPassphrase', ({ passphrase, raw }) => { const mnemonic = Mnemonic.fromRaw(raw); expect(mnemonic.getPassphrase()).toStrictEqual(passphrase); }); }); describe('Generate Cases', () => { it('generateRandom should return instance of Mnemonic', () => { const mnemonic = Mnemonic.generateRandom(); expect(mnemonic).toBeInstanceOf(Mnemonic); }); it('generateRandom should return instance of Mnemonic with passphrase', () => { const mnemonic = Mnemonic.generateRandom('GeneralProtocols!'); expect(mnemonic).toBeInstanceOf(Mnemonic); expect(mnemonic.getPassphrase()).toBe('GeneralProtocols!'); }); }); describe('Validations', () => { it('isValid should succeed if valid mnemonic', () => { const isValid = Mnemonic.isValid( 'goose just fan kidney barrel album wave crowd movie flame cousin exhaust', ); expect(isValid).toBe(true); }); it('isValid should fail if invalid mnemonic', () => { // 11 words. const isValid = Mnemonic.isValid( 'goose just fan kidney barrel album wave crowd movie flame cousin', ); expect(isValid).toBe(false); }); it('isValidWord should succeed if word is in word list', () => { const isValid = Mnemonic.isValidWord('goose'); expect(isValid).toBe(true); }); it('isValidWord should fail if word is not in word list', () => { const isValid = Mnemonic.isValidWord('fnord'); expect(isValid).toBe(false); }); }); describe('Fail cases', () => { it('fromPhrase should fail if invalid number of words', () => { expect(() => { // 11 words. Mnemonic.fromPhrase( 'goose just fan kidney barrel album wave crowd movie flame cousin', ); }).toThrow(); }); it('generateRandom should fail if invalid wordList contains invalid number of words', () => { expect(() => { // 11 words. Mnemonic.generateRandom('GeneralProtocols!', ['one', 'two', 'three']); }).toThrow(); }); it('fromWords should fail if invalid number of words', () => { expect(() => { // 11 words. Mnemonic.fromWords([ 'goose', 'just', 'fan', 'kidney', 'barrel', 'album', 'wave', 'crowd', 'movie', 'flame', 'cousin', ]); }).toThrow(); }); it('fromPhrase should fail if word not in dictionary', () => { expect(() => { // Invalid word "fnord". Mnemonic.fromPhrase( 'fnord just fan kidney barrel album wave crowd movie flame cousin exhaust', ); }).toThrow(); }); it('fromWord should fail if word not in dictionary', () => { expect(() => { // Invalid word "fnord". Mnemonic.fromWords([ 'fnord', 'just', 'fan', 'kidney', 'barrel', 'album', 'wave', 'crowd', 'movie', 'flame', 'cousin', 'exhaust', ]); }).toThrow(); }); });