minimal-xec-wallet
Version:
A minimalist eCash (XEC) wallet npm library, for use in web apps. Supports eTokens.
68 lines (55 loc) ⢠2.44 kB
JavaScript
/*
Create a new XEC wallet with a randomly generated mnemonic.
This example shows how to create a new wallet and save it to wallet.json.
*/
const MinimalXECWallet = require('../../index')
const WalletHelper = require('../utils/wallet-helper')
async function createNewWallet () {
try {
console.log('š Creating new XEC wallet...\n')
// Check if wallet already exists
if (WalletHelper.walletExists()) {
console.log('ā ļø Wallet already exists!')
console.log(' Delete existing wallet first or use restore-from-mnemonic.js')
console.log(' Existing wallet path:', WalletHelper.getWalletPath())
// Show existing wallet info
const existingWallet = WalletHelper.loadWallet()
if (existingWallet) {
WalletHelper.displayWalletInfo(existingWallet)
}
return
}
// Create new wallet (no parameters = new random mnemonic)
const wallet = new MinimalXECWallet()
// Wait for wallet creation to complete
await wallet.walletInfoPromise
console.log('ā
New wallet created successfully!\n')
// Display the wallet information
console.log('š New Wallet Details:')
console.log('ā'.repeat(50))
console.log(`Mnemonic: ${wallet.walletInfo.mnemonic}`)
console.log(`XEC Address: ${wallet.walletInfo.xecAddress}`)
console.log(`HD Path: ${wallet.walletInfo.hdPath}`)
console.log('ā'.repeat(50))
// Save to wallet.json file
const saved = WalletHelper.saveWallet(wallet.walletInfo, 'New XEC Wallet')
if (saved) {
console.log('\nš Wallet ready for use!')
console.log(' You can now run other examples to interact with your wallet')
console.log(' Next steps:')
console.log(' ⢠Fund your wallet by sending XEC to:', wallet.walletInfo.xecAddress)
console.log(' ⢠Check balance: node examples/wallet-info/get-balance.js')
console.log(' ⢠View transactions: node examples/wallet-info/get-transactions.js')
}
// Security reminder
console.log('\nš SECURITY REMINDER:')
console.log(' ⢠Keep your mnemonic phrase safe and private')
console.log(' ⢠Anyone with your mnemonic can access your funds')
console.log(' ⢠Consider backing up your wallet: the mnemonic is in wallet.json')
} catch (err) {
console.error('ā Failed to create wallet:', err.message)
process.exit(1)
}
}
// Run the example
createNewWallet()