meta-log-db
Version:
Native database package for Meta-Log (ProLog, DataLog, R5RS)
147 lines • 7.39 kB
JavaScript
"use strict";
/**
* Browser Crypto Tests
*
* Tests for BIP32/39/44 cryptographic implementation and storage encryption.
*
* @see {@link https://github.com/automaton-system/meta-log-db/blob/main/docs/27-Meta-Log-Browser-Db/BIP32-39-44-INTEGRATION.md BIP32/39/44 Integration Guide}
* @see {@link https://github.com/automaton-system/meta-log-db/blob/main/docs/27-Meta-Log-Browser-Db/README.md Meta-Log Browser Database Documentation}
*
* Related Documentation:
* - meta-log-browser-db-bip32-39-44-guide: Complete BIP32/39/44 guide
* - meta-log-browser-db-readme: Browser database overview
* - meta-log-browser-db-indexeddb-guide: Storage encryption usage
*
* Test Coverage:
* - BIP39 mnemonic generation and validation
* - BIP32 HD key derivation
* - BIP44 standard derivation paths
* - Storage encryption/decryption
* - Key derivation from mnemonics
*/
Object.defineProperty(exports, "__esModule", { value: true });
const bip39_1 = require("../crypto/bip39");
const bip32_1 = require("../crypto/bip32");
const bip44_1 = require("../crypto/bip44");
const storage_encryption_1 = require("../crypto/storage-encryption");
describe('BIP39', () => {
describe('generateMnemonic', () => {
it('should generate mnemonic with 256-bit strength', async () => {
const mnemonic = await (0, bip39_1.generateMnemonic)(256);
expect(mnemonic).toBeDefined();
expect(typeof mnemonic).toBe('string');
expect(mnemonic.split(' ').length).toBeGreaterThan(0);
});
it('should generate mnemonic with 128-bit strength', async () => {
const mnemonic = await (0, bip39_1.generateMnemonic)(128);
expect(mnemonic).toBeDefined();
expect(typeof mnemonic).toBe('string');
});
});
describe('validateMnemonic', () => {
it('should validate valid mnemonic', async () => {
const mnemonic = await (0, bip39_1.generateMnemonic)(256);
const isValid = (0, bip39_1.validateMnemonic)(mnemonic);
expect(isValid).toBe(true);
});
it('should reject invalid mnemonic', () => {
const isValid = (0, bip39_1.validateMnemonic)('invalid mnemonic phrase');
expect(isValid).toBe(false);
});
});
describe('mnemonicToSeed', () => {
it('should convert mnemonic to seed', async () => {
const mnemonic = await (0, bip39_1.generateMnemonic)(256);
const seed = await (0, bip39_1.mnemonicToSeed)(mnemonic);
expect(seed).toBeInstanceOf(Uint8Array);
expect(seed.length).toBe(64); // 512 bits = 64 bytes
});
it('should convert mnemonic to seed with passphrase', async () => {
const mnemonic = await (0, bip39_1.generateMnemonic)(256);
const seed1 = await (0, bip39_1.mnemonicToSeed)(mnemonic);
const seed2 = await (0, bip39_1.mnemonicToSeed)(mnemonic, 'passphrase');
// Seeds should be different with different passphrases
expect(seed1).not.toEqual(seed2);
});
});
});
describe('BIP32', () => {
describe('deriveKey', () => {
it('should derive key from seed', async () => {
const mnemonic = await (0, bip39_1.generateMnemonic)(256);
const seed = await (0, bip39_1.mnemonicToSeed)(mnemonic);
const key = await (0, bip32_1.deriveKey)(seed, "m/44'/60'/0'/0/0");
expect(key).toBeDefined();
expect(key.algorithm.name).toBe('AES-GCM');
});
});
});
describe('BIP44', () => {
describe('deriveStorageKey', () => {
it('should derive storage key for local purpose', async () => {
const mnemonic = await (0, bip39_1.generateMnemonic)(256);
const key = await (0, bip44_1.deriveStorageKey)(mnemonic, 'local');
expect(key).toBeDefined();
expect(key.algorithm.name).toBe('AES-GCM');
});
it('should derive different keys for different purposes', async () => {
const mnemonic = await (0, bip39_1.generateMnemonic)(256);
const key1 = await (0, bip44_1.deriveStorageKey)(mnemonic, 'local');
const key2 = await (0, bip44_1.deriveStorageKey)(mnemonic, 'published');
// Keys should be different
const key1Raw = await crypto.subtle.exportKey('raw', key1);
const key2Raw = await crypto.subtle.exportKey('raw', key2);
expect(key1Raw).not.toEqual(key2Raw);
});
});
describe('StorageDerivationPaths', () => {
it('should provide standard paths', () => {
expect(bip44_1.StorageDerivationPaths.LOCAL_PRIVATE).toBeDefined();
expect(bip44_1.StorageDerivationPaths.PUBLISHED_ROOT).toBeDefined();
expect(bip44_1.StorageDerivationPaths.CONTRIBUTOR_SIGNING).toBeDefined();
expect(bip44_1.StorageDerivationPaths.EPHEMERAL_SHARING).toBeDefined();
});
it('should generate helper paths', () => {
const manifestPath = bip44_1.StorageDerivationPaths.publishedManifest(0, 1);
expect(manifestPath).toContain("m/44'/999'/1'");
const contributorPath = bip44_1.StorageDerivationPaths.contributorKey(0);
expect(contributorPath).toContain("m/44'/999'/2'");
});
});
});
describe('Storage Encryption', () => {
describe('encryptData / decryptData', () => {
it('should encrypt and decrypt data', async () => {
const mnemonic = await (0, bip39_1.generateMnemonic)(256);
const key = await (0, bip44_1.deriveStorageKey)(mnemonic, 'local');
const data = 'sensitive data';
const encrypted = await (0, storage_encryption_1.encryptData)(data, key);
const decrypted = await (0, storage_encryption_1.decryptData)(encrypted, key);
expect(decrypted).toBe(data);
});
it('should produce different encrypted data for same input', async () => {
const mnemonic = await (0, bip39_1.generateMnemonic)(256);
const key = await (0, bip44_1.deriveStorageKey)(mnemonic, 'local');
const data = 'sensitive data';
const encrypted1 = await (0, storage_encryption_1.encryptData)(data, key);
const encrypted2 = await (0, storage_encryption_1.encryptData)(data, key);
// Should be different due to random IV
expect(encrypted1).not.toBe(encrypted2);
// But both should decrypt to same data
const decrypted1 = await (0, storage_encryption_1.decryptData)(encrypted1, key);
const decrypted2 = await (0, storage_encryption_1.decryptData)(encrypted2, key);
expect(decrypted1).toBe(data);
expect(decrypted2).toBe(data);
});
});
describe('encryptDataWithMnemonic / decryptDataWithMnemonic', () => {
it('should encrypt and decrypt with mnemonic', async () => {
const mnemonic = await (0, bip39_1.generateMnemonic)(256);
const data = 'sensitive data';
const encrypted = await (0, storage_encryption_1.encryptDataWithMnemonic)(data, mnemonic, 'local');
const decrypted = await (0, storage_encryption_1.decryptDataWithMnemonic)(encrypted, mnemonic, 'local');
expect(decrypted).toBe(data);
});
});
});
//# sourceMappingURL=crypto.test.js.map