minimal-xec-wallet
Version:
A minimalist eCash (XEC) wallet npm library, for use in web apps. Supports eTokens.
115 lines (96 loc) ⢠5.3 kB
JavaScript
/*
Export an XEC wallet's private key to WIF (Wallet Import Format).
This example shows how to export private keys in different formats.
*/
const MinimalXECWallet = require('../../index')
const WalletHelper = require('../utils/wallet-helper')
async function exportToWif () {
try {
console.log('š Exporting XEC wallet private key to WIF format...\n')
// Check if wallet exists
if (!WalletHelper.walletExists()) {
console.error('ā No wallet found!')
console.log(' Create a wallet first using one of these commands:')
console.log(' ⢠node examples/wallet-creation/create-new-wallet.js')
console.log(' ⢠node examples/wallet-creation/restore-from-mnemonic.js')
console.log(' ⢠node examples/wallet-creation/import-from-wif.js')
process.exit(1)
}
// Load wallet from wallet.json
const walletInfo = WalletHelper.loadWallet()
const wallet = new MinimalXECWallet(walletInfo.mnemonic || walletInfo.privateKey)
await wallet.walletInfoPromise
console.log('š Current Wallet Information:')
console.log('ā'.repeat(50))
console.log(`XEC Address: ${wallet.walletInfo.xecAddress}`)
console.log(`Wallet Type: ${wallet.walletInfo.mnemonic ? 'HD Wallet' : 'Single Key'}`)
console.log('ā'.repeat(50))
console.log('\nš Exporting Private Key in WIF Formats:')
console.log('ā'.repeat(50))
// Export mainnet compressed WIF
const mainnetCompressed = wallet.exportPrivateKeyAsWIF(true, false)
console.log(`Mainnet Compressed: ${mainnetCompressed}`)
console.log(`āā Validation: ${wallet.validateWIF(mainnetCompressed) ? 'ā
Valid' : 'ā Invalid'}`)
console.log(`āā Length: ${mainnetCompressed.length} characters`)
console.log('āā Usage: Most common format for importing')
// Export mainnet uncompressed WIF
const mainnetUncompressed = wallet.exportPrivateKeyAsWIF(false, false)
console.log(`\nMainnet Uncompressed: ${mainnetUncompressed}`)
console.log(`āā Validation: ${wallet.validateWIF(mainnetUncompressed) ? 'ā
Valid' : 'ā Invalid'}`)
console.log(`āā Length: ${mainnetUncompressed.length} characters`)
console.log('āā Usage: Legacy format, less common')
// Export testnet compressed WIF
const testnetCompressed = wallet.exportPrivateKeyAsWIF(true, true)
console.log(`\nTestnet Compressed: ${testnetCompressed}`)
console.log(`āā Validation: ${wallet.validateWIF(testnetCompressed) ? 'ā
Valid' : 'ā Invalid'}`)
console.log(`āā Length: ${testnetCompressed.length} characters`)
console.log('āā Usage: For testnet applications')
// Export testnet uncompressed WIF
const testnetUncompressed = wallet.exportPrivateKeyAsWIF(false, true)
console.log(`\nTestnet Uncompressed: ${testnetUncompressed}`)
console.log(`āā Validation: ${wallet.validateWIF(testnetUncompressed) ? 'ā
Valid' : 'ā Invalid'}`)
console.log(`āā Length: ${testnetUncompressed.length} characters`)
console.log('āā Usage: For testnet, legacy format')
console.log('\nš WIF Format Reference:')
console.log('ā'.repeat(50))
console.log('Format | Prefix | Network | Compression')
console.log('ā'.repeat(50))
console.log('Mainnet Compressed | K, L | Mainnet | Yes')
console.log('Mainnet Uncompressed | 5 | Mainnet | No')
console.log('Testnet Compressed | c | Testnet | Yes')
console.log('Testnet Uncompressed | 9 | Testnet | No')
console.log('\nš Round-trip Validation Test:')
console.log('ā'.repeat(30))
// Test round-trip conversion
const testWIF = mainnetCompressed
const importedWallet = new MinimalXECWallet(testWIF)
await importedWallet.walletInfoPromise
const addressMatch = importedWallet.walletInfo.xecAddress === wallet.walletInfo.xecAddress
console.log(`Original Address: ${wallet.walletInfo.xecAddress}`)
console.log(`Imported Address: ${importedWallet.walletInfo.xecAddress}`)
console.log(`Addresses Match: ${addressMatch ? 'ā
Yes' : 'ā No'}`)
if (addressMatch) {
console.log('\nš Success! WIF export and import working correctly.')
} else {
console.log('\nā Warning! Address mismatch detected.')
}
console.log('\nš SECURITY WARNINGS:')
console.log('ā'.repeat(50))
console.log('⢠WIF private keys provide FULL access to your funds')
console.log('⢠Never share WIF keys with anyone')
console.log('⢠Store WIF keys securely and offline')
console.log('⢠Anyone with your WIF can steal your XEC')
console.log('⢠Use WIF export only when necessary (wallet recovery, etc.)')
console.log('\nš” Usage Examples:')
console.log('ā'.repeat(50))
console.log('⢠Import into other wallets or applications')
console.log('⢠Paper wallet creation for cold storage')
console.log('⢠Wallet backup and recovery')
console.log('⢠Cross-platform wallet migration')
} catch (err) {
console.error('ā Failed to export WIF:', err.message)
process.exit(1)
}
}
// Run the example
exportToWif()