refactor: switch to new CID class from libs5

This commit is contained in:
Derrick Hammer 2023-09-07 18:55:50 -04:00
parent bcf6511068
commit 7b7d770a22
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
5 changed files with 9 additions and 98 deletions

8
npm-shrinkwrap.json generated
View File

@ -8,7 +8,7 @@
"name": "@lumeweb/libportal",
"version": "0.2.0-develop.25",
"dependencies": {
"@lumeweb/libs5": "^0.1.0-develop.48",
"@lumeweb/libs5": "^0.1.0-develop.50",
"@noble/curves": "^1.1.0",
"@noble/hashes": "^1.3.1",
"detect-node": "^2.1.0",
@ -1928,9 +1928,9 @@
"peer": true
},
"node_modules/@lumeweb/libs5": {
"version": "0.1.0-develop.48",
"resolved": "https://registry.npmjs.org/@lumeweb/libs5/-/libs5-0.1.0-develop.48.tgz",
"integrity": "sha512-XO7btWOWMTrYD47uPtdVNLpni0PGOn4XDEsFO94WNO77WJ5E9wSDEFvpPjYjEi/51A2k4g3ku54TVPFCPDCHew==",
"version": "0.1.0-develop.50",
"resolved": "https://registry.npmjs.org/@lumeweb/libs5/-/libs5-0.1.0-develop.50.tgz",
"integrity": "sha512-mPywIV3n7YS5GdJHM0Ge9ODYeaHcdY6WJJMxlIr9OT2SudjCJJ426BynxGWSvJZR0O8u/SPyxNR6g+Uh1iC/1g==",
"dependencies": {
"@noble/curves": "^1.1.0",
"@noble/hashes": "^1.3.1",

View File

@ -34,7 +34,7 @@
"semantic-release": "semantic-release"
},
"dependencies": {
"@lumeweb/libs5": "^0.1.0-develop.48",
"@lumeweb/libs5": "^0.1.0-develop.50",
"@noble/curves": "^1.1.0",
"@noble/hashes": "^1.3.1",
"detect-node": "^2.1.0",

View File

@ -1,88 +0,0 @@
import { base58btc } from "multiformats/bases/base58";
import * as edUtils from "@noble/curves/abstract/utils";
import { CID_HASH_TYPES, CID_TYPES } from "@lumeweb/libs5";
export interface CID {
hash: Uint8Array;
size: bigint;
type: number;
hashType: number;
}
export function encodeCid(
hash: Uint8Array,
size: bigint,
type?: number,
hashType?: number,
raw?: boolean,
);
export function encodeCid(
hash: string,
size: bigint,
type?: number,
hashType?: number,
raw?: boolean,
);
export function encodeCid(
hash: any,
size: bigint,
type = CID_TYPES.RAW,
hashType = CID_HASH_TYPES.BLAKE3,
raw: boolean = false,
) {
if (typeof hash === "string") {
hash = edUtils.hexToBytes(hash);
}
if (!(hash instanceof Uint8Array)) {
throw new Error();
}
if (size === undefined || size === null) {
throw new Error("size required");
}
size = BigInt(size);
const sizeBytes = new Uint8Array(8);
const sizeView = new DataView(sizeBytes.buffer);
sizeView.setBigInt64(0, size, true);
const prefixedHash = Uint8Array.from([type, hashType, ...hash, ...sizeBytes]);
if (raw) {
return prefixedHash;
}
return base58btc.encode(prefixedHash).toString();
}
export function decodeCid(cid: string | Uint8Array): CID {
let bytes = cid;
if (typeof bytes === "string") {
bytes = base58btc.decode(bytes);
}
if (!Object.values(CID_TYPES).includes(bytes[0])) {
throw new Error("Invalid cid type");
}
if (!Object.values(CID_HASH_TYPES).includes(bytes[1])) {
throw new Error("Invalid cid hash type");
}
const type = bytes[0];
const hashType = bytes[1];
bytes = bytes.slice(2);
let cidHash = bytes.slice(0, 32);
let size = bytes.slice(32);
const sizeView = new DataView(size.buffer);
return {
hash: cidHash,
size: sizeView.getBigInt64(0, true),
type,
hashType,
};
}

View File

@ -19,7 +19,6 @@ import streamToBlob from "stream-to-blob";
import defer from "p-defer";
import { blake3 } from "@noble/hashes/blake3";
import { encodeCid } from "./cid.js";
import {
AuthStatusResponse,
LoginResponse,
@ -27,6 +26,7 @@ import {
} from "./responses/auth.js";
import isNode from "detect-node";
import { utf8ToBytes } from "@noble/curves/abstract/utils";
import { CID, CID_TYPES } from "@lumeweb/libs5";
type NodeReadableStreamType = typeof import("stream").Readable;
type NodePassThroughStreamType = typeof import("stream").PassThrough;
@ -469,10 +469,10 @@ export class Client {
await ret.promise;
const cid = encodeCid(hash, size as bigint);
const cid = CID.fromHash(hash, Number(size));
while (true) {
const status = await this.getUploadStatus(cid as string);
const status = await this.getUploadStatus(cid.toString());
if (status.status === "uploaded") {
break;
@ -483,7 +483,7 @@ export class Client {
});
}
return cid as string;
return cid.toString();
}
async getUploadStatus(cid: string) {

View File

@ -1,3 +1,2 @@
export * from "./client.js";
export * from "./cid.js";
export * from "./verify.js";