A music player that connects to your cloud/distributed storage.
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

at v4 51 lines 1.9 kB view raw
1import * as CID from "@atcute/cid"; 2import { equals, toSha256 } from "@atcute/uint8array"; 3 4/** 5 * @param {0x55 | 0x71} code 6 * @param {Uint8Array<any>} data 7 * 8 * @example Returns a non-empty base32 CID string, consistent for same input, different for different inputs 9 * ```js 10 * import { create } from "~/common/cid.js"; 11 * 12 * const data = new TextEncoder().encode("hello world"); 13 * const cid = await create(0x55, data); 14 * if (typeof cid !== "string" || cid.length === 0) throw new Error("CID should be a non-empty string"); 15 * if (!/^[a-z2-7]+$/.test(cid)) throw new Error("CID should be base32-encoded"); 16 * 17 * const cid2 = await create(0x55, data); 18 * if (cid !== cid2) throw new Error("same input should produce same CID"); 19 * 20 * const cidDiff = await create(0x55, new TextEncoder().encode("world")); 21 * if (cid === cidDiff) throw new Error("different input should produce different CID"); 22 * 23 * const cidCodec = await create(0x71, data); 24 * if (cid === cidCodec) throw new Error("different codec should produce different CID"); 25 * ``` 26 */ 27export async function create(code, data) { 28 const cid = await CID.create(code, data); 29 return CID.toString(cid); 30} 31 32/** 33 * @param {Uint8Array<any>} data 34 * @param {string} expected 35 * 36 * @example Returns true for matching data and false for mismatched data 37 * ```js 38 * import { create, verify } from "~/common/cid.js"; 39 * 40 * const data = new TextEncoder().encode("hello"); 41 * const cid = await create(0x55, data); 42 * if (!await verify(data, cid)) throw new Error("should verify matching data"); 43 * if (await verify(new TextEncoder().encode("world"), cid)) throw new Error("should not verify mismatched data"); 44 * ``` 45 */ 46export async function verify(data, expected) { 47 const expectedCid = CID.fromString(expected); 48 const digest = await toSha256(data); 49 50 return equals(digest, expectedCid.digest.contents); 51}