diff --git a/deno.jsonc b/deno.jsonc index 4b01f7eb..13bc72c6 100644 --- a/deno.jsonc +++ b/deno.jsonc @@ -5,7 +5,6 @@ "imports": { "98.css": "npm:98.css@^0.1.21", "@atcute/cbor": "npm:@atcute/cbor@^2.3.0", - "@atcute/cid": "https://esm.sh/@atcute/cid@2.4.0", "@atcute/lexicons": "npm:@atcute/lexicons@^1.2.7", "@automerge/automerge": "npm:@automerge/automerge@^3.2.3", "@bradenmacdonald/s3-lite-client": "jsr:@bradenmacdonald/s3-lite-client@^0.9.4", @@ -28,6 +27,7 @@ "idb-keyval": "npm:idb-keyval@^6.2.2", "iso-base": "npm:iso-base@^4.3.0", "lit-html": "npm:lit-html@^3.3.1", + "multiformats": "npm:multiformats@^13.4.2", "query-string": "npm:query-string@^9.3.1", "subsonic-api": "npm:subsonic-api@^3.2.0", "throttle-debounce": "npm:throttle-debounce@^5.0.2", diff --git a/src/common/cid.js b/src/common/cid.js new file mode 100644 index 00000000..355242fd --- /dev/null +++ b/src/common/cid.js @@ -0,0 +1,14 @@ +import { CID } from "multiformats/cid"; +import { sha256 } from "multiformats/hashes/sha2"; + +/** + * @param {number} code + * @param {Uint8Array} data + */ +export async function create(code, data) { + const hash = await sha256.digest(data); + const cid = CID.create(1, code, hash); + console.log(cid.toString()); + + return cid.toString(); +} diff --git a/src/common/facets/utils.js b/src/common/facets/utils.js index a4cd5465..253af2f5 100644 --- a/src/common/facets/utils.js +++ b/src/common/facets/utils.js @@ -1,7 +1,7 @@ // import { fragments, serializeFragments } from "@fcrozatier/htmlcrunch"; -import * as CID from "@atcute/cid"; import { Temporal } from "@js-temporal/polyfill"; +import * as CID from "../cid.js"; /** * @import {Facet} from "@definitions/types.d.ts" @@ -25,7 +25,7 @@ export async function facetFromUrl({ name, url }, { fetchHTML }) { $type: "sh.diffuse.output.facet", createdAt: timestamp, id: crypto.randomUUID(), - cid: cid ? CID.toString(cid) : undefined, + cid, html, name, updatedAt: timestamp, diff --git a/src/facets/index.js b/src/facets/index.js index 188bbc69..dabb96ac 100644 --- a/src/facets/index.js +++ b/src/facets/index.js @@ -1,4 +1,3 @@ -import * as CID from "@atcute/cid"; import { Temporal } from "@js-temporal/polyfill"; import { html, render } from "lit-html"; @@ -8,6 +7,7 @@ import { html as langHtml } from "@codemirror/lang-html"; import { javascript as langJs } from "@codemirror/lang-javascript"; import { autocompletion } from "@codemirror/autocomplete"; +import * as CID from "@common/cid"; import foundation from "@common/facets/foundation.js"; import { effect, signal } from "@common/signal.js"; import { facetFromUrl } from "@common/facets/utils.js"; @@ -220,14 +220,14 @@ async function onBuildSubmit(event) { const facet = $editingFacet.value ? { ...$editingFacet.value, - cid: CID.toString(cid), + cid, html, name, } : { $type: "sh.diffuse.output.facet", id: crypto.randomUUID(), - cid: CID.toString(cid), + cid, html, name, }; @@ -260,7 +260,7 @@ async function editFacet(ogFacet) { const cid = await CID.create(0x55, new TextEncoder().encode(html)); facet.html = html; - facet.cid = CID.toString(cid); + facet.cid = cid; } $editingFacet.value = facet; diff --git a/src/facets/l/index.js b/src/facets/l/index.js index 076e79ff..fbb7f2e1 100644 --- a/src/facets/l/index.js +++ b/src/facets/l/index.js @@ -1,5 +1,4 @@ -import * as CID from "@atcute/cid"; - +import * as CID from "@common/cid.js"; import foundation from "@common/facets/foundation.js"; import { effect } from "@common/signal.js"; @@ -66,7 +65,7 @@ effect(async () => { const cid = await CID.create(0x55, new TextEncoder().encode(html)); facet.html = html; - facet.cid = CID.toString(cid); + facet.cid = cid } loadIntoContainer(facet);