@ibgib/core-gib 0.1.44 → 0.1.47

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 (31) hide show
  1. package/dist/keystone/keystone-constants.d.mts +9 -0
  2. package/dist/keystone/keystone-constants.d.mts.map +1 -1
  3. package/dist/keystone/keystone-constants.mjs +9 -0
  4. package/dist/keystone/keystone-constants.mjs.map +1 -1
  5. package/dist/keystone/keystone-helpers.d.mts +19 -0
  6. package/dist/keystone/keystone-helpers.d.mts.map +1 -1
  7. package/dist/keystone/keystone-helpers.mjs +292 -4
  8. package/dist/keystone/keystone-helpers.mjs.map +1 -1
  9. package/dist/keystone/keystone-types.d.mts +2 -0
  10. package/dist/keystone/keystone-types.d.mts.map +1 -1
  11. package/dist/sync/sync-peer/sync-peer-v1.d.mts.map +1 -1
  12. package/dist/sync/sync-peer/sync-peer-v1.mjs +12 -3
  13. package/dist/sync/sync-peer/sync-peer-v1.mjs.map +1 -1
  14. package/dist/sync/sync-saga-context/sync-saga-context-helpers.d.mts +0 -38
  15. package/dist/sync/sync-saga-context/sync-saga-context-helpers.d.mts.map +1 -1
  16. package/dist/sync/sync-saga-context/sync-saga-context-helpers.mjs +1 -83
  17. package/dist/sync/sync-saga-context/sync-saga-context-helpers.mjs.map +1 -1
  18. package/dist/sync/sync-saga-context/sync-saga-context-types.d.mts +24 -4
  19. package/dist/sync/sync-saga-context/sync-saga-context-types.d.mts.map +1 -1
  20. package/dist/sync/sync-saga-coordinator.d.mts +14 -2
  21. package/dist/sync/sync-saga-coordinator.d.mts.map +1 -1
  22. package/dist/sync/sync-saga-coordinator.mjs +128 -11
  23. package/dist/sync/sync-saga-coordinator.mjs.map +1 -1
  24. package/package.json +1 -1
  25. package/src/keystone/keystone-constants.mts +9 -0
  26. package/src/keystone/keystone-helpers.mts +320 -5
  27. package/src/keystone/keystone-types.mts +4 -1
  28. package/src/sync/sync-peer/sync-peer-v1.mts +13 -3
  29. package/src/sync/sync-saga-context/sync-saga-context-helpers.mts +3 -107
  30. package/src/sync/sync-saga-context/sync-saga-context-types.mts +25 -4
  31. package/src/sync/sync-saga-coordinator.mts +159 -9
@@ -50,8 +50,8 @@ import { SYNC_SAGA_MSG_ATOM } from "./sync-saga-message/sync-saga-message-consta
50
50
  import { SyncSagaInfo } from "./sync-types.mjs";
51
51
  import { splitPerTjpAndOrDna, getTimelinesGroupedByTjp, isIbGib, getIbGibsFromCache_fallbackToSpaces } from "../common/other/ibgib-helper.mjs";
52
52
  import { SyncPeerWitness } from "./sync-peer/sync-peer-types.mjs";
53
- import { SyncSagaContextIbGib_V1, } from "./sync-saga-context/sync-saga-context-types.mjs";
54
- import { createSyncSagaContext, validateContextAndSagaFrame } from "./sync-saga-context/sync-saga-context-helpers.mjs";
53
+ import { SyncSagaContextData_V1, SyncSagaContextIbGib_V1, SyncSagaContextRel8ns_V1, } from "./sync-saga-context/sync-saga-context-types.mjs";
54
+ import { getSyncSagaContextIb, validateContextAndSagaFrame } from "./sync-saga-context/sync-saga-context-helpers.mjs";
55
55
  import { newupSubject, } from "../common/pubsub/subject/subject-helper.mjs";
56
56
  import { SubjectWitness } from "../common/pubsub/subject/subject-types.mjs";
57
57
  import { getSyncSagaMessageFromFrame } from "./sync-saga-message/sync-saga-message-helpers.mjs";
@@ -64,6 +64,8 @@ import { GRAFT_INFO_REL8N_NAME } from "./graft-info/graft-info-constants.mjs";
64
64
  import { GraftInfoIbGib_V1 } from "./graft-info/graft-info-types.mjs";
65
65
  import { validateIbGibIntrinsically } from "@ibgib/ts-gib/dist/V1/validate-helper.mjs";
66
66
  import { KEYSTONE_VERB_MANAGE } from "../keystone/keystone-constants.mjs";
67
+ import { validateKeystoneGraph } from "../keystone/keystone-helpers.mjs";
68
+ import { SYNC_SAGA_CONTEXT_ATOM } from "./sync-saga-context/sync-saga-context-constants.mjs";
67
69
 
68
70
 
69
71
  const logalot = GLOBAL_LOG_A_LOT;
@@ -87,7 +89,7 @@ export class SyncSagaCoordinator {
87
89
  private lc: string = `[${SyncSagaCoordinator.name}]`;
88
90
 
89
91
  constructor(
90
- private keystone: KeystoneService_V1
92
+ private keystoneSvc: KeystoneService_V1
91
93
  ) {
92
94
 
93
95
  }
@@ -209,6 +211,9 @@ export class SyncSagaCoordinator {
209
211
 
210
212
  if (useSessionIdentity) {
211
213
  if (!identitySecret) { throw new Error(`useSessionIdentity is true, but identitySecret is falsy. Must provide a secret if you want to use a session identity. (E: 81915860c4dd3ea4dfd81825fa74c126)`); }
214
+ // creates the initial session identity (keystone). the flow
215
+ // (i think) will go: evolve saga frame, then sign keystone,
216
+ // then create/send context.
212
217
  sessionIdentity = await this.getSessionIdentity({ sagaId, identitySecret, metaspace, localSpace });
213
218
  }
214
219
 
@@ -322,12 +327,13 @@ export class SyncSagaCoordinator {
322
327
  // create the return context
323
328
  const { frame, payloadIbGibsDomain } = contextResult.nextFrameInfo;
324
329
 
325
- const responseCtx = await createSyncSagaContext({
330
+ const responseCtx = await this.createSyncSagaContext({
326
331
  sagaFrame: frame,
327
332
  localSpace: mySpace,
328
333
  payloadIbGibsDomain,
329
- // todo: we need to thoroughly go through the identity per each step after getting basic merging
330
- sessionKeystones: identity ? [identity] : undefined, // ??
334
+ sessionKeystone: identity,
335
+ sessionSecret: identitySecret,
336
+ metaspace,
331
337
  });
332
338
 
333
339
  const immediateValidationErrors = await validateContextAndSagaFrame({
@@ -461,7 +467,7 @@ export class SyncSagaCoordinator {
461
467
  // Generate keystone with DUAL pools. We have to first genesis with
462
468
  // one and then add the other, because we have two distinct
463
469
  // secrets.
464
- const sessionIdentity = await this.keystone.genesis({
470
+ const sessionIdentity = await this.keystoneSvc.genesis({
465
471
  masterSecret: sessionSecret,
466
472
  configs: [primaryPoolConfig],
467
473
  metaspace,
@@ -569,9 +575,10 @@ export class SyncSagaCoordinator {
569
575
  // #endregion set up peer observable for any domainPayloadsMap
570
576
 
571
577
  // ...create/compose the Request Context itself...
572
- const requestCtx = await createSyncSagaContext({
578
+ const requestCtx = await this.createSyncSagaContext({
573
579
  sagaFrame: currentFrame,
574
- sessionKeystones: sessionIdentity ? [sessionIdentity] : undefined,
580
+ sessionKeystone: sessionIdentity,
581
+ sessionSecret: identitySecret,
575
582
  /**
576
583
  * init frame: empty
577
584
  * ack frame: possible push offers
@@ -580,6 +587,7 @@ export class SyncSagaCoordinator {
580
587
  */
581
588
  payloadIbGibsDomain: nextDomainIbGibs,
582
589
  localSpace,
590
+ metaspace,
583
591
  });
584
592
 
585
593
  // #region Log what we're sending
@@ -707,6 +715,148 @@ export class SyncSagaCoordinator {
707
715
  }
708
716
  }
709
717
 
718
+
719
+ /**
720
+ * Creates new SyncSagaContext stone. Puts/registers in {@link localSpace}
721
+ * immediately after creation.
722
+ *
723
+ * @returns The context ibGib.
724
+ *
725
+ * ## notes
726
+ *
727
+ * the other ibgibs that are related to this context stone should already be
728
+ * put/registered in {@link localSpace}.
729
+ */
730
+ private async createSyncSagaContext({
731
+ sagaFrame,
732
+ sessionKeystone,
733
+ sessionSecret,
734
+ payloadIbGibsDomain,
735
+ metaspace,
736
+ localSpace,
737
+ }: {
738
+ /**
739
+ * The main saga frame (Init, Ack, etc.).
740
+ */
741
+ sagaFrame: SyncIbGib_V1;
742
+ /**
743
+ * Session identity keystone.
744
+ */
745
+ sessionKeystone: KeystoneIbGib_V1 | undefined;
746
+ /**
747
+ * If using session ({@link sessionKeystone} is truthy), this must be
748
+ * provided in order to sign it.
749
+ */
750
+ sessionSecret: string | undefined;
751
+ /**
752
+ * Domain payload ibgibs when the sync saga frame includes actual domain
753
+ * payloads to send, e.g., in a Delta frame.
754
+ */
755
+ payloadIbGibsDomain?: IbGib_V1[];
756
+ /**
757
+ * reference to the current metaspace
758
+ */
759
+ metaspace: MetaspaceService,
760
+ /**
761
+ * we persist the context in the local/sender space (relative to our
762
+ * execution POV) right when we create it.
763
+ */
764
+ localSpace: IbGibSpaceAny;
765
+ }): Promise<SyncSagaContextIbGib_V1> {
766
+ const lc = `[${this.createSyncSagaContext.name}]`;
767
+ try {
768
+ if (logalot) { console.log(`${lc} starting... (I: 6b87bee313e811d1d2fc90e87fbec826)`); }
769
+
770
+ // #region sanity/validation
771
+ if (!sagaFrame.data) { throw new Error(`(UNEXPECTED) sagaFrame.data falsy? (E: 04c49b4cccba6842a8b52e4c6f570726)`); }
772
+ if (!sagaFrame.data.n && sagaFrame.data.n !== 0) { throw new Error(`(UNEXPECTED) sagaFrame.data.n falsy and not 0? (E: 45b508da64a8b28428b11765d684b826)`); }
773
+ if (sessionKeystone && !sessionSecret) { throw new Error(`(UNEXPECTED) sessionKeystone truthy but sessionSecret falsy? (E: 705ecc25038b12df0e94c90c5561e426)`); }
774
+ // #endregion sanity/validation
775
+
776
+ const date = new Date();
777
+ const timestamp = getTimestamp(date);
778
+ const timestampMs = date.getMilliseconds();
779
+
780
+ const data: SyncSagaContextData_V1 = {
781
+ timestamp,
782
+ timestampMs,
783
+ sagaN: sagaFrame.data.n,
784
+ };
785
+
786
+ // Domain Payloads
787
+ const payloadAddrsDomain = payloadIbGibsDomain ?
788
+ payloadIbGibsDomain?.map(x => getIbGibAddr({ ibGib: x })) :
789
+ undefined;
790
+ if (payloadAddrsDomain && payloadAddrsDomain.length > 0) {
791
+ data[SYNC_SAGA_PAYLOAD_ADDRS_DOMAIN] = payloadAddrsDomain;
792
+ }
793
+
794
+ // rel8ns should always have saga frame, sometimes have keystone
795
+ const rel8ns: SyncSagaContextRel8ns_V1 = {
796
+ sagaFrame: [getIbGibAddr({ ibGib: sagaFrame })],
797
+ };
798
+ if (sessionKeystone) {
799
+ const keystoneErrors = await validateKeystoneGraph({
800
+ keystoneIbGib: sessionKeystone,
801
+ space: localSpace,
802
+ getLatest: true,
803
+ invalidIfMoreRecentKeystoneFoundInSpace: true,
804
+ });
805
+ if (keystoneErrors.length > 0) {
806
+ throw new Error(`invalid sessionKeystone. errors: ${keystoneErrors} (E: 3881b8caf2d803767a331e1141e84826)`);
807
+ }
808
+ // this addr is BEFORE we sign. So each context ibgib itself will
809
+ // point to the frame of the keystone just before that keystone
810
+ // signs with this context as its target.
811
+ rel8ns.sessionKeystone = [getIbGibAddr({ ibGib: sessionKeystone })];
812
+ }
813
+
814
+ // Generate standard ib
815
+ const ib = await getSyncSagaContextIb({ data });
816
+
817
+ const contextIbGib = await Factory_V1.stone<SyncSagaContextData_V1, SyncSagaContextRel8ns_V1>({
818
+ parentPrimitiveIb: SYNC_SAGA_CONTEXT_ATOM,
819
+ ib,
820
+ data,
821
+ rel8ns,
822
+ }) as SyncSagaContextIbGib_V1;
823
+
824
+ // put/register immediately. Note that contextIbGib at this point is
825
+ // pure DTO, i.e., only ib, gib, data, rel8ns props.
826
+ await putInSpace({ ibGib: contextIbGib, space: localSpace, });
827
+ await registerNewIbGib({
828
+ ibGib: contextIbGib,
829
+ space: localSpace,
830
+ fnBroadcast: undefined,
831
+ });
832
+
833
+ // Attach actual ibgibs for transport (not pure DTO now)
834
+ contextIbGib.sagaFrame = sagaFrame;
835
+ if (payloadIbGibsDomain && payloadIbGibsDomain.length > 0) {
836
+ contextIbGib.payloadIbGibsDomain = payloadIbGibsDomain;
837
+ }
838
+
839
+ if (sessionKeystone) {
840
+ if (!sessionSecret) { throw new Error(`(UNEXPECTED) sessionKeystone truthy but sessionSecret falsy? we should have thrown before now (E: a2b0517a37b559543968b888f2067826)`); }
841
+ const contextAddr = getIbGibAddr({ ibGib: contextIbGib });
842
+ contextIbGib.signedSessionKeystone = await this.keystoneSvc.sign({
843
+ latestKeystone: sessionKeystone,
844
+ claim: { target: contextAddr, }, // verb?
845
+ space: localSpace,
846
+ masterSecret: sessionSecret,
847
+ metaspace,
848
+ });
849
+ }
850
+
851
+ return contextIbGib;
852
+ } catch (error) {
853
+ console.error(`${lc} ${extractErrorMsg(error)}`);
854
+ throw error;
855
+ } finally {
856
+ if (logalot) { console.log(`${lc} complete.`); }
857
+ }
858
+ }
859
+
710
860
  /**
711
861
  * Helper to get Knowledge Map for specific domain ibGibs or TJPs.
712
862
  * Useful for testing and external validation.