{"record":{"id":"6f6af5cb61192ca1","repo":"clockworklabs/SpacetimeDB","slug":"uint8array-is-not-16-bytes-long-array","errorCode":null,"errorMessage":"Uint8Array is not 16 bytes long: ${array}","messagePattern":"Uint8Array is not 16 bytes long: (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"crates/bindings-typescript/src/lib/util.ts","lineNumber":48,"sourceCode":"  // Check all keys and compare values recursively\n  for (const key of keys1) {\n    if (!keys2.includes(key) || !deepEqual(obj1[key], obj2[key])) {\n      return false;\n    }\n  }\n\n  return true;\n}\n\nexport function uint8ArrayToHexString(array: Uint8Array): string {\n  return Array.prototype.map\n    .call(array.reverse(), x => ('00' + x.toString(16)).slice(-2))\n    .join('');\n}\n\nexport function uint8ArrayToU128(array: Uint8Array): bigint {\n  if (array.length != 16) {\n    throw new Error(`Uint8Array is not 16 bytes long: ${array}`);\n  }\n  return new BinaryReader(array).readU128();\n}\n\nexport function uint8ArrayToU256(array: Uint8Array): bigint {\n  if (array.length != 32) {\n    throw new Error(`Uint8Array is not 32 bytes long: [${array}]`);\n  }\n  return new BinaryReader(array).readU256();\n}\n\nexport function hexStringToUint8Array(str: string): Uint8Array {\n  if (str.startsWith('0x')) {\n    str = str.slice(2);\n  }\n  const matches = str.match(/.{1,2}/g) || [];\n  const data = Uint8Array.from(\n    matches.map((byte: string) => parseInt(byte, 16))","sourceCodeStart":30,"sourceCodeEnd":66,"githubUrl":"https://github.com/clockworklabs/SpacetimeDB/blob/524b4487d949b61a07d4f39c862d1290259dfd20/crates/bindings-typescript/src/lib/util.ts#L30-L66","documentation":"uint8ArrayToU128 interprets the bytes as a little-endian 128-bit integer via BinaryReader.readU128, so exactly 16 bytes are required; any other length throws. It also backs hexStringToU128, so a hex string decoding to the wrong byte count surfaces here too.","triggerScenarios":"Passing a Uint8Array whose length is not 16 directly, or a hex string that decodes to a non-16-byte array (e.g. a 64-char identity/address hex) through the hex-to-u128 helpers that call this function.","commonSituations":"Reusing 32-byte Identity/address bytes for a u128 field; slicing arrays with wrong offsets; hex strings with odd length or without zero padding.","solutions":["Validate array.length === 16 before calling","Check the source hex string: after stripping '0x' it must be exactly 32 hex chars","Use the matching converter - uint8ArrayToU256 for 32-byte values"],"exampleFix":"// before\nconst v = uint8ArrayToU128(identityBytes); // identityBytes is 32 bytes -> throws\n\n// after\nconst v = uint8ArrayToU256(identityBytes); // 32-byte value\n// or slice the correct 16 bytes: uint8ArrayToU128(identityBytes.slice(0, 16))","handlingStrategy":"validation","validationCode":"function isU128Bytes(array: Uint8Array): boolean {\n  return array.length === 16;\n}\n// if (!isU128Bytes(bytes)) throw new TypeError(`expected 16 bytes, got ${bytes.length}`);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Check length before converting; 16 bytes = u128, 32 bytes = u256","Validate hex strings: 32 hex chars (after stripping 0x) for u128, 64 for u256","Centralize byte-width knowledge in one conversion helper per width"],"tags":["bytes","u128","conversion","javascript","validation"],"backgroundTag":"fixed-width-byte-length-mismatch","analyzedSha":"524b4487d949b61a07d4f39c862d1290259dfd20","analyzedAt":"2026-08-16T23:58:54.611Z","schemaVersion":2},"datasetVersion":"2026-08-17T04:17:16.089Z"}