neozipkit 0.2.1 → 0.3.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.
- package/LICENSE +22 -0
- package/README.md +410 -186
- package/contracts/ABI.txt +889 -0
- package/contracts/Bytecode.txt +1 -0
- package/contracts/COMPILATION_NOTES.md +59 -0
- package/contracts/QUICK_DEPLOY.md +286 -0
- package/contracts/README.md +235 -131
- package/contracts/VERIFICATION.md +308 -0
- package/contracts/deploy-interactive.js +448 -0
- package/contracts/deployment-arbitrum-sepolia.json +12 -0
- package/contracts/deployment-arbitrum.json +12 -0
- package/contracts/deployment-eth-sepolia.json +12 -0
- package/contracts/hardhat.config.js +114 -0
- package/contracts/package.json +20 -0
- package/contracts/scripts/compile-and-extract.js +119 -0
- package/contracts/scripts/verify-contract.js +223 -0
- package/contracts/src/{NZIP-NFT-Private.sol → NZIP-NFT-v2.0.sol} +66 -151
- package/contracts/src/NZIP-NFT.sol +9 -15
- package/dist/blockchain/browser/TokenVerifierBrowser.d.ts +67 -0
- package/dist/blockchain/browser/TokenVerifierBrowser.d.ts.map +1 -0
- package/dist/blockchain/browser/TokenVerifierBrowser.js +313 -0
- package/dist/blockchain/browser/TokenVerifierBrowser.js.map +1 -0
- package/dist/blockchain/browser/WalletManagerBrowser.d.ts +98 -0
- package/dist/blockchain/browser/WalletManagerBrowser.d.ts.map +1 -0
- package/dist/blockchain/browser/WalletManagerBrowser.js +434 -0
- package/dist/blockchain/browser/WalletManagerBrowser.js.map +1 -0
- package/dist/blockchain/browser/ZipkitMinterBrowser.d.ts +58 -0
- package/dist/blockchain/browser/ZipkitMinterBrowser.d.ts.map +1 -0
- package/dist/blockchain/browser/ZipkitMinterBrowser.js +317 -0
- package/dist/blockchain/browser/ZipkitMinterBrowser.js.map +1 -0
- package/dist/blockchain/browser/index.d.ts +11 -0
- package/dist/blockchain/browser/index.d.ts.map +1 -0
- package/dist/blockchain/browser/index.js +16 -0
- package/dist/blockchain/browser/index.js.map +1 -0
- package/dist/blockchain/core/WalletManager.d.ts +124 -0
- package/dist/blockchain/core/WalletManager.d.ts.map +1 -0
- package/dist/blockchain/{ZipKitWallet.js → core/WalletManager.js} +157 -177
- package/dist/blockchain/core/WalletManager.js.map +1 -0
- package/dist/blockchain/{ZipKitMinter.d.ts → core/ZipkitMinter.d.ts} +46 -11
- package/dist/blockchain/core/ZipkitMinter.d.ts.map +1 -0
- package/dist/blockchain/core/ZipkitMinter.js +548 -0
- package/dist/blockchain/core/ZipkitMinter.js.map +1 -0
- package/dist/blockchain/{ZipKitOTS.d.ts → core/ZipkitOTS.d.ts} +6 -15
- package/dist/blockchain/core/ZipkitOTS.d.ts.map +1 -0
- package/dist/blockchain/{ZipKitOTS.js → core/ZipkitOTS.js} +26 -84
- package/dist/blockchain/core/ZipkitOTS.js.map +1 -0
- package/dist/blockchain/core/ZipkitVerifier.d.ts +177 -0
- package/dist/blockchain/core/ZipkitVerifier.d.ts.map +1 -0
- package/dist/blockchain/core/ZipkitVerifier.js +632 -0
- package/dist/blockchain/core/ZipkitVerifier.js.map +1 -0
- package/dist/blockchain/core/contracts.d.ts +83 -0
- package/dist/blockchain/core/contracts.d.ts.map +1 -0
- package/dist/blockchain/core/contracts.js +341 -0
- package/dist/blockchain/core/contracts.js.map +1 -0
- package/dist/blockchain/core/index.d.ts +12 -0
- package/dist/blockchain/core/index.d.ts.map +1 -0
- package/dist/blockchain/core/index.js +59 -0
- package/dist/blockchain/core/index.js.map +1 -0
- package/dist/blockchain/index.d.ts +11 -7
- package/dist/blockchain/index.d.ts.map +1 -0
- package/dist/blockchain/index.js +18 -34
- package/dist/blockchain/index.js.map +1 -0
- package/dist/blockchain/node/WalletManagerNode.d.ts +47 -0
- package/dist/blockchain/node/WalletManagerNode.d.ts.map +1 -0
- package/dist/blockchain/node/WalletManagerNode.js +203 -0
- package/dist/blockchain/node/WalletManagerNode.js.map +1 -0
- package/dist/blockchain/node/index.d.ts +12 -0
- package/dist/blockchain/node/index.d.ts.map +1 -0
- package/dist/blockchain/node/index.js +24 -0
- package/dist/blockchain/node/index.js.map +1 -0
- package/dist/browser/ZipkitBrowser.d.ts +27 -0
- package/dist/browser/ZipkitBrowser.d.ts.map +1 -0
- package/dist/browser/{ZipKitBrowser.js → ZipkitBrowser.js} +96 -43
- package/dist/browser/ZipkitBrowser.js.map +1 -0
- package/dist/browser/index.d.ts +12 -0
- package/dist/browser/index.d.ts.map +1 -0
- package/dist/browser/index.js +42 -0
- package/dist/browser/index.js.map +1 -0
- package/dist/browser-esm/index.d.ts +12 -0
- package/dist/browser-esm/index.js +59559 -0
- package/dist/browser-esm/index.js.map +7 -0
- package/dist/browser-umd/index.d.ts +12 -0
- package/dist/browser-umd/index.js +59576 -0
- package/dist/browser-umd/index.js.map +7 -0
- package/dist/browser-umd/index.min.js +63 -0
- package/dist/browser.d.ts +8 -14
- package/dist/browser.js +13 -28
- package/dist/core/ZipCompress.d.ts +99 -0
- package/dist/core/ZipCompress.d.ts.map +1 -0
- package/dist/core/ZipCompress.js +309 -0
- package/dist/core/ZipCompress.js.map +1 -0
- package/dist/core/ZipDecompress.d.ts +58 -0
- package/dist/core/ZipDecompress.d.ts.map +1 -0
- package/dist/core/ZipDecompress.js +172 -0
- package/dist/core/ZipDecompress.js.map +1 -0
- package/dist/{shared → core}/ZipEntry.d.ts +8 -1
- package/dist/core/ZipEntry.d.ts.map +1 -0
- package/dist/{shared → core}/ZipEntry.js +225 -82
- package/dist/core/ZipEntry.js.map +1 -0
- package/dist/core/Zipkit.d.ts +315 -0
- package/dist/core/Zipkit.d.ts.map +1 -0
- package/dist/core/Zipkit.js +647 -0
- package/dist/core/Zipkit.js.map +1 -0
- package/dist/core/components/HashCalculator.d.ts +138 -0
- package/dist/core/components/HashCalculator.d.ts.map +1 -0
- package/dist/core/components/HashCalculator.js +360 -0
- package/dist/core/components/HashCalculator.js.map +1 -0
- package/dist/core/components/Logger.d.ts +73 -0
- package/dist/core/components/Logger.d.ts.map +1 -0
- package/dist/core/components/Logger.js +156 -0
- package/dist/core/components/Logger.js.map +1 -0
- package/dist/core/components/ProgressTracker.d.ts +43 -0
- package/dist/core/components/ProgressTracker.d.ts.map +1 -0
- package/dist/core/components/ProgressTracker.js +112 -0
- package/dist/core/components/ProgressTracker.js.map +1 -0
- package/dist/{shared → core}/components/Support.d.ts +1 -0
- package/dist/core/components/Support.d.ts.map +1 -0
- package/dist/{shared → core}/components/Support.js +1 -0
- package/dist/core/components/Support.js.map +1 -0
- package/dist/{shared → core}/components/Util.d.ts +7 -2
- package/dist/core/components/Util.d.ts.map +1 -0
- package/dist/{shared → core}/components/Util.js +14 -34
- package/dist/core/components/Util.js.map +1 -0
- package/dist/{shared → core}/constants/Errors.d.ts +1 -2
- package/dist/core/constants/Errors.d.ts.map +1 -0
- package/dist/{shared → core}/constants/Errors.js +1 -3
- package/dist/core/constants/Errors.js.map +1 -0
- package/dist/{shared → core}/constants/Headers.d.ts +3 -0
- package/dist/core/constants/Headers.d.ts.map +1 -0
- package/dist/{shared → core}/constants/Headers.js +20 -17
- package/dist/core/constants/Headers.js.map +1 -0
- package/dist/core/encryption/Manager.d.ts +58 -0
- package/dist/core/encryption/Manager.d.ts.map +1 -0
- package/dist/core/encryption/Manager.js +121 -0
- package/dist/core/encryption/Manager.js.map +1 -0
- package/dist/core/encryption/ZipCrypto.d.ts +172 -0
- package/dist/core/encryption/ZipCrypto.d.ts.map +1 -0
- package/dist/core/encryption/ZipCrypto.js +554 -0
- package/dist/core/encryption/ZipCrypto.js.map +1 -0
- package/dist/core/encryption/index.d.ts +9 -0
- package/dist/core/encryption/index.d.ts.map +1 -0
- package/dist/core/encryption/index.js +17 -0
- package/dist/core/encryption/index.js.map +1 -0
- package/dist/core/encryption/types.d.ts +29 -0
- package/dist/core/encryption/types.d.ts.map +1 -0
- package/dist/core/encryption/types.js +12 -0
- package/dist/core/encryption/types.js.map +1 -0
- package/dist/core/index.d.ts +24 -0
- package/dist/core/index.d.ts.map +1 -0
- package/dist/core/index.js +57 -0
- package/dist/core/index.js.map +1 -0
- package/dist/core/version.d.ts +5 -0
- package/dist/core/version.d.ts.map +1 -0
- package/dist/core/version.js +31 -0
- package/dist/core/version.js.map +1 -0
- package/dist/index.d.ts +8 -18
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +29 -35
- package/dist/index.js.map +1 -0
- package/dist/node/ZipCompressNode.d.ts +123 -0
- package/dist/node/ZipCompressNode.d.ts.map +1 -0
- package/dist/node/ZipCompressNode.js +576 -0
- package/dist/node/ZipCompressNode.js.map +1 -0
- package/dist/node/ZipDecompressNode.d.ts +120 -0
- package/dist/node/ZipDecompressNode.d.ts.map +1 -0
- package/dist/node/ZipDecompressNode.js +461 -0
- package/dist/node/ZipDecompressNode.js.map +1 -0
- package/dist/node/ZipkitNode.d.ts +406 -0
- package/dist/node/ZipkitNode.d.ts.map +1 -0
- package/dist/node/ZipkitNode.js +1305 -0
- package/dist/node/ZipkitNode.js.map +1 -0
- package/dist/node/index.d.ts +26 -0
- package/dist/node/index.d.ts.map +1 -0
- package/dist/node/index.js +65 -0
- package/dist/node/index.js.map +1 -0
- package/dist/types/index.d.ts +59 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/index.js +36 -1
- package/dist/types/index.js.map +1 -0
- package/examples/README.md +269 -0
- package/examples/blockchain-tokenize.ts +354 -0
- package/examples/create-zip.ts +172 -0
- package/examples/extract-zip.ts +158 -0
- package/examples/list-zip.ts +161 -0
- package/examples/test-files/data.json +116 -0
- package/examples/test-files/document.md +80 -0
- package/examples/test-files/document.txt +2 -0
- package/examples/test-files/file1.txt +48 -0
- package/examples/test-files/file2.txt +80 -0
- package/examples/tsconfig.json +44 -0
- package/examples/unzip-token.ts +491 -0
- package/examples/verify-tokenized-zip.ts +305 -0
- package/examples/zip-token.ts +604 -0
- package/package.json +108 -24
- package/src/blockchain/README.md +59 -66
- package/src/{browser/TokenVerifierBrowser.ts-remove → blockchain/browser/TokenVerifierBrowser.ts} +125 -79
- package/src/blockchain/browser/WalletManagerBrowser.ts +486 -0
- package/src/blockchain/browser/ZipkitMinterBrowser.ts +400 -0
- package/src/blockchain/browser/index.ts +13 -0
- package/src/blockchain/{ZipKitWallet.ts → core/WalletManager.ts} +241 -143
- package/src/blockchain/core/ZipkitMinter.ts +716 -0
- package/src/blockchain/{ZipKitOTS.ts → core/ZipkitOTS.ts} +28 -62
- package/src/blockchain/core/ZipkitVerifier.ts +853 -0
- package/src/blockchain/core/contracts.ts +370 -0
- package/src/blockchain/core/index.ts +71 -0
- package/src/blockchain/index.ts +21 -55
- package/src/blockchain/node/WalletManagerNode.ts +171 -0
- package/src/blockchain/node/index.ts +40 -0
- package/src/browser/{ZipKitBrowser.ts → ZipkitBrowser.ts} +104 -41
- package/src/browser/index.esm.ts +37 -0
- package/src/browser/index.ts +25 -0
- package/src/core/ZipCompress.ts +398 -0
- package/src/core/ZipDecompress.ts +212 -0
- package/src/{shared → core}/ZipEntry.ts +240 -90
- package/src/core/Zipkit.ts +794 -0
- package/src/core/components/HashCalculator.ts +384 -0
- package/src/core/components/Logger.ts +180 -0
- package/src/core/components/ProgressTracker.ts +134 -0
- package/src/{shared → core}/components/Util.ts +12 -34
- package/src/{shared → core}/constants/Errors.ts +0 -4
- package/src/{shared → core}/constants/Headers.ts +19 -17
- package/src/core/encryption/Manager.ts +137 -0
- package/src/core/encryption/ZipCrypto.ts +650 -0
- package/src/core/encryption/index.ts +15 -0
- package/src/core/encryption/types.ts +33 -0
- package/src/core/index.ts +34 -0
- package/src/core/version.ts +33 -0
- package/src/index.ts +54 -25
- package/src/node/ZipCompressNode.ts +632 -0
- package/src/node/ZipDecompressNode.ts +508 -0
- package/src/node/ZipkitNode.ts +1559 -0
- package/src/node/index.ts +69 -0
- package/src/types/index.ts +131 -1
- package/src/types/modules.d.ts +1 -11
- package/src/types/opentimestamps.d.ts +1 -23
- package/contracts/INTEGRATION.md +0 -338
- package/contracts/NETWORKS.md +0 -156
- package/contracts/NZIP-NFT-bytecode.txt +0 -1
- package/contracts/constants.ts +0 -952
- package/contracts/deploy-bytecode.mjs +0 -94
- package/dist/blockchain/CertificateToken.d.ts +0 -48
- package/dist/blockchain/CertificateToken.js +0 -386
- package/dist/blockchain/ZipKitMinter.js +0 -347
- package/dist/blockchain/ZipKitNFT.d.ts +0 -3
- package/dist/blockchain/ZipKitNFT.js +0 -7
- package/dist/blockchain/ZipKitVerifier.d.ts +0 -69
- package/dist/blockchain/ZipKitVerifier.js +0 -289
- package/dist/blockchain/ZipKitWallet.d.ts +0 -69
- package/dist/blockchain/contracts.d.ts +0 -14
- package/dist/blockchain/contracts.js +0 -77
- package/dist/blockchain/types.d.ts +0 -47
- package/dist/blockchain/types.js +0 -30
- package/dist/browser/ZipKitBrowser.d.ts +0 -18
- package/dist/browser/ZipKitStampBrowser.d.ts +0 -2
- package/dist/browser/ZipKitStampBrowser.js +0 -12
- package/dist/browser-index.d.ts +0 -18
- package/dist/browser-index.js +0 -57
- package/dist/server/ZipKitServer.d.ts +0 -74
- package/dist/server/ZipKitServer.js +0 -310
- package/dist/shared/ZipKit.d.ts +0 -243
- package/dist/shared/ZipKit.js +0 -772
- package/dist/shared/components/CertificateOTS.d.ts +0 -15
- package/dist/shared/components/CertificateOTS.js +0 -116
- package/dist/shared/components/FileSys.d.ts +0 -2
- package/dist/shared/components/FileSys.js +0 -113
- package/dist/shared/components/HashAccumulator.d.ts +0 -55
- package/dist/shared/components/HashAccumulator.js +0 -100
- package/dist/shared/components/ZipCrypto.d.ts +0 -3
- package/dist/shared/components/ZipCrypto.js +0 -78
- package/dist/shared/components/merkleTree.d.ts +0 -66
- package/dist/shared/components/merkleTree.js +0 -136
- package/dist/shared/types/index.d.ts +0 -21
- package/dist/shared/types/index.js +0 -3
- package/dist/shared/types.d.ts +0 -33
- package/dist/shared/types.js +0 -8
- package/dist/shared/version.d.ts +0 -2
- package/dist/shared/version.js +0 -6
- package/src/blockchain/CertificateToken.ts +0 -444
- package/src/blockchain/ZipKitMinter.ts +0 -470
- package/src/blockchain/ZipKitNFT.ts +0 -12
- package/src/blockchain/ZipKitVerifier.ts +0 -382
- package/src/blockchain/contracts.ts +0 -88
- package/src/blockchain/types.ts +0 -91
- package/src/browser/CertificateGeneratorBrowser.ts-remove +0 -498
- package/src/browser/ZipKitStampBrowser.ts +0 -9
- package/src/browser-index.ts +0 -36
- package/src/server/README.md +0 -25
- package/src/server/ZipKitServer.ts +0 -305
- package/src/shared/ZipKit.ts +0 -881
- package/src/shared/components/CertificateOTS.ts +0 -132
- package/src/shared/components/FileSys.ts +0 -87
- package/src/shared/components/HashAccumulator.ts +0 -113
- package/src/shared/components/ZipCrypto.ts +0 -91
- package/src/shared/components/merkleTree.ts +0 -162
- package/src/shared/types/index.ts +0 -25
- package/src/shared/types/modules.d.ts +0 -22
- package/src/shared/types/opentimestamps.d.ts +0 -23
- package/src/shared/types.d.ts +0 -8
- package/src/shared/types.ts +0 -43
- package/src/shared/version.ts +0 -3
- /package/src/{shared → core}/components/Support.ts +0 -0
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import ZipEntry from '../ZipEntry';
|
|
2
|
-
import type { AttestationValue } from '../types';
|
|
3
|
-
/**
|
|
4
|
-
* Generates a certificate HTML document based on timestamp information
|
|
5
|
-
*
|
|
6
|
-
* @param attestStr - Attestation message
|
|
7
|
-
* @param zipFile - Name of the zip file
|
|
8
|
-
* @param hashDigest - Hash digest of the zip file
|
|
9
|
-
* @param needsUpdateMsg - Message indicating if update is needed
|
|
10
|
-
* @param attestationValues - Array of attestation values
|
|
11
|
-
* @param zipEntries - Array of ZipEntry objects
|
|
12
|
-
* @param narrow - Whether to use narrow layout
|
|
13
|
-
* @returns Generated HTML certificate as string
|
|
14
|
-
*/
|
|
15
|
-
export default function CertificateTemplate(attestStr: string | null, zipFile: string, hashDigest: string, needsUpdateMsg: string, attestationValues: AttestationValue[], zipEntries: ZipEntry[], narrow?: boolean): string;
|
|
@@ -1,116 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
// ======================================
|
|
3
|
-
// CertificateOTS.ts
|
|
4
|
-
// Copyright (c) 2025 NeoWare, Inc. All rights reserved.
|
|
5
|
-
// ======================================
|
|
6
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
7
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
8
|
-
};
|
|
9
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
|
-
exports.default = CertificateTemplate;
|
|
11
|
-
const fs_1 = __importDefault(require("fs"));
|
|
12
|
-
const path_1 = __importDefault(require("path"));
|
|
13
|
-
const Headers_1 = require("../constants/Headers");
|
|
14
|
-
/**
|
|
15
|
-
* Generates a certificate HTML document based on timestamp information
|
|
16
|
-
*
|
|
17
|
-
* @param attestStr - Attestation message
|
|
18
|
-
* @param zipFile - Name of the zip file
|
|
19
|
-
* @param hashDigest - Hash digest of the zip file
|
|
20
|
-
* @param needsUpdateMsg - Message indicating if update is needed
|
|
21
|
-
* @param attestationValues - Array of attestation values
|
|
22
|
-
* @param zipEntries - Array of ZipEntry objects
|
|
23
|
-
* @param narrow - Whether to use narrow layout
|
|
24
|
-
* @returns Generated HTML certificate as string
|
|
25
|
-
*/
|
|
26
|
-
function CertificateTemplate(attestStr, zipFile, hashDigest, needsUpdateMsg, attestationValues, zipEntries, narrow) {
|
|
27
|
-
// Load the HTML template
|
|
28
|
-
let templatePath;
|
|
29
|
-
try {
|
|
30
|
-
// Try to find the template from the package root
|
|
31
|
-
templatePath = path_1.default.resolve(process.cwd(), 'assets/templates/certificate.html');
|
|
32
|
-
if (!fs_1.default.existsSync(templatePath)) {
|
|
33
|
-
// If not found in current working directory, try relative to this file
|
|
34
|
-
templatePath = path_1.default.resolve(__dirname, '../../assets/templates/certificate.html');
|
|
35
|
-
}
|
|
36
|
-
if (!fs_1.default.existsSync(templatePath)) {
|
|
37
|
-
throw new Error('Certificate template not found');
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
catch (error) {
|
|
41
|
-
console.error('Error resolving template path:', error);
|
|
42
|
-
return '';
|
|
43
|
-
}
|
|
44
|
-
// Read the template file
|
|
45
|
-
let htmlTemplate;
|
|
46
|
-
try {
|
|
47
|
-
htmlTemplate = fs_1.default.readFileSync(templatePath, 'utf8');
|
|
48
|
-
}
|
|
49
|
-
catch (error) {
|
|
50
|
-
console.error('Error reading template file:', error);
|
|
51
|
-
return '';
|
|
52
|
-
}
|
|
53
|
-
const updateWarning = needsUpdateMsg || '';
|
|
54
|
-
// Generate attestation HTML
|
|
55
|
-
let attestHTML = '';
|
|
56
|
-
if (attestationValues && attestationValues.length > 0) {
|
|
57
|
-
attestationValues.forEach((attest) => {
|
|
58
|
-
attestHTML += `
|
|
59
|
-
<div class="key-value-pair">
|
|
60
|
-
<div class="key">Block Height:</div>
|
|
61
|
-
<div class="value">${attest['blockHeight']}</div>
|
|
62
|
-
</div>
|
|
63
|
-
<div class="key-value-pair">
|
|
64
|
-
<div class="key">Transaction:</div>
|
|
65
|
-
<div class="value">${attest['transactionId']}</div>
|
|
66
|
-
</div>
|
|
67
|
-
<div class="key-value-pair">
|
|
68
|
-
<div class="key">Merkle Root:</div>
|
|
69
|
-
<div class="value">${attest['merkleRoot']}</div>
|
|
70
|
-
</div>
|
|
71
|
-
<br/>
|
|
72
|
-
`;
|
|
73
|
-
});
|
|
74
|
-
}
|
|
75
|
-
// Generate files table HTML
|
|
76
|
-
let stampEntry = null;
|
|
77
|
-
let files = [];
|
|
78
|
-
for (const entry of zipEntries) {
|
|
79
|
-
if (entry.filename === Headers_1.TIMESTAMP_METADATA) {
|
|
80
|
-
stampEntry = entry;
|
|
81
|
-
}
|
|
82
|
-
else if (entry.filename === Headers_1.TIMESTAMP_SUBMITTED) {
|
|
83
|
-
if (stampEntry == null)
|
|
84
|
-
stampEntry = entry;
|
|
85
|
-
}
|
|
86
|
-
else if (entry.sha256) {
|
|
87
|
-
let file = {
|
|
88
|
-
name: entry.filename,
|
|
89
|
-
size: entry.uncompressedSize.toString(),
|
|
90
|
-
date: entry.toFormattedUTCDateString(),
|
|
91
|
-
hash: entry.sha256,
|
|
92
|
-
};
|
|
93
|
-
files.push(file);
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
let filesTableHTML = '';
|
|
97
|
-
files.forEach((file) => {
|
|
98
|
-
filesTableHTML += `
|
|
99
|
-
<tr>
|
|
100
|
-
<td class="table-data1">${file.name}</td>
|
|
101
|
-
<td class="table-data">${file.size}</td>
|
|
102
|
-
<td class="table-data">${file.date}</td>
|
|
103
|
-
<td class="table-data">${file.hash}</td>
|
|
104
|
-
</tr>
|
|
105
|
-
`;
|
|
106
|
-
});
|
|
107
|
-
// Replace placeholders in the template
|
|
108
|
-
let html = htmlTemplate
|
|
109
|
-
.replace(/{{ATTEST_STR}}/g, attestStr || '')
|
|
110
|
-
.replace(/{{UPDATE_WARNING}}/g, needsUpdateMsg ? `<div class="warning">${needsUpdateMsg}</div>` : '')
|
|
111
|
-
.replace(/{{ZIP_FILE}}/g, zipFile)
|
|
112
|
-
.replace(/{{HASH_DIGEST}}/g, hashDigest)
|
|
113
|
-
.replace(/{{ATTESTATION_HTML}}/g, attestHTML)
|
|
114
|
-
.replace(/{{FILES_TABLE_HTML}}/g, filesTableHTML);
|
|
115
|
-
return html;
|
|
116
|
-
}
|
|
@@ -1,113 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
// ======================================
|
|
3
|
-
// FileSys.ts
|
|
4
|
-
// Copyright (c) 2024 NeoWare, Inc. All rights reserved.
|
|
5
|
-
// ======================================
|
|
6
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
7
|
-
if (k2 === undefined) k2 = k;
|
|
8
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
9
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
10
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
11
|
-
}
|
|
12
|
-
Object.defineProperty(o, k2, desc);
|
|
13
|
-
}) : (function(o, m, k, k2) {
|
|
14
|
-
if (k2 === undefined) k2 = k;
|
|
15
|
-
o[k2] = m[k];
|
|
16
|
-
}));
|
|
17
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
18
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
19
|
-
}) : function(o, v) {
|
|
20
|
-
o["default"] = v;
|
|
21
|
-
});
|
|
22
|
-
var __importStar = (this && this.__importStar) || (function () {
|
|
23
|
-
var ownKeys = function(o) {
|
|
24
|
-
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
25
|
-
var ar = [];
|
|
26
|
-
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
27
|
-
return ar;
|
|
28
|
-
};
|
|
29
|
-
return ownKeys(o);
|
|
30
|
-
};
|
|
31
|
-
return function (mod) {
|
|
32
|
-
if (mod && mod.__esModule) return mod;
|
|
33
|
-
var result = {};
|
|
34
|
-
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
35
|
-
__setModuleDefault(result, mod);
|
|
36
|
-
return result;
|
|
37
|
-
};
|
|
38
|
-
})();
|
|
39
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
40
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
41
|
-
};
|
|
42
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
43
|
-
exports.outputToFile = outputToFile;
|
|
44
|
-
exports.makeDir = makeDir;
|
|
45
|
-
const fs = __importStar(require("fs"));
|
|
46
|
-
const pth = __importStar(require("path"));
|
|
47
|
-
const Errors_1 = __importDefault(require("../constants/Errors"));
|
|
48
|
-
// Output a buffer to a file
|
|
49
|
-
function outputToFile(fileName, buffer, dirPath, overwrite, never, fileAttr) {
|
|
50
|
-
// get Unix file permissions
|
|
51
|
-
const _fattr = fileAttr ? (((fileAttr >>> 0) | 0) >> 16) & 0xfff : 0;
|
|
52
|
-
const _path = dirPath ? dirPath + pth.sep + fileName : fileName;
|
|
53
|
-
if (fs.existsSync(_path)) {
|
|
54
|
-
console.log('File exists: ', _path);
|
|
55
|
-
if (never)
|
|
56
|
-
return false;
|
|
57
|
-
if (!overwrite) {
|
|
58
|
-
console.log('Overwrite file: ', _path);
|
|
59
|
-
return false; // cannot overwrite
|
|
60
|
-
}
|
|
61
|
-
const stat = fs.statSync(_path);
|
|
62
|
-
if (stat.isDirectory())
|
|
63
|
-
return false;
|
|
64
|
-
}
|
|
65
|
-
// Check if we need to create a new folder
|
|
66
|
-
const _folder = pth.dirname(_path);
|
|
67
|
-
if (!fs.existsSync(_folder)) {
|
|
68
|
-
makeDir(_folder);
|
|
69
|
-
}
|
|
70
|
-
var fd;
|
|
71
|
-
try {
|
|
72
|
-
fd = fs.openSync(_path, "w", 438); // 0666
|
|
73
|
-
}
|
|
74
|
-
catch (e) {
|
|
75
|
-
fs.chmodSync(_path, 438);
|
|
76
|
-
fd = fs.openSync(_path, "w", 438);
|
|
77
|
-
}
|
|
78
|
-
if (fd) {
|
|
79
|
-
try {
|
|
80
|
-
fs.writeSync(fd, buffer, 0, buffer.length, 0);
|
|
81
|
-
}
|
|
82
|
-
finally {
|
|
83
|
-
fs.closeSync(fd);
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
if (_fattr)
|
|
87
|
-
fs.chmodSync(_path, _fattr);
|
|
88
|
-
return true;
|
|
89
|
-
}
|
|
90
|
-
// Make directories tree for a given path
|
|
91
|
-
function makeDir(folder) {
|
|
92
|
-
console.log('mkDir: ', folder);
|
|
93
|
-
// Sync - make directories tree
|
|
94
|
-
function mkdirSync(fpath) {
|
|
95
|
-
let resolvedPath = fpath.split(pth.sep)[0];
|
|
96
|
-
fpath.split(pth.sep).forEach(function (name) {
|
|
97
|
-
if (!name || name.endsWith(":"))
|
|
98
|
-
return;
|
|
99
|
-
resolvedPath += pth.sep + name;
|
|
100
|
-
var stat;
|
|
101
|
-
try {
|
|
102
|
-
stat = fs.statSync(resolvedPath);
|
|
103
|
-
}
|
|
104
|
-
catch (e) {
|
|
105
|
-
fs.mkdirSync(resolvedPath);
|
|
106
|
-
}
|
|
107
|
-
if (stat && stat.isFile())
|
|
108
|
-
throw Errors_1.default.FILE_IN_THE_WAY.replace("%s", resolvedPath);
|
|
109
|
-
});
|
|
110
|
-
}
|
|
111
|
-
mkdirSync(folder);
|
|
112
|
-
}
|
|
113
|
-
;
|
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Class for accumulating and combining SHA-256 hashes using both XOR and Merkle tree methods
|
|
3
|
-
*
|
|
4
|
-
* Usage example:
|
|
5
|
-
* ```typescript
|
|
6
|
-
* const accumulator = new HashAccumulator();
|
|
7
|
-
* accumulator.addHash('a1b2c3d4');
|
|
8
|
-
* accumulator.addHash('12345678');
|
|
9
|
-
* console.log(accumulator.xorHash()); // Outputs the accumulated XOR result
|
|
10
|
-
* console.log(accumulator.merkleRoot()); // Outputs the accumulated Merkle root
|
|
11
|
-
* ```
|
|
12
|
-
*/
|
|
13
|
-
export default class HashAccumulator {
|
|
14
|
-
private hashes;
|
|
15
|
-
private xorResult;
|
|
16
|
-
/**
|
|
17
|
-
* Creates a new HashAccumulator instance
|
|
18
|
-
* Initializes empty hash array and zero-filled XOR buffer
|
|
19
|
-
*/
|
|
20
|
-
constructor();
|
|
21
|
-
/**
|
|
22
|
-
* Adds a hash to both the XOR accumulation and hash array
|
|
23
|
-
* @param hash - Hash value as hex string or Buffer
|
|
24
|
-
* The hash must be 32 bytes (SHA-256)
|
|
25
|
-
*/
|
|
26
|
-
addHash(hash: string | Buffer): void;
|
|
27
|
-
/**
|
|
28
|
-
* Gets the accumulated XOR of all added hashes
|
|
29
|
-
* @returns Hex string of XOR result
|
|
30
|
-
*/
|
|
31
|
-
xorHash(): string;
|
|
32
|
-
/**
|
|
33
|
-
* Creates a Merkle tree from accumulated hashes and returns the root
|
|
34
|
-
* @returns Hex string of Merkle root, or null if no hashes added
|
|
35
|
-
*/
|
|
36
|
-
merkleRoot(): string | null;
|
|
37
|
-
/**
|
|
38
|
-
* Gets the number of hashes accumulated
|
|
39
|
-
* @returns Number of accumulated hashes
|
|
40
|
-
*/
|
|
41
|
-
leafCount(): number;
|
|
42
|
-
/**
|
|
43
|
-
* Resets the accumulator to initial state
|
|
44
|
-
* Clears hash array and zeroes XOR buffer
|
|
45
|
-
*/
|
|
46
|
-
clear(): void;
|
|
47
|
-
/**
|
|
48
|
-
* Combines multiple hash results into a single Merkle root
|
|
49
|
-
* @param results - Array of objects containing merkleRoot properties
|
|
50
|
-
* @returns Combined Merkle root as hex string, or null if no valid roots
|
|
51
|
-
*/
|
|
52
|
-
static combineResults(results: {
|
|
53
|
-
merkleRoot?: string;
|
|
54
|
-
}[]): string | null;
|
|
55
|
-
}
|
|
@@ -1,100 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
// ======================================
|
|
3
|
-
// HashAccumulator.ts
|
|
4
|
-
// ======================================
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
const merkleTree_1 = require("./merkleTree");
|
|
7
|
-
/**
|
|
8
|
-
* Class for accumulating and combining SHA-256 hashes using both XOR and Merkle tree methods
|
|
9
|
-
*
|
|
10
|
-
* Usage example:
|
|
11
|
-
* ```typescript
|
|
12
|
-
* const accumulator = new HashAccumulator();
|
|
13
|
-
* accumulator.addHash('a1b2c3d4');
|
|
14
|
-
* accumulator.addHash('12345678');
|
|
15
|
-
* console.log(accumulator.xorHash()); // Outputs the accumulated XOR result
|
|
16
|
-
* console.log(accumulator.merkleRoot()); // Outputs the accumulated Merkle root
|
|
17
|
-
* ```
|
|
18
|
-
*/
|
|
19
|
-
class HashAccumulator {
|
|
20
|
-
/**
|
|
21
|
-
* Creates a new HashAccumulator instance
|
|
22
|
-
* Initializes empty hash array and zero-filled XOR buffer
|
|
23
|
-
*/
|
|
24
|
-
constructor() {
|
|
25
|
-
this.hashes = [];
|
|
26
|
-
this.xorResult = Buffer.alloc(32, 0);
|
|
27
|
-
}
|
|
28
|
-
/**
|
|
29
|
-
* Adds a hash to both the XOR accumulation and hash array
|
|
30
|
-
* @param hash - Hash value as hex string or Buffer
|
|
31
|
-
* The hash must be 32 bytes (SHA-256)
|
|
32
|
-
*/
|
|
33
|
-
addHash(hash) {
|
|
34
|
-
// Convert hash to Buffer if it's a string
|
|
35
|
-
const hashBuffer = typeof hash === 'string'
|
|
36
|
-
? Buffer.from(hash, 'hex')
|
|
37
|
-
: hash;
|
|
38
|
-
// Add to array for Merkle tree
|
|
39
|
-
this.hashes.push(hashBuffer);
|
|
40
|
-
// XOR accumulation
|
|
41
|
-
for (let i = 0; i < 32; i++) {
|
|
42
|
-
this.xorResult[i] ^= hashBuffer[i];
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
/**
|
|
46
|
-
* Gets the accumulated XOR of all added hashes
|
|
47
|
-
* @returns Hex string of XOR result
|
|
48
|
-
*/
|
|
49
|
-
xorHash() {
|
|
50
|
-
return this.xorResult.toString('hex');
|
|
51
|
-
}
|
|
52
|
-
/**
|
|
53
|
-
* Creates a Merkle tree from accumulated hashes and returns the root
|
|
54
|
-
* @returns Hex string of Merkle root, or null if no hashes added
|
|
55
|
-
*/
|
|
56
|
-
merkleRoot() {
|
|
57
|
-
let merkleRoot = null;
|
|
58
|
-
if (this.hashes.length > 0) {
|
|
59
|
-
const tree = new merkleTree_1.MerkleTree(this.hashes, {
|
|
60
|
-
hashLeaves: false, // Leaves are already hashed
|
|
61
|
-
sortLeaves: true, // Sort leaves for consistent results
|
|
62
|
-
sortPairs: true, // Sort pairs for consistent results
|
|
63
|
-
duplicateOdd: true // Duplicate odd leaf for balanced tree
|
|
64
|
-
});
|
|
65
|
-
merkleRoot = tree.getRoot().toString('hex');
|
|
66
|
-
}
|
|
67
|
-
return merkleRoot;
|
|
68
|
-
}
|
|
69
|
-
/**
|
|
70
|
-
* Gets the number of hashes accumulated
|
|
71
|
-
* @returns Number of accumulated hashes
|
|
72
|
-
*/
|
|
73
|
-
leafCount() {
|
|
74
|
-
return this.hashes.length;
|
|
75
|
-
}
|
|
76
|
-
/**
|
|
77
|
-
* Resets the accumulator to initial state
|
|
78
|
-
* Clears hash array and zeroes XOR buffer
|
|
79
|
-
*/
|
|
80
|
-
clear() {
|
|
81
|
-
this.hashes = [];
|
|
82
|
-
this.xorResult = Buffer.alloc(32, 0);
|
|
83
|
-
}
|
|
84
|
-
/**
|
|
85
|
-
* Combines multiple hash results into a single Merkle root
|
|
86
|
-
* @param results - Array of objects containing merkleRoot properties
|
|
87
|
-
* @returns Combined Merkle root as hex string, or null if no valid roots
|
|
88
|
-
*/
|
|
89
|
-
static combineResults(results) {
|
|
90
|
-
const accumulator = new HashAccumulator();
|
|
91
|
-
// Combine all hashes
|
|
92
|
-
for (const result of results) {
|
|
93
|
-
if (result.merkleRoot) {
|
|
94
|
-
accumulator.addHash(result.merkleRoot);
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
return accumulator.merkleRoot();
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
exports.default = HashAccumulator;
|
|
@@ -1,78 +0,0 @@
|
|
|
1
|
-
// ======================================
|
|
2
|
-
// ZipCrypto.ts
|
|
3
|
-
// Copyright (c) 2024 NeoWare, Inc. All rights reserved.
|
|
4
|
-
// ======================================
|
|
5
|
-
// Zip Cryptography Routines
|
|
6
|
-
"use strict";
|
|
7
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
|
-
exports.sha256 = sha256;
|
|
9
|
-
exports.encrypter = encrypter;
|
|
10
|
-
exports.decrypter = decrypter;
|
|
11
|
-
const crypto_1 = require("crypto");
|
|
12
|
-
const Util_1 = require("./Util");
|
|
13
|
-
const Headers_1 = require("../constants/Headers");
|
|
14
|
-
// Return the SHA256 hash of a buffer
|
|
15
|
-
function sha256(data) {
|
|
16
|
-
const hash = (0, crypto_1.createHash)('sha256');
|
|
17
|
-
return hash
|
|
18
|
-
.update(data)
|
|
19
|
-
.digest('hex');
|
|
20
|
-
}
|
|
21
|
-
class InitKeys {
|
|
22
|
-
constructor(pw) {
|
|
23
|
-
this.keys = new Uint32Array([0x12345678, 0x23456789, 0x34567890]);
|
|
24
|
-
const pass = typeof pw === 'string' ? Buffer.from(pw) : pw;
|
|
25
|
-
for (let i = 0; i < pass.length; i++) {
|
|
26
|
-
this.updateKeys(pass[i]);
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
updateKeys(byteValue) {
|
|
30
|
-
this.keys[0] = (0, Util_1.crc32update)(this.keys[0], byteValue);
|
|
31
|
-
this.keys[1] = ((this.keys[1] + (this.keys[0] & 0xff)) * 0x08088405 + 1) >>> 0;
|
|
32
|
-
this.keys[2] = (0, Util_1.crc32update)(this.keys[2], this.keys[1] >>> 24);
|
|
33
|
-
return byteValue;
|
|
34
|
-
}
|
|
35
|
-
next() {
|
|
36
|
-
const k = (this.keys[2] | 2) >>> 0; // key
|
|
37
|
-
return ((0, Util_1.uMul)(k, k ^ 1) >> 8) & 0xff; // decode
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
// Return encrypting function, result and pos is here so we dont have to merge buffers later
|
|
41
|
-
function make_encrypter(pwd) {
|
|
42
|
-
const keys = new InitKeys(pwd);
|
|
43
|
-
return function (data, result, pos = 0) {
|
|
44
|
-
// result - we create new Buffer for results
|
|
45
|
-
if (!result)
|
|
46
|
-
result = Buffer.alloc(data.length);
|
|
47
|
-
// process input data
|
|
48
|
-
for (let c of data) {
|
|
49
|
-
const k = keys.next(); // save key byte
|
|
50
|
-
result[pos++] = c ^ k; // save val
|
|
51
|
-
keys.updateKeys(c); // update keys with decoded byte
|
|
52
|
-
}
|
|
53
|
-
return result;
|
|
54
|
-
};
|
|
55
|
-
}
|
|
56
|
-
// Encrypt data with password and include a checkByte verification byte at the end
|
|
57
|
-
function encrypter(data, checkByte, pwd) {
|
|
58
|
-
if (!data)
|
|
59
|
-
return null;
|
|
60
|
-
const _encrypter = make_encrypter(pwd);
|
|
61
|
-
const salt = (0, crypto_1.randomFillSync)(Buffer.alloc(Headers_1.ENCRYPT_HDR_SIZE));
|
|
62
|
-
salt[Headers_1.ENCRYPT_HDR_SIZE - 1] = checkByte;
|
|
63
|
-
// create encryption header
|
|
64
|
-
const result = Buffer.alloc(data.length + Headers_1.ENCRYPT_HDR_SIZE);
|
|
65
|
-
_encrypter(salt, result);
|
|
66
|
-
// encode content
|
|
67
|
-
return _encrypter(data, result, Headers_1.ENCRYPT_HDR_SIZE);
|
|
68
|
-
}
|
|
69
|
-
// Return the decrypter function
|
|
70
|
-
function decrypter(pwd) {
|
|
71
|
-
const keys = new InitKeys(pwd);
|
|
72
|
-
return function (data) {
|
|
73
|
-
for (let i = 0; i < data.length; i++) {
|
|
74
|
-
data[i] = keys.updateKeys(data[i] ^ keys.next());
|
|
75
|
-
}
|
|
76
|
-
return data;
|
|
77
|
-
};
|
|
78
|
-
}
|
|
@@ -1,66 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Configuration options for Merkle tree construction
|
|
3
|
-
*/
|
|
4
|
-
interface MerkleTreeOptions {
|
|
5
|
-
hashLeaves: boolean;
|
|
6
|
-
sortLeaves: boolean;
|
|
7
|
-
sortPairs: boolean;
|
|
8
|
-
duplicateOdd: boolean;
|
|
9
|
-
}
|
|
10
|
-
/**
|
|
11
|
-
* Implementation of a Merkle Tree (hash tree) using SHA-256
|
|
12
|
-
*
|
|
13
|
-
* A Merkle tree is a binary tree where each non-leaf node is the hash of its two children.
|
|
14
|
-
* This allows efficient verification of large datasets by only storing/transmitting the root hash
|
|
15
|
-
* and generating proofs of inclusion for individual items.
|
|
16
|
-
*/
|
|
17
|
-
export declare class MerkleTree {
|
|
18
|
-
private leaves;
|
|
19
|
-
private layers;
|
|
20
|
-
private options;
|
|
21
|
-
/**
|
|
22
|
-
* Creates a new Merkle tree from an array of leaf nodes
|
|
23
|
-
* @param leaves - Array of leaf node data as Buffers
|
|
24
|
-
* @param options - Configuration options for tree construction
|
|
25
|
-
*/
|
|
26
|
-
constructor(leaves: Buffer[], options?: Partial<MerkleTreeOptions>);
|
|
27
|
-
/**
|
|
28
|
-
* Computes SHA-256 hash of input data
|
|
29
|
-
* @param data - Data to hash
|
|
30
|
-
* @returns Buffer containing hash
|
|
31
|
-
*/
|
|
32
|
-
private hash;
|
|
33
|
-
/**
|
|
34
|
-
* Combines two child hashes to create parent hash
|
|
35
|
-
* @param left - Left child hash
|
|
36
|
-
* @param right - Right child hash
|
|
37
|
-
* @returns Combined hash of children
|
|
38
|
-
*/
|
|
39
|
-
private combinedHash;
|
|
40
|
-
/**
|
|
41
|
-
* Builds the Merkle tree by creating successive layers of hashes
|
|
42
|
-
* @param nodes - Array of nodes in current layer
|
|
43
|
-
*/
|
|
44
|
-
private createHashes;
|
|
45
|
-
/**
|
|
46
|
-
* Gets the root hash of the Merkle tree
|
|
47
|
-
* @returns Buffer containing root hash
|
|
48
|
-
*/
|
|
49
|
-
getRoot(): Buffer;
|
|
50
|
-
/**
|
|
51
|
-
* Generates a proof of inclusion for a leaf node
|
|
52
|
-
* @param leaf - The leaf node to generate proof for
|
|
53
|
-
* @returns Array of sibling hashes needed to reconstruct root
|
|
54
|
-
* @throws Error if leaf not found in tree
|
|
55
|
-
*/
|
|
56
|
-
getProof(leaf: Buffer): Buffer[];
|
|
57
|
-
/**
|
|
58
|
-
* Verifies a proof of inclusion for a leaf node
|
|
59
|
-
* @param proof - Array of sibling hashes from getProof()
|
|
60
|
-
* @param targetHash - Hash of the leaf node being verified
|
|
61
|
-
* @param root - Expected root hash
|
|
62
|
-
* @returns boolean indicating if proof is valid
|
|
63
|
-
*/
|
|
64
|
-
verify(proof: Buffer[], targetHash: Buffer, root: Buffer): boolean;
|
|
65
|
-
}
|
|
66
|
-
export {};
|
|
@@ -1,136 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.MerkleTree = void 0;
|
|
4
|
-
const crypto_1 = require("crypto");
|
|
5
|
-
/**
|
|
6
|
-
* Implementation of a Merkle Tree (hash tree) using SHA-256
|
|
7
|
-
*
|
|
8
|
-
* A Merkle tree is a binary tree where each non-leaf node is the hash of its two children.
|
|
9
|
-
* This allows efficient verification of large datasets by only storing/transmitting the root hash
|
|
10
|
-
* and generating proofs of inclusion for individual items.
|
|
11
|
-
*/
|
|
12
|
-
class MerkleTree {
|
|
13
|
-
/**
|
|
14
|
-
* Creates a new Merkle tree from an array of leaf nodes
|
|
15
|
-
* @param leaves - Array of leaf node data as Buffers
|
|
16
|
-
* @param options - Configuration options for tree construction
|
|
17
|
-
*/
|
|
18
|
-
constructor(leaves, options = {}) {
|
|
19
|
-
this.options = {
|
|
20
|
-
hashLeaves: false,
|
|
21
|
-
sortLeaves: true,
|
|
22
|
-
sortPairs: true,
|
|
23
|
-
duplicateOdd: true,
|
|
24
|
-
...options
|
|
25
|
-
};
|
|
26
|
-
// Process leaves according to options
|
|
27
|
-
this.leaves = leaves.map(leaf => {
|
|
28
|
-
if (this.options.hashLeaves) {
|
|
29
|
-
return this.hash(leaf);
|
|
30
|
-
}
|
|
31
|
-
return Buffer.isBuffer(leaf) ? leaf : Buffer.from(leaf);
|
|
32
|
-
});
|
|
33
|
-
if (this.options.sortLeaves) {
|
|
34
|
-
this.leaves.sort(Buffer.compare);
|
|
35
|
-
}
|
|
36
|
-
this.layers = [this.leaves];
|
|
37
|
-
this.createHashes(this.leaves);
|
|
38
|
-
}
|
|
39
|
-
/**
|
|
40
|
-
* Computes SHA-256 hash of input data
|
|
41
|
-
* @param data - Data to hash
|
|
42
|
-
* @returns Buffer containing hash
|
|
43
|
-
*/
|
|
44
|
-
hash(data) {
|
|
45
|
-
return (0, crypto_1.createHash)('sha256').update(data).digest();
|
|
46
|
-
}
|
|
47
|
-
/**
|
|
48
|
-
* Combines two child hashes to create parent hash
|
|
49
|
-
* @param left - Left child hash
|
|
50
|
-
* @param right - Right child hash
|
|
51
|
-
* @returns Combined hash of children
|
|
52
|
-
*/
|
|
53
|
-
combinedHash(left, right) {
|
|
54
|
-
if (this.options.sortPairs && Buffer.compare(left, right) > 0) {
|
|
55
|
-
[left, right] = [right, left];
|
|
56
|
-
}
|
|
57
|
-
return this.hash(Buffer.concat([left, right]));
|
|
58
|
-
}
|
|
59
|
-
/**
|
|
60
|
-
* Builds the Merkle tree by creating successive layers of hashes
|
|
61
|
-
* @param nodes - Array of nodes in current layer
|
|
62
|
-
*/
|
|
63
|
-
createHashes(nodes) {
|
|
64
|
-
while (nodes.length > 1) {
|
|
65
|
-
const layerIndex = this.layers.length;
|
|
66
|
-
this.layers.push([]);
|
|
67
|
-
// Process pairs of nodes
|
|
68
|
-
for (let i = 0; i < nodes.length - 1; i += 2) {
|
|
69
|
-
const left = nodes[i];
|
|
70
|
-
const right = nodes[i + 1];
|
|
71
|
-
const hash = this.combinedHash(left, right);
|
|
72
|
-
this.layers[layerIndex].push(hash);
|
|
73
|
-
}
|
|
74
|
-
// Handle odd number of nodes
|
|
75
|
-
if (nodes.length % 2 === 1) {
|
|
76
|
-
const last = nodes[nodes.length - 1];
|
|
77
|
-
if (this.options.duplicateOdd) {
|
|
78
|
-
// Duplicate the last node if odd
|
|
79
|
-
const hash = this.combinedHash(last, last);
|
|
80
|
-
this.layers[layerIndex].push(hash);
|
|
81
|
-
}
|
|
82
|
-
else {
|
|
83
|
-
// Push last node up to next layer
|
|
84
|
-
this.layers[layerIndex].push(last);
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
nodes = this.layers[layerIndex];
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
/**
|
|
91
|
-
* Gets the root hash of the Merkle tree
|
|
92
|
-
* @returns Buffer containing root hash
|
|
93
|
-
*/
|
|
94
|
-
getRoot() {
|
|
95
|
-
return this.layers[this.layers.length - 1][0];
|
|
96
|
-
}
|
|
97
|
-
/**
|
|
98
|
-
* Generates a proof of inclusion for a leaf node
|
|
99
|
-
* @param leaf - The leaf node to generate proof for
|
|
100
|
-
* @returns Array of sibling hashes needed to reconstruct root
|
|
101
|
-
* @throws Error if leaf not found in tree
|
|
102
|
-
*/
|
|
103
|
-
getProof(leaf) {
|
|
104
|
-
let index = this.leaves.findIndex(item => item.equals(leaf));
|
|
105
|
-
if (index === -1) {
|
|
106
|
-
throw new Error('Leaf not found in tree');
|
|
107
|
-
}
|
|
108
|
-
return this.layers.reduce((proof, layer) => {
|
|
109
|
-
if (layer.length === 1)
|
|
110
|
-
return proof;
|
|
111
|
-
const pairIndex = index % 2 === 0 ? index + 1 : index - 1;
|
|
112
|
-
if (pairIndex < layer.length) {
|
|
113
|
-
proof.push(layer[pairIndex]);
|
|
114
|
-
}
|
|
115
|
-
index = Math.floor(index / 2);
|
|
116
|
-
return proof;
|
|
117
|
-
}, []);
|
|
118
|
-
}
|
|
119
|
-
/**
|
|
120
|
-
* Verifies a proof of inclusion for a leaf node
|
|
121
|
-
* @param proof - Array of sibling hashes from getProof()
|
|
122
|
-
* @param targetHash - Hash of the leaf node being verified
|
|
123
|
-
* @param root - Expected root hash
|
|
124
|
-
* @returns boolean indicating if proof is valid
|
|
125
|
-
*/
|
|
126
|
-
verify(proof, targetHash, root) {
|
|
127
|
-
let computedHash = targetHash;
|
|
128
|
-
for (const proofElement of proof) {
|
|
129
|
-
computedHash = Buffer.compare(computedHash, proofElement) <= 0
|
|
130
|
-
? this.combinedHash(computedHash, proofElement)
|
|
131
|
-
: this.combinedHash(proofElement, computedHash);
|
|
132
|
-
}
|
|
133
|
-
return computedHash.equals(root);
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
exports.MerkleTree = MerkleTree;
|