@ibgib/core-gib 0.1.4 → 0.1.8

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 (156) hide show
  1. package/README.md +26 -0
  2. package/dist/assumptions.respec.mjs.map +1 -1
  3. package/dist/common/alias/alias-helper.mjs +1 -1
  4. package/dist/common/alias/alias-helper.mjs.map +1 -1
  5. package/dist/common/comment/comment-helper.mjs.map +1 -1
  6. package/dist/common/display/display-helper.mjs.map +1 -1
  7. package/dist/common/display/display-types.d.mts +1 -1
  8. package/dist/common/display/display-types.d.mts.map +1 -1
  9. package/dist/common/encrypt/encrypt-helper.mjs.map +1 -1
  10. package/dist/common/encrypt/encrypt-types.d.mts +1 -1
  11. package/dist/common/encrypt/encrypt-types.d.mts.map +1 -1
  12. package/dist/common/error/error-helper.mjs.map +1 -1
  13. package/dist/common/form/form-helper.mjs.map +1 -1
  14. package/dist/common/form/form-items.d.mts.map +1 -1
  15. package/dist/common/import-export/import-export-helper.web.mjs.map +1 -1
  16. package/dist/common/link/link-helper.mjs.map +1 -1
  17. package/dist/common/meta-stone/meta-stone-helper.mjs.map +1 -1
  18. package/dist/common/meta-stone/meta-stone.respec.mjs.map +1 -1
  19. package/dist/common/other/graph-helper.mjs +3 -3
  20. package/dist/common/other/graph-helper.mjs.map +1 -1
  21. package/dist/common/other/ibgib-helper.mjs.map +1 -1
  22. package/dist/common/other/ibgib-helper.respec.mjs.map +1 -1
  23. package/dist/common/other/other-constants.mjs +4 -4
  24. package/dist/common/other/other-constants.mjs.map +1 -1
  25. package/dist/common/other/other-helper.web.mjs.map +1 -1
  26. package/dist/common/other/other-types.d.mts +10 -10
  27. package/dist/common/other/other-types.d.mts.map +1 -1
  28. package/dist/common/other/svg-helper.mjs.map +1 -1
  29. package/dist/common/pic/pic-helper.mjs.map +1 -1
  30. package/dist/common/pubsub/observable/observable-base-v1.mjs +1 -1
  31. package/dist/common/pubsub/observable/observable-base-v1.mjs.map +1 -1
  32. package/dist/common/pubsub/observable/observable-event/observable-event-helper.mjs.map +1 -1
  33. package/dist/common/pubsub/observable/observable-helper.mjs.map +1 -1
  34. package/dist/common/pubsub/observable/observable-types.d.mts +1 -1
  35. package/dist/common/pubsub/observable/observable-types.d.mts.map +1 -1
  36. package/dist/common/pubsub/observer/observer-helper.mjs.map +1 -1
  37. package/dist/common/pubsub/observer/observer-types.d.mts.map +1 -1
  38. package/dist/common/pubsub/subject/subject-helper.mjs.map +1 -1
  39. package/dist/common/pubsub/subject/subject-types.d.mts +1 -1
  40. package/dist/common/pubsub/subject/subject-types.d.mts.map +1 -1
  41. package/dist/common/pubsub/subject/subject-v1.mjs.map +1 -1
  42. package/dist/common/pubsub/subject/subject.respec.mjs.map +1 -1
  43. package/dist/common/pubsub/subscription/subscription-helper.mjs.map +1 -1
  44. package/dist/common/pubsub/subscription/subscription-types.d.mts.map +1 -1
  45. package/dist/common/pubsub/subscription/subscription-v1.mjs.map +1 -1
  46. package/dist/common/secret/secret-helper.mjs.map +1 -1
  47. package/dist/common/secret/secret-types.d.mts +1 -1
  48. package/dist/common/secret/secret-types.d.mts.map +1 -1
  49. package/dist/common/secret/secret.respec.mjs +1 -1
  50. package/dist/common/secret/secret.respec.mjs.map +1 -1
  51. package/dist/common/tag/tag-helper.mjs.map +1 -1
  52. package/dist/core-helper.respec.mjs.map +1 -1
  53. package/dist/keystone/keystone-config-builder.d.mts +77 -0
  54. package/dist/keystone/keystone-config-builder.d.mts.map +1 -0
  55. package/dist/keystone/keystone-config-builder.mjs +157 -0
  56. package/dist/keystone/keystone-config-builder.mjs.map +1 -0
  57. package/dist/keystone/keystone-constants.d.mts +36 -0
  58. package/dist/keystone/keystone-constants.d.mts.map +1 -0
  59. package/dist/keystone/keystone-constants.mjs +39 -0
  60. package/dist/keystone/keystone-constants.mjs.map +1 -0
  61. package/dist/keystone/keystone-helpers.d.mts +117 -0
  62. package/dist/keystone/keystone-helpers.d.mts.map +1 -0
  63. package/dist/keystone/keystone-helpers.mjs +455 -0
  64. package/dist/keystone/keystone-helpers.mjs.map +1 -0
  65. package/dist/keystone/keystone-service-v1.d.mts +77 -0
  66. package/dist/keystone/keystone-service-v1.d.mts.map +1 -0
  67. package/dist/keystone/keystone-service-v1.mjs +502 -0
  68. package/dist/keystone/keystone-service-v1.mjs.map +1 -0
  69. package/dist/keystone/keystone-service-v1.respec.d.mts +2 -0
  70. package/dist/keystone/keystone-service-v1.respec.d.mts.map +1 -0
  71. package/dist/keystone/keystone-service-v1.respec.mjs +460 -0
  72. package/dist/keystone/keystone-service-v1.respec.mjs.map +1 -0
  73. package/dist/keystone/keystone-types.d.mts +248 -0
  74. package/dist/keystone/keystone-types.d.mts.map +1 -0
  75. package/dist/keystone/keystone-types.mjs +50 -0
  76. package/dist/keystone/keystone-types.mjs.map +1 -0
  77. package/dist/keystone/strategy/hash-reveal-v1/hash-reveal-v1.d.mts +35 -0
  78. package/dist/keystone/strategy/hash-reveal-v1/hash-reveal-v1.d.mts.map +1 -0
  79. package/dist/keystone/strategy/hash-reveal-v1/hash-reveal-v1.mjs +107 -0
  80. package/dist/keystone/strategy/hash-reveal-v1/hash-reveal-v1.mjs.map +1 -0
  81. package/dist/keystone/strategy/keystone-strategy-factory.d.mts +15 -0
  82. package/dist/keystone/strategy/keystone-strategy-factory.d.mts.map +1 -0
  83. package/dist/keystone/strategy/keystone-strategy-factory.mjs +26 -0
  84. package/dist/keystone/strategy/keystone-strategy-factory.mjs.map +1 -0
  85. package/dist/keystone/strategy/keystone-strategy.d.mts +48 -0
  86. package/dist/keystone/strategy/keystone-strategy.d.mts.map +1 -0
  87. package/dist/keystone/strategy/keystone-strategy.mjs +14 -0
  88. package/dist/keystone/strategy/keystone-strategy.mjs.map +1 -0
  89. package/dist/respec-gib.node.mjs +3 -1
  90. package/dist/respec-gib.node.mjs.map +1 -1
  91. package/dist/spec-helper.node.respec.d.mts.map +1 -1
  92. package/dist/spec-helper.node.respec.mjs +4 -6
  93. package/dist/spec-helper.node.respec.mjs.map +1 -1
  94. package/dist/timeline/timeline-api.mjs +12 -12
  95. package/dist/timeline/timeline-api.mjs.map +1 -1
  96. package/dist/witness/anonymous-fn/anonymous-fn-helper.mjs.map +1 -1
  97. package/dist/witness/anonymous-fn/anonymous-fn-v1.mjs.map +1 -1
  98. package/dist/witness/app/app-base-v1.mjs.map +1 -1
  99. package/dist/witness/app/app-helper.mjs.map +1 -1
  100. package/dist/witness/app/app-types.d.mts.map +1 -1
  101. package/dist/witness/factory/dynamic-form-factory-base.mjs.map +1 -1
  102. package/dist/witness/light-witness-base-v1.mjs.map +1 -1
  103. package/dist/witness/robbot/robbot-base-v1.mjs +1 -1
  104. package/dist/witness/robbot/robbot-base-v1.mjs.map +1 -1
  105. package/dist/witness/robbot/robbot-helper.mjs.map +1 -1
  106. package/dist/witness/robbot/robbot-types.d.mts +20 -20
  107. package/dist/witness/robbot/robbot-types.d.mts.map +1 -1
  108. package/dist/witness/space/bootstrap/bootstrap-helper.mjs.map +1 -1
  109. package/dist/witness/space/filesystem-space/filesystem-space-v1.mjs.map +1 -1
  110. package/dist/witness/space/filesystem-space/filesystem-space-v1.respec.mjs.map +1 -1
  111. package/dist/witness/space/filesystem-space/node-filesystem-space/node-filesystem-space-helper.mjs.map +1 -1
  112. package/dist/witness/space/filesystem-space/node-filesystem-space/node-filesystem-space-types.d.mts.map +1 -1
  113. package/dist/witness/space/filesystem-space/node-filesystem-space/node-filesystem-space-v1.mjs +1 -1
  114. package/dist/witness/space/filesystem-space/node-filesystem-space/node-filesystem-space-v1.mjs.map +1 -1
  115. package/dist/witness/space/filesystem-space/node-filesystem-space/respec/testSpace_createAndInit.node-filesystem-space-v1.respec.mjs.map +1 -1
  116. package/dist/witness/space/filesystem-space/node-filesystem-space/respec/testSpace_persistTransformResult.node-filesystem-space-v1.respec.mjs.map +1 -1
  117. package/dist/witness/space/filesystem-space/node-filesystem-space/respec/testSpace_putGetDelete.node-filesystem-space-v1.respec.mjs.map +1 -1
  118. package/dist/witness/space/filesystem-space/node-filesystem-space/respec/testSpace_registerNewIbGib_GetLatest.node-filesystem-space-v1.respec.mjs.map +1 -1
  119. package/dist/witness/space/inner-space/inner-space-v1.mjs.map +1 -1
  120. package/dist/witness/space/inner-space/inner-space-v1.respec.mjs.map +1 -1
  121. package/dist/witness/space/metaspace/metaspace-base.mjs +1 -1
  122. package/dist/witness/space/metaspace/metaspace-base.mjs.map +1 -1
  123. package/dist/witness/space/metaspace/metaspace-innerspace/metaspace-innerspace-helper.mjs.map +1 -1
  124. package/dist/witness/space/metaspace/metaspace-innerspace/metaspace-innerspace.mjs.map +1 -1
  125. package/dist/witness/space/outer-space/outer-space-helper.mjs.map +1 -1
  126. package/dist/witness/space/outer-space/outer-space-types.d.mts.map +1 -1
  127. package/dist/witness/space/outer-space/outer-space-types.mjs +1 -1
  128. package/dist/witness/space/outer-space/outer-space-types.mjs.map +1 -1
  129. package/dist/witness/space/reconciliation-space/reconciliation-space-base.mjs.map +1 -1
  130. package/dist/witness/space/reconciliation-space/reconciliation-space-helper.mjs.map +1 -1
  131. package/dist/witness/space/space-base-v1.mjs.map +1 -1
  132. package/dist/witness/space/space-constants.mjs +4 -4
  133. package/dist/witness/space/space-constants.mjs.map +1 -1
  134. package/dist/witness/space/space-helper.mjs +2 -2
  135. package/dist/witness/space/space-helper.mjs.map +1 -1
  136. package/dist/witness/space/space-respec-helper.mjs.map +1 -1
  137. package/dist/witness/space/space-types.d.mts +4 -4
  138. package/dist/witness/space/space-types.d.mts.map +1 -1
  139. package/dist/witness/witness-base-v1.mjs.map +1 -1
  140. package/dist/witness/witness-form-builder.mjs.map +1 -1
  141. package/dist/witness/witness-helper.mjs.map +1 -1
  142. package/dist/witness/witness-with-context/witness-with-context-base-v1.mjs.map +1 -1
  143. package/package.json +6 -5
  144. package/src/keystone/README.md +162 -0
  145. package/src/keystone/keystone-config-builder.mts +187 -0
  146. package/src/keystone/keystone-constants.mts +44 -0
  147. package/src/keystone/keystone-helpers.mts +571 -0
  148. package/src/keystone/keystone-service-v1.mts +611 -0
  149. package/src/keystone/keystone-service-v1.respec.mts +555 -0
  150. package/src/keystone/keystone-types.mts +315 -0
  151. package/src/keystone/strategy/hash-reveal-v1/hash-reveal-v1.mts +146 -0
  152. package/src/keystone/strategy/keystone-strategy-factory.mts +35 -0
  153. package/src/keystone/strategy/keystone-strategy.mts +71 -0
  154. package/src/respec-gib.node.mts +3 -1
  155. package/src/spec-helper.node.respec.mts +4 -6
  156. package/src/witness/robbot/robbot-base-v1.mts +1 -1
@@ -0,0 +1,611 @@
1
+ import { extractErrorMsg, hash, pretty } from '@ibgib/helper-gib/dist/helpers/utils-helper.mjs';
2
+ import { getIbGibAddr } from '@ibgib/ts-gib/dist/helper.mjs';
3
+ import { mut8 } from '@ibgib/ts-gib/dist/V1/transforms/mut8.mjs';
4
+ import { Factory_V1 } from '@ibgib/ts-gib/dist/V1/factory.mjs';
5
+ import { getGib, getGibInfo } from '@ibgib/ts-gib/dist/V1/transforms/transform-helper.mjs';
6
+ import { TransformResult } from '@ibgib/ts-gib/dist/types.mjs';
7
+ import { validateIbGibIntrinsically } from '@ibgib/ts-gib/dist/V1/validate-helper.mjs';
8
+
9
+ import { GLOBAL_LOG_A_LOT } from '../core-constants.mjs';
10
+ import {
11
+ KeystoneData_V1,
12
+ KeystoneIbGib_V1,
13
+ KeystonePoolConfig,
14
+ KeystoneClaim,
15
+ KeystoneProof,
16
+ KeystoneChallengePool,
17
+ KeystoneSolution,
18
+ KeystoneReplenishStrategy,
19
+ KeystoneRevocationInfo,
20
+ KEYSTONE_REPLENISH_STRATEGY_VALID_VALUES,
21
+ } from './keystone-types.mjs';
22
+ import { KeystoneStrategyFactory } from './strategy/keystone-strategy-factory.mjs';
23
+ import { KEYSTONE_ATOM, POOL_ID_REVOKE, KEYSTONE_VERB_REVOKE } from './keystone-constants.mjs';
24
+ import {
25
+ addToBindingMap, getDeterministicRequirements, getKeystoneIb,
26
+ removeFromBindingMap,
27
+ validateKeystoneTransition,
28
+ verifyProofAgainstPool,
29
+ } from './keystone-helpers.mjs';
30
+ import { getDependencyGraph } from '../common/other/graph-helper.mjs';
31
+ import { IbGibSpaceAny } from '../witness/space/space-base-v1.mjs';
32
+ import { MetaspaceService } from '../witness/space/metaspace/metaspace-types.mjs';
33
+ import { IbGibSpaceOptionsCmd } from '../witness/space/space-types.mjs';
34
+
35
+ const logalot = GLOBAL_LOG_A_LOT || true;
36
+
37
+ /**
38
+ * Facade for managing Keystone Identities.
39
+ *
40
+ * Handles Genesis, Authorized Evolution (Signing), and Validation.
41
+ */
42
+ export class KeystoneService_V1 {
43
+ protected lc: string = `[${KeystoneService_V1.name}]`;
44
+
45
+ /**
46
+ * Creates a brand new Keystone Identity Timeline.
47
+ */
48
+ async genesis({
49
+ masterSecret,
50
+ configs,
51
+ metaspace,
52
+ space,
53
+ }: {
54
+ masterSecret: string;
55
+ configs: KeystonePoolConfig[];
56
+ metaspace: MetaspaceService;
57
+ space: IbGibSpaceAny;
58
+ }): Promise<KeystoneIbGib_V1> {
59
+ const lc = `${this.lc}[${this.genesis.name}]`;
60
+ try {
61
+ if (logalot) { console.log(`${lc} starting... (I: c98ae8adbc5888dbf84c5aced7610b25)`); }
62
+
63
+ const challengePools: KeystoneChallengePool[] = [];
64
+
65
+ for (const config of configs) {
66
+ const strategy = KeystoneStrategyFactory.create({ config });
67
+ const poolSecret = await strategy.derivePoolSecret({ masterSecret });
68
+ const challenges: { [id: string]: any } = {};
69
+ const bindingMap: { [char: string]: string[] } = {};
70
+
71
+ const targetSize = config.behavior.size;
72
+ const timestamp = Date.now().toString();
73
+
74
+ for (let i = 0; i < targetSize; i++) {
75
+ const challengeId = await this.generateOpaqueChallengeId({
76
+ salt: config.salt, timestamp, index: i
77
+ });
78
+
79
+ const solution = await strategy.generateSolution({
80
+ poolSecret, poolId: config.salt, challengeId,
81
+ });
82
+ const challenge = await strategy.generateChallenge({ solution });
83
+ challenges[challengeId] = challenge;
84
+
85
+ // Populate Binding Map
86
+ addToBindingMap(bindingMap, challengeId);
87
+ }
88
+
89
+ challengePools.push({
90
+ id: config.salt,
91
+ config,
92
+ challenges,
93
+ bindingMap
94
+ });
95
+ }
96
+
97
+ const data: KeystoneData_V1 = { challengePools, proofs: [] };
98
+ const keystoneIbGib = await this.createKeystoneIbGibImpl({ data, metaspace, space });
99
+ return keystoneIbGib;
100
+ } catch (error) {
101
+ console.error(`${lc} ${extractErrorMsg(error)}`);
102
+ throw error;
103
+ } finally {
104
+ if (logalot) { console.log(`${lc} complete.`); }
105
+ }
106
+ }
107
+
108
+ /**
109
+ * Signs a claim using a hybrid selection strategy.
110
+ * Supports: Mandatory IDs (Alice) + Sequential (FIFO) + Random (Stochastic).
111
+ */
112
+ async sign({
113
+ latestKeystone,
114
+ masterSecret,
115
+ claim,
116
+ poolId,
117
+ requiredChallengeIds = [],
118
+ frameDetails,
119
+ metaspace,
120
+ space,
121
+ }: {
122
+ latestKeystone: KeystoneIbGib_V1;
123
+ masterSecret: string;
124
+ claim: Partial<KeystoneClaim>;
125
+ poolId?: string;
126
+ requiredChallengeIds?: string[];
127
+ frameDetails?: any;
128
+ metaspace: MetaspaceService;
129
+ space: IbGibSpaceAny;
130
+ }): Promise<KeystoneIbGib_V1> {
131
+ const lc = `${this.lc}[${this.sign.name}]`;
132
+ try {
133
+ if (logalot) { console.log(`${lc} starting... (I: 519e0810cce8647ce83bdb3b5019a825)`); }
134
+
135
+ const prevData = latestKeystone.data!;
136
+ if (prevData.revocationInfo) { throw new Error(`keystone has been revoked (latestKeystone.data.revocationInfo is truthy). (E: 4f2198c39116d15c48ba191940316825)`); }
137
+
138
+ let pool: KeystoneChallengePool | undefined;
139
+ const poolsToSearch = prevData.challengePools;
140
+ if (logalot) { console.log(`${lc} Searching pools: ${poolsToSearch.map(p => p.id).join(', ')} for target: ${poolId || 'auto'}`); }
141
+
142
+ // ---------------------------------------------------------------
143
+ // 0. POOL RESOLUTION (Deterministic Mapping)
144
+ // ---------------------------------------------------------------
145
+ if (poolId) {
146
+ // Explicit selection
147
+ pool = poolsToSearch.find(p => p.id === poolId);
148
+ if (!pool) { throw new Error(`Pool not found: ${poolId} (E: genuuid)`); }
149
+
150
+ // Enforce Policy on Explicit Selection
151
+ // FIX: Check length > 0. If empty, it's a default pool (allow all).
152
+ if (pool.config.allowedVerbs && pool.config.allowedVerbs.length > 0 && claim.verb) {
153
+ if (!pool.config.allowedVerbs.includes(claim.verb)) {
154
+ throw new Error(`Pool ${poolId} is not authorized for verb: ${claim.verb} (E: genuuid)`);
155
+ }
156
+ }
157
+ } else {
158
+ // Automatic Resolution based on Verb
159
+ if (!claim.verb) { throw new Error(`Cannot auto-resolve pool without a verb in the claim. (E: genuuid)`); }
160
+
161
+ // 1. Look for Specific Match
162
+ pool = poolsToSearch.find(p =>
163
+ p.config.allowedVerbs && p.config.allowedVerbs.includes(claim.verb!)
164
+ );
165
+
166
+ // 2. Look for General/Default (No restrictions)
167
+ if (!pool) {
168
+ pool = poolsToSearch.find(p =>
169
+ !p.config.allowedVerbs || p.config.allowedVerbs.length === 0
170
+ );
171
+ }
172
+
173
+ if (!pool) { throw new Error(`No suitable pool found for verb: ${claim.verb} (E: genuuid)`); }
174
+ }
175
+
176
+
177
+ // 1. Get Deterministic Requirements (Demands -> Binding -> FIFO)
178
+ const { mandatoryIds, availableIds } = getDeterministicRequirements({
179
+ pool,
180
+ requiredChallengeIds,
181
+ targetAddr: claim.target
182
+ });
183
+
184
+ // 2. Stochastic Selection
185
+ const randomCount = pool.config.behavior.selectRandomly;
186
+ const randomIds: string[] = [];
187
+
188
+ if (logalot) {
189
+ console.log(`${lc} Pool Info: id=${pool.id} invalid=${!pool} size=${Object.keys(pool.challenges || {}).length}`);
190
+ console.log(`${lc} Behavior: seq=${pool.config.behavior.selectSequentially} rand=${randomCount} binding=${pool.config.behavior.targetBindingChars}`);
191
+ console.log(`${lc} Requirements: mandatory=${mandatoryIds.size} available=${availableIds.length}`);
192
+ }
193
+
194
+ if (randomCount > 0) {
195
+ if (availableIds.length < randomCount) {
196
+ throw new Error(`Insufficient challenges for random requirement. Need ${randomCount}, have ${availableIds.length}`);
197
+ }
198
+ // Shuffle & Pick
199
+ const shuffled = availableIds.sort(() => 0.5 - Math.random());
200
+ randomIds.push(...shuffled.slice(0, randomCount));
201
+ }
202
+
203
+ // 3. Combine & Solve
204
+ const finalSelectedIds = [...mandatoryIds, ...randomIds];
205
+
206
+ const strategy = KeystoneStrategyFactory.create({ config: pool.config });
207
+ const secretToUse = masterSecret;
208
+ const poolSecret = await strategy.derivePoolSecret({ masterSecret: secretToUse });
209
+ const solutions: KeystoneSolution[] = [];
210
+
211
+ for (const id of finalSelectedIds) {
212
+ const solution = await strategy.generateSolution({
213
+ poolSecret, poolId: pool.id, challengeId: id,
214
+ });
215
+ solutions.push(solution);
216
+ }
217
+
218
+ // 4. Replenish
219
+ const nextPools = await this.applyReplenishmentStrategy({
220
+ prevPools: poolsToSearch,
221
+ targetPoolId: pool.id,
222
+ consumedIds: finalSelectedIds,
223
+ masterSecret: secretToUse,
224
+ strategy,
225
+ config: pool.config
226
+ });
227
+
228
+ // 5. Build Proof (Include requiredChallengeIds!)
229
+ const proof: KeystoneProof = {
230
+ claim,
231
+ solutions,
232
+ requiredChallengeIds: requiredChallengeIds.length > 0 ? requiredChallengeIds : undefined
233
+ };
234
+
235
+ const newData: KeystoneData_V1 = {
236
+ challengePools: nextPools,
237
+ proofs: [proof],
238
+ frameDetails,
239
+ };
240
+
241
+ const newIbGib = await this.evolveKeystoneIbGibImpl({ prevIbGib: latestKeystone, newData, metaspace, space });
242
+ return newIbGib;
243
+
244
+ } catch (error) {
245
+ console.error(`${lc} ${extractErrorMsg(error)}`);
246
+ throw error;
247
+ } finally {
248
+ if (logalot) { console.log(`${lc} complete.`); }
249
+ }
250
+ }
251
+
252
+ /**
253
+ * Validates a keystone.
254
+ *
255
+ * ## NOTES
256
+ *
257
+ * Atow (12/22/2025) this only validates the transition from Prev -> Curr.
258
+ *
259
+ * @returns Array of validation error strings. Empty array means Valid.
260
+ *
261
+ * @see {@link validateKeystoneTransition}
262
+ */
263
+ async validate({
264
+ currentIbGib,
265
+ prevIbGib,
266
+ }: {
267
+ currentIbGib: KeystoneIbGib_V1;
268
+ prevIbGib: KeystoneIbGib_V1;
269
+ }): Promise<string[]> {
270
+ // todo: change this to validate the entire keystone graph. the next
271
+ // step is to walk the history and validate each transition.
272
+ const errors = await validateKeystoneTransition({ currentIbGib, prevIbGib });
273
+ return errors;
274
+ }
275
+
276
+ /**
277
+ * Permanently revokes the Identity.
278
+ *
279
+ * Logic:
280
+ * 1. Locates the 'revoke' pool.
281
+ * 2. Consumes ALL available challenges in that pool (Scorched Earth).
282
+ * 3. Sets the revocationInfo on the new frame.
283
+ */
284
+ async revoke({
285
+ latestKeystone,
286
+ masterSecret,
287
+ reason = "User initiated revocation",
288
+ frameDetails,
289
+ metaspace,
290
+ space,
291
+ }: {
292
+ latestKeystone: KeystoneIbGib_V1;
293
+ masterSecret: string;
294
+ reason?: string;
295
+ frameDetails?: any;
296
+ metaspace: MetaspaceService;
297
+ space?: IbGibSpaceAny;
298
+ }): Promise<KeystoneIbGib_V1> {
299
+ const lc = `${this.lc}[${this.revoke.name}]`;
300
+ try {
301
+ const prevData = latestKeystone.data!;
302
+ space ??= await metaspace.getLocalUserSpace({ lock: false });
303
+ if (!space) { throw new Error(`(UNEXPECTED) space falsy and couldn't get default local user space from the metaspace? (E: 73c8bfc0e7383a540ea1d6b14b020125)`); }
304
+
305
+ // 1. Find Revocation Pool
306
+ const pool = prevData.challengePools.find(p => p.id === POOL_ID_REVOKE);
307
+ if (!pool) { throw new Error(`Revocation pool not found (E: 8c4f18c5461c1d601283108878c79825)`); }
308
+
309
+ // 2. Select Challenges (Standard Policy, NOT ALL)
310
+ const claim: Partial<KeystoneClaim> = {
311
+ verb: KEYSTONE_VERB_REVOKE,
312
+ target: getIbGibAddr({ ibGib: latestKeystone })
313
+ };
314
+
315
+ const { mandatoryIds, availableIds } = getDeterministicRequirements({
316
+ pool,
317
+ requiredChallengeIds: [], // Revocation usually doesn't have external demands
318
+ targetAddr: claim.target
319
+ });
320
+
321
+ // Stochastic Selection
322
+ const randomCount = pool.config.behavior.selectRandomly;
323
+ const randomIds: string[] = [];
324
+ if (randomCount > 0) {
325
+ if (availableIds.length < randomCount) { throw new Error(`Insufficient challenges. availableIds.length (${availableIds.length}) is less than required random count (${randomCount}) (E: b2e3570ab998dfdbab5fbdda1e43d825)`); }
326
+ const shuffled = availableIds.sort(() => 0.5 - Math.random());
327
+ randomIds.push(...shuffled.slice(0, randomCount));
328
+ }
329
+
330
+ const selectedIds = [...mandatoryIds, ...randomIds];
331
+ if (selectedIds.length === 0) { throw new Error(`Revocation policy selected 0 challenges? Check config for pool. id: ${pool.id}. pool.config: ${pretty(pool.config)} (E: 97e5a8356d241ae7b882db791cb1f825)`); }
332
+
333
+ // 3. Solve Selected
334
+ const strategy = KeystoneStrategyFactory.create({ config: pool.config });
335
+ const poolSecret = await strategy.derivePoolSecret({ masterSecret });
336
+ const solutions: KeystoneSolution[] = [];
337
+
338
+ for (const id of selectedIds) {
339
+ const solution = await strategy.generateSolution({
340
+ poolSecret, poolId: pool.id, challengeId: id,
341
+ });
342
+ solutions.push(solution);
343
+ }
344
+
345
+ // 4. Replenish (Scorched Earth)
346
+ const nextPools = await this.applyReplenishmentStrategy({
347
+ prevPools: prevData.challengePools,
348
+ targetPoolId: pool.id,
349
+ consumedIds: selectedIds,
350
+ masterSecret,
351
+ strategy,
352
+ config: pool.config
353
+ });
354
+
355
+ // 5. Construct Proof
356
+ const proof: KeystoneProof = {
357
+ claim,
358
+ solutions,
359
+ };
360
+
361
+ const revocationInfo: KeystoneRevocationInfo = { reason, proof };
362
+
363
+ const newData: KeystoneData_V1 = {
364
+ challengePools: nextPools,
365
+ proofs: [proof],
366
+ revocationInfo,
367
+ frameDetails
368
+ };
369
+
370
+ return await this.evolveKeystoneIbGibImpl({
371
+ prevIbGib: latestKeystone,
372
+ newData,
373
+ metaspace,
374
+ space,
375
+ });
376
+
377
+ } catch (error) {
378
+ console.error(`${lc} ${extractErrorMsg(error)}`);
379
+ throw error;
380
+ } finally {
381
+ if (logalot) { console.log(`${lc} complete.`); }
382
+ }
383
+ }
384
+
385
+ /**
386
+ * Generates an opaque, collision-resistant ID for a challenge.
387
+ * atow (2025/12/22) - Hash(Salt + Timestamp + Index)
388
+ */
389
+ private async generateOpaqueChallengeId({
390
+ salt,
391
+ timestamp,
392
+ index
393
+ }: {
394
+ salt: string,
395
+ timestamp: string,
396
+ index: number
397
+ }): Promise<string> {
398
+ // Use full SHA-256 hash, or slice it?
399
+ // User suggested substring is fine for pool uniqueness.
400
+ // Let's use first 16 chars of hex (64 bits) for brevity + safety.
401
+ const raw = await hash({ s: `${salt}${timestamp}${index}` });
402
+ return raw.substring(0, 16);
403
+ }
404
+
405
+ private async applyReplenishmentStrategy({
406
+ prevPools,
407
+ targetPoolId,
408
+ consumedIds,
409
+ masterSecret,
410
+ strategy,
411
+ config
412
+ }: {
413
+ prevPools: KeystoneChallengePool[];
414
+ targetPoolId: string;
415
+ consumedIds: string[];
416
+ masterSecret: string;
417
+ strategy: any;
418
+ config: KeystonePoolConfig;
419
+ }): Promise<KeystoneChallengePool[]> {
420
+ const newPools = JSON.parse(JSON.stringify(prevPools));
421
+ if (logalot) { console.log(`[applyReplenishmentStrategy] Cloned pools. Ref check: ${prevPools === newPools} (should be false)`); }
422
+ const targetIdx = newPools.findIndex((p: any) => p.id === targetPoolId);
423
+ if (targetIdx === -1) { throw new Error(`Target pool ${targetPoolId} not found in keytstone data. (E: 75200388d22744838634524233772545)`); }
424
+ const pool = newPools[targetIdx];
425
+
426
+ const poolSecret = await strategy.derivePoolSecret({ masterSecret });
427
+ const timestamp = Date.now().toString();
428
+
429
+ const strategyType = config.behavior.replenish;
430
+
431
+ // Clean up Binding Map for consumed IDs
432
+ consumedIds.forEach(id => {
433
+ if (pool.bindingMap) { removeFromBindingMap(pool.bindingMap, id); }
434
+ });
435
+
436
+ if (strategyType === KeystoneReplenishStrategy.topUp) {
437
+ // Remove consumed
438
+ consumedIds.forEach(id => delete pool.challenges[id]);
439
+
440
+ // Add New
441
+ for (let i = 0; i < consumedIds.length; i++) {
442
+ const newId = await this.generateOpaqueChallengeId({
443
+ salt: config.salt, timestamp, index: i
444
+ });
445
+
446
+ const solution = await strategy.generateSolution({
447
+ poolSecret, poolId: pool.id, challengeId: newId
448
+ });
449
+ pool.challenges[newId] = await strategy.generateChallenge({ solution });
450
+
451
+ // Update Binding Map
452
+ if (!pool.bindingMap) { pool.bindingMap = {}; }
453
+ addToBindingMap(pool.bindingMap, newId);
454
+ }
455
+ } else if (strategyType === KeystoneReplenishStrategy.replaceAll) {
456
+ pool.challenges = {};
457
+ pool.bindingMap = {};
458
+
459
+ for (let i = 0; i < config.behavior.size; i++) {
460
+ const newId = await this.generateOpaqueChallengeId({
461
+ salt: config.salt, timestamp, index: i
462
+ });
463
+ const solution = await strategy.generateSolution({
464
+ poolSecret, poolId: pool.id, challengeId: newId
465
+ });
466
+ pool.challenges[newId] = await strategy.generateChallenge({ solution });
467
+ addToBindingMap(pool.bindingMap, newId);
468
+ }
469
+ } else if (strategyType === KeystoneReplenishStrategy.consume) {
470
+ consumedIds.forEach(id => delete pool.challenges[id]);
471
+ } else if (strategyType === KeystoneReplenishStrategy.scorchedEarth) {
472
+ pool.challenges = {};
473
+ pool.bindingMap = {};
474
+ } else {
475
+ throw new Error(`Unknown replenish strategy: ${strategyType}. Valid list: ${pretty(KEYSTONE_REPLENISH_STRATEGY_VALID_VALUES)} (E: 0acf56f1e1486240080e11e8046d0825)`);
476
+ }
477
+
478
+ return newPools;
479
+ }
480
+
481
+ /**
482
+ * Creates a new keystone ibgib that has no dna and no past.
483
+ */
484
+ private async createKeystoneIbGibImpl({
485
+ data,
486
+ metaspace,
487
+ space,
488
+ }: {
489
+ data: KeystoneData_V1,
490
+ metaspace: MetaspaceService,
491
+ space?: IbGibSpaceAny,
492
+ }): Promise<KeystoneIbGib_V1> {
493
+ const lc = `${this.lc}[${this.createKeystoneIbGibImpl.name}]`;
494
+ try {
495
+ if (logalot) { console.log(`${lc} starting... (I: 5e32389700e9899e788cbefacef7c825)`); }
496
+
497
+ space ??= await metaspace.getLocalUserSpace({ lock: false });
498
+ if (!space) { throw new Error(`(UNEXPECTED) space was falsy and we couldn't get default local user space from metaspace? (E: 9a6498cf16a8801f19ec376749742225)`); }
499
+
500
+ // create the actual keystoneIbGib
501
+ const resFirstGen = await Factory_V1.firstGen({
502
+ parentIbGib: Factory_V1.primitive({ ib: KEYSTONE_ATOM }),
503
+ ib: await getKeystoneIb({ keystoneData: data }),
504
+ data,
505
+ dna: false,
506
+ nCounter: true,
507
+ tjp: {
508
+ timestamp: true,
509
+ uuid: true,
510
+ },
511
+ }) as TransformResult<KeystoneIbGib_V1>;
512
+ const keystoneIbGib = resFirstGen.newIbGib;
513
+
514
+ if (!keystoneIbGib.data) { throw new Error(`(UNEXPECTED) keystoneIbGib.data falsy? We expect the data to be populated with real keystone data. (E: 38a358facdb89d16d81d48c8520d3d25)`); }
515
+ if (!keystoneIbGib.rel8ns) { throw new Error(`(UNEXPECTED) keystoneIbGib.rel8ns falsy? we expect the rel8ns to have ancestor and past. (E: 20cb7723dc33ae1ef808fe76d1bf4b25)`); }
516
+ if (!keystoneIbGib.rel8ns.past || keystoneIbGib.rel8ns.past.length === 0) {
517
+ 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)`);
518
+ }
519
+
520
+ // reset n
521
+ keystoneIbGib.data.n = 0;
522
+ // reset tjp
523
+ keystoneIbGib.data.isTjp = true;
524
+ delete keystoneIbGib.rel8ns.tjp;
525
+ // reset past
526
+ delete keystoneIbGib.rel8ns.past;
527
+
528
+ // recalculate gib
529
+ keystoneIbGib.gib = await getGib({ ibGib: keystoneIbGib });
530
+
531
+ // save and register
532
+ await metaspace.put({ ibGib: keystoneIbGib, space, });
533
+ await metaspace.registerNewIbGib({ ibGib: keystoneIbGib, space, });
534
+
535
+ return keystoneIbGib;
536
+ } catch (error) {
537
+ console.error(`${lc} ${extractErrorMsg(error)}`);
538
+ throw error;
539
+ } finally {
540
+ if (logalot) { console.log(`${lc} complete.`); }
541
+ }
542
+ }
543
+
544
+ private async evolveKeystoneIbGibImpl({
545
+ prevIbGib,
546
+ newData,
547
+ metaspace,
548
+ space,
549
+ }: {
550
+ prevIbGib: KeystoneIbGib_V1,
551
+ newData: KeystoneData_V1
552
+ metaspace: MetaspaceService,
553
+ space: IbGibSpaceAny,
554
+ }): Promise<KeystoneIbGib_V1> {
555
+ const lc = `${this.lc}[${this.evolveKeystoneIbGibImpl.name}]`;
556
+ try {
557
+ if (logalot) { console.log(`${lc} starting... (I: 8b10e8920f08b7842803665834cf8925)`); }
558
+
559
+ if (!prevIbGib.data) { throw new Error(`(UNEXPECTED) prevIbGib.data falsy? (E: 5e84875bf992c585b979e6c8ed5bf225)`); }
560
+ 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)`); }
561
+
562
+ const prevData = prevIbGib.data;
563
+ /**
564
+ * we want to completely replace these keys, so we will remove them
565
+ * from the data. This occurs first in the underlying mut8
566
+ * transform.
567
+ * @see {@link mut8}
568
+ */
569
+ let dataToRemove: Partial<KeystoneData_V1> | undefined = {}
570
+ if (prevData.proofs) { dataToRemove.proofs = []; }
571
+ if (prevData.challengePools) { dataToRemove.challengePools = []; }
572
+ if (prevData.frameDetails) { dataToRemove.frameDetails = {}; }
573
+ if (Object.keys(dataToRemove).length === 0) { dataToRemove = undefined; }
574
+
575
+ const resMut8 = await mut8({
576
+ src: prevIbGib,
577
+ dataToRemove,
578
+ dataToAddOrPatch: newData,
579
+ // dna: false, // explicitly set to false just to show
580
+ nCounter: true,
581
+ });
582
+
583
+ 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)`); }
584
+ if (!!resMut8.dnas) { throw new Error(`(UNEXPECTED) resMut8.dnas truthy? We do not want dnas with keystones. (E: 49470513d018f97d28024f4e82da3b25)`); }
585
+
586
+
587
+ const newKeystoneIbGib = resMut8.newIbGib as KeystoneIbGib_V1;
588
+
589
+ // run validation here
590
+ const errors = await this.validate({
591
+ currentIbGib: newKeystoneIbGib,
592
+ prevIbGib,
593
+ });
594
+ if (errors.length > 0) {
595
+ console.error(`${lc} Validation Failed:\n${errors.join('\n')}`);
596
+ throw new Error(`(UNEXPECTED) invalid keystone after we just evolved it? Errors: ${errors.join('; ')} (E: ae2c58406c1db7687879dfb89fc1f825)`);
597
+ }
598
+
599
+ // save and register
600
+ await metaspace.put({ ibGib: newKeystoneIbGib, space, });
601
+ await metaspace.registerNewIbGib({ ibGib: newKeystoneIbGib, space, });
602
+
603
+ return newKeystoneIbGib;
604
+ } catch (error) {
605
+ console.error(`${lc} ${extractErrorMsg(error)}`);
606
+ throw error;
607
+ } finally {
608
+ if (logalot) { console.log(`${lc} complete.`); }
609
+ }
610
+ }
611
+ }