refactor: switch to using frozen objects to store CID_TYPES, METADATA_TYPES, and PARENT_LINK_TYPES

This commit is contained in:
Derrick Hammer 2023-09-02 05:33:44 -04:00
parent d03fae0356
commit d05d011fcd
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
2 changed files with 40 additions and 49 deletions

View File

@ -1,13 +1,10 @@
export const cidTypeRaw = 0x26; export const CID_TYPES = {
export const cidTypeMetadataMedia = 0xc5; RAW: 0x26,
// const cidTypeMetadataFile = 0xc6; METADATA_MEDIA: 0xc5,
export const cidTypeMetadataWebApp = 0x59; METADATA_WEBAPP: 0x59,
export const cidTypeResolver = 0x25; RESOLVER: 0x25,
USER_IDENTITY: 0x77,
export const cidTypeUserIdentity = 0x77; BRIDGE: 0x3a,
export const cidTypeBridge = 0x3a;
// format for dynamic encrypted CID // format for dynamic encrypted CID
// type algo key resolver_type mkey_ed255 pubkey // type algo key resolver_type mkey_ed255 pubkey
// in entry: encrypt(RAW CID or MEDIA or SOMETHING) // in entry: encrypt(RAW CID or MEDIA or SOMETHING)
@ -15,26 +12,18 @@ export const cidTypeBridge = 0x3a;
/// Used for immutable encrypted files and metadata formats, key can never be re-used /// Used for immutable encrypted files and metadata formats, key can never be re-used
/// ///
/// Used for file versions in Vup /// Used for file versions in Vup
export const cidTypeEncryptedStatic = 0xae; ENCRYPTED_STATIC: 0xae,
ENCRYPTED_DYNAMIC: 0xad,
};
Object.freeze(CID_TYPES);
export const REGISTRY_TYPES = {
CID: 0x5a,
/// Used for encrypted files with update support /// Used for encrypted files with update support
/// ///
/// can point to resolver CID, Stream CID, Directory Metadata or Media Metadata object /// can point to resolver CID, Stream CID, Directory Metadata or Media Metadata object
export const cidTypeEncryptedDynamic = 0xad; ENCRYPTED_CID: 0x5e,
};
export const CID_TYPES = [
cidTypeRaw,
cidTypeMetadataMedia,
cidTypeMetadataWebApp,
cidTypeResolver,
cidTypeUserIdentity,
cidTypeBridge,
cidTypeEncryptedStatic,
cidTypeEncryptedDynamic,
];
export const registryS5CIDByte = 0x5a;
export const registryS5EncryptedByte = 0x5e;
// ! some multicodec bytes // ! some multicodec bytes
// BLAKE3 with default output size of 256 bits // BLAKE3 with default output size of 256 bits
@ -59,23 +48,25 @@ export const metadataTypeDirectory = 0x04;
export const metadataTypeProofs = 0x05; export const metadataTypeProofs = 0x05;
export const metadataTypeUserIdentity = 0x07; export const metadataTypeUserIdentity = 0x07;
export const METADATA_TYPES = [ export const METADATA_TYPES = {
metadataTypeMedia, MEDIA: 0x02,
metadataTypeWebApp, WEBAPP: 0x03,
metadataTypeDirectory, DIRECTORY: 0x04,
metadataTypeProofs, PROOF: 0x05,
metadataTypeUserIdentity, USER_IDENTITY: 0x07,
]; };
Object.freeze(METADATA_TYPES);
export const parentLinkTypeUserIdentity = 1; export const parentLinkTypeUserIdentity = 1;
export const parentLinkTypeBoard = 5; export const parentLinkTypeBoard = 5;
export const parentLinkTypeBridgeUser = 10; export const parentLinkTypeBridgeUser = 10;
export const PARENT_LINK_TYPES = [ export const PARENT_LINK_TYPES = {
parentLinkTypeUserIdentity, USER_IDENTITY: 1,
parentLinkTypeBoard, BOARD: 5,
parentLinkTypeBridgeUser, BRIDGE_USER: 10,
]; };
Object.freeze(PARENT_LINK_TYPES);
export const registryMaxDataSize = 64; export const registryMaxDataSize = 64;

View File

@ -1,7 +1,7 @@
import { base64url } from "multiformats/bases/base64"; import { base64url } from "multiformats/bases/base64";
import { base32 } from "multiformats/bases/base32"; import { base32 } from "multiformats/bases/base32";
import { equalBytes } from "@noble/curves/abstract/utils"; import { equalBytes } from "@noble/curves/abstract/utils";
import { cidTypeBridge } from "#constants.js"; import { CID_TYPES } from "#constants.js";
export class Multihash { export class Multihash {
fullBytes: Uint8Array; fullBytes: Uint8Array;
@ -35,7 +35,7 @@ export class Multihash {
} }
toString(): string { toString(): string {
return this.functionType === cidTypeBridge return this.functionType === CID_TYPES.BRIDGE
? new TextDecoder().decode(this.fullBytes) ? new TextDecoder().decode(this.fullBytes)
: this.toBase64Url(); : this.toBase64Url();
} }