CID.parse constructor

CID.parse(
  1. String cid
)

Returns the new instance of CID based on string cid.

Implementation

factory CID.parse(final String cid) {
  if (!cid.startsWith('b')) {
    throw InvalidCidError('CID v1 should be encoded in base32 format');
  }

  // The multibase prefix `b` denotes *lowercase* base32; an uppercase body
  // would use the prefix `B`. Reject a lowercase `b` followed by a
  // non-lowercase base32 body rather than silently accepting it.
  final body = cid.substring(1);
  if (body != body.toLowerCase()) {
    throw InvalidCidError(
      'CID v1 base32 body must be lowercase for multibase prefix "b"',
    );
  }

  // Decode exactly once, then validate the decoded bytes.
  return CID(_ensureBytesFormat(_decode(cid)));
}