FuelLabs/fuels-ts · error · FuelError
INVALID_DATA
INVALID_DATA
Error message
invalid data:${nameMessage} ${value}
If you are attempting to transform a hex value, please make sure it is being passed as a string and wrapped in quotes. What it means
Thrown by arrayify when the input value is neither a Uint8Array nor a string matching the strict hex regex /^0x([0-9a-f][0-9a-f])*$/i. The regex requires an even number of hex digits prefixed with exactly '0x'. Any other shape (odd-length hex, missing prefix, number, object) is treated as invalid data.
Source
Thrown at packages/utils/src/utils/arrayify.ts:33
if (copy) {
return new Uint8Array(value);
}
return value;
}
if (typeof value === 'string' && value.match(/^0x([0-9a-f][0-9a-f])*$/i)) {
const result = new Uint8Array((value.length - 2) / 2);
let offset = 2;
for (let i = 0; i < result.length; i++) {
result[i] = parseInt(value.substring(offset, offset + 2), 16);
offset += 2;
}
return result;
}
const nameMessage = name ? ` ${name} -` : '';
const message = `invalid data:${nameMessage} ${value}\nIf you are attempting to transform a hex value, please make sure it is being passed as a string and wrapped in quotes.`;
throw new FuelError(ErrorCode.INVALID_DATA, message);
};
View on GitHub (pinned to b3f37c91ac)
Solutions
- Pass the value as a string with an '0x' prefix and an even number of hex digits (e.g. '0xdeadbeef').
- If you have a number, convert it first: '0x' + value.toString(16) (and pad to even length).
- Wrap hex literals in quotes in JSON/config so they are not parsed as numbers.
- Validate with a regex before calling arrayify when data is untrusted.
Example fix
// before
arrayify(deadbeef); // ReferenceError / not a string
arrayify('abc'); // no 0x prefix
arrayify('0xabc'); // odd length
// after
arrayify('0xdeadbeef'); // valid: prefixed, even length
arrayify(new Uint8Array([0xde, 0xad, 0xbe, 0xef])); Defensive patterns
Strategy: validation
Validate before calling
const HEX_RE = /^0x([0-9a-f][0-9a-f])*$/i;
function isBytesLike(v: unknown): v is Uint8Array | string {
return v instanceof Uint8Array || (typeof v === 'string' && HEX_RE.test(v));
}
if (!isBytesLike(value)) throw new Error('expected Uint8Array or 0x-prefixed even-length hex');
arrayify(value); Type guard
const isHexBytes = (v: unknown): v is string => typeof v === 'string' && /^0x([0-9a-f][0-9a-f])*$/i.test(v);
Try / catch
try {
const bytes = arrayify(value, 'myField');
} catch (e) {
if (e instanceof FuelError && e.code === ErrorCode.INVALID_DATA) {
// coerce or reject the bad input
}
throw e;
} Prevention
- Always pass hex as a quoted string with an '0x' prefix and even digit count.
- Convert numbers to hex strings before passing to byte utilities.
- Pre-validate untrusted input with the hex regex.
When it happens
Trigger: Calling arrayify(value) with: a hex string lacking the '0x' prefix (e.g. 'deadbeef'), a hex string with an odd number of digits (e.g. '0xabc'), a plain number, a boolean, an object, or undefined. The nameMessage includes the optional 'name' argument for context.
Common situations: Passing a number where a hex string is expected. Forgetting quotes around a hex literal in config/JSON (parses as a number). Concatenating hex fragments and ending up with an odd digit count. Copy-pasting a hex value without the '0x' prefix. Calling low-level utils directly with data from an untyped source.
Related errors
AI-assisted analysis of FuelLabs/fuels-ts@b3f37c91ac (2026-08-12).
Data as JSON: /api/errors/7dca091dcf460b27.
Report an issue: GitHub.