Search code examples
gocryptographycertificatebitcoin

Use secp256k1 in Go


I'm trying to use the elliptic curve secp256k1 in Go with the library "crypto/x509". After the key pair generation, I obtain respectively the public key pubKey and the private key privKey. After that, I want to generate a certificate that include the public key, but before I want to store the private key in a .pem file:

keyDer, err := x509.MarshalECPrivateKey(privKey)
    if err != nil {
        log.Fatalf("Failed to serialize ECDSA key: %s\n", err)
    }

but when I try to marshal an EC private key into ASN.1, DER format and to compile the code, I receive an error that said:

Failed to serialize ECDSA key: x509: unknown elliptic curve

In this case it's necessary for me to work with that particular curve, so I cannot change to prime256v1 or ''similar curve''. Is there a solution that permits to add the support for secp256k1 in crypto/x509 library, or another way/suggestion?


Solution

  • There is no secp256k1 curve type in go.

    How did you created key-pairs?

    I faced this problem. In my case, I used the go-ethereum package to create this curve type. So, I used the same package's function to parse the key.

    So you have to use same package's specified function to parse the key-pair.

    This x509.MarshalECPrivateKey(privKey) will only helps to Marshal go's standard curve type.