import { extractErrorMsg, getTimestamp, getUUID, } from '@ibgib/helper-gib/dist/helpers/utils-helper.mjs'; import { Factory_V1 as factory } from '@ibgib/ts-gib/dist/V1/factory.mjs'; import { ROOT } from '@ibgib/ts-gib/dist/V1/constants.mjs'; import { getIbGibAddr, } from '@ibgib/ts-gib/dist/helper.mjs'; import { KeystoneService_V1 } from '@ibgib/core-gib/dist/keystone/keystone-service-v1.mjs'; import { KeystoneReplenishStrategy } from '@ibgib/core-gib/dist/keystone/keystone-types.mjs'; import { createManagePoolConfig } from '@ibgib/core-gib/dist/keystone/keystone-config-builder.mjs'; import { KeystoneStrategyFactory } from '@ibgib/core-gib/dist/keystone/strategy/keystone-strategy-factory.mjs'; import { KEYSTONE_VERB_CONNECT, POOL_ID_CONNECT } from '@ibgib/core-gib/dist/keystone/keystone-constants.mjs'; import { getGlobalMetaspace_waitIfNeeded } from "@ibgib/web-gib/dist/helpers.mjs"; import { SESSION_KEYSTONE_POLICY, getConnectChallenge } from "../../common/keystone-policies.mjs"; import { SpaceGibApiBridge } from '../api/space-gib-api-bridge.mjs'; import { debugState, devLog, lc } from './common.mjs'; let _ws: WebSocket | null = null; export function initWsTestButton(): void { const lc_fn = `${lc}[initWsTestButton]`; const btn = document.getElementById('btn-test-ws') as HTMLButtonElement | null; if (!btn) { console.warn(`${lc_fn} btn-test-ws not found — skipping`); return; } btn.addEventListener('click', () => { try { if (_ws && _ws.readyState === WebSocket.OPEN) { _ws.send(JSON.stringify({ ping: 'hello from client', ts: Date.now() })); devLog('→ sent ping on existing connection'); return; } const protocol = location.protocol === 'https:' ? 'wss:' : 'ws:'; const wsUrl = `${protocol}//${location.host}/api/debug/ws-echo`; devLog(`→ connecting to ${wsUrl}`); _ws = new WebSocket(wsUrl); _ws.addEventListener('open', () => { devLog('✓ WebSocket connected'); btn.textContent = 'Send WS Ping'; }); _ws.addEventListener('message', (ev) => { devLog(`← ${ev.data}`); }); _ws.addEventListener('close', (ev) => { devLog(`✗ WebSocket closed (code ${ev.code})`); btn.textContent = 'Test WebSocket'; _ws = null; }); _ws.addEventListener('error', (ev) => { devLog(`✗ WebSocket error — check console`); console.error(`${lc_fn} WebSocket error`, ev); }); } catch (error) { devLog(`✗ ${extractErrorMsg(error)}`); console.error(`${lc_fn} ${extractErrorMsg(error)}`); } }); } export function initPrintStateButton(): void { const btn = document.getElementById('btn-print-dev-state') as HTMLButtonElement | null; if (!btn) { return; } btn.addEventListener('click', () => { devLog(`Printing debug state to console...`); console.dir(debugState); }); } export function initCreateDomainKeystoneButton(): void { const lc_fn = `${lc}[initCreateDomainKeystoneButton]`; const btn = document.getElementById('btn-create-domain-keystone') as HTMLButtonElement | null; if (!btn) { return; } btn.addEventListener('click', async () => { try { btn.disabled = true; devLog('Generating dummy Domain Keystone (I)...'); const metaspace = await getGlobalMetaspace_waitIfNeeded(); const space = await metaspace.getLocalUserSpace({}); if (!space) { throw new Error("No default space found in metaspace."); } const masterSecret = 'password'; const keystoneService = new KeystoneService_V1(); const domainI = await keystoneService.genesis({ masterSecret, configs: [ createManagePoolConfig({ id: 'default', salt: await getUUID(), replenishStrategy: KeystoneReplenishStrategy.topUp, }), ], metaspace, space, frameDetails: { client: 'space-gib-web-dev', timestamp: getTimestamp() } }); const iAddr = getIbGibAddr({ ibGib: domainI }); devLog(`✓ Local genesis complete: ${iAddr}`); debugState.domainI = domainI; debugState.domainIMasterSecret = masterSecret; devLog(`Posting genesis keystone to server...`); const apiBridge = new SpaceGibApiBridge(); const resSync = await apiBridge.postGenesisKeystone(domainI); if (!resSync.success) { devLog(`✗ Server rejected genesis keystone: ${resSync.message}`); btn.disabled = false; return; } devLog(`✓ Server accepted Domain Keystone (I)`); btn.textContent = '✓ Domain Keystone Created'; const nextBtn = document.getElementById('btn-create-test-ibgib') as HTMLButtonElement; if (nextBtn) { nextBtn.disabled = false; } } catch (error) { devLog(`✗ ${extractErrorMsg(error)}`); console.error(`${lc_fn} ${extractErrorMsg(error)}`); btn.disabled = false; } }); } export function initCreateTestIbGibButton(): void { const lc_fn = `${lc}[initCreateTestIbGibButton]`; const btn = document.getElementById('btn-create-test-ibgib') as HTMLButtonElement | null; if (!btn) { console.warn(`${lc_fn} btn-create-test-ibgib not found — skipping`); return; } btn.addEventListener('click', async () => { try { devLog('Generating dummy X^Xtjp ibgib...'); const resX = await factory.firstGen({ parentIbGib: ROOT, ib: 'test data', data: { hello: 'world', random: Math.random() }, dna: true, nCounter: true, tjp: { uuid: true, timestamp: true } }); const xIbGib = resX.newIbGib; const xAddr = getIbGibAddr({ ibGib: xIbGib }); debugState.targetX = xIbGib; devLog(`✓ Created X: ${xAddr}`); btn.textContent = '✓ Test IbGib (X) Created'; const nextBtn = document.getElementById('btn-create-session-keystone') as HTMLButtonElement; if (nextBtn) { nextBtn.disabled = false; } } catch (error) { devLog(`✗ ${extractErrorMsg(error)}`); console.error(`${lc_fn} ${extractErrorMsg(error)}`); } }); } export function initCreateSessionKeystoneButton(): void { const lc_fn = `${lc}[initCreateSessionKeystoneButton]`; const btn = document.getElementById('btn-create-session-keystone') as HTMLButtonElement | null; if (!btn) { console.warn(`${lc_fn} btn-create-session-keystone not found — skipping`); return; } btn.addEventListener('click', async () => { try { devLog('Generating Session Keystone (S^Stjp)...'); const domainI = debugState.domainI; const targetX = debugState.targetX; if (!domainI) { devLog('⚠ No domain keystone (I) found in state. Please Create Domain Keystone first.'); return; } if (!targetX) { devLog('⚠ No target ibgib (X) found in state. Please Create Test IbGib first.'); return; } const metaspace = await getGlobalMetaspace_waitIfNeeded(); const space = await metaspace.getLocalUserSpace({}); if (!space) { throw new Error("No default space found in metaspace."); } const sessionSecret = 'ephemeral-' + Math.random().toString(); const keystoneService = new KeystoneService_V1(); console.warn(`${lc}[NAG] placeholder genesis keystone config right now (W: 69b108687cf8bec0f9a8e148bcca8d26)`); const sIbGib = await keystoneService.genesis({ masterSecret: sessionSecret, configs: [ { id: SESSION_KEYSTONE_POLICY.DEFAULT_POOL.ID, salt: await getUUID(), type: SESSION_KEYSTONE_POLICY.COMMON.TYPE, algo: SESSION_KEYSTONE_POLICY.COMMON.ALGO, rounds: SESSION_KEYSTONE_POLICY.COMMON.ROUNDS, behavior: { size: SESSION_KEYSTONE_POLICY.DEFAULT_POOL.SIZE, replenish: SESSION_KEYSTONE_POLICY.COMMON.REPLENISH, selectSequentially: SESSION_KEYSTONE_POLICY.DEFAULT_POOL.SELECT_SEQUENTIALLY, selectRandomly: SESSION_KEYSTONE_POLICY.DEFAULT_POOL.SELECT_RANDOMLY, targetBindingCount: SESSION_KEYSTONE_POLICY.DEFAULT_POOL.TARGET_BINDING_COUNT, }, allowedVerbs: [], }, { id: SESSION_KEYSTONE_POLICY.CONNECT_POOL.ID, salt: await getUUID(), type: SESSION_KEYSTONE_POLICY.COMMON.TYPE, algo: SESSION_KEYSTONE_POLICY.COMMON.ALGO, rounds: SESSION_KEYSTONE_POLICY.COMMON.ROUNDS, behavior: { size: SESSION_KEYSTONE_POLICY.CONNECT_POOL.SIZE, replenish: SESSION_KEYSTONE_POLICY.COMMON.REPLENISH, selectSequentially: SESSION_KEYSTONE_POLICY.CONNECT_POOL.SELECT_SEQUENTIALLY, selectRandomly: SESSION_KEYSTONE_POLICY.CONNECT_POOL.SELECT_RANDOMLY, targetBindingCount: SESSION_KEYSTONE_POLICY.CONNECT_POOL.TARGET_BINDING_COUNT, }, allowedVerbs: [SESSION_KEYSTONE_POLICY.CONNECT_POOL.VERB], } ], metaspace, space, frameDetails: { client: 'space-gib-web-session', target_I: domainI ? getIbGibAddr({ ibGib: domainI }) : undefined, target_X: targetX ? getIbGibAddr({ ibGib: targetX }) : undefined, } }); const sAddr = getIbGibAddr({ ibGib: sIbGib }); debugState.sessionS = sIbGib; debugState.sessionSecret = sessionSecret; devLog(`✓ Created Session Keystone: ${sAddr}`); btn.textContent = '✓ Session Keystone Created'; } catch (error) { devLog(`✗ ${extractErrorMsg(error)}`); console.error(`${lc_fn} ${extractErrorMsg(error)}`); } }); }