0

I'm using that Javascript code to generate three things: private key, public key and public address (last 40 characters from public key):

const EC = require('elliptic').ec;
const ec = new EC('secp256k1');

const key = ec.genKeyPair();
const publicKey = key.getPublic('hex');
const privateKey = key.getPrivate('hex');

console.log();
console.log('Private key: ', privateKey);

console.log();
console.log('Public key (big): ', publicKey);

console.log();
console.log('Public key (shorten): ', publicKey.slice(-40));

Although that code generates a private key, when I import that private key to Metamask (or other Ethereum wallet) it creates a wallet with a different public address than that address previously logged in terminal.

I don't know why it is happening, although I have some guesses about the possibilities:

  • I could be missing some necessary step when converting from private key to public key (hashing it, maybe?)

  • or when I'm logging the private and public key on console, each time it's logging from a new instance of the key

Does anyone know why it's happening and how to solve it?

3
  • Every time you run the js code on the console, a new pair of keys will be generated. So probably your second reason is true
    – pbsh
    Commented Feb 5, 2022 at 4:04
  • I know that but each time I run the code I'm logging both keys together (private and public), so they should be the same.
    – Márcio
    Commented Feb 5, 2022 at 11:44
  • Solved it. The code was generating a valid public address for Bitcoin, not Ethereum. Both Bitcoin and Ethereum have the same process to create a private key, but in order to generate an Ethereum public address from it, it's necessary more complex steps, not presented in that code.
    – Márcio
    Commented Feb 9, 2022 at 0:08

1 Answer 1

0

Solved it. The code was generating a valid public address for Bitcoin, not Ethereum. Both Bitcoin and Ethereum have the same process to create a private key, but in order to generate an Ethereum public address from it, it's necessary more complex steps, not presented in that code.

2

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.