import { CID } from 'multiformats/cid'; /** * Codec code that indicates the CID references a CBOR-encoded data structure. * * Used when encoding structured data in AT Protocol repositories. * * @see {@link https://dasl.ing/cid.html Content IDs (DASL)} */ export declare const CBOR_DATA_CODEC = 113; export type CBOR_DATA_CODEC = typeof CBOR_DATA_CODEC; /** * Codec code that indicates the CID references raw binary data (like media blobs). * * Used in DASL CIDs for binary blobs like images and media. * * @see {@link https://dasl.ing/cid.html Content IDs (DASL)} */ export declare const RAW_DATA_CODEC = 85; export type RAW_DATA_CODEC = typeof RAW_DATA_CODEC; /** * Hash code that indicates that a CID uses SHA-256. */ export declare const SHA256_HASH_CODE: 18; export type SHA256_HASH_CODE = typeof SHA256_HASH_CODE; /** * Hash code that indicates that a CID uses SHA-512. */ export declare const SHA512_HASH_CODE: 19; export type SHA512_HASH_CODE = typeof SHA512_HASH_CODE; /** * Represent the hash part of a CID, which includes the hash algorithm code and * the raw digest bytes. * * @see {@link https://dasl.ing/cid.html Content IDs (DASL)} */ export interface Multihash { /** * Code of the hash algorithm (e.g., SHA256_HASH_CODE). */ code: THashCode; /** * Raw digest bytes. */ digest: Uint8Array; } /** * Compares two {@link Multihash} for equality. * * @param a - First {@link Multihash} * @param b - Second {@link Multihash} * @returns `true` if both multihashes have the same code and digest */ export declare function multihashEquals(a: Multihash, b: Multihash): boolean; declare module 'multiformats/cid' { /** * @deprecated use the {@link Cid} interface from `@atproto/lex-data`, and * related helpers ({@link isCid}, {@link ifCid}, {@link asCid}, * {@link parseCid}, {@link decodeCid}), instead. * * This is marked as deprecated because we want to discourage direct usage of * `multiformats/cid` in dependent packages, and instead have them rely on the * {@link Cid} interface from `@atproto/lex-data`. The {@link CID} class from * `multiformats` version <10 has compatibility issues with certain TypeScript * configuration, which can lead to type errors in dependent packages. * * We are stuck with version 9 because `@atproto` packages did not drop * CommonJS support yet, and multiformats version 10 only supports ES modules. * * In order to avoid compatibility issues, while preparing for future breaking * changes (CID in multiformats v10+ has a slightly different interface), as * we update or swap out `multiformats`, `@atproto/lex-data` provides its own * stable {@link Cid} interface. */ interface CID { } } export { /** @deprecated */ CID }; /** * Converts a {@link Cid} to a multiformats {@link CID} instance. * * @deprecated Packages depending on `@atproto/lex-data` should use the * {@link Cid} interface instead of relying on `multiformats`'s {@link CID} * implementation directly. This is to avoid compatibility issues, and in order * to allow better portability, compatibility and future updates. */ export declare function asMultiformatsCID(input: Cid): CID & Cid; /** * Content Identifier (CID) for addressing content by its hash. * * CIDs are self-describing content addresses used throughout AT Protocol for * linking to data by its cryptographic hash. This interface provides a * stable API that is compatible with the `multiformats` library but avoids * direct dependency issues. * * @typeParam TVersion - CID version (0 or 1) * @typeParam TCodec - Multicodec content type code * @typeParam THashCode - Multihash algorithm code * * @example * ```typescript * import type { Cid } from '@atproto/lex-data' * import { parseCid, isCid } from '@atproto/lex-data' * * // Parse a CID from a string * const cid: Cid = parseCid('bafyreib...') * * // Check if a value is a CID * if (isCid(value)) { * console.log(cid.toString()) * } * ``` * * @see {@link isCid} to check if a value is a valid CID * @see {@link parseCid} to parse a CID from a string * @see {@link decodeCid} to decode a CID from bytes * @see {@link https://dasl.ing/cid.html Content IDs (DASL)} */ export interface Cid { /** CID version (0 or 1). AT Protocol uses CIDv1. */ readonly version: TVersion; /** Coded (e.g., {@link CBOR_DATA_CODEC}, {@link RAW_DATA_CODEC}). */ readonly code: TCodec; /** The multihash containing the hash algorithm and digest. */ readonly multihash: Multihash; /** * Binary representation of the whole CID. */ readonly bytes: Uint8Array; /** * Compares this CID with another for equality. * * @param other - The CID to compare with * @returns `true` if the CIDs are equal */ equals(other: Cid): boolean; /** * Returns the string representation of this CID (base32 for v1, base58btc for v0). */ toString(): string; } /** * Represents the CID of raw binary data (like media blobs). * * The use of {@link SHA256_HASH_CODE} is recommended but not required for raw CIDs. * * @see {@link https://atproto.com/specs/data-model#link-and-cid-formats AT Protocol Data Model - Link and CID Formats} */ export type RawCid = Cid<1, RAW_DATA_CODEC>; /** * Type guard to check if a CID is a raw binary CID. * * @param cid - The CID to check * @returns `true` if the CID is a version 1 CID with raw multicodec */ export declare function isRawCid(cid: Cid): cid is RawCid; /** * Represents a DASL compliant CID. * * DASL CIDs are version 1 CIDs using either raw or DAG-CBOR multicodec * with SHA-256 multihash. * * @see {@link https://dasl.ing/cid.html Content IDs (DASL)} */ export type DaslCid = Cid<1, RAW_DATA_CODEC | CBOR_DATA_CODEC, SHA256_HASH_CODE>; /** * Type guard to check if a CID is DASL compliant. * * @param cid - The CID to check * @returns `true` if the CID is DASL compliant (v1, raw/dag-cbor, sha256) */ export declare function isDaslCid(cid: Cid): cid is DaslCid; /** * Represents the CID of AT Protocol DAG-CBOR data (like repository MST nodes). * * CBOR CIDs are version 1 CIDs using DAG-CBOR multicodec with SHA-256 multihash. * * @see {@link https://atproto.com/specs/data-model#link-and-cid-formats AT Protocol Data Model - Link and CID Formats} */ export type CborCid = Cid<1, CBOR_DATA_CODEC, SHA256_HASH_CODE>; /** * Type guard to check if a CID is a DAG-CBOR CID. * * @param cid - The CID to check * @returns `true` if the CID is a DAG-CBOR CID (v1, dag-cbor, sha256) */ export declare function isCborCid(cid: Cid): cid is CborCid; /** * Options for checking CID flavor constraints. */ export type CheckCidOptions = { /** * The CID flavor to check for. * - `'raw'` - Raw binary CID ({@link RawCid}) * - `'cbor'` - DAG-CBOR CID ({@link CborCid}) * - `'dasl'` - DASL compliant CID ({@link DaslCid}) */ flavor?: 'raw' | 'cbor' | 'dasl'; }; /** * Infers the CID type based on check options. * * @typeParam TOptions - The options used for checking */ export type InferCheckedCid = TOptions extends { flavor: 'raw'; } ? RawCid : TOptions extends { flavor: 'cbor'; } ? CborCid : Cid; /** * Type guard to check whether a {@link Cid} instance meets specific flavor * constraints. */ export declare function checkCid(cid: Cid, options: TOptions): cid is InferCheckedCid; export declare function checkCid(cid: Cid, options?: CheckCidOptions): boolean; /** * Type guard to check whether a value is a valid {@link Cid} instance, * optionally checking for specific flavor constraints. */ export declare function isCid(value: unknown, options: TOptions): value is InferCheckedCid; export declare function isCid(value: unknown, options?: CheckCidOptions): value is Cid; /** * Returns the input value as a {@link Cid} if it is valid, or `null` otherwise. */ export declare function ifCid(value: unknown, options: TOptions): (TValue & InferCheckedCid) | null; export declare function ifCid(value: TValue, options?: CheckCidOptions): (TValue & Cid) | null; /** * Returns the input value as a {@link Cid} if it is valid. * * @throws if the input is not a valid {@link Cid}. */ export declare function asCid(value: TValue, options: TOptions): TValue & InferCheckedCid; export declare function asCid(value: TValue, options?: CheckCidOptions): TValue & Cid; /** * Decodes a CID from its binary representation. * * @see {@link https://dasl.ing/cid.html DASL-CIDs} * @throws if the input do not represent a valid DASL {@link Cid} */ export declare function decodeCid(cidBytes: Uint8Array, options: TOptions): InferCheckedCid; export declare function decodeCid(cidBytes: Uint8Array, options?: CheckCidOptions): Cid; /** * Parses a CID string into a Cid object. * * @throws if the input is not a valid CID string. */ export declare function parseCid(input: string, options: TOptions): InferCheckedCid; export declare function parseCid(input: string, options?: CheckCidOptions): Cid; /** * Validates that a string is a valid CID representation. * * Unlike {@link parseCid}, this function returns a boolean instead of throwing. * It also verifies that the string is the canonical representation of the CID. * * @param input - The string to validate * @param options - Optional flavor constraints * @returns `true` if the string is a valid CID */ export declare function validateCidString(input: string, options?: CheckCidOptions): boolean; /** * Safely parses a CID string, returning `null` on failure instead of throwing. * * @param input - The string to parse * @param options - Optional flavor constraints * @returns The parsed CID, or `null` if parsing fails * * @example * ```typescript * import { parseCidSafe } from '@atproto/lex-data' * * const cid = parseCidSafe('bafyreib...') * if (cid) { * console.log(cid.toString()) * } * ``` */ export declare function parseCidSafe(input: string, options: TOptions): InferCheckedCid | null; export declare function parseCidSafe(input: string, options?: CheckCidOptions): Cid | null; /** * Ensures that a string is a valid CID representation. * * @param input - The string to validate * @param options - Optional flavor constraints * @throws If the string is not a valid CID */ export declare function ensureValidCidString(input: string, options?: CheckCidOptions): void; /** * Verifies whether the multihash of a given {@link cid} matches the hash of the provided {@link bytes}. * @params cid The CID to match against the bytes. * @params bytes The bytes to verify. * @returns true if the CID matches the bytes, false otherwise. */ export declare function isCidForBytes(cid: Cid, bytes: Uint8Array): Promise; /** * Creates a CID from a multicodec, multihash code, and digest. * * @param code - The multicodec content type code * @param multihashCode - The multihash algorithm code * @param digest - The raw hash digest bytes * @returns A new CIDv1 instance * * @example * ```typescript * import { createCid, RAW_DATA_CODEC, SHA256_HASH_CODE } from '@atproto/lex-data' * * const cid = createCid(RAW_DATA_CODEC, SHA256_HASH_CODE, hashDigest) * ``` */ export declare function createCid(code: TCodec, multihashCode: THashCode, digest: Uint8Array): Cid<1, TCodec, THashCode>; /** * Creates a DAG-CBOR CID for the given CBOR bytes. * * Computes the SHA-256 hash of the bytes and creates a CIDv1 with DAG-CBOR multicodec. * * @param bytes - The CBOR-encoded bytes to hash * @returns A promise that resolves to the CborCid */ export declare function cidForCbor(bytes: Uint8Array): Promise; /** * Creates a raw CID for the given binary bytes. * * Computes the SHA-256 hash of the bytes and creates a CIDv1 with raw multicodec. * * @param bytes - The raw binary bytes to hash * @returns A promise that resolves to the RawCid */ export declare function cidForRawBytes(bytes: Uint8Array): Promise; /** * Creates a raw CID from an existing SHA-256 hash digest. * * @param digest - The SHA-256 hash digest (must be 32 bytes) * @returns A RawCid with the given digest * @throws If the digest is not a valid SHA-256 hash (not 32 bytes) */ export declare function cidForRawHash(digest: Uint8Array): RawCid; //# sourceMappingURL=cid.d.ts.map