UNPKG

@atproto/did

Version:

DID resolution and verification library

71 lines 2.4 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.atprotoDidSchema = void 0; exports.isAtprotoDid = isAtprotoDid; exports.asAtprotoDid = asAtprotoDid; exports.assertAtprotoDid = assertAtprotoDid; exports.assertAtprotoDidWeb = assertAtprotoDidWeb; exports.isAtprotoDidWeb = isAtprotoDidWeb; const zod_1 = require("zod"); const did_error_js_1 = require("./did-error.js"); const methods_js_1 = require("./methods.js"); exports.atprotoDidSchema = zod_1.z .string() .refine(isAtprotoDid, `Atproto only allows "plc" and "web" DID methods`); function isAtprotoDid(input) { // Optimized equivalent of: // return isDidPlc(input) || isAtprotoDidWeb(input) if (typeof input !== 'string') { return false; } else if (input.startsWith(methods_js_1.DID_PLC_PREFIX)) { return (0, methods_js_1.isDidPlc)(input); } else if (input.startsWith(methods_js_1.DID_WEB_PREFIX)) { return isAtprotoDidWeb(input); } else { return false; } } function asAtprotoDid(input) { assertAtprotoDid(input); return input; } function assertAtprotoDid(input) { if (typeof input !== 'string') { throw new did_error_js_1.InvalidDidError(typeof input, `DID must be a string`); } else if (input.startsWith(methods_js_1.DID_PLC_PREFIX)) { (0, methods_js_1.assertDidPlc)(input); } else if (input.startsWith(methods_js_1.DID_WEB_PREFIX)) { assertAtprotoDidWeb(input); } else { throw new did_error_js_1.InvalidDidError(input, `Atproto only allows "plc" and "web" DID methods`); } } function assertAtprotoDidWeb(input) { (0, methods_js_1.assertDidWeb)(input); if (input.includes(':', methods_js_1.DID_WEB_PREFIX.length)) { throw new did_error_js_1.InvalidDidError(input, `Atproto does not allow path components in Web DIDs`); } if (input.includes('%3A', methods_js_1.DID_WEB_PREFIX.length) && !input.startsWith('did:web:localhost%3A')) { throw new did_error_js_1.InvalidDidError(input, `Atproto does not allow port numbers in Web DIDs, except for localhost`); } } /** * @see {@link https://atproto.com/specs/did#blessed-did-methods} */ function isAtprotoDidWeb(input) { try { assertAtprotoDidWeb(input); return true; } catch { return false; } } //# sourceMappingURL=atproto.js.map