Created
June 12, 2024 13:06
-
-
Save felipekm/ba7953ce7f55e059558901d37714ec64 to your computer and use it in GitHub Desktop.
BTC wallet creation
This file contains hidden or 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
const bitcoin = require('bitcoinjs-lib'); | |
const bip39 = require('bip39'); | |
const bip32 = require('bip32'); | |
// Generate a random mnemonic (12 words) | |
const mnemonic = bip39.generateMnemonic(); | |
console.log('Mnemonic:', mnemonic); | |
// Generate seed from mnemonic | |
const seed = bip39.mnemonicToSeedSync(mnemonic); | |
// Create a root node from seed | |
const root = bip32.fromSeed(seed); | |
// Bitcoin mainnet | |
const network = bitcoin.networks.bitcoin; | |
// Derive a node based on BIP44 path | |
const path = "m/44'/0'/0'/0/0"; | |
const child = root.derivePath(path); | |
// Generate the wallet address | |
const { address } = bitcoin.payments.p2pkh({ pubkey: child.publicKey, network }); | |
console.log('Address:', address); | |
console.log('Private Key:', child.toWIF()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment