neozipkit 0.1.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.
Files changed (135) hide show
  1. package/README.md +400 -0
  2. package/contracts/Lock.sol +34 -0
  3. package/contracts/bytecode.txt +1 -0
  4. package/contracts/compile-contract.js +168 -0
  5. package/contracts/deploy/deploy-both-contracts.js +286 -0
  6. package/contracts/deploy/deploy-enhanced-contract.js +197 -0
  7. package/contracts/deploy/deploy-private-contract.js +190 -0
  8. package/contracts/deploy/deploy-public-clean.js +1063 -0
  9. package/contracts/deploy/deploy-public-contract.js +1107 -0
  10. package/contracts/deployment.config.example.js +25 -0
  11. package/contracts/remix-compile.js +222 -0
  12. package/contracts/setup-deployment.js +51 -0
  13. package/contracts/src/NZIP-NFT-Private.sol +350 -0
  14. package/contracts/src/NZIP-NFT.sol +265 -0
  15. package/contracts/src/ZipFileNFT-DEPRECATED-6field.sol +260 -0
  16. package/contracts/src/ZipFileNFT-DEPRECATED-Enhanced.sol +289 -0
  17. package/dist/browser/CertificateGeneratorBrowser.d.ts +75 -0
  18. package/dist/browser/CertificateGeneratorBrowser.js +433 -0
  19. package/dist/browser/TokenVerifierBrowser.d.ts +76 -0
  20. package/dist/browser/TokenVerifierBrowser.js +299 -0
  21. package/dist/browser/ZipKitBrowser.d.ts +18 -0
  22. package/dist/browser/ZipKitBrowser.js +250 -0
  23. package/dist/browser/ZipKitStampBrowser.d.ts +2 -0
  24. package/dist/browser/ZipKitStampBrowser.js +12 -0
  25. package/dist/browser-index.d.ts +21 -0
  26. package/dist/browser-index.js +58 -0
  27. package/dist/browser.d.ts +21 -0
  28. package/dist/browser.js +58 -0
  29. package/dist/index.d.ts +29 -0
  30. package/dist/index.js +76 -0
  31. package/dist/server/blockchain/CertificateToken.d.ts +48 -0
  32. package/dist/server/blockchain/CertificateToken.js +386 -0
  33. package/dist/server/blockchain/NFTMinter.d.ts +41 -0
  34. package/dist/server/blockchain/NFTMinter.js +323 -0
  35. package/dist/server/blockchain/TokenQuerier.d.ts +31 -0
  36. package/dist/server/blockchain/TokenQuerier.js +259 -0
  37. package/dist/server/blockchain/TokenVerifier.d.ts +20 -0
  38. package/dist/server/blockchain/TokenVerifier.js +395 -0
  39. package/dist/server/blockchain/WalletAnalyzer.d.ts +81 -0
  40. package/dist/server/blockchain/WalletAnalyzer.js +356 -0
  41. package/dist/server/blockchain/WalletFormatter.d.ts +39 -0
  42. package/dist/server/blockchain/WalletFormatter.js +196 -0
  43. package/dist/server/blockchain/WalletManager.d.ts +46 -0
  44. package/dist/server/blockchain/WalletManager.js +294 -0
  45. package/dist/server/blockchain/ZipMetadata.d.ts +27 -0
  46. package/dist/server/blockchain/ZipMetadata.js +131 -0
  47. package/dist/server/blockchain/ZipTokenProcessor.d.ts +44 -0
  48. package/dist/server/blockchain/ZipTokenProcessor.js +204 -0
  49. package/dist/server/blockchain/constants.d.ts +102 -0
  50. package/dist/server/blockchain/constants.js +968 -0
  51. package/dist/server/blockchain/index.d.ts +9 -0
  52. package/dist/server/blockchain/index.js +44 -0
  53. package/dist/server/blockchain/test-certificate.d.ts +8 -0
  54. package/dist/server/blockchain/test-certificate.js +100 -0
  55. package/dist/server/blockchain/types.d.ts +69 -0
  56. package/dist/server/blockchain/types.js +51 -0
  57. package/dist/server/platform/server/ZipKitServer.d.ts +105 -0
  58. package/dist/server/platform/server/ZipKitServer.js +437 -0
  59. package/dist/server/platform/servercomponents/ZipKitOTS.d.ts +91 -0
  60. package/dist/server/platform/servercomponents/ZipKitOTS.js +271 -0
  61. package/dist/shared/ZipEntry.d.ts +131 -0
  62. package/dist/shared/ZipEntry.js +686 -0
  63. package/dist/shared/ZipKit.d.ts +249 -0
  64. package/dist/shared/ZipKit.js +789 -0
  65. package/dist/shared/components/CertificateOTS.d.ts +15 -0
  66. package/dist/shared/components/CertificateOTS.js +116 -0
  67. package/dist/shared/components/FileSys.d.ts +2 -0
  68. package/dist/shared/components/FileSys.js +113 -0
  69. package/dist/shared/components/HashAccumulator.d.ts +55 -0
  70. package/dist/shared/components/HashAccumulator.js +100 -0
  71. package/dist/shared/components/Support.d.ts +63 -0
  72. package/dist/shared/components/Support.js +70 -0
  73. package/dist/shared/components/Util.d.ts +21 -0
  74. package/dist/shared/components/Util.js +115 -0
  75. package/dist/shared/components/ZipCrypto.d.ts +3 -0
  76. package/dist/shared/components/ZipCrypto.js +78 -0
  77. package/dist/shared/components/merkleTree.d.ts +66 -0
  78. package/dist/shared/components/merkleTree.js +136 -0
  79. package/dist/shared/constants/Errors.d.ts +53 -0
  80. package/dist/shared/constants/Errors.js +69 -0
  81. package/dist/shared/constants/Headers.d.ts +167 -0
  82. package/dist/shared/constants/Headers.js +191 -0
  83. package/dist/shared/types/index.d.ts +21 -0
  84. package/dist/shared/types/index.js +3 -0
  85. package/dist/shared/types.d.ts +33 -0
  86. package/dist/shared/types.js +8 -0
  87. package/dist/shared/version.d.ts +4 -0
  88. package/dist/shared/version.js +7 -0
  89. package/dist/types/index.d.ts +21 -0
  90. package/dist/types/index.js +3 -0
  91. package/examples/unzip-token.ts +488 -0
  92. package/examples/zip-token.ts +604 -0
  93. package/package.json +117 -0
  94. package/src/browser/CertificateGeneratorBrowser.ts +498 -0
  95. package/src/browser/TokenVerifierBrowser.ts +344 -0
  96. package/src/browser/ZipKitBrowser.ts +242 -0
  97. package/src/browser/ZipKitStampBrowser.ts +9 -0
  98. package/src/browser-index.ts +36 -0
  99. package/src/index.ts +48 -0
  100. package/src/server/blockchain/CertificateToken.ts +444 -0
  101. package/src/server/blockchain/NFTMinter.ts +357 -0
  102. package/src/server/blockchain/TokenQuerier.ts +287 -0
  103. package/src/server/blockchain/TokenVerifier.ts +453 -0
  104. package/src/server/blockchain/WalletAnalyzer.ts +397 -0
  105. package/src/server/blockchain/WalletFormatter.ts +206 -0
  106. package/src/server/blockchain/WalletManager.ts +294 -0
  107. package/src/server/blockchain/ZipMetadata.ts +117 -0
  108. package/src/server/blockchain/ZipTokenProcessor.ts +234 -0
  109. package/src/server/blockchain/constants.ts +969 -0
  110. package/src/server/blockchain/index.ts +42 -0
  111. package/src/server/blockchain/test-certificate.ts +137 -0
  112. package/src/server/blockchain/types.ts +134 -0
  113. package/src/server/platform/server/ZipKitServer.ts +459 -0
  114. package/src/server/platform/servercomponents/ZipKitOTS.ts +313 -0
  115. package/src/server/platform/servercomponents/opentimestamps.d.ts +23 -0
  116. package/src/shared/ZipEntry.ts +767 -0
  117. package/src/shared/ZipKit.ts +899 -0
  118. package/src/shared/components/CertificateOTS.ts +132 -0
  119. package/src/shared/components/FileSys.ts +87 -0
  120. package/src/shared/components/HashAccumulator.ts +113 -0
  121. package/src/shared/components/Support.ts +77 -0
  122. package/src/shared/components/Util.ts +113 -0
  123. package/src/shared/components/ZipCrypto.ts +91 -0
  124. package/src/shared/components/merkleTree.ts +162 -0
  125. package/src/shared/constants/Errors.ts +82 -0
  126. package/src/shared/constants/Headers.ts +203 -0
  127. package/src/shared/types/index.ts +25 -0
  128. package/src/shared/types/modules.d.ts +22 -0
  129. package/src/shared/types/opentimestamps.d.ts +23 -0
  130. package/src/shared/types.d.ts +8 -0
  131. package/src/shared/types.ts +43 -0
  132. package/src/shared/version.ts +4 -0
  133. package/src/types/index.ts +25 -0
  134. package/src/types/modules.d.ts +22 -0
  135. package/src/types/opentimestamps.d.ts +23 -0
@@ -0,0 +1,313 @@
1
+ // ======================================
2
+ // ZipKitOTS.ts
3
+ // Copyright (c) 2025 NeoWare, Inc. All rights reserved.
4
+ // ======================================
5
+ // ZipKit OpenTimestamps Routines
6
+
7
+ "use server";
8
+ "use strict";
9
+
10
+ import moment from 'moment-timezone';
11
+
12
+ // Create a singleton instance of OpenTimestamps
13
+
14
+ // May need to remove the global._bitcore object to stop the error:
15
+ declare global {
16
+ var _bitcore: any;
17
+ }
18
+ if (global._bitcore) delete global._bitcore;
19
+
20
+ let OTSInstance: any = null;
21
+
22
+ async function getOTS() {
23
+ if (!OTSInstance) {
24
+ OTSInstance = (await import('opentimestamps')).default;
25
+ }
26
+ return OTSInstance;
27
+ }
28
+
29
+ /**
30
+ * ZipKitOTS - OpenTimestamps utilities for NeoZip
31
+ *
32
+ * This module provides functions for creating and verifying OpenTimestamps
33
+ * proofs for ZIP archives and their contents. It includes utilities for timestamp
34
+ * creation, verification, and serialization/deserialization.
35
+ */
36
+
37
+ /**
38
+ * Interface for timestamp verification results
39
+ * Contains all necessary information about a verified timestamp
40
+ */
41
+ export interface TimestampResult {
42
+ merkleRoot: string | null; // The hash digest used for verification
43
+ ots: ArrayBuffer | null; // The OpenTimestamps proof data (allow both types)
44
+ verified: boolean; // Whether verification was successful
45
+ results: any | null; // Results returned from OpenTimestamps verification
46
+ error: string | null; // Error message if verification failed
47
+ attestations: any | null; // Parsed attestations in JSON string format
48
+ upgradedOts: ArrayBuffer | null; // Upgraded OTS data if available
49
+ }
50
+
51
+ // TimestampInfo object for adding to the database
52
+ export interface TimestampInfo {
53
+ message: string;
54
+ results: string[];
55
+ attestDate: Date | null;
56
+ attestHeight: number | null;
57
+ submittedUri: string | null;
58
+ otsUpgraded: boolean;
59
+ }
60
+
61
+ /**
62
+ * Verifies a timestamp proof against a file hash
63
+ *
64
+ * @param hashDigest - SHA-256 hash digest of the file or merkle root (hex string)
65
+ * @param otsData - The OpenTimestamps proof data
66
+ * @returns Promise with verification result or null on critical failure
67
+ *
68
+ * @example
69
+ * // Verify a timestamp proof
70
+ * const hashDigest = "8a9d..."; // SHA-256 digest in hex format
71
+ * const result = await verifyTimestamp(hashDigest, otsBuffer);
72
+ *
73
+ * if (result) {
74
+ * if (result.error) {
75
+ * console.error("Verification failed:", result.error);
76
+ * } else {
77
+ * console.log("Verification succeeded:", result.verified);
78
+ *
79
+ * if (result.upgradedOts) {
80
+ * // Save the upgraded timestamp
81
+ * }
82
+ * }
83
+ * }
84
+ */
85
+ export async function verifyTimestamp(
86
+ hashDigest: string | null,
87
+ ots: ArrayBuffer | null,
88
+ ): Promise<TimestampResult | { error: string } | null> {
89
+ if (!hashDigest || !ots) {
90
+ console.error('verifyTimestamp - Invalid parameters');
91
+ return null;
92
+ }
93
+
94
+ const tsResult: TimestampResult = {
95
+ merkleRoot: hashDigest,
96
+ ots: ots,
97
+ verified: false,
98
+ results: null,
99
+ error: null,
100
+ attestations: null,
101
+ upgradedOts: null,
102
+ };
103
+
104
+ try {
105
+ const OpenTimestamps = await getOTS();
106
+ // Convert the base64 string to a Buffer
107
+ // const _otsBuffer = Buffer.from(ots, 'base64');
108
+ const _otsBuffer = Buffer.from(ots);
109
+ const detachedOts = OpenTimestamps.DetachedTimestampFile.deserialize(_otsBuffer);
110
+
111
+ const digest = Buffer.from(hashDigest, 'hex');
112
+ const hashOts = OpenTimestamps.DetachedTimestampFile.fromHash(
113
+ new OpenTimestamps.Ops.OpSHA256(),
114
+ digest
115
+ );
116
+
117
+ // Check if the MetaStamp should be upgraded
118
+ const upgrade = await OpenTimestamps.upgrade(detachedOts);
119
+ if (upgrade) {
120
+ console.log('verifyTimestamp === Timestamp should be upgraded!');
121
+ tsResult.upgradedOts = detachedOts.serializeToBytes();
122
+ }
123
+
124
+ // Verify the MetaStamp
125
+ tsResult.results = await OpenTimestamps.verify(detachedOts, hashOts, {
126
+ ignoreBitcoinNode: true, // Don't check for local Bitcoin node
127
+ timeout: 5000
128
+ });
129
+
130
+ var _attest: any = [];
131
+ detachedOts.timestamp.allAttestations().forEach((attestation: any) => {
132
+ if (attestation instanceof OpenTimestamps.Notary.UnknownAttestation) {
133
+ console.log('Unknown attestation type');
134
+ } else {
135
+ // console.log(attestation);
136
+ _attest.push(attestation);
137
+ }
138
+ });
139
+ tsResult.attestations = JSON.stringify(_attest);
140
+
141
+ // Check if the results show a verified timestamp
142
+ if (Object.keys(tsResult.results).length < 1) {
143
+ tsResult.results = null;
144
+ tsResult.verified = false;
145
+ } else {
146
+ tsResult.verified = true;
147
+ }
148
+
149
+ console.log('verifyTimestamp === tsResult: ', tsResult);
150
+ return tsResult;
151
+ } catch (error: any) {
152
+ let errMsg = error?.message || 'Timestamp Verification Failed!';
153
+ if (errMsg.indexOf('ESOCKETTIMEDOUT') != -1) {
154
+ errMsg = 'Server timed out, Try again in a few minutes.';
155
+ } else if (errMsg.indexOf('502') != -1) {
156
+ errMsg = 'Server failed to respond, Try again in a few minutes.';
157
+ } else if (errMsg.indexOf('ECONNRESET') != -1 ) {
158
+ errMsg = 'Server connection reset, Try again in a few minutes.';
159
+ }
160
+ return { error: errMsg };
161
+ }
162
+ }
163
+
164
+ /**
165
+ * Creates a timestamp proof for a file hash using OpenTimestamps
166
+ *
167
+ * @param hashDigest - SHA-256 hash digest of the file or merkle root (hex string)
168
+ * @returns Promise containing the OpenTimestamps proof or null on failure
169
+ *
170
+ * @example
171
+ * // Create a timestamp for a file hash
172
+ * const hashDigest = "8a9d..."; // SHA-256 digest in hex format
173
+ * const timestampProof = await createTimestamp(hashDigest);
174
+ *
175
+ * if (timestampProof) {
176
+ * // Save the timestamp proof to storage
177
+ * }
178
+ */
179
+ export async function createTimestamp(hashDigest: string | null): Promise<Buffer | null> {
180
+ if (!hashDigest) return null;
181
+
182
+ try {
183
+ const OpenTimestamps = await getOTS();
184
+
185
+ // Convert hex string to Buffer
186
+ const hashBuffer = Buffer.from(hashDigest, 'hex');
187
+
188
+ // Create detached timestamp file
189
+ const detached = OpenTimestamps.DetachedTimestampFile.fromHash(
190
+ new OpenTimestamps.Ops.OpSHA256(),
191
+ hashBuffer
192
+ );
193
+
194
+ // Create the timestamp
195
+ await OpenTimestamps.stamp(detached);
196
+
197
+ // Return the serialized timestamp
198
+ return Buffer.from(detached.serializeToBytes());
199
+
200
+ } catch (error) {
201
+ console.error('Error creating timestamp:', error);
202
+ throw new Error('Error creating timestamp');
203
+ }
204
+ }
205
+
206
+ /**
207
+ * Result of deserializing an OpenTimestamps proof
208
+ */
209
+ export interface DeserializeOtsResult {
210
+ attestStr: string | null; // Human-readable attestation string
211
+ needsUpdateMsg: string | null; // Message if timestamp needs upgrading
212
+ attestationValues: any[]; // Array of attestation details
213
+ }
214
+
215
+ /**
216
+ * Deserializes timestamp metadata to extract attestation information
217
+ * Used for generating timestamp certificates and displaying verification details
218
+ *
219
+ * @param tsResult - Timestamp verification result to deserialize
220
+ * @returns JSON string of deserialized data or null on failure
221
+ */
222
+ export async function deserializeOts(
223
+ tsResult: TimestampResult,
224
+ ) : Promise< string | null > {
225
+
226
+ let result: DeserializeOtsResult = {
227
+ attestStr: null,
228
+ needsUpdateMsg: null,
229
+ attestationValues: [],
230
+ };
231
+
232
+ for (const chain in tsResult.results) {
233
+ let _results = tsResult.results[chain];
234
+ const _attestDate = moment(_results.timestamp * 1000).tz(moment.tz.guess()).format('MM-DD-YYYY hh:mma z');
235
+ const blockchain = chain.charAt(0).toUpperCase() + chain.slice(1);
236
+ result.attestStr = `<b>${blockchain} block ${_results.height}</b> attests existence as of <b>${_attestDate}</b>`;
237
+ };
238
+
239
+ let timestamp = tsResult.ots;
240
+ if (tsResult.upgradedOts != null) {
241
+ result.needsUpdateMsg = "This Zip file requires updated Timestamp Metadata!";
242
+ // Use the updated timestamp metadata
243
+ timestamp = tsResult.upgradedOts;
244
+ }
245
+
246
+ // Extract the attestation values from the timestamp metadata
247
+ if (!timestamp) return null;
248
+ const OpenTimestamps = await getOTS();
249
+ const detached = OpenTimestamps.DetachedTimestampFile.deserialize(Buffer.from(timestamp));
250
+ const infoResult = OpenTimestamps.info(detached);
251
+
252
+ // Extract all transaction IDs using a regular expression
253
+ const transactionIdMatches = [...infoResult.matchAll(/transaction id ([a-f0-9]+)/gi)];
254
+ const transactionIds = transactionIdMatches.map(match => match[1]);
255
+
256
+ // Extract the value inside the parentheses using a regular expression
257
+ const blockHeightMatches = [...infoResult.matchAll(/verify BitcoinBlockHeaderAttestation\((\d+)\)/g)];
258
+ const blockHeights = blockHeightMatches.map(match => match[1]);
259
+
260
+ // Extract all values after "block merkle root" using a regular expression
261
+ const merkleRootMatches = [...infoResult.matchAll(/block merkle root ([a-f0-9]+)/gi)];
262
+ const merkleRoots = merkleRootMatches.map(match => match[1]);
263
+
264
+ // Group the values in a loop
265
+ result.attestationValues = [];
266
+ if (transactionIds.length === blockHeights.length && transactionIds.length === merkleRoots.length) {
267
+ for (let i = 0; i < transactionIds.length; i++) {
268
+ result.attestationValues.push({
269
+ blockHeight: blockHeights[i],
270
+ transactionId: transactionIds[i],
271
+ merkleRoot: merkleRoots[i]
272
+ });
273
+ }
274
+
275
+ // Sort the grouped values by blockHeight in ascending order
276
+ result.attestationValues.sort((a, b) => a.blockHeight - b.blockHeight);
277
+ }
278
+
279
+ return JSON.stringify(result);
280
+ }
281
+
282
+ // Parse the verifyResult into a TimestampInfo object
283
+ export async function parseVerifyResult(verifyResult: TimestampResult): Promise<TimestampInfo> {
284
+ let tsInfo: TimestampInfo = {
285
+ message: '',
286
+ results: [],
287
+ attestDate: null,
288
+ attestHeight: null,
289
+ submittedUri: null,
290
+ otsUpgraded: false
291
+ };
292
+
293
+ for (const chain in verifyResult?.results) {
294
+ let _results = verifyResult.results[chain];
295
+ const _date = moment(_results.timestamp * 1000)
296
+ .tz('UTC')
297
+ .format('MM-DD-YYYY hh:mma z');
298
+ const _blockchain = chain.charAt(0).toUpperCase() + chain.slice(1);
299
+ tsInfo.message = 'Validated - ' + _blockchain + ' block ' + _results.height + ' attests existence as of ' + _date;
300
+ if (tsInfo.attestHeight == null) {
301
+ tsInfo.attestDate = new Date(_results.timestamp * 1000);
302
+ tsInfo.attestHeight = _results.height;
303
+ }
304
+ tsInfo.results.push(tsInfo.message);
305
+ };
306
+
307
+ if (verifyResult?.upgradedOts) {
308
+ tsInfo.otsUpgraded = true;
309
+ tsInfo.results.push('Notice: Please select "Upgrade Timestamp" to save the upgraded timestamp.');
310
+ }
311
+
312
+ return tsInfo;
313
+ }
@@ -0,0 +1,23 @@
1
+ /**
2
+ * Type declarations for OpenTimestamps module
3
+ */
4
+ declare module 'opentimestamps' {
5
+ namespace stamp {
6
+ function create(fileBuffer: Buffer): Promise<Buffer>;
7
+ }
8
+
9
+ namespace verify {
10
+ function verifyTimestamp(ots: Buffer, fileHash: Buffer): Promise<any>;
11
+ }
12
+
13
+ namespace utils {
14
+ function hexToBytes(hex: string): number[];
15
+ function bytesToHex(bytes: number[]): string;
16
+ }
17
+
18
+ export default {
19
+ stamp,
20
+ verify,
21
+ utils
22
+ };
23
+ }