FuelLabs/fuels-ts · error · FuelError
ENCODE_ERROR
ENCODE_ERROR
Error message
Invalid ${this.type}. What it means
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.
Source
Thrown at packages/abi-coder/src/encoding/coders/B256Coder.ts:19
import { ErrorCode, FuelError } from '@fuel-ts/errors';
import { bn, toHex } from '@fuel-ts/math';
import { arrayify } from '@fuel-ts/utils';
import { WORD_SIZE } from '../../utils/constants';
import { Coder } from './AbstractCoder';
export class B256Coder extends Coder<string, string> {
constructor() {
super('b256', 'b256', WORD_SIZE * 4);
}
encode(value: string): Uint8Array {
let encodedValue;
try {
encodedValue = arrayify(value);
} catch (error) {
throw new FuelError(ErrorCode.ENCODE_ERROR, `Invalid ${this.type}.`);
}
if (encodedValue.length !== this.encodedLength) {
throw new FuelError(ErrorCode.ENCODE_ERROR, `Invalid ${this.type}.`);
}
return encodedValue;
}
decode(data: Uint8Array, offset: number): [string, number] {
if (data.length < this.encodedLength) {
throw new FuelError(ErrorCode.DECODE_ERROR, `Invalid b256 data size.`);
}
let bytes = data.slice(offset, offset + this.encodedLength);
const decoded = bn(bytes);
if (decoded.isZero()) {
bytes = new Uint8Array(32);
}View on GitHub (pinned to b3f37c91ac)
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.
Example fix
// before
await contract.functions.foo('0xABC').call(); // too short / invalid
// after
await contract.functions.foo('0x' + 'ab'.repeat(32)).call(); Defensive patterns
Strategy: type-guard
Validate before calling
const B256_RE = /^0x[0-9a-fA-F]{64}$/;
if (typeof value !== 'string' || !B256_RE.test(value)) {
throw new TypeError(`Expected a 32-byte hex string (0x + 64 hex chars)`);
} Type guard
function isB256Hex(value) {
return typeof value === 'string' && /^0x[0-9a-fA-F]{64}$/.test(value);
} Try / catch
try {
b256Coder.encode(value);
} catch (e) {
if (e.code === 'ENCODE_ERROR' && /Invalid b256/.test(e.message)) {
value = bn(value).toHex(32); // normalize, retry
} else throw e;
} Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
AI-assisted analysis of FuelLabs/fuels-ts@b3f37c91ac (2026-08-12).
Data as JSON: /api/errors/f99427de46aa9bad.
Report an issue: GitHub.