-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
executable file
·485 lines (430 loc) · 18.7 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
import { createContentRevisionUtil, getFileByHashUtil } from "./core/content";
import { createFormRevisionUtil, hideFormElementsUtil, unHideFormElementsUtil } from "./core/forms";
import { linkAquaTreesToMultipleAquaTreesUtil, linkAquaTreeUtil, linkMultipleAquaTreesUtil } from "./core/link";
import { checkIfFileAlreadyNotarizedUtil, createGenesisRevision, fetchFilesToBeReadUtil, getLastRevisionUtil, getRevisionByHashUtil, removeLastRevisionUtil } from "./core/revision";
import { signAquaTreeUtil, signMultipleAquaTreesUtil } from "./core/signature";
import { verifyAndGetGraphDataRevisionUtil, verifyAndGetGraphDataUtil, verifyAquaTreeRevisionUtil, verifyAquaTreeUtil } from "./core/verify";
import { witnessAquaTreeUtil, witnessMultipleAquaTreesUtil } from "./core/witness";
import { Result } from "./type_guards";
import { AquaTree, AquaTreeWrapper, AquaOperationData, CredentialsData, FileObject, LogData, Revision, SignType, WitnessNetwork, WitnessPlatformType, WitnessType, VerificationGraphData } from "./types"
import { default as packageJson } from "./../package.json";
import { logAquaTree } from "./aquavhtree";
import { getHashSum } from "./utils";
export * from "./utils";
export * from "./types";
export * from "./type_guards";
export { recoverWalletAddress } from "./core/signature"
export * from "./core/formatter"
// Letes writesome docs here
/**
* @class Aquafier
* @description This class is the main class that contains all the methods that can be used to interact with the aqua tree data structure
* @method removeLastRevision
* @method createContentRevision
* @method createGenesisRevision
* @method verifyAquaTree
* @method verifyAquaTreeRevision
* @method witnessAquaTree
* @method witnessMultipleAquaTrees
* @method signAquaTree
* @method signMultipleAquaTrees
* @method linkAquaTree
* @method linkMultipleAquaTrees
* @method linkAquaTreesToMultipleAquaTrees
* @method createFormRevision
* @method hideFormElements
* @method unHideFormElements
* @method fetchFilesToBeRead
* @method checkIfFileAlreadyNotarized
* @method getRevisionByHash
* @method getLastRevision
* @method getFileByHash
* @method getFileHash
*/
export default class Aquafier {
// Revision
/**
* @method removeLastRevision
* @description This method removes the last revision from the aqua tree
* @param aquaTree - The aqua tree to remove the last revision from
* @returns Result<AquaOperationData, LogData[]>
*/
removeLastRevision = (aquaTree: AquaTree): Result<AquaOperationData, LogData[]> => {
return removeLastRevisionUtil(aquaTree)
}
/**
* @method createContentRevision
* @description This method creates a content revision for the aqua tree
* @param aquaTree - The aqua tree to create the content revision for
* @param fileObject - The file object to create the content revision for
* @param enableScalar - A boolean value to enable scalar
* @returns Result<AquaOperationData, LogData[]>
*/
createContentRevision = async (aquaTree: AquaTreeWrapper, fileObject: FileObject, enableScalar: boolean = true): Promise<Result<AquaOperationData, LogData[]>> => {
return createContentRevisionUtil(aquaTree, fileObject, enableScalar)
}
/**
* @method createGenesisRevision
* @description This method creates a genesis revision for the aqua tree
* @param fileObject - The file object to create the genesis revision for
* @param isForm - A boolean value to check if the file object is a form
* @param enableContent - A boolean value to enable content
* @param enableScalar - A boolean value to enable scalar
* @returns Result<AquaOperationData, LogData[]>
*/
createGenesisRevision = async (fileObject: FileObject, isForm: boolean = false, enableContent: boolean = false, enableScalar: boolean = true): Promise<Result<AquaOperationData, LogData[]>> => {
return createGenesisRevision(fileObject, isForm, enableContent, enableScalar)
}
/**
* @method verifyAquaTree
* @description This method verifies the aqua tree
* @param aquaTree - The aqua tree to verify
* @param fileObject[] - The file objects of the aqua tree that will be useful for verification
* @returns Result<AquaOperationData, LogData[]>
*/
verifyAquaTree = async (aquaTree: AquaTree, fileObject: Array<FileObject>): Promise<Result<AquaOperationData, LogData[]>> => {
return verifyAquaTreeUtil(aquaTree, fileObject)
}
/**
* @method verifyAquaTreeRevision
* @description This method verifies the aqua tree revision
* @param aquaTree - The aqua tree to verify
* @param revision - The revision to verify
* @param revisionItemHash - The revision item hash to verify
* @param fileObject[] - The file objects of the aqua tree that will be useful for verification
* @returns Result<AquaOperationData, LogData[]>
*/
verifyAquaTreeRevision = async (aquaTree: AquaTree, revision: Revision, revisionItemHash: string, fileObject: Array<FileObject>): Promise<Result<AquaOperationData, LogData[]>> => {
return verifyAquaTreeRevisionUtil(aquaTree, revision, revisionItemHash, fileObject)
}
verifyAndGetGraphData = async (aquaTree: AquaTree, fileObject: Array<FileObject>): Promise<Result<VerificationGraphData, LogData[]>> => {
return verifyAndGetGraphDataUtil(aquaTree, fileObject)
}
// we need aqua tree because of the file index and the previous verification hash
verifyAndGetGraphDataRevision = async (aquaTree: AquaTree, revision: Revision, revisionItemHash: string, fileObject: Array<FileObject>): Promise<Result<VerificationGraphData, LogData[]>> => {
return verifyAndGetGraphDataRevisionUtil(aquaTree, revision, revisionItemHash, fileObject)
}
/**
* @method witnessAquaTree
* @description This method witnesses the aqua tree
* @param aquaTree - The aqua tree to witness
* @param witnessType - The witness type to use
* @param witnessNetwork - The witness network to use
* @param witnessPlatform - The witness platform to use
* @param credentials - The credentials to use
* @param enableScalar - A boolean value to enable scalar
* @returns Result<AquaOperationData, LogData[]>
*/
witnessAquaTree = async (aquaTree: AquaTreeWrapper, witnessType: WitnessType, witnessNetwork: WitnessNetwork, witnessPlatform: WitnessPlatformType, credentials: CredentialsData, enableScalar: boolean = true): Promise<Result<AquaOperationData, LogData[]>> => {
return witnessAquaTreeUtil(aquaTree, witnessType, witnessNetwork, witnessPlatform, credentials, enableScalar)
}
/**
* @method witnessMultipleAquaTrees
* @description This method witnesses multiple aqua trees
* @param aquaTrees - The aqua trees to witness
* @param witnessType - The witness type to use
* @param witnessNetwork - The witness network to use
* @param witnessPlatform - The witness platform to use
* @param credentials - The credentials to use
* @param enableScalar - A boolean value to enable scalar
* @returns Result<AquaOperationData, LogData[]>
*/
witnessMultipleAquaTrees = async (aquaTrees: AquaTreeWrapper[], witnessType: WitnessType, witnessNetwork: WitnessNetwork, witnessPlatform: WitnessPlatformType, credentials: CredentialsData, enableScalar: boolean = true): Promise<Result<AquaOperationData, LogData[]>> => {
return witnessMultipleAquaTreesUtil(aquaTrees, witnessType, witnessNetwork, witnessPlatform, credentials, enableScalar)
}
/**
* @method signAquaTree
* @description This method signs the aqua tree
* @param aquaTree - The aqua tree to sign
* @param signType - The sign type to use
* @param credentials - The credentials to use
* @param enableScalar - A boolean value to enable scalar
* @returns Result<AquaOperationData, LogData[]>
*/
signAquaTree = async (aquaTree: AquaTreeWrapper, signType: SignType, credentials: CredentialsData, enableScalar: boolean = true): Promise<Result<AquaOperationData, LogData[]>> => {
return signAquaTreeUtil(aquaTree, signType, credentials, enableScalar)
}
/**
* @method signMultipleAquaTrees
* @description This method signs multiple aqua trees
* @param aquaTrees - The aqua trees to sign
* @param signType - The sign type to use
* @param credentials - The credentials to use
* @returns Result<AquaOperationData, LogData[]>
*/
signMultipleAquaTrees = async (aquaTrees: AquaTreeWrapper[], signType: SignType, credentials: CredentialsData): Promise<Result<AquaOperationData, LogData[]>> => {
return signMultipleAquaTreesUtil(aquaTrees, signType, credentials)
}
/**
* @method linkAquaTree
* @description This method links an aqua tree to another aqua tree
* @param aquaTreeWrapper - The aqua tree to link
* @param linkAquaTreeWrapper - The aqua tree to link to
* @param enableScalar - A boolean value to enable scalar
* @returns Result<AquaOperationData, LogData[]>
*/
linkAquaTree = async (aquaTreeWrapper: AquaTreeWrapper, linkAquaTreeWrapper: AquaTreeWrapper, enableScalar: boolean = true): Promise<Result<AquaOperationData, LogData[]>> => {
return linkAquaTreeUtil(aquaTreeWrapper, linkAquaTreeWrapper, enableScalar)
}
/**
* @method linkMultipleAquaTrees
* @description This method links multiple aqua trees to another aqua tree
* @param aquaTreeWrappers - The aqua trees to link
* @param linkAquaTreeWrapper - The aqua tree to link to
* @param enableScalar - A boolean value to enable scalar
* @returns Result<AquaOperationData, LogData[]>
*/
linkMultipleAquaTrees = async (aquaTreeWrappers: AquaTreeWrapper[], linkAquaTreeWrapper: AquaTreeWrapper, enableScalar: boolean = true): Promise<Result<AquaOperationData, LogData[]>> => {
return linkMultipleAquaTreesUtil(aquaTreeWrappers, linkAquaTreeWrapper, enableScalar)
}
/**
* @method linkAquaTreesToMultipleAquaTrees
* @description This method links multiple aqua trees to multiple aqua trees
* @param aquaTreeWrappers - The aqua trees to link
* @param linkAquaTreeWrapper - The aqua trees to link to
* @param enableScalar - A boolean value to enable scalar
* @returns Result<AquaOperationData, LogData[]>
*/
linkAquaTreesToMultipleAquaTrees = async (aquaTreeWrappers: AquaTreeWrapper, linkAquaTreeWrapper: AquaTreeWrapper[], enableScalar: boolean = true): Promise<Result<AquaOperationData, LogData[]>> => {
return linkAquaTreesToMultipleAquaTreesUtil(aquaTreeWrappers, linkAquaTreeWrapper, enableScalar)
}
/**
* @method createFormRevision
* @description This method creates a form revision for the aqua tree
* @param aquaTree - The aqua tree to create the form revision for
* @param fileObject - The file object to create the form revision for
* @param enableScalar - A boolean value to enable scalar
* @returns Result<AquaOperationData, Log[]>
*/
createFormRevision = async (aquaTree: AquaTreeWrapper, fileObject: FileObject, enableScalar: boolean = true): Promise<Result<AquaOperationData, LogData[]>> => {
return createFormRevisionUtil(aquaTree, fileObject, enableScalar)
}
/**
* @method hideFormElements
* @description This method hides form elements
* @param aquaTree - The aqua tree to hide form elements
* @param keyToHide - The key to hide
* @returns Result<AquaOperationData, LogData[]>
*/
hideFormElements = async (aquaTree: AquaTreeWrapper, keyToHide: string): Promise<Result<AquaOperationData, LogData[]>> => {
return hideFormElementsUtil(aquaTree, keyToHide)
}
/**
* @method unHideFormElements
* @description This method unhides form elements
* @param aquaTree - The aqua tree to unhide form elements
* @param keyToUnHide - The key to unhide
* @param content - The content to unhide
* @returns Result<AquaOperationData, Log[]>
*/
unHideFormElements = async (aquaTree: AquaTreeWrapper, keyToUnHide: string, content: string): Promise<Result<AquaOperationData, LogData[]>> => {
return unHideFormElementsUtil(aquaTree, keyToUnHide, content)
}
//get files to be read to file objects
fetchFilesToBeRead = (aquaTree: AquaTree): string[] => {
return fetchFilesToBeReadUtil(aquaTree)
}
checkIfFileAlreadyNotarized = (aquaTree: AquaTree, fileObject: FileObject): boolean => {
return checkIfFileAlreadyNotarizedUtil(aquaTree, fileObject)
}
// Revisions
getRevisionByHash = (aquaTree: AquaTree, hash: string): Result<Revision, LogData[]> => {
return getRevisionByHashUtil(aquaTree, hash)
}
// Revisions
getLastRevision = (aquaTree: AquaTree): Result<Revision, LogData[]> => {
return getLastRevisionUtil(aquaTree)
}
// get file
getFileByHash = async (aquaTree: AquaTree, hash: string): Promise<Result<string, LogData[]>> => {
return getFileByHashUtil(aquaTree, hash)
}
getFileHash = (fileContent: string | Uint8Array): string => {
return getHashSum(fileContent)
}
getVersionFromPackageJson = (): string => {
let version = "1.3.2.0"
console.log(packageJson.version);
return packageJson.version ? packageJson.version : version
}
renderTree = (aquaTree: AquaTree) => {
if (aquaTree.tree) {
logAquaTree(aquaTree?.tree)
}
}
}
/**
* Chainable API for Aqua operations
*
* This class provides a fluent interface for performing operations on Aqua Trees.
* It allows chaining multiple operations like notarization, signing, witnessing,
* and verification while maintaining state and collecting logs.
*
* @example
* ```typescript
* const aqua = new AquafierChainable(tree)
* .notarize(file)
* .sign("metamask", credentials)
* .witness("eth", "sepolia")
* .verify();
* ```
*/
export class AquafierChainable {
/** Current Aqua Tree state */
private value: AquaTree;
/** Result of last verification operation */
private verificationResult: Result<AquaOperationData, LogData[]>;
/** Collected operation logs */
private logs: LogData[] = [];
/**
* Creates a new chainable Aqua operation sequence
*
* @param initialValue - Optional initial Aqua Tree
*/
constructor(initialValue: AquaTree | null) {
if (initialValue) {
this.value = initialValue;
}
}
/**
* Extracts Aqua Tree from operation result
*
* @param result - Result to unwrap
* @returns Aqua Tree from result
* @throws If result is Err
*/
unwrap(result: Result<AquaOperationData, LogData[]>): AquaTree {
if (result.isErr()) {
this.logs.push(...result.data)
throw Error("an error occured")
} else {
this.logs.push(...result.data.logData)
}
return result.data.aquaTree!
}
/**
* Creates a genesis revision for file notarization
*
* @param fileObject - File to notarize
* @param isForm - Whether file is a form
* @param enableContent - Whether to include content
* @param enableScalar - Whether to enable scalar values
* @returns This instance for chaining
*/
async notarize(fileObject: FileObject, isForm: boolean = false, enableContent: boolean = false, enableScalar: boolean = true): Promise<this> {
let data = await createGenesisRevision(fileObject, isForm, enableContent, enableScalar);
if (data.isOk()) {
this.value = this.unwrap(data);
this.logs.push(...data.data.logData)
} else {
this.logs.push(...data.data)
}
return this;
}
/**
* Signs the current Aqua Tree state
*
* @param signType - Type of signature (cli, metamask, did)
* @param credentials - Signing credentials
* @param enableScalar - Whether to enable scalar values
* @returns This instance for chaining
*/
async sign(signType: SignType = "metamask", credentials: CredentialsData = {
mnemonic: "",
nostr_sk: "",
"did_key": "",
alchemy_key: "",
witness_eth_network: "",
witness_method: ""
}, enableScalar: boolean = true): Promise<this> {
let data = await signAquaTreeUtil({
aquaTree: this.value,
fileObject: {
fileName: "test.txt",
fileContent: "",
path: "/fake/path/test.txt"
},
revision: ""
}, signType, credentials, enableScalar)
// this.value = this.unwrap(data);
if (data.isOk()) {
this.value = this.unwrap(data);
this.logs.push(...data.data.logData)
} else {
this.logs.push(...data.data)
}
return this;
}
/**
* Witnesses the current Aqua Tree state
*
* @param witnessType - Type of witness (eth, tsa, nostr)
* @param witnessNetwork - Network for witnessing
* @param witnessPlatform - Platform for witnessing
* @param credentials - Witness credentials
* @param enableScalar - Whether to enable scalar values
* @returns This instance for chaining
*/
async witness(witnessType: WitnessType = "eth", witnessNetwork: WitnessNetwork = "sepolia", witnessPlatform: WitnessPlatformType = "metamask", credentials: CredentialsData = {
mnemonic: "",
nostr_sk: "",
"did_key": "",
alchemy_key: "",
witness_eth_network: "",
witness_method: ""
}, enableScalar: boolean = true): Promise<this> {
let data = await witnessAquaTreeUtil({
aquaTree: this.value,
fileObject: undefined,
revision: ""
}, witnessType, witnessNetwork, witnessPlatform, credentials, enableScalar);
// this.value = this.unwrap(data);
if (data.isOk()) {
this.value = this.unwrap(data);
this.logs.push(...data.data.logData)
} else {
this.logs.push(...data.data)
}
return this;
}
/**
* Verifies the current Aqua Tree state
*
* @param linkedFileObject - Linked files for verification
* @returns This instance for chaining
*/
async verify(linkedFileObject: Array<FileObject> = []): Promise<this> {
let data = await verifyAquaTreeUtil(this.value, linkedFileObject)
if (data.isOk()) {
this.logs.push(...data.data.logData)
} else {
this.logs.push(...data.data)
}
this.verificationResult = data;
return this;
}
/**
* Gets the current Aqua Tree state
*
* @returns Current Aqua Tree
*/
getValue(): AquaTree {
return this.value;
}
/**
* Gets the result of last verification
*
* @returns Verification result
*/
getVerificationValue(): Result<AquaOperationData, LogData[]> {
return this.verificationResult;
}
/**
* Gets all collected operation logs
*
* @returns Array of log entries
*/
getLogs(): LogData[] {
return this.logs;
}
}