Search code examples
javascriptfrontendecdsa

Javascript: Generating ECDSA public key from private key


Is there any library that supports deriving the ecdsa public key from the private key for javascript (frontend)? (With the private key, we can generate the corresponding public key)

I studied the localethereum white paper, and I would like to implement the crypto layer.

It says:

AccountKeyIdentityPublic — Using the SECP‐256k1 curve, an ECDSA public key that corresponds to AccountKeyIdentityPrivate.

However, it seems that lots of libraries (1, 2) do not support this function.

Anyone can provide me some advise? Thanks!


Solution

  • You can do such thing with a library that support point multiplication. To get your public key, you just have to multiply a generation point G by your private key.

    For example with elliptic package :

    var EC = require('elliptic').ec;
    
    // Create and initialize EC context
    // (better do it once and reuse it)
    var ec = new EC('secp256k1');
    
    // Then generate the public point/key corresponding to your secret key.
    var pubPoint = ec.keyFromSecret(secret).getPublic();