mirror of
https://github.com/tcsenpai/falconTS.git
synced 2025-06-03 02:00:08 +00:00
121 lines
4.1 KiB
TypeScript
121 lines
4.1 KiB
TypeScript
import { uint8ArrayToMnemonic, mnemonicToUint8Array, validateMnemonic, generateMnemonic } from '../src/mnemonic';
|
|
import Falcon from '../src/falcon';
|
|
|
|
describe('Mnemonic Implementation', () => {
|
|
describe('Basic Functionality', () => {
|
|
it('should generate valid mnemonics', () => {
|
|
const mnemonic = generateMnemonic();
|
|
expect(validateMnemonic(mnemonic)).toBe(true);
|
|
|
|
// Check mnemonic format
|
|
const words = mnemonic.split(' ');
|
|
expect(words.length).toBe(12);
|
|
});
|
|
|
|
it('should convert between Uint8Array and mnemonic', () => {
|
|
const originalData = new Uint8Array([1, 2, 3, 4, 5]);
|
|
const mnemonic = uint8ArrayToMnemonic(originalData);
|
|
const recoveredData = mnemonicToUint8Array(mnemonic);
|
|
|
|
// The recovered data should be 48 bytes (required for Falcon)
|
|
expect(recoveredData.length).toBe(48);
|
|
});
|
|
|
|
it('should validate mnemonics correctly', () => {
|
|
const validMnemonic = generateMnemonic();
|
|
expect(validateMnemonic(validMnemonic)).toBe(true);
|
|
|
|
const invalidMnemonic = 'not a valid mnemonic phrase';
|
|
expect(validateMnemonic(invalidMnemonic)).toBe(false);
|
|
});
|
|
});
|
|
|
|
describe('Falcon Integration', () => {
|
|
let falcon: Falcon;
|
|
let mnemonic: string;
|
|
let seed: Uint8Array;
|
|
|
|
beforeEach(async () => {
|
|
// Generate a new mnemonic and initialize Falcon
|
|
mnemonic = generateMnemonic();
|
|
seed = mnemonicToUint8Array(mnemonic);
|
|
|
|
falcon = new Falcon();
|
|
await falcon.init();
|
|
});
|
|
|
|
it('should generate valid 48-byte seeds', () => {
|
|
const seed = mnemonicToUint8Array(mnemonic);
|
|
expect(seed.length).toBe(48);
|
|
});
|
|
|
|
it('should generate working key pairs from mnemonic', async () => {
|
|
// Generate keys from seed
|
|
await falcon.genkey(seed);
|
|
const keypair = await falcon.getKeypair();
|
|
|
|
expect(keypair.pk).toBeDefined();
|
|
expect(keypair.sk).toBeDefined();
|
|
});
|
|
|
|
it('should recover working keys from mnemonic', async () => {
|
|
// Generate original keys
|
|
await falcon.genkey(seed);
|
|
const originalKeypair = await falcon.getKeypair();
|
|
|
|
// Create new instance and recover keys
|
|
const recoveredFalcon = new Falcon();
|
|
await recoveredFalcon.init();
|
|
await recoveredFalcon.genkey(seed);
|
|
const recoveredKeypair = await recoveredFalcon.getKeypair();
|
|
|
|
// Compare keys
|
|
expect(Buffer.from(recoveredKeypair.pk).toString('hex'))
|
|
.toBe(Buffer.from(originalKeypair.pk).toString('hex'));
|
|
expect(Buffer.from(recoveredKeypair.sk).toString('hex'))
|
|
.toBe(Buffer.from(originalKeypair.sk).toString('hex'));
|
|
});
|
|
|
|
it('should sign and verify with recovered keys', async () => {
|
|
// Generate original keys and sign a message
|
|
await falcon.genkey(seed);
|
|
const message = 'Test message';
|
|
const signature = await falcon.sign(message);
|
|
|
|
// Verify with original keys
|
|
const originalVerification = await falcon.verify(message, signature);
|
|
expect(originalVerification).toBe(true);
|
|
|
|
// Recover keys in new instance
|
|
const recoveredFalcon = new Falcon();
|
|
await recoveredFalcon.init();
|
|
await recoveredFalcon.genkey(seed);
|
|
|
|
// Sign with recovered keys
|
|
const newMessage = 'Another test message';
|
|
const newSignature = await recoveredFalcon.sign(newMessage);
|
|
|
|
// Verify with recovered keys
|
|
const recoveredVerification = await recoveredFalcon.verify(newMessage, newSignature);
|
|
expect(recoveredVerification).toBe(true);
|
|
|
|
// Cross-verify: verify new signature with original instance
|
|
const crossVerification = await falcon.verify(newMessage, newSignature);
|
|
expect(crossVerification).toBe(true);
|
|
});
|
|
|
|
it('should handle invalid mnemonics', () => {
|
|
const invalidMnemonics = [
|
|
'invalid mnemonic',
|
|
'too few words',
|
|
'too many words in this mnemonic phrase which should fail',
|
|
''
|
|
];
|
|
|
|
invalidMnemonics.forEach(mnemonic => {
|
|
expect(validateMnemonic(mnemonic)).toBe(false);
|
|
expect(() => mnemonicToUint8Array(mnemonic)).toThrow();
|
|
});
|
|
});
|
|
});
|
|
});
|