{"record":{"id":"f4972dfe5e475ce6","repo":"denoland/deno","slug":"err-crypto-ecdh-invalid-format","errorCode":"ERR_CRYPTO_ECDH_INVALID_FORMAT","errorMessage":"Invalid ECDH format: ${format}","messagePattern":"Invalid ECDH format: (.+?)","errorType":"exception","errorClass":"ERR_CRYPTO_ECDH_INVALID_FORMAT","httpStatus":null,"severity":"error","filePath":"ext/node/polyfills/internal/crypto/diffiehellman.ts","lineNumber":1356,"sourceCode":"  getPublicKey(encoding?: any): Buffer | string {\n    return this.#diffiehellman.getPublicKey(encoding);\n  }\n}\n\nDiffieHellmanGroup.prototype = DiffieHellmanGroupImpl.prototype;\nDiffieHellmanGroup.prototype.constructor = DiffieHellmanGroup;\n\nfunction ECDH(curve: string) {\n  return new ECDHImpl(curve);\n}\n\nfunction validateEcdhFormat(format: any): void {\n  if (\n    format !== \"compressed\" &&\n    format !== \"uncompressed\" &&\n    format !== \"hybrid\"\n  ) {\n    throw new ERR_CRYPTO_ECDH_INVALID_FORMAT(String(format));\n  }\n}\n\nfunction ecdhEncode(\n  buffer: Buffer,\n  encoding?: any,\n): Buffer | string {\n  if (encoding === undefined || encoding === \"buffer\") {\n    return buffer;\n  }\n  // deno-lint-ignore deno-internal/prefer-primordials -- Buffer.prototype.toString(encoding) has no primordial\n  return buffer.toString(encoding);\n}\n\nclass ECDHImpl {\n  #curve: any; // the selected curve\n  #privbuf: Buffer | null = null; // the private key\n  #pubbuf: Buffer | null = null; // the public key","sourceCodeStart":1338,"sourceCodeEnd":1374,"githubUrl":"https://github.com/denoland/deno/blob/9ad36f7a2cce60488e6ec52283efb32efddaf93a/ext/node/polyfills/internal/crypto/diffiehellman.ts#L1338-L1374","documentation":"validateEcdhFormat (diffiehellman.ts:1356) validates the point-format parameter used by ECDH's getPublicKey and convertKey. Only 'compressed', 'uncompressed' and 'hybrid' are accepted; anything else throws ERR_CRYPTO_ECDH_INVALID_FORMAT naming the rejected value. Output encodings ('base64', 'hex') are a different parameter, which is the common confusion here.","triggerScenarios":"ecdh.getPublicKey(null, 'base64') — an output encoding passed in the format slot; convertKey(key, curve, inEnc, outEnc, 'hex'); capitalized variants like 'Compressed'; format names invented from WebCrypto ('raw', 'spki').","commonSituations":"Argument-order mix-ups because getPublicKey(encoding, format) takes the encoding first; passing every string option into the format position; copy-paste from APIs with different parameter orders.","solutions":["Remember the order: getPublicKey(encoding, format) — encoding first, format second","Use only 'compressed' | 'uncompressed' | 'hybrid' for the format parameter","If you just want text output, pass the encoding and omit the format: getPublicKey('base64')"],"exampleFix":"// before\necdh.getPublicKey('base64', 'hex'); // 'hex' lands in format -> ERR_CRYPTO_ECDH_INVALID_FORMAT\n\n// after\necdh.getPublicKey('base64'); // encoding only, uncompressed point\n// or explicit point format:\necdh.getPublicKey(null, 'compressed').toString('base64');","handlingStrategy":"validation","validationCode":"const ECDH_FORMATS = new Set(['compressed', 'uncompressed', 'hybrid']);\nfunction assertPointFormat(fmt) {\n  if (fmt !== undefined && !ECDH_FORMATS.has(fmt)) throw new TypeError(`invalid ECDH format: ${fmt}`);\n}","typeGuard":"const isEcdhPointFormat = (f) => f === undefined || f === 'compressed' || f === 'uncompressed' || f === 'hybrid';","tryCatchPattern":"try { key = ecdh.getPublicKey(null, fmt); } catch (e) { if (e?.code === 'ERR_CRYPTO_ECDH_INVALID_FORMAT') { key = ecdh.getPublicKey(null, 'uncompressed'); } else throw e; }","preventionTips":["Remember getPublicKey(encoding, format) — the encoding comes first","Define a union type 'compressed' | 'uncompressed' | 'hybrid' for the format parameter"],"tags":["crypto","ecdh","validation","node-compat"],"backgroundTag":"invalid-format-option","analyzedSha":"9ad36f7a2cce60488e6ec52283efb32efddaf93a","analyzedAt":"2026-08-20T13:07:44.778Z","schemaVersion":2},"datasetVersion":"2026-08-21T13:17:26.733Z"}