@atproto/did
Version:
DID resolution and verification library
49 lines • 1.87 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.DID_PLC_PREFIX = void 0;
exports.isDidPlc = isDidPlc;
exports.asDidPlc = asDidPlc;
exports.assertDidPlc = assertDidPlc;
const did_error_js_1 = require("../did-error.js");
const DID_PLC_PREFIX = `did:plc:`;
exports.DID_PLC_PREFIX = DID_PLC_PREFIX;
const DID_PLC_PREFIX_LENGTH = DID_PLC_PREFIX.length;
const DID_PLC_LENGTH = 32;
function isDidPlc(input) {
// Optimization: equivalent to try/catch around "assertDidPlc"
if (typeof input !== 'string')
return false;
if (input.length !== DID_PLC_LENGTH)
return false;
if (!input.startsWith(DID_PLC_PREFIX))
return false;
for (let i = DID_PLC_PREFIX_LENGTH; i < DID_PLC_LENGTH; i++) {
if (!isBase32Char(input.charCodeAt(i)))
return false;
}
return true;
}
function asDidPlc(input) {
assertDidPlc(input);
return input;
}
function assertDidPlc(input) {
if (typeof input !== 'string') {
throw new did_error_js_1.InvalidDidError(typeof input, `DID must be a string`);
}
if (!input.startsWith(DID_PLC_PREFIX)) {
throw new did_error_js_1.InvalidDidError(input, `Invalid did:plc prefix`);
}
if (input.length !== DID_PLC_LENGTH) {
throw new did_error_js_1.InvalidDidError(input, `did:plc must be ${DID_PLC_LENGTH} characters long`);
}
// The following check is not necessary, as the check below is more strict:
// assertDidMsid(input, DID_PLC_PREFIX.length)
for (let i = DID_PLC_PREFIX_LENGTH; i < DID_PLC_LENGTH; i++) {
if (!isBase32Char(input.charCodeAt(i))) {
throw new did_error_js_1.InvalidDidError(input, `Invalid character at position ${i}`);
}
}
}
const isBase32Char = (c) => (c >= 0x61 && c <= 0x7a) || (c >= 0x32 && c <= 0x37); // [a-z2-7]
//# sourceMappingURL=plc.js.map