UNPKG

@storacha/client

Version:

Client for the storacha.network w3up api

23 lines 816 B
import { CarWriter } from '@ipld/car'; import { CID } from 'multiformats/cid'; import * as raw from 'multiformats/codecs/raw'; import { sha256 } from 'multiformats/hashes/sha2'; import * as CAR from '@ucanto/transport/car'; /** * @param {Uint8Array} bytes */ export async function toCAR(bytes) { const hash = await sha256.digest(bytes); const root = CID.create(1, raw.code, hash); const { writer, out } = CarWriter.create(root); void writer.put({ cid: root, bytes }); void writer.close(); const chunks = []; for await (const chunk of out) { chunks.push(chunk); } const blob = new Blob(chunks); const cid = await CAR.codec.link(new Uint8Array(await blob.arrayBuffer())); return Object.assign(blob, { cid, roots: [root], bytes }); } //# sourceMappingURL=car.js.map