import { extractErrorMsg, getUUID, getTimestamp } 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 { KeystoneIbGib_V1 } from '@ibgib/core-gib/dist/keystone/keystone-types.mjs'; import { KeystoneService_V1 } from '@ibgib/core-gib/dist/keystone/keystone-service-v1.mjs'; import { getGlobalMetaspace_waitIfNeeded } from "@ibgib/web-gib/dist/helpers.mjs"; import { mut8Timeline } from '@ibgib/core-gib/dist/timeline/timeline-api.mjs'; import { graphsAreEquivalent } from '@ibgib/core-gib/dist/common/other/graph-helper.mjs'; import { SyncPeerWebSocketSender_V1 } from '@ibgib/core-gib/dist/sync/sync-peer/sync-peer-websocket/sync-peer-websocket-sender/sync-peer-websocket-sender-v1.mjs'; import { SyncSagaCoordinator } from '@ibgib/core-gib/dist/sync/sync-saga-coordinator.mjs'; import { SyncConflictStrategy } from '@ibgib/core-gib/dist/sync/sync-constants.mjs'; import { SESSION_KEYSTONE_POLICY, getSpaceGibPoolConfig } from "../../common/keystone-policies.mjs"; import { SpaceGibApiBridge } from '../api/space-gib-api-bridge.mjs'; import { debugState, devLog, lc, performPhaseSetup, performPhaseSync, getDelegateAndSessionInfo } from './common.mjs'; export function init4_1bSetupButton(): void { const btn = document.getElementById('btn-4-1b-setup') as HTMLButtonElement | null; if (!btn) { return; } btn.addEventListener('click', () => { performPhaseSetup({ phaseText: '4.1B', btnId: 'btn-4-1b-setup', nextBtnId: 'btn-4-1b-sync', masterSecret: 'test-sender-secret-phase4-1', syncSalt: 'senderidentitysyncsaltphase4-1', manageSalt: 'senderidentitymanagesaltphase4-1', ib: 'test data 4.1b', data: { hello: 'world4_1', random: Math.random() } }); }); } export function init4_1bSyncButton(): void { const btn = document.getElementById('btn-4-1b-sync') as HTMLButtonElement | null; if (!btn) { return; } btn.addEventListener('click', () => { performPhaseSync({ phaseText: '4.1B', btnId: 'btn-4-1b-sync', nextBtnId: 'btn-4-1b-check' }); }); } export function init4_1bCheckButton(): void { const lc_fn = `${lc}[init4_1bCheckButton]`; const btn = document.getElementById('btn-4-1b-check') as HTMLButtonElement | null; if (!btn) { return; } btn.addEventListener('click', async () => { try { devLog('4.1B 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!; const targetX = debugState.targetX!; const xAddr = getIbGibAddr({ ibGib: targetX }); const domainAddr = getIbGibAddr({ ibGib: domainI }); // 1. Assert Target X exists locally (with evolved timeline) and get latest local graph const latestLocalXAddr = await metaspace.getLatestAddr({ addr: xAddr, space }); if (!latestLocalXAddr) { devLog('✗ Check: Evolved target X NOT found in local space!'); return; } devLog(`✓ Check: Evolved target X found locally at: ${latestLocalXAddr}`); const localXGraph = await metaspace.getDependencyGraph({ ibGibAddr: latestLocalXAddr, space }); if (!localXGraph || Object.keys(localXGraph).length === 0) { devLog('✗ Check: Failed to load target X dependency graph locally.'); return; } devLog(`✓ Check: Target X local graph loaded (${Object.keys(localXGraph).length} nodes).`); // 2. Fetch server's Target X graph via API bridge const apiBridge = new SpaceGibApiBridge(); const serverGetRes = await apiBridge.getIbGibGraph(domainAddr, xAddr, true); if (!serverGetRes.success || !serverGetRes.graph) { devLog(`✗ Check: Failed to fetch target X graph from server: ${serverGetRes.message}`); return; } const serverXGraph = serverGetRes.graph; devLog(`✓ Check: Target X server graph loaded (${Object.keys(serverXGraph).length} nodes).`); // 3. Verify graphs are equivalent using graphsAreEquivalent const equal = graphsAreEquivalent({ graphA: localXGraph, graphB: serverXGraph }); if (equal) { devLog('✓ Check: Target X dependency graphs are equivalent on client and server.'); } else { devLog('✗ Check: Target X dependency graphs mismatch between client and server!'); return; } // 4. Assert evolved sender identity I1 exists locally and on server const latestLocalIAddr = await metaspace.getLatestAddr({ addr: domainAddr, space }); if (!latestLocalIAddr) { devLog('✗ Check: Evolved domain keystone tip I1 NOT found in local space!'); return; } devLog(`✓ Check: Evolved Domain Keystone (I1) exists locally.`); const getRes = await metaspace.get({ addrs: [latestLocalIAddr], space }); const localI = getRes.ibGibs?.[0] as KeystoneIbGib_V1 | undefined; if (!localI) { devLog('✗ Check: Failed to load I1 from local space.'); return; } const { latestLocalDelegateAddr, sessionIdentityTjpAddr } = await getDelegateAndSessionInfo({ localI, metaspace, space }); if (!latestLocalDelegateAddr) { devLog('✗ Check: Evolved delegate keystone tip NOT found in local space!'); return; } if (!sessionIdentityTjpAddr) { devLog('✗ Check: Delegate sync claim does not target a session keystone.'); return; } devLog(`✓ Check: Delegate Keystone and Sync Claim to Session verified locally.`); const serverDelGetRes = await apiBridge.getIbGib(domainAddr, latestLocalDelegateAddr); if (!serverDelGetRes.success || !serverDelGetRes.ibGib) { devLog(`✗ Check: Evolved Delegate Keystone NOT found on server!`); return; } devLog(`✓ Check: Evolved Delegate Keystone exists on server.`); const serverIGetRes = await apiBridge.getIbGib(domainAddr, latestLocalIAddr); if (!serverIGetRes.success || !serverIGetRes.ibGib) { devLog(`✗ Check: Evolved Domain Keystone (I1) NOT found on server!`); return; } devLog(`✓ Check: Evolved Domain Keystone (I1) exists on server.`); // 5. Assert session identity S tip exists locally and on server const latestLocalSAddr = await metaspace.getLatestAddr({ addr: sessionIdentityTjpAddr, space }); if (!latestLocalSAddr) { devLog('✗ Check: Latest Session Keystone (S) NOT found in local space.'); return; } devLog(`✓ Check: Found latest session keystone (S) locally: ${latestLocalSAddr}`); const localSRes = await metaspace.get({ addrs: [latestLocalSAddr], space }); const sessionS = localSRes.ibGibs?.[0] as KeystoneIbGib_V1 | undefined; if (!sessionS) { devLog('✗ Check: Failed to load latest Session Keystone (S) locally.'); return; } // Verify S evolved to correct n (n === 3 or 4) const n = sessionS.data?.n; devLog(`✓ Check: S tip evolved to n = ${n}.`); if (n !== 3 && n !== 4) { devLog(`⚠ Check: Expected S to evolve to n = 3 or 4, got n = ${n}. Continuing anyway...`); } const serverSGetRes = await apiBridge.getIbGib(domainAddr, latestLocalSAddr); if (!serverSGetRes.success || !serverSGetRes.ibGib) { devLog(`✗ Check: Latest Session Keystone (S) NOT found on server!`); return; } devLog(`✓ Check: Latest Session Keystone (S) exists on server.`); // 6. Assert S graph is identical on client and server const localSGraph = await metaspace.getDependencyGraph({ ibGibAddr: latestLocalSAddr, space }); const serverSGetGraphRes = await apiBridge.getIbGibGraph(domainAddr, sessionIdentityTjpAddr, true); if (!serverSGetGraphRes.success || !serverSGetGraphRes.graph) { devLog(`✗ Check: Failed to fetch session S graph from server: ${serverSGetGraphRes.message}`); return; } const serverSGraph = serverSGetGraphRes.graph; const sGraphsEqual = graphsAreEquivalent({ graphA: localSGraph, graphB: serverSGraph }); if (sGraphsEqual) { devLog('✓ Check: Session Keystone (S) dependency graphs are equivalent on client and server.'); } else { devLog('✗ Check: Session Keystone (S) dependency graphs mismatch between client and server!'); return; } devLog('🎉 ALL PHASE 4.1B TRANSACTION SYNC CHECKS PASSED FLAWLESSLY! ✓'); btn.textContent = '✓ 4.1B All Passed'; } catch (error) { devLog(`✗ 4.1B Check FAILED: ${extractErrorMsg(error)}`); console.error(`${lc_fn} 4.1B Check error:`, error); } }); }