Skip to content

Instantly share code, notes, and snippets.

@vinarmani
Last active December 29, 2023 13:19
Show Gist options
  • Save vinarmani/0d57c91ff638b7526e991eaf7fb45974 to your computer and use it in GitHub Desktop.
Save vinarmani/0d57c91ff638b7526e991eaf7fb45974 to your computer and use it in GitHub Desktop.
Generate Ethereum and Bitcoin addresses using the number 1 as a private key
import * as secp from '@noble/secp256k1'
import ripemd160 from 'ripemd160-js/ripemd160.mjs'
import * as sha3 from 'js-sha3'
import { createHash } from 'node:crypto'
import { binary_to_base58 } from 'base58-js'
// Generate public key for private key of number 1 (or any number between 1 and ~10^77)
const privateKey = Buffer.alloc(32)
privateKey.writeUInt8(0x1, 31)
const publicKey = secp.getPublicKey(privateKey, false)
console.log("Private Key:", privateKey.toString('hex'));
console.log("Public Key:", Buffer.from(publicKey).toString('hex'));
// Use public key (without first byte) to generate Ethereum Address
const ethAddress = sha3.default.keccak256(publicKey.subarray(1)).slice(-40)
console.log('Ethereum Address:', `0x${ethAddress}`)
// Use public key to generate Bitcoin address
// First apply SHA256 to public key
const pubKeyHash = createHash('sha256').update(publicKey).digest()
// Then apply RIPE-MD160 to hashed public key
ripemd160(pubKeyHash).then(payload => {
// Version byte
const versionByte = Buffer.alloc(1, 0x00)
// Version + payload
const versionPayload = Buffer.concat([
versionByte,
Buffer.from(payload)
])
// Make checksum using double SHA-256 of version+payload
const singleSha256 = createHash('sha256').update(versionPayload).digest()
const doubleSha256 = createHash('sha256').update(singleSha256).digest()
// Concatenate first 4 bytes of checksum to version+payload
const addressCheck = Buffer.concat([
versionPayload,
doubleSha256.subarray(0, 4)
])
console.log("Bitcoin Address", binary_to_base58(addressCheck))
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment