import { extractErrorMsg } from '@ibgib/helper-gib/dist/helpers/utils-helper.mjs'; import { getIbGibAddr } from '@ibgib/ts-gib/dist/helper.mjs'; import { KeystoneIbGib_V1 } from '@ibgib/core-gib/dist/keystone/keystone-types.mjs'; import { POOL_ID_CONNECT, POOL_ID_SYNC } from '@ibgib/core-gib/dist/keystone/keystone-constants.mjs'; import { getGlobalMetaspace_waitIfNeeded } from "@ibgib/web-gib/dist/helpers.mjs"; import { debugState, devLog, lc, performPhaseSetup, performPhaseSync } from './common.mjs'; export function init2bSetupButton(): void { const btn = document.getElementById('btn-2b-setup') as HTMLButtonElement | null; if (!btn) { return; } btn.addEventListener('click', () => { performPhaseSetup({ phaseText: '2B', btnId: 'btn-2b-setup', nextBtnId: 'btn-2b-sync', masterSecret: 'test-sender-secret-phase2', syncSalt: 'senderidentitysyncsaltphase2', manageSalt: 'senderidentitymanagesaltphase2', ib: 'test data', data: { hello: 'world2', random: Math.random() } }); }); } export function init2bSyncButton(): void { const btn = document.getElementById('btn-2b-sync') as HTMLButtonElement | null; if (!btn) { return; } btn.addEventListener('click', () => { performPhaseSync({ phaseText: '2B', btnId: 'btn-2b-sync', nextBtnId: 'btn-2b-check', expectSyncFailure: true, failureMessageSuffix: 'connect phase successfully run; subsequent sync errors expected in 2B' }); }); } export function init2bCheckButton(): void { const lc_fn = `${lc}[init2bCheckButton]`; const btn = document.getElementById('btn-2b-check') as HTMLButtonElement | null; if (!btn) { return; } btn.addEventListener('click', async () => { try { devLog('2B Check: Asserting cryptographic and WebSocket state expectations...'); const metaspace = await getGlobalMetaspace_waitIfNeeded(); const space = await metaspace.getLocalUserSpace({}); if (!space) { throw new Error("No default space."); } const domainI = debugState.domainI!; // 1. Resolve evolved sender identity I1 locally const domainAddr = getIbGibAddr({ ibGib: domainI }); const newSenderIdentityAddr = await metaspace.getLatestAddr({ addr: domainAddr, space }); if (!newSenderIdentityAddr) { devLog('✗ Check: Evolved domain keystone tip I1 NOT found in local space!'); return; } devLog(`✓ Check: Evolved Domain Keystone (I1) created and stored locally.`); const getRes = await metaspace.get({ addrs: [newSenderIdentityAddr], space }); const newSenderIdentity = getRes.ibGibs?.[0] as KeystoneIbGib_V1 | undefined; if (!newSenderIdentity) { devLog('✗ Check: Failed to load I1 from local space.'); return; } const syncProof = newSenderIdentity.data?.proofs?.find(p => p.claim?.verb === 'sync'); const sessionIdentityTjpAddr = syncProof?.claim?.target; if (!sessionIdentityTjpAddr) { devLog('✗ Check: I1 sync claim does not target a session keystone.'); return; } devLog(`✓ Check: Sync claim targets session keystone: ${sessionIdentityTjpAddr}`); // 2. Load Session Keystone (S) locally and get latest tip const latestSessionSAddr = await metaspace.getLatestAddr({ addr: sessionIdentityTjpAddr, space }); if (!latestSessionSAddr) { devLog('✗ Check: Latest Session Keystone (S) NOT found in local space.'); return; } devLog(`✓ Check: Found latest session keystone (S) locally.`); const sRes = await metaspace.get({ addrs: [latestSessionSAddr], space }); const sessionIdentity = sRes.ibGibs?.[0] as KeystoneIbGib_V1 | undefined; if (!sessionIdentity) { devLog('✗ Check: Failed to load latest Session Keystone (S).'); return; } // Verify S contains connect and sync pools const sPools = sessionIdentity.data?.challengePools ?? []; const connectPool = sPools.find(p => p.id === POOL_ID_CONNECT); const syncPool = sPools.find(p => p.id === POOL_ID_SYNC); if (connectPool && syncPool) { devLog('✓ Check: S contains both connect and sync challenge pools.'); } else { devLog(`✗ Check: S is missing challenge pools! (connect: ${!!connectPool}, sync: ${!!syncPool})`); return; } // Verify S was successfully evolved (depleted connect pool) const remainingChallenges = Object.keys(connectPool.challenges).length; if (remainingChallenges === 0) { devLog('✓ Check: S connect pool successfully depleted (0 challenges remaining).'); } else { devLog(`✗ Check: S connect pool not depleted! (${remainingChallenges} challenges remaining)`); return; } // 3. Verify WebSocket Connection is open and active if (debugState.senderPeer && debugState.senderPeer.isSocketOpen) { devLog('✓ Check: WebSocket connection is active and OPEN.'); } else { devLog('✗ Check: WebSocket connection is NOT open or peer is missing!'); return; } devLog('🎉 ALL PHASE 2B CONNECT CHECKS PASSED FLAWLESSLY! ✓'); btn.textContent = '✓ 2B All Passed'; const p3SetupBtn = document.getElementById('btn-3b-setup') as HTMLButtonElement | null; if (p3SetupBtn) { p3SetupBtn.disabled = false; } } catch (error) { devLog(`✗ 2B Check FAILED: ${extractErrorMsg(error)}`); console.error(`${lc_fn} 2B Check error:`, error); } }); }