Compare commits
4 Commits
v0.2.0-dev
...
v0.2.0-dev
Author | SHA1 | Date |
---|---|---|
semantic-release-bot | d7a670c5ea | |
Derrick Hammer | 043f1657ab | |
Derrick Hammer | 7ce52cbff7 | |
Derrick Hammer | 7dc8067e95 |
12
CHANGELOG.md
12
CHANGELOG.md
|
@ -1,3 +1,15 @@
|
||||||
|
# [0.2.0-develop.42](https://git.lumeweb.com/LumeWeb/libweb/compare/v0.2.0-develop.41...v0.2.0-develop.42) (2023-09-04)
|
||||||
|
|
||||||
|
|
||||||
|
### Bug Fixes
|
||||||
|
|
||||||
|
* update encodeCid overloads and return types ([7dc8067](https://git.lumeweb.com/LumeWeb/libweb/commit/7dc8067e958bb62efe856cdb234ac0010b6d2896))
|
||||||
|
|
||||||
|
|
||||||
|
### Features
|
||||||
|
|
||||||
|
* add encodeRegistryCid, encodeRegistryValue, decodeRegistryValue, decodeRegistryCid ([7ce52cb](https://git.lumeweb.com/LumeWeb/libweb/commit/7ce52cbff7e622a9046e6a36d4e664d211043e1e))
|
||||||
|
|
||||||
# [0.2.0-develop.41](https://git.lumeweb.com/LumeWeb/libweb/compare/v0.2.0-develop.40...v0.2.0-develop.41) (2023-09-03)
|
# [0.2.0-develop.41](https://git.lumeweb.com/LumeWeb/libweb/compare/v0.2.0-develop.40...v0.2.0-develop.41) (2023-09-03)
|
||||||
|
|
||||||
# [0.2.0-develop.40](https://git.lumeweb.com/LumeWeb/libweb/compare/v0.2.0-develop.39...v0.2.0-develop.40) (2023-09-03)
|
# [0.2.0-develop.40](https://git.lumeweb.com/LumeWeb/libweb/compare/v0.2.0-develop.39...v0.2.0-develop.40) (2023-09-03)
|
||||||
|
|
|
@ -1,12 +1,12 @@
|
||||||
{
|
{
|
||||||
"name": "@lumeweb/libweb",
|
"name": "@lumeweb/libweb",
|
||||||
"version": "0.2.0-develop.41",
|
"version": "0.2.0-develop.42",
|
||||||
"lockfileVersion": 3,
|
"lockfileVersion": 3,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"packages": {
|
"packages": {
|
||||||
"": {
|
"": {
|
||||||
"name": "@lumeweb/libweb",
|
"name": "@lumeweb/libweb",
|
||||||
"version": "0.2.0-develop.41",
|
"version": "0.2.0-develop.42",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@lumeweb/community-portals": "^0.1.0-develop.6",
|
"@lumeweb/community-portals": "^0.1.0-develop.6",
|
||||||
"@lumeweb/libportal": "0.2.0-develop.23",
|
"@lumeweb/libportal": "0.2.0-develop.23",
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"name": "@lumeweb/libweb",
|
"name": "@lumeweb/libweb",
|
||||||
"version": "0.2.0-develop.41",
|
"version": "0.2.0-develop.42",
|
||||||
"main": "lib/index.js",
|
"main": "lib/index.js",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"repository": {
|
"repository": {
|
||||||
|
|
108
src/cid.ts
108
src/cid.ts
|
@ -1,27 +1,42 @@
|
||||||
import { ErrTuple } from "#types.js";
|
import { ErrTuple } from "#types.js";
|
||||||
|
import type { CID } from "@lumeweb/libportal";
|
||||||
import {
|
import {
|
||||||
decodeCid as decodeCidPortal,
|
decodeCid as decodeCidPortal,
|
||||||
encodeCid as encodeCidPortal,
|
encodeCid as encodeCidPortal,
|
||||||
} from "@lumeweb/libportal";
|
} from "@lumeweb/libportal";
|
||||||
import { addContextToErr } from "#err.js";
|
import { addContextToErr } from "#err.js";
|
||||||
import type { CID } from "@lumeweb/libportal";
|
import { CID_HASH_TYPES, CID_TYPES, REGISTRY_TYPES } from "@lumeweb/libs5";
|
||||||
|
import { concatBytes } from "@noble/hashes/utils";
|
||||||
|
|
||||||
export function encodeCid(hash: Uint8Array, size: bigint): ErrTuple<string>;
|
export function encodeCid(
|
||||||
export function encodeCid(hash: string, size: bigint): ErrTuple<string>;
|
hash: Uint8Array,
|
||||||
|
size: bigint,
|
||||||
|
type?: number,
|
||||||
|
hashType?: number,
|
||||||
|
raw?: boolean,
|
||||||
|
): ErrTuple<string | Uint8Array>;
|
||||||
|
export function encodeCid(
|
||||||
|
hash: string,
|
||||||
|
size: bigint,
|
||||||
|
type?: number,
|
||||||
|
hashType?: number,
|
||||||
|
raw?: boolean,
|
||||||
|
): ErrTuple<string | Uint8Array>;
|
||||||
export function encodeCid(
|
export function encodeCid(
|
||||||
hash: any,
|
hash: any,
|
||||||
size: bigint,
|
size: bigint,
|
||||||
type?: number,
|
type?: number,
|
||||||
hashType?: number,
|
hashType?: number,
|
||||||
): ErrTuple<CID> {
|
raw: boolean = false,
|
||||||
|
): ErrTuple<string | Uint8Array> {
|
||||||
try {
|
try {
|
||||||
return [encodeCidPortal(hash, size, type, hashType), null];
|
return [encodeCidPortal(hash, size, type, hashType, raw), null];
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
return [null, addContextToErr(e as Error, "failed to encode cid")];
|
return ["", addContextToErr(e as Error, "failed to encode cid")];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function decodeCid(cid: string): ErrTuple<CID> {
|
export function decodeCid(cid: string | Uint8Array): ErrTuple<CID> {
|
||||||
try {
|
try {
|
||||||
return [decodeCidPortal(cid), null];
|
return [decodeCidPortal(cid), null];
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
@ -38,4 +53,83 @@ export function verifyCid(cid: string): boolean {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function encodeRegistryCid(
|
||||||
|
hash: Uint8Array,
|
||||||
|
size?: bigint,
|
||||||
|
type?: number,
|
||||||
|
hashType?: number,
|
||||||
|
raw?: boolean,
|
||||||
|
): ErrTuple<string | Uint8Array>;
|
||||||
|
export function encodeRegistryCid(
|
||||||
|
hash: string,
|
||||||
|
size?: bigint,
|
||||||
|
type?: number,
|
||||||
|
hashType?: number,
|
||||||
|
raw?: boolean,
|
||||||
|
): ErrTuple<string | Uint8Array>;
|
||||||
|
export function encodeRegistryCid(
|
||||||
|
hash: any,
|
||||||
|
size = BigInt(0),
|
||||||
|
type = CID_TYPES.RESOLVER,
|
||||||
|
hashType = CID_HASH_TYPES.ED25519,
|
||||||
|
raw: boolean = false,
|
||||||
|
): ErrTuple<string | Uint8Array> {
|
||||||
|
return encodeCid(hash, size, type, hashType, raw);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function encodeRegistryValue(
|
||||||
|
cid: CID | string,
|
||||||
|
type: number = REGISTRY_TYPES.CID,
|
||||||
|
hashType = CID_HASH_TYPES.BLAKE3,
|
||||||
|
): ErrTuple<Uint8Array> {
|
||||||
|
if (typeof cid === "string") {
|
||||||
|
let err;
|
||||||
|
[cid, err] = decodeCid(cid);
|
||||||
|
|
||||||
|
if (err) {
|
||||||
|
return [new Uint8Array(), err];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const [ret, err] = encodeCid(
|
||||||
|
cid.hash,
|
||||||
|
cid.size,
|
||||||
|
CID_TYPES.RESOLVER,
|
||||||
|
CID_HASH_TYPES.BLAKE3,
|
||||||
|
true,
|
||||||
|
);
|
||||||
|
|
||||||
|
if (err) {
|
||||||
|
return [new Uint8Array(), err];
|
||||||
|
}
|
||||||
|
|
||||||
|
return [
|
||||||
|
concatBytes(Uint8Array.from([REGISTRY_TYPES.CID]), ret as Uint8Array),
|
||||||
|
null,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
export function decodeRegistryValue(hash: Uint8Array): ErrTuple<CID> {
|
||||||
|
if (!Object.values(REGISTRY_TYPES).includes(hash[0])) {
|
||||||
|
return [null, "invalid registry type"];
|
||||||
|
}
|
||||||
|
|
||||||
|
hash = hash.slice(1);
|
||||||
|
|
||||||
|
return decodeCid(hash);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function decodeRegistryCid(cid: string | Uint8Array): ErrTuple<CID> {
|
||||||
|
const [ret, err] = decodeCid(cid);
|
||||||
|
if (err) {
|
||||||
|
return [null, err];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ret.type !== CID_TYPES.RESOLVER) {
|
||||||
|
return [null, "not a valid registry cid"];
|
||||||
|
}
|
||||||
|
|
||||||
|
return [cid, null];
|
||||||
|
}
|
||||||
|
|
||||||
export type { CID } from "@lumeweb/libportal";
|
export type { CID } from "@lumeweb/libportal";
|
||||||
|
|
Loading…
Reference in New Issue