rose-squared-sdk 0.1.0

Privacy-preserving encrypted search SDK implementing the SWiSSSE protocol with forward/backward security and volume-hiding, compilable to WebAssembly
Documentation

import init, { WasmVault } from '../../pkg/rose_squared_sdk.js';

async function main() {
    await init();

    const log = document.getElementById('log');
    const passwordInput = document.getElementById('password');
    const saltInput = document.getElementById('salt');
    const keywordsInput = document.getElementById('keywords');
    const docIdInput = document.getElementById('docId');
    const searchKeywordInput = document.getElementById('searchKeyword');

    let vault;

    function hexToBytes(hex) {
        const bytes = new Uint8Array(hex.length / 2);
        for (let i = 0; i < hex.length; i += 2) {
            bytes[i / 2] = parseInt(hex.substr(i, 2), 16);
        }
        return bytes;
    }

    document.getElementById('initVault').addEventListener('click', async () => {
        try {
            const password = passwordInput.value;
            const salt = hexToBytes(saltInput.value);
            vault = await WasmVault.new(password, salt);
            log.textContent = 'Vault initialized successfully.';
        } catch (e) {
            log.textContent = `Error: ${e}`;
        }
    });

    document.getElementById('addDocument').addEventListener('click', async () => {
        if (!vault) {
            log.textContent = 'Initialize the vault first.';
            return;
        }
        try {
            const keywords = keywordsInput.value.split(',').map(k => k.trim());
            const docId = docIdInput.value;
            await vault.add_document(keywords, docId);
            log.textContent = `Document "${docId}" added with keywords: ${keywords.join(', ')}`;
        } catch (e) {
            log.textContent = `Error: ${e}`;
        }
    });

    document.getElementById('search').addEventListener('click', async () => {
        if (!vault) {
            log.textContent = 'Initialize the vault first.';
            return;
        }
        try {
            const keyword = searchKeywordInput.value;
            const results = await vault.search(keyword);
            log.textContent = `Search results for "${keyword}":
${results.join('
')}`;
        } catch (e) {
            log.textContent = `Error: ${e}`;
        }
    });

    document.getElementById('exportState').addEventListener('click', async () => {
        if (!vault) {
            log.textContent = 'Initialize the vault first.';
            return;
        }
        try {
            const state = vault.export_state();
            // In a real app, you would save this to a file or send it to a server.
            console.log('Exported state:', state);
            log.textContent = 'State exported to the console.';
        } catch (e) {
            log.textContent = `Error: ${e}`;
        }
    });

    document.getElementById('importState').addEventListener('click', async () => {
        // This is just a placeholder. In a real app, you would load the state from a file.
        log.textContent = 'Importing state is not implemented in this example.';
    });
}

main();