fromU64 static method

CanisterId fromU64(
  1. int val
)

Implementation

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

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

  // for-loops in const fn are not supported
  data[0] = valU8a[0];
  data[1] = valU8a[1];
  data[2] = valU8a[2];
  data[3] = valU8a[3];
  data[4] = valU8a[4];
  data[5] = valU8a[5];
  data[6] = valU8a[6];
  data[7] = valU8a[7];

  // 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;

  var blobLength = 8 /* the u64 */ + 1 /* the last 0x01 */;

  data[blobLength] = TYPE_OPAQUE;
  return CanisterId(
      Principal.create(blobLength + 1, Uint8List.fromList(data)));
  // Self(PrincipalId::new_opaque_from_array(data, blob_length))
}