refactor: update cid encoding/decoding to use libs5 CID_HASH_TYPES and CID_TYPES, and use a default type of raw and default hash type of blake3

This commit is contained in:
Derrick Hammer 2023-09-02 06:25:49 -04:00
parent 0625b1acbb
commit 9bece08a19
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
3 changed files with 221 additions and 32 deletions

203
npm-shrinkwrap.json generated
View File

@ -8,6 +8,7 @@
"name": "@lumeweb/libportal", "name": "@lumeweb/libportal",
"version": "0.2.0-develop.16", "version": "0.2.0-develop.16",
"dependencies": { "dependencies": {
"@lumeweb/libs5": "^0.1.0-develop.43",
"@lumeweb/node-library-preset": "git+https://git.lumeweb.com/LumeWeb/node-library-preset.git", "@lumeweb/node-library-preset": "git+https://git.lumeweb.com/LumeWeb/node-library-preset.git",
"@noble/curves": "^1.1.0", "@noble/curves": "^1.1.0",
"@noble/hashes": "^1.3.1", "@noble/hashes": "^1.3.1",
@ -1926,6 +1927,29 @@
"dev": true, "dev": true,
"peer": true "peer": true
}, },
"node_modules/@lumeweb/libs5": {
"version": "0.1.0-develop.43",
"resolved": "https://registry.npmjs.org/@lumeweb/libs5/-/libs5-0.1.0-develop.43.tgz",
"integrity": "sha512-kUGuT4kK9YVggqyMUj+pPY7okAFKxzACZTlA4BBBsgxAGIdr+iZLQwMfDlwzE4bdxndS52g1XM39Xv3K2xA/lQ==",
"dependencies": {
"@noble/curves": "^1.1.0",
"@noble/hashes": "^1.3.1",
"detect-node": "^2.1.0",
"level": "^8.0.0",
"multiformats": "^12.0.1",
"p-defer": "^4.0.0",
"ws": "^8.13.0"
}
},
"node_modules/@lumeweb/libs5/node_modules/multiformats": {
"version": "12.1.0",
"resolved": "https://registry.npmjs.org/multiformats/-/multiformats-12.1.0.tgz",
"integrity": "sha512-/qTOKKnU8nwcVURjRcS+UN0QYgdS5BPZzY10Aiciu2SqncyCVMGV8KtD83EBFmsuJDsSEmT4sGvzcTkCoMw0sQ==",
"engines": {
"node": ">=16.0.0",
"npm": ">=7.0.0"
}
},
"node_modules/@lumeweb/node-library-preset": { "node_modules/@lumeweb/node-library-preset": {
"version": "0.1.0", "version": "0.1.0",
"resolved": "git+https://git.lumeweb.com/LumeWeb/node-library-preset.git#841e005d7cb3c939bd040823206d48e93ac9119a", "resolved": "git+https://git.lumeweb.com/LumeWeb/node-library-preset.git#841e005d7cb3c939bd040823206d48e93ac9119a",
@ -3692,6 +3716,23 @@
"node": ">=6.5" "node": ">=6.5"
} }
}, },
"node_modules/abstract-level": {
"version": "1.0.3",
"resolved": "https://registry.npmjs.org/abstract-level/-/abstract-level-1.0.3.tgz",
"integrity": "sha512-t6jv+xHy+VYwc4xqZMn2Pa9DjcdzvzZmQGRjTFc8spIbRGHgBrEKbPq+rYXc7CCo0lxgYvSgKVg9qZAhpVQSjA==",
"dependencies": {
"buffer": "^6.0.3",
"catering": "^2.1.0",
"is-buffer": "^2.0.5",
"level-supports": "^4.0.0",
"level-transcoder": "^1.0.1",
"module-error": "^1.0.1",
"queue-microtask": "^1.2.3"
},
"engines": {
"node": ">=12"
}
},
"node_modules/acorn": { "node_modules/acorn": {
"version": "8.9.0", "version": "8.9.0",
"resolved": "https://registry.npmjs.org/acorn/-/acorn-8.9.0.tgz", "resolved": "https://registry.npmjs.org/acorn/-/acorn-8.9.0.tgz",
@ -4146,7 +4187,6 @@
"version": "1.5.1", "version": "1.5.1",
"resolved": "https://registry.npmjs.org/base64-js/-/base64-js-1.5.1.tgz", "resolved": "https://registry.npmjs.org/base64-js/-/base64-js-1.5.1.tgz",
"integrity": "sha512-AKpaYlHn8t4SVbOHCy+b5+KKgvR4vrsD8vbvrbiQJps7fKDTkjkDry6ji0rUJjC0kzbNePLwzxq8iypo41qeWA==", "integrity": "sha512-AKpaYlHn8t4SVbOHCy+b5+KKgvR4vrsD8vbvrbiQJps7fKDTkjkDry6ji0rUJjC0kzbNePLwzxq8iypo41qeWA==",
"dev": true,
"funding": [ "funding": [
{ {
"type": "github", "type": "github",
@ -4246,6 +4286,17 @@
"node": ">=8" "node": ">=8"
} }
}, },
"node_modules/browser-level": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/browser-level/-/browser-level-1.0.1.tgz",
"integrity": "sha512-XECYKJ+Dbzw0lbydyQuJzwNXtOpbMSq737qxJN11sIRTErOMShvDpbzTlgju7orJKvx4epULolZAuJGLzCmWRQ==",
"dependencies": {
"abstract-level": "^1.0.2",
"catering": "^2.1.1",
"module-error": "^1.0.2",
"run-parallel-limit": "^1.1.0"
}
},
"node_modules/browserslist": { "node_modules/browserslist": {
"version": "4.21.9", "version": "4.21.9",
"resolved": "https://registry.npmjs.org/browserslist/-/browserslist-4.21.9.tgz", "resolved": "https://registry.npmjs.org/browserslist/-/browserslist-4.21.9.tgz",
@ -4306,7 +4357,6 @@
"version": "6.0.3", "version": "6.0.3",
"resolved": "https://registry.npmjs.org/buffer/-/buffer-6.0.3.tgz", "resolved": "https://registry.npmjs.org/buffer/-/buffer-6.0.3.tgz",
"integrity": "sha512-FTiCpNxtwiZZHEZbcbTIcZjERVICn9yq/pDFkTl95/AxzD1naBctN7YO68riM/gLSDY7sdrMby8hofADYuuqOA==", "integrity": "sha512-FTiCpNxtwiZZHEZbcbTIcZjERVICn9yq/pDFkTl95/AxzD1naBctN7YO68riM/gLSDY7sdrMby8hofADYuuqOA==",
"dev": true,
"funding": [ "funding": [
{ {
"type": "github", "type": "github",
@ -4609,6 +4659,14 @@
"cdl": "bin/cdl.js" "cdl": "bin/cdl.js"
} }
}, },
"node_modules/catering": {
"version": "2.1.1",
"resolved": "https://registry.npmjs.org/catering/-/catering-2.1.1.tgz",
"integrity": "sha512-K7Qy8O9p76sL3/3m7/zLKbRkyOlSZAgzEaLhyj2mXS8PsCud2Eo4hAb8aLtZqHh0QGqLcb9dlJSu6lHRVENm1w==",
"engines": {
"node": ">=6"
}
},
"node_modules/chalk": { "node_modules/chalk": {
"version": "4.1.2", "version": "4.1.2",
"resolved": "https://registry.npmjs.org/chalk/-/chalk-4.1.2.tgz", "resolved": "https://registry.npmjs.org/chalk/-/chalk-4.1.2.tgz",
@ -4695,6 +4753,22 @@
"dev": true, "dev": true,
"peer": true "peer": true
}, },
"node_modules/classic-level": {
"version": "1.3.0",
"resolved": "https://registry.npmjs.org/classic-level/-/classic-level-1.3.0.tgz",
"integrity": "sha512-iwFAJQYtqRTRM0F6L8h4JCt00ZSGdOyqh7yVrhhjrOpFhmBjNlRUey64MCiyo6UmQHMJ+No3c81nujPv+n9yrg==",
"hasInstallScript": true,
"dependencies": {
"abstract-level": "^1.0.2",
"catering": "^2.1.0",
"module-error": "^1.0.1",
"napi-macros": "^2.2.2",
"node-gyp-build": "^4.3.0"
},
"engines": {
"node": ">=12"
}
},
"node_modules/clean-stack": { "node_modules/clean-stack": {
"version": "2.2.0", "version": "2.2.0",
"resolved": "https://registry.npmjs.org/clean-stack/-/clean-stack-2.2.0.tgz", "resolved": "https://registry.npmjs.org/clean-stack/-/clean-stack-2.2.0.tgz",
@ -8228,7 +8302,6 @@
"version": "1.2.1", "version": "1.2.1",
"resolved": "https://registry.npmjs.org/ieee754/-/ieee754-1.2.1.tgz", "resolved": "https://registry.npmjs.org/ieee754/-/ieee754-1.2.1.tgz",
"integrity": "sha512-dcyqhDvX1C46lXZcVqCpK+FtMRQVdIMN6/Df5js2zouUsqG7I6sFxitIC+7KYK29KdXOLHdu9zL4sFnoVQnqaA==", "integrity": "sha512-dcyqhDvX1C46lXZcVqCpK+FtMRQVdIMN6/Df5js2zouUsqG7I6sFxitIC+7KYK29KdXOLHdu9zL4sFnoVQnqaA==",
"dev": true,
"funding": [ "funding": [
{ {
"type": "github", "type": "github",
@ -8500,6 +8573,28 @@
"url": "https://github.com/sponsors/ljharb" "url": "https://github.com/sponsors/ljharb"
} }
}, },
"node_modules/is-buffer": {
"version": "2.0.5",
"resolved": "https://registry.npmjs.org/is-buffer/-/is-buffer-2.0.5.tgz",
"integrity": "sha512-i2R6zNFDwgEHJyQUtJEk0XFi1i0dPFn/oqjK3/vPCcDeJvW5NQ83V8QbicfF1SupOaB0h8ntgBC2YiE7dfyctQ==",
"funding": [
{
"type": "github",
"url": "https://github.com/sponsors/feross"
},
{
"type": "patreon",
"url": "https://www.patreon.com/feross"
},
{
"type": "consulting",
"url": "https://feross.org/support"
}
],
"engines": {
"node": ">=4"
}
},
"node_modules/is-builtin-module": { "node_modules/is-builtin-module": {
"version": "3.2.1", "version": "3.2.1",
"resolved": "https://registry.npmjs.org/is-builtin-module/-/is-builtin-module-3.2.1.tgz", "resolved": "https://registry.npmjs.org/is-builtin-module/-/is-builtin-module-3.2.1.tgz",
@ -10015,6 +10110,42 @@
"url": "https://github.com/chalk/strip-ansi?sponsor=1" "url": "https://github.com/chalk/strip-ansi?sponsor=1"
} }
}, },
"node_modules/level": {
"version": "8.0.0",
"resolved": "https://registry.npmjs.org/level/-/level-8.0.0.tgz",
"integrity": "sha512-ypf0jjAk2BWI33yzEaaotpq7fkOPALKAgDBxggO6Q9HGX2MRXn0wbP1Jn/tJv1gtL867+YOjOB49WaUF3UoJNQ==",
"dependencies": {
"browser-level": "^1.0.1",
"classic-level": "^1.2.0"
},
"engines": {
"node": ">=12"
},
"funding": {
"type": "opencollective",
"url": "https://opencollective.com/level"
}
},
"node_modules/level-supports": {
"version": "4.0.1",
"resolved": "https://registry.npmjs.org/level-supports/-/level-supports-4.0.1.tgz",
"integrity": "sha512-PbXpve8rKeNcZ9C1mUicC9auIYFyGpkV9/i6g76tLgANwWhtG2v7I4xNBUlkn3lE2/dZF3Pi0ygYGtLc4RXXdA==",
"engines": {
"node": ">=12"
}
},
"node_modules/level-transcoder": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/level-transcoder/-/level-transcoder-1.0.1.tgz",
"integrity": "sha512-t7bFwFtsQeD8cl8NIoQ2iwxA0CL/9IFw7/9gAjOonH0PWTTiRfY7Hq+Ejbsxh86tXobDQ6IOiddjNYIfOBs06w==",
"dependencies": {
"buffer": "^6.0.3",
"module-error": "^1.0.1"
},
"engines": {
"node": ">=12"
}
},
"node_modules/leven": { "node_modules/leven": {
"version": "3.1.0", "version": "3.1.0",
"resolved": "https://registry.npmjs.org/leven/-/leven-3.1.0.tgz", "resolved": "https://registry.npmjs.org/leven/-/leven-3.1.0.tgz",
@ -11180,6 +11311,14 @@
"node": ">=0.10.0" "node": ">=0.10.0"
} }
}, },
"node_modules/module-error": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/module-error/-/module-error-1.0.2.tgz",
"integrity": "sha512-0yuvsqSCv8LbaOKhnsQ/T5JhyFlCYLPXK3U2sgV10zoKQwzs/MyfuQUOZQ1V/6OCOJsK/TRgNVrPuPDqtdMFtA==",
"engines": {
"node": ">=10"
}
},
"node_modules/ms": { "node_modules/ms": {
"version": "2.1.2", "version": "2.1.2",
"resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz", "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz",
@ -11215,6 +11354,11 @@
"url": "https://github.com/sponsors/raouldeheer" "url": "https://github.com/sponsors/raouldeheer"
} }
}, },
"node_modules/napi-macros": {
"version": "2.2.2",
"resolved": "https://registry.npmjs.org/napi-macros/-/napi-macros-2.2.2.tgz",
"integrity": "sha512-hmEVtAGYzVQpCKdbQea4skABsdXW4RUh5t5mJ2zzqowJS2OyXZTU1KhDVFhx+NlWZ4ap9mqR9TcDO3LTTttd+g=="
},
"node_modules/natural-compare": { "node_modules/natural-compare": {
"version": "1.4.0", "version": "1.4.0",
"resolved": "https://registry.npmjs.org/natural-compare/-/natural-compare-1.4.0.tgz", "resolved": "https://registry.npmjs.org/natural-compare/-/natural-compare-1.4.0.tgz",
@ -11319,6 +11463,16 @@
"node": "^12.13 || ^14.13 || >=16" "node": "^12.13 || ^14.13 || >=16"
} }
}, },
"node_modules/node-gyp-build": {
"version": "4.6.1",
"resolved": "https://registry.npmjs.org/node-gyp-build/-/node-gyp-build-4.6.1.tgz",
"integrity": "sha512-24vnklJmyRS8ViBNI8KbtK/r/DmXQMRiOMXTNz2nrTnAYUwjmEEbnnpB/+kt+yWRv73bPsSPRFddrcIbAxSiMQ==",
"bin": {
"node-gyp-build": "bin.js",
"node-gyp-build-optional": "optional.js",
"node-gyp-build-test": "build-test.js"
}
},
"node_modules/node-gyp/node_modules/abbrev": { "node_modules/node-gyp/node_modules/abbrev": {
"version": "1.1.1", "version": "1.1.1",
"resolved": "https://registry.npmjs.org/abbrev/-/abbrev-1.1.1.tgz", "resolved": "https://registry.npmjs.org/abbrev/-/abbrev-1.1.1.tgz",
@ -16098,7 +16252,6 @@
"version": "1.2.3", "version": "1.2.3",
"resolved": "https://registry.npmjs.org/queue-microtask/-/queue-microtask-1.2.3.tgz", "resolved": "https://registry.npmjs.org/queue-microtask/-/queue-microtask-1.2.3.tgz",
"integrity": "sha512-NuaNSa6flKT5JaSYQzJok04JzTL1CA6aGhv5rfLW3PgqA+M2ChpZQnAC8h8i4ZFkBS8X5RqkDBHA7r4hej3K9A==", "integrity": "sha512-NuaNSa6flKT5JaSYQzJok04JzTL1CA6aGhv5rfLW3PgqA+M2ChpZQnAC8h8i4ZFkBS8X5RqkDBHA7r4hej3K9A==",
"dev": true,
"funding": [ "funding": [
{ {
"type": "github", "type": "github",
@ -16679,6 +16832,28 @@
"queue-microtask": "^1.2.2" "queue-microtask": "^1.2.2"
} }
}, },
"node_modules/run-parallel-limit": {
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/run-parallel-limit/-/run-parallel-limit-1.1.0.tgz",
"integrity": "sha512-jJA7irRNM91jaKc3Hcl1npHsFLOXOoTkPCUL1JEa1R82O2miplXXRaGdjW/KM/98YQWDhJLiSs793CnXfblJUw==",
"funding": [
{
"type": "github",
"url": "https://github.com/sponsors/feross"
},
{
"type": "patreon",
"url": "https://www.patreon.com/feross"
},
{
"type": "consulting",
"url": "https://feross.org/support"
}
],
"dependencies": {
"queue-microtask": "^1.2.2"
}
},
"node_modules/rxjs": { "node_modules/rxjs": {
"version": "7.8.1", "version": "7.8.1",
"resolved": "https://registry.npmjs.org/rxjs/-/rxjs-7.8.1.tgz", "resolved": "https://registry.npmjs.org/rxjs/-/rxjs-7.8.1.tgz",
@ -19262,6 +19437,26 @@
"url": "https://github.com/sponsors/sindresorhus" "url": "https://github.com/sponsors/sindresorhus"
} }
}, },
"node_modules/ws": {
"version": "8.13.0",
"resolved": "https://registry.npmjs.org/ws/-/ws-8.13.0.tgz",
"integrity": "sha512-x9vcZYTrFPC7aSIbj7sRCYo7L/Xb8Iy+pW0ng0wt2vCJv7M9HOMy0UoN3rr+IFC7hb7vXoqS+P9ktyLLLhO+LA==",
"engines": {
"node": ">=10.0.0"
},
"peerDependencies": {
"bufferutil": "^4.0.1",
"utf-8-validate": ">=5.0.2"
},
"peerDependenciesMeta": {
"bufferutil": {
"optional": true
},
"utf-8-validate": {
"optional": true
}
}
},
"node_modules/xtend": { "node_modules/xtend": {
"version": "4.0.2", "version": "4.0.2",
"resolved": "https://registry.npmjs.org/xtend/-/xtend-4.0.2.tgz", "resolved": "https://registry.npmjs.org/xtend/-/xtend-4.0.2.tgz",

View File

@ -34,6 +34,7 @@
"semantic-release": "semantic-release" "semantic-release": "semantic-release"
}, },
"dependencies": { "dependencies": {
"@lumeweb/libs5": "^0.1.0-develop.43",
"@lumeweb/node-library-preset": "git+https://git.lumeweb.com/LumeWeb/node-library-preset.git", "@lumeweb/node-library-preset": "git+https://git.lumeweb.com/LumeWeb/node-library-preset.git",
"@noble/curves": "^1.1.0", "@noble/curves": "^1.1.0",
"@noble/hashes": "^1.3.1", "@noble/hashes": "^1.3.1",

View File

@ -1,16 +1,22 @@
import { base58btc } from "multiformats/bases/base58"; import { base58btc } from "multiformats/bases/base58";
import * as edUtils from "@noble/curves/abstract/utils"; import * as edUtils from "@noble/curves/abstract/utils";
import { CID_HASH_TYPES, CID_TYPES } from "@lumeweb/libs5";
export const MAGIC_BYTES = new Uint8Array([0x26, 0x1f]);
export interface CID { export interface CID {
hash: Uint8Array; hash: Uint8Array;
size: bigint; size: bigint;
type: number;
hashType: number;
} }
export function encodeCid(hash: Uint8Array, size: bigint); export function encodeCid(hash: Uint8Array, size: bigint);
export function encodeCid(hash: string, size: bigint); export function encodeCid(hash: string, size: bigint);
export function encodeCid(hash: any, size: bigint) { export function encodeCid(
hash: any,
size: bigint,
type = CID_TYPES.RAW,
hashType = CID_HASH_TYPES.BLAKE3,
) {
if (typeof hash === "string") { if (typeof hash === "string") {
hash = edUtils.hexToBytes(hash); hash = edUtils.hexToBytes(hash);
} }
@ -29,17 +35,24 @@ export function encodeCid(hash: any, size: bigint) {
const sizeView = new DataView(sizeBytes.buffer); const sizeView = new DataView(sizeBytes.buffer);
sizeView.setBigInt64(0, size, true); sizeView.setBigInt64(0, size, true);
const prefixedHash = Uint8Array.from([...MAGIC_BYTES, ...hash, ...sizeBytes]); const prefixedHash = Uint8Array.from([type, hashType, ...hash, ...sizeBytes]);
return base58btc.encode(prefixedHash).toString(); return base58btc.encode(prefixedHash).toString();
} }
export function decodeCid(cid: string): CID { export function decodeCid(cid: string): CID {
let bytes = base58btc.decode(cid); let bytes = base58btc.decode(cid);
if (!arrayBufferEqual(bytes.slice(0, 2).buffer, MAGIC_BYTES.buffer)) { if (!Object.values(CID_TYPES).includes(bytes[0])) {
throw new Error("Invalid cid"); 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); bytes = bytes.slice(2);
let cidHash = bytes.slice(0, 32); let cidHash = bytes.slice(0, 32);
let size = bytes.slice(32); let size = bytes.slice(32);
@ -48,27 +61,7 @@ export function decodeCid(cid: string): CID {
return { return {
hash: cidHash, hash: cidHash,
size: sizeView.getBigInt64(0, true), size: sizeView.getBigInt64(0, true),
type,
hashType,
}; };
} }
function arrayBufferEqual(buf1, buf2) {
if (buf1 === buf2) {
return true;
}
if (buf1.byteLength !== buf2.byteLength) {
return false;
}
var view1 = new DataView(buf1);
var view2 = new DataView(buf2);
var i = buf1.byteLength;
while (i--) {
if (view1.getUint8(i) !== view2.getUint8(i)) {
return false;
}
}
return true;
}