Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 18 additions & 18 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@super-protocol/ctl",
"version": "0.12.1",
"version": "0.12.3",
"description": "A tool for publishing values in a secure and reliable way.",
"main": "./build/index.js",
"type": "commonjs",
Expand Down Expand Up @@ -37,7 +37,7 @@
"@super-protocol/distributed-secrets": "1.1.7",
"@super-protocol/dto-js": "1.2.10",
"@super-protocol/sdk-js": "3.13.3",
"@super-protocol/sp-files-addon": "^0.11.0",
"@super-protocol/sp-files-addon": "^0.12.1",
"@types/tar-stream": "^3.1.3",
"axios": "1.6.2",
"bip39": "^3.1.0",
Expand Down
58 changes: 58 additions & 0 deletions src/commands/filesCalculateHash.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import path from 'path';
import { calculateResourceHash } from '@super-protocol/sp-files-addon';
import { cryptoUtils } from '@super-protocol/sdk-js';
import { promises as fs } from 'fs';
import Printer from '../printer';

export interface FilesCalculateHashParams {
localPath: string;
}

export default async (params: FilesCalculateHashParams): Promise<void> => {
const inputPath = typeof params.localPath === 'string' ? params.localPath.trim() : '';
if (!inputPath) {
Printer.print('Filename should be defined');
return;
}

const localPath = path.resolve(inputPath);

try {
const stat = await fs.stat(localPath);
if (stat.isDirectory()) {
const files = await fs.readdir(localPath);

Printer.print(`Found folder "${localPath}" with ${files.length} top-level entries`);
} else if (stat.isFile()) {
Printer.print(`Found file "${localPath}"`);
} else {
Printer.print(`Found path "${localPath}" (not a regular file or directory)`);
}
} catch (error) {
if ((error as NodeJS.ErrnoException).code === 'ENOENT') {
Printer.error(`\nFile or folder is missing on path ${localPath}`);
return;
}

throw error;
}

try {
const objectName = path.basename(localPath);
Printer.print('Calculating hash...');

const objectHash = await calculateResourceHash(localPath);
const rootHash = await cryptoUtils.getDirHashFileContents({ [objectName]: objectHash });

const raw = rootHash.hash ?? '';
const colonIndex = raw.indexOf(':');

const hash = {
algo: colonIndex > 0 ? raw.slice(0, colonIndex).toLowerCase() : '',
hash: colonIndex > 0 ? raw.slice(colonIndex + 1) : raw,
};
Printer.print(JSON.stringify(hash, null, 2));
} catch (error) {
throw new Error(`Failed to calculate hash: ${(error as Error).message}`);
}
};
9 changes: 9 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import addonDownload from './commands/filesDownload.addon';
import upload from './commands/filesUpload';
import addonUpload, { FilesUploadParams } from './commands/filesUpload.addon';
import filesDelete from './commands/filesDelete';
import filesCalculateHash from './commands/filesCalculateHash';
import addonFilesDelete from './commands/filesDelete.addon';
import providersList from './commands/providersList';
import providersGet from './commands/providersGet';
Expand Down Expand Up @@ -1594,6 +1595,14 @@ async function main(): Promise<void> {
}
});

filesCommand
.command('calculate-hash')
.description('Calculate the hash of a file or all files in a folder')
.argument('localPath', 'Path to the file or folder')
.action(async (localPath: string) => {
await filesCalculateHash({ localPath });
});

solutionsCommand
.command('generate-key')
.description('Generate a solution signing key')
Expand Down
Loading