{"record":{"id":"b2b2f529f53afaa8","repo":"ipfs/kubo","slug":"converting-std-go-key-to-libp2p-key-w","errorCode":null,"errorMessage":"converting std Go key to libp2p key: %w","messagePattern":"converting std Go key to libp2p key: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"core/commands/keystore.go","lineNumber":511,"sourceCode":"\n\t\t\tif pemBlock.Type != \"PRIVATE KEY\" {\n\t\t\t\treturn fmt.Errorf(\"expected PRIVATE KEY type in PEM block but got: %s\", pemBlock.Type)\n\t\t\t}\n\n\t\t\tstdKey, err := parsePKCS8PrivateKey(pemBlock.Bytes)\n\t\t\tif err != nil {\n\t\t\t\treturn fmt.Errorf(\"parsing PKCS8 format: %w\", err)\n\t\t\t}\n\n\t\t\t// In case ed25519.PrivateKey is returned we need the pointer for\n\t\t\t// conversion to libp2p (see export command for more details).\n\t\t\tif ed25519KeyPointer, ok := stdKey.(ed25519.PrivateKey); ok {\n\t\t\t\tstdKey = &ed25519KeyPointer\n\t\t\t}\n\n\t\t\tsk, _, err = crypto.KeyPairFromStdKey(stdKey)\n\t\t\tif err != nil {\n\t\t\t\treturn fmt.Errorf(\"converting std Go key to libp2p key: %w\", err)\n\t\t\t}\n\t\tcase keyFormatLibp2pCleartextOption:\n\t\t\tsk, err = crypto.UnmarshalPrivateKey(data)\n\t\t\tif err != nil {\n\t\t\t\t// check if data is PEM, if so, provide user with hint\n\t\t\t\tpemBlock, _ := pem.Decode(data)\n\t\t\t\tif pemBlock != nil {\n\t\t\t\t\treturn fmt.Errorf(\"unexpected PEM block for format=%s: try again with format=%s\", keyFormatLibp2pCleartextOption, keyFormatPemCleartextOption)\n\t\t\t\t}\n\t\t\t\treturn fmt.Errorf(\"unable to unmarshall format=%s: %w\", keyFormatLibp2pCleartextOption, err)\n\t\t\t}\n\n\t\tdefault:\n\t\t\treturn fmt.Errorf(\"unrecognized import format: %s\", importFormat)\n\t\t}\n\n\t\t// We only allow importing keys of the same type we generate (see list in\n\t\t// https://github.com/ipfs/interface-go-ipfs-core/blob/1c3d8fc/options/key.go#L58-L60),","sourceCodeStart":493,"sourceCodeEnd":529,"githubUrl":"https://github.com/ipfs/kubo/blob/329838acdfafae224582930457efe80aa217afc0/core/commands/keystore.go#L493-L529","documentation":"The PKCS8 key parsed successfully but go-libp2p's crypto.KeyPairFromStdKey could not convert the standard Go key into a libp2p private key. This happens for key types libp2p does not support (e.g. ECDSA/unknown types from PKCS8) or an internal conversion failure.","triggerScenarios":"`ipfs key import name -f pem-pkcs8-cleartext` with a PKCS8 ECDSA (or other unsupported-algorithm) key; KeyPairFromStdKey only maps *rsa.PrivateKey, ed25519.PrivateKey(*), and ecdsa it rejects/errors on.","commonSituations":"Importing an openssl EC key (`openssl genpkey -algorithm EC ...`) or a key type produced by another stack that Go parses but libp2p cannot represent.","solutions":["Check the key algorithm with `openssl pkey -in key.pem -noout -text`; if it is EC or another unsupported type, generate an Ed25519 or RSA key instead","Regenerate: `openssl genpkey -algorithm ED25519 -out key.pem` and re-import","If the key must be kept, re-derive an RSA/Ed25519 key you control and use it for IPNS naming"],"exampleFix":"// before\n$ openssl genpkey -algorithm EC -pkeyopt ec_paramgen_curve:P-256 -out key.pem\n$ ipfs key import mykey -f pem-pkcs8-cleartext key.pem\n// after\n$ openssl genpkey -algorithm ED25519 -out key.pem\n$ ipfs key import mykey -f pem-pkcs8-cleartext key.pem","handlingStrategy":"type-guard","validationCode":"block, _ := pem.Decode(data)\n_ = block\n// pre-check algorithm: only RSA and Ed25519 are convertible to libp2p\ntxt, _ := exec.Command(\"openssl\", \"pkey\", \"-in\", \"key.pem\", \"-noout\", \"-text\").Output()\nif bytes.Contains(txt, []byte(\"ED25519\")) || bytes.Contains(txt, []byte(\"Private-Key: (\")) && !bytes.Contains(txt, []byte(\"ASN1 OID\")) {\n    // likely RSA or Ed25519: proceed\n}","typeGuard":"func isLibp2pConvertible(stdKey interface{}) bool {\n    switch k := stdKey.(type) {\n    case *rsa.PrivateKey:\n        return true\n    case ed25519.PrivateKey, *ed25519.PrivateKey:\n        return true\n    default:\n        _ = k\n        return false\n    }\n}","tryCatchPattern":null,"preventionTips":["Use Ed25519 or RSA keys for IPFS/IPNS imports","Check `openssl pkey -text` output to confirm the algorithm before importing","Remember ECDSA parses in Go but is not a libp2p-supported identity key"],"tags":["keystore","libp2p","key-conversion","key-import"],"backgroundTag":"unsupported-key-type","analyzedSha":"329838acdfafae224582930457efe80aa217afc0","analyzedAt":"2026-09-03T18:30:52.135Z","contentChangedAt":"2026-09-03T18:30:52.135Z","schemaVersion":2},"datasetVersion":"2026-09-11T00:17:11.886Z"}