I need to generate a key pair that can be used to write the a besu (ethereum) private ledger. I wrote the following code but the architect says this will probably not work with ethereum.

The examples he gave me do not compile with the latest packages, seems overly complex, or make no sense to me in that they don't do anything different from my simple code.

Here's my code:

import * as keypair from 'keypair';
import * as forge from 'node-forge';
const pair = keypair();
const publicKey = forge.pki.publicKeyFromPem(pair.public);
const privateKey = forge.pki.privateKeyFromPem(pair.private);

Not making excuses, I don't know where to begin or what to even search on. What do I need to generate to have acceptable keys for use with ethereum?

  • 1
    why are you not using web3.utils package from web3js library?
    – Yahya
    Nov 13, 2019 at 16:17

1 Answer 1


Try this:

const crypto = require("crypto");
const util = require("ethereumjs-util");

const privateKey = "0x" + crypto.randomBytes(32).toString("hex");
const publicKey = "0x" + util.privateToPublic(privateKey).toString("hex");

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.