CID class

Represents a Content Identifier (CID).

CIDs are self-describing content-addressed identifiers used in systems like IPFS. They uniquely identify a piece of content using a cryptographic hash and include metadata about the content's encoding and hash algorithm.

This class supports parsing CIDs from strings or bytes, creating new CIDs (including from raw data), and converting between CID versions.

See the CID specification for more details.

Examples

Creating a CID from data:

final data = utf8.encode('Hello World!');
final cid = CID.fromData(CID.V1, 'dag-pb', Uint8List.fromList(data));
print(cid.toString()); // e.g., bafybeid7qoywk77r7rj3slobqfekdvs57qwuwh5d2z3sqsw52iabe3mqne

Parsing a CID string:

final parsedCid = CID.fromString('QmWvQxTqbG2Z9HPJgG57jjwR154cKhbtJenbyYTWkjgF3e');
print('Version: ${parsedCid.version}, Codec: ${parsedCid.codecName}');

Constructors

CID.new(int version, int codec, Uint8List multihash)
Creates a new CID instance from its constituent parts.
CID.create(int version, String codecName, Uint8List multihash)
Creates a CID instance from a version, codec name, and pre-computed multihash.
factory
CID.fromBytes(Uint8List bytes)
Decodes a CID from its binary representation (Uint8List).
factory
CID.fromData(int version, String codecName, Uint8List data, [String multihashName = 'sha2-256'])
Creates a CID by hashing the input data.
factory
CID.fromString(String s)
Decodes a CID from its string representation.
factory

Properties

codec int
The multicodec code indicating the format of the content referenced by the CID.
final
codecName String
Returns the string name of the codec (e.g., "dag-pb", "raw").
no setter
hashCode int
The hash code for this object.
no setteroverride
multihash Uint8List
The full multihash bytes, including the hash algorithm code, digest length, and the hash digest itself.
final
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
version int
The version of the CID (either V0 or V1).
final

Methods

noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toBytes() Uint8List
Encodes this CID into its binary representation as a Uint8List.
toString() String
Returns the string representation of this CID.
override
toV0() CID
Converts this CID to CIDv0, if possible.
toV1() CID
Converts this CID to CIDv1.

Operators

operator ==(Object other) bool
The equality operator.
override

Constants

V0 → const int
Constant representing CID Version 0.
V1 → const int
Constant representing CID Version 1.