1

So I am trying to get public key in hex formatting. In some documentation I found the following will give public key from the private key, but it is not formatted correctly as I can see:

publicKey := privateKey.Public()

// output is &{0xc0000aa810 111467676592638012114394423922557735151709477236068497041031680458777853727687 91044589275583075592054080804256995961966216084890183147914377743969771213580}


publicKeyECDSA, ok := publicKey.(*ecdsa.PublicKey)
if !ok {
    log.Fatal("error casting public key to ECDSA")
}

// output looks the same here: &{0xc0000aa810 111467676592638012114394423922557735151709477236068497041031680458777853727687 91044589275583075592054080804256995961966216084890183147914377743969771213580}

publicKeyBytes := crypto.FromECDSAPub(publicKeyECDSA) 

// output: [4 246 112 120 28 51 214 136 11 9 156 225 31 55 248 83 107 177 101 138 103 54 81 101 66 67 247 150 1 88 0 147 199 201 73 105 81 71 44 213 141 125 198 50 50 47 21 2 25 193 203 214 105 129 64 151 83 64 247 101 247 190 42 83 12]

But what I am trying to get is the hex formatted of this. It does look like privateKey,Public() gives this, but added things with it. Is there a way to get just the hex representation of the public key so I can store and use later in the code?

  • Try to convert the publickKeyBytes to a hex string and omit the first byte (the 4). This should give you a hex string which is 128 characters long (64 bytes). Like this: hex := fmt.Sprintf("%x", publicKeyBytes[1:]) – Thorkil Værge Jan 8 at 19:55
1

The output is 65 bytes long and starts with 0x04. This indicates to me that this is a DER encoding of the public key (which is what Bitcoin and all its fork uses). Ethereum does not DER encode its public keys, public keys in Ethereum are only 64 bytes long.

Try to convert the publicKeyBytes to a hex string and omit the first byte (the 4). This should give you a hex string which is 128 characters long (64 bytes). Like this:

 hex := fmt.Sprintf("%x", publicKeyBytes[1:]).

Ethereum may expect this public key hex string to start with "0x". In that case:

hex := fmt.Sprintf("0x%x", publicKeyBytes[1:]).

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.