eth-public-address-from-private
Version:
Get Ethereum public address from private key using ethers.js
18 lines (15 loc) • 526 B
JavaScript
const { Wallet } = require("ethers");
/**
* Takes a private key and returns the associated Ethereum public address.
* @param {string} privateKey - The private key of the Ethereum wallet (0x... format)
* @returns {string} The public address (0x... format)
*/
function getPublicAddress(privateKey) {
try {
const wallet = new Wallet(privateKey);
return wallet.address;
} catch (error) {
throw new Error("Invalid private key");
}
}
module.exports = { getPublicAddress };