{"record":{"id":"0d605661e5929ea8","repo":"gchq/CyberChef","slug":"error-alphabet-must-be-of-length-58","errorCode":null,"errorMessage":"Error: alphabet must be of length 58","messagePattern":"Error: alphabet must be of length 58","errorType":"exception","errorClass":"OperationError","httpStatus":null,"severity":"error","filePath":"src/core/operations/ToBase58.mjs","lineNumber":52,"sourceCode":"            }\n        ];\n    }\n\n    /**\n     * @param {ArrayBuffer} input\n     * @param {Object[]} args\n     * @returns {string}\n     */\n    run(input, args) {\n        input = new Uint8Array(input);\n        let alphabet = args[0] || ALPHABET_OPTIONS[0].value,\n            result = [];\n\n        alphabet = Utils.expandAlphRange(alphabet).join(\"\");\n\n        if (alphabet.length !== 58 ||\n            [].unique.call(alphabet).length !== 58) {\n            throw new OperationError(\"Error: alphabet must be of length 58\");\n        }\n\n        if (input.length === 0) return \"\";\n\n        let zeroPrefix = 0;\n        for (let i = 0; i < input.length && input[i] === 0; i++) {\n            zeroPrefix++;\n        }\n\n        input.forEach(function(b) {\n            let carry = b;\n\n            for (let i = 0; i < result.length; i++) {\n                carry += result[i] << 8;\n                result[i] = carry % 58;\n                carry = (carry / 58) | 0;\n            }\n","sourceCodeStart":34,"sourceCodeEnd":70,"githubUrl":"https://github.com/gchq/CyberChef/blob/4290ea753912378913b1f3f54e0fc5720afeda5d/src/core/operations/ToBase58.mjs#L34-L70","documentation":"To Base58 requires an alphabet of exactly 58 characters, and all 58 must be distinct. The guard checks both length === 58 and unique count === 58, because Base58's whole purpose is a 58-symbol alphabet (Bitcoin-style excludes ambiguous 0/O/I/l).","triggerScenarios":"Editing the alphabet argument so it no longer has 58 unique characters: fewer/more characters, duplicate characters, or an expanded alphabetic range (e.g. A-Z) that does not total 58.","commonSituations":"Customising the alphabet for a non-standard Base58 variant; accidental truncation when pasting; typing an alphabet with a repeated symbol.","solutions":["Use the default Bitcoin alphabet: 123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz.","Count the alphabet characters and ensure exactly 58 unique ones.","Use an alphabetic range expansion (e.g. 'A-Z') only if it expands to 58 unique characters."],"exampleFix":"// before: alphabet = \"12345...\" (truncated, <58 chars) -> throws\n// after:  alphabet = \"123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz\" -> 58 unique, passes","handlingStrategy":"validation","validationCode":"const set = new Set([...alphabet]);\nif (alphabet.length !== 58 || set.size !== 58) {\n  throw new Error(\"Base58 alphabet must have 58 unique chars\");\n}","typeGuard":"const isValidBase58Alphabet = a => a.length === 58 && new Set(a).size === 58;","tryCatchPattern":"try { toBase58(input, [alphabet]); }\ncatch (e) { if (/length 58/.test(e.message)) { alphabet = DEFAULT_BASE58; } else throw e; }","preventionTips":["Use the canonical 58-character Bitcoin alphabet unless you have a specific variant.","Validate length and uniqueness before encoding.","Avoid hand-typing alphabets; copy from a trusted source."],"tags":["base58","alphabet","validation","argument"],"backgroundTag":null,"analyzedSha":"4290ea753912378913b1f3f54e0fc5720afeda5d","analyzedAt":"2026-08-13T06:05:50.210Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}