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 { SpaceGibApiBridge } from '../api/space-gib-api-bridge.mjs'; import { debugState, devLog, lc, performPhaseSetup, performPhaseSync } from './common.mjs'; export function init1bSetupButton(): void { const btn = document.getElementById('btn-1b-setup') as HTMLButtonElement | null; if (!btn) { return; } btn.addEventListener('click', () => { performPhaseSetup({ phaseText: '1B', btnId: 'btn-1b-setup', nextBtnId: 'btn-1b-sync', masterSecret: 'test-sender-secret-phase1', syncSalt: 'senderidentitysyncsaltphase1', manageSalt: 'senderidentitymanagesaltphase1', ib: 'test data', data: { hello: 'world', random: Math.random() } }); }); } export function init1bSyncButton(): void { const btn = document.getElementById('btn-1b-sync') as HTMLButtonElement | null; if (!btn) { return; } btn.addEventListener('click', () => { performPhaseSync({ phaseText: '1B', btnId: 'btn-1b-sync', nextBtnId: 'btn-1b-check', expectSyncFailure: true, failureMessageSuffix: 'establish phase successfully run; connection/sync errors expected in 1B' }); }); } export function init1bCheckButton(): void { const lc_fn = `${lc}[init1bCheckButton]`; const btn = document.getElementById('btn-1b-check') as HTMLButtonElement | null; if (!btn) { return; } btn.addEventListener('click', async () => { try { devLog('1B Check: Asserting cryptographic and durable state expectations from respec...'); const metaspace = await getGlobalMetaspace_waitIfNeeded(); const space = await metaspace.getLocalUserSpace({}); if (!space) { throw new Error("No default space."); } const domainI = debugState.domainI!; const targetX = debugState.targetX!; // 1. Resolve evolved sender identity I1 locally const newSenderIdentityAddr = await metaspace.getLatestAddr({ addr: getIbGibAddr({ ibGib: domainI }), 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'); if (!syncProof) { devLog('✗ Check: I1 is missing sync-verb claim proof!'); } else { devLog('✓ Check: I1 contains valid sync-verb claim proof.'); } 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 const sRes = await metaspace.get({ addrs: [sessionIdentityTjpAddr], space }); const sessionIdentity = sRes.ibGibs?.[0] as KeystoneIbGib_V1 | undefined; if (!sessionIdentity) { devLog('✗ Check: Session Keystone (S) NOT found in local space.'); return; } devLog('✓ Check: Session Keystone (S) successfully created locally.'); const sPools = sessionIdentity.data?.challengePools ?? []; const hasConnectPool = sPools.some(p => p.id === POOL_ID_CONNECT); const hasSyncPool = sPools.some(p => p.id === POOL_ID_SYNC); if (hasConnectPool && hasSyncPool) { devLog('✓ Check: S contains both connect and sync challenge pools.'); } else { devLog(`✗ Check: S is missing challenge pools! (connect: ${hasConnectPool}, sync: ${hasSyncPool})`); } // 3. Verify Server-Side (receiver) registrations via GET REST API devLog('Check: Verifying registrations on server (receiver metaspace)...'); const apiBridge = new SpaceGibApiBridge(); let hasI = false; let hasI1 = false; let hasS = false; const domainAddr = getIbGibAddr({ ibGib: domainI }); const resGetDomainIGraphFromServer = await apiBridge.getKeystoneGraph(domainAddr); if (resGetDomainIGraphFromServer.success && resGetDomainIGraphFromServer.graph) { const domainGraph = resGetDomainIGraphFromServer.graph; hasI = domainGraph[domainAddr] !== undefined; hasI1 = domainGraph[newSenderIdentityAddr] !== undefined; } else { devLog(`✗ Check Server: Failed to fetch keystone graph from server: ${resGetDomainIGraphFromServer.message}`); } const resGetSessionSFromServer = await apiBridge.getIbGib(domainAddr, sessionIdentityTjpAddr); if (resGetSessionSFromServer.success && resGetSessionSFromServer.ibGib) { hasS = true; } else { devLog(`✗ Check Server: Failed to fetch session keystone from server: ${resGetSessionSFromServer.message}`); } if (hasI) { devLog('✓ Check Server: Genesis Domain Keystone (I) registered on server.'); } else devLog('✗ Check Server: Genesis Domain Keystone (I) missing on server!'); if (hasI1) { devLog('✓ Check Server: Evolved Domain Keystone (I1) accepted and stored on server.'); } else devLog('✗ Check Server: Evolved Domain Keystone (I1) missing on server!'); if (hasS) { devLog('✓ Check Server: Session Keystone (S) accepted and stored on server.'); } else devLog('✗ Check Server: Session Keystone (S) missing on server!'); if (hasI && hasI1 && hasS) { devLog('🎉 ALL PHASE 1B ESTABLISH CHECKS PASSED FLAWLESSLY! ✓'); btn.textContent = '✓ 1B All Passed'; const btn2bSetup = document.getElementById('btn-2b-setup') as HTMLButtonElement | null; if (btn2bSetup) { btn2bSetup.disabled = false; } } } catch (error) { devLog(`✗ 1B Check FAILED: ${extractErrorMsg(error)}`); console.error(`${lc_fn} 1B Check error:`, error); } }); }