UNPKG

@arcblock/did

Version:

Javascript lib to work with ArcBlock DID

91 lines (89 loc) 2.96 kB
const require_method = require('./method.cjs'); //#region src/parse.ts /** * Universal DID Parser * * Parses, formats, and extracts components from DID strings. * Supports all registered methods (abt, afs, aos, spaces, name). */ /** DID format regex: did:{method}:{identifier} where method is [a-z0-9]+ */ const DID_REGEX = /^did:([a-z0-9]+):(.+)$/; /** * Parse a full DID string into its components. * * @param did - Full DID string (e.g. 'did:abt:z1abc...') * @returns Parsed DID object * @throws If the input is not a valid DID string */ function parse(did) { if (typeof did !== "string") throw new Error("DID must be a string"); if (!did) throw new Error("DID string cannot be empty"); const match = did.match(DID_REGEX); if (!match) throw new Error("Invalid DID format"); const [, method, identifier] = match; if (!method) throw new Error("Invalid DID format"); if (!identifier) throw new Error("Invalid DID format"); if (!require_method.isKnownMethod(method)) throw new Error(`Unknown DID method: ${method}`); return { method, identifier, full: did }; } /** * Format an identifier and method into a full DID string. * * Handles idempotency: if the identifier already has a `did:{method}:` prefix * matching the target method, returns it as-is. * * @param identifier - The identifier (address or name) * @param method - The DID method (default: 'abt') * @returns Full DID string * @throws If the method is unknown or identifier is empty */ function formatDid(identifier, method = require_method.DEFAULT_METHOD) { if (!identifier) throw new Error("Identifier cannot be empty"); if (!require_method.isKnownMethod(method)) throw new Error(`Unknown DID method: ${method}`); const prefix = `did:${method}:`; if (identifier.startsWith(prefix)) return identifier; if (identifier.startsWith("did:")) { const match = identifier.match(DID_REGEX); if (match) { if (match[1] === method) return identifier; return `did:${method}:${match[2]}`; } } return `did:${method}:${identifier}`; } /** * Extract the method from a DID string. * * @param did - A DID string or bare address * @returns The method string, or null if no prefix found */ function extractMethod(did) { if (typeof did !== "string" || !did) return null; const match = did.match(DID_REGEX); if (!match) return null; const method = match[1]; if (require_method.isKnownMethod(method)) return method; return null; } /** * Extract the identifier from a DID string, stripping the `did:{method}:` prefix. * If the input has no prefix, returns it as-is. * * @param did - A DID string or bare address * @returns The identifier portion */ function extractIdentifier(did) { if (typeof did !== "string" || !did) return ""; const match = did.match(/^did:[a-z0-9]+:(.+)$/); if (match) return match[1]; return did; } //#endregion exports.extractIdentifier = extractIdentifier; exports.extractMethod = extractMethod; exports.formatDid = formatDid; exports.parse = parse;