Last active
March 18, 2024 14:46
-
-
Save MicahZoltu/ebea04ad04e91ec09942ff3ddc0c1c96 to your computer and use it in GitHub Desktop.
Generate Ethereum Mnemonic & Address
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
mkdir temp | |
cd temp | |
cat >index.mjs <<'EOF' | |
import { ethereum, mnemonic, secp256k1, hdWallet } from '@zoltu/ethereum-crypto' | |
// necessary so @peculiar/webcrypto looks like browser WebCrypto, which @zoltu/ethereum-crypto needs | |
import webcrypto from '@peculiar/webcrypto' | |
globalThis.crypto = new webcrypto.Crypto() | |
export async function generateAccount() { | |
const words = await mnemonic.generateRandom(256) | |
const seed = await mnemonic.toSeed(words) | |
const privateKey = await hdWallet.privateKeyFromSeed(seed, `m/44'/60'/0'/0/0`) | |
const publicKey = await secp256k1.privateKeyToPublicKey(privateKey) | |
const address = await ethereum.publicKeyToAddress(publicKey) | |
const addressString = await ethereum.addressToChecksummedString(address) | |
console.log(words.join(' ')) | |
console.log(privateKey.toString(16).padStart(32, '0')) | |
console.log(addressString) | |
} | |
generateAccount() | |
.then(() => process.exit()) | |
.catch(error => { | |
console.log('An error occurred.'); | |
console.log(error); | |
process.exit(); | |
}); | |
EOF | |
cat >package.json <<'EOF' | |
{ | |
"main": "index.mjs", | |
"type": "module", | |
"dependencies": { | |
"@peculiar/webcrypto": "1.1.6", | |
"@zoltu/ethereum-crypto": "2.1.4" | |
} | |
} | |
EOF | |
npm install | |
node index.mjs |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment