ensureValidDid function

void ensureValidDid(
  1. String did
)

Implementation

void ensureValidDid(final String did) {
  // check that all chars are boring ASCII
  if (!_didAllowedCharsRegExp.hasMatch(did)) {
    throw InvalidDidError(
      'Disallowed characters in DID (ASCII letters, digits, and a couple '
      'other characters only)',
    );
  }

  final parts = did.split(':');
  if (parts.length < 3) {
    throw InvalidDidError(
      'DID requires prefix, method, and method-specific content',
    );
  }

  if (parts[0] != 'did') {
    throw InvalidDidError('DID requires "did:" prefix');
  }

  if (!_didMethodRegExp.hasMatch(parts[1])) {
    throw InvalidDidError('DID method must be lower-case letters');
  }

  if (did.endsWith(':') || did.endsWith('%')) {
    throw InvalidDidError('DID can not end with ":" or "%"');
  }

  // The atproto spec sets a current hard limit of 2048 characters.
  if (did.length > 2048) {
    throw InvalidDidError('DID is too long (2048 chars max)');
  }
}