Amend Primitives/Tooling
This commit is contained in:
@@ -1,21 +1,23 @@
|
||||
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<{
|
||||
fromString: string;
|
||||
fromPhrase: string;
|
||||
fromWords: Array<string>;
|
||||
passphrase?: string;
|
||||
raw: MnemonicRaw;
|
||||
toString: string;
|
||||
toStringWithoutPassphrase: string;
|
||||
toSeed: Uint8Array;
|
||||
toPhrase: string;
|
||||
toPhraseWithoutPassphrase: string;
|
||||
toWords: Array<string>;
|
||||
toWordsWithoutPassphrase: Array<string>;
|
||||
}> = [
|
||||
// No pass-phrase
|
||||
{
|
||||
fromString:
|
||||
fromPhrase:
|
||||
'goose just fan kidney barrel album wave crowd movie flame cousin exhaust',
|
||||
fromWords: [
|
||||
'goose',
|
||||
@@ -47,9 +49,12 @@ describe('Success cases', () => {
|
||||
'exhaust',
|
||||
],
|
||||
},
|
||||
toString:
|
||||
toSeed: Bytes.fromHex(
|
||||
'7066f43a742829f0edce7d8e3c31651df320f13913995a6500f4e515db372b35202227f923ff181defc35cea02f8468df39f8fa9d97a202d252e61562e4bbfb5',
|
||||
).toBytes(),
|
||||
toPhrase:
|
||||
'goose just fan kidney barrel album wave crowd movie flame cousin exhaust',
|
||||
toStringWithoutPassphrase:
|
||||
toPhraseWithoutPassphrase:
|
||||
'goose just fan kidney barrel album wave crowd movie flame cousin exhaust',
|
||||
toWords: [
|
||||
'goose',
|
||||
@@ -82,7 +87,7 @@ describe('Success cases', () => {
|
||||
},
|
||||
// Passphrase
|
||||
{
|
||||
fromString:
|
||||
fromPhrase:
|
||||
'goose just fan kidney barrel album wave crowd movie flame cousin exhaust GeneralProtocols!',
|
||||
fromWords: [
|
||||
'goose',
|
||||
@@ -117,9 +122,12 @@ describe('Success cases', () => {
|
||||
],
|
||||
passphrase: 'GeneralProtocols!',
|
||||
},
|
||||
toString:
|
||||
toSeed: Bytes.fromHex(
|
||||
'2f706756ab2083f7ce07c0c0fcde8f14f062da5de839f42763262b25e0d871f03dd2b561d55eaacc471c94da656ee7b0007fb48a64e31ec91a1a2ba706b46b89',
|
||||
).toBytes(),
|
||||
toPhrase:
|
||||
'goose just fan kidney barrel album wave crowd movie flame cousin exhaust GeneralProtocols!',
|
||||
toStringWithoutPassphrase:
|
||||
toPhraseWithoutPassphrase:
|
||||
'goose just fan kidney barrel album wave crowd movie flame cousin exhaust',
|
||||
toWords: [
|
||||
'goose',
|
||||
@@ -153,44 +161,46 @@ describe('Success cases', () => {
|
||||
},
|
||||
];
|
||||
|
||||
it.each(testCases)(
|
||||
'from(string)',
|
||||
({ fromString: phrase, raw: expected }) => {
|
||||
const mnemonic = Mnemonic.from(phrase);
|
||||
expect(mnemonic.toRaw()).toStrictEqual(expected);
|
||||
},
|
||||
);
|
||||
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: expected }) => {
|
||||
it.each(testCases)('from(words)', ({ fromWords, raw }) => {
|
||||
const mnemonic = Mnemonic.from(fromWords);
|
||||
expect(mnemonic.toRaw()).toStrictEqual(expected);
|
||||
expect(mnemonic.toRaw()).toStrictEqual(raw);
|
||||
});
|
||||
|
||||
it.each(testCases)('fromString', ({ fromString, raw: expected }) => {
|
||||
const mnemonic = Mnemonic.fromString(fromString);
|
||||
expect(mnemonic.toRaw()).toStrictEqual(expected);
|
||||
it.each(testCases)('fromPhrase', ({ fromPhrase, raw }) => {
|
||||
const mnemonic = Mnemonic.fromPhrase(fromPhrase);
|
||||
expect(mnemonic.toRaw()).toStrictEqual(raw);
|
||||
});
|
||||
|
||||
it.each(testCases)('fromWords', ({ fromWords, raw: expected }) => {
|
||||
it.each(testCases)('fromWords', ({ fromWords, raw }) => {
|
||||
const mnemonic = Mnemonic.fromWords(fromWords);
|
||||
expect(mnemonic.toRaw()).toStrictEqual(expected);
|
||||
expect(mnemonic.toRaw()).toStrictEqual(raw);
|
||||
});
|
||||
|
||||
it.each(testCases)('fromWords', ({ fromWords, raw: expected }) => {
|
||||
it.each(testCases)('fromWords', ({ fromWords, raw }) => {
|
||||
const mnemonic = Mnemonic.fromWords(fromWords);
|
||||
expect(mnemonic.toRaw()).toStrictEqual(expected);
|
||||
expect(mnemonic.toRaw()).toStrictEqual(raw);
|
||||
});
|
||||
|
||||
it.each(testCases)('toString', ({ toString, raw }) => {
|
||||
it.each(testCases)('toSeed', ({ toSeed, raw }) => {
|
||||
const mnemonic = Mnemonic.fromRaw(raw);
|
||||
expect(mnemonic.toString()).toStrictEqual(toString);
|
||||
expect(mnemonic.toSeed()).toStrictEqual(toSeed);
|
||||
});
|
||||
|
||||
it.each(testCases)('toPhrase', ({ toPhrase, raw }) => {
|
||||
const mnemonic = Mnemonic.fromRaw(raw);
|
||||
expect(mnemonic.toPhrase()).toStrictEqual(toPhrase);
|
||||
});
|
||||
|
||||
it.each(testCases)(
|
||||
'toString(withoutPassphrase)',
|
||||
({ toStringWithoutPassphrase, raw }) => {
|
||||
'toPhrase(withoutPassphrase)',
|
||||
({ toPhraseWithoutPassphrase, raw }) => {
|
||||
const mnemonic = Mnemonic.fromRaw(raw);
|
||||
expect(mnemonic.toString(false)).toStrictEqual(toStringWithoutPassphrase);
|
||||
expect(mnemonic.toPhrase(false)).toStrictEqual(toPhraseWithoutPassphrase);
|
||||
},
|
||||
);
|
||||
|
||||
@@ -254,10 +264,10 @@ describe('Validations', () => {
|
||||
});
|
||||
|
||||
describe('Fail cases', () => {
|
||||
it('fromString should fail if invalid number of words', () => {
|
||||
it('fromPhrase should fail if invalid number of words', () => {
|
||||
expect(() => {
|
||||
// 11 words.
|
||||
Mnemonic.fromString(
|
||||
Mnemonic.fromPhrase(
|
||||
'goose just fan kidney barrel album wave crowd movie flame cousin',
|
||||
);
|
||||
}).toThrow();
|
||||
@@ -289,10 +299,10 @@ describe('Fail cases', () => {
|
||||
}).toThrow();
|
||||
});
|
||||
|
||||
it('fromString should fail if word not in dictionary', () => {
|
||||
it('fromPhrase should fail if word not in dictionary', () => {
|
||||
expect(() => {
|
||||
// Invalid word "fnord".
|
||||
Mnemonic.fromString(
|
||||
Mnemonic.fromPhrase(
|
||||
'fnord just fan kidney barrel album wave crowd movie flame cousin exhaust',
|
||||
);
|
||||
}).toThrow();
|
||||
|
||||
Reference in New Issue
Block a user