1

I am trying to use the Bitcoin ECDSA Library to generate an Ethereum address from a private key. I took the private key from my Metamask wallet then used the following code to generate the public key.

secp256k1_pubkey pubkey;
unsigned char seckey[32];
// initialize secret key with value from metamask
secp256k1_context* ctx = secp256k1_context_create(SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY);
secp256k1_context_randomize(ctx, randomize);
secp256k1_ec_pubkey_create(ctx, &pubkey, seckey);
// use pubkey

I then run the public key through Keccak-256 (with Input type = hex) and take the last 20 bytes as stated in Appendix F of the yellow paper.

However I get a different value than the public address I can see in my Metamask wallet. What am I doing wrong here?

1 Answer 1

1

You must serialize the public key before use like so:

unsigned char uncompressed_pubkey[65];
secp256k1_ec_pubkey_serialize(ctx, uncompressed_pubkey, &len, &pubkey, SECP256K1_EC_UNCOMPRESSED);

Now take the last 64 bytes of uncompressed_pubkey and run Keccak-256 on it as before.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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