Skip to content

Instantly share code, notes, and snippets.

@giuseppe998e
Last active December 17, 2021 20:11
Show Gist options
  • Save giuseppe998e/adc2349f0179273aec54d33f26e82e07 to your computer and use it in GitHub Desktop.
Save giuseppe998e/adc2349f0179273aec54d33f26e82e07 to your computer and use it in GitHub Desktop.
Javascript function that generates an Ethereum wallet (address, pubKey and privKey) using a random private key
/**
* MIT License
*
* Copyright (c) 2021 Giuseppe Eletto <[email protected]>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
/*
// Browser version:
$ browserify -p tinyify ethwallet-generator.js --s genEthereumWallet | uglifyjs > ethwallet-generator.bundle.js
// Browser version usage:
<script src="ethwallet-generator.bundle.js"></script>
<script>
const wallet = genEthereumWallet()
console.log(wallet)
</script>
*/
/**
* Create a wallet from a random private key
* @returns {{address: String, pubKey: String, privKey: String}}
*/
module.exports = () => {
const { Buffer } = require('safe-buffer')
const randomBytes = require('randombytes')
const { privateKeyVerify, publicKeyCreate } = require('secp256k1')
const keccak = require('keccak')
/**
* Generates a random private key
* @returns Buffer
*/
const genPrivateKey = () => {
for (let privKey; (privKey = randomBytes(32)); )
if (privateKeyVerify(privKey)) return privKey
}
/**
* Generates the public key for the given private key
* @param Buffer privKey
* @returns Buffer
*/
const genPublicKey = (privKey) => {
const pubKey = publicKeyCreate(privKey, false).slice(1)
return Buffer.from(pubKey)
}
/**
* Generates the address based on the given public key
* @param Buffer pubKey
* @returns String
*/
const genAddress = (pubKey) => {
const address = keccak('keccak256').update(pubKey).digest('hex').slice(24)
const hash = keccak('keccak256').update(address).digest('hex')
let result = ''
for (let i = 0; i < address.length; i++) {
result += parseInt(hash[i], 16) >= 0x08 ? address[i].toUpperCase() : address[i]
}
return result
}
// Private & Public keys
const privKey = genPrivateKey()
const pubKey = genPublicKey(privKey)
// Address
const address = genAddress(pubKey)
// Return Wallet object
return {
address: `0x${address}`,
pubKey: pubKey.toString('hex'),
privKey: privKey.toString('hex'),
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment