@ibgib/core-gib 0.1.58 → 0.1.60

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 (96) hide show
  1. package/CHANGELOG.md +9 -1
  2. package/dist/sync/sync-peer/sync-peer-http-receiver/sync-peer-http-receiver-v1.d.mts.map +1 -1
  3. package/dist/sync/sync-peer/sync-peer-http-receiver/sync-peer-http-receiver-v1.mjs +6 -2
  4. package/dist/sync/sync-peer/sync-peer-http-receiver/sync-peer-http-receiver-v1.mjs.map +1 -1
  5. package/dist/sync/sync-peer/sync-peer-innerspace/sync-peer-innerspace-types.d.mts +1 -1
  6. package/dist/sync/sync-peer/sync-peer-innerspace/sync-peer-innerspace-v1.d.mts +1 -1
  7. package/dist/sync/sync-peer/sync-peer-innerspace/sync-peer-innerspace-v1.d.mts.map +1 -1
  8. package/dist/sync/sync-peer/sync-peer-innerspace/sync-peer-innerspace-v1.mjs +7 -11
  9. package/dist/sync/sync-peer/sync-peer-innerspace/sync-peer-innerspace-v1.mjs.map +1 -1
  10. package/dist/sync/sync-peer/sync-peer-types.d.mts +24 -1
  11. package/dist/sync/sync-peer/sync-peer-types.d.mts.map +1 -1
  12. package/dist/sync/sync-peer/sync-peer-v1.d.mts +15 -4
  13. package/dist/sync/sync-peer/sync-peer-v1.d.mts.map +1 -1
  14. package/dist/sync/sync-peer/sync-peer-v1.mjs +120 -25
  15. package/dist/sync/sync-peer/sync-peer-v1.mjs.map +1 -1
  16. package/dist/sync/sync-peer/sync-peer-websocket/sync-peer-websocket-constants.d.mts +46 -0
  17. package/dist/sync/sync-peer/sync-peer-websocket/sync-peer-websocket-constants.d.mts.map +1 -0
  18. package/dist/sync/sync-peer/sync-peer-websocket/sync-peer-websocket-constants.mjs +45 -0
  19. package/dist/sync/sync-peer/sync-peer-websocket/sync-peer-websocket-constants.mjs.map +1 -0
  20. package/dist/sync/sync-peer/{sync-peer-websocket-receiver → sync-peer-websocket/sync-peer-websocket-receiver}/sync-peer-websocket-receiver-types.d.mts +2 -2
  21. package/dist/sync/sync-peer/sync-peer-websocket/sync-peer-websocket-receiver/sync-peer-websocket-receiver-types.d.mts.map +1 -0
  22. package/dist/sync/sync-peer/sync-peer-websocket/sync-peer-websocket-receiver/sync-peer-websocket-receiver-types.mjs.map +1 -0
  23. package/dist/sync/sync-peer/{sync-peer-websocket-receiver → sync-peer-websocket/sync-peer-websocket-receiver}/sync-peer-websocket-receiver-v1.d.mts +11 -8
  24. package/dist/sync/sync-peer/sync-peer-websocket/sync-peer-websocket-receiver/sync-peer-websocket-receiver-v1.d.mts.map +1 -0
  25. package/dist/sync/sync-peer/{sync-peer-websocket-receiver → sync-peer-websocket/sync-peer-websocket-receiver}/sync-peer-websocket-receiver-v1.mjs +115 -61
  26. package/dist/sync/sync-peer/sync-peer-websocket/sync-peer-websocket-receiver/sync-peer-websocket-receiver-v1.mjs.map +1 -0
  27. package/dist/sync/sync-peer/{sync-peer-websocket-receiver → sync-peer-websocket/sync-peer-websocket-receiver}/sync-websocket-peer-helpers.d.mts +3 -3
  28. package/dist/sync/sync-peer/sync-peer-websocket/sync-peer-websocket-receiver/sync-websocket-peer-helpers.d.mts.map +1 -0
  29. package/dist/sync/sync-peer/{sync-peer-websocket-receiver → sync-peer-websocket/sync-peer-websocket-receiver}/sync-websocket-peer-helpers.mjs +6 -6
  30. package/dist/sync/sync-peer/sync-peer-websocket/sync-peer-websocket-receiver/sync-websocket-peer-helpers.mjs.map +1 -0
  31. package/dist/sync/sync-peer/{sync-peer-websocket-sender → sync-peer-websocket/sync-peer-websocket-sender}/sync-peer-websocket-sender-types.d.mts +1 -1
  32. package/dist/sync/sync-peer/sync-peer-websocket/sync-peer-websocket-sender/sync-peer-websocket-sender-types.d.mts.map +1 -0
  33. package/dist/sync/sync-peer/sync-peer-websocket/sync-peer-websocket-sender/sync-peer-websocket-sender-types.mjs.map +1 -0
  34. package/dist/sync/sync-peer/{sync-peer-websocket-sender → sync-peer-websocket/sync-peer-websocket-sender}/sync-peer-websocket-sender-v1.d.mts +22 -4
  35. package/dist/sync/sync-peer/sync-peer-websocket/sync-peer-websocket-sender/sync-peer-websocket-sender-v1.d.mts.map +1 -0
  36. package/dist/sync/sync-peer/sync-peer-websocket/sync-peer-websocket-sender/sync-peer-websocket-sender-v1.mjs +447 -0
  37. package/dist/sync/sync-peer/sync-peer-websocket/sync-peer-websocket-sender/sync-peer-websocket-sender-v1.mjs.map +1 -0
  38. package/dist/sync/sync-saga-context/sync-saga-context-helpers.d.mts +22 -5
  39. package/dist/sync/sync-saga-context/sync-saga-context-helpers.d.mts.map +1 -1
  40. package/dist/sync/sync-saga-context/sync-saga-context-helpers.mjs +263 -28
  41. package/dist/sync/sync-saga-context/sync-saga-context-helpers.mjs.map +1 -1
  42. package/dist/sync/sync-saga-context/sync-saga-context-types.d.mts +13 -0
  43. package/dist/sync/sync-saga-context/sync-saga-context-types.d.mts.map +1 -1
  44. package/dist/sync/sync-saga-coordinator.d.mts +12 -1
  45. package/dist/sync/sync-saga-coordinator.d.mts.map +1 -1
  46. package/dist/sync/sync-saga-coordinator.mjs +106 -12
  47. package/dist/sync/sync-saga-coordinator.mjs.map +1 -1
  48. package/dist/sync/sync-types.d.mts +24 -0
  49. package/dist/sync/sync-types.d.mts.map +1 -1
  50. package/dist/sync/sync-types.mjs +0 -1
  51. package/dist/sync/sync-types.mjs.map +1 -1
  52. package/dist/sync/sync-withid.connect.respec.mjs +3 -3
  53. package/dist/sync/sync-withid.connect.respec.mjs.map +1 -1
  54. package/dist/sync/sync-withid.pingpong.respec.d.mts +11 -0
  55. package/dist/sync/sync-withid.pingpong.respec.d.mts.map +1 -0
  56. package/dist/sync/sync-withid.pingpong.respec.mjs +199 -0
  57. package/dist/sync/sync-withid.pingpong.respec.mjs.map +1 -0
  58. package/dist/witness/space/inner-space/inner-space-v1.d.mts.map +1 -1
  59. package/dist/witness/space/inner-space/inner-space-v1.mjs +1 -1
  60. package/dist/witness/space/inner-space/inner-space-v1.mjs.map +1 -1
  61. package/package.json +1 -1
  62. package/src/sync/docs/security-3b.md +92 -0
  63. package/src/sync/docs/security.md +107 -39
  64. package/src/sync/sync-peer/sync-peer-http-receiver/sync-peer-http-receiver-v1.mts +6 -2
  65. package/src/sync/sync-peer/sync-peer-innerspace/sync-peer-innerspace-types.mts +1 -1
  66. package/src/sync/sync-peer/sync-peer-innerspace/sync-peer-innerspace-v1.mts +11 -14
  67. package/src/sync/sync-peer/sync-peer-types.mts +28 -1
  68. package/src/sync/sync-peer/sync-peer-v1.mts +127 -35
  69. package/src/sync/sync-peer/sync-peer-websocket/README.md +42 -0
  70. package/src/sync/sync-peer/sync-peer-websocket/sync-peer-websocket-constants.mts +68 -0
  71. package/src/sync/sync-peer/{sync-peer-websocket-receiver → sync-peer-websocket/sync-peer-websocket-receiver}/sync-peer-websocket-receiver-types.mts +2 -2
  72. package/src/sync/sync-peer/{sync-peer-websocket-receiver → sync-peer-websocket/sync-peer-websocket-receiver}/sync-peer-websocket-receiver-v1.mts +128 -71
  73. package/src/sync/sync-peer/{sync-peer-websocket-receiver → sync-peer-websocket/sync-peer-websocket-receiver}/sync-websocket-peer-helpers.mts +8 -8
  74. package/src/sync/sync-peer/{sync-peer-websocket-sender → sync-peer-websocket/sync-peer-websocket-sender}/sync-peer-websocket-sender-types.mts +1 -1
  75. package/src/sync/sync-peer/sync-peer-websocket/sync-peer-websocket-sender/sync-peer-websocket-sender-v1.mts +509 -0
  76. package/src/sync/sync-saga-context/sync-saga-context-helpers.mts +267 -36
  77. package/src/sync/sync-saga-context/sync-saga-context-types.mts +14 -0
  78. package/src/sync/sync-saga-coordinator.mts +148 -8
  79. package/src/sync/sync-types.mts +28 -4
  80. package/src/sync/sync-withid.connect.respec.mts +3 -3
  81. package/src/sync/sync-withid.pingpong.respec.mts +234 -0
  82. package/src/witness/space/inner-space/inner-space-v1.mts +4 -5
  83. package/dist/sync/sync-peer/sync-peer-websocket-receiver/sync-peer-websocket-receiver-types.d.mts.map +0 -1
  84. package/dist/sync/sync-peer/sync-peer-websocket-receiver/sync-peer-websocket-receiver-types.mjs.map +0 -1
  85. package/dist/sync/sync-peer/sync-peer-websocket-receiver/sync-peer-websocket-receiver-v1.d.mts.map +0 -1
  86. package/dist/sync/sync-peer/sync-peer-websocket-receiver/sync-peer-websocket-receiver-v1.mjs.map +0 -1
  87. package/dist/sync/sync-peer/sync-peer-websocket-receiver/sync-websocket-peer-helpers.d.mts.map +0 -1
  88. package/dist/sync/sync-peer/sync-peer-websocket-receiver/sync-websocket-peer-helpers.mjs.map +0 -1
  89. package/dist/sync/sync-peer/sync-peer-websocket-sender/sync-peer-websocket-sender-types.d.mts.map +0 -1
  90. package/dist/sync/sync-peer/sync-peer-websocket-sender/sync-peer-websocket-sender-types.mjs.map +0 -1
  91. package/dist/sync/sync-peer/sync-peer-websocket-sender/sync-peer-websocket-sender-v1.d.mts.map +0 -1
  92. package/dist/sync/sync-peer/sync-peer-websocket-sender/sync-peer-websocket-sender-v1.mjs +0 -282
  93. package/dist/sync/sync-peer/sync-peer-websocket-sender/sync-peer-websocket-sender-v1.mjs.map +0 -1
  94. package/src/sync/sync-peer/sync-peer-websocket-sender/sync-peer-websocket-sender-v1.mts +0 -321
  95. /package/dist/sync/sync-peer/{sync-peer-websocket-receiver → sync-peer-websocket/sync-peer-websocket-receiver}/sync-peer-websocket-receiver-types.mjs +0 -0
  96. /package/dist/sync/sync-peer/{sync-peer-websocket-sender → sync-peer-websocket/sync-peer-websocket-sender}/sync-peer-websocket-sender-types.mjs +0 -0
@@ -2,25 +2,25 @@
2
2
  * @module sync saga context helpers
3
3
  */
4
4
 
5
- import { extractErrorMsg, getTimestamp } from '@ibgib/helper-gib/dist/helpers/utils-helper.mjs';
6
- import { Factory_V1 } from '@ibgib/ts-gib/dist/V1/factory.mjs';
7
- import { getIbGibAddr } from '@ibgib/ts-gib/dist/helper.mjs';
8
- import { IbGib_V1, } from '@ibgib/ts-gib/dist/V1/types.mjs';
9
- import { Ib } from '@ibgib/ts-gib/dist/types.mjs';
5
+ import { extractErrorMsg, pretty, } from '@ibgib/helper-gib/dist/helpers/utils-helper.mjs';
6
+ import { getIbGibAddr, } from '@ibgib/ts-gib/dist/helper.mjs';
7
+ import { Ib, } from '@ibgib/ts-gib/dist/types.mjs';
10
8
  import { validateIbGibIntrinsically } from '@ibgib/ts-gib/dist/V1/validate-helper.mjs';
11
9
 
12
10
  import { GLOBAL_LOG_A_LOT } from '../../core-constants.mjs';
13
11
  import { SYNC_SAGA_CONTEXT_ATOM } from './sync-saga-context-constants.mjs';
14
- import { SYNC_SAGA_PAYLOAD_ADDRS_DOMAIN } from '../sync-constants.mjs';
12
+ import { SYNC_SAGA_PAYLOAD_ADDRS_DOMAIN, SYNC_MSG_REL8N_NAME } from '../sync-constants.mjs';
15
13
  import {
16
14
  SyncSagaContextData_V1, SyncSagaContextIbGib_V1, SyncSagaContextIb_V1,
17
- SyncSagaContextRel8ns_V1,
18
15
  } from './sync-saga-context-types.mjs';
19
16
  import { IbGibSpaceAny } from '../../witness/space/space-base-v1.mjs';
20
- import { getFromSpace, putInSpace, registerNewIbGib } from '../../witness/space/space-helper.mjs';
21
- import { SyncIbGib_V1, SyncSagaFrameDependencyGraph } from '../sync-types.mjs';
17
+ import { getFromSpace, getLatestAddrs, getTjpIbGib } from '../../witness/space/space-helper.mjs';
18
+ import { SessionGenesisFrameDetails, } from '../sync-types.mjs';
22
19
  import { validateSyncSagaFrame } from '../sync-helpers.mjs';
23
- import { isIbGibWithAtom } from '../../common/other/ibgib-helper.mjs';
20
+ import { isIbGibWithAtom, toDto } from '../../common/other/ibgib-helper.mjs';
21
+ import { KeystoneService_V1 } from '../../keystone/keystone-service-v1.mjs';
22
+ import { KeystoneData_V1, KeystoneIbGib_V1 } from '../../keystone/keystone-types.mjs';
23
+ import { KEYSTONE_VERB_SYNC } from '../../keystone/keystone-constants.mjs';
24
24
 
25
25
  const logalot = GLOBAL_LOG_A_LOT;
26
26
 
@@ -150,6 +150,26 @@ export async function validateContextAndSagaFrame({
150
150
  errors.push(`context.sagaFrame is falsy. (E: b4edd88f4963f493789f83b29ba2df26)`);
151
151
  }
152
152
 
153
+ if (context.sagaFrameMsg) {
154
+ const sagaFrameMsgErrors =
155
+ await validateIbGibIntrinsically({ ibGib: context.sagaFrameMsg }) ?? [];
156
+ sagaFrameMsgErrors.forEach(x => errors.push(x));
157
+
158
+ if (context.sagaFrame) {
159
+ const expectedMsgAddr = context.sagaFrame.rel8ns?.[SYNC_MSG_REL8N_NAME]?.[0];
160
+ if (expectedMsgAddr) {
161
+ const actualMsgAddr = getIbGibAddr({ ibGib: context.sagaFrameMsg });
162
+ if (actualMsgAddr !== expectedMsgAddr) {
163
+ errors.push(`context.sagaFrameMsg address (${actualMsgAddr}) does not match the stone address referenced in sagaFrame relations (${expectedMsgAddr}). (E: a983b271fcae46bbad7e82098bc24826)`);
164
+ }
165
+ } else {
166
+ errors.push(`context.sagaFrame is missing the message stone relation '${SYNC_MSG_REL8N_NAME}'. (E: da872cf3a8d46dbbad89d0a68d712826)`);
167
+ }
168
+ }
169
+ } else {
170
+ errors.push(`context.sagaFrameMsg is falsy. (E: ed405a72ab0d8bbdca7b9605d8f9a26)`);
171
+ }
172
+
153
173
  // if this is already invalid, we could have intrinsic validation
154
174
  // errors, which are a non-starter.
155
175
  if (errors.length > 0) { return errors; /* <<<< returns early */ }
@@ -192,42 +212,253 @@ export async function validateContextDomainPayloadIbGibs({ context }: { context:
192
212
  }
193
213
  }
194
214
 
195
- export async function authenticateContext({
196
- }): Promise<string[]> {
197
- const lc = `[${authenticateContext.name}]`;
198
- try {
199
- if (logalot) { console.log(`${lc} starting... (I: 3c34e8f1d6ef965f98725c88459ea926)`); }
200
- console.warn(`${lc}[NAG] not thrown. not implemented right now after removing all identity-related code. (W: e5fad31cfb49eef198a189a82dbcf726)`)
201
- return [];
202
- } catch (error) {
203
- console.error(`${lc} ${extractErrorMsg(error)}`);
204
- throw error;
205
- } finally {
206
- if (logalot) { console.log(`${lc} complete.`); }
207
- }
208
- }
209
-
210
215
  /**
211
- * move to sync-peer-helpers.mts as a pure function?
216
+ * "Intrinsically": This authenticates assuming we have already established that
217
+ * this context is a valid continuation of previous sync contexts/saga state. It
218
+ * does not check that the session identity contained on this context is a valid
219
+ * continuation.
220
+ *
221
+ * ## notes
222
+ *
223
+ * Say an attacker tries to hijack a legit sync process by capturing previous
224
+ * context(s). Then the attacker just either creates its own sessionIdentity
225
+ *
226
+ * ## implementation notes
227
+ *
228
+ * This is a HUGE function right now, because there are just a lot of things to
229
+ * check. I'm basically going through and just taking every assumption that I
230
+ * can think of and encoding it.
231
+ *
232
+ * We will need to refactor this at some point to neaten it up, but we should
233
+ * not remove sections without EXTREMELY good reasoning, as this would reduce
234
+ * security.
212
235
  */
213
- export async function authorizeContext({
236
+ export async function authenticateContextIntrinsically({
214
237
  context,
215
- fullSagaHistory
238
+ space,
239
+ // stageInProtocol,
216
240
  }: {
217
241
  context: SyncSagaContextIbGib_V1,
218
- fullSagaHistory: SyncSagaFrameDependencyGraph[],
242
+ space: IbGibSpaceAny,
243
+ // /**
244
+ // * @see {@link StageInProtocol}
245
+ // */
246
+ // stageInProtocol: StageInProtocol,
219
247
  }): Promise<string[]> {
220
- const lc = `[${authorizeContext.name}]`;
248
+ const lc = `[${authenticateContextIntrinsically.name}]`;
221
249
  try {
222
- if (logalot) { console.log(`${lc} starting... (I: 48c918b41ceec0cd489ca3b8819e6826)`); }
250
+ if (logalot) { console.log(`${lc} starting... (I: 3c34e8f1d6ef965f98725c88459ea926)`); }
223
251
 
224
- console.error(`${lc} NAG ERROR (NOT THROWN): not implemented. authorize business logic (v1 must have this, but later when we are working on admin vs. student)(E: bc3a78f2dab18ab64c36d055a4b50526)`);
252
+ const errors: string[] = [];
225
253
 
226
- return [];
254
+ if (!context.data) { throw new Error(`(UNEXPECTED) context.data falsy? (E: 3e4ddd0eb4b828ad489658d88d9a6326)`); }
255
+ if (!context.rel8ns) { throw new Error(`(UNEXPECTED) context.rel8ns falsy? (E: 8026589d4fed69c828334ee842074326)`); }
256
+
257
+ const { sagaFrame, signedSessionIdentity: currSessionIdentity } = context;
258
+ if (!sagaFrame.data) { throw new Error(`(UNEXPECTED) sagaFrame.data falsy? (E: b61cc82d25984c92f75db74a5a855b26)`); }
259
+
260
+ // We only sign at the context level.
261
+ // If context has no signedSessionIdentity, skip authentication (anonymous or broker response).
262
+ if (!currSessionIdentity) {
263
+ // check the sync saga to determine if there _should_ be a session
264
+ // identity according to the given sagaFrame (which could be
265
+ // malicious, remember!!)
266
+ // todo: add logic to SET this property when using identity, then (and only then) remove this todo.
267
+ console.error(`${lc}[NAG][not thrown] sagaFrame.data.sessionIdentityTjpAddr logic needs to be added in coordinator (E: 4fc47800a1086c917a47381824280826)`);
268
+ if (sagaFrame.data.sessionIdentityTjpAddr) {
269
+ errors.push('Context has no session identity, but sync saga frame shows a session identity (sagaFrame.data.sessionIdentityTjpAddr is truthy). (E: 69dd6cdc2e1859c0f3d62958c4339826)')
270
+ return errors; /* <<<< returns early */
271
+ } else if (context.rel8ns.sessionIdentity) {
272
+ errors.push('Context has no signed session identity, but context.rel8ns.sessionIdentity is truthy. (E: 96a04a8a6c88ea8bf88118f89ad8e326)')
273
+ return errors; /* <<<< returns early */
274
+ } else {
275
+ // nothing further to authenticate
276
+ if (logalot) { console.log(`${lc} context has no signedSessionIdentity and sync saga frame doesn't state there should be session identity. So nothing further to authenticate - returning early with no authentication errors. (I: d708735f9a2899ee98f762b8a09ed826)`); }
277
+ return []; /* <<<< returns early */
278
+ }
279
+ }
280
+ const currSessionIdentityAddr = getIbGibAddr({ ibGib: currSessionIdentity });
281
+
282
+ // ensure the context rel8ns points to a session identity
283
+ const prevSessionIdentityAddrs_accordingToContextRel8ns = context.rel8ns?.sessionIdentity ?? [];
284
+ if (prevSessionIdentityAddrs_accordingToContextRel8ns.length === 0) {
285
+ errors.push(`context.rel8ns.sessionIdentity is falsy/empty but context.signedSessionIdentity is present. (E: 66f906421eb2468c0b33f908a3cf2826)`);
286
+ return errors; /* <<<< returns early */
287
+ }
288
+
289
+ if (prevSessionIdentityAddrs_accordingToContextRel8ns.length > 1) {
290
+ errors.push(`context.rel8ns.sessionIdentity has multiple identity addrs. (E: 489428bfe6fdaa4cd885b938dc4c5826)`);
291
+ return errors; /* <<<< returns early */
292
+ }
293
+
294
+ // ensure the context session identity is the immediate past of the
295
+ // current session identity
296
+ const prevSessionIdentityAddr = prevSessionIdentityAddrs_accordingToContextRel8ns[0];
297
+
298
+ // Confirm previous session identity addr exists in space and that it is
299
+ // the most recent in the session keystone's timeline
300
+
301
+ const resGetLatestAddr = await getLatestAddrs({ addrs: [prevSessionIdentityAddr], space });
302
+ if (!resGetLatestAddr) { throw new Error(`(UNEXPECTED) resGetLatestAddr for prevSessionIdentityAddr in space (${space.ib}) falsy? (E: 7b207e5cbcec9037ea5adbe822ead826)`); }
303
+ if (!resGetLatestAddr.data) { throw new Error(`(UNEXPECTED) resGetLatestAddr.data for prevSessionIdentityAddr in space (${space.ib}) falsy? (E: de4eb8d730c8c4dcb59c8b9c79277826)`); }
304
+ if (!resGetLatestAddr.data.success) {
305
+ throw new Error(`(UNEXPECTED) resGetLatestAddr.data.success falsy? (E: c94298dfd9684ad6a87eb748459aa826)`);
306
+ }
307
+ const { latestAddrsMap } = resGetLatestAddr.data;
308
+ if (!latestAddrsMap) { throw new Error(`(UNEXPECTED) resGetLatestAddr.data.latestAddrsMap falsy? (E: 19f1fd5fe798cf2e5fa923919169d826)`); }
309
+ if (Object.keys(latestAddrsMap).length !== 1) {
310
+ throw new Error(`(UNEXPECTED) Object.keys(latestAddrsMap).length !== 1? (E: fe526a0747589c6427a8bcc86da34a26)`);
311
+ }
312
+ const prevSessionIdentityAddr_latest = latestAddrsMap[prevSessionIdentityAddr];
313
+ if (!prevSessionIdentityAddr_latest) {
314
+ errors.push(`prevSessionIdentityAddr (${prevSessionIdentityAddr}) not found in space (${space.ib}). this should have been the incoming prevSessionIdentityAddr (E: f6d042bd6b54819998653228dee34226)`);
315
+ return errors; /* <<<< returns early */
316
+ }
317
+ if (prevSessionIdentityAddr !== prevSessionIdentityAddr_latest) {
318
+ if (prevSessionIdentityAddr_latest === currSessionIdentityAddr) {
319
+ // this is ok? if the sender peer is calling this just to validate **before sending**, then this will be the case. If the receiver is calling this code before continuing the sync/at the start of continuing the sync, then this will not hit.
320
+ // debugger; // in sync saga context auth, want to know if this hits...this does hit, so my thoughts on the innerspace/sender peer seem to be correct
321
+ } else {
322
+ // debugger; // in sync saga context auth, want to know if this hits...so far this does NOT hit
323
+ errors.push(`context.rel8ns.sessionIdentity does not point to the most recent in the space (${space.ib}). (E: 2f8288f53c87b6aa47bd2178d9df0c26)`)
324
+
325
+ // #region debug error keystone
326
+ console.log(`context: ${pretty(toDto({ ibGib: context }))}`)
327
+ console.log(`prevSessionIdentityAddr (context.rel8ns.sessionIdentity): ${prevSessionIdentityAddr}`)
328
+ console.log(`prevSessionIdentityAddr_latest: ${prevSessionIdentityAddr_latest}`)
329
+ console.log(`currSessionIdentity (context.signedSessionIdentity): ${pretty(toDto({
330
+ ibGib: {
331
+ ib: currSessionIdentity.ib,
332
+ gib: currSessionIdentity.gib,
333
+ rel8ns: currSessionIdentity.rel8ns,
334
+ data: {
335
+ ...currSessionIdentity.data,
336
+ challengePools: currSessionIdentity.data.challengePools.map(p => {
337
+ return { ...p, challenges: {} }
338
+ }),
339
+ } satisfies Partial<KeystoneData_V1>,
340
+
341
+ }
342
+ }))}`)
343
+ console.log(`currSessionIdentityAddr: ${currSessionIdentityAddr}`)
344
+
345
+ // #endregion debug error keystone
346
+ return errors; /* <<<< returns early */
347
+ }
348
+ }
349
+
350
+ const resGetPrevSessionIdentity = await getFromSpace({ addr: prevSessionIdentityAddr, space });
351
+ if (!resGetPrevSessionIdentity.success || resGetPrevSessionIdentity.ibGibs?.length !== 1) {
352
+ errors.push(`could not fetch latest sender identity ${prevSessionIdentityAddr} from space (${space.ib}). (E: fd48c3e64c9fa4efd8a1f8280af18226)`);
353
+ return errors;
354
+ }
355
+ const prevSessionIdentity = resGetPrevSessionIdentity.ibGibs[0] as KeystoneIbGib_V1;
356
+
357
+ // get the session identity tjp, which has frame details that link back
358
+ // to the identity that authorized the session
359
+ const sessionIdentityTjp =
360
+ await getTjpIbGib({ ibGib: prevSessionIdentity, naive: true, space }) as KeystoneIbGib_V1 | undefined;
361
+ if (!sessionIdentityTjp) { throw new Error(`(UNEXPECTED) couldn't get sessionIdentityTjp in space (${space.ib})? we have already gotten the identity itself in the space, so we would expect the entire timeline to exist in it. (E: 9be0382ff1c8a0e77645ea38c096f826)`); }
362
+ const sessionIdentityTjpAddr = getIbGibAddr({ ibGib: sessionIdentityTjp });
363
+ if (sessionIdentityTjpAddr !== sagaFrame.data.sessionIdentityTjpAddr) {
364
+ throw new Error(`(UNEXPECTED) sessionIdentityTjpAddr !== sagaFrame.data.sessionIdentityTjpAddr? (E: c9a4ad5c2728fe38e86afc58e4abaf26)`);
365
+ }
366
+
367
+ const sessionGenesisFrameDetails = sessionIdentityTjp.data.frameDetails as SessionGenesisFrameDetails;
368
+ if (!sessionGenesisFrameDetails) {
369
+ errors.push(`Invalid session identity tjp: sessionIdentityTjp.data.frameDetails is falsy. (E: 0187f8f804a84256281720586620b826)`);
370
+ return errors; /* <<<< returns early */
371
+ }
372
+ const { senderIdentityAddr, senderIdentityTjpAddr } = sessionGenesisFrameDetails;
373
+ if (!senderIdentityAddr) { throw new Error(`sessionGenesisFrameDetails.senderIdentityAddr falsy (E: 02a0c80a3ead9e3af8af4cf3b156e826)`); }
374
+ if (!senderIdentityTjpAddr) { throw new Error(`sessionGenesisFrameDetails.senderIdentityTjpAddr falsy (E: 271928090ff5dc56d4bb63d8d5c68826)`); }
375
+
376
+ const resGetLatestAddr_senderIdentity =
377
+ await getLatestAddrs({ addrs: [senderIdentityTjpAddr, senderIdentityAddr], space });
378
+ if (!resGetLatestAddr_senderIdentity) { throw new Error(`(UNEXPECTED) resGetLatestAddr_senderIdentity for prevSessionIdentityAddr in space (${space.ib}) falsy? (E: 2e4ae8083b6fb7cbb8fae2a519062926)`); }
379
+ if (!resGetLatestAddr_senderIdentity.data) { throw new Error(`(UNEXPECTED) resGetLatestAddr_senderIdentity.data for prevSessionIdentityAddr in space (${space.ib}) falsy? (E: 2e231850c2a898cc282b4b2841056826)`); }
380
+ if (!resGetLatestAddr_senderIdentity.data.success) {
381
+ throw new Error(`(UNEXPECTED) resGetLatestAddr_senderIdentity.data.success falsy? (E: e93508f03e0475925875b00746ffd826)`);
382
+ }
383
+ const { latestAddrsMap: latestAddrsMap_senderIdentity } = resGetLatestAddr_senderIdentity.data;
384
+ if (!latestAddrsMap_senderIdentity) { throw new Error(`(UNEXPECTED) resGetLatestAddr_senderIdentity.data.latestAddrsMap falsy? (E: 87a91e3f9968ad9ba79cdfe8cd878326)`); }
385
+ if (Object.keys(latestAddrsMap_senderIdentity).length !== 2 && senderIdentityTjpAddr !== senderIdentityAddr) {
386
+ throw new Error(`(UNEXPECTED) Object.keys(latestAddrsMap_senderIdentity).length !== 2 && senderIdentityTjpAddr !== senderIdentityAddr? (E: fe46bd584853d8e1e8e2d11f52012826)`);
387
+ }
388
+ // these two should be the same, we're just confirming that they're both
389
+ // on the same timeline.
390
+ const senderIdentityTjpAddr_latest = latestAddrsMap_senderIdentity[senderIdentityTjpAddr];
391
+ const senderIdentityAddr_latest = latestAddrsMap_senderIdentity[senderIdentityAddr];
392
+ if (!senderIdentityAddr_latest) { throw new Error(`(UNEXPECTED) senderIdentityAddr_latest falsy? (E: e151798ae2e9241578d09948937c4b26)`); }
393
+ if (senderIdentityTjpAddr_latest !== senderIdentityAddr_latest) {
394
+ throw new Error(`senderIdentityTjpAddr_latest !== senderIdentityAddr_latest (E: 52478a1053589e72665031a853cc1826)`);
395
+ }
396
+
397
+ // ATOW, we're only allowing a single sync to occur on an identity at
398
+ // any given time (which makes sense). We also are assuming that the
399
+ // sender identity is not doing anything ELSE at this time, which in
400
+ // the (far) future may change. So the user couldn't edit their primary
401
+ // identity's profile, description, etc., while the sync is in progress.
402
+ // This may ultimately be asking too much though. But for now, we'll
403
+ // enforce that the latest senderIdentity addr should be that addr that
404
+ // authorized the session keystone.
405
+ // if (senderIdentityAddr_latest !== senderIdentityAddr) {
406
+ // errors.push(`The senderIdentityAddr referenced in the session keystone's genesis frameDetails (${senderIdentityAddr}) is DIFFERENT than the latest sender identity addr (${senderIdentityAddr_latest}). This means that the sender has done something besides the current sync operation, which isn't supported at this time. (E: a02598271b48cbeb584e45abde121826)`);
407
+ // return errors; /* <<<< returns early */
408
+ // }
409
+
410
+ // now we confirm the other direction: sender identity should have been
411
+ // signed with "sync" verb and targeting the sessionIdentity tjp
412
+ // (genesis) frame.
413
+ const resGetSenderIdentity_latest = await getFromSpace({
414
+ addr: senderIdentityAddr_latest,
415
+ space,
416
+ });
417
+ if (!resGetSenderIdentity_latest.success || resGetSenderIdentity_latest.ibGibs?.length !== 1) {
418
+ errors.push(`could not fetch latest sender identity ${prevSessionIdentityAddr} from space (${space.ib}). (E: 3565ff0ed458f5a2384c40b16e849826)`);
419
+ return errors; /* <<<< returns early */
420
+ }
421
+ const senderIdentity_latest = resGetSenderIdentity_latest.ibGibs[0] as KeystoneIbGib_V1;
422
+ if (!senderIdentity_latest.data.proofs) {
423
+ errors.push(`Invalid sender identity. Proofs empty/falsy. (E: ebf488853061614d2b5b137828119526)`);
424
+ return errors; /* <<<< returns early */
425
+ }
426
+ const syncClaim = senderIdentity_latest.data.proofs.find(p =>
427
+ p.claim.verb === KEYSTONE_VERB_SYNC
428
+ )?.claim;
429
+ if (!syncClaim) {
430
+ errors.push(`Most recent senderIdentity has no proof whose claim.verb === ${KEYSTONE_VERB_SYNC}. (E: b0f488ecccbbfe43d9a0b7c8a29d7826)`);
431
+ return errors; /* <<<< returns early */
432
+ }
433
+ if (syncClaim.target !== sessionIdentityTjpAddr) {
434
+ errors.push(`Most recent sender identity claim has claim.verb === ${KEYSTONE_VERB_SYNC} but DOES NOT target expected session identity addr ${prevSessionIdentityAddr}. (E: 3e7f18d99848969be8586423d5ccb826)`);
435
+ return errors;
436
+ }
437
+
438
+ const keystoneSvc = new KeystoneService_V1();
439
+ const transitionErrors = await keystoneSvc.validate({
440
+ currentIbGib: currSessionIdentity,
441
+ prevIbGib: prevSessionIdentity,
442
+ });
443
+ if (transitionErrors.length > 0) {
444
+ errors.push(`Invalid session identity transition: ${transitionErrors.join(', ')} (E: da1c81c6d3c86aec3254f48fe7514226)`);
445
+ }
446
+
447
+ // we have a valid keystone evolution/signing, but was it specifically
448
+ // for this incoming context? verify that the signing targets context.
449
+ const contextAddr = getIbGibAddr({ ibGib: context });
450
+ const targetsThisContext =
451
+ currSessionIdentity.data?.proofs?.some(p => p.claim.target === contextAddr);
452
+ if (!targetsThisContext) {
453
+ errors.push(`Session identity signature does not target current context ibgib (${contextAddr}). (E: acae68938c287178c878d1b88bebb826)`);
454
+ }
455
+
456
+ return errors;
227
457
  } catch (error) {
228
- console.error(`${lc} ${extractErrorMsg(error)}`);
229
- throw error;
458
+ const emsg = `${lc} ${extractErrorMsg(error)}`;
459
+ console.error(emsg);
460
+ return [`authentication produced an error: ${emsg} (E: 45e014b82af81993d936611ca6fc4d26)`];
230
461
  } finally {
231
462
  if (logalot) { console.log(`${lc} complete.`); }
232
463
  }
233
- }
464
+ }
@@ -8,6 +8,7 @@ import { IbGibData_V1, IbGibRel8ns_V1, IbGib_V1 } from '@ibgib/ts-gib/dist/V1/ty
8
8
  import { SYNC_SAGA_PAYLOAD_ADDRS_DOMAIN } from '../sync-constants.mjs';
9
9
  import { SyncIbGib_V1 } from '../sync-types.mjs';
10
10
  import { SYNC_SAGA_CONTEXT_ATOM } from './sync-saga-context-constants.mjs';
11
+ import { KeystoneIbGib_V1 } from '../../keystone/keystone-types.mjs';
11
12
 
12
13
  export interface SyncSagaContextIb_V1 {
13
14
  atom: typeof SYNC_SAGA_CONTEXT_ATOM;
@@ -48,6 +49,10 @@ export interface SyncSagaContextRel8ns_V1 extends IbGibRel8ns_V1 {
48
49
  */
49
50
  sagaFrame: IbGibAddr[];
50
51
 
52
+ /**
53
+ * Ephemeral session identity genesis address to confirm S_genesis.
54
+ */
55
+ sessionIdentity?: IbGibAddr[];
51
56
  }
52
57
 
53
58
  /**
@@ -70,4 +75,13 @@ export interface SyncSagaContextIbGib_V1 extends IbGib_V1<SyncSagaContextData_V1
70
75
  */
71
76
  sagaFrame: SyncIbGib_V1;
72
77
 
78
+ /**
79
+ * Evolved session identity frame signed by Alice targeting this context.
80
+ */
81
+ signedSessionIdentity?: KeystoneIbGib_V1;
82
+
83
+ /**
84
+ * The actual message stone (the sync saga message containing state/stage).
85
+ */
86
+ sagaFrameMsg: IbGib_V1;
73
87
  }