@ibgib/ts-gib 0.4.9 → 0.5.0

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 (66) hide show
  1. package/.vscode/settings.json +3 -1
  2. package/CHANGELOG.md +15 -0
  3. package/README.md +30 -5
  4. package/dist/V1/constants.d.mts +30 -1
  5. package/dist/V1/constants.d.mts.map +1 -1
  6. package/dist/V1/constants.mjs +30 -1
  7. package/dist/V1/constants.mjs.map +1 -1
  8. package/dist/V1/factory.d.mts +17 -2
  9. package/dist/V1/factory.d.mts.map +1 -1
  10. package/dist/V1/factory.mjs +15 -0
  11. package/dist/V1/factory.mjs.map +1 -1
  12. package/dist/V1/sha256v1.d.mts.map +1 -1
  13. package/dist/V1/sha256v1.mjs +1 -1
  14. package/dist/V1/sha256v1.mjs.map +1 -1
  15. package/dist/V1/transforms/fork.d.mts.map +1 -1
  16. package/dist/V1/transforms/fork.mjs +2 -1
  17. package/dist/V1/transforms/fork.mjs.map +1 -1
  18. package/dist/V1/transforms/mut8.d.mts.map +1 -1
  19. package/dist/V1/transforms/mut8.mjs +2 -1
  20. package/dist/V1/transforms/mut8.mjs.map +1 -1
  21. package/dist/V1/transforms/rel8.d.mts.map +1 -1
  22. package/dist/V1/transforms/rel8.mjs +2 -1
  23. package/dist/V1/transforms/rel8.mjs.map +1 -1
  24. package/dist/V1/transforms/transform-helper.d.mts.map +1 -1
  25. package/dist/V1/transforms/transform-helper.mjs +2 -1
  26. package/dist/V1/transforms/transform-helper.mjs.map +1 -1
  27. package/dist/V1/v1-helper.d.mts +38 -0
  28. package/dist/V1/v1-helper.d.mts.map +1 -0
  29. package/dist/V1/v1-helper.mjs +87 -0
  30. package/dist/V1/v1-helper.mjs.map +1 -0
  31. package/dist/V1/validate-helper.d.mts +83 -0
  32. package/dist/V1/validate-helper.d.mts.map +1 -0
  33. package/dist/V1/validate-helper.mjs +227 -0
  34. package/dist/V1/validate-helper.mjs.map +1 -0
  35. package/dist/helper.d.mts +0 -54
  36. package/dist/helper.d.mts.map +1 -1
  37. package/dist/helper.mjs +0 -114
  38. package/dist/helper.mjs.map +1 -1
  39. package/dist/types.d.mts +39 -8
  40. package/dist/types.d.mts.map +1 -1
  41. package/dist/types.mjs +31 -0
  42. package/dist/types.mjs.map +1 -1
  43. package/jasmine-browser.json +2 -2
  44. package/package.json +5 -1
  45. package/src/V1/constants.mts +31 -3
  46. package/src/V1/factory.mts +18 -2
  47. package/src/V1/factory.spec.mts +2 -1
  48. package/src/V1/sha256v1.mts +5 -5
  49. package/src/V1/transforms/fork.mts +2 -1
  50. package/src/V1/transforms/fork.spec.mts +2 -1
  51. package/src/V1/transforms/mut8.mts +2 -1
  52. package/src/V1/transforms/mut8.spec.mts +4 -3
  53. package/src/V1/transforms/rel8.mts +2 -1
  54. package/src/V1/transforms/rel8.spec.mts +2 -1
  55. package/src/V1/transforms/transform-helper.mts +3 -2
  56. package/src/V1/transforms/transform-helper.spec.mts +6 -5
  57. package/src/V1/v1-helper.mts +90 -0
  58. package/src/V1/validate-helper.mts +286 -0
  59. package/src/helper.mts +0 -116
  60. package/src/helper.spec.mts +47 -115
  61. package/src/types.mts +40 -9
  62. package/dist/index.cjs +0 -4
  63. package/dist/index.cjs.map +0 -1
  64. package/dist/index.d.cts +0 -4
  65. package/dist/index.d.cts.map +0 -1
  66. package/src/index.cts +0 -3
@@ -13,6 +13,6 @@
13
13
  "random": true
14
14
  },
15
15
  "browser": {
16
- "name": "firefox"
16
+ "name": "headlessChrome"
17
17
  }
18
- }
18
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ibgib/ts-gib",
3
- "version": "0.4.9",
3
+ "version": "0.5.0",
4
4
  "description": "ibgib library with low-level graphing-related substrate functionality, e.g. creating raw ibgibs and transformations. node19+ needed for heavily-used webcrypto hashing isomorphically consumed in node and browsers (apps).",
5
5
  "repository": {
6
6
  "url": "https://gitlab.com/ibgib/ts-gib",
@@ -32,6 +32,7 @@
32
32
  "test:browser:serve:nobuild": "npx jasmine-browser-runner serve --config=jasmine-browser.json ",
33
33
  "man:test:browser:serve:nobuild": "use this if you are troubleshooting existing dist output and don't want to overwrite those files. (and you're debugging in the browser)",
34
34
  "pack": "npm pack --pack-destination=\"./published\"",
35
+ "prepare:publish:minor": "npm run clean && npm version minor && npm run build && npm run pack",
35
36
  "prepare:publish": "npm run clean && npm version patch && npm run build && npm run pack",
36
37
  "man:prepare:publish": "use this to patch > build > pack for publishing to npm repo"
37
38
  },
@@ -57,5 +58,8 @@
57
58
  },
58
59
  "publishConfig": {
59
60
  "access": "public"
61
+ },
62
+ "dependencies": {
63
+ "@ibgib/helper-gib": "^0.0.2"
60
64
  }
61
65
  }
@@ -1,8 +1,27 @@
1
1
  import { IbGib_V1 } from './types.mjs';
2
2
 
3
+ /**
4
+ * This is essentially an alias of the string literal `'ib'`
5
+ */
3
6
  export const IB = 'ib';
7
+ /**
8
+ * This is essentially an alias of the string literal `'gib'`
9
+ *
10
+ * When you have a primitive ibgib, this should be the `ibGib.gib` value, as
11
+ * there is no hashing metadata for primitives.
12
+ */
4
13
  export const GIB = 'gib';
14
+ /**
15
+ * This is the root ibgib from which all ibgib are ultimately forked.
16
+ * It has 0 and infinite data, 0 and infinite rel8ns. Its ib and gib
17
+ * are simply 'ib' and 'gib'.
18
+ */
5
19
  export const ROOT: IbGib_V1 = { ib: IB, gib: GIB, }
20
+ /**
21
+ * The default delimiter is the caret (^) symbol. This is derived from
22
+ * taking an ib^gib address and turning it into an ibGib object (note
23
+ * the capitalized "G").
24
+ */
6
25
  export const IBGIB_DELIMITER = '^';
7
26
  /**
8
27
  * Gib is often just a single hash for a single ib^gib record.
@@ -15,9 +34,18 @@ export const IBGIB_DELIMITER = '^';
15
34
  * @example "comment abc^TJPHASH123", "comment abc^TJPHASH123.THISRECORDHASH456"
16
35
  */
17
36
  export const GIB_DELIMITER = '.';
37
+ /**
38
+ * This is the address of the ROOT ibgib with the default delimiter (caret ^).
39
+ */
18
40
  export const ROOT_ADDR = 'ib^gib'; // `${IB}${IBGIB_DELIMITER}${GIB}`;
19
-
20
41
  /**
21
- * Some rel8ns should not be able to be renamed or removed.
42
+ * Some rel8ns should not be able to be renamed or removed, as these have
43
+ * "special" semantic meaning in the low-level graphing protocol.
44
+ *
45
+ * ## notes
46
+ *
47
+ * * Of course, this can be forged or tinkered with, and that ultimately is what
48
+ * consensus would be utilized for.
49
+ * * protocols built on top of this may also have reserved rel8n/data key names
22
50
  */
23
- export const FORBIDDEN_ADD_RENAME_REMOVE_REL8N_NAMES = ['past', 'ancestor', 'dna', 'tjp'];
51
+ export const FORBIDDEN_ADD_RENAME_REMOVE_REL8N_NAMES = ['past', 'ancestor', 'dna', 'tjp'];
@@ -9,6 +9,12 @@ export class Factory_V1 {
9
9
  static root() {
10
10
  return Factory_V1.primitive({ ib: IB });
11
11
  }
12
+
13
+ /**
14
+ * Returns an ibGib primitive with the given `ib`, e.g. "{ib: '7', gib: 'gib'}".
15
+ *
16
+ * @returns a single primitive ibGib
17
+ */
12
18
  static primitive({
13
19
  ib
14
20
  }: {
@@ -17,14 +23,24 @@ export class Factory_V1 {
17
23
  return { ib, gib: GIB };
18
24
  }
19
25
 
26
+ /**
27
+ * generates multiple primitive ibgibs from the given `ibs` array.
28
+ * @returns array of generated ibgibs.
29
+ */
20
30
  static primitives({
21
31
  ibs
22
32
  }: {
23
33
  ibs: Ib[]
24
- }) {
34
+ }): IbGib_V1[] {
25
35
  return ibs.map(ib => Factory_V1.primitive({ ib }));
26
36
  }
27
37
 
38
+ /**
39
+ * Takes the incoming `parentIbGib` and forks it. Then applies the
40
+ * `data` and/or `rel8ns` if any, depending on the other config params.
41
+ *
42
+ * @returns The entire transform result object that includes the newly produced ibGib as well as any derivative/intermediate ibgibs.
43
+ */
28
44
  static async firstGen<TData = any>({
29
45
  ib = IB,
30
46
  parentIbGib = Factory_V1.root(),
@@ -45,7 +61,7 @@ export class Factory_V1 {
45
61
  linkedRel8ns?: string[],
46
62
  noTimestamp?: boolean,
47
63
  nCounter?: boolean,
48
- }) {
64
+ }): Promise<TransformResult<IbGib_V1>> {
49
65
  const lc = `[firstGen]`;
50
66
  /** * Multiple transform steps will create multiple results. */
51
67
  const interimResults: TransformResult<IbGib_V1>[] = [];
@@ -2,7 +2,8 @@
2
2
  import { IbGib_V1, IbGibRel8ns_V1, Rel8n } from './types.mjs';
3
3
  import { fork } from './transforms/fork.mjs';
4
4
  import { TransformOpts_Fork, IbGibAddr, Ib, TransformResult, IbGibRel8ns } from '../types.mjs';
5
- import { pretty, clone, delay, getIbGibAddr } from '../helper.mjs';
5
+ import { getIbGibAddr, getIbAndGib } from '../helper.mjs';
6
+ import { pretty, clone, delay, } from '@ibgib/helper-gib';
6
7
  import { ROOT, ROOT_ADDR } from './constants.mjs';
7
8
  import { Factory_V1 as factory } from './factory.mjs';
8
9
  import { mut8 } from './transforms/mut8.mjs';
@@ -1,9 +1,9 @@
1
- let crypto: any = globalThis.crypto;
2
- let { subtle } = crypto;
3
-
1
+ import { extractErrorMsg } from "@ibgib/helper-gib";
4
2
  import { IbGib_V1, IbGibData_V1, IbGibRel8ns_V1 } from "./types.mjs";
5
3
  import { Ib } from "../types.mjs";
6
- import { extractErrorMsg } from "../helper.mjs";
4
+
5
+ let crypto: any = globalThis.crypto;
6
+ let { subtle } = crypto;
7
7
 
8
8
  /**
9
9
  * Performs the gib hash like V1
@@ -82,4 +82,4 @@ export async function hashToHexCopy(
82
82
  console.error(extractErrorMsg(e.message));
83
83
  return undefined;
84
84
  }
85
- };
85
+ };
@@ -1,4 +1,5 @@
1
- import { getIbGibAddr, clone, getUUID, getTimestamp } from '../../helper.mjs';
1
+ import { getIbGibAddr } from '../../helper.mjs';
2
+ import { clone, getUUID, getTimestamp } from '@ibgib/helper-gib';
2
3
  import { sha256v1 } from '../sha256v1.mjs';
3
4
  import { IbGib_V1, IbGibData_V1, IbGibRel8ns_V1, Rel8n } from '../types.mjs';
4
5
  import { TransformOpts_Fork, TransformResult } from '../../types.mjs';
@@ -19,7 +19,8 @@
19
19
  import { IbGib_V1, IbGibRel8ns_V1, Rel8n } from '../types.mjs';
20
20
  import { fork } from './fork.mjs';
21
21
  import { TransformOpts_Fork, IbGibAddr, Ib, TransformResult } from '../../types.mjs';
22
- import { pretty, clone, delay, getIbGibAddr } from '../../helper.mjs';
22
+ import { getIbGibAddr } from '../../helper.mjs';
23
+ import { pretty, clone, delay, } from '@ibgib/helper-gib';
23
24
  import { ROOT, ROOT_ADDR } from '../constants.mjs';
24
25
  import { mut8 } from './mut8.mjs';
25
26
  import { getGib } from './transform-helper.mjs';
@@ -1,4 +1,5 @@
1
- import { getIbGibAddr, clone, getTimestamp } from '../../helper.mjs';
1
+ import { getIbGibAddr } from '../../helper.mjs';
2
+ import { clone, getTimestamp, } from '@ibgib/helper-gib';
2
3
  import { IbGib_V1, IbGibData_V1, IbGibRel8ns_V1, Rel8n } from '../types.mjs';
3
4
  import { TransformOpts_Mut8, TransformResult } from '../../types.mjs';
4
5
  import { IBGIB_DELIMITER, } from '../constants.mjs';
@@ -7,11 +7,12 @@
7
7
  * going.
8
8
  */
9
9
 
10
- import { IbGib_V1, IbGibRel8ns_V1, Rel8n } from '../types.mjs';
10
+ import { IbGib_V1, IbGibRel8ns_V1 } from '../types.mjs';
11
11
  import { TransformOpts_Mut8, IbGibAddr } from '../../types.mjs';
12
- import { pretty, clone, delay, getIbGibAddr, getIbAndGib, } from '../../helper.mjs';
12
+ import { getIbGibAddr, getIbAndGib, } from '../../helper.mjs';
13
+ import { clone, delay, } from '@ibgib/helper-gib';
13
14
  import { sha256v1, } from '../sha256v1.mjs';
14
- import { ROOT, ROOT_ADDR, GIB_DELIMITER } from '../constants.mjs';
15
+ import { ROOT } from '../constants.mjs';
15
16
  import { fork } from './fork.mjs';
16
17
  import { mut8 } from './mut8.mjs';
17
18
  import { Factory_V1 as factory } from '../factory.mjs';
@@ -1,4 +1,5 @@
1
- import { getIbGibAddr, clone, getTimestamp } from '../../helper.mjs';
1
+ import { getIbGibAddr, getIbAndGib } from '../../helper.mjs';
2
+ import { pretty, clone, delay, getTimestamp, } from '@ibgib/helper-gib';
2
3
  import { sha256v1 } from '../sha256v1.mjs';
3
4
  import { IbGib_V1, IbGibData_V1, IbGibRel8ns_V1, Rel8n } from '../types.mjs';
4
5
  import { TransformOpts_Rel8, TransformResult, IbGibAddr } from '../../types.mjs';
@@ -9,7 +9,8 @@
9
9
 
10
10
  import { IbGib_V1, IbGibRel8ns_V1, Rel8n } from '../types.mjs';
11
11
  import { TransformOpts_Rel8, IbGibAddr, IbGibRel8ns } from '../../types.mjs';
12
- import { pretty, clone, delay, getIbGibAddr, getIbAndGib } from '../../helper.mjs';
12
+ import { getIbGibAddr, getIbAndGib } from '../../helper.mjs';
13
+ import { pretty, clone, delay, } from '@ibgib/helper-gib';
13
14
  import { ROOT, ROOT_ADDR } from '../constants.mjs';
14
15
  import { fork } from './fork.mjs';
15
16
  // import { mut8 } from './mut8.mjs';
@@ -1,5 +1,6 @@
1
- import { TransformOpts, IbGib, Gib, Ib, IbGibAddr, } from "../../types.mjs";
2
- import { clone, getIbGibAddr, getIbAndGib, HashAlgorithm, extractErrorMsg, } from "../../helper.mjs";
1
+ import { clone, extractErrorMsg, } from "@ibgib/helper-gib";
2
+ import { TransformOpts, IbGib, Gib, IbGibAddr, } from "../../types.mjs";
3
+ import { getIbGibAddr, getIbAndGib, } from "../../helper.mjs";
3
4
  import { sha256v1, } from "../sha256v1.mjs";
4
5
  import { GibInfo, IbGib_V1, } from "../types.mjs";
5
6
  import { IBGIB_DELIMITER, GIB, GIB_DELIMITER } from "../constants.mjs";
@@ -3,13 +3,14 @@
3
3
  */
4
4
 
5
5
 
6
- import { IbGib_V1, IbGibRel8ns_V1, Rel8n } from '../types.mjs';
7
- import { TransformOpts_Rel8, IbGibAddr, IbGibRel8ns } from '../../types.mjs';
8
- import { pretty, clone, delay, getIbGibAddr } from '../../helper.mjs';
9
- import { ROOT, ROOT_ADDR } from '../constants.mjs';
6
+ // import { IbGib_V1, IbGibRel8ns_V1, Rel8n } from '../types.mjs';
7
+ // import { TransformOpts_Rel8, IbGibAddr, IbGibRel8ns } from '../../types.mjs';
8
+ // import { getIbGibAddr, getIbAndGib } from '../../helper.mjs';
9
+ // import { pretty, clone, delay, } from '@ibgib/helper-gib';
10
+ // import { ROOT, ROOT_ADDR } from '../constants.mjs';
10
11
  import { fork } from './fork.mjs';
11
12
  // import { mut8 } from './mut8.mjs';
12
- import { rel8 } from './rel8.mjs';
13
+ // import { rel8 } from './rel8.mjs';
13
14
  import { Factory_V1 as factory } from '../factory.mjs';
14
15
  import { isDna } from './transform-helper.mjs';
15
16
 
@@ -0,0 +1,90 @@
1
+ import { getIbGibAddr, getIbAndGib } from '../helper.mjs';
2
+ import { pretty, clone, delay, } from '@ibgib/helper-gib';
3
+ import * as cTsGib from './constants.mjs';
4
+ import { getGibInfo } from './transforms/transform-helper.mjs';
5
+ import { IbGibRel8ns_V1, IbGib_V1 } from "./types.mjs";
6
+
7
+ /**
8
+ * Helper function that checks the given `ibGib` to see if it
9
+ * either has a tjp or is a tjp itself.
10
+ *
11
+ * ## notes
12
+ *
13
+ * Only unique ibGibs are meant to have tjps, or rather, if an
14
+ * ibGib timeline is expected to be unique over "time", then the
15
+ * tjp is an extremely convenient mechanism that provides a
16
+ * "name" for that timeline.
17
+ *
18
+ * Otherwise, if they are not unique, then successive "different"
19
+ * timelines cannot be easily referenced by their first unique
20
+ * frame in time, making it much harder to pub/sub updates among
21
+ * other things. (If there are no unique frames, then they are
22
+ * the same ibGib.)
23
+ *
24
+ * ## tjp = temporal junction point
25
+ *
26
+ * I've written elsewhere on this as well. Refer to B2tF2.
27
+ *
28
+ * @returns true if the ibGib has/is a tjp, else false
29
+ */
30
+ export function hasTjp({ ibGib }: { ibGib: IbGib_V1 }): boolean {
31
+ const lc = `[${hasTjp.name}]`;
32
+
33
+ if (!ibGib) {
34
+ console.warn(`${lc} ibGib falsy. (W: 884178562f5b4f15933ac4d98db74cc6)`);
35
+ return false;
36
+ }
37
+
38
+ if (ibGib.data?.isTjp || ibGib.rel8ns?.tjp?.length! > 0) {
39
+ return true;
40
+ }
41
+
42
+ // dna transforms do not have tjp
43
+ const dnaPrimitives = ['fork^gib', 'mut8^gib', 'rel8^gib'];
44
+ if ((ibGib.rel8ns?.ancestor ?? []).some(x => dnaPrimitives.includes(x))) {
45
+ return false;
46
+ }
47
+
48
+ if (!ibGib.gib) {
49
+ console.warn(`${lc} ibGib.gib falsy. (W: 6400d780822b44d992846f1196509be3)`);
50
+ return false;
51
+ }
52
+ if (ibGib.gib.includes(cTsGib.GIB_DELIMITER)) {
53
+ return true;
54
+ }
55
+
56
+ if (ibGib.gib === cTsGib.GIB) {
57
+ // primitive
58
+ return false;
59
+ }
60
+
61
+ // use more expensive getGibInfo call.
62
+ // could possibly just return false at this point, but since gib info
63
+ // would change if we change our standards for gib, this is nicer.
64
+ const gibInfo = getGibInfo({ ibGibAddr: getIbGibAddr({ ibGib }) });
65
+ return gibInfo.tjpGib ? true : false;
66
+ }
67
+
68
+ /**
69
+ * If you have an ibgib object, it may also contain other properties/functions.
70
+ *
71
+ * This helper function takes the incoming object and returns a copy
72
+ * @param param0
73
+ * @returns
74
+ */
75
+ export function toDto<TData, TRel8ns extends IbGibRel8ns_V1 = IbGibRel8ns_V1>({
76
+ ibGib,
77
+ }: {
78
+ ibGib: IbGib_V1,
79
+ }): IbGib_V1<TData, TRel8ns> {
80
+ const lc = `[${toDto.name}]`;
81
+ if (!ibGib.ib) { console.warn(`${lc} ibGib.ib is falsy. (W: e60e41c2a1fc48268379d88ce13cb77b)`); }
82
+ if (!ibGib.gib) { console.warn(`${lc} ibGib.gib is falsy. (W: fb3889cbf0684ae4ac51e48f28570377)`); }
83
+
84
+ let dtoIbGib: IbGib_V1<TData, TRel8ns> = { ib: (ibGib.ib || '').slice() };
85
+ if (ibGib.gib) { dtoIbGib.gib = ibGib.gib.slice(); };
86
+ if (ibGib.data) { dtoIbGib.data = clone(ibGib.data); }
87
+ if (ibGib.rel8ns) { dtoIbGib.rel8ns = clone(ibGib.rel8ns); }
88
+
89
+ return dtoIbGib;
90
+ }
@@ -0,0 +1,286 @@
1
+ import { Ib, IbGibAddr, } from '../types.mjs';
2
+ import { IbGib_V1, } from './types.mjs';
3
+ import { IBGIB_DELIMITER, GIB, IB, GIB_DELIMITER, } from './constants.mjs';
4
+ import { isPrimitive, getGib, getGibInfo } from './transforms/transform-helper.mjs';
5
+
6
+ // import { hasTjp, toDto } from './ibgib';
7
+
8
+ import { hasTjp, toDto } from './v1-helper.mjs';
9
+ import { HEXADECIMAL_HASH_STRING_REGEXP_32, HEXADECIMAL_HASH_STRING_REGEXP_64 } from '@ibgib/helper-gib';
10
+ import { getIbAndGib, getIbGibAddr } from '../helper.mjs';
11
+
12
+ const logalot = false;
13
+
14
+ /**
15
+ * validates the ibGib's address (`ib` and `gib` properties) and recalculates
16
+ * the `gib` against the `ibGib.gib`.
17
+ *
18
+ * this validates not only that the punctiliar gib hash for this ibgib record
19
+ * hashes to the same value, but it also checks the internal tjp address and
20
+ * ensures that it is the same tjp gib in the gib field.
21
+ *
22
+ * ## notes
23
+ *
24
+ * * By checking the tjp gib is the same in the address as in the tjp rel8n, we
25
+ * are providing (extremely?) good corroboration that the tjp listed in the
26
+ * address is accurate. However, it may still be theoretically possible to
27
+ * forge an ibgib that both hashes to the punctiliar hash and matches up this
28
+ * tjpAddr.gib. This would be AFAICT quite challenging.
29
+ */
30
+ export async function validateIbGibIntrinsically({
31
+ ibGib
32
+ }: {
33
+ ibGib: IbGib_V1
34
+ }): Promise<string[] | null> {
35
+ const lc = `[${validateIbGibIntrinsically.name}]`;
36
+ try {
37
+ let errors: string[] = [];
38
+ if (ibGib) {
39
+ const addr = getIbGibAddr({ ibGib });
40
+ errors = validateIbGibAddr({ addr }) ?? [];
41
+
42
+ if (errors.length > 0) {
43
+ console.error(`${lc} errors found in addr: ${addr}`);
44
+ return errors; // <<<< returns early
45
+ }
46
+
47
+ // rest of the function assumes correctly formatted ib and gib
48
+
49
+ // if it's a primitive, the caller knows (or should know!) there are no
50
+ // metadata guarantees.
51
+ if (isPrimitive({ gib: ibGib.gib })) { return null; }
52
+
53
+ // this validates not only that the punctiliar gib hash for this ibgib record
54
+ // hashes to the same value, but it also checks the internal tjp address and
55
+ // ensures that it is the same tjp gib.
56
+ // not necessary the dto here, but I'm sensitive at this point.
57
+ let gottenGib = await getGib({ ibGib: toDto({ ibGib }), hasTjp: hasTjp({ ibGib }) });
58
+ if (gottenGib !== ibGib.gib) {
59
+ if (ibGib.data?.src && ibGib.data.srcAddr && ibGib.ib === 'rel8') {
60
+ // this is NOT the place to do this, but I'm plodding through trying to think of how to fix it
61
+ // this is what it looks like in data
62
+ // {
63
+ // "rel8nsToAddByAddr":{
64
+ // "comment":["comment 11221^F6B9477984D31FEF5FD25297CA39126A64ADDE2C91C1A2D670CCFE3965DEC4C9.847F0E9A1B2738DC167B32AA9B2D7D9B88B76F21848B1F193E994BB21F90DAEE"]
65
+ // },
66
+ // "dna":true,
67
+ // "nCounter":true,
68
+ // "type":"rel8"
69
+ // }
70
+ delete ibGib.data.src;
71
+ delete ibGib.data.srcAddr;
72
+ gottenGib = await getGib({ ibGib: toDto({ ibGib }), hasTjp: hasTjp({ ibGib }) });
73
+ if (gottenGib !== ibGib.gib) {
74
+ errors.push(`Ibgib invalid intrinsically - gottenGib (${gottenGib}) does not equal ibGib.gib (${ibGib.gib}). (E: 020b71479e944b2198fe436e7e137786)`);
75
+ // } else {
76
+ // debugger;
77
+ }
78
+ } else {
79
+ errors.push(`Ibgib invalid intrinsically - gottenGib (${gottenGib}) does not equal ibGib.gib (${ibGib.gib}). (E: 7416db016878430ca3c5b20697f164ed)`);
80
+ }
81
+ }
82
+
83
+ return errors.length > 0 ? errors : null;
84
+ } else {
85
+ errors.push(`ibGib is itself falsy. (E: 4fb98caf6ed24ef7b35a19cef56e2d7e)`);
86
+ return errors;
87
+ }
88
+
89
+ } catch (error) {
90
+ console.error(`${lc} ${error.message}`);
91
+ throw error;
92
+ }
93
+ }
94
+
95
+ /**
96
+ * Naive synchronous validation for ibgib addresses.
97
+ *
98
+ * @returns error string array if validation errors found, else null
99
+ */
100
+ export function validateIbGibAddr({
101
+ addr,
102
+ delimiter,
103
+ version,
104
+ }: {
105
+ addr: IbGibAddr,
106
+ delimiter?: string,
107
+ version?: string,
108
+ }): string[] | null {
109
+ const lc = `[${validateIbGibAddr.name}]`;
110
+ try {
111
+ let errors: string[] = [];
112
+ if (version) { console.warn(`${lc} version not implemented yet. Ignoring. (W: 2d19db16ec0c4766b5d35248787671f3)`); }
113
+
114
+ // validate as a whole
115
+ if (!addr) {
116
+ errors.push(`addr required. (E: e9a54041aa0b41c1bb2324d9d2d42c7f)`);
117
+ return errors;
118
+ }
119
+ delimiter = delimiter || IBGIB_DELIMITER;
120
+ if (!addr.includes(delimiter)) { errors.push(`No delimiter (${delimiter}) found. (E: 05e28dcb70ff44019edc53ed508bd1e8)`); }
121
+ if (addr.startsWith(delimiter)) { errors.push(`addr starts with delim. (E: d29f808c5a47452f9bb3ea684694c6eb)`); }
122
+
123
+ // validate pieces...
124
+ const { ib, gib } = getIbAndGib({ ibGibAddr: addr, delimiter });
125
+
126
+ // ...ib
127
+ const resValidateIb = validateIb({ ib, ibGibAddrDelimiter: delimiter, version });
128
+ if (resValidateIb) { errors = errors.concat(resValidateIb); }
129
+
130
+ // ...gib
131
+ const resValidateGib = validateGib({ gib, ibGibAddrDelimiter: delimiter, version });
132
+ if (resValidateGib) { errors = errors.concat(resValidateGib); }
133
+
134
+ if (errors.length > 0) {
135
+ if (logalot) { console.log(`${lc} errors.length > 0. errors: ${errors.join('|')} (I: 9c18f993e138f15613e4c6a340d41722)`); }
136
+ }
137
+
138
+ // we're done
139
+ return errors.length > 0 ? errors : null;
140
+ } catch (error) {
141
+ console.error(`${lc} ${error.message}`);
142
+ throw error;
143
+ }
144
+ }
145
+
146
+ /**
147
+ * Naive validation of ib.
148
+ *
149
+ * @returns errors array if found, else null
150
+ */
151
+ export function validateIb({
152
+ ib,
153
+ ibGibAddrDelimiter,
154
+ version,
155
+ }: {
156
+ ib: Ib,
157
+ ibGibAddrDelimiter?: string,
158
+ version?: string,
159
+ }): string[] | null {
160
+ const lc = `[${validateIb.name}]`;
161
+ try {
162
+ const errors: string[] = [];
163
+ if (version) { console.warn(`${lc} version not implemented yet. Ignoring. (W: 71228ba4ed994aaa8149910e295ab087)`); }
164
+
165
+ if (!ib) {
166
+ errors.push(`ib required. (E: a76d06c7b9c24db3a731a91dbe46acd5)`);
167
+ return errors;
168
+ }
169
+
170
+ if (ib === IB) { return null; }
171
+
172
+ ibGibAddrDelimiter = ibGibAddrDelimiter || IBGIB_DELIMITER;
173
+ if (ib.includes(ibGibAddrDelimiter)) { errors.push(`ib contains ibGibAddrDelimiter (${ibGibAddrDelimiter}) (E: 09e61b46c3e84874bc02b6918f1f2c39)`); }
174
+
175
+ return errors.length > 0 ? errors : null;
176
+ } catch (error) {
177
+ console.error(`${lc} ${error.message}`);
178
+ throw error;
179
+ }
180
+ }
181
+
182
+ /**
183
+ * validates a `gib` of some ibGib/ibGibAddr.
184
+ *
185
+ * @returns array of validation error strings (if any) or null
186
+ */
187
+ export function validateGib({
188
+ gib,
189
+ gibDelimiter,
190
+ ibGibAddrDelimiter,
191
+ version,
192
+ }: {
193
+ /**
194
+ * gib to validate.
195
+ *
196
+ * ## notes
197
+ *
198
+ * If the gib has a tjp embedded in it (i.e. the associated ibgib has a
199
+ * tjp), then this will call validation on that tjpGib recursively.
200
+ */
201
+ gib: Ib,
202
+ /**
203
+ * This is a delimiter used with tjpGibs.
204
+ *
205
+ * atow this is a dot (`'.'`).
206
+ *
207
+ * ## notes
208
+ *
209
+ * THIS IS NOT THE SAME THING AS THE `ibGibAddrDelimiter`!
210
+ */
211
+ gibDelimiter?: string,
212
+ /**
213
+ * This is a delimiter used with the entire ibGibAddr.
214
+ *
215
+ * atow this is a caret (`'^'`).
216
+ *
217
+ * ## notes
218
+ *
219
+ * THIS IS NOT THE SAME THING AS THE `gibDelimiter`!
220
+ */
221
+ ibGibAddrDelimiter?: string,
222
+ /**
223
+ * Ignored atow, but in the future, probably will be used.
224
+ * May end up being an IbGibAddr but who knows.
225
+ */
226
+ version?: string,
227
+ }): string[] | null {
228
+ const lc = `[${validateGib.name}]`;
229
+ try {
230
+ const errors: string[] = [];
231
+ if (version) { console.warn(`${lc} version not implemented yet. Ignoring. (E: 90ced1db69774702b92acb261bdaee23)`); }
232
+
233
+ if (!gib) {
234
+ errors.push(`gib required. (E: e217de4035b04086827199f4bace189c)`);
235
+ return errors;
236
+ }
237
+
238
+ ibGibAddrDelimiter = ibGibAddrDelimiter || IBGIB_DELIMITER;
239
+ /** Need to move this to ts-gib */
240
+ const INVALID_GIB_CHARS = [ibGibAddrDelimiter];
241
+ const invalidCharsFound: string[] = [];
242
+ INVALID_GIB_CHARS.forEach(invalidChar => {
243
+ if (gib.includes(invalidChar)) { invalidCharsFound.push(invalidChar); }
244
+ });
245
+ if (invalidCharsFound.length > 0) {
246
+ errors.push(`gib (${gib}}) contains invalid characters: (${JSON.stringify(invalidCharsFound.join(','))}) (E: 1e584258d9e049ba9ce7e516f3ab97f1)`);
247
+ }
248
+
249
+ // punctiliar = point, i.e., a single point in the universe, either a
250
+ // single point in time in a tjp ibgib's timeline, or a single point in
251
+ // space that lives outside of time (has no tjp thus no timeline).
252
+ //
253
+ // So if we've gotten here in code, then our gib is truthy and doesn't
254
+ // contain invalid characters.
255
+
256
+ const { punctiliarHash, tjpGib, isPrimitive } =
257
+ getGibInfo({ gib, gibDelimiter: gibDelimiter || GIB_DELIMITER });
258
+
259
+ // automatically valid if it's a primitive, as the caller should expect
260
+ // no cryptographical guarantees
261
+ if (isPrimitive) { return null; }
262
+
263
+ // Gib is not primitive so must have at least the punctiliar hash.
264
+ if (!punctiliarHash) { throw new Error(`${lc} punctiliarHash is falsy on a non-primitive gib. (E: 72835394918241bdb2632bf0510bdae5)`); }
265
+ const punctiliarHashIs_32 = punctiliarHash!.match(HEXADECIMAL_HASH_STRING_REGEXP_32);
266
+ const punctiliarHashIs_64 = punctiliarHash!.match(HEXADECIMAL_HASH_STRING_REGEXP_64);
267
+ if (!punctiliarHashIs_32 && !punctiliarHashIs_64) {
268
+ errors.push('gib punctiliar hash is neither a 32- or 64-char hash string. (E: d47ff6d6e14b4c02a62107090c8dad39)');
269
+ }
270
+
271
+ if (tjpGib) {
272
+ // if it is an ibgib in a timeline, that timeline has a tjp and this
273
+ // gib has a tjpGib component. So we must recursively validate the
274
+ // tjpGib
275
+ const tjpGibValidationErrors = validateGib({ gib: tjpGib });
276
+ if ((tjpGibValidationErrors ?? []).length > 0) {
277
+ errors.push(`tjpGib has errors (E: d6b79228d4a64c0b967cdb0efcea4d0d). tjpGibValidationErrors: ${tjpGibValidationErrors!.join('. ')}`);
278
+ }
279
+ }
280
+
281
+ return errors.length > 0 ? errors : null;
282
+ } catch (error) {
283
+ console.error(`${lc} ${error.message}`);
284
+ throw error;
285
+ }
286
+ }