Skip to content

Instantly share code, notes, and snippets.

@MohamadFazuan
Last active May 18, 2022 01:29
Show Gist options
  • Save MohamadFazuan/176da0cca47ca7e32d864ef023c4cf45 to your computer and use it in GitHub Desktop.
Save MohamadFazuan/176da0cca47ca7e32d864ef023c4cf45 to your computer and use it in GitHub Desktop.
Create mulitple wallet in single mnemonic passphrase
var bip39 = require('bip39');
var bip32 = require('bip32');
var web3sol = require('@solana/web3.js');
var ethers = require('ethers');
const bitcoin = require('bitcoinjs-lib');
const { base58 } = require('ethers/lib/utils');
const mneamonic = bip39.generateMnemonic();
console.log(mneamonic);
const seed = bip39.mnemonicToSeedSync(mneamonic).slice(0, 32);
// console.log(seed); // <Buffer 62 23 97 07 fe b9 12 14 0d f7 c6 be 26 fd 2e 4d 14 f9 2f bd df 63 73 5e 47 53 24 23 1a 8a 04 d6 73 6c f1 8d aa 0b 55 27 da 00 a4 e8 23 00 27 d9 95 bb ... 14 more bytes>
// Solana
const network_solana = web3sol.clusterApiUrl("devnet");
const newAccount_solana = web3sol.Keypair.fromSeed(seed, network_solana);
console.log(`\nSolana Public Key: ${newAccount_solana.publicKey}`);
console.log(`Solana Private Key: ${base58.encode(newAccount_solana.secretKey)}`);
// Bitcoin
const network_bitcoin = bitcoin.networks.testnet;
const path_btc = `m/44'/1'/0'/0`;
let root = bip32.fromSeed(seed, network_bitcoin);
let newAccount_bitcoin = root.derivePath(path_btc);
let node = newAccount_bitcoin.derive(0).derive(0);
let btcAddress = bitcoin.payments.p2pkh({
pubkey: node.publicKey,
network: network_bitcoin,
}).address;
console.log(`\nBitcoin Address: ${btcAddress}`);
console.log(`Bitcoin Private key: ${node.toWIF()}`);
// Ethereum
const path_eth = `m/44'/60'/0'/0`;
const wallet = ethers.Wallet.fromMnemonic(mneamonic, path_eth);
console.log(`\nEthereum Address: ${wallet.address.toString()}`);
console.log(`Ethereum Private Key: ${wallet.privateKey.toString()}`);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment