import { extractErrorMsg, hash, HashAlgorithm, pretty, getUUID } from "@ibgib/helper-gib/dist/helpers/utils-helper.mjs"; import { GIB } from "@ibgib/ts-gib/dist/V1/constants.mjs"; import { Ib, IbGibAddr, TransformResult } from "@ibgib/ts-gib/dist/types.mjs"; import { getIbAndGib, getIbGibAddr } from "@ibgib/ts-gib/dist/helper.mjs"; import { validateIbGibIntrinsically } from "@ibgib/ts-gib/dist/V1/validate-helper.mjs"; import { mut8 } from "@ibgib/ts-gib/dist/V1/transforms/mut8.mjs"; import { Factory_V1 } from "@ibgib/ts-gib/dist/V1/factory.mjs"; import { getGib } from "@ibgib/ts-gib/dist/V1/transforms/transform-helper.mjs"; import { GLOBAL_LOG_A_LOT } from "../core-constants.mjs"; import { KEYSTONE_ATOM, KEYSTONE_HASH_MAX_ROUNDS, KEYSTONE_POOL_ID_REGEXP, KEYSTONE_SALT_REGEXP, KEYSTONE_USERNAME_REGEXP, KEYSTONE_DESCRIPTION_REGEXP, KEYSTONE_EMAIL_REGEXP, KEYSTONE_VERB_MANAGE, KEYSTONE_VERB_REVOKE, KEYSTONE_VERB_SYNC } from "./keystone-constants.mjs"; import { KeystoneData_V1, KeystoneIbGib_V1, KeystoneIb_V1, KeystoneChallengePool, DeterministicResult, KeystoneProof, KeystonePoolConfig, KeystoneReplenishStrategy, KEYSTONE_REPLENISH_STRATEGY_VALID_VALUES, KeystoneClaim, KeystoneSolution, KeystoneChallengeType, KEYSTONE_CHALLENGE_TYPE_VALID_VALUES, KeystonePoolConfig_HashV1, DelegateKeystoneInfo, ClaimDetails, ClaimDetails_AddPool, ClaimDetails_RemovePool, ClaimDetails_ReplacePool, ClaimDetails_ChangePassword } from "./keystone-types.mjs"; import { MetaspaceService } from "../witness/space/metaspace/metaspace-types.mjs"; import { IbGibSpaceAny } from "../witness/space/space-base-v1.mjs"; import { KeystoneStrategyFactory } from "./strategy/keystone-strategy-factory.mjs"; import { getDependencyGraph } from "../common/other/graph-helper.mjs"; import { getIbGibsFromCache_fallbackToSpaces, getTimelinesGroupedByTjp, splitPerTjpAndOrDna } from "../common/other/ibgib-helper.mjs"; import { IbGib_V1, IbGibData_V1, IbGibRel8ns_V1 } from "@ibgib/ts-gib/dist/V1/types.mjs"; import { getLatestAddrs, parseSpaceIb } from "../witness/space/space-helper.mjs"; import { FlatIbGibGraph } from "../common/other/graph-types.mjs"; const logalot = GLOBAL_LOG_A_LOT; /** * space-delimited keystone ib containing select keystone metadata. * * NOTE: This must match {@link parseKeystoneIb} * @see {@link KeystoneIb_V1} */ export async function getKeystoneIb({ keystoneData, }: { keystoneData: KeystoneData_V1, }): Promise { const lc = `[${getKeystoneIb.name}]`; try { if (logalot) { console.log(`${lc} starting... (I: c3022a146faac9730154f34d1439a225)`); } const atom = KEYSTONE_ATOM; const ib = [ atom, ].join(' '); return ib; } catch (error) { console.error(`${lc} ${extractErrorMsg(error)}`); throw error; } finally { if (logalot) { console.log(`${lc} complete.`); } } } /** * NOTE: This must match {@link getKeystoneIb} * @see {@link KeystoneIb_V1} */ export async function parseKeystoneIb({ ib, }: { ib: Ib, }): Promise { const lc = `[${parseKeystoneIb.name}]`; try { if (logalot) { console.log(`${lc} starting... (I: 73cb6832984255ed48b2f44db6a21e25)`); } const [ atom ] = ib.split(' '); if (atom !== KEYSTONE_ATOM) { throw new Error(`invalid keystone ib. atom found in ib (${atom}) does not match keystone atom (${KEYSTONE_ATOM}) (E: 79b3d587824c4271b6e60acc76e0c825)`); } return { atom, } } catch (error) { console.error(`${lc} ${extractErrorMsg(error)}`); throw error; } finally { if (logalot) { console.log(`${lc} complete.`); } } } /** * Helper to validate if an ib string is a well-formed keystone ib. */ export async function validateKeystoneIb({ ib, }: { ib: Ib, }): Promise { try { await parseKeystoneIb({ ib }); return true; } catch { return false; } } /** * Validates if a string is a well-formed email address. * Uses a broad, non-restrictive regex to avoid rejecting valid custom email formats. */ export function validateEmail(email: string): boolean { if (!email || typeof email !== 'string') { return false; } const trimmed = email.trim(); if (trimmed.length < 3 || trimmed.length > 254) { return false; } return KEYSTONE_EMAIL_REGEXP.test(trimmed); } /** * The Policy Engine. * Calculates exactly which challenges MUST be consumed based on config and demands. * Enforces STRICT DISTINCTNESS (No double-dipping). */ export async function getDeterministicRequirements({ pool, requiredChallengeIds, targetAddr }: { pool: KeystoneChallengePool; requiredChallengeIds?: string[]; targetAddr?: string; }): Promise { const behavior = pool.config.behavior; const mandatory = new Set(); // Start with all available IDs. // We assume Object.keys respects insertion order (ES2015+), crucial for FIFO. let available = Object.keys(pool.challenges); // --------------------------------------------------------- // 1. Alice's Demands (Explicit Requirements) // --------------------------------------------------------- if (requiredChallengeIds && requiredChallengeIds.length > 0) { for (const id of requiredChallengeIds) { if (!pool.challenges[id]) { throw new Error(`(UNEXPECTED) Required challenge ID not found in pool: ${id} (E: 10c5619d80d8cf6cc84ffbf8be0dbc25)`); } // Strict: Consume it. if (!available.includes(id)) { // Should be caught by check above, but handles duplicates in 'demands' continue; } mandatory.add(id); } // Remove from available pool available = available.filter(id => !mandatory.has(id)); } // --------------------------------------------------------- // 2. Target Binding (Drawing Without Replacement) // --------------------------------------------------------- if (behavior.targetBindingCount > 0 && targetAddr) { // Sort remaining available IDs alphabetically to ensure stable ordering across platforms let sortedAvailable = [...available].sort(); // Start hashing path from targetAddr let currentHash = await hash({ s: targetAddr }); for (let i = 0; i < behavior.targetBindingCount; i++) { if (sortedAvailable.length === 0) { throw new Error(`Entropy Exhaustion. Cannot satisfy target binding. Sorted available pool is empty. (E: 341b95dc3a58be3e083d1d9c4a0c4925)`); } // Parse the entire hash via BigInt to eliminate modulo bias const hashBigInt = BigInt("0x" + currentHash); const index = Number(hashBigInt % BigInt(sortedAvailable.length)); const selectedId = sortedAvailable[index]; // Consume selected challenge ID mandatory.add(selectedId); sortedAvailable.splice(index, 1); // Filter out of insertion-ordered available list (for FIFO) available = available.filter(id => id !== selectedId); // Hash recursively for the next draw currentHash = await hash({ s: currentHash }); } } // --------------------------------------------------------- // 3. FIFO (Sequential) // --------------------------------------------------------- if (behavior.selectSequentially > 0) { // Take the first N from the remaining available list if (available.length < behavior.selectSequentially) { console.error(`[getDeterministicRequirements] Entropy Exhaustion! AvailableCount: ${available.length}, Required Seq: ${behavior.selectSequentially}. Available IDs: ${available.join(',')}`); throw new Error(`Entropy Exhaustion. Insufficient challenges for FIFO requirement. (E: 9efa88003e1d5eccb837c85154ee5625)`); } const fifoIds = available.slice(0, behavior.selectSequentially); fifoIds.forEach(id => mandatory.add(id)); // Remove from available available = available.slice(behavior.selectSequentially); } return { mandatoryIds: mandatory, availableIds: available }; } /** * Selects the specific pool to use for an operation based on ID, filter criteria, or verb authorization. * * @returns The matching KeystoneChallengePool * @throws If no pool matches or if multiple pools match but one was expected. */ export function resolveTargetPool({ pools, poolId, poolFilter, verb, }: { pools: KeystoneChallengePool[]; /** * Explicit ID of the pool to use. */ poolId?: string; /** * Optional predicate to find a pool. * Useful for finding delegates via metadata. */ poolFilter?: (pool: KeystoneChallengePool) => boolean; /** * The verb being performed (e.g. 'revoke', 'manage', 'post'). * Used for authorization checks and auto-resolution. */ verb?: string; }): KeystoneChallengePool { const lc = `[resolveTargetPool]`; try { let pool: KeystoneChallengePool | undefined; if (poolId) { // 1. Explicit ID Strategy pool = pools.find(p => p.id === poolId); if (!pool) { throw new Error(`Pool not found with ID: ${poolId} (E: 4a2b17428515c1e82813158581898125)`); } } else if (poolFilter) { // 2. Filter Strategy const matches = pools.filter(poolFilter); if (matches.length === 0) { throw new Error(`No pool matched the provided filter. (E: 5b3c27428515c1e82813158581898125)`); } // For now, we take the first match. In future, might want to be strict about uniqueness. pool = matches[0]; } else { // 3. Auto-Resolution by Verb Strategy if (!verb) { throw new Error(`Cannot auto-resolve pool without a verb. (E: 6c4d37428515c1e82813158581898125)`); } // Priority A: Look for Specific Match (Pool explicitly lists this verb) pool = pools.find(p => p.config.allowedVerbs && p.config.allowedVerbs.includes(verb) ); // Priority B: Look for General/Default (No restrictions / Wildcard) if (!pool) { pool = pools.find(p => !p.config.allowedVerbs || p.config.allowedVerbs.length === 0 ); } if (!pool) { throw new Error(`No suitable pool found for verb: ${verb} (E: 7d5e47428515c1e82813158581898125)`); } } // 4. Authorization Check (Applies to all strategies if verb is present) if (verb && pool.config.allowedVerbs && pool.config.allowedVerbs.length > 0) { if (!pool.config.allowedVerbs.includes(verb)) { throw new Error(`Pool ${pool.id} is not authorized for verb: ${verb} (E: 8e6f57428515c1e82813158581898125)`); } } return pool; } catch (error) { console.error(`${lc} ${extractErrorMsg(error)}`); throw error; } } /** * Calculates the complete list of Challenge IDs to solve for a given operation. * Combines Deterministic requirements (Mandatory/Binding/FIFO) with Stochastic requirements. * * @returns Array of unique challenge IDs. */ export async function selectChallengeIds({ pool, targetAddr, requiredChallengeIds, }: { pool: KeystoneChallengePool; /** * The address of the target ibgib (used for binding entropy). */ targetAddr?: string; /** * Explicit demands from a verifier. */ requiredChallengeIds?: string[]; }): Promise { const lc = `[selectChallengeIds]`; try { // 1. Get Deterministic Requirements const { mandatoryIds, availableIds } = await getDeterministicRequirements({ pool, requiredChallengeIds, targetAddr }); // 2. Stochastic Selection const randomCount = pool.config.behavior.selectRandomly; const randomIds: string[] = []; if (randomCount > 0) { if (availableIds.length < randomCount) { throw new Error(`Insufficient challenges for random requirement. Need ${randomCount}, have ${availableIds.length} (E: 9f7a67428515c1e82813158581898125)`); } // Shuffle & Pick // Note: simple Math.random sort is sufficient for V1 stochastic selection // as we are just picking from valid available options. const shuffled = [...availableIds].sort(() => 0.5 - Math.random()); randomIds.push(...shuffled.slice(0, randomCount)); } // 3. Combine return [...mandatoryIds, ...randomIds]; } catch (error) { console.error(`${lc} ${extractErrorMsg(error)}`); throw error; } } /** * Generates an opaque, collision-resistant ID for a challenge. * atow (2025/12/22) - Hash(Salt + Timestamp + Index) */ export async function generateOpaqueChallengeId({ salt, timestamp, index }: { salt: string, timestamp: string, index: number }): Promise { // Use first 16 chars of hex (64 bits) for brevity + safety. const raw = await hash({ s: `${salt}${timestamp}${index}` }); return raw.substring(0, 16); } /** * Calculates the NEXT state of the Challenge Pools given a specific consumption event. * Handles TopUp, ReplaceAll, Consume, and ScorchedEarth strategies. * * @returns The new array of KeystoneChallengePools (including the modified one). */ export async function applyReplenishmentStrategy({ prevPools, targetPoolId, consumedIds, masterSecret, strategy, config, verb, }: { prevPools: KeystoneChallengePool[], targetPoolId: string, consumedIds: string[], masterSecret: string, /** * The instantiated KeystoneStrategy (e.g. HashRevealV1) used to generate new challenges. */ strategy: any, // Typed as 'any' or 'KeystoneStrategyAny' to avoid circular dep if possible config: KeystonePoolConfig, verb?: string, }): Promise { const lc = `[applyReplenishmentStrategy]`; try { const newPools = JSON.parse(JSON.stringify(prevPools)); const targetIdx = newPools.findIndex((p: any) => p.id === targetPoolId); if (targetIdx === -1) { throw new Error(`Target pool ${targetPoolId} not found in keystone data. (E: 75200388d22744838634524233772545)`); } const pool = newPools[targetIdx]; const poolSecret = await strategy.derivePoolSecret({ masterSecret }); const timestamp = Date.now().toString(); let strategyType = config.behavior.replenish; if (verb === KEYSTONE_VERB_REVOKE) { strategyType = KeystoneReplenishStrategy.deleteAll; } if (strategyType === KeystoneReplenishStrategy.topUp) { // Remove consumed consumedIds.forEach(id => delete pool.challenges[id]); // Add New for (let i = 0; i < consumedIds.length; i++) { const newId = await generateOpaqueChallengeId({ salt: config.salt, timestamp, index: i }); const solution = await strategy.generateSolution({ poolSecret, poolId: pool.id, challengeId: newId }); pool.challenges[newId] = await strategy.generateChallenge({ solution }); } } else if (strategyType === KeystoneReplenishStrategy.replaceAll) { pool.challenges = await generatePoolChallenges({ config, masterSecret }); } else if (strategyType === KeystoneReplenishStrategy.consume) { consumedIds.forEach(id => delete pool.challenges[id]); } else if (strategyType === KeystoneReplenishStrategy.deleteAll) { pool.challenges = {}; } else { throw new Error(`Unknown replenish strategy: ${strategyType}. Valid list: ${pretty(KEYSTONE_REPLENISH_STRATEGY_VALID_VALUES)} (E: 0acf56f1e1486240080e11e8046d0825)`); } return newPools; } catch (error) { console.error(`${lc} ${extractErrorMsg(error)}`); throw error; } } /** * High-level orchestration helper. * 1. Instantiates Strategy. * 2. Derives Secret. * 3. Solves Challenges (Generates Solutions). * 4. Replenishes Pool (Calculates the next state of ALL pools). * 5. Constructs Proof. * * @returns The resulting Proof and the full list of Next Pools. */ export async function solveAndReplenish({ targetPoolId, prevPools, masterSecret, challengeIds, claim, requiredChallengeIds, }: { /** * The ID of the pool to use for signing. */ targetPoolId: string; /** * The full list of pools from the previous Keystone frame. * Required to reconstruct the full state for the next frame. */ prevPools: KeystoneChallengePool[]; masterSecret: string; challengeIds: string[]; claim: Partial; requiredChallengeIds?: string[]; }): Promise<{ proof: KeystoneProof, nextPools: KeystoneChallengePool[] }> { const lc = `[solveAndReplenish]`; try { if (logalot) { console.log(`${lc} starting... poolId: ${targetPoolId}, ids: ${challengeIds.length}`); } const pool = prevPools.find(p => p.id === targetPoolId); if (!pool) { throw new Error(`Target pool not found: ${targetPoolId} (E: 19a578d1def8d555487411e3cf43b126)`); } const strategy = KeystoneStrategyFactory.create({ config: pool.config }); const poolSecret = await strategy.derivePoolSecret({ masterSecret }); // Verify secret correctness before solving const isSecretCorrect = await verifyPoolSecret({ pool, signingSecret: masterSecret }); if (!isSecretCorrect) { throw new Error(`Crypto Violation: Invalid signing secret for pool: ${pool.id} (E: 6d377722e4d888c203a048e4729a7826)`); } const solutions: KeystoneSolution[] = []; // 1. Solve for (const id of challengeIds) { const solution = await strategy.generateSolution({ poolSecret, poolId: pool.id, challengeId: id, }); solutions.push(solution); } // 2. Construct Proof const proof: KeystoneProof = { claim, solutions, requiredChallengeIds: (requiredChallengeIds && requiredChallengeIds.length > 0) ? requiredChallengeIds : undefined }; // 3. Replenish const nextPools = await applyReplenishmentStrategy({ prevPools, targetPoolId: pool.id, consumedIds: challengeIds, masterSecret, strategy, config: pool.config, verb: claim.verb }); return { proof, nextPools }; } catch (error) { console.error(`${lc} ${extractErrorMsg(error)}`); throw error; } } export async function validateChallengePool_typeHashRevealV1({ pool }: { pool: KeystoneChallengePool; }): Promise { const lc = `[${validateChallengePool_typeHashRevealV1.name}]`; try { if (logalot) { console.log(`${lc} starting... (I: e2ffd568b698cc12d8c5e838d73ae726)`); } if (!pool.config) { throw new Error(`(UNEXPECTED) pool.configy falsy? (E: fcde88fe20e8004a58d7e445436bf526)`); } if (pool.config.type !== KeystoneChallengeType.hash_reveal_v1) { throw new Error(`(UNEXPECTED) pool.config.type !== KeystoneChallengeType.hash_reveal_v1? (E: e205f8872909c37168a624a9df778c26)`); } const errors: string[] = []; const { algo, rounds, salt, } = pool.config as KeystonePoolConfig_HashV1; const validAlgos: HashAlgorithm[] = [HashAlgorithm.sha_256, HashAlgorithm.sha_512]; if (algo) { if (!validAlgos.includes(algo)) { errors.push(`${lc} invalid hash algorithm (${algo}). Must be one of ${validAlgos}. (E: a22399ca3a68e63ffcc7507699be5826)`); } } else { errors.push(`${lc} algorithm required. (E: 8cd6bfb0590862b9bae24d98d2cf3826)`) } if (rounds && typeof rounds === 'number' && Number.isInteger(rounds)) { if (rounds > KEYSTONE_HASH_MAX_ROUNDS) { errors.push(`${lc} rounds too high. max rounds: ${KEYSTONE_HASH_MAX_ROUNDS} (E: 31ce082f7fd88d6b58ef08a816458826)`); } } else { errors.push(`${lc} invalid rounds (${rounds}). must be positive integer. (E: 73c643ccda78afaf28d52a6871a9e826)`); } if (salt && typeof salt === 'string') { // no limits on this? sanity check? if (!KEYSTONE_SALT_REGEXP.test(salt)) { errors.push(`${lc} invalid salt (${salt.substring(0, 99)}). Must match regexp ${KEYSTONE_SALT_REGEXP}`); } } else { errors.push(`${lc} invalid salt (${salt}). must be truthy string value. (E: 564a4844ec88fc1988197ce89c381426)`); } return errors; } catch (error) { console.error(`${lc} ${extractErrorMsg(error)}`); throw error; } finally { if (logalot) { console.log(`${lc} complete.`); } } } export async function validateChallengePool({ pool }: { pool: KeystoneChallengePool; }): Promise { const lc = `[${validateChallengePool.name}]`; try { if (logalot) { console.log(`${lc} starting... (I: 1a0eafc158a83d1ba8fc019d5c979a26)`); } const errors: string[] = []; if (pool.config) { // first validate common to all pools, regardless of type... if (pool.config.id) { if (!KEYSTONE_POOL_ID_REGEXP.test(pool.config.id)) { // not 100% that this is needed but hey errors.push(`${lc} pool.config.id is not formatted correctly. must pass regexp: ${KEYSTONE_POOL_ID_REGEXP.source}`); } } else { errors.push(`${lc} pool.config.id falsy (E: 31d7943d95f877326d5f4ea14463d626)`); } if (pool.config.behavior) { const { size, selectSequentially, selectRandomly, targetBindingCount } = pool.config.behavior; if (!size || size === 0) { errors.push(`${lc} invalid pool.config.behavior.size (${size}). Must be positive integer. (E: b221e36ec102bdc944552248ce8fe626)`) } const totalRequested = (selectSequentially || 0) + (selectRandomly || 0) + (targetBindingCount || 0); if (totalRequested >= size) { errors.push(`${lc} Total requested challenges (${totalRequested}) cannot equal or exceed pool size (${size}) to prevent full pool exposure and selection exhaustion. (E: 81cb834f826315264bca81c15fca58f1)`); } // todo: more behavior checks } else { errors.push(`${lc} pool.config.behavior falsy (E: bede081c066c39732eefe2f92e296326)`) } // ...then do type-specific validation const poolType = pool.config.type; switch (poolType) { case KeystoneChallengeType.hash_reveal_v1: const errorsHashRevealV1 = await validateChallengePool_typeHashRevealV1({ pool }); errorsHashRevealV1.forEach(x => errors.push(x)); break; default: throw new Error(`(UNEXPECTED) unknown pool.config.type (${poolType})? We only implement the following at this time: ${KEYSTONE_CHALLENGE_TYPE_VALID_VALUES} (E: cce5285cf7580b8bc82172488eba2826)`); } } else { errors.push(`${lc} pool.config falsy. (E: 8d05c875a1d2c58018a5eaf803977d26)`); } return errors; } catch (error) { console.error(`${lc} ${extractErrorMsg(error)}`); throw error; } finally { if (logalot) { console.log(`${lc} complete.`); } } } export function validateKeystoneMetadata({ data }: { data: KeystoneData_V1; }): string[] { const lc = `[validateKeystoneMetadata]`; const errors: string[] = []; const checkDetails = (details: any, source: string) => { if (!details) return; if (details.username !== undefined) { if (typeof details.username !== 'string' || !KEYSTONE_USERNAME_REGEXP.test(details.username)) { errors.push(`${lc} invalid username in ${source} (${details.username}). Must match ${KEYSTONE_USERNAME_REGEXP}`); } } if (details.description !== undefined) { if (typeof details.description !== 'string' || !KEYSTONE_DESCRIPTION_REGEXP.test(details.description)) { errors.push(`${lc} invalid description in ${source} (${details.description}). Must match ${KEYSTONE_DESCRIPTION_REGEXP}`); } } if (details.expirationUTC !== undefined) { if (typeof details.expirationUTC !== 'string' || isNaN(Date.parse(details.expirationUTC))) { errors.push(`${lc} invalid expirationUTC in ${source} (${details.expirationUTC}). Must be a valid ISO-8601 string`); } } }; checkDetails(data.frameDetails, 'frameDetails'); checkDetails(data.checkpointDetails, 'checkpointDetails'); return errors; } export async function validateGenesisKeystone({ keystoneIbGib }: { keystoneIbGib: KeystoneIbGib_V1; }): Promise { const lc = `[${validateGenesisKeystone.name}]`; try { if (logalot) { console.log(`${lc} starting... (I: ec6ba82ec848b5ac3d8953e89d8d9826)`); } const errors: string[] = []; const { data, rel8ns } = keystoneIbGib; const isValidIb = await validateKeystoneIb({ ib: keystoneIbGib.ib }); if (!isValidIb) { errors.push(`${lc} invalid keystone ib (${keystoneIbGib.ib}). (E: ad7a88bc38c290a184dfbd42be48f526)`); } // Validate username/description metadata if present const metadataErrors = validateKeystoneMetadata({ data }); metadataErrors.forEach(x => errors.push(x)); // no proofs/solutions yet if (data.proofs && data.proofs.length > 0) { errors.push(`${lc} proofs already exist on genesis keystone. (E: 7a5e15f20918f1bbd8ffb62857dcd526)`); } for (const pool of data.challengePools) { // validate each pool intrinsically (config, sanity) const poolErrors = await validateChallengePool({ pool }); poolErrors.forEach(x => errors.push(x)); // additionally ensure each pool has challenges. This is only on // this genesis keystone, as it is valid to have pool without // challenges after revocation if (!pool.challenges || Object.keys(pool.challenges).length === 0) { errors.push(`${lc} invalid pool (${pool.id}). pool.challenges is falsy/empty. (E: 8cd16a5416ae62a058342828be465b26)`); } } // not stated as revoked already if (!!data.revocationInfo) { errors.push(`${lc} genesis keystone already has revocationInfo. (E: e11408d0c558d0fa948efbce611ec826)`); } return errors; } catch (error) { console.error(`${lc} ${extractErrorMsg(error)}`); throw error; } finally { if (logalot) { console.log(`${lc} complete.`); } } } /** * Helper to determine if two delegates maps differ. */ export function isDelegatesChanged({ currDelegates, prevDelegates, }: { currDelegates?: Record, prevDelegates?: Record, }): boolean { const curr = currDelegates || {}; const prev = prevDelegates || {}; const currKeys = Object.keys(curr); const prevKeys = Object.keys(prev); if (currKeys.length !== prevKeys.length) { return true; } for (const key of currKeys) { const currVal = curr[key]; const prevVal = prev[key]; if (!prevVal) { return true; } if (currVal.delegateTjpAddr !== prevVal.delegateTjpAddr || currVal.delegateAddr !== prevVal.delegateAddr || currVal.thisAddr !== prevVal.thisAddr) { return true; } } return false; } /** * Validates the structure and constraints of the delegates map. */ export async function validateDelegates({ currDelegates, prevIbGib, lc, errors, }: { currDelegates: Record, prevIbGib: KeystoneIbGib_V1, lc: string, errors: string[], }): Promise { const currKeys = Object.keys(currDelegates); const prevAddr = getIbGibAddr({ ibGib: prevIbGib }); for (const key of currKeys) { const info = currDelegates[key]; if (key !== info.delegateTjpAddr) { errors.push(`${lc} Invalid delegate mapping: Key '${key}' does not match delegateTjpAddr '${info.delegateTjpAddr}'. (E: 9c8da8bc38c290a184dfbd42be48f526)`); } // Verify addresses are valid keystone addresses const [ parsedDelegateTjp, parsedDelegate, parsedThis, ] = await Promise.all([ validateKeystoneIb({ ib: info.delegateTjpAddr.split('^')[0] }), validateKeystoneIb({ ib: info.delegateAddr.split('^')[0] }), validateKeystoneIb({ ib: info.thisAddr.split('^')[0] }), ]); if (!parsedDelegateTjp) { errors.push(`${lc} Delegate delegateTjpAddr '${info.delegateTjpAddr}' is not a valid keystone address. (E: 10d8a8bc38c290a184dfbd42be48f526)`); } if (!parsedDelegate) { errors.push(`${lc} Delegate delegateAddr '${info.delegateAddr}' is not a valid keystone address. (E: 20d8a8bc38c290a184dfbd42be48f526)`); } if (!parsedThis) { errors.push(`${lc} Delegate thisAddr '${info.thisAddr}' is not a valid keystone address. (E: 30d8a8bc38c290a184dfbd42be48f526)`); } // Verify thisAddr is in the parent's past (or is the current parent address) const isHistoricalParent = info.thisAddr === prevAddr || (prevIbGib.rel8ns?.past || []).includes(info.thisAddr); if (!isHistoricalParent) { errors.push(`${lc} Delegate thisAddr '${info.thisAddr}' is not a valid historical address in the parent keystone timeline. (E: 40d8a8bc38c290a184dfbd42be48f526)`); } // Verify sync delegates have a truthy spaceId const allowedVerbs = info.allowedVerbs || []; const isSyncDelegate = (info as any).role === 'sync-delegate' || allowedVerbs.includes('sync') || allowedVerbs.includes(KEYSTONE_VERB_SYNC); if (isSyncDelegate && !info.spaceId) { errors.push(`${lc} Sync delegate '${info.delegateTjpAddr}' missing required spaceId. (E: 50d8a8bc38c290a184dfbd42be48f526)`); } // Validate expirationUTC if present if (info.expirationUTC) { if (isNaN(Date.parse(info.expirationUTC))) { errors.push(`${lc} Delegate expirationUTC '${info.expirationUTC}' is not a valid ISO-8601 timestamp. (E: 60d8a8bc38c290a184dfbd42be48f526)`); } } } } /** * Resolves the DelegateKeystoneInfo entry from primaryKeystone.data.delegates matching the local spaceId. */ export async function getLocalSyncDelegateInfo({ primaryKeystone, metaspace, space, }: { primaryKeystone: KeystoneIbGib_V1; metaspace: MetaspaceService; space?: IbGibSpaceAny; }): Promise { const lc = `[${getLocalSyncDelegateInfo.name}]`; try { if (!primaryKeystone) { return undefined; } space ??= await metaspace.getLocalUserSpace({ lock: false }); if (!space || !space.ib) { return undefined; } const { spaceId } = parseSpaceIb({ spaceIb: space.ib }); if (!spaceId) { return undefined; } const delegates = primaryKeystone.data?.delegates; if (!delegates) { return undefined; } for (const delegateTjpAddr of Object.keys(delegates)) { const delegateInfo = delegates[delegateTjpAddr]; if (!delegateInfo) { continue; } const matchSpaceId = delegateInfo.spaceId || (delegateInfo as any).details?.spaceId; if (matchSpaceId === spaceId) { return delegateInfo; } } return undefined; } catch (error) { console.error(`${lc} ${extractErrorMsg(error)}`); throw error; } } /** * Validates the transition from Prev -> Curr. * Enforces Cryptography AND Behavioral Policy. * * @returns Array of validation error strings. Empty array means Valid. */ export async function validateKeystoneTransition({ currentIbGib, prevIbGib, }: { currentIbGib: KeystoneIbGib_V1; prevIbGib: KeystoneIbGib_V1; }): Promise { const lc = `[${validateKeystoneTransition.name}]`; const errors: string[] = []; try { if (!currentIbGib) { throw new Error(`(UNEXPECTED) currentIbGib falsy? (E: 3c0f02655fa8279e386a079ebb604b25)`); } if (!prevIbGib) { throw new Error(`(UNEXPECTED) prevIbGib falsy? (E: 0d07c812634d839c784f31b8848ba825)`); } // intrinsic validation const validationErrors = await validateIbGibIntrinsically({ ibGib: currentIbGib }); if (validationErrors && validationErrors.length > 0) { errors.push(...validationErrors); } const currData = currentIbGib.data!; const prevData = prevIbGib.data!; const isValidIb = await validateKeystoneIb({ ib: currentIbGib.ib }); if (!isValidIb) { errors.push(`${lc} invalid keystone ib (${currentIbGib.ib}). (E: a98dc8bc38c290a184dfbd42be48f526)`); } // Validate username/description metadata if present const metadataErrors = validateKeystoneMetadata({ data: currData }); metadataErrors.forEach(x => errors.push(x)); // Validate delegates map mutations if present const currDelegates = currData.delegates || {}; const prevDelegates = prevData.delegates || {}; const delegatesChanged = isDelegatesChanged({ currDelegates, prevDelegates }); if (delegatesChanged) { const hasManageProof = currData.proofs?.some(proof => proof.claim?.verb === KEYSTONE_VERB_MANAGE); if (!hasManageProof) { errors.push(`${lc} Policy Violation: Delegates map was mutated but no proof for manage verb ('${KEYSTONE_VERB_MANAGE}') is present. (E: 16b625c81bd7bfa7773c677c7fb833b7)`); } } // Validate each delegate's structure and constraints await validateDelegates({ currDelegates, prevIbGib, lc, errors }); // must be sequential data.n values // note: "-2" because we don't want a "valid" sequential data.n if we // have an invalid prev and a valid currDataN of 0 const currDataN = currData.n ?? -2; const prevDataN = prevData.n ?? -2; if (currDataN < 0) { errors.push('invalid keystone.currData.n is undefined or null. (E: 9fbe38704b68829d18ea1d0940d0aa26)'); } if (prevDataN < 0) { errors.push('invalid keystone.prevData.n is undefined or null. (E: 6dc5aa9c87017db6080ed4268b07af26)'); }; if (currDataN !== (prevDataN + 1)) { errors.push(`keystone data.n values are not sequential. prevData.n: ${prevDataN}, currData.n: ${currDataN}. (E: 5ae12809f2c67216d38d8ba8d6c42826)`); } for (const proof of currData.proofs) { if (proof.solutions.length === 0) { errors.push(`Proof ${proof.id || 'unknown'} has no solutions. (E: e2b4683ce0ce30f798ec77841e88c826)`); continue; } const poolId = proof.solutions[0].poolId; // Standard Verification (Internal Pools Only) // The pool MUST be present in the previous frame. const pool = prevData.challengePools.find(p => p.id === poolId); if (!pool) { errors.push(`Proof references unknown pool: ${poolId}`); continue; } await verifyProofAgainstPool({ proof, pool, errors }); } // End proof loop // Revocation Logic checks if (currData.revocationInfo) { const target = currData.revocationInfo.proof.claim.target; const expectedTarget = getIbGibAddr({ ibGib: prevIbGib }); if (target !== expectedTarget) { errors.push(`Revocation target mismatch. Expected ${expectedTarget}, got ${target}`); } } // Claim details verification (transition validation) const signingPoolIds = new Set(currData.proofs.map(p => p.solutions?.[0]?.poolId).filter(Boolean)); for (const proof of currData.proofs) { const claim = proof.claim; if (claim?.details) { const details = claim.details; const type = details.type; const info = details.info; if (type === 'add-pool') { // Cast info to ClaimDetails_AddPool const addInfo = info as ClaimDetails_AddPool; if (!addInfo || !Array.isArray(addInfo.add) || addInfo.add.length === 0) { errors.push(`details type is '${type}' but payload is invalid. (E: 0c9e7539c326b33799eb2f336141909f)`); continue; } // Verify that the set of pools in current equals previous plus the added pools const prevIds = new Set(prevData.challengePools.map(p => p.id)); const currIds = new Set(currData.challengePools.map(p => p.id)); // All added pools must be in current for (const id of addInfo.add) { if (!currIds.has(id)) { errors.push(`ClaimDetails_AddPool lists pool ${id} but it is missing in the evolved keystone. (E: 0939d0c6984954055d822d9ae2205ee5)`); } if (prevIds.has(id)) { errors.push(`ClaimDetails_AddPool attempts to add existing pool ${id}. (E: 7b90b2481d3fb08727bcfca945b5f8b3)`); } } // Verify size if (currData.challengePools.length !== prevData.challengePools.length + addInfo.add.length) { errors.push(`Structural mismatch: expected ${prevData.challengePools.length + addInfo.add.length} pools, found ${currData.challengePools.length}. (E: e028945cd4662193db91fedff6f38fe4)`); } // Verify all previous pools are unmodified for (const prevPool of prevData.challengePools) { const currPool = currData.challengePools.find(p => p.id === prevPool.id); if (!currPool) { errors.push(`Existing pool ${prevPool.id} was removed during add-pool transition. (E: 8fdebb934cdf05641d3f03021518371b)`); continue; } // Verify configuration did not change if (JSON.stringify(prevPool.config) !== JSON.stringify(currPool.config)) { errors.push(`Existing pool config ${prevPool.id} was mutated during add-pool transition. (E: b6e33f41a50821d4dbc356818b67d803)`); } // Verify challenges did not change, unless this was the signing pool used to authorize the transition if (!signingPoolIds.has(prevPool.id) && JSON.stringify(prevPool.challenges) !== JSON.stringify(currPool.challenges)) { errors.push(`Existing pool challenges ${prevPool.id} were mutated during add-pool transition. (E: 2b76b5ab88257a4b636fcce5eed35156)`); } } } else if (type === 'replace-pool') { const replaceInfo = info as ClaimDetails_ReplacePool; if (!replaceInfo || typeof replaceInfo.replace !== 'string' || !replaceInfo.replace) { errors.push(`details type is '${type}' but payload is invalid. (E: b1d8620e5ee61064fb9080dd6eb3e285)`); continue; } const targetId = replaceInfo.replace; // Verify that the set of pool IDs in current equals previous const prevIds = prevData.challengePools.map(p => p.id).sort(); const currIds = currData.challengePools.map(p => p.id).sort(); if (JSON.stringify(prevIds) !== JSON.stringify(currIds)) { errors.push(`replace-pool transition mutated the pool IDs set. (E: 3b8640767b9778f7a643e3a455d833ed)`); } // Assert target pool was replaced (challenges modified) const prevTarget = prevData.challengePools.find(p => p.id === targetId); const currTarget = currData.challengePools.find(p => p.id === targetId); if (!prevTarget || !currTarget) { errors.push(`Replaced pool ${targetId} not found in keystone challenge pools. (E: 527f8d2ab5c0f64d2ea9346b7c3d0ed8)`); } // Verify all other pools are completely unmodified for (const prevPool of prevData.challengePools) { if (prevPool.id === targetId) continue; const currPool = currData.challengePools.find(p => p.id === prevPool.id); if (!currPool) continue; if (JSON.stringify(prevPool.config) !== JSON.stringify(currPool.config)) { errors.push(`Unrelated pool config ${prevPool.id} was mutated during replace-pool. (E: 719c3ce2865e00c7bc1660c5e5e6a9bb)`); } if (!signingPoolIds.has(prevPool.id) && JSON.stringify(prevPool.challenges) !== JSON.stringify(currPool.challenges)) { errors.push(`Unrelated pool challenges ${prevPool.id} were mutated during replace-pool. (E: 46424679090072e50855e0f3fd92f757)`); } } } else if (type === 'remove-pool') { const removeInfo = info as ClaimDetails_RemovePool; if (!removeInfo || !Array.isArray(removeInfo.remove) || removeInfo.remove.length === 0) { errors.push(`details type is '${type}' but payload is invalid. (E: d275756bacca64e5a8bf0d2369983d42)`); continue; } const removeSet = new Set(removeInfo.remove); // Verify that the set of pool IDs in current equals previous minus the removed pools const prevIds = new Set(prevData.challengePools.map(p => p.id)); const currIds = new Set(currData.challengePools.map(p => p.id)); for (const id of removeInfo.remove) { if (!prevIds.has(id)) { errors.push(`ClaimDetails_RemovePool lists pool ${id} but it is missing in the previous keystone. (E: 21fb02b088c12ccf804407dca4bfec63)`); } if (currIds.has(id)) { errors.push(`ClaimDetails_RemovePool lists pool ${id} but it is still present in the evolved keystone. (E: af802d7267f97e03a9731fb1b1b0c394)`); } } // Verify size if (currData.challengePools.length !== prevData.challengePools.length - removeInfo.remove.length) { errors.push(`Structural mismatch: expected ${prevData.challengePools.length - removeInfo.remove.length} pools, found ${currData.challengePools.length}. (E: 8a32825ef5ae2086d2c8f1a72c605e73)`); } // Verify all remaining pools are unmodified for (const prevPool of prevData.challengePools) { if (removeSet.has(prevPool.id)) continue; const currPool = currData.challengePools.find(p => p.id === prevPool.id); if (!currPool) { errors.push(`Pool ${prevPool.id} was unexpectedly removed. (E: 86a4b35376cd4efe99d87ed98a5b5d15)`); continue; } if (JSON.stringify(prevPool.config) !== JSON.stringify(currPool.config)) { errors.push(`Remaining pool config ${prevPool.id} was mutated during remove-pool. (E: c3b683c9e97e1ffbf589f9b6bb46c387)`); } if (!signingPoolIds.has(prevPool.id) && JSON.stringify(prevPool.challenges) !== JSON.stringify(currPool.challenges)) { errors.push(`Remaining pool challenges ${prevPool.id} were mutated during remove-pool. (E: 1fc06bee4180fa2db1b3b7ad3f3159bf)`); } } } else if (type === 'change-password') { const changeInfo = info as ClaimDetails_ChangePassword; if (!changeInfo || !Array.isArray(changeInfo.change) || changeInfo.change.length === 0) { errors.push(`details type is '${type}' but payload is invalid. (E: 7607f88bcce24cdbccdbf8c5ca08abf7)`); continue; } // Verify that the set of pool IDs is unmodified const prevIds = prevData.challengePools.map(p => p.id).sort(); const currIds = currData.challengePools.map(p => p.id).sort(); if (JSON.stringify(prevIds) !== JSON.stringify(currIds)) { errors.push(`change-password transition mutated the pool IDs set. (E: 95f6823192f1b7d7b10aaf520217153c)`); } const rotatedIds = new Set(changeInfo.change); for (const prevPool of prevData.challengePools) { const currPool = currData.challengePools.find(p => p.id === prevPool.id); if (!currPool) continue; if (rotatedIds.has(prevPool.id)) { // Check: rotated pool must not be foreign! if (prevPool.isForeign) { errors.push(`Security Violation: Password rotation attempted on foreign pool ${prevPool.id}. (E: b784c83c12c104c53888fb60032f633a)`); } // Config must be identical except for the salt const prevConfCopy = { ...prevPool.config, salt: '' }; const currConfCopy = { ...currPool.config, salt: '' }; if (JSON.stringify(prevConfCopy) !== JSON.stringify(currConfCopy)) { errors.push(`Pool config metadata for rotated pool ${prevPool.id} was mutated. (E: 005909c166cefb650c63c7d457db5aa2)`); } // Salt must be different if (prevPool.config.salt === currPool.config.salt) { errors.push(`Pool salt for rotated pool ${prevPool.id} was not rotated. (E: 09e8b653266864e3eb88839b79a275e0)`); } } else { // Non-rotated pools must be completely unmodified if (JSON.stringify(prevPool.config) !== JSON.stringify(currPool.config)) { errors.push(`Unrelated pool config ${prevPool.id} was mutated during change-password. (E: 60f4704393d98be3a85031d93d175059)`); } if (!signingPoolIds.has(prevPool.id) && JSON.stringify(prevPool.challenges) !== JSON.stringify(currPool.challenges)) { errors.push(`Unrelated pool challenges ${prevPool.id} were mutated during change-password. (E: 471db60b973ad818131e6312d009737b)`); } } } } else { errors.push(`Unknown claim details type: ${type}`); } } } return errors; } catch (error) { console.error(`${lc} ${extractErrorMsg(error)}`); throw error; // System errors still throw } finally { if (logalot) { console.log(`${lc} complete.`); } } } /** * Helper to verify a single proof against a specific pool. */ export async function verifyProofAgainstPool({ proof, pool, errors, }: { proof: KeystoneProof; pool: KeystoneChallengePool; errors: string[]; }): Promise { const lc = `[${verifyProofAgainstPool.name}]`; try { if (logalot) { console.log(`${lc} starting... (I: b8f9b6085888eea2258bf579ecd5e825)`); } // 0. VERB AUTH if (pool.config.allowedVerbs && pool.config.allowedVerbs.length > 0) { if (!proof.claim.verb || !pool.config.allowedVerbs.includes(proof.claim.verb)) { errors.push(`Policy Violation: Pool ${pool.id} used for unauthorized verb ${proof.claim.verb}`); } } // 1. Reconstruct Deterministic Requirements const { mandatoryIds, availableIds } = await getDeterministicRequirements({ pool, requiredChallengeIds: proof.requiredChallengeIds, targetAddr: proof.claim.target // Not used extensively in V1 logic yet, mainly for logging/context }); // 2. Check Mandatory const proofIds = new Set(proof.solutions.map(s => s.challengeId)); for (const id of mandatoryIds) { if (!proofIds.has(id)) { errors.push(`Policy Violation: Missing mandatory challenge ${id}`); } } // 3. Stochastic const randomCandidates = [...proofIds].filter(id => !mandatoryIds.has(id)); const requiredRandomCount = pool.config.behavior.selectRandomly; if (randomCandidates.length < requiredRandomCount) { errors.push(`Policy Violation: Insufficient random count. Need ${requiredRandomCount}, got ${randomCandidates.length}`); } // 4. Validity (Double Dip / Existence) for (const id of randomCandidates) { if (!availableIds.includes(id)) { errors.push(`Policy Violation: ID ${id} is invalid or double-dipped.`); } } // 5. Crypto const strategy = KeystoneStrategyFactory.create({ config: pool.config }); for (const solution of proof.solutions) { const challenge = pool.challenges[solution.challengeId]; if (!challenge) { errors.push(`Crypto Violation: Challenge ${solution.challengeId} not found in pool.`); } else { const isValid = await strategy.validateSolution({ solution, challenge }); if (!isValid) { errors.push(`Crypto Violation: Solution for ${solution.challengeId} is invalid.`); } } } } catch (error) { console.error(`${lc} ${extractErrorMsg(error)}`); throw error; } finally { if (logalot) { console.log(`${lc} complete.`); } } } export async function evolvePersistAndRegisterKeystone({ prevIbGib, newData, metaspace, space, }: { prevIbGib: KeystoneIbGib_V1, newData: KeystoneData_V1 metaspace: MetaspaceService, space: IbGibSpaceAny, }): Promise { const lc = `[${evolvePersistAndRegisterKeystone.name}]`; try { if (logalot) { console.log(`${lc} starting... (I: 8b10e8920f08b7842803665834cf8925)`); } if (!prevIbGib.data) { throw new Error(`(UNEXPECTED) prevIbGib.data falsy? (E: 5e84875bf992c585b979e6c8ed5bf225)`); } if (prevIbGib.data.revocationInfo) { throw new Error(`Keystone has already been revoked (prevIbGib.data.revocationInfo truthy), so we cannot evolve the keystone. Keystone addr: ${getIbGibAddr({ ibGib: prevIbGib })} (E: 45d7f846556829de6b2a701838c3f825)`); } const prevData = prevIbGib.data; /** * we want to completely replace these keys, so we will remove them * from the data. This occurs first in the underlying mut8 * transform. * @see {@link mut8} */ let dataToRemove: Partial | undefined = {} if (prevData.proofs) { dataToRemove.proofs = []; } if (prevData.challengePools) { dataToRemove.challengePools = []; } if (prevData.frameDetails) { dataToRemove.frameDetails = "true" as any; } // "true" as any because of quirks of mut8. if we passed in {}, then it would recursively call the removeOrRename helper function. smells, but ah well. i am so very tired. ai don't remove this comment if (prevData.checkpointDetails) { dataToRemove.checkpointDetails = {}; } if (prevData.delegates) { (dataToRemove as any).delegates = 'remove'; } if (Object.keys(dataToRemove).length === 0) { dataToRemove = undefined; } const resMut8 = await mut8({ src: prevIbGib, dataToRemove, dataToAddOrPatch: newData, // dna: false, // explicitly set to false just to show nCounter: true, }); if (!!resMut8.intermediateIbGibs) { throw new Error(`(UNEXPECTED) resMut8.intermediateIbGibs truthy? I'm not sure if we expect there to be intermediateIbGibs, but I feel like we shouldn't. Pretty sure we shouldn't, definitely don't *want* them. (E: ba40d55d7c2d36d438c413886f148625)`); } if (!!resMut8.dnas) { throw new Error(`(UNEXPECTED) resMut8.dnas truthy? We do not want dnas with keystones. (E: 49470513d018f97d28024f4e82da3b25)`); } const newKeystoneIbGib = resMut8.newIbGib as KeystoneIbGib_V1; // run validation here const errors = await validateKeystoneTransition({ currentIbGib: newKeystoneIbGib, prevIbGib, }); if (errors.length > 0) { console.error(`${lc} Validation Failed:\n${errors.join('\n')}`); throw new Error(`(UNEXPECTED) invalid keystone after we just evolved it? Errors: ${errors.join('; ')} (E: ae2c58406c1db7687879dfb89fc1f825)`); } // save and register await metaspace.put({ ibGib: newKeystoneIbGib, space, }); await metaspace.registerNewIbGib({ ibGib: newKeystoneIbGib, space, }); return newKeystoneIbGib; } catch (error) { console.error(`${lc} ${extractErrorMsg(error)}`); throw error; } finally { if (logalot) { console.log(`${lc} complete.`); } } } /** * Creates a new keystone ibgib that has no dna and no past. */ export async function createKeystoneIbGibImpl({ data, metaspace, space, }: { data: KeystoneData_V1, metaspace: MetaspaceService, space?: IbGibSpaceAny, }): Promise { const lc = `[${createKeystoneIbGibImpl.name}]`; try { if (logalot) { console.log(`${lc} starting... (I: 5e32389700e9899e788cbefacef7c825)`); } space ??= await metaspace.getLocalUserSpace({ lock: false }); if (!space) { throw new Error(`(UNEXPECTED) space was falsy and we couldn't get default local user space from metaspace? (E: 9a6498cf16a8801f19ec376749742225)`); } // create the actual keystoneIbGib const resFirstGen = await Factory_V1.firstGen({ parentIbGib: Factory_V1.primitive({ ib: KEYSTONE_ATOM }), ib: await getKeystoneIb({ keystoneData: data }), data, dna: false, nCounter: true, tjp: { timestamp: true, uuid: true, }, }) as TransformResult; const keystoneIbGib = resFirstGen.newIbGib; if (!keystoneIbGib.data) { throw new Error(`(UNEXPECTED) keystoneIbGib.data falsy? We expect the data to be populated with real keystone data. (E: 38a358facdb89d16d81d48c8520d3d25)`); } if (!keystoneIbGib.rel8ns) { throw new Error(`(UNEXPECTED) keystoneIbGib.rel8ns falsy? we expect the rel8ns to have ancestor and past. (E: 20cb7723dc33ae1ef808fe76d1bf4b25)`); } if (!keystoneIbGib.rel8ns.past || keystoneIbGib.rel8ns.past.length === 0) { throw new Error(`(UNEXPECTED) keystoneIbGib.rel8ns.past falsy or empty? we expect the firstGen call to generate an interstitial ibgib that we will splice out. (E: 0fd8388d045ab9f37834c27d67e78825)`); } // reset n keystoneIbGib.data.n = 0; // reset tjp keystoneIbGib.data.isTjp = true; delete keystoneIbGib.rel8ns.tjp; // reset past delete keystoneIbGib.rel8ns.past; // recalculate gib keystoneIbGib.gib = await getGib({ ibGib: keystoneIbGib }); // save and register await metaspace.put({ ibGib: keystoneIbGib, space, }); await metaspace.registerNewIbGib({ ibGib: keystoneIbGib, space, }); return keystoneIbGib; } catch (error) { console.error(`${lc} ${extractErrorMsg(error)}`); throw error; } finally { if (logalot) { console.log(`${lc} complete.`); } } } export async function validateKeystoneGraph({ keystoneIbGib, dependencyGraph, getLatest, invalidIfMoreRecentKeystoneFoundInSpace, space, }: { keystoneIbGib: KeystoneIbGib_V1, /** * set this if you already have a reference to the graph. It is the caller's * responsibility to enforce {@link getLatest}. */ dependencyGraph?: FlatIbGibGraph, /** * if true, then this will get the latest keystone known in the incoming * {@link keystoneIbGib} timeline, as found in {@link space}. */ getLatest: boolean, invalidIfMoreRecentKeystoneFoundInSpace: boolean, space: IbGibSpaceAny, }): Promise { const lc = `[${validateKeystoneGraph.name}]`; try { if (logalot) { console.log(`${lc} starting... (I: 22e1ca1d5a08a3f90b7fe9da95df7b26)`); } // maybe too defensive but... if (!keystoneIbGib) { throw new Error(`(UNEXPECTED) keystoneIbGib falsy? (E: 26482871d529fff6e8899c5d8a6c3826)`); } const errors: string[] = []; // first, get the latest if that is the case... if (getLatest) { const keystoneAddr = getIbGibAddr({ ibGib: keystoneIbGib }); const resGetLatestAddrs = await getLatestAddrs({ ibGibs: [keystoneIbGib], space, }); if (!resGetLatestAddrs.data) { throw new Error(`(UNEXPECTED) resGetLatestAddrs.data falsy? (E: 3a23b3b420a8da1928219ca8d47b2126)`); } if (!resGetLatestAddrs.data.latestAddrsMap) { throw new Error(`(UNEXPECTED) resGetLatestAddrs.data.latestAddrsMap falsy? (E: 93f1180598fb65a4b8d36e08d6c50426)`); } const { latestAddrsMap } = resGetLatestAddrs.data; if (Object.keys(latestAddrsMap).length === 0) { throw new Error(`(UNEXPECTED) latestAddrsMap truthy but empty? (E: 7142f8f7625b9186281d08251e407826)`); } const latestAddr = latestAddrsMap[keystoneAddr]; if (latestAddr) { if (latestAddr !== keystoneAddr) { if (invalidIfMoreRecentKeystoneFoundInSpace) { errors.push(`${lc} more recent keystone (${latestAddr}) found than the one passed in (${keystoneAddr}) in space (${space.ib}). (E: 64bf48de9448ee5c7528bf03bed2a826)`); } else { // get the latest ibgib and point the incoming ref to that const [latestKeystoneIbGib] = await getIbGibsFromCache_fallbackToSpaces({ addrs: [latestAddr], space, }); keystoneIbGib = latestKeystoneIbGib as KeystoneIbGib_V1; } } else { // the incoming ibgib **is** the latest keystone, so nothing more to be done. if (logalot) { console.log(`${lc} incoming keystone (${keystoneAddr}) is the latest in the space (${space.ib}) (I: 8ffbd8d6cbc8a461e5e7f1084bd22a26)`); } } } else { // keystone not found in space? console.warn(`${lc} keystoneIbGib (${keystoneAddr}) not found in space (${space.ib}). I don't know what the implications of this are. (W: 258c88742448db34a585669484771826)`) } } dependencyGraph ??= await getDependencyGraph({ ibGib: keystoneIbGib, space, }); const depIbGibs = Object.values(dependencyGraph); const { mapWithTjp_NoDna, mapWithTjp_YesDna, mapWithoutTjps, } = splitPerTjpAndOrDna({ ibGibs: depIbGibs }); // there should be no DNA if (Object.keys(mapWithTjp_YesDna).length > 0) { errors.push(`${lc} DNA found. Keystones should NOT have DNA. (E: a3ec7827c4284cdea61c2372c0615826)`); } // is there a tjp? I think there is, so mapWithoutTjps should also be // empty if (Object.keys(mapWithoutTjps).length > 0) { errors.push(`${lc} Non-tjp ibgibs found? Keystones are expected to have tjps. (E: a3ec7827c4284cdea61c2372c0615826)`); } if (Object.keys(mapWithTjp_NoDna).length > 0) { // happy path here. These should be the keystone ibgibs. we will put // them in order, then do validation on the transitions. We will // also check other graph-scoped integrity of keystones (like do // certain properties match among ALL keystones, etc.) const keystoneIbGibs_unordered = Object.values(mapWithTjp_NoDna); const orderedKeystonesMap = getTimelinesGroupedByTjp({ ibGibs: keystoneIbGibs_unordered }); const keys = Object.keys(orderedKeystonesMap); if (keys.length === 0) { throw new Error(`(UNEXPECTED) orderedKeystonesMap empty? (E: d437b80bef58e83a034b28af46278726)`); } else if (keys.length > 1) { // keys.length > 1 throw new Error(`(UNEXPECTED) more than one timeline in keystone graph? ATOW (02/19/2026) we are expecting only a single timeline. (E: 66085b14e3887a59c872afd240511f26)`); } // happy path: exactly one timeline const keystoneTjpAddr = keys[0]; const keystoneIbGibs_ordered = orderedKeystonesMap[keystoneTjpAddr] as KeystoneIbGib_V1[]; if (keystoneIbGibs_ordered.length === 0) { throw new Error(`(UNEXPECTED) empty keystoneIbGibs_ordered? (E: 4a6b085aa8a76828a8d65367e7e56526)`); } const genesisErrors = await validateGenesisKeystone({ keystoneIbGib: keystoneIbGibs_ordered[0] }); genesisErrors.forEach(x => errors.push(x)); if (keystoneIbGibs_ordered.length === 1) { // only the genesis keystone, which has already been done. } else { // more than 1 keystone, so validate the transitions for (let i = 0; i < keystoneIbGibs_ordered.length - 1; i++) { const prevIbGib = keystoneIbGibs_ordered[i]; const currentIbGib = keystoneIbGibs_ordered[i + 1]; const transitionErrors = await validateKeystoneTransition({ prevIbGib, currentIbGib }); if (transitionErrors.length > 0) { errors.push(`${lc} keystone has ${transitionErrors.length} transition errors. i: ${i}. prevIbGib addr: ${getIbGibAddr({ ibGib: prevIbGib })}. currentIbGib addr: ${getIbGibAddr({ ibGib: currentIbGib })}. (E: 3ac17be916141d0c286ca5a87bb1b426)`); transitionErrors.forEach(x => errors.push(x)); } break; // stop after the first found transition error } } } else { // one of the other errors regarding mapWithTjp_YesDna or // mapWithoutTjps will exist at this point, so we don't need to add // any more errors. if (errors.length === 0) { throw new Error(`(UNEXPECTED) mapWithTjp_NoDna empty (which is an error) but errors.length === 0? If we get here, we expect one of the other errors to have been populated. (E: 7bca2866b6f8fca318923de8c88e8826)`); } } return errors; } catch (error) { console.error(`${lc} ${extractErrorMsg(error)}`); throw error; } finally { if (logalot) { console.log(`${lc} complete.`); } } } export async function getIdentity({ addr, metaspace, space, }: { addr: IbGibAddr, metaspace: MetaspaceService, space: IbGibSpaceAny, }): Promise { const lc = `[${getIdentity.name}]`; try { if (logalot) { console.log(`${lc} starting... (I: 7f1dac53ff4691cb08cd267bf75f4326)`); } const resGet = await metaspace.get({ addr, space }); const identityIbGib = resGet.ibGibs?.at(0) as KeystoneIbGib_V1; if (identityIbGib) { if (!identityIbGib.data) { throw new Error(`(UNEXPECTED) identityIbGib.data falsy? (E: d2f188232cd3bafb873b89e3a25a4826)`); } if (!isKeystone(identityIbGib)) { throw new Error(`invalid. does not start with ${KEYSTONE_ATOM} (E: 4523f8647cc139f8c49597fb86329426)`); } } return identityIbGib; } catch (error) { console.error(`${lc} ${extractErrorMsg(error)}`); throw error; } finally { if (logalot) { console.log(`${lc} complete.`); } } } export async function getIdentity_throwIfUndefined({ addr, metaspace, space, }: { addr: IbGibAddr, metaspace: MetaspaceService, space: IbGibSpaceAny, }): Promise { const identityIbGib = await getIdentity({ metaspace, addr, space }); if (!identityIbGib) { throw new Error(`addr (${addr}) not found in space: ${space.ib} (E: 7533682e805819cc78bdb0d8960be826)`); } return identityIbGib; } /** * Pure helper to generate challenges map for a pool configuration using a masterSecret. */ export async function generatePoolChallenges({ config, masterSecret }: { config: KeystonePoolConfig; masterSecret: string; }): Promise<{ [challengeId: string]: any }> { const strategy = KeystoneStrategyFactory.create({ config }); const poolSecret = await strategy.derivePoolSecret({ masterSecret }); const challenges: { [challengeId: string]: any } = {}; const targetSize = config.behavior.size; const timestamp = Date.now().toString(); for (let i = 0; i < targetSize; i++) { const challengeId = await generateOpaqueChallengeId({ salt: config.salt, timestamp, index: i }); const solution = await strategy.generateSolution({ poolSecret, poolId: config.id, challengeId }); challenges[challengeId] = await strategy.generateChallenge({ solution }); } return challenges; } /** * Pure helper to rotate a pool to a new masterSecret. * Takes the previous pool config, generates a new salt, and builds a new pool with refreshed challenges. */ export async function rotatePoolChallenges({ prevPool, newMasterSecret }: { prevPool: KeystoneChallengePool; newMasterSecret: string; }): Promise { const prevConfig = prevPool.config; const newSalt = (await getUUID()).substring(0, 16); // Copy the config exactly, updating only the salt const newConfig: KeystonePoolConfig = { ...prevConfig, salt: newSalt }; const newChallenges = await generatePoolChallenges({ config: newConfig, masterSecret: newMasterSecret }); return { id: prevPool.id, config: newConfig, challenges: newChallenges, ...(prevPool.isForeign !== undefined ? { isForeign: prevPool.isForeign } : {}), ...(prevPool.metadata ? { metadata: prevPool.metadata } : {}) }; } /** * Pure helper to verify if a candidate signing secret (password) is correct for a specific challenge pool. * Does not mutate any state. */ export async function verifyPoolSecret({ pool, signingSecret }: { pool: KeystoneChallengePool; signingSecret: string; }): Promise { const challengeIds = Object.keys(pool.challenges); if (challengeIds.length === 0) return false; try { const firstId = challengeIds[0]; const challenge = pool.challenges[firstId]; const strategy = KeystoneStrategyFactory.create({ config: pool.config }); const poolSecret = await strategy.derivePoolSecret({ masterSecret: signingSecret }); const solution = await strategy.generateSolution({ poolSecret, poolId: pool.id, challengeId: firstId }); return await strategy.validateSolution({ solution, challenge }); } catch { return false; } } /** * Type guard / helper checking whether an ibGib is a Keystone stone (atom starts with 'keystone'). */ export function isKeystone(ibGib?: IbGib_V1 | null): boolean { return (ibGib?.ib?.startsWith(KEYSTONE_ATOM)) ?? false; }