Invalid format for ECDH public key
Production Risk
Low — caught immediately at call site.
Thrown when an invalid format argument is passed to ecdh.getPublicKey() or ecdh.setPublicKey(). The valid format values are "compressed", "uncompressed", and "hybrid". Any other string causes this error.
- 1Passing an unrecognised format string to getPublicKey()
- 2Typo in the format argument (e.g. "compress" instead of "compressed")
- 3Passing undefined or null as the format argument
Triggered when getPublicKey() or setPublicKey() receives an invalid format argument.
const { createECDH } = require('crypto');
const ecdh = createECDH('prime256v1');
ecdh.generateKeys();
ecdh.getPublicKey(null, 'compact'); // 'compact' is not a valid formatexpected output
Error [ERR_CRYPTO_ECDH_INVALID_FORMAT]: Invalid ECDH format: compact
Fix
Use a valid format string
WHEN When calling getPublicKey() or setPublicKey()
const pub = ecdh.getPublicKey(null, 'compressed'); // or const pub2 = ecdh.getPublicKey(null, 'uncompressed');
Why this works
Valid format values are "compressed", "uncompressed", and "hybrid"; using one satisfies the validation.
const { createECDH } = require('crypto');
const ecdh = createECDH('prime256v1');
ecdh.generateKeys();
ecdh.getPublicKey(null, 'compact'); // 'compact' is not a valid format // this triggers ERR_CRYPTO_ECDH_INVALID_FORMATtry {
// operation that may throw ERR_CRYPTO_ECDH_INVALID_FORMAT
riskyOperation()
} catch (err) {
if (err.code === 'ERR_CRYPTO_ECDH_INVALID_FORMAT') {
console.error('ERR_CRYPTO_ECDH_INVALID_FORMAT:', err.message)
} else {
throw err
}
}// Validate inputs before calling the operation
function safe_err_crypto_ecdh_invalid_format(...args) {
// validate args here
return performOperation(...args)
}✕ Pass arbitrary strings as the format argument
Only three exact string values are accepted; anything else throws.
Node.js Error Codes Documentation
Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev