diff --git a/npm-shrinkwrap.json b/npm-shrinkwrap.json index f26b244..e12b5b6 100644 --- a/npm-shrinkwrap.json +++ b/npm-shrinkwrap.json @@ -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", diff --git a/package.json b/package.json index d4db105..4bcf0e7 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/cid.ts b/src/cid.ts deleted file mode 100644 index 082854f..0000000 --- a/src/cid.ts +++ /dev/null @@ -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, - }; -} diff --git a/src/client.ts b/src/client.ts index b3dd2b3..c3bab9f 100644 --- a/src/client.ts +++ b/src/client.ts @@ -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) { diff --git a/src/index.ts b/src/index.ts index 77070e8..16b3d55 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,3 +1,2 @@ export * from "./client.js"; -export * from "./cid.js"; export * from "./verify.js";