CID constructor
Creates a new CID instance from its constituent parts.
version
: The CID version, must be either CID.V0 or CID.V1.codec
: The multicodec code (integer) indicating the format of the content.multihash
: The full multihashUint8List
(code + length + digest).
Throws ArgumentError if the version is invalid or if a CIDv0 is constructed with a codec other than 'dag-pb'.
Consider using factory constructors like CID.fromData, CID.fromString, or CID.fromBytes for more common use cases.
Implementation
CID(this.version, this.codec, this.multihash) {
if (version != V0 && version != V1) {
throw ArgumentError('Invalid CID version: $version. Must be $V0 or $V1.');
}
// Further validation for multihash structure could be added here.
// For V0, codec is implicitly dag-pb (0x70) and multihash is sha2-256.
if (version == V0) {
if (codec != codecNameToCode['dag-pb']!) {
throw ArgumentError('CIDv0 codec must be dag-pb (0x${codecNameToCode['dag-pb']!.toRadixString(16)}), got 0x${codec.toRadixString(16)}');
}
// V0 multihashes are implicitly sha2-256, 32 bytes digest.
// The multihash package should handle this, but a check here could be useful.
// For example, check if mh.Multihash.decode(multihash).name == 'sha2-256'
}
}