@ibgib/ts-gib 0.4.9 → 0.5.1
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.
- package/.vscode/settings.json +3 -1
- package/CHANGELOG.md +20 -0
- package/README.md +30 -5
- package/dist/V1/constants.d.mts +30 -1
- package/dist/V1/constants.d.mts.map +1 -1
- package/dist/V1/constants.mjs +30 -1
- package/dist/V1/constants.mjs.map +1 -1
- package/dist/V1/factory.d.mts +17 -2
- package/dist/V1/factory.d.mts.map +1 -1
- package/dist/V1/factory.mjs +15 -0
- package/dist/V1/factory.mjs.map +1 -1
- package/dist/V1/sha256v1.d.mts.map +1 -1
- package/dist/V1/sha256v1.mjs +1 -1
- package/dist/V1/sha256v1.mjs.map +1 -1
- package/dist/V1/transforms/fork.d.mts.map +1 -1
- package/dist/V1/transforms/fork.mjs +2 -1
- package/dist/V1/transforms/fork.mjs.map +1 -1
- package/dist/V1/transforms/index.d.mts +1 -1
- package/dist/V1/transforms/index.d.mts.map +1 -1
- package/dist/V1/transforms/index.mjs +1 -1
- package/dist/V1/transforms/index.mjs.map +1 -1
- package/dist/V1/transforms/mut8.d.mts.map +1 -1
- package/dist/V1/transforms/mut8.mjs +2 -1
- package/dist/V1/transforms/mut8.mjs.map +1 -1
- package/dist/V1/transforms/rel8.d.mts.map +1 -1
- package/dist/V1/transforms/rel8.mjs +2 -1
- package/dist/V1/transforms/rel8.mjs.map +1 -1
- package/dist/V1/transforms/transform-helper.d.mts.map +1 -1
- package/dist/V1/transforms/transform-helper.mjs +2 -1
- package/dist/V1/transforms/transform-helper.mjs.map +1 -1
- package/dist/V1/v1-helper.d.mts +38 -0
- package/dist/V1/v1-helper.d.mts.map +1 -0
- package/dist/V1/v1-helper.mjs +87 -0
- package/dist/V1/v1-helper.mjs.map +1 -0
- package/dist/V1/validate-helper.d.mts +83 -0
- package/dist/V1/validate-helper.d.mts.map +1 -0
- package/dist/V1/validate-helper.mjs +227 -0
- package/dist/V1/validate-helper.mjs.map +1 -0
- package/dist/helper.d.mts +0 -54
- package/dist/helper.d.mts.map +1 -1
- package/dist/helper.mjs +0 -114
- package/dist/helper.mjs.map +1 -1
- package/dist/types.d.mts +39 -8
- package/dist/types.d.mts.map +1 -1
- package/dist/types.mjs +31 -0
- package/dist/types.mjs.map +1 -1
- package/jasmine-browser.json +2 -2
- package/package.json +5 -1
- package/src/V1/constants.mts +31 -3
- package/src/V1/factory.mts +18 -2
- package/src/V1/factory.spec.mts +2 -1
- package/src/V1/sha256v1.mts +5 -5
- package/src/V1/transforms/fork.mts +2 -1
- package/src/V1/transforms/fork.spec.mts +2 -1
- package/src/V1/transforms/index.mts +1 -1
- package/src/V1/transforms/mut8.mts +2 -1
- package/src/V1/transforms/mut8.spec.mts +4 -3
- package/src/V1/transforms/rel8.mts +2 -1
- package/src/V1/transforms/rel8.spec.mts +2 -1
- package/src/V1/transforms/transform-helper.mts +3 -2
- package/src/V1/transforms/transform-helper.spec.mts +6 -5
- package/src/V1/v1-helper.mts +90 -0
- package/src/V1/validate-helper.mts +286 -0
- package/src/helper.mts +0 -116
- package/src/helper.spec.mts +47 -115
- package/src/types.mts +40 -9
- package/dist/index.cjs +0 -4
- package/dist/index.cjs.map +0 -1
- package/dist/index.d.cts +0 -4
- package/dist/index.d.cts.map +0 -1
- package/src/index.cts +0 -3
|
@@ -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
|
+
}
|
package/src/helper.mts
CHANGED
|
@@ -1,19 +1,6 @@
|
|
|
1
1
|
import { Ib, Gib, IbGib, IbGibAddr, IbAndGib } from './types.mjs';
|
|
2
|
-
let crypto: any = globalThis.crypto;
|
|
3
|
-
let { subtle } = crypto;
|
|
4
2
|
|
|
5
|
-
export type HashAlgorithm = 'SHA-256' | 'SHA-512';
|
|
6
|
-
export const HashAlgorithm: { [key: string]: HashAlgorithm } = {
|
|
7
|
-
'sha_256': 'SHA-256' as HashAlgorithm,
|
|
8
|
-
'sha_512': 'SHA-512' as HashAlgorithm,
|
|
9
|
-
}
|
|
10
3
|
|
|
11
|
-
export function clone(obj: any) {
|
|
12
|
-
return JSON.parse(JSON.stringify(obj));
|
|
13
|
-
}
|
|
14
|
-
export function getTimestamp() {
|
|
15
|
-
return (new Date()).toUTCString();
|
|
16
|
-
}
|
|
17
4
|
/**
|
|
18
5
|
* Gets the ib^gib address from the given ib and gib or
|
|
19
6
|
* from the ibGib object.
|
|
@@ -87,106 +74,3 @@ export function getIbAndGib({
|
|
|
87
74
|
}
|
|
88
75
|
}
|
|
89
76
|
}
|
|
90
|
-
|
|
91
|
-
/**
|
|
92
|
-
* Simple hash function.
|
|
93
|
-
*
|
|
94
|
-
* NOTE:
|
|
95
|
-
* This is not used for ibGib.gib values (ATOW)
|
|
96
|
-
* but rather as a helper function for generating random UUIDs.
|
|
97
|
-
*
|
|
98
|
-
* @param s string to hash
|
|
99
|
-
* @param algorithm to use, currently only 'SHA-256'
|
|
100
|
-
*/
|
|
101
|
-
export async function hash({
|
|
102
|
-
s,
|
|
103
|
-
algorithm = 'SHA-256',
|
|
104
|
-
}: {
|
|
105
|
-
s: string,
|
|
106
|
-
algorithm?: HashAlgorithm,
|
|
107
|
-
}): Promise<string> {
|
|
108
|
-
if (!s) { return ''; }
|
|
109
|
-
|
|
110
|
-
const validAlgorithms = Object.values(HashAlgorithm);
|
|
111
|
-
if (!validAlgorithms.includes(algorithm)) {
|
|
112
|
-
console.error(`Only ${validAlgorithms} implemented`); return '';
|
|
113
|
-
}
|
|
114
|
-
try {
|
|
115
|
-
const msgUint8 = new TextEncoder().encode(s);
|
|
116
|
-
const buffer = await subtle.digest(algorithm, msgUint8);
|
|
117
|
-
const asArray = Array.from(new Uint8Array(buffer));
|
|
118
|
-
return asArray.map(b => b.toString(16).padStart(2, '0')).join('');
|
|
119
|
-
} catch (e) {
|
|
120
|
-
console.error(extractErrorMsg(e.message));
|
|
121
|
-
return '';
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
/**
|
|
126
|
-
* Simple func to generate UUID (sha-256 hash basically).
|
|
127
|
-
*
|
|
128
|
-
* @param seedSize size of seed for UUID generation
|
|
129
|
-
*/
|
|
130
|
-
export async function getUUID(seedSize = 64): Promise<string> {
|
|
131
|
-
let uuid: string = '';
|
|
132
|
-
if (seedSize < 32) { throw new Error(`Seed size must be at least 32`); }
|
|
133
|
-
if (!globalThis.crypto) { throw new Error(`Cannot create UUID, as unknown crypto library version. If using node.js, v19+ is required. (E: c02cee3fd8a94f678d3f4ebe9dc49797)`); }
|
|
134
|
-
|
|
135
|
-
const values = crypto.getRandomValues(new Uint8Array(16));
|
|
136
|
-
uuid = await hash({ s: values.join('') });
|
|
137
|
-
|
|
138
|
-
if (!uuid) { throw new Error(`Did not create UUID...hmm...`); }
|
|
139
|
-
|
|
140
|
-
return uuid;
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
/**
|
|
144
|
-
* Syntactic sugar for JSON.stringify(obj, null, 2);
|
|
145
|
-
*
|
|
146
|
-
* @param obj to pretty stringify
|
|
147
|
-
*/
|
|
148
|
-
export function pretty(obj: any): string {
|
|
149
|
-
return JSON.stringify(obj, null, 2);
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
/**
|
|
153
|
-
* Just delays given number of ms.
|
|
154
|
-
*
|
|
155
|
-
* @param ms milliseconds to delay
|
|
156
|
-
*/
|
|
157
|
-
export async function delay(ms: number): Promise<void> {
|
|
158
|
-
return new Promise<void>(resolve => {
|
|
159
|
-
setTimeout(() => {
|
|
160
|
-
resolve();
|
|
161
|
-
}, ms);
|
|
162
|
-
});
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
/**
|
|
166
|
-
* extracts the error message from an error object/string/falsy arg.
|
|
167
|
-
*
|
|
168
|
-
* ## notes
|
|
169
|
-
*
|
|
170
|
-
* * some libs throw errors, some throw just strings.
|
|
171
|
-
* * who knows what else it could be.
|
|
172
|
-
*
|
|
173
|
-
* ## todo
|
|
174
|
-
*
|
|
175
|
-
* * extract inner errors/causes if we ever use this function extensively.
|
|
176
|
-
*
|
|
177
|
-
* @param error the error object in the catch area of the try..catch block.
|
|
178
|
-
* @returns error.message if it's a string, error itself if it's a string, or canned error messages if it's falsy or none of the above.
|
|
179
|
-
*/
|
|
180
|
-
export function extractErrorMsg(error: any): string {
|
|
181
|
-
if (!error && error !== 0) {
|
|
182
|
-
return '[error is falsy]';
|
|
183
|
-
} else if (typeof error === 'string') {
|
|
184
|
-
return error;
|
|
185
|
-
} else if (typeof error.message === 'string') {
|
|
186
|
-
return error.message;
|
|
187
|
-
} else if (typeof error === 'number') {
|
|
188
|
-
return JSON.stringify(error);
|
|
189
|
-
} else {
|
|
190
|
-
return `[error is not a string and error.message is not a string. typeof error: ${typeof error}]`;
|
|
191
|
-
}
|
|
192
|
-
}
|
package/src/helper.spec.mts
CHANGED
|
@@ -1,127 +1,59 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
import { getIbAndGib, getIbGibAddr } from './helper.mjs';
|
|
2
|
+
import { Ib, IbGib } from './types.mjs';
|
|
3
|
+
import { ROOT, ROOT_ADDR } from './V1/constants.mjs';
|
|
4
4
|
|
|
5
|
-
import * as h from './helper.mjs';
|
|
6
|
-
import { HashAlgorithm } from './helper.mjs';
|
|
7
5
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
const SOME_STRING_HASH_512 = "C48C780DA7C43EE7047CDAB0D7A2FDF5DC050DF3291D3CAE973FB0F51BE8535668592EB25A730C95A23D3B4D57924893E7E87C86DC0F52C6D5D2E34EE41203E5";
|
|
11
|
-
const SOME_OTHER_STRING = "This is quite a different string of stuff.";
|
|
12
|
-
const TEST_HASHES: { [key: string]: string } = {
|
|
13
|
-
[SOME_STRING + h.HashAlgorithm.sha_256]: SOME_STRING_HASH,
|
|
14
|
-
[SOME_STRING + h.HashAlgorithm.sha_512]: SOME_STRING_HASH_512,
|
|
15
|
-
[SOME_OTHER_STRING + h.HashAlgorithm.sha_256]: 'AE4C18B37B2329770E05EA3F946C6EB6DE56D2DC568E1F5CBB395E2A1556F58A',
|
|
16
|
-
[SOME_OTHER_STRING + h.HashAlgorithm.sha_512]: '2F1A72B21C914ED459319DF4B5D70E81CEE67C4B48BC74CD6BA8F41DCEC20DCD100C914913F370B2782985268D05C590B46F3FE9005A7477BA952935D2454E53',
|
|
17
|
-
}
|
|
6
|
+
describe(`getIbGibAddr`, () => {
|
|
7
|
+
// unsure if these would change when not in V1...(these tests are outside V1 atow)
|
|
18
8
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
levelOne: {
|
|
25
|
-
levelTwo: {
|
|
26
|
-
buckle: "your shoe",
|
|
27
|
-
three: "four",
|
|
28
|
-
objSimple: objSimple,
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
};
|
|
32
|
-
const cloneADeep = h.clone(objADeep);
|
|
33
|
-
expect(cloneADeep?.levelOne?.levelTwo?.buckle).toBe("your shoe");
|
|
34
|
-
expect(cloneADeep?.levelOne?.levelTwo?.three).toBe("four");
|
|
35
|
-
expect(cloneADeep?.levelOne?.levelTwo?.objSimple).toEqual(objSimple);
|
|
36
|
-
|
|
37
|
-
cloneADeep.levelOne.levelTwo.objSimple.a = SOME_OTHER_STRING;
|
|
38
|
-
|
|
39
|
-
// original should **still** be the first value
|
|
40
|
-
expect(objSimple.a).toBe(SOME_STRING);
|
|
41
|
-
// clone should be changed.
|
|
42
|
-
expect(cloneADeep.levelOne.levelTwo.objSimple.a).toBe(SOME_OTHER_STRING);
|
|
43
|
-
});
|
|
44
|
-
});
|
|
45
|
-
|
|
46
|
-
describe(`when getting timestamp`, () => {
|
|
47
|
-
it(`should get the current date as UTCString`, async () => {
|
|
48
|
-
// implementation detail hmm....
|
|
49
|
-
const timestamp = h.getTimestamp();
|
|
50
|
-
const date = new Date(timestamp);
|
|
51
|
-
const dateAsUTCString = date.toUTCString();
|
|
52
|
-
expect(timestamp).toBe(dateAsUTCString);
|
|
9
|
+
it(`should get the right addr for ROOT`, async () => {
|
|
10
|
+
let ibGib = ROOT;
|
|
11
|
+
let gotten = getIbGibAddr({ ibGib });
|
|
12
|
+
expect(gotten).toBeTruthy();
|
|
13
|
+
expect(gotten).toEqual(ROOT_ADDR);
|
|
53
14
|
});
|
|
54
|
-
});
|
|
55
|
-
|
|
56
|
-
describe(`when hashing (helper)`, () => {
|
|
57
|
-
const TEST_ALGORITHMS: h.HashAlgorithm[] = Object.values(h.HashAlgorithm);
|
|
58
15
|
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
16
|
+
it(`should get the right addr for primitives`, async () => {
|
|
17
|
+
let ibs: Ib[] = ['7', 'foo', 'ibgib', 'wakka doodle'];
|
|
18
|
+
for (let i = 0; i < ibs.length; i++) {
|
|
19
|
+
const ib = ibs[i];
|
|
20
|
+
let ibGib: IbGib = { ib, gib: 'gib' };
|
|
21
|
+
let gotten = getIbGibAddr({ ibGib });
|
|
22
|
+
expect(gotten).toBeTruthy();
|
|
23
|
+
expect(gotten).toEqual(`${ib}^gib`);
|
|
24
|
+
}
|
|
63
25
|
});
|
|
64
26
|
|
|
65
|
-
for (let algorithm of TEST_ALGORITHMS) {
|
|
66
|
-
it(`should hash consistently with explicit ${algorithm}`, async () => {
|
|
67
|
-
// const hash = await h.hash({s: SOME_STRING, algorithm: "SHA-256"}) || "";
|
|
68
|
-
let hash = await h.hash({ s: SOME_STRING, algorithm }) || "";
|
|
69
|
-
expect(hash.toUpperCase()).toBe(TEST_HASHES[SOME_STRING + algorithm]);
|
|
70
|
-
hash = await h.hash({ s: SOME_OTHER_STRING, algorithm }) || "";
|
|
71
|
-
expect(hash.toUpperCase()).toBe(TEST_HASHES[SOME_OTHER_STRING + algorithm]);
|
|
72
|
-
});
|
|
73
|
-
it(`should hash without collisions, 1000 times, ${algorithm}`, async () => {
|
|
74
|
-
const hashes: string[] = [];
|
|
75
|
-
const salt = await h.getUUID(1024);
|
|
76
|
-
// console.log(`salt: ${salt}`);
|
|
77
|
-
for (let i = 0; i < 1000; i++) {
|
|
78
|
-
const hash = await h.hash({ s: salt + i.toString(), algorithm }) || "";
|
|
79
|
-
// console.log(hash);
|
|
80
|
-
expect(hashes).not.toContain(hash);
|
|
81
|
-
hashes.push(hash);
|
|
82
|
-
}
|
|
83
|
-
});
|
|
84
|
-
};
|
|
85
27
|
});
|
|
86
28
|
|
|
87
|
-
describe(`
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
29
|
+
describe(`getIbAndGib`, () => {
|
|
30
|
+
// unsure if these would change when not in V1...(these tests are outside V1 atow)
|
|
31
|
+
|
|
32
|
+
it(`should get the right ib & gib for ROOT, with ibGib param`, async () => {
|
|
33
|
+
let ibGib = ROOT;
|
|
34
|
+
let gotten = getIbAndGib({ ibGib });
|
|
35
|
+
expect(gotten).toBeTruthy();
|
|
36
|
+
expect(gotten.ib).toEqual('ib');
|
|
37
|
+
expect(gotten.gib).toEqual('gib');
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
it(`should get the right ib & gib for ROOT_ADDR, with ibGibAddr param`, async () => {
|
|
41
|
+
let gotten = getIbAndGib({ ibGibAddr: ROOT_ADDR });
|
|
42
|
+
expect(gotten).toBeTruthy();
|
|
43
|
+
expect(gotten.ib).toEqual('ib');
|
|
44
|
+
expect(gotten.gib).toEqual('gib');
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it(`should get the right ib & gib for primitives`, async () => {
|
|
48
|
+
let ibs: Ib[] = ['7', 'foo', 'ibgib', 'wakka doodle'];
|
|
49
|
+
for (let i = 0; i < ibs.length; i++) {
|
|
50
|
+
const ib = ibs[i];
|
|
51
|
+
let ibGib: IbGib = { ib, gib: 'gib' };
|
|
52
|
+
let gotten = getIbAndGib({ ibGib });
|
|
53
|
+
expect(gotten).toBeTruthy();
|
|
54
|
+
expect(gotten.ib).toEqual(ib);
|
|
55
|
+
expect(gotten.gib).toEqual('gib');
|
|
94
56
|
}
|
|
95
57
|
});
|
|
96
|
-
});
|
|
97
58
|
|
|
98
|
-
|
|
99
|
-
it(`should return canned msg when error is falsy`, () => {
|
|
100
|
-
const defaultMsg = '[error is falsy]'; // duplicate of code in helper.mjs
|
|
101
|
-
[null, undefined, ''].forEach(error => {
|
|
102
|
-
expect(h.extractErrorMsg(error)).withContext(JSON.stringify(error)).toEqual(defaultMsg);
|
|
103
|
-
});
|
|
104
|
-
});
|
|
105
|
-
it(`should return incoming error if it is a string`, () => {
|
|
106
|
-
['string here', 'undefined', '42', 'ibgib'].forEach(stringError => {
|
|
107
|
-
expect(h.extractErrorMsg(stringError)).withContext(JSON.stringify(stringError)).toEqual(stringError);
|
|
108
|
-
});
|
|
109
|
-
});
|
|
110
|
-
it(`should return error.message if it's a thrown error`, () => {
|
|
111
|
-
['string here', 'undefined', 'something went wrong', 'danger. out of memory (E: ce86ffd7a0174c1d8ce5e56c807dd4b1)']
|
|
112
|
-
.map(x => new Error(x))
|
|
113
|
-
.forEach(error => {
|
|
114
|
-
expect(h.extractErrorMsg(error)).withContext(error.message).toEqual(error.message);
|
|
115
|
-
});
|
|
116
|
-
});
|
|
117
|
-
it(`should return incoming error stringified if it is a number`, () => {
|
|
118
|
-
[1234, 0, 1_000, Number.MAX_SAFE_INTEGER, Number.MIN_SAFE_INTEGER, -5, 1 / 23].forEach(numberError => {
|
|
119
|
-
expect(h.extractErrorMsg(numberError)).withContext(JSON.stringify(numberError)).toEqual(JSON.stringify(numberError));
|
|
120
|
-
});
|
|
121
|
-
});
|
|
122
|
-
it(`should return canned response with type if incoming error is none of the above`, () => {
|
|
123
|
-
let error = { x: 1, y: 2 };
|
|
124
|
-
let msg = h.extractErrorMsg(error);
|
|
125
|
-
expect(msg).withContext(JSON.stringify(error)).toBeTruthy();
|
|
126
|
-
});
|
|
127
|
-
});
|
|
59
|
+
});
|
package/src/types.mts
CHANGED
|
@@ -1,17 +1,44 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
* fundamentally you are taking two ibGibs and producing a third.
|
|
2
|
+
* @module types
|
|
4
3
|
*
|
|
5
|
-
*
|
|
4
|
+
* These are the core types at the lowest level of the ibGib protocol.
|
|
6
5
|
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
6
|
+
* The core of the simplicity of the ibGib protocol is that fundamentally you
|
|
7
|
+
* are encompassing the idea of an ibgib (similar to a base class "object" or
|
|
8
|
+
* "thing") that has intrinsic data and relationships to extrinsic ibgib. Each
|
|
9
|
+
* ibgib is content addressed with a unique addressing style called its ib^gib
|
|
10
|
+
* address. This allows us semantics for abstracting the "content addressing"
|
|
11
|
+
* per use case, with various schemas allowed, but in general the `ib` is
|
|
12
|
+
* per-use-case metadata and the `gib` is the more conventional hash of the
|
|
13
|
+
* ibgib record to allow for the cryptographic guarantees one would expect. Each
|
|
14
|
+
* discrete frame in time of the ibgib is its own ibgib, and a timeline stream
|
|
15
|
+
* of that ibgib is created via the "past" relationship (`rel8n`).
|
|
9
16
|
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
17
|
+
* New ibgib records are created via "transforms", of which atow there are three
|
|
18
|
+
* kinds: `mut8`, `rel8`, `fork`
|
|
19
|
+
*
|
|
20
|
+
* These are actually different aspects of the single function of relationship
|
|
21
|
+
* and time, either extending or creating a timeline.
|
|
22
|
+
*
|
|
23
|
+
* Mut8 is intrinsic, rel8 is extrinsic, fork makes a new timeline. Mut8
|
|
24
|
+
* changes a timeline, rel8 changes a timeline's link(s), fork creates a new
|
|
25
|
+
* timeline.
|
|
26
|
+
*
|
|
27
|
+
* ## notes
|
|
28
|
+
*
|
|
29
|
+
* I have attempted to make these version independent, though we will not know
|
|
30
|
+
* how successful that is until we do a V2.
|
|
31
|
+
*/
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Type alias of string (or empty string) that is meant to convey that it
|
|
35
|
+
* is used as the ib in an ib^gib address or an `IbGib` class.
|
|
13
36
|
*/
|
|
14
37
|
export declare type Ib = '' | string;
|
|
38
|
+
/**
|
|
39
|
+
* Type alias of string (or empty string) that is meant to convey that it
|
|
40
|
+
* is used as the gib in an ib^gib address or an `IbGib` class.
|
|
41
|
+
*/
|
|
15
42
|
export declare type Gib = '' | 'gib' | string;
|
|
16
43
|
/**
|
|
17
44
|
* Convenient for when destructuring an IbGibAddr
|
|
@@ -27,6 +54,11 @@ export interface IbAndGib {
|
|
|
27
54
|
* that's to decide for future.
|
|
28
55
|
*/
|
|
29
56
|
export declare type IbGibAddr = string;
|
|
57
|
+
/**
|
|
58
|
+
* Type alias that indicates that this is intended to be a tjp (temporal
|
|
59
|
+
* junction point) address.
|
|
60
|
+
*/
|
|
61
|
+
export declare type TjpIbGibAddr = IbGibAddr;
|
|
30
62
|
export declare type IbGibRel8ns = {
|
|
31
63
|
/**
|
|
32
64
|
* The key is the rel8nName.
|
|
@@ -224,7 +256,6 @@ export interface TransformOpts_Rel8<TSrc extends IbGib = IbGib> extends Transfor
|
|
|
224
256
|
*/
|
|
225
257
|
rel8nsToRemoveByAddr?: IbGibRel8ns;
|
|
226
258
|
}
|
|
227
|
-
|
|
228
259
|
export interface TransformResult<TOut extends IbGib> {
|
|
229
260
|
/**
|
|
230
261
|
* The final result of the transformation
|
package/dist/index.cjs
DELETED
package/dist/index.cjs.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","sourceRoot":"","sources":["../src/index.cts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,gBAAgB,CAAC;AACrC,cAAc,aAAa,CAAC;AAC5B,cAAc,cAAc,CAAC"}
|
package/dist/index.d.cts
DELETED
package/dist/index.d.cts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.cts","sourceRoot":"","sources":["../src/index.cts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,gBAAgB,CAAC;AACrC,cAAc,aAAa,CAAC;AAC5B,cAAc,cAAc,CAAC"}
|
package/src/index.cts
DELETED