{"record":{"id":"247b5ece74417905","repo":"FuelLabs/fuels-ts","slug":"encode-error-247b5e","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/B512Coder.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 B512Coder extends Coder<string, string> {\n  constructor() {\n    super('b512', 'struct B512', WORD_SIZE * 8);\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 b512 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(64);\n    }","sourceCodeStart":1,"sourceCodeEnd":37,"githubUrl":"https://github.com/FuelLabs/fuels-ts/blob/b3f37c91aca4aa9d5e4c0d3967f66237190826ea/packages/abi-coder/src/encoding/coders/B512Coder.ts#L1-L37","documentation":"Thrown by B512Coder.encode (this.type resolves to 'struct B512', so the message reads \"Invalid struct B512.\") when arrayify() rejects the input. arrayify only accepts a Uint8Array or a string matching /^0x([0-9a-f][0-9a-f])*$/i; anything else is caught and re-thrown as ENCODE_ERROR before any length check runs. A B512 is a 64-byte FuelVM signature value, so the input must be parseable bytes first.","triggerScenarios":"Calling b512.encode(value), or encoding any function input typed as b512 via Interface/Contract, where value is a string that fails the hex regex: missing '0x' prefix, an odd number of hex digits, non-hex characters (e.g. spaces, '0X' capitalized 'X' path is fine but raw base64/filename text is not), or a non-string/non-Uint8Array runtime value.","commonSituations":"A signature that lost its '0x' during JSON round-trip; passing a base64 or PEM string instead of hex; a value that was coerced to a number and back to a decimal string; copy/paste truncating the hex; feeding a wallet recovery phrase or address where a signature was expected.","solutions":["Normalize the value to a '0x'-prefixed hex string before encode, prepending '0x' if missing.","If you hold raw bytes, pass a Uint8Array (arrayify accepts it) or convert with hexlify first.","Validate the format with a regex and confirm it is exactly 128 hex chars before calling encode.","Trace where the signature originated to stop the malformed value at the source instead of patching at encode time."],"exampleFix":"// before\nsigCoder.encode(sigStr) // sigStr lost its '0x' prefix in transit\n\n// after\nconst clean = sigStr.startsWith('0x') ? sigStr : `0x${sigStr}`\nsigCoder.encode(clean)","handlingStrategy":"validation","validationCode":"const B512_HEX = /^0x[0-9a-fA-F]{128}$/\nfunction isValidB512(v: unknown): v is string {\n  return typeof v === 'string' && B512_HEX.test(v)\n}\nif (!isValidB512(value)) throw new Error('expected 0x-prefixed 64-byte hex')\nsigCoder.encode(value)","typeGuard":"const isHexString = (v: unknown): v is string =>\n  typeof v === 'string' && /^0x([0-9a-fA-F]{2})*$/.test(v)","tryCatchPattern":"try { sigCoder.encode(value) }\ncatch (e) { if ((e as FuelError).code === ErrorCode.ENCODE_ERROR) { /* normalize value or surface input error */ } throw e }","preventionTips":["Keep signatures as '0x'-prefixed hex strings throughout your data flow.","Validate hex format at the trust boundary (API/Form input), not only at encode.","Avoid JSON round-trips that drop or alter the '0x' prefix."],"tags":["abi-coder","encoding","b512","hex","validation"],"backgroundTag":null,"analyzedSha":"b3f37c91aca4aa9d5e4c0d3967f66237190826ea","analyzedAt":"2026-08-12T20:30:56.448Z","schemaVersion":2},"datasetVersion":"2026-08-12T23:17:12.415Z"}