fromCid static method

PeerId fromCid(
  1. CID cid
)

FromCid converts a CID to a peer ID, if possible.

Implementation

static PeerId fromCid(cid_lib.CID cid) { // Parameter type updated to aliased CID
  // Note: The input 'cid' here is already a CID object.
  // Use constants CID.V1, CID.V0 and properties from the cid object.
  // Access codec from cid_lib.codecs map.
  if (cid.version == cid_lib.CID.V1 && cid.codec == cid_lib.codecNameToCode['libp2p-key']!) {
    return PeerId(cid.multihash);
  }
  if (cid.version == cid_lib.CID.V0) {
    // CIDv0 is also acceptable, its multihash is used directly.
    return PeerId(cid.multihash);
  }
  throw FormatException('Invalid CID: not a libp2p-key CID (v1 with codec 0x${cid_lib.codecNameToCode['libp2p-key']!}) or a CIDv0. Got v${cid.version} codec 0x${cid.codec.toRadixString(16)}');
}