{"record":{"id":"a08e6a1d953f37bc","repo":"ipfs/kubo","slug":"converting-libp2p-private-key-to-std-go-key-w","errorCode":null,"errorMessage":"converting libp2p private key to std Go key: %w","messagePattern":"converting libp2p private key to std Go key: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"core/commands/keystore.go","lineNumber":231,"sourceCode":"\t\t// (this makes export work when ipfs daemon is already running)\n\t\tksp := filepath.Join(cfgRoot, \"keystore\")\n\t\tks, err := keystore.NewFSKeystore(ksp)\n\t\tif err != nil {\n\t\t\treturn err\n\t\t}\n\n\t\tsk, err := ks.Get(name)\n\t\tif err != nil {\n\t\t\treturn fmt.Errorf(\"key with name '%s' doesn't exist\", name)\n\t\t}\n\n\t\texportFormat, _ := req.Options[keyFormatOptionName].(string)\n\t\tvar formattedKey []byte\n\t\tswitch exportFormat {\n\t\tcase keyFormatPemCleartextOption:\n\t\t\tstdKey, err := crypto.PrivKeyToStdKey(sk)\n\t\t\tif err != nil {\n\t\t\t\treturn fmt.Errorf(\"converting libp2p private key to std Go key: %w\", err)\n\t\t\t}\n\t\t\t// For some reason the ed25519.PrivateKey does not use pointer\n\t\t\t// receivers, so we need to convert it for MarshalPKCS8PrivateKey.\n\t\t\t// (We should probably change this upstream in PrivKeyToStdKey).\n\t\t\tif ed25519KeyPointer, ok := stdKey.(*ed25519.PrivateKey); ok {\n\t\t\t\tstdKey = *ed25519KeyPointer\n\t\t\t}\n\t\t\tif secpKey, ok := stdKey.(*crypto.Secp256k1PrivateKey); ok {\n\t\t\t\t// crypto/x509 does not support the secp256k1 curve\n\t\t\t\tformattedKey, err = marshalSecp256k1PrivateKey(secpKey)\n\t\t\t} else {\n\t\t\t\t// This function supports a restricted list of public key algorithms,\n\t\t\t\t// but we generate and use only the RSA and ed25519 types that are on that list.\n\t\t\t\tformattedKey, err = x509.MarshalPKCS8PrivateKey(stdKey)\n\t\t\t}\n\t\t\tif err != nil {\n\t\t\t\treturn fmt.Errorf(\"marshalling key to PKCS8 format: %w\", err)\n\t\t\t}","sourceCodeStart":213,"sourceCodeEnd":249,"githubUrl":"https://github.com/ipfs/kubo/blob/329838acdfafae224582930457efe80aa217afc0/core/commands/keystore.go#L213-L249","documentation":"Failure in the PEM export path of `ipfs key export`: crypto.PrivKeyToStdKey could not convert the stored libp2p private key into a standard Go key type. It fires when the stored key is of a type the converter does not support (the keystore normally only holds rsa, ed25519, and secp256k1), or when the serialized key bytes are malformed/corrupted.","triggerScenarios":"Thrown at core/commands/keystore.go:231 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Export with --format=libp2p-protobuf-cleartext, which serializes the key without the std-conversion step","Regenerate the key with ipfs key gen --type=rsa|ed25519|secp256k1 if it is corrupted or of an exotic type","Report upstream if a freshly generated supported key type hits this"],"exampleFix":null,"handlingStrategy":"fallback","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"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"}