Skip to content

Instantly share code, notes, and snippets.

@ac12644
Created December 18, 2023 09:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ac12644/a183b82a7f7fec132cb07dbccfdd31e4 to your computer and use it in GitHub Desktop.
Save ac12644/a183b82a7f7fec132cb07dbccfdd31e4 to your computer and use it in GitHub Desktop.
// Importing the crypto module for cryptographic functions
const crypto = require('crypto');
// Importing the bs58check module for Base58Check encoding
const bs58check = require('bs58check');
/**
* Generates a public key using elliptic curve cryptography (ECC).
* ECC uses smaller keys compared to non-ECC cryptography to provide equivalent security.
*/
const generatePublicKey = () => {
// Generating a key pair using Bitcoin's secp256k1 elliptic curve.
const { privateKey } = crypto.generateKeyPairSync('ec', {
namedCurve: 'secp256k1', // Using secp256k1 curve.
publicKeyEncoding: { type: 'spki', format: 'der' }, // Encoding the public key in DER format.
privateKeyEncoding: { type: 'pkcs8', format: 'der' } // Encoding the private key in DER format.
});
// Deriving the public key from the private key.
const publicKey = crypto.createPublicKey({ key: privateKey, format: 'der', type: 'pkcs8' });
// Exporting the public key in DER format.
return publicKey.export({ type: 'spki', format: 'der' });
};
/**
* Hashes the public key first with SHA-256 and then with RIPEMD-160 to create a public key hash.
* This two-step hashing process is used for additional security.
*/
const hashPublicKey = (publicKey) => {
// Hashing the public key using SHA-256.
const sha256Hash = crypto.createHash('sha256').update(publicKey).digest();
// Hashing the SHA-256 output using RIPEMD-160.
return crypto.createHash('ripemd160').update(sha256Hash).digest();
};
/**
* Creates a Bitcoin address from the hashed public key.
* The function supports different network bytes for mainnet and testnet addresses.
*/
const createBitcoinAddress = (hashedPublicKey, networkByte = 0x00) => {
// Creating a buffer for the network version byte (0x00 for mainnet, 0x6f for testnet).
const versionBuffer = Buffer.alloc(1, networkByte);
// Concatenating the version byte with the hashed public key.
const payload = Buffer.concat([versionBuffer, hashedPublicKey]);
// Encoding the payload using Base58Check.
return bs58check.encode(payload);
};
// Generating the public key.
const publicKey = generatePublicKey();
// Hashing the public key.
const hashedPublicKey = hashPublicKey(publicKey);
// Creating a Bitcoin address for mainnet.
const bitcoinAddressMainnet = createBitcoinAddress(hashedPublicKey, 0x00);
// Creating a Bitcoin address for testnet.
const bitcoinAddressTestnet = createBitcoinAddress(hashedPublicKey, 0x6f);
// Logging the generated addresses to the console.
console.log(`Generated Bitcoin Address (Mainnet): ${bitcoinAddressMainnet}`);
console.log(`Generated Bitcoin Address (Testnet): ${bitcoinAddressTestnet}`);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment