toBytes method
Converts the CID to its binary representation.
Implementation
Uint8List toBytes() {
if (version == 0) {
// CIDv0 is just the multihash
return multihash.toBytes();
}
// CIDv1: <version><codec><multihash>
final buffer = BytesBuilder();
buffer.addByte(0x01); // version 1
// Encode codec as varint
int codecCode;
try {
codecCode = EncodingUtils.getCodeFromCodec(codec ?? 'raw');
} catch (e) {
// If codec not found, fallback to raw or throw?
// Let's assume raw if unknown? Or throw to prevent bad CIDs?
// Existing code defaulted to _raw.
// But existing code only checked 'dag-pb'.
// If I pass 'dag-cbor', it defaulted to raw.
// Now I want it to find 'dag-cbor'.
// If 'unknown', throw.
throw FormatException('Unsupported codec during CID encoding: $codec');
}
buffer.add(_encodeVarint(codecCode));
// Add multihash
buffer.add(multihash.toBytes());
return buffer.toBytes();
}