CanisterId.fromU64 constructor

CanisterId.fromU64(
  1. int val
)

Implementation

factory CanisterId.fromU64(int val) {
  // It is important to use big endian here to ensure that the generated
  // `PrincipalId`s still maintain ordering.
  final data = List.generate(_maxLengthInBytes, (index) => 0);

  // Specify explicitly the length, so as to assert at compile time that a u64
  // takes exactly 8 bytes
  final valU8a = val.toU8a(bitLength: 64);

  data.replaceRange(0, 8, valU8a.sublist(0, 8));
  // Even though not defined in the interface spec, add another 0x1 to the array
  // to create a sub category that could be used in future.
  data[8] = 0x01;

  const blobLength = 8 + 1; // The U64 + The last 0x01.

  data[blobLength] = _typeOpaque;
  return CanisterId(
    Principal.create(blobLength + 1, Uint8List.fromList(data), null),
  );
}