@ibgib/core-gib 0.1.6 → 0.1.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (52) hide show
  1. package/dist/keystone/keystone-config-builder.d.mts +77 -0
  2. package/dist/keystone/keystone-config-builder.d.mts.map +1 -0
  3. package/dist/keystone/keystone-config-builder.mjs +157 -0
  4. package/dist/keystone/keystone-config-builder.mjs.map +1 -0
  5. package/dist/keystone/keystone-config-builder.respec.d.mts +2 -0
  6. package/dist/keystone/keystone-config-builder.respec.d.mts.map +1 -0
  7. package/dist/keystone/keystone-config-builder.respec.mjs +34 -0
  8. package/dist/keystone/keystone-config-builder.respec.mjs.map +1 -0
  9. package/dist/keystone/keystone-constants.d.mts +38 -0
  10. package/dist/keystone/keystone-constants.d.mts.map +1 -0
  11. package/dist/keystone/keystone-constants.mjs +41 -0
  12. package/dist/keystone/keystone-constants.mjs.map +1 -0
  13. package/dist/keystone/keystone-helpers.d.mts +170 -0
  14. package/dist/keystone/keystone-helpers.d.mts.map +1 -0
  15. package/dist/keystone/keystone-helpers.mjs +639 -0
  16. package/dist/keystone/keystone-helpers.mjs.map +1 -0
  17. package/dist/keystone/keystone-service-v1.d.mts +110 -0
  18. package/dist/keystone/keystone-service-v1.d.mts.map +1 -0
  19. package/dist/keystone/keystone-service-v1.mjs +325 -0
  20. package/dist/keystone/keystone-service-v1.mjs.map +1 -0
  21. package/dist/keystone/keystone-service-v1.respec.d.mts +2 -0
  22. package/dist/keystone/keystone-service-v1.respec.d.mts.map +1 -0
  23. package/dist/keystone/keystone-service-v1.respec.mjs +838 -0
  24. package/dist/keystone/keystone-service-v1.respec.mjs.map +1 -0
  25. package/dist/keystone/keystone-types.d.mts +270 -0
  26. package/dist/keystone/keystone-types.d.mts.map +1 -0
  27. package/dist/keystone/keystone-types.mjs +50 -0
  28. package/dist/keystone/keystone-types.mjs.map +1 -0
  29. package/dist/keystone/strategy/hash-reveal-v1/hash-reveal-v1.d.mts +35 -0
  30. package/dist/keystone/strategy/hash-reveal-v1/hash-reveal-v1.d.mts.map +1 -0
  31. package/dist/keystone/strategy/hash-reveal-v1/hash-reveal-v1.mjs +107 -0
  32. package/dist/keystone/strategy/hash-reveal-v1/hash-reveal-v1.mjs.map +1 -0
  33. package/dist/keystone/strategy/keystone-strategy-factory.d.mts +15 -0
  34. package/dist/keystone/strategy/keystone-strategy-factory.d.mts.map +1 -0
  35. package/dist/keystone/strategy/keystone-strategy-factory.mjs +26 -0
  36. package/dist/keystone/strategy/keystone-strategy-factory.mjs.map +1 -0
  37. package/dist/keystone/strategy/keystone-strategy.d.mts +48 -0
  38. package/dist/keystone/strategy/keystone-strategy.d.mts.map +1 -0
  39. package/dist/keystone/strategy/keystone-strategy.mjs +14 -0
  40. package/dist/keystone/strategy/keystone-strategy.mjs.map +1 -0
  41. package/package.json +2 -1
  42. package/src/keystone/README.md +162 -0
  43. package/src/keystone/keystone-config-builder.mts +187 -0
  44. package/src/keystone/keystone-config-builder.respec.mts +49 -0
  45. package/src/keystone/keystone-constants.mts +46 -0
  46. package/src/keystone/keystone-helpers.mts +780 -0
  47. package/src/keystone/keystone-service-v1.mts +427 -0
  48. package/src/keystone/keystone-service-v1.respec.mts +1012 -0
  49. package/src/keystone/keystone-types.mts +339 -0
  50. package/src/keystone/strategy/hash-reveal-v1/hash-reveal-v1.mts +146 -0
  51. package/src/keystone/strategy/keystone-strategy-factory.mts +35 -0
  52. package/src/keystone/strategy/keystone-strategy.mts +71 -0
@@ -0,0 +1,780 @@
1
+ import { extractErrorMsg, hash, pretty } from "@ibgib/helper-gib/dist/helpers/utils-helper.mjs";
2
+ import { Ib, TransformResult } from "@ibgib/ts-gib/dist/types.mjs";
3
+ import { getIbAndGib, getIbGibAddr } from "@ibgib/ts-gib/dist/helper.mjs";
4
+ import { validateIbGibIntrinsically } from "@ibgib/ts-gib/dist/V1/validate-helper.mjs";
5
+ import { mut8 } from "@ibgib/ts-gib/dist/V1/transforms/mut8.mjs";
6
+ import { Factory_V1 } from "@ibgib/ts-gib/dist/V1/factory.mjs";
7
+ import { getGib } from "@ibgib/ts-gib/dist/V1/transforms/transform-helper.mjs";
8
+
9
+ import { GLOBAL_LOG_A_LOT } from "../core-constants.mjs";
10
+ import { KEYSTONE_ATOM } from "./keystone-constants.mjs";
11
+ import { KeystoneData_V1, KeystoneIbGib_V1, KeystoneIbInfo_V1, KeystoneChallengePool, DeterministicResult, KeystoneProof, KeystonePoolConfig, KeystoneReplenishStrategy, KEYSTONE_REPLENISH_STRATEGY_VALID_VALUES, KeystoneClaim, KeystoneSolution } from "./keystone-types.mjs";
12
+ import { MetaspaceService } from "../witness/space/metaspace/metaspace-types.mjs";
13
+ import { IbGibSpaceAny } from "../witness/space/space-base-v1.mjs";
14
+ import { KeystoneStrategyFactory } from "./strategy/keystone-strategy-factory.mjs";
15
+
16
+ const logalot = GLOBAL_LOG_A_LOT;
17
+
18
+ /**
19
+ * space-delimited keystone ib containing select keystone metadata.
20
+ *
21
+ * NOTE: This must match {@link parseKeystoneIb}
22
+ * @see {@link KeystoneIbInfo_V1}
23
+ */
24
+ export async function getKeystoneIb({
25
+ keystoneData,
26
+ }: {
27
+ keystoneData: KeystoneData_V1,
28
+ }): Promise<Ib> {
29
+ const lc = `[${getKeystoneIb.name}]`;
30
+ try {
31
+ if (logalot) { console.log(`${lc} starting... (I: c3022a146faac9730154f34d1439a225)`); }
32
+
33
+ const atom = KEYSTONE_ATOM;
34
+
35
+ const ib = [
36
+ atom,
37
+ ].join(' ');
38
+
39
+ return ib;
40
+ } catch (error) {
41
+ console.error(`${lc} ${extractErrorMsg(error)}`);
42
+ throw error;
43
+ } finally {
44
+ if (logalot) { console.log(`${lc} complete.`); }
45
+ }
46
+ }
47
+
48
+ /**
49
+ * NOTE: This must match {@link getKeystoneIb}
50
+ * @see {@link KeystoneIbInfo_V1}
51
+ */
52
+ export async function parseKeystoneIb({
53
+ ib,
54
+ }: {
55
+ ib: Ib,
56
+ }): Promise<KeystoneIbInfo_V1> {
57
+ const lc = `[${parseKeystoneIb.name}]`;
58
+ try {
59
+ if (logalot) { console.log(`${lc} starting... (I: 73cb6832984255ed48b2f44db6a21e25)`); }
60
+ const [
61
+ atom
62
+ ] = ib.split(' ');
63
+
64
+ if (atom !== KEYSTONE_ATOM) {
65
+ throw new Error(`invalid keystone ib. atom found in ib (${atom}) does not match keystone atom (${KEYSTONE_ATOM}) (E: 79b3d587824c4271b6e60acc76e0c825)`);
66
+ }
67
+
68
+ return {
69
+ atom,
70
+ }
71
+ } catch (error) {
72
+ console.error(`${lc} ${extractErrorMsg(error)}`);
73
+ throw error;
74
+ } finally {
75
+ if (logalot) { console.log(`${lc} complete.`); }
76
+ }
77
+ }
78
+
79
+ /**
80
+ * The Policy Engine.
81
+ * Calculates exactly which challenges MUST be consumed based on config and demands.
82
+ * Enforces STRICT DISTINCTNESS (No double-dipping).
83
+ */
84
+ export function getDeterministicRequirements({
85
+ pool,
86
+ requiredChallengeIds,
87
+ targetAddr
88
+ }: {
89
+ pool: KeystoneChallengePool;
90
+ requiredChallengeIds?: string[];
91
+ targetAddr?: string;
92
+ }): DeterministicResult {
93
+ const behavior = pool.config.behavior;
94
+ const mandatory = new Set<string>();
95
+
96
+ // Start with all available IDs.
97
+ // We assume Object.keys respects insertion order (ES2015+), crucial for FIFO.
98
+ let available = Object.keys(pool.challenges);
99
+
100
+ // ---------------------------------------------------------
101
+ // 1. Alice's Demands (Explicit Requirements)
102
+ // ---------------------------------------------------------
103
+ if (requiredChallengeIds && requiredChallengeIds.length > 0) {
104
+ for (const id of requiredChallengeIds) {
105
+ if (!pool.challenges[id]) {
106
+ throw new Error(`(UNEXPECTED) Required challenge ID not found in pool: ${id} (E: 2c9f8...)`);
107
+ }
108
+ // Strict: Consume it.
109
+ if (!available.includes(id)) {
110
+ // Should be caught by check above, but handles duplicates in 'demands'
111
+ continue;
112
+ }
113
+ mandatory.add(id);
114
+ }
115
+ // Remove from available pool
116
+ available = available.filter(id => !mandatory.has(id));
117
+ }
118
+
119
+ // ---------------------------------------------------------
120
+ // 2. Target Binding (Explicit Buckets)
121
+ // ---------------------------------------------------------
122
+ if (behavior.targetBindingChars > 0 && targetAddr) {
123
+ const { gib } = getIbAndGib({ ibGibAddr: targetAddr });
124
+ if (gib) {
125
+ // Get required hex prefixes (e.g. 'a', 'b', 'c', '1')
126
+ const prefixes = gib.substring(0, behavior.targetBindingChars).toLowerCase();
127
+
128
+ for (const char of prefixes) {
129
+ // Look in the Explicit Bucket
130
+ const bucket = pool.bindingMap[char] || [];
131
+
132
+ // Find the first ID in this bucket that is still in 'available'
133
+ const match = bucket.find(id => available.includes(id));
134
+
135
+ if (!match) {
136
+ throw new Error(`Entropy Exhaustion. Cannot satisfy binding for char '${char}'. (E: 8d3a1...)`);
137
+ }
138
+
139
+ // Strict: Consume it.
140
+ mandatory.add(match);
141
+ available = available.filter(id => id !== match);
142
+ }
143
+ }
144
+ }
145
+
146
+ // ---------------------------------------------------------
147
+ // 3. FIFO (Sequential)
148
+ // ---------------------------------------------------------
149
+ if (behavior.selectSequentially > 0) {
150
+ // Take the first N from the remaining available list
151
+ if (available.length < behavior.selectSequentially) {
152
+ console.error(`[getDeterministicRequirements] Entropy Exhaustion! AvailableCount: ${available.length}, Required Seq: ${behavior.selectSequentially}. Available IDs: ${available.join(',')}`);
153
+ throw new Error(`Entropy Exhaustion. Insufficient challenges for FIFO requirement. (E: 9c2b4...)`);
154
+ }
155
+
156
+ const fifoIds = available.slice(0, behavior.selectSequentially);
157
+ fifoIds.forEach(id => mandatory.add(id));
158
+
159
+ // Remove from available
160
+ available = available.slice(behavior.selectSequentially);
161
+ }
162
+
163
+ return { mandatoryIds: mandatory, availableIds: available };
164
+ }
165
+
166
+ /**
167
+ * Helper to update the Binding Map when adding new Challenge IDs.
168
+ * Uses "Implicit Bucketing" (ID start char) but can be extended for full coverage.
169
+ */
170
+ export function addToBindingMap(
171
+ map: { [char: string]: string[] },
172
+ challengeId: string
173
+ ): void {
174
+ const firstChar = challengeId.charAt(0).toLowerCase();
175
+ // Validate it is hex
176
+ if (/[0-9a-f]/.test(firstChar)) {
177
+ if (!map[firstChar]) map[firstChar] = [];
178
+ map[firstChar].push(challengeId);
179
+
180
+ // OPTIONAL: Implement Full Coverage Strategy here?
181
+ // e.g. map[challengeId[1]].push(challengeId) ...
182
+ // For V1, we stick to Native/Implicit bucket (Index 0).
183
+ }
184
+ }
185
+
186
+ /**
187
+ * Helper to clean up Binding Map when removing IDs.
188
+ */
189
+ export function removeFromBindingMap(
190
+ map: { [char: string]: string[] },
191
+ challengeId: string
192
+ ): void {
193
+ // Since we don't know exactly which buckets an ID is in (if we did multi-bucket),
194
+ // we strictly should scan all. For V1 Native, we check first char.
195
+ // SAFE IMPLEMENTATION: Scan all buckets.
196
+ for (const key of Object.keys(map)) {
197
+ map[key] = map[key].filter(id => id !== challengeId);
198
+ }
199
+ }
200
+
201
+ /**
202
+ * Selects the specific pool to use for an operation based on ID, filter criteria, or verb authorization.
203
+ *
204
+ * @returns The matching KeystoneChallengePool
205
+ * @throws If no pool matches or if multiple pools match but one was expected.
206
+ */
207
+ export function resolveTargetPool({
208
+ pools,
209
+ poolId,
210
+ poolFilter,
211
+ verb,
212
+ }: {
213
+ pools: KeystoneChallengePool[];
214
+ /**
215
+ * Explicit ID of the pool to use.
216
+ */
217
+ poolId?: string;
218
+ /**
219
+ * Optional predicate to find a pool.
220
+ * Useful for finding delegates via metadata.
221
+ */
222
+ poolFilter?: (pool: KeystoneChallengePool) => boolean;
223
+ /**
224
+ * The verb being performed (e.g. 'revoke', 'manage', 'post').
225
+ * Used for authorization checks and auto-resolution.
226
+ */
227
+ verb?: string;
228
+ }): KeystoneChallengePool {
229
+ const lc = `[resolveTargetPool]`;
230
+ try {
231
+ let pool: KeystoneChallengePool | undefined;
232
+
233
+ if (poolId) {
234
+ // 1. Explicit ID Strategy
235
+ pool = pools.find(p => p.id === poolId);
236
+ if (!pool) { throw new Error(`Pool not found with ID: ${poolId} (E: 4a2b17428515c1e82813158581898125)`); }
237
+ } else if (poolFilter) {
238
+ // 2. Filter Strategy
239
+ const matches = pools.filter(poolFilter);
240
+ if (matches.length === 0) { throw new Error(`No pool matched the provided filter. (E: 5b3c27428515c1e82813158581898125)`); }
241
+ // For now, we take the first match. In future, might want to be strict about uniqueness.
242
+ pool = matches[0];
243
+ } else {
244
+ // 3. Auto-Resolution by Verb Strategy
245
+ if (!verb) { throw new Error(`Cannot auto-resolve pool without a verb. (E: 6c4d37428515c1e82813158581898125)`); }
246
+
247
+ // Priority A: Look for Specific Match (Pool explicitly lists this verb)
248
+ pool = pools.find(p =>
249
+ p.config.allowedVerbs && p.config.allowedVerbs.includes(verb)
250
+ );
251
+
252
+ // Priority B: Look for General/Default (No restrictions / Wildcard)
253
+ if (!pool) {
254
+ pool = pools.find(p =>
255
+ !p.config.allowedVerbs || p.config.allowedVerbs.length === 0
256
+ );
257
+ }
258
+
259
+ if (!pool) { throw new Error(`No suitable pool found for verb: ${verb} (E: 7d5e47428515c1e82813158581898125)`); }
260
+ }
261
+
262
+ // 4. Authorization Check (Applies to all strategies if verb is present)
263
+ if (verb && pool.config.allowedVerbs && pool.config.allowedVerbs.length > 0) {
264
+ if (!pool.config.allowedVerbs.includes(verb)) {
265
+ throw new Error(`Pool ${pool.id} is not authorized for verb: ${verb} (E: 8e6f57428515c1e82813158581898125)`);
266
+ }
267
+ }
268
+
269
+ return pool;
270
+ } catch (error) {
271
+ console.error(`${lc} ${extractErrorMsg(error)}`);
272
+ throw error;
273
+ }
274
+ }
275
+
276
+ /**
277
+ * Calculates the complete list of Challenge IDs to solve for a given operation.
278
+ * Combines Deterministic requirements (Mandatory/Binding/FIFO) with Stochastic requirements.
279
+ *
280
+ * @returns Array of unique challenge IDs.
281
+ */
282
+ export function selectChallengeIds({
283
+ pool,
284
+ targetAddr,
285
+ requiredChallengeIds,
286
+ }: {
287
+ pool: KeystoneChallengePool;
288
+ /**
289
+ * The address of the target ibgib (used for binding entropy).
290
+ */
291
+ targetAddr?: string;
292
+ /**
293
+ * Explicit demands from a verifier.
294
+ */
295
+ requiredChallengeIds?: string[];
296
+ }): string[] {
297
+ const lc = `[selectChallengeIds]`;
298
+ try {
299
+ // 1. Get Deterministic Requirements
300
+ const { mandatoryIds, availableIds } = getDeterministicRequirements({
301
+ pool,
302
+ requiredChallengeIds,
303
+ targetAddr
304
+ });
305
+
306
+ // 2. Stochastic Selection
307
+ const randomCount = pool.config.behavior.selectRandomly;
308
+ const randomIds: string[] = [];
309
+
310
+ if (randomCount > 0) {
311
+ if (availableIds.length < randomCount) {
312
+ throw new Error(`Insufficient challenges for random requirement. Need ${randomCount}, have ${availableIds.length} (E: 9f7a67428515c1e82813158581898125)`);
313
+ }
314
+ // Shuffle & Pick
315
+ // Note: simple Math.random sort is sufficient for V1 stochastic selection
316
+ // as we are just picking from valid available options.
317
+ const shuffled = [...availableIds].sort(() => 0.5 - Math.random());
318
+ randomIds.push(...shuffled.slice(0, randomCount));
319
+ }
320
+
321
+ // 3. Combine
322
+ return [...mandatoryIds, ...randomIds];
323
+ } catch (error) {
324
+ console.error(`${lc} ${extractErrorMsg(error)}`);
325
+ throw error;
326
+ }
327
+ }
328
+
329
+ /**
330
+ * Generates an opaque, collision-resistant ID for a challenge.
331
+ * atow (2025/12/22) - Hash(Salt + Timestamp + Index)
332
+ */
333
+ export async function generateOpaqueChallengeId({
334
+ salt,
335
+ timestamp,
336
+ index
337
+ }: {
338
+ salt: string,
339
+ timestamp: string,
340
+ index: number
341
+ }): Promise<string> {
342
+ // Use first 16 chars of hex (64 bits) for brevity + safety.
343
+ const raw = await hash({ s: `${salt}${timestamp}${index}` });
344
+ return raw.substring(0, 16);
345
+ }
346
+
347
+ /**
348
+ * Calculates the NEXT state of the Challenge Pools given a specific consumption event.
349
+ * Handles TopUp, ReplaceAll, Consume, and ScorchedEarth strategies.
350
+ *
351
+ * @returns The new array of KeystoneChallengePools (including the modified one).
352
+ */
353
+ export async function applyReplenishmentStrategy({
354
+ prevPools,
355
+ targetPoolId,
356
+ consumedIds,
357
+ masterSecret,
358
+ strategy,
359
+ config,
360
+ }: {
361
+ prevPools: KeystoneChallengePool[],
362
+ targetPoolId: string,
363
+ consumedIds: string[],
364
+ masterSecret: string,
365
+ /**
366
+ * The instantiated KeystoneStrategy (e.g. HashRevealV1) used to generate new challenges.
367
+ */
368
+ strategy: any, // Typed as 'any' or 'KeystoneStrategyAny' to avoid circular dep if possible
369
+ config: KeystonePoolConfig,
370
+ }): Promise<KeystoneChallengePool[]> {
371
+ const lc = `[applyReplenishmentStrategy]`;
372
+ try {
373
+ const newPools = JSON.parse(JSON.stringify(prevPools));
374
+ const targetIdx = newPools.findIndex((p: any) => p.id === targetPoolId);
375
+ if (targetIdx === -1) { throw new Error(`Target pool ${targetPoolId} not found in keystone data. (E: 75200388d22744838634524233772545)`); }
376
+ const pool = newPools[targetIdx];
377
+
378
+ const poolSecret = await strategy.derivePoolSecret({ masterSecret });
379
+ const timestamp = Date.now().toString();
380
+
381
+ const strategyType = config.behavior.replenish;
382
+
383
+ // Clean up Binding Map for consumed IDs
384
+ consumedIds.forEach(id => {
385
+ if (pool.bindingMap) { removeFromBindingMap(pool.bindingMap, id); }
386
+ });
387
+
388
+ if (strategyType === KeystoneReplenishStrategy.topUp) {
389
+ // Remove consumed
390
+ consumedIds.forEach(id => delete pool.challenges[id]);
391
+
392
+ // Add New
393
+ for (let i = 0; i < consumedIds.length; i++) {
394
+ const newId = await generateOpaqueChallengeId({
395
+ salt: config.salt, timestamp, index: i
396
+ });
397
+
398
+ const solution = await strategy.generateSolution({
399
+ poolSecret, poolId: pool.id, challengeId: newId
400
+ });
401
+ pool.challenges[newId] = await strategy.generateChallenge({ solution });
402
+
403
+ // Update Binding Map
404
+ if (!pool.bindingMap) { pool.bindingMap = {}; }
405
+ addToBindingMap(pool.bindingMap, newId);
406
+ }
407
+ } else if (strategyType === KeystoneReplenishStrategy.replaceAll) {
408
+ pool.challenges = {};
409
+ pool.bindingMap = {};
410
+
411
+ for (let i = 0; i < config.behavior.size; i++) {
412
+ const newId = await generateOpaqueChallengeId({
413
+ salt: config.salt, timestamp, index: i
414
+ });
415
+ const solution = await strategy.generateSolution({
416
+ poolSecret, poolId: pool.id, challengeId: newId
417
+ });
418
+ pool.challenges[newId] = await strategy.generateChallenge({ solution });
419
+ addToBindingMap(pool.bindingMap, newId);
420
+ }
421
+ } else if (strategyType === KeystoneReplenishStrategy.consume) {
422
+ consumedIds.forEach(id => delete pool.challenges[id]);
423
+ } else if (strategyType === KeystoneReplenishStrategy.scorchedEarth) {
424
+ pool.challenges = {};
425
+ pool.bindingMap = {};
426
+ } else {
427
+ throw new Error(`Unknown replenish strategy: ${strategyType}. Valid list: ${pretty(KEYSTONE_REPLENISH_STRATEGY_VALID_VALUES)} (E: 0acf56f1e1486240080e11e8046d0825)`);
428
+ }
429
+
430
+ return newPools;
431
+ } catch (error) {
432
+ console.error(`${lc} ${extractErrorMsg(error)}`);
433
+ throw error;
434
+ }
435
+ }
436
+
437
+ /**
438
+ * High-level orchestration helper.
439
+ * 1. Instantiates Strategy.
440
+ * 2. Derives Secret.
441
+ * 3. Solves Challenges (Generates Solutions).
442
+ * 4. Replenishes Pool (Calculates the next state of ALL pools).
443
+ * 5. Constructs Proof.
444
+ *
445
+ * @returns The resulting Proof and the full list of Next Pools.
446
+ */
447
+ export async function solveAndReplenish({
448
+ targetPoolId,
449
+ prevPools,
450
+ masterSecret,
451
+ challengeIds,
452
+ claim,
453
+ requiredChallengeIds,
454
+ }: {
455
+ /**
456
+ * The ID of the pool to use for signing.
457
+ */
458
+ targetPoolId: string;
459
+ /**
460
+ * The full list of pools from the previous Keystone frame.
461
+ * Required to reconstruct the full state for the next frame.
462
+ */
463
+ prevPools: KeystoneChallengePool[];
464
+ masterSecret: string;
465
+ challengeIds: string[];
466
+ claim: Partial<KeystoneClaim>;
467
+ requiredChallengeIds?: string[];
468
+ }): Promise<{ proof: KeystoneProof, nextPools: KeystoneChallengePool[] }> {
469
+ const lc = `[solveAndReplenish]`;
470
+ try {
471
+ if (logalot) { console.log(`${lc} starting... poolId: ${targetPoolId}, ids: ${challengeIds.length}`); }
472
+
473
+ const pool = prevPools.find(p => p.id === targetPoolId);
474
+ if (!pool) { throw new Error(`Target pool not found: ${targetPoolId} (E: a1b2c3d4e5f6a7b8c9d0e1f2a3b4c5d6)`); }
475
+
476
+ const strategy = KeystoneStrategyFactory.create({ config: pool.config });
477
+ const poolSecret = await strategy.derivePoolSecret({ masterSecret });
478
+ const solutions: KeystoneSolution[] = [];
479
+
480
+ // 1. Solve
481
+ for (const id of challengeIds) {
482
+ const solution = await strategy.generateSolution({
483
+ poolSecret, poolId: pool.id, challengeId: id,
484
+ });
485
+ solutions.push(solution);
486
+ }
487
+
488
+ // 2. Construct Proof
489
+ const proof: KeystoneProof = {
490
+ claim,
491
+ solutions,
492
+ requiredChallengeIds: (requiredChallengeIds && requiredChallengeIds.length > 0) ? requiredChallengeIds : undefined
493
+ };
494
+
495
+ // 3. Replenish
496
+ const nextPools = await applyReplenishmentStrategy({
497
+ prevPools,
498
+ targetPoolId: pool.id,
499
+ consumedIds: challengeIds,
500
+ masterSecret,
501
+ strategy,
502
+ config: pool.config
503
+ });
504
+
505
+ return { proof, nextPools };
506
+ } catch (error) {
507
+ console.error(`${lc} ${extractErrorMsg(error)}`);
508
+ throw error;
509
+ }
510
+ }
511
+
512
+ /**
513
+ * Validates the transition from Prev -> Curr.
514
+ * Enforces Cryptography AND Behavioral Policy.
515
+ *
516
+ * @returns Array of validation error strings. Empty array means Valid.
517
+ */
518
+ export async function validateKeystoneTransition({
519
+ currentIbGib,
520
+ prevIbGib,
521
+ }: {
522
+ currentIbGib: KeystoneIbGib_V1;
523
+ prevIbGib: KeystoneIbGib_V1;
524
+ }): Promise<string[]> {
525
+ const lc = `[${validateKeystoneTransition.name}]`;
526
+ const errors: string[] = [];
527
+ try {
528
+ if (!currentIbGib) { throw new Error(`(UNEXPECTED) currentIbGib falsy? (E: 3c0f02655fa8279e386a079ebb604b25)`); }
529
+ if (!prevIbGib) { throw new Error(`(UNEXPECTED) prevIbGib falsy? (E: 0d07c812634d839c784f31b8848ba825)`); }
530
+
531
+ // intrinsic validation
532
+ const validationErrors = await validateIbGibIntrinsically({ ibGib: currentIbGib });
533
+ if (validationErrors && validationErrors.length > 0) {
534
+ errors.push(...validationErrors);
535
+ }
536
+
537
+ const currData = currentIbGib.data!;
538
+ const prevData = prevIbGib.data!;
539
+
540
+ for (const proof of currData.proofs) {
541
+ if (proof.solutions.length === 0) {
542
+ errors.push(`Proof ${proof.id || 'unknown'} has no solutions.`);
543
+ continue;
544
+ }
545
+
546
+ const poolId = proof.solutions[0].poolId;
547
+
548
+ // Standard Verification (Internal Pools Only)
549
+ // The pool MUST be present in the previous frame.
550
+ const pool = prevData.challengePools.find(p => p.id === poolId);
551
+ if (!pool) {
552
+ errors.push(`Proof references unknown pool: ${poolId}`);
553
+ continue;
554
+ }
555
+
556
+ await verifyProofAgainstPool({ proof, pool, errors });
557
+
558
+ } // End proof loop
559
+
560
+ // Revocation Logic checks
561
+ if (currData.revocationInfo) {
562
+ const target = currData.revocationInfo.proof.claim.target;
563
+ const expectedTarget = getIbGibAddr({ ibGib: prevIbGib });
564
+ if (target !== expectedTarget) {
565
+ errors.push(`Revocation target mismatch. Expected ${expectedTarget}, got ${target}`);
566
+ }
567
+ }
568
+
569
+ return errors;
570
+ } catch (error) {
571
+ console.error(`${lc} ${extractErrorMsg(error)}`);
572
+ throw error; // System errors still throw
573
+ } finally {
574
+ if (logalot) { console.log(`${lc} complete.`); }
575
+ }
576
+ }
577
+
578
+ /**
579
+ * Helper to verify a single proof against a specific pool.
580
+ */
581
+ export async function verifyProofAgainstPool({
582
+ proof,
583
+ pool,
584
+ errors,
585
+ }: {
586
+ proof: KeystoneProof;
587
+ pool: KeystoneChallengePool;
588
+ errors: string[];
589
+ }): Promise<void> {
590
+ const lc = `[${verifyProofAgainstPool.name}]`;
591
+ try {
592
+ if (logalot) { console.log(`${lc} starting... (I: b8f9b6085888eea2258bf579ecd5e825)`); }
593
+
594
+ // 0. VERB AUTH
595
+ if (pool.config.allowedVerbs && pool.config.allowedVerbs.length > 0) {
596
+ if (!proof.claim.verb || !pool.config.allowedVerbs.includes(proof.claim.verb)) {
597
+ errors.push(`Policy Violation: Pool ${pool.id} used for unauthorized verb ${proof.claim.verb}`);
598
+ }
599
+ }
600
+
601
+ // 1. Reconstruct Deterministic Requirements
602
+ const { mandatoryIds, availableIds } = getDeterministicRequirements({
603
+ pool,
604
+ requiredChallengeIds: proof.requiredChallengeIds,
605
+ targetAddr: proof.claim.target // Not used extensively in V1 logic yet, mainly for logging/context
606
+ });
607
+
608
+ // 2. Check Mandatory
609
+ const proofIds = new Set(proof.solutions.map(s => s.challengeId));
610
+ for (const id of mandatoryIds) {
611
+ if (!proofIds.has(id)) {
612
+ errors.push(`Policy Violation: Missing mandatory challenge ${id}`);
613
+ }
614
+ }
615
+
616
+ // 3. Stochastic
617
+ const randomCandidates = [...proofIds].filter(id => !mandatoryIds.has(id));
618
+ const requiredRandomCount = pool.config.behavior.selectRandomly;
619
+ if (randomCandidates.length < requiredRandomCount) {
620
+ errors.push(`Policy Violation: Insufficient random count. Need ${requiredRandomCount}, got ${randomCandidates.length}`);
621
+ }
622
+
623
+ // 4. Validity (Double Dip / Existence)
624
+ for (const id of randomCandidates) {
625
+ if (!availableIds.includes(id)) {
626
+ errors.push(`Policy Violation: ID ${id} is invalid or double-dipped.`);
627
+ }
628
+ }
629
+
630
+ // 5. Crypto
631
+ const strategy = KeystoneStrategyFactory.create({ config: pool.config });
632
+ for (const solution of proof.solutions) {
633
+ const challenge = pool.challenges[solution.challengeId];
634
+ if (!challenge) {
635
+ errors.push(`Crypto Violation: Challenge ${solution.challengeId} not found in pool.`);
636
+ } else {
637
+ const isValid = await strategy.validateSolution({ solution, challenge });
638
+ if (!isValid) {
639
+ errors.push(`Crypto Violation: Solution for ${solution.challengeId} is invalid.`);
640
+ }
641
+ }
642
+ }
643
+ } catch (error) {
644
+ console.error(`${lc} ${extractErrorMsg(error)}`);
645
+ throw error;
646
+ } finally {
647
+ if (logalot) { console.log(`${lc} complete.`); }
648
+ }
649
+ }
650
+
651
+ export async function evolvePersistAndRegisterKeystone({
652
+ prevIbGib,
653
+ newData,
654
+ metaspace,
655
+ space,
656
+ }: {
657
+ prevIbGib: KeystoneIbGib_V1,
658
+ newData: KeystoneData_V1
659
+ metaspace: MetaspaceService,
660
+ space: IbGibSpaceAny,
661
+ }): Promise<KeystoneIbGib_V1> {
662
+ const lc = `[${evolvePersistAndRegisterKeystone.name}]`;
663
+ try {
664
+ if (logalot) { console.log(`${lc} starting... (I: 8b10e8920f08b7842803665834cf8925)`); }
665
+
666
+ if (!prevIbGib.data) { throw new Error(`(UNEXPECTED) prevIbGib.data falsy? (E: 5e84875bf992c585b979e6c8ed5bf225)`); }
667
+ 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)`); }
668
+
669
+ const prevData = prevIbGib.data;
670
+ /**
671
+ * we want to completely replace these keys, so we will remove them
672
+ * from the data. This occurs first in the underlying mut8
673
+ * transform.
674
+ * @see {@link mut8}
675
+ */
676
+ let dataToRemove: Partial<KeystoneData_V1> | undefined = {}
677
+ if (prevData.proofs) { dataToRemove.proofs = []; }
678
+ if (prevData.challengePools) { dataToRemove.challengePools = []; }
679
+ if (prevData.frameDetails) { dataToRemove.frameDetails = {}; }
680
+ if (Object.keys(dataToRemove).length === 0) { dataToRemove = undefined; }
681
+
682
+ const resMut8 = await mut8({
683
+ src: prevIbGib,
684
+ dataToRemove,
685
+ dataToAddOrPatch: newData,
686
+ // dna: false, // explicitly set to false just to show
687
+ nCounter: true,
688
+ });
689
+
690
+ 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)`); }
691
+ if (!!resMut8.dnas) { throw new Error(`(UNEXPECTED) resMut8.dnas truthy? We do not want dnas with keystones. (E: 49470513d018f97d28024f4e82da3b25)`); }
692
+
693
+
694
+ const newKeystoneIbGib = resMut8.newIbGib as KeystoneIbGib_V1;
695
+
696
+ // run validation here
697
+ const errors = await validateKeystoneTransition({
698
+ currentIbGib: newKeystoneIbGib,
699
+ prevIbGib,
700
+ });
701
+ if (errors.length > 0) {
702
+ console.error(`${lc} Validation Failed:\n${errors.join('\n')}`);
703
+ throw new Error(`(UNEXPECTED) invalid keystone after we just evolved it? Errors: ${errors.join('; ')} (E: ae2c58406c1db7687879dfb89fc1f825)`);
704
+ }
705
+
706
+ // save and register
707
+ await metaspace.put({ ibGib: newKeystoneIbGib, space, });
708
+ await metaspace.registerNewIbGib({ ibGib: newKeystoneIbGib, space, });
709
+
710
+ return newKeystoneIbGib;
711
+ } catch (error) {
712
+ console.error(`${lc} ${extractErrorMsg(error)}`);
713
+ throw error;
714
+ } finally {
715
+ if (logalot) { console.log(`${lc} complete.`); }
716
+ }
717
+ }
718
+
719
+ /**
720
+ * Creates a new keystone ibgib that has no dna and no past.
721
+ */
722
+ export async function createKeystoneIbGibImpl({
723
+ data,
724
+ metaspace,
725
+ space,
726
+ }: {
727
+ data: KeystoneData_V1,
728
+ metaspace: MetaspaceService,
729
+ space?: IbGibSpaceAny,
730
+ }): Promise<KeystoneIbGib_V1> {
731
+ const lc = `[${createKeystoneIbGibImpl.name}]`;
732
+ try {
733
+ if (logalot) { console.log(`${lc} starting... (I: 5e32389700e9899e788cbefacef7c825)`); }
734
+
735
+ space ??= await metaspace.getLocalUserSpace({ lock: false });
736
+ if (!space) { throw new Error(`(UNEXPECTED) space was falsy and we couldn't get default local user space from metaspace? (E: 9a6498cf16a8801f19ec376749742225)`); }
737
+
738
+ // create the actual keystoneIbGib
739
+ const resFirstGen = await Factory_V1.firstGen({
740
+ parentIbGib: Factory_V1.primitive({ ib: KEYSTONE_ATOM }),
741
+ ib: await getKeystoneIb({ keystoneData: data }),
742
+ data,
743
+ dna: false,
744
+ nCounter: true,
745
+ tjp: {
746
+ timestamp: true,
747
+ uuid: true,
748
+ },
749
+ }) as TransformResult<KeystoneIbGib_V1>;
750
+ const keystoneIbGib = resFirstGen.newIbGib;
751
+
752
+ if (!keystoneIbGib.data) { throw new Error(`(UNEXPECTED) keystoneIbGib.data falsy? We expect the data to be populated with real keystone data. (E: 38a358facdb89d16d81d48c8520d3d25)`); }
753
+ if (!keystoneIbGib.rel8ns) { throw new Error(`(UNEXPECTED) keystoneIbGib.rel8ns falsy? we expect the rel8ns to have ancestor and past. (E: 20cb7723dc33ae1ef808fe76d1bf4b25)`); }
754
+ if (!keystoneIbGib.rel8ns.past || keystoneIbGib.rel8ns.past.length === 0) {
755
+ 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)`);
756
+ }
757
+
758
+ // reset n
759
+ keystoneIbGib.data.n = 0;
760
+ // reset tjp
761
+ keystoneIbGib.data.isTjp = true;
762
+ delete keystoneIbGib.rel8ns.tjp;
763
+ // reset past
764
+ delete keystoneIbGib.rel8ns.past;
765
+
766
+ // recalculate gib
767
+ keystoneIbGib.gib = await getGib({ ibGib: keystoneIbGib });
768
+
769
+ // save and register
770
+ await metaspace.put({ ibGib: keystoneIbGib, space, });
771
+ await metaspace.registerNewIbGib({ ibGib: keystoneIbGib, space, });
772
+
773
+ return keystoneIbGib;
774
+ } catch (error) {
775
+ console.error(`${lc} ${extractErrorMsg(error)}`);
776
+ throw error;
777
+ } finally {
778
+ if (logalot) { console.log(`${lc} complete.`); }
779
+ }
780
+ }