Search code examples
ethereumweb3js

Generating ethereum address from ECDSA Public Key


I am using node-webcrypto-p11 and generating keys via following code

keys = crypto.subtle.generateKey({name: "ECDSA", namedCurve: "K-256"}, false, ["sign", "verify"]);

what is the eth address.

('0x' + keccak('keccak256').update(key).digest().slice(-20).toString('hex');)

I am finding eth address like this is this correct?


Solution

  • It seems right to me. You can test your solution with this little script

    const wallet = require('ethereumjs-wallet');
    
    var account = wallet.fromPrivateKey(Buffer.from('348ce564d427a3311b6536bbcff9390d69395b06ed6c486954e971d960fe8709', 'hex'));
    console.log(account.getChecksumAddressString());
    

    or with web3:

    var account = web3.eth.accounts.privateKeyToAccount('0x348ce564d427a3311b6536bbcff9390d69395b06ed6c486954e971d960fe8709');
    console.log(account.address);