fromContent static method

Future<CID> fromContent(
  1. Uint8List content, {
  2. String codec = 'raw',
  3. String hashType = 'sha2-256',
  4. int version = 1,
})

Creates a CID from raw content.

Specify codec, hashType, and version if they differ from defaults.

Implementation

static Future<CID> fromContent(
  Uint8List content, {
  String codec = 'raw',
  String hashType = 'sha2-256',
  int version = 1,
}) async {
  Digest digest;
  if (hashType == 'sha2-256') {
    digest = sha256.convert(content);
  } else {
    throw UnsupportedError('Hash type $hashType not supported');
  }

  final mhInfo = Multihash.encode(hashType, Uint8List.fromList(digest.bytes));

  if (version == 0) {
    return CID.v0(Uint8List.fromList(digest.bytes));
  } else {
    return CID.v1(codec, mhInfo);
  }
}