@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,838 @@
1
+ import { respecfully, iReckon, firstOfAll, respecfullyDear, ifWeMight } from '@ibgib/helper-gib/dist/respec-gib/respec-gib.mjs';
2
+ const maam = `[${import.meta.url}]`, sir = maam;
3
+ import { hash } from '@ibgib/helper-gib/dist/helpers/utils-helper.mjs';
4
+ import { getIbGibAddr } from '@ibgib/ts-gib/dist/helper.mjs';
5
+ import { GLOBAL_LOG_A_LOT } from '../core-constants.mjs';
6
+ import { KeystoneStrategyFactory } from './strategy/keystone-strategy-factory.mjs';
7
+ import { createRevocationPoolConfig, createStandardPoolConfig } from './keystone-config-builder.mjs';
8
+ import { POOL_ID_DEFAULT, POOL_ID_REVOKE, KEYSTONE_VERB_REVOKE, KEYSTONE_VERB_MANAGE } from './keystone-constants.mjs';
9
+ import { KeystoneService_V1 } from './keystone-service-v1.mjs';
10
+ import { addToBindingMap } from './keystone-helpers.mjs';
11
+ const logalot = GLOBAL_LOG_A_LOT;
12
+ // /**
13
+ // * not sure where to put this, but we probably will want to reuse this in the
14
+ // * future (assuming it works)
15
+ // * @returns metaspace service reference
16
+ // */
17
+ // async function getNewInitializedInMemoryMetaspaceForTesting({
18
+ // defaultSpaceName,
19
+ // }: {
20
+ // defaultSpaceName: string,
21
+ // }): Promise<MetaspaceService> {
22
+ // const lc = `[${getNewInitializedInMemoryMetaspaceForTesting.name}]`;
23
+ // try {
24
+ // if (logalot) { console.log(`${lc} starting... (I: 766d7596addcb73f4820586469233b25)`); }
25
+ // let metaspace = new Metaspace_Innerspace(/*cacheSvc*/undefined);
26
+ // if (logalot) { console.log(`${lc} creating metaspace complete. initializing... (I: 61b74d62e8832c9fa853e4b8c4c2d825)`); }
27
+ // getGibInfo()
28
+ // await metaspace.initialize({
29
+ // spaceName: defaultSpaceName,
30
+ // /**
31
+ // * passing in undefined will use the defaults. probably will need to
32
+ // * adjust this for testing purposes, but let's see what happens with
33
+ // * this first.
34
+ // */
35
+ // metaspaceFactory: {
36
+ // fnDtoToSpace: async () => {
37
+ // if (!currentSpace) { currentSpace = new IbGibTestSpace(); }
38
+ // return currentSpace;
39
+ // },
40
+ // fnZeroSpaceFactory: () => {
41
+ // if (!currentZeroSpace) { currentZeroSpace = new IbGibTestSpace(); }
42
+ // return currentZeroSpace;
43
+ // },
44
+ // fnDefaultLocalSpaceFactory: async () => {
45
+ // if (!currentSpace) { currentSpace = new IbGibTestSpace(); }
46
+ // return currentSpace;
47
+ // },
48
+ // // export type DtoToSpaceFunction = (spaceDto: IbGib_V1) => Promise<IbGibSpaceAny>;
49
+ // // export type ZeroSpaceFactoryFunction = () => IbGibSpaceAny;
50
+ // // export type LocalSpaceFactoryFunction = (opts: CreateLocalSpaceOptions) => Promise<IbGibSpaceAny | undefined>;
51
+ // },
52
+ // getFnAlert: () => { return async ({ title, msg }) => console.log(title, msg) },
53
+ // getFnPrompt: () => {
54
+ // return async ({ title, msg }) => {
55
+ // // if this is needed, we might set up some way for testing
56
+ // // to prepare either a queue of prompts or some kind of map or getter
57
+ // // and put it on the metaspace itself
58
+ // throw new Error(`not implemented (E: c7ef688a02f8cb74487260f9274ac825)`);
59
+ // // promptForText({ title, msg, confirm: false });
60
+ // }
61
+ // },
62
+ // getFnPromptPassword: () => {
63
+ // return async () => {
64
+ // // similar to getFnPrompt, if we need a _different_
65
+ // // password, we might set up some way for testing to prepare
66
+ // // either a queue of passwords or some kind of map or getter
67
+ // // and put it on the metaspace itself
68
+ // return 'password';
69
+ // // promptForSecret({ confirm: true })
70
+ // }
71
+ // },
72
+ // });
73
+ // return metaspace;
74
+ // } catch (error) {
75
+ // console.error(`${lc} ${extractErrorMsg(error)}`);
76
+ // throw error;
77
+ // } finally {
78
+ // if (logalot) { console.log(`${lc} complete.`); }
79
+ // }
80
+ // }
81
+ /**
82
+ * A simple in-memory map acting as a Space.
83
+ * Pure Storage. No Indexing logic.
84
+ */
85
+ class MockIbGibSpace {
86
+ name;
87
+ store = new Map();
88
+ constructor(name = "mock_space") {
89
+ this.name = name;
90
+ }
91
+ async put({ ibGib }) {
92
+ const addr = getIbGibAddr({ ibGib });
93
+ this.store.set(addr, JSON.parse(JSON.stringify(ibGib))); // Deep copy
94
+ }
95
+ async get({ addr }) {
96
+ const data = this.store.get(addr);
97
+ return data ? JSON.parse(JSON.stringify(data)) : null;
98
+ }
99
+ async witness(arg) {
100
+ const cmd = arg.data?.cmd;
101
+ if (cmd === 'get') {
102
+ const addrs = arg.data.ibGibAddrs || [];
103
+ const ibGibs = [];
104
+ for (const addr of addrs) {
105
+ const ig = await this.get({ addr });
106
+ if (ig)
107
+ ibGibs.push(ig);
108
+ }
109
+ return { ibGibs };
110
+ }
111
+ return undefined;
112
+ }
113
+ }
114
+ /**
115
+ * A partial mock of Metaspace.
116
+ * Handles:
117
+ * 1. Retrieving the local space.
118
+ * 2. Delegating 'put' to the space.
119
+ * 3. 'registerNewIbGib': Tracking the HEAD of a timeline.
120
+ */
121
+ class MockMetaspaceService {
122
+ space;
123
+ /**
124
+ * Map of TJP Gib (Timeline ID) -> Latest IbGib Addr (Head)
125
+ */
126
+ timelineHeads = new Map();
127
+ constructor(space) {
128
+ this.space = space;
129
+ }
130
+ async getLocalUserSpace({ lock }) {
131
+ return this.space;
132
+ }
133
+ /**
134
+ * Metaspace often acts as a facade for put, defaulting to local space.
135
+ */
136
+ async put(args) {
137
+ const target = args.space || this.space;
138
+ return target.put(args);
139
+ }
140
+ /**
141
+ * Tracks the latest version of an ibGib timeline.
142
+ */
143
+ async registerNewIbGib(args) {
144
+ const { ibGib } = args;
145
+ const targetSpace = args.space || this.space;
146
+ // 1. Ensure it is stored
147
+ await targetSpace.put({ ibGib });
148
+ // 2. Extract TJP (Timeline Identifier)
149
+ // Simplified logic mirroring getGibInfo
150
+ const gib = ibGib.gib || '';
151
+ let tjpGib = gib;
152
+ if (gib.includes('.')) {
153
+ // It's a frame in a timeline: "punctiliarHash.tjpHash"
154
+ // The TJP is the suffix.
155
+ const parts = gib.split('.');
156
+ tjpGib = parts.slice(1).join('.');
157
+ }
158
+ // Else: It's a Primitive or a TJP itself (Genesis).
159
+ // If Genesis (isTjp=true), the gib IS the tjpGib.
160
+ const addr = getIbGibAddr({ ibGib });
161
+ this.timelineHeads.set(tjpGib, addr);
162
+ }
163
+ }
164
+ // ===========================================================================
165
+ // SUITE A: STRATEGY VECTORS (The Math)
166
+ // ===========================================================================
167
+ await respecfully(sir, 'Suite A: Strategy Vectors (HashRevealV1)', async () => {
168
+ // Setup generic variables
169
+ const masterSecret = "TestSecret_12345";
170
+ const salt = "TestPool";
171
+ let config;
172
+ firstOfAll(sir, async () => {
173
+ // Use our standard builder to get a valid config object
174
+ config = createStandardPoolConfig(salt);
175
+ });
176
+ await respecfully(sir, 'Derivation Logic', async () => {
177
+ await ifWeMight(sir, 'derivePoolSecret with same inputs returns same output', async () => {
178
+ const strategy = KeystoneStrategyFactory.create({ config });
179
+ const secretA = await strategy.derivePoolSecret({ masterSecret });
180
+ const secretB = await strategy.derivePoolSecret({ masterSecret });
181
+ iReckon(sir, secretA).asTo('secret consistency').willEqual(secretB);
182
+ iReckon(sir, secretA).asTo('secret length').isGonnaBeTruthy();
183
+ });
184
+ await ifWeMight(sir, 'derivePoolSecret with different master secret returns different output', async () => {
185
+ const strategy = KeystoneStrategyFactory.create({ config });
186
+ const secretA = await strategy.derivePoolSecret({ masterSecret });
187
+ const secretB = await strategy.derivePoolSecret({ masterSecret: masterSecret + "_diff" });
188
+ iReckon(sir, secretA).asTo('secrets differ').not.willEqual(secretB);
189
+ });
190
+ await ifWeMight(sir, 'derivePoolSecret with different salt returns different output', async () => {
191
+ // Modify salt in a copy of config
192
+ const configB = { ...config, salt: "OtherPool" };
193
+ const strategyA = KeystoneStrategyFactory.create({ config });
194
+ const strategyB = KeystoneStrategyFactory.create({ config: configB });
195
+ const secretA = await strategyA.derivePoolSecret({ masterSecret });
196
+ const secretB = await strategyB.derivePoolSecret({ masterSecret });
197
+ iReckon(sir, secretA).asTo('salt affects secret').not.willEqual(secretB);
198
+ });
199
+ });
200
+ await respecfully(sir, 'Challenge/Solution Logic', async () => {
201
+ await ifWeMight(sir, 'generateSolution -> generateChallenge -> validateSolution loop works', async () => {
202
+ const strategy = KeystoneStrategyFactory.create({ config });
203
+ const poolSecret = await strategy.derivePoolSecret({ masterSecret });
204
+ const challengeId = "a3ff7843552870fc28bef2b"; // arbitrary random challengeId
205
+ // 1. Generate Solution
206
+ const solution = await strategy.generateSolution({ poolSecret, poolId: salt, challengeId });
207
+ iReckon(sir, solution.value).asTo('solution value exists').isGonnaBeTruthy();
208
+ iReckon(sir, solution.challengeId).asTo('id matches').willEqual(challengeId);
209
+ // 2. Generate Public Challenge from Solution
210
+ const challenge = await strategy.generateChallenge({ solution });
211
+ iReckon(sir, challenge.hash).asTo('challenge hash exists').isGonnaBeTruthy();
212
+ // 3. Validate
213
+ const isValid = await strategy.validateSolution({ solution, challenge });
214
+ iReckon(sir, isValid).asTo('valid pair should pass').isGonnaBeTrue();
215
+ });
216
+ await ifWeMight(sir, 'validateSolution fails for mismatched values', async () => {
217
+ const strategy = KeystoneStrategyFactory.create({ config });
218
+ const poolSecret = await strategy.derivePoolSecret({ masterSecret });
219
+ const challengeId = "8c994f3ed598f150e25513"; // arbitrary random challengeId
220
+ // Generate real pair
221
+ const solution = await strategy.generateSolution({ poolSecret, poolId: salt, challengeId });
222
+ const challenge = await strategy.generateChallenge({ solution });
223
+ // Tamper with solution value
224
+ const badSolution = { ...solution, value: "hacked_value" };
225
+ const isValid = await strategy.validateSolution({ solution: badSolution, challenge });
226
+ iReckon(sir, isValid).asTo('tampered solution should fail').isGonnaBeFalse();
227
+ });
228
+ await ifWeMight(sir, 'validateSolution fails for mismatched challenge hashes', async () => {
229
+ const strategy = KeystoneStrategyFactory.create({ config });
230
+ const poolSecret = await strategy.derivePoolSecret({ masterSecret });
231
+ // Generate pair A
232
+ const challengeId_A = "416c38cfd6ee63dbf8d4e5ef36"; // arbitrary random challengeId
233
+ const solutionA = await strategy.generateSolution({ poolSecret, poolId: salt, challengeId: challengeId_A });
234
+ // Generate pair B
235
+ const challengeId_B = "c487ef6b7878fae798c3"; // arbitrary random challengeId
236
+ const solutionB = await strategy.generateSolution({ poolSecret, poolId: salt, challengeId: challengeId_B });
237
+ const challengeB = await strategy.generateChallenge({ solution: solutionB });
238
+ // Check A against B
239
+ const isValid = await strategy.validateSolution({ solution: solutionA, challenge: challengeB });
240
+ iReckon(sir, isValid).asTo('mismatched pair should fail').isGonnaBeFalse();
241
+ });
242
+ });
243
+ });
244
+ // ===========================================================================
245
+ // SUITE B: SERVICE LIFECYCLE (Genesis -> Sign -> Validate)
246
+ // ===========================================================================
247
+ await respecfully(sir, 'Suite B: Service Lifecycle', async () => {
248
+ const service = new KeystoneService_V1();
249
+ const masterSecret = "AliceSecretKey_987654321";
250
+ let mockSpace;
251
+ let mockMetaspace;
252
+ let genesisKeystone;
253
+ let signedKeystone;
254
+ firstOfAll(sir, async () => {
255
+ mockSpace = new MockIbGibSpace();
256
+ mockMetaspace = new MockMetaspaceService(mockSpace);
257
+ });
258
+ await respecfully(sir, 'Genesis', async () => {
259
+ await ifWeMight(sir, 'creates a valid genesis frame and persists it', async () => {
260
+ const config = createStandardPoolConfig(POOL_ID_DEFAULT);
261
+ genesisKeystone = await service.genesis({
262
+ masterSecret,
263
+ configs: [config],
264
+ metaspace: mockMetaspace,
265
+ space: mockSpace,
266
+ });
267
+ // Verify Object
268
+ iReckon(sir, genesisKeystone).asTo('genesis object').isGonnaBeTruthy();
269
+ iReckon(sir, genesisKeystone.data?.isTjp).asTo('isTjp').isGonnaBeTrue();
270
+ // Verify Persistence
271
+ const addr = getIbGibAddr({ ibGib: genesisKeystone });
272
+ const saved = await mockSpace.get({ addr });
273
+ iReckon(sir, saved).asTo('persisted to space').isGonnaBeTruthy();
274
+ // Verify Registration (Timeline Tracking)
275
+ // Genesis gib should be registered as a timeline head
276
+ const head = mockMetaspace.timelineHeads.get(genesisKeystone.gib);
277
+ iReckon(sir, head).asTo('genesis registered as timeline head').willEqual(addr);
278
+ });
279
+ });
280
+ await respecfully(sir, 'Signing (Evolution)', async () => {
281
+ await ifWeMight(sir, 'evolves the keystone with a valid proof', async () => {
282
+ const claim = {
283
+ target: "comment 123^gib",
284
+ verb: "post"
285
+ };
286
+ const details = { note: "First post!" };
287
+ signedKeystone = await service.sign({
288
+ latestKeystone: genesisKeystone,
289
+ masterSecret,
290
+ claim,
291
+ poolId: POOL_ID_DEFAULT,
292
+ frameDetails: details,
293
+ metaspace: mockMetaspace,
294
+ space: mockSpace,
295
+ });
296
+ iReckon(sir, signedKeystone).asTo('new frame created').isGonnaBeTruthy();
297
+ iReckon(sir, signedKeystone).asTo('is different frame').not.isGonnaBe(genesisKeystone);
298
+ // NOTE: If this fails, check if 'sign' calls 'space.put' or 'metaspace.put'!
299
+ // In your current 'sign' implementation, you return the object but might have missed the save step.
300
+ const addr = getIbGibAddr({ ibGib: signedKeystone });
301
+ const saved = await mockSpace.get({ addr });
302
+ iReckon(sir, saved).asTo('persisted to space').isGonnaBeTruthy();
303
+ });
304
+ });
305
+ await respecfully(sir, 'Validation', async () => {
306
+ await ifWeMight(sir, 'validates the genesis->signed transition', async () => {
307
+ const errors = await service.validate({
308
+ prevIbGib: genesisKeystone,
309
+ currentIbGib: signedKeystone,
310
+ // metaspace: mockMetaspace,
311
+ // space: mockSpace as any,
312
+ });
313
+ iReckon(sir, errors.length).asTo('signature validation has no errors').willEqual(0);
314
+ });
315
+ });
316
+ });
317
+ // ===========================================================================
318
+ // SUITE C: SECURITY & SAD PATHS
319
+ // ===========================================================================
320
+ await respecfully(sir, 'Suite C: Security Vectors', async () => {
321
+ const service = new KeystoneService_V1();
322
+ const aliceSecret = "AliceSecret_111";
323
+ const eveSecret = "EveSecret_666";
324
+ let mockSpace;
325
+ let mockMetaspace;
326
+ let genesisKeystone;
327
+ firstOfAll(sir, async () => {
328
+ mockSpace = new MockIbGibSpace();
329
+ mockMetaspace = new MockMetaspaceService(mockSpace);
330
+ // Setup Alice's Identity
331
+ const config = createStandardPoolConfig(POOL_ID_DEFAULT);
332
+ config.behavior.size = 10;
333
+ genesisKeystone = await service.genesis({
334
+ masterSecret: aliceSecret,
335
+ configs: [config],
336
+ metaspace: mockMetaspace,
337
+ space: mockSpace,
338
+ });
339
+ });
340
+ await respecfully(sir, 'Wrong Secret (Forgery)', async () => {
341
+ await ifWeMight(sir, 'prevents creation of forged frames', async () => {
342
+ const claim = { target: "comment 123^gib", verb: "post" };
343
+ let errorCaught = false;
344
+ let errorMsg = "";
345
+ try {
346
+ // Eve tries to sign Alice's keystone.
347
+ // This MUST fail because sign() calls evolve(), which calls validate().
348
+ await service.sign({
349
+ latestKeystone: genesisKeystone,
350
+ masterSecret: eveSecret,
351
+ claim,
352
+ poolId: POOL_ID_DEFAULT,
353
+ metaspace: mockMetaspace,
354
+ space: mockSpace,
355
+ });
356
+ }
357
+ catch (e) {
358
+ errorCaught = true;
359
+ errorMsg = e.message;
360
+ }
361
+ iReckon(sir, errorCaught).asTo('service rejected forgery').isGonnaBeTrue();
362
+ // Verify it was a crypto error, not something else
363
+ iReckon(sir, errorMsg).asTo('error mentions crypto violation').includes('Crypto Violation');
364
+ });
365
+ });
366
+ await respecfully(sir, 'Policy Violation (Restricted Verbs)', async () => {
367
+ await ifWeMight(sir, 'throws error if signing forbidden verb with restricted pool', async () => {
368
+ // Create a specific restricted pool config manually
369
+ const restrictedPoolId = "read_only_pool";
370
+ const restrictedConfig = createStandardPoolConfig(restrictedPoolId);
371
+ // Manually restrict it (since Builder defaults to undefined/allow-all)
372
+ restrictedConfig.allowedVerbs = ['read'];
373
+ const restrictedGenesis = await service.genesis({
374
+ masterSecret: aliceSecret,
375
+ configs: [restrictedConfig],
376
+ metaspace: mockMetaspace,
377
+ space: mockSpace,
378
+ });
379
+ // Try to sign "write" using "read_only_pool"
380
+ const claim = { target: "data^gib", verb: "write" };
381
+ let errorCaught = false;
382
+ try {
383
+ await service.sign({
384
+ latestKeystone: restrictedGenesis,
385
+ masterSecret: aliceSecret,
386
+ claim,
387
+ poolId: restrictedPoolId, // Force use of restricted pool
388
+ metaspace: mockMetaspace,
389
+ space: mockSpace,
390
+ });
391
+ }
392
+ catch (e) {
393
+ errorCaught = true;
394
+ // Optional: Check error message contains "not authorized"
395
+ }
396
+ iReckon(sir, errorCaught).asTo('policy enforced').isGonnaBeTrue();
397
+ });
398
+ });
399
+ });
400
+ // ===========================================================================
401
+ // SUITE D: REVOCATION
402
+ // ===========================================================================
403
+ await respecfullyDear(sir, 'Suite D: Revocation', async () => {
404
+ const service = new KeystoneService_V1();
405
+ const masterSecret = "AliceSecret_RevokeTest";
406
+ let mockSpace;
407
+ let mockMetaspace;
408
+ let genesisKeystone;
409
+ firstOfAll(sir, async () => {
410
+ mockSpace = new MockIbGibSpace();
411
+ mockMetaspace = new MockMetaspaceService(mockSpace);
412
+ // Setup Identity WITH a Revocation Pool
413
+ const stdConfig = createStandardPoolConfig(POOL_ID_DEFAULT);
414
+ const revokeConfig = createRevocationPoolConfig(POOL_ID_REVOKE); // Special Config
415
+ genesisKeystone = await service.genesis({
416
+ masterSecret,
417
+ configs: [stdConfig, revokeConfig],
418
+ metaspace: mockMetaspace,
419
+ space: mockSpace,
420
+ });
421
+ });
422
+ await respecfully(sir, 'Revoke Lifecycle', async () => {
423
+ let revokedKeystone;
424
+ await ifWeMight(sir, 'successfully creates a revocation frame', async () => {
425
+ revokedKeystone = await service.revoke({
426
+ latestKeystone: genesisKeystone,
427
+ masterSecret,
428
+ reason: "Key compromised",
429
+ metaspace: mockMetaspace,
430
+ space: mockSpace,
431
+ });
432
+ iReckon(sir, revokedKeystone).isGonnaBeTruthy();
433
+ // Check Data
434
+ const data = revokedKeystone.data;
435
+ iReckon(sir, data.revocationInfo).asTo('revocation info present').isGonnaBeTruthy();
436
+ iReckon(sir, data.revocationInfo.reason).willEqual("Key compromised");
437
+ iReckon(sir, data.revocationInfo.proof.claim.verb).willEqual(KEYSTONE_VERB_REVOKE);
438
+ });
439
+ await ifWeMight(sir, 'validates the revocation frame', async () => {
440
+ const errors = await service.validate({
441
+ prevIbGib: genesisKeystone,
442
+ currentIbGib: revokedKeystone,
443
+ // metaspace: mockMetaspace,
444
+ // space: mockSpace as any,
445
+ });
446
+ iReckon(sir, errors.length).asTo('no validation errors').willEqual(0);
447
+ });
448
+ await ifWeMight(sir, 'consumed the revocation pool (Scorched Earth)', async () => {
449
+ const data = revokedKeystone.data;
450
+ const revokePool = data.challengePools.find(p => p.id === POOL_ID_REVOKE);
451
+ // The pool should exist...
452
+ iReckon(sir, revokePool).isGonnaBeTruthy();
453
+ // Should be empty (0 challenges)
454
+ const remaining = Object.keys(revokePool.challenges);
455
+ iReckon(sir, remaining.length).asTo('pool depleted').willEqual(0);
456
+ });
457
+ });
458
+ });
459
+ // ===========================================================================
460
+ // SUITE E: STRUCTURAL EVOLUTION (addPools)
461
+ // ===========================================================================
462
+ await respecfullyDear(sir, 'Suite E: Structural Evolution (addPools)', async () => {
463
+ const service = new KeystoneService_V1();
464
+ const aliceSecret = "Alice_Master_Key";
465
+ const bobSecret = "Bob_Foreign_Key";
466
+ let mockSpace;
467
+ let mockMetaspace;
468
+ let aliceKeystone;
469
+ // Helper to generate a "Foreign" pool (e.g. from Bob)
470
+ const createForeignPool = async (id, verbs = []) => {
471
+ const config = createStandardPoolConfig(id);
472
+ config.allowedVerbs = verbs;
473
+ // We use the factory manually here to simulate Bob doing this offline
474
+ const strategy = KeystoneStrategyFactory.create({ config });
475
+ const poolSecret = await strategy.derivePoolSecret({ masterSecret: bobSecret });
476
+ const challenges = {};
477
+ const bindingMap = {};
478
+ for (let i = 0; i < 10; i++) {
479
+ // Manually replicate ID gen for test
480
+ const raw = await hash({ s: `${config.salt}${Date.now()}${i}` });
481
+ const challengeId = raw.substring(0, 16);
482
+ const solution = await strategy.generateSolution({
483
+ poolSecret, poolId: config.salt, challengeId,
484
+ });
485
+ const challenge = await strategy.generateChallenge({ solution });
486
+ challenges[challengeId] = challenge;
487
+ addToBindingMap(bindingMap, challengeId);
488
+ }
489
+ return {
490
+ id,
491
+ config,
492
+ challenges,
493
+ bindingMap,
494
+ isForeign: true,
495
+ metadata: { owner: 'Bob' }
496
+ };
497
+ };
498
+ firstOfAll(sir, async () => {
499
+ mockSpace = new MockIbGibSpace();
500
+ mockMetaspace = new MockMetaspaceService(mockSpace);
501
+ // Alice Genesis: Standard pool (allows all verbs, including 'manage')
502
+ const config = createStandardPoolConfig(POOL_ID_DEFAULT);
503
+ aliceKeystone = await service.genesis({
504
+ masterSecret: aliceSecret,
505
+ configs: [config],
506
+ metaspace: mockMetaspace,
507
+ space: mockSpace,
508
+ });
509
+ });
510
+ await respecfully(sir, 'Happy Path', async () => {
511
+ await ifWeMight(sir, 'authorizes and adds a foreign pool', async () => {
512
+ const bobPool = await createForeignPool("pool_bob", ["post"]);
513
+ const updatedKeystone = await service.addPools({
514
+ latestKeystone: aliceKeystone,
515
+ masterSecret: aliceSecret,
516
+ newPools: [bobPool],
517
+ metaspace: mockMetaspace,
518
+ space: mockSpace,
519
+ });
520
+ // 1. Verify new state
521
+ iReckon(sir, updatedKeystone).isGonnaBeTruthy();
522
+ const pools = updatedKeystone.data.challengePools;
523
+ iReckon(sir, pools.length).asTo('pool count increased').willEqual(2);
524
+ const foundBob = pools.find(p => p.id === "pool_bob");
525
+ iReckon(sir, foundBob).asTo('bob pool exists').isGonnaBeTruthy();
526
+ iReckon(sir, foundBob.isForeign).asTo('isForeign flag').isGonnaBeTrue();
527
+ // 2. Verify Proof
528
+ const proof = updatedKeystone.data.proofs[0];
529
+ iReckon(sir, proof.claim.verb).asTo('proof verb').willEqual("manage");
530
+ // Alice signed this using HER pool (default), not Bob's
531
+ iReckon(sir, proof.solutions[0].poolId).willEqual(POOL_ID_DEFAULT);
532
+ // 3. Verify Validity
533
+ const errors = await service.validate({
534
+ prevIbGib: aliceKeystone,
535
+ currentIbGib: updatedKeystone,
536
+ });
537
+ iReckon(sir, errors.length).asTo('validation passed').willEqual(0);
538
+ // Update local ref for next tests
539
+ aliceKeystone = updatedKeystone;
540
+ });
541
+ });
542
+ await respecfully(sir, 'Permissions & Logic', async () => {
543
+ await ifWeMight(sir, 'fails if no pool allows "manage" verb', async () => {
544
+ // 1. Create a restricted keystone
545
+ const restrictedConfig = createStandardPoolConfig("read_only");
546
+ restrictedConfig.allowedVerbs = ['read']; // No 'manage'
547
+ const restrictedKeystone = await service.genesis({
548
+ masterSecret: aliceSecret,
549
+ configs: [restrictedConfig],
550
+ metaspace: mockMetaspace,
551
+ space: mockSpace,
552
+ });
553
+ const newPool = await createForeignPool("pool_test");
554
+ let errorCaught = false;
555
+ try {
556
+ await service.addPools({
557
+ latestKeystone: restrictedKeystone,
558
+ masterSecret: aliceSecret,
559
+ newPools: [newPool],
560
+ metaspace: mockMetaspace,
561
+ space: mockSpace,
562
+ });
563
+ }
564
+ catch (e) {
565
+ errorCaught = true;
566
+ // Should fail in resolveTargetPool or addPools logic
567
+ // console.log("Caught expected error:", e.message);
568
+ }
569
+ iReckon(sir, errorCaught).asTo('permission denied').isGonnaBeTrue();
570
+ });
571
+ await ifWeMight(sir, 'fails on ID collision', async () => {
572
+ // Try to add "pool_bob" again (it was added in Happy Path)
573
+ const duplicatePool = await createForeignPool("pool_bob");
574
+ let errorCaught = false;
575
+ try {
576
+ await service.addPools({
577
+ latestKeystone: aliceKeystone, // This already has pool_bob
578
+ masterSecret: aliceSecret,
579
+ newPools: [duplicatePool],
580
+ metaspace: mockMetaspace,
581
+ space: mockSpace,
582
+ });
583
+ }
584
+ catch (e) {
585
+ errorCaught = true;
586
+ iReckon(sir, e.message).includes("ID collision");
587
+ }
588
+ iReckon(sir, errorCaught).asTo('collision detected').isGonnaBeTrue();
589
+ });
590
+ });
591
+ });
592
+ // ===========================================================================
593
+ // SUITE E: STRUCTURAL EVOLUTION (addPools)
594
+ // ===========================================================================
595
+ await respecfullyDear(sir, 'Suite E: Structural Evolution (addPools)', async () => {
596
+ const service = new KeystoneService_V1();
597
+ const aliceSecret = "Alice_Master_Key";
598
+ const bobSecret = "Bob_Foreign_Key";
599
+ let mockSpace;
600
+ let mockMetaspace;
601
+ let aliceKeystone;
602
+ // Helper to simulate Bob creating a pool "offline" to give to Alice
603
+ const createForeignPool = async (id, verbs = []) => {
604
+ const config = createStandardPoolConfig(id);
605
+ config.allowedVerbs = verbs;
606
+ // We use the factory manually here to simulate Bob doing this on his own machine
607
+ const strategy = KeystoneStrategyFactory.create({ config });
608
+ const poolSecret = await strategy.derivePoolSecret({ masterSecret: bobSecret });
609
+ const challenges = {};
610
+ const bindingMap = {};
611
+ // Generate a small set of challenges
612
+ for (let i = 0; i < 10; i++) {
613
+ const raw = await hash({ s: `${config.salt}${Date.now()}${i}` });
614
+ const challengeId = raw.substring(0, 16);
615
+ const solution = await strategy.generateSolution({
616
+ poolSecret, poolId: config.salt, challengeId,
617
+ });
618
+ const challenge = await strategy.generateChallenge({ solution });
619
+ challenges[challengeId] = challenge;
620
+ addToBindingMap(bindingMap, challengeId);
621
+ }
622
+ return {
623
+ id,
624
+ config,
625
+ challenges,
626
+ bindingMap,
627
+ isForeign: true,
628
+ metadata: { owner: 'Bob', role: 'Delegate' }
629
+ };
630
+ };
631
+ firstOfAll(sir, async () => {
632
+ mockSpace = new MockIbGibSpace();
633
+ mockMetaspace = new MockMetaspaceService(mockSpace);
634
+ // Alice Genesis: Standard pool (allows all verbs, including 'manage')
635
+ const config = createStandardPoolConfig(POOL_ID_DEFAULT);
636
+ aliceKeystone = await service.genesis({
637
+ masterSecret: aliceSecret,
638
+ configs: [config],
639
+ metaspace: mockMetaspace,
640
+ space: mockSpace,
641
+ });
642
+ });
643
+ await respecfully(sir, 'Happy Path', async () => {
644
+ await ifWeMight(sir, 'authorizes and adds a foreign pool', async () => {
645
+ const bobPool = await createForeignPool("pool_bob", ["post"]);
646
+ const updatedKeystone = await service.addPools({
647
+ latestKeystone: aliceKeystone,
648
+ masterSecret: aliceSecret,
649
+ newPools: [bobPool],
650
+ metaspace: mockMetaspace,
651
+ space: mockSpace,
652
+ });
653
+ // 1. Verify new state
654
+ iReckon(sir, updatedKeystone).isGonnaBeTruthy();
655
+ const pools = updatedKeystone.data.challengePools;
656
+ iReckon(sir, pools.length).asTo('pool count increased').willEqual(2);
657
+ const foundBob = pools.find(p => p.id === "pool_bob");
658
+ iReckon(sir, foundBob).asTo('bob pool exists').isGonnaBeTruthy();
659
+ iReckon(sir, foundBob.isForeign).asTo('isForeign flag').isGonnaBeTrue();
660
+ // 2. Verify Proof
661
+ const proof = updatedKeystone.data.proofs[0];
662
+ iReckon(sir, proof.claim.verb).asTo('proof verb').willEqual(KEYSTONE_VERB_MANAGE);
663
+ // Alice signed this using HER pool (default), not Bob's
664
+ iReckon(sir, proof.solutions[0].poolId).willEqual(POOL_ID_DEFAULT);
665
+ // 3. Verify Validity
666
+ const errors = await service.validate({
667
+ prevIbGib: aliceKeystone,
668
+ currentIbGib: updatedKeystone,
669
+ });
670
+ iReckon(sir, errors.length).asTo('validation passed').willEqual(0);
671
+ // Update local ref for next tests
672
+ aliceKeystone = updatedKeystone;
673
+ });
674
+ });
675
+ await respecfully(sir, 'Permissions & Logic', async () => {
676
+ await ifWeMight(sir, 'fails if no pool allows "manage" verb', async () => {
677
+ // 1. Create a restricted keystone (read-only)
678
+ const restrictedConfig = createStandardPoolConfig("read_only");
679
+ restrictedConfig.allowedVerbs = ['read']; // No 'manage'
680
+ const restrictedKeystone = await service.genesis({
681
+ masterSecret: aliceSecret,
682
+ configs: [restrictedConfig],
683
+ metaspace: mockMetaspace,
684
+ space: mockSpace,
685
+ });
686
+ const newPool = await createForeignPool("pool_test");
687
+ let errorCaught = false;
688
+ try {
689
+ await service.addPools({
690
+ latestKeystone: restrictedKeystone,
691
+ masterSecret: aliceSecret,
692
+ newPools: [newPool],
693
+ metaspace: mockMetaspace,
694
+ space: mockSpace,
695
+ });
696
+ }
697
+ catch (e) {
698
+ errorCaught = true;
699
+ // Optional: Check error message
700
+ // iReckon(sir, e.message).includes("No local pool found with 'manage'");
701
+ }
702
+ iReckon(sir, errorCaught).asTo('permission denied').isGonnaBeTrue();
703
+ });
704
+ await ifWeMight(sir, 'fails on ID collision', async () => {
705
+ // Try to add "pool_bob" again (it was added in Happy Path)
706
+ const duplicatePool = await createForeignPool("pool_bob");
707
+ let errorCaught = false;
708
+ try {
709
+ await service.addPools({
710
+ latestKeystone: aliceKeystone, // This already has pool_bob
711
+ masterSecret: aliceSecret,
712
+ newPools: [duplicatePool],
713
+ metaspace: mockMetaspace,
714
+ space: mockSpace,
715
+ });
716
+ }
717
+ catch (e) {
718
+ errorCaught = true;
719
+ iReckon(sir, e.message).includes("collision");
720
+ }
721
+ iReckon(sir, errorCaught).asTo('collision detected').isGonnaBeTrue();
722
+ });
723
+ });
724
+ });
725
+ // ===========================================================================
726
+ // SUITE F: DEEP INSPECTION (Granularity & Serialization)
727
+ // ===========================================================================
728
+ await respecfullyDear(sir, 'Suite F: Deep Inspection', async () => {
729
+ const service = new KeystoneService_V1();
730
+ const aliceSecret = "Alice_Deep_Inspect";
731
+ const salt = "granularity_pool";
732
+ let mockSpace;
733
+ let mockMetaspace;
734
+ let genesisKeystone;
735
+ // We use a specific hybrid config to test exact selection logic
736
+ const hybridConfig = createStandardPoolConfig(salt);
737
+ // 2 FIFO + 2 Random = 4 Total per sign
738
+ hybridConfig.behavior.selectSequentially = 2;
739
+ hybridConfig.behavior.selectRandomly = 2;
740
+ hybridConfig.behavior.size = 20; // Small enough to track, large enough to be random
741
+ firstOfAll(sir, async () => {
742
+ mockSpace = new MockIbGibSpace();
743
+ mockMetaspace = new MockMetaspaceService(mockSpace);
744
+ genesisKeystone = await service.genesis({
745
+ masterSecret: aliceSecret,
746
+ configs: [hybridConfig],
747
+ metaspace: mockMetaspace,
748
+ space: mockSpace,
749
+ });
750
+ });
751
+ await respecfully(sir, 'Proof Granularity & Math', async () => {
752
+ let signedKeystone;
753
+ await ifWeMight(sir, 'generates exactly the expected number of solutions', async () => {
754
+ signedKeystone = await service.sign({
755
+ latestKeystone: genesisKeystone,
756
+ masterSecret: aliceSecret,
757
+ claim: { verb: "post", target: "data^gib" },
758
+ metaspace: mockMetaspace,
759
+ space: mockSpace,
760
+ });
761
+ const proofs = signedKeystone.data.proofs;
762
+ iReckon(sir, proofs.length).asTo('proof count').willEqual(1);
763
+ const solutions = proofs[0].solutions;
764
+ // 2 Sequential + 2 Random = 4
765
+ iReckon(sir, solutions.length).asTo('solution count').willEqual(4);
766
+ });
767
+ await ifWeMight(sir, 'verifies the math manually (White-box Crypto Check)', async () => {
768
+ const proof = signedKeystone.data.proofs[0];
769
+ const poolSnapshot = genesisKeystone.data.challengePools.find(p => p.id === salt);
770
+ // We iterate every solution in the proof and MANUALLY verify the hash relationship
771
+ // bypassing the Service's validation logic to ensure the raw math holds up.
772
+ for (const solution of proof.solutions) {
773
+ // 1. Find the challenge in the *Previous* frame (Genesis)
774
+ const challenge = poolSnapshot.challenges[solution.challengeId];
775
+ if (!challenge) {
776
+ throw new Error(`Test Failure: Solution references ID ${solution.challengeId} which was not in Genesis pool.`);
777
+ }
778
+ // 2. Re-implement HashReveal V1 verification logic locally in the test
779
+ // Hash(Salt + Value + Salt)
780
+ // Note: rounds=1 in standard config
781
+ const indexSalt = solution.challengeId;
782
+ const calculatedHash = await hash({
783
+ s: `${indexSalt}${solution.value}${indexSalt}`,
784
+ algorithm: 'SHA-256'
785
+ });
786
+ // 3. Assert
787
+ iReckon(sir, calculatedHash).asTo(`Manual hash verification for ${solution.challengeId}`).willEqual(challenge.hash);
788
+ }
789
+ });
790
+ await ifWeMight(sir, 'verifies FIFO logic (Deterministic Selection)', async () => {
791
+ const proof = signedKeystone.data.proofs[0];
792
+ const poolSnapshot = genesisKeystone.data.challengePools.find(p => p.id === salt);
793
+ // The first N keys in the pool should be the FIFO targets.
794
+ // Assumption: Object.keys returns insertion order (Standard in modern JS engines)
795
+ const allIds = Object.keys(poolSnapshot.challenges);
796
+ const expectedFifoIds = allIds.slice(0, 2);
797
+ const solvedIds = proof.solutions.map(s => s.challengeId);
798
+ // Check that our solution list *includes* the expected FIFO IDs
799
+ const hasFirst = solvedIds.includes(expectedFifoIds[0]);
800
+ const hasSecond = solvedIds.includes(expectedFifoIds[1]);
801
+ iReckon(sir, hasFirst).asTo(`Solution includes 1st FIFO ID (${expectedFifoIds[0]})`).isGonnaBeTrue();
802
+ iReckon(sir, hasSecond).asTo(`Solution includes 2nd FIFO ID (${expectedFifoIds[1]})`).isGonnaBeTrue();
803
+ });
804
+ });
805
+ // await respecfully(sir, 'DTO & Serialization', async () => {
806
+ // await ifWeMight(sir, 'survives a clone/JSON-cycle without corruption', async () => {
807
+ // // 1. Create a DTO (simulate network transmission/storage)
808
+ // // 'clone' does a JSON stringify/parse under the hood (usually) or structured clone.
809
+ // const dto = clone(signedKeystone);
810
+ // // 2. Structural checks
811
+ // iReckon(sir, dto).asTo('dto exists').isGonnaBeTruthy();
812
+ // iReckon(sir, dto.data).asTo('dto data').isGonnaBeTruthy();
813
+ // iReckon(sir, dto.data!.proofs).asTo('dto proofs').isGonnaBeTruthy();
814
+ // // 3. Functional check: Can the service validate this DTO?
815
+ // // This ensures no prototypes or hidden properties were lost that the service depends on.
816
+ // const errors = await service.validate({
817
+ // prevIbGib: genesisKeystone,
818
+ // currentIbGib: dto, // Passing the DTO, not the original object
819
+ // });
820
+ // iReckon(sir, errors.length).asTo('DTO validation errors').willEqual(0);
821
+ // });
822
+ // await ifWeMight(sir, 'ensures data contains no functions or circular refs', async () => {
823
+ // // A crude but effective test: ensure JSON.stringify doesn't throw
824
+ // // and the result is equal to the object (if we parsed it back).
825
+ // const jsonStr = JSON.stringify(signedKeystone);
826
+ // const parsed = JSON.parse(jsonStr);
827
+ // // Compare specific deep fields
828
+ // const originalSolution = signedKeystone.data!.proofs[0].solutions[0].value;
829
+ // const parsedSolution = parsed.data.proofs[0].solutions[0].value;
830
+ // iReckon(sir, parsedSolution).asTo('deep property survives stringify').willEqual(originalSolution);
831
+ // // Ensure no extra properties were lost (rudimentary check)
832
+ // const origKeys = Object.keys(signedKeystone.data!);
833
+ // const parsedKeys = Object.keys(parsed.data);
834
+ // iReckon(sir, parsedKeys.length).asTo('key count matches').willEqual(origKeys.length);
835
+ // });
836
+ // });
837
+ });
838
+ //# sourceMappingURL=keystone-service-v1.respec.mjs.map