@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,427 @@
1
+ import { extractErrorMsg, } from '@ibgib/helper-gib/dist/helpers/utils-helper.mjs';
2
+ import { getIbGibAddr } from '@ibgib/ts-gib/dist/helper.mjs';
3
+
4
+ import { GLOBAL_LOG_A_LOT } from '../core-constants.mjs';
5
+ import {
6
+ KeystoneData_V1, KeystoneIbGib_V1, KeystonePoolConfig, KeystoneClaim,
7
+ KeystoneChallengePool, KeystoneReplenishStrategy, KeystoneRevocationInfo,
8
+ } from './keystone-types.mjs';
9
+ import { KeystoneStrategyFactory } from './strategy/keystone-strategy-factory.mjs';
10
+ import { POOL_ID_REVOKE, KEYSTONE_VERB_REVOKE, KEYSTONE_VERB_MANAGE } from './keystone-constants.mjs';
11
+ import {
12
+ addToBindingMap, createKeystoneIbGibImpl, evolvePersistAndRegisterKeystone,
13
+ generateOpaqueChallengeId, resolveTargetPool, selectChallengeIds,
14
+ solveAndReplenish, validateKeystoneTransition,
15
+ } from './keystone-helpers.mjs';
16
+ import { IbGibSpaceAny } from '../witness/space/space-base-v1.mjs';
17
+ import { MetaspaceService } from '../witness/space/metaspace/metaspace-types.mjs';
18
+
19
+ const logalot = GLOBAL_LOG_A_LOT || true;
20
+
21
+ /**
22
+ * Facade for managing Keystone Identities.
23
+ *
24
+ * Handles Genesis, Authorized Evolution (Signing), and Validation.
25
+ */
26
+ export class KeystoneService_V1 {
27
+ protected lc: string = `[${KeystoneService_V1.name}]`;
28
+
29
+ /**
30
+ * Creates a brand new Keystone Identity Timeline.
31
+ */
32
+ async genesis({
33
+ masterSecret,
34
+ configs,
35
+ metaspace,
36
+ space,
37
+ }: {
38
+ masterSecret: string;
39
+ configs: KeystonePoolConfig[];
40
+ metaspace: MetaspaceService;
41
+ space: IbGibSpaceAny;
42
+ }): Promise<KeystoneIbGib_V1> {
43
+ const lc = `${this.lc}[${this.genesis.name}]`;
44
+ try {
45
+ if (logalot) { console.log(`${lc} starting... (I: c98ae8adbc5888dbf84c5aced7610b25)`); }
46
+
47
+ const challengePools: KeystoneChallengePool[] = [];
48
+
49
+ for (const config of configs) {
50
+ const strategy = KeystoneStrategyFactory.create({ config });
51
+ const poolSecret = await strategy.derivePoolSecret({ masterSecret });
52
+ const challenges: { [id: string]: any } = {};
53
+ const bindingMap: { [char: string]: string[] } = {};
54
+
55
+ const targetSize = config.behavior.size;
56
+ const timestamp = Date.now().toString();
57
+
58
+ for (let i = 0; i < targetSize; i++) {
59
+ const challengeId = await generateOpaqueChallengeId({
60
+ salt: config.salt, timestamp, index: i
61
+ });
62
+
63
+ const solution = await strategy.generateSolution({
64
+ poolSecret, poolId: config.salt, challengeId,
65
+ });
66
+ const challenge = await strategy.generateChallenge({ solution });
67
+ challenges[challengeId] = challenge;
68
+
69
+ // Populate Binding Map
70
+ addToBindingMap(bindingMap, challengeId);
71
+ }
72
+
73
+ challengePools.push({
74
+ id: config.salt,
75
+ config,
76
+ challenges,
77
+ bindingMap
78
+ });
79
+ }
80
+
81
+ if (challengePools.length === 0) { throw new Error(`No challenge pools created. (E: 38e538530996940e1f16a8b199995825)`); }
82
+
83
+ const data: KeystoneData_V1 = { challengePools, proofs: [] };
84
+ const keystoneIbGib = await createKeystoneIbGibImpl({ data, metaspace, space });
85
+ return keystoneIbGib;
86
+ } catch (error) {
87
+ console.error(`${lc} ${extractErrorMsg(error)}`);
88
+ throw error;
89
+ } finally {
90
+ if (logalot) { console.log(`${lc} complete.`); }
91
+ }
92
+ }
93
+
94
+ /**
95
+ * Signs a claim by solving challenges from a specific pool and evolving the Keystone timeline.
96
+ *
97
+ * Uses a hybrid selection strategy: Mandatory IDs (Alice) + Sequential (FIFO) + Random (Stochastic).
98
+ *
99
+ * Supports Delegation via `poolFilter` to find specific foreign pools.
100
+ */
101
+ async sign({
102
+ latestKeystone,
103
+ masterSecret,
104
+ claim,
105
+ poolId,
106
+ poolFilter,
107
+ requiredChallengeIds = [],
108
+ frameDetails,
109
+ metaspace,
110
+ space,
111
+ }: {
112
+ latestKeystone: KeystoneIbGib_V1;
113
+ /**
114
+ * The secret used to solve the challenges.
115
+ * If signing with a native pool, this is the User's Master Secret.
116
+ * If signing with a foreign/delegated pool, this is the Delegate's Secret.
117
+ */
118
+ masterSecret: string;
119
+ claim: Partial<KeystoneClaim>;
120
+ /**
121
+ * Explicit ID of the pool to use.
122
+ */
123
+ poolId?: string;
124
+ /**
125
+ * Optional predicate to find a pool.
126
+ * Useful for finding delegates via metadata without knowing the exact ID.
127
+ * e.g. (p) => p.metadata?.delegate === 'Bob'
128
+ */
129
+ poolFilter?: (pool: KeystoneChallengePool) => boolean;
130
+ requiredChallengeIds?: string[];
131
+ frameDetails?: any;
132
+ metaspace: MetaspaceService;
133
+ space: IbGibSpaceAny;
134
+ }): Promise<KeystoneIbGib_V1> {
135
+ const lc = `${this.lc}[${this.sign.name}]`;
136
+ try {
137
+ if (logalot) { console.log(`${lc} starting...`); }
138
+
139
+ const prevData = latestKeystone.data!;
140
+
141
+ if (prevData.revocationInfo) { throw new Error(`Keystone has been revoked. Cannot sign. (E: 4f2198c39116d15c48ba191940316825)`); }
142
+
143
+ // 1. Identify Authority (Resolve Pool)
144
+ const pool = resolveTargetPool({
145
+ pools: prevData.challengePools,
146
+ poolId,
147
+ poolFilter,
148
+ verb: claim.verb
149
+ });
150
+
151
+ if (logalot) { console.log(`${lc} Selected pool: ${pool.id} (size: ${Object.keys(pool.challenges).length}) (I: genuuid)`); }
152
+
153
+ // 2. Calculate Costs (Select IDs)
154
+ const idsToSolve = selectChallengeIds({
155
+ pool,
156
+ targetAddr: claim.target,
157
+ requiredChallengeIds
158
+ });
159
+
160
+ // 3. Pay the Cost (Solve & Replenish)
161
+ // This helper handles the Strategy creation, Secret derivation, Solving,
162
+ // and the calculation of the Next state for ALL pools.
163
+ const { proof, nextPools } = await solveAndReplenish({
164
+ targetPoolId: pool.id,
165
+ prevPools: prevData.challengePools,
166
+ masterSecret,
167
+ challengeIds: idsToSolve,
168
+ claim,
169
+ requiredChallengeIds
170
+ });
171
+
172
+ // 4. Construct New Data
173
+ const newData: KeystoneData_V1 = {
174
+ challengePools: nextPools,
175
+ proofs: [proof],
176
+ frameDetails,
177
+ // Revocation info is undefined for a standard sign operation
178
+ };
179
+
180
+ // 5. Commit (Evolve, Persist, Register)
181
+ const resKeystone = await evolvePersistAndRegisterKeystone({
182
+ prevIbGib: latestKeystone,
183
+ newData,
184
+ metaspace,
185
+ space
186
+ });
187
+
188
+ return resKeystone;
189
+ } catch (error) {
190
+ console.error(`${lc} ${extractErrorMsg(error)}`);
191
+ throw error;
192
+ } finally {
193
+ if (logalot) { console.log(`${lc} complete.`); }
194
+ }
195
+ }
196
+
197
+ /**
198
+ * Validates a keystone.
199
+ *
200
+ * ## NOTES
201
+ *
202
+ * Atow (12/22/2025) this only validates the transition from Prev -> Curr.
203
+ *
204
+ * @returns Array of validation error strings. Empty array means Valid.
205
+ *
206
+ * @see {@link validateKeystoneTransition}
207
+ */
208
+ async validate({
209
+ currentIbGib,
210
+ prevIbGib,
211
+ }: {
212
+ currentIbGib: KeystoneIbGib_V1;
213
+ prevIbGib: KeystoneIbGib_V1;
214
+ }): Promise<string[]> {
215
+ // todo: change this to validate the entire keystone graph. the next
216
+ // step is to walk the history and validate each transition.
217
+ const errors = await validateKeystoneTransition({ currentIbGib, prevIbGib });
218
+ return errors;
219
+ }
220
+
221
+ /**
222
+ * Permanently revokes the Identity.
223
+ *
224
+ * Logic:
225
+ * 1. Locates the 'revoke' pool.
226
+ * 2. Solves required challenges to prove ownership.
227
+ * 3. Wipes the pool (via 'scorched-earth' strategy in solveAndReplenish).
228
+ * 4. Sets the revocationInfo on the new frame.
229
+ */
230
+ async revoke({
231
+ latestKeystone,
232
+ masterSecret,
233
+ reason = "User initiated revocation",
234
+ frameDetails,
235
+ metaspace,
236
+ space,
237
+ }: {
238
+ latestKeystone: KeystoneIbGib_V1;
239
+ masterSecret: string;
240
+ reason?: string;
241
+ frameDetails?: any;
242
+ metaspace: MetaspaceService;
243
+ space: IbGibSpaceAny;
244
+ }): Promise<KeystoneIbGib_V1> {
245
+ const lc = `${this.lc}[${this.revoke.name}]`;
246
+ try {
247
+ if (logalot) { console.log(`${lc} starting...`); }
248
+
249
+ const prevData = latestKeystone.data!;
250
+
251
+ // 1. Identify Authority (Resolve Revoke Pool)
252
+ const pool = resolveTargetPool({
253
+ pools: prevData.challengePools,
254
+ poolId: POOL_ID_REVOKE // Explicitly require the special revoke pool
255
+ });
256
+
257
+ // 2. Construct Claim
258
+ const claim: Partial<KeystoneClaim> = {
259
+ verb: KEYSTONE_VERB_REVOKE,
260
+ target: getIbGibAddr({ ibGib: latestKeystone })
261
+ };
262
+
263
+ // 3. Calculate Costs
264
+ const idsToSolve = selectChallengeIds({
265
+ pool,
266
+ targetAddr: claim.target,
267
+ requiredChallengeIds: []
268
+ });
269
+
270
+ if (idsToSolve.length === 0) { throw new Error(`Revocation policy selected 0 challenges? Check config for pool ${pool.id}. Revocation requires proof. (E: 97e5a8356d241ae7b882db791cb1f825)`); }
271
+
272
+ // 4. Pay the Cost & Scorched Earth
273
+ // The revoke pool config should have 'replenish: scorched-earth',
274
+ // causing solveAndReplenish to return an empty pool in nextPools.
275
+ const { proof, nextPools } = await solveAndReplenish({
276
+ targetPoolId: pool.id,
277
+ prevPools: prevData.challengePools,
278
+ masterSecret,
279
+ challengeIds: idsToSolve,
280
+ claim,
281
+ requiredChallengeIds: []
282
+ });
283
+ // warn if nextPools contains pool.id that isn't empty (we were
284
+ // supposed to do "scorched earth" which empties the pool)
285
+ if (nextPools.find(p => p.id === pool.id && Object.keys(p.challenges).length > 0)) {
286
+ console.warn(`${lc} revocation pool ${pool.id} is not empty after revocation. Is the revocation pool replenish strategy set to ${KeystoneReplenishStrategy.scorchedEarth}? (W: 300c28bc8b98fc3e3c0b0d988344f825)`);
287
+ }
288
+
289
+ // 5. Construct Revocation Info
290
+ const revocationInfo: KeystoneRevocationInfo = { reason, proof };
291
+
292
+ // 6. Construct New Data
293
+ const newData: KeystoneData_V1 = {
294
+ challengePools: nextPools,
295
+ proofs: [proof],
296
+ revocationInfo,
297
+ frameDetails
298
+ };
299
+
300
+ // 7. Commit
301
+ const newKeystone = await evolvePersistAndRegisterKeystone({
302
+ prevIbGib: latestKeystone,
303
+ newData,
304
+ metaspace,
305
+ space
306
+ });
307
+
308
+ return newKeystone;
309
+ } catch (error) {
310
+ console.error(`${lc} ${extractErrorMsg(error)}`);
311
+ throw error;
312
+ } finally {
313
+ if (logalot) { console.log(`${lc} complete.`); }
314
+ }
315
+ }
316
+
317
+ /**
318
+ * Structural evolution: Adds new challenge pools to the keystone.
319
+ *
320
+ * Use Case: Adding a delegate (Server) for SSO, adding a recovery key,
321
+ * or rotating to a new set of pools.
322
+ *
323
+ * Requires the Master Secret to authorize the change via a pool containing
324
+ * the 'manage' verb.
325
+ */
326
+ async addPools({
327
+ latestKeystone,
328
+ masterSecret,
329
+ newPools,
330
+ metaspace,
331
+ space,
332
+ }: {
333
+ latestKeystone: KeystoneIbGib_V1;
334
+ /**
335
+ * Alice's Master Secret.
336
+ * Required to solve challenges from the Admin/Manage pool to authorize this change.
337
+ */
338
+ masterSecret: string;
339
+ /**
340
+ * The pools to add.
341
+ * NOTE: These are fully constructed Pool objects.
342
+ * If they are foreign (Bob's), Alice must have constructed them
343
+ * using Bob's challenges + Her config restrictions + isForeign=true.
344
+ */
345
+ newPools: KeystoneChallengePool[];
346
+ metaspace: MetaspaceService;
347
+ space: IbGibSpaceAny;
348
+ }): Promise<KeystoneIbGib_V1> {
349
+ const lc = `${this.lc}[${this.addPools.name}]`;
350
+ try {
351
+ if (logalot) { console.log(`${lc} starting...`); }
352
+
353
+ if (!latestKeystone.data) { throw new Error(`(UNEXPECTED) latestKeystone.data falsy? (E: 7334c8faed128166a999d428c7805b25)`); }
354
+ const prevData = latestKeystone.data;
355
+
356
+ if (prevData.revocationInfo) { throw new Error(`Keystone has been revoked. Cannot add pools. (E: 8599f8f51c78d722252ddb2894fdbe25)`); }
357
+
358
+ if (newPools.length === 0) { throw new Error(`No new pools provided to add. (E: 6599f8f51c78d722252ddb2894fdbe25)`); }
359
+
360
+ // 1. Identify Authority (Resolve Admin Pool)
361
+ // We need a pool that allows the 'manage' verb.
362
+ const adminPool = resolveTargetPool({
363
+ pools: prevData.challengePools,
364
+ verb: KEYSTONE_VERB_MANAGE,
365
+ });
366
+
367
+ if (logalot) { console.log(`${lc} Authorized via pool: ${adminPool.id}`); }
368
+
369
+ // 2. Construct the Management Claim
370
+ const target = getIbGibAddr({ ibGib: latestKeystone });
371
+ const claim: Partial<KeystoneClaim> = {
372
+ verb: KEYSTONE_VERB_MANAGE,
373
+ target, // I am managing myself
374
+ // Scope creates a cryptographic commitment to WHICH pools are being added
375
+ scope: JSON.stringify({ add: newPools.map(p => p.id) })
376
+ };
377
+
378
+ // 3. Calculate Costs
379
+ const idsToSolve = selectChallengeIds({
380
+ pool: adminPool,
381
+ targetAddr: target
382
+ });
383
+
384
+ // 4. Pay the Cost (Solve & Replenish)
385
+ // This authorizes the change by evolving the admin pool.
386
+ const { proof, nextPools: replenishedExistingPools } = await solveAndReplenish({
387
+ targetPoolId: adminPool.id,
388
+ prevPools: prevData.challengePools,
389
+ masterSecret,
390
+ challengeIds: idsToSolve,
391
+ claim,
392
+ });
393
+
394
+ // 5. Mutate Structure (Add the new pools)
395
+ // We verify ID uniqueness cheaply here to prevent blatant errors
396
+ const existingIds = new Set(replenishedExistingPools.map(p => p.id));
397
+ for (const newPool of newPools) {
398
+ if (existingIds.has(newPool.id)) {
399
+ throw new Error(`Cannot add pool. ID collision: ${newPool.id} (E: 8a4c2b1d3e5f6a7b8c9d0e1f2a3b4c5d)`);
400
+ }
401
+ }
402
+
403
+ const finalPools = [...replenishedExistingPools, ...newPools];
404
+
405
+ // 6. Construct New Data
406
+ const newData: KeystoneData_V1 = {
407
+ challengePools: finalPools,
408
+ proofs: [proof], // The proof authorizes the structure change
409
+ };
410
+
411
+ // 7. Commit
412
+ const newKeystone = await evolvePersistAndRegisterKeystone({
413
+ prevIbGib: latestKeystone,
414
+ newData,
415
+ metaspace,
416
+ space
417
+ });
418
+
419
+ return newKeystone;
420
+ } catch (error) {
421
+ console.error(`${lc} ${extractErrorMsg(error)}`);
422
+ throw error;
423
+ } finally {
424
+ if (logalot) { console.log(`${lc} complete.`); }
425
+ }
426
+ }
427
+ }