How to generate an Ethereum address using JavaScript ?
Generating an Ethereum address might be easier than you thought.
Using the ethereum-cryptography library which has been audited, this post provides a front-end demo with a walkthrough of the JavaScript code.
Front-end demo
Front-end demo: here
Github repository: here
Script
A script is also provided to generate ETH addresses in console.
git clone https://github.com/0x2024antonio/ethereum-cryptography-learning.git
Once you clone the github repository, you can also run the script by
cd ethereum-cryptography-learning/eth-address-generation-react
npm install
npm run generate
If you successfully run the script, you will see the following results in the console:
[
{
privateKey: '45748b3c165e6531b4bee389ff34f33cf255f8e41827f91de21df44d68c78ce3',
publicKey: '0443e2cd86068a8cb59d1fb71405f37aaff5dc3d9f446ef97a678276a32a75530c1c4a9299e8978617f5ca577d9d4ba21079228adccfe7658c1fdd0e8b33c57d17',
ethAddress: '0x7e50572B8F6fCc21335158743B24380d08f115C4'
},
{
privateKey: '40b87f22023d103db37c68b8e653c08b6a3f63fc098672ef49624084763c3f42',
publicKey: '042d3513a3476aa1f56a199d2c6d311d3dc21c4b0b1925412bd74342c8e9e247d43681b3408e705ca117355ae9c18bfb4c17ca1048000154350f9d04c560ee7fb5',
ethAddress: '0xe1bc5489ab4782c55a313AEd9f30d049E2356aE1'
},
{
privateKey: '2c2231ec99039923e38ed2e11123c2b1c89a68e18f8be1e4b7f2abbbf3c5a899',
publicKey: '0444c9f99edca4664ebc837406dd5239d99e3d8890b45d2bd40c5e6ce7cfe3b38cd678cff61461f6abbb0a2b7927e2552a23293e047614397ac5ea518b91faa5c5',
ethAddress: '0x5B22380cFA33EA7F3B57a997A7BF01094B9BE641'
}
]
Step-by-step code walkthrough
Step 0: Import libraries in ethereum-cryptography
const { secp256k1 } = require("ethereum-cryptography/secp256k1");
const { keccak256 } = require("ethereum-cryptography/keccak");
const { toHex, utf8ToBytes } = require("ethereum-cryptography/utils");
Step 1: Generate a private key randomly
const getPrivateKey = () => {
const privateKey = secp256k1.utils.randomPrivateKey();
return {
privateKey: privateKey,
privateKeyHex: toHex(privateKey),
};
};
Step 2: Generate a public key from the private key
const getPublicKey = (privateKey, isCompressed = false) => {
const publicKey = secp256k1.getPublicKey(privateKey, isCompressed);
return {
publicKey: publicKey,
publicKeyHex: toHex(publicKey),
};
};
Step 3: Get an ethereum address without checksum by hashing the public key and taking the last 20 bytes
const ethAddressNoCheckSum = toHex(
keccak256(publicKey.slice(1)).slice(-20)
).toLowerCase();
Step 4: Get an ethereum address by applying a checksum algorithm
const ethAddressNoCheckSumHashHex = toHex(
keccak256(utf8ToBytes(ethAddressNoCheckSum))
);
const tempArray = [];
const isNumberChar = (char) => {
return /^[0123456789]$/.test(char);
};
const isHexChar = (char) => {
return /^[abcdef]$/.test(char);
};
const isTargetChar = (char) => {
return /^[89abcdef]$/.test(char);
};
[...ethAddressNoCheckSum].forEach((char, index) => {
if (isNumberChar(char)) {
tempArray.push(char);
} else if (isHexChar(char.toLowerCase())) {
tempArray.push(
isTargetChar(ethAddressNoCheckSumHashHex[index])
? char.toUpperCase()
: char.toLowerCase()
);
} else {
console.log("invalid characters being input");
}
});
Step 5: Add the 0x prefix
const ethAddress = "0x" + tempArray.join("");