import { respecfully, iReckon, ifWe, ifWeMight } from '@ibgib/helper-gib/dist/respec-gib/respec-gib.mjs'; import { HashAlgorithm } from '@ibgib/helper-gib/dist/helpers/utils-helper.mjs'; import { KeystoneVerb, KEYSTONE_VERB_ACCEPT, KEYSTONE_VERB_REJECT, POOL_ID_ACCEPT, POOL_ID_REJECT } from '../keystone-constants.mjs'; import { ADMIN_CHALLENGE_SCOPE_EXCEPTION_KEYS } from './admin-constants.mjs'; import { deriveAdminChallengeSecret, createAdminChallengeGenesisKeystone, createAdminChallengeGenesisKeystones, solveAdminChallengeKeystone, verifyAdminChallengeResolution, } from './admin-helpers.mjs'; import { createAdminRequestIbGib, isAdminRequest, validateAdminRequest, } from './admin-request/index.mjs'; const sir = `[${import.meta.url}]`; await respecfully(sir, 'Admin Challenge Secret Derivation & Keystone Lifecycle', async () => { await ifWe(sir, 'deriveAdminChallengeSecret produces deterministic outputs', async () => { const masterSecret = 'super-secret-admin-passphrase-999'; const nonce = 'c63b6510-74e8-4672-9b2f-92931dc17fa1'; const scope = ADMIN_CHALLENGE_SCOPE_EXCEPTION_KEYS; const verb = KeystoneVerb.ACCEPT; const secret1 = await deriveAdminChallengeSecret({ masterSecret, nonce, scope, verb, rounds: 10, }); const secret2 = await deriveAdminChallengeSecret({ masterSecret, nonce, scope, verb, rounds: 10, }); iReckon(sir, secret1).willEqual(secret2); iReckon(sir, typeof secret1 === 'string' && secret1.length === 64).willEqual(true); }); await ifWe(sir, 'guarantees verb isolation between accept and reject', async () => { const masterSecret = 'super-secret-admin-passphrase-999'; const nonce = 'c63b6510-74e8-4672-9b2f-92931dc17fa1'; const scope = ADMIN_CHALLENGE_SCOPE_EXCEPTION_KEYS; const acceptSecret = await deriveAdminChallengeSecret({ masterSecret, nonce, scope, verb: KEYSTONE_VERB_ACCEPT, rounds: 10, }); const rejectSecret = await deriveAdminChallengeSecret({ masterSecret, nonce, scope, verb: KEYSTONE_VERB_REJECT, rounds: 10, }); iReckon(sir, acceptSecret !== rejectSecret).willEqual(true); }); await ifWe(sir, 'guarantees nonce sensitivity', async () => { const masterSecret = 'super-secret-admin-passphrase-999'; const scope = ADMIN_CHALLENGE_SCOPE_EXCEPTION_KEYS; const verb = KeystoneVerb.ACCEPT; const secret1 = await deriveAdminChallengeSecret({ masterSecret, nonce: 'nonce-alpha-1111', scope, verb, rounds: 10, }); const secret2 = await deriveAdminChallengeSecret({ masterSecret, nonce: 'nonce-beta-2222', scope, verb, rounds: 10, }); iReckon(sir, secret1 !== secret2).willEqual(true); }); await ifWe(sir, 'guarantees scope isolation', async () => { const masterSecret = 'super-secret-admin-passphrase-999'; const nonce = 'c63b6510-74e8-4672-9b2f-92931dc17fa1'; const verb = KeystoneVerb.ACCEPT; const secret1 = await deriveAdminChallengeSecret({ masterSecret, nonce, scope: 'exception-keys', verb, rounds: 10, }); const secret2 = await deriveAdminChallengeSecret({ masterSecret, nonce, scope: 'billing-disputes', verb, rounds: 10, }); iReckon(sir, secret1 !== secret2).willEqual(true); }); await ifWe(sir, 'guarantees master secret sensitivity', async () => { const nonce = 'c63b6510-74e8-4672-9b2f-92931dc17fa1'; const scope = ADMIN_CHALLENGE_SCOPE_EXCEPTION_KEYS; const verb = KeystoneVerb.ACCEPT; const secret1 = await deriveAdminChallengeSecret({ masterSecret: 'admin-password-A', nonce, scope, verb, rounds: 10, }); const secret2 = await deriveAdminChallengeSecret({ masterSecret: 'admin-password-B', nonce, scope, verb, rounds: 10, }); iReckon(sir, secret1 !== secret2).willEqual(true); }); await ifWe(sir, 'rejects invalid or missing parameters', async () => { const masterSecret = 'some-secret'; const nonce = 'some-nonce'; const scope = 'some-scope'; const verb = KeystoneVerb.ACCEPT; let threwMissingMaster = false; try { await deriveAdminChallengeSecret({ masterSecret: '', nonce, scope, verb, }); } catch { threwMissingMaster = true; } iReckon(sir, threwMissingMaster).willEqual(true); let threwMissingNonce = false; try { await deriveAdminChallengeSecret({ masterSecret, nonce: '', scope, verb, }); } catch { threwMissingNonce = true; } iReckon(sir, threwMissingNonce).willEqual(true); let threwMissingScope = false; try { await deriveAdminChallengeSecret({ masterSecret, nonce, scope: '', verb, }); } catch { threwMissingScope = true; } iReckon(sir, threwMissingScope).willEqual(true); let threwMissingVerb = false; try { await deriveAdminChallengeSecret({ masterSecret, nonce, scope, verb: '', }); } catch { threwMissingVerb = true; } iReckon(sir, threwMissingVerb).willEqual(true); let threwInvalidRounds = false; try { await deriveAdminChallengeSecret({ masterSecret, nonce, scope, verb, rounds: 0, }); } catch { threwInvalidRounds = true; } iReckon(sir, threwInvalidRounds).willEqual(true); }); await ifWe(sir, 'supports sha512 algorithm', async () => { const masterSecret = 'super-secret-admin-passphrase-999'; const nonce = 'c63b6510-74e8-4672-9b2f-92931dc17fa1'; const scope = ADMIN_CHALLENGE_SCOPE_EXCEPTION_KEYS; const verb = KeystoneVerb.ACCEPT; const secretSha512 = await deriveAdminChallengeSecret({ masterSecret, nonce, scope, verb, rounds: 5, algorithm: HashAlgorithm.sha_512, }); const secretSha256 = await deriveAdminChallengeSecret({ masterSecret, nonce, scope, verb, rounds: 5, algorithm: HashAlgorithm.sha_256, }); iReckon(sir, secretSha512.length).willEqual(128); iReckon(sir, secretSha512 !== secretSha256).willEqual(true); }); await ifWe(sir, 'creates, solves, and verifies One-Off Challenge Genesis Keystone', async () => { const masterSecret = 'admin-secure-master-pw-12345'; const nonce = 'unique-challenge-nonce-uuid-777'; const scope = ADMIN_CHALLENGE_SCOPE_EXCEPTION_KEYS; // 1. Create Genesis Keystone (Frame n=0) const genesisKeystone = await createAdminChallengeGenesisKeystone({ masterSecret, nonce, scope, targetIdentifier: 'cool-handle', applicantDetails: { applicantName: 'Bob Developer', reason: 'Building a cool p2p browser app', }, rounds: 10, }); iReckon(sir, genesisKeystone.data?.n).willEqual(0); iReckon(sir, genesisKeystone.data?.isTjp).willEqual(true); iReckon(sir, typeof genesisKeystone.gib === 'string' && genesisKeystone.gib.length > 0).willEqual(true); iReckon(sir, genesisKeystone.data?.challengePools?.length).willEqual(2); // 2. Solve 'accept' pool const solvedKeystone = await solveAdminChallengeKeystone({ genesisKeystone, masterSecret, poolId: POOL_ID_ACCEPT, resolutionNotes: 'Approved developer program handle exception', }); iReckon(sir, solvedKeystone.data?.n).willEqual(1); iReckon(sir, solvedKeystone.data?.proofs?.length).willEqual(1); iReckon(sir, solvedKeystone.data?.proofs?.[0]?.solutions?.[0]?.poolId).willEqual(POOL_ID_ACCEPT); // 3. Cryptographically verify the resolution const verificationErrors = await verifyAdminChallengeResolution({ genesisKeystone, solvedKeystone, expectedPoolId: POOL_ID_ACCEPT, }); iReckon(sir, verificationErrors.length).willEqual(0); // 4. Verify that wrong pool expectation detects mismatch const mismatchErrors = await verifyAdminChallengeResolution({ genesisKeystone, solvedKeystone, expectedPoolId: POOL_ID_REJECT, }); iReckon(sir, mismatchErrors.length > 0).willEqual(true); // 5. Verify that wrong secret throws during solve let threwWrongSecret = false; try { await solveAdminChallengeKeystone({ genesisKeystone, masterSecret: 'wrong-admin-password', poolId: POOL_ID_ACCEPT, }); } catch { threwWrongSecret = true; } iReckon(sir, threwWrongSecret).willEqual(true); }); await ifWe(sir, 'creates and validates an immutable AdminRequest stone ibgib', async () => { const adminRequest = await createAdminRequestIbGib({ scope: ADMIN_CHALLENGE_SCOPE_EXCEPTION_KEYS, targetIdentifier: 'alice', applicantDetails: { siteUsername: 'alice', email: 'alice@ibgib.space', reason: 'Need short handle for dev', }, }); iReckon(sir, !!adminRequest.gib).willEqual(true); iReckon(sir, isAdminRequest(adminRequest)).willEqual(true); iReckon(sir, adminRequest.data?.scope).willEqual(ADMIN_CHALLENGE_SCOPE_EXCEPTION_KEYS); iReckon(sir, adminRequest.data?.targetIdentifier).willEqual('alice'); iReckon(sir, adminRequest.ib.startsWith('adminrequest ')).willEqual(true); const errors = await validateAdminRequest({ adminRequestIbGib: adminRequest }); iReckon(sir, errors.length).willEqual(0); }); await ifWe(sir, 'creates per-admin keystones targeting an AdminRequest stone and solves', async () => { const adminRequest = await createAdminRequestIbGib({ scope: ADMIN_CHALLENGE_SCOPE_EXCEPTION_KEYS, targetIdentifier: 'bob', applicantDetails: { siteUsername: 'bob', email: 'bob@ibgib.space', }, }); const adminRequestAddr = `${adminRequest.ib}^${adminRequest.gib}`; const admins = [ { id: 'admin-alpha', password: 'alpha-secret-12345' }, { id: 'admin-beta', password: 'beta-secret-67890' }, ]; const keystones = await createAdminChallengeGenesisKeystones({ admins, adminRequestIbGib: adminRequest, nonce: 'test-multi-admin-nonce-001', rounds: 2, }); iReckon(sir, !!keystones['admin-alpha']).willEqual(true); iReckon(sir, !!keystones['admin-beta']).willEqual(true); iReckon(sir, keystones['admin-alpha'].tjpGib !== keystones['admin-beta'].tjpGib).willEqual(true); // Alpha solves their keystone targeting the admin request stone const solvedAlpha = await solveAdminChallengeKeystone({ genesisKeystone: keystones['admin-alpha'].genesisKeystone, masterSecret: 'alpha-secret-12345', poolId: POOL_ID_ACCEPT, adminRequestIbGib: adminRequest, resolutionNotes: 'Alpha approved', }); iReckon(sir, solvedAlpha.data?.proofs?.[0]?.solutions?.[0]?.poolId).willEqual(POOL_ID_ACCEPT); iReckon(sir, solvedAlpha.data?.proofs?.[0]?.claim?.target).willEqual(adminRequestAddr); // Verify cryptographic resolution const errors = await verifyAdminChallengeResolution({ genesisKeystone: keystones['admin-alpha'].genesisKeystone, solvedKeystone: solvedAlpha, expectedPoolId: POOL_ID_ACCEPT, expectedTargetAddr: adminRequestAddr, }); iReckon(sir, errors.length).willEqual(0); }); });