{"record":{"id":"f99427de46aa9bad","repo":"FuelLabs/fuels-ts","slug":"encode-error-f99427","errorCode":"ENCODE_ERROR","errorMessage":"Invalid ${this.type}.","messagePattern":"Invalid (.+?)\\.","errorType":"error_code","errorClass":"FuelError","httpStatus":null,"severity":"error","filePath":"packages/abi-coder/src/encoding/coders/B256Coder.ts","lineNumber":19,"sourceCode":"import { ErrorCode, FuelError } from '@fuel-ts/errors';\nimport { bn, toHex } from '@fuel-ts/math';\nimport { arrayify } from '@fuel-ts/utils';\n\nimport { WORD_SIZE } from '../../utils/constants';\n\nimport { Coder } from './AbstractCoder';\n\nexport class B256Coder extends Coder<string, string> {\n  constructor() {\n    super('b256', 'b256', WORD_SIZE * 4);\n  }\n\n  encode(value: string): Uint8Array {\n    let encodedValue;\n    try {\n      encodedValue = arrayify(value);\n    } catch (error) {\n      throw new FuelError(ErrorCode.ENCODE_ERROR, `Invalid ${this.type}.`);\n    }\n    if (encodedValue.length !== this.encodedLength) {\n      throw new FuelError(ErrorCode.ENCODE_ERROR, `Invalid ${this.type}.`);\n    }\n    return encodedValue;\n  }\n\n  decode(data: Uint8Array, offset: number): [string, number] {\n    if (data.length < this.encodedLength) {\n      throw new FuelError(ErrorCode.DECODE_ERROR, `Invalid b256 data size.`);\n    }\n\n    let bytes = data.slice(offset, offset + this.encodedLength);\n\n    const decoded = bn(bytes);\n    if (decoded.isZero()) {\n      bytes = new Uint8Array(32);\n    }","sourceCodeStart":1,"sourceCodeEnd":37,"githubUrl":"https://github.com/FuelLabs/fuels-ts/blob/b3f37c91aca4aa9d5e4c0d3967f66237190826ea/packages/abi-coder/src/encoding/coders/B256Coder.ts#L1-L37","documentation":"Thrown by `B256Coder.encode` (code `ENCODE_ERROR`) when `arrayify(value)` throws while converting the input to bytes — i.e. the value is not a valid hex string the bytes utility can parse. `b256` expects a 32-byte hex value such as a Fuel address/Hash.","triggerScenarios":"Passing a non-hex string (`'0xzz...'`), a value missing the `0x` prefix where required, a number, an object, or `undefined` to a `b256` input; passing a base58 bech32 string instead of hex.","commonSituations":"Reading an ID from an external source that returns a different encoding; typos in literal hex strings; passing a `BN` instance where a hex string is expected.","solutions":["Pass a hex string of the form `0x` + 64 hex characters (32 bytes).","If you hold a different representation, convert it first — e.g. `address.toB256()` for a `Address`/bech32 value, or `bn(value).toHex(32)`.","Validate the format with a regex (`/^0x[0-9a-fA-F]{64}$/`) before encoding."],"exampleFix":"// before\nawait contract.functions.foo('0xABC').call(); // too short / invalid\n// after\nawait contract.functions.foo('0x' + 'ab'.repeat(32)).call();","handlingStrategy":"type-guard","validationCode":"const B256_RE = /^0x[0-9a-fA-F]{64}$/;\nif (typeof value !== 'string' || !B256_RE.test(value)) {\n  throw new TypeError(`Expected a 32-byte hex string (0x + 64 hex chars)`);\n}","typeGuard":"function isB256Hex(value) {\n  return typeof value === 'string' && /^0x[0-9a-fA-F]{64}$/.test(value);\n}","tryCatchPattern":"try {\n  b256Coder.encode(value);\n} catch (e) {\n  if (e.code === 'ENCODE_ERROR' && /Invalid b256/.test(e.message)) {\n    value = bn(value).toHex(32); // normalize, retry\n  } else throw e;\n}","preventionTips":["Validate b256 inputs with a `/^0x[0-9a-fA-F]{64}$/` regex before encoding.","Use `Address.toB256()` / `bn.toHex(32)` to normalize other representations.","Avoid passing numeric or non-hex encodings to b256 fields."],"tags":["abi-coder","encoding","b256","validation"],"backgroundTag":null,"analyzedSha":"b3f37c91aca4aa9d5e4c0d3967f66237190826ea","analyzedAt":"2026-08-12T20:30:56.448Z","schemaVersion":2},"datasetVersion":"2026-08-12T23:17:12.415Z"}