UNPKG

@atproto/did

Version:

DID resolution and verification library

57 lines 2.5 kB
import { z } from 'zod'; declare const DID_PREFIX = "did:"; export { DID_PREFIX }; /** * Type representation of a Did, with method. * * ```bnf * did = "did:" method-name ":" method-specific-id * method-name = 1*method-char * method-char = %x61-7A / DIGIT * method-specific-id = *( *idchar ":" ) 1*idchar * idchar = ALPHA / DIGIT / "." / "-" / "_" / pct-encoded * pct-encoded = "%" HEXDIG HEXDIG * ``` * * @example * ```ts * type DidWeb = Did<'web'> // `did:web:${string}` * type DidCustom = Did<'web' | 'plc'> // `did:${'web' | 'plc'}:${string}` * type DidNever = Did<' invalid 🥴 '> // never * type DidFoo = Did<'foo' | ' invalid 🥴 '> // `did:foo:${string}` * ``` * * @see {@link https://www.w3.org/TR/did-core/#did-syntax} */ export type Did<M extends string = string> = `did:${AsDidMethod<M>}:${string}`; /** * DID Method */ export type AsDidMethod<M> = string extends M ? string : AsDidMethodInternal<M, ''>; type AlphanumericChar = DigitChar | LowerAlphaChar; type DigitChar = '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9'; type LowerAlphaChar = 'a' | 'b' | 'c' | 'd' | 'e' | 'f' | 'g' | 'h' | 'i' | 'j' | 'k' | 'l' | 'm' | 'n' | 'o' | 'p' | 'q' | 'r' | 's' | 't' | 'u' | 'v' | 'w' | 'x' | 'y' | 'z'; type AsDidMethodInternal<S, Acc extends string> = S extends `${infer H}${infer T}` ? H extends AlphanumericChar ? AsDidMethodInternal<T, `${Acc}${H}`> : never : Acc extends '' ? never : Acc; /** * DID Method-name check function. * * Check if the input is a valid DID method name, at the position between * `start` (inclusive) and `end` (exclusive). */ export declare function assertDidMethod(input: string, start?: number, end?: number): void; /** * This method assumes the input is a valid Did */ export declare function extractDidMethod<D extends Did>(did: D): D extends Did<infer M> ? M : string; /** * DID Method-specific identifier check function. * * Check if the input is a valid DID method-specific identifier, at the position * between `start` (inclusive) and `end` (exclusive). */ export declare function assertDidMsid(input: string, start?: number, end?: number): void; export declare function assertDid(input: unknown): asserts input is Did; export declare function isDid(input: unknown): input is Did; export declare function asDid(input: unknown): Did; export declare const didSchema: z.ZodEffects<z.ZodString, `did:${string}:${string}`, string>; //# sourceMappingURL=did.d.ts.map