feat: add new downloadSmallObject function that only hashes the data and compares it and does not verify it in real time

This commit is contained in:
Derrick Hammer 2023-08-10 02:47:57 -04:00
parent 9d153cde40
commit 747aeb7d2e
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
3 changed files with 52 additions and 2 deletions

8
npm-shrinkwrap.json generated
View File

@ -12,7 +12,8 @@
"@lumeweb/libportal": "0.2.0-develop.17", "@lumeweb/libportal": "0.2.0-develop.17",
"@lumeweb/node-library-preset": "0.2.7", "@lumeweb/node-library-preset": "0.2.7",
"@noble/curves": "^1.1.0", "@noble/curves": "^1.1.0",
"@noble/hashes": "^1.3.1" "@noble/hashes": "^1.3.1",
"binconv": "^0.2.0"
}, },
"devDependencies": { "devDependencies": {
"@semantic-release/changelog": "^6.0.3", "@semantic-release/changelog": "^6.0.3",
@ -3814,6 +3815,11 @@
"node": ">=8" "node": ">=8"
} }
}, },
"node_modules/binconv": {
"version": "0.2.0",
"resolved": "https://registry.npmjs.org/binconv/-/binconv-0.2.0.tgz",
"integrity": "sha512-FAVbv8Fe1lsk1XQeWaYzfIQ9EnBgLJxjlqVA5XzbusP1YCzFgXUfIfmuanNmfM7i0XsNCNnYOnnq7NRLVKRgdQ=="
},
"node_modules/bottleneck": { "node_modules/bottleneck": {
"version": "2.19.5", "version": "2.19.5",
"resolved": "https://registry.npmjs.org/bottleneck/-/bottleneck-2.19.5.tgz", "resolved": "https://registry.npmjs.org/bottleneck/-/bottleneck-2.19.5.tgz",

View File

@ -25,7 +25,8 @@
"@lumeweb/libportal": "0.2.0-develop.17", "@lumeweb/libportal": "0.2.0-develop.17",
"@lumeweb/node-library-preset": "0.2.7", "@lumeweb/node-library-preset": "0.2.7",
"@noble/curves": "^1.1.0", "@noble/curves": "^1.1.0",
"@noble/hashes": "^1.3.1" "@noble/hashes": "^1.3.1",
"binconv": "^0.2.0"
}, },
"publishConfig": { "publishConfig": {
"access": "public" "access": "public"

View File

@ -1,6 +1,12 @@
import { getActivePortals } from "#portal.js"; import { getActivePortals } from "#portal.js";
import { ErrTuple } from "#types.js"; import { ErrTuple } from "#types.js";
import { decodeCid, getVerifiableStream } from "@lumeweb/libportal"; import { decodeCid, getVerifiableStream } from "@lumeweb/libportal";
import {
readableStreamToUint8Array,
uint8ArrayToReadableStream,
} from "binconv";
import { equalBytes } from "@noble/curves/abstract/utils";
import { blake3 } from "@noble/hashes/blake3";
const NO_PORTALS_ERROR = [null, "no active portals"] as ErrTuple; const NO_PORTALS_ERROR = [null, "no active portals"] as ErrTuple;
@ -37,3 +43,40 @@ export async function downloadObject(cid: string): Promise<ErrTuple> {
return NO_PORTALS_ERROR; return NO_PORTALS_ERROR;
} }
export async function downloadSmallObject(cid: string): Promise<ErrTuple> {
const activePortals = getActivePortals();
if (!activePortals.length) {
return NO_PORTALS_ERROR;
}
for (const portal of activePortals) {
if (!(await portal.isLoggedIn())) {
try {
await portal.register();
} catch {}
await portal.login();
}
let stream;
try {
stream = await portal.downloadFile(cid);
} catch {
continue;
}
const CID = decodeCid(cid);
const data = await readableStreamToUint8Array(stream);
if (!equalBytes(blake3(data), CID.hash)) {
return [null, "cid verification failed"];
}
return [uint8ArrayToReadableStream(data), null];
}
return NO_PORTALS_ERROR;
}