charms-wallet-js
Version:
Professional Bitcoin wallet library for Charms ecosystem using @scure stack
107 lines (90 loc) ⢠4.79 kB
JavaScript
// Basic functionality tests for Charms Wallet JS
const { CharmsWallet, AddressWallet } = require('../dist/index.js');
async function testBasicFunctionality() {
console.log('š® Testing Charms Wallet JS Basic Functionality\n');
try {
// Test 1: Create wallet instance
console.log('1. Creating wallet instance...');
const wallet = new CharmsWallet({ network: 'testnet' });
console.log(' ā
Wallet created successfully');
// Test 2: Generate mnemonic
console.log('2. Generating mnemonic...');
const mnemonic = wallet.generateMnemonic();
console.log(' ā
Mnemonic generated:', mnemonic.split(' ').length, 'words');
// Test 3: Validate mnemonic
console.log('3. Validating mnemonic...');
const validation = wallet.validateMnemonic(mnemonic);
console.log(' ā
Mnemonic validation:', validation.valid ? 'VALID' : 'INVALID');
// Test 4: Generate address
console.log('4. Generating Taproot address...');
const address = await CharmsWallet.generateAddress(mnemonic, 'testnet', 0);
console.log(' ā
Address generated:', address);
// Test 5: Generate change address
console.log('5. Generating change address...');
const changeAddress = await CharmsWallet.generateAddress(mnemonic, 'testnet', 1);
console.log(' ā
Change address generated:', changeAddress);
// Test 6: Validate address
console.log('6. Validating address...');
const addressValidation = wallet.validateAddress(address);
console.log(' ā
Address validation:', addressValidation.valid ? 'VALID' : 'INVALID');
if (addressValidation.valid) {
console.log(' Network:', addressValidation.network);
console.log(' Type:', addressValidation.type);
}
// Test 7: Get private key
console.log('7. Getting private key...');
const privateKey = await CharmsWallet.getPrivateKey(mnemonic, 'testnet', 0);
console.log(' ā
Private key obtained, length:', privateKey.length, 'bytes');
// Test 8: Get complete address data
console.log('8. Getting complete address data...');
const addressData = await CharmsWallet.getAddressData(mnemonic, 'testnet', 0);
console.log(' ā
Address data obtained');
console.log(' Address matches:', addressData.address === address ? 'YES' : 'NO');
console.log(' Public key length:', addressData.publicKey.length, 'bytes');
console.log(' X-only pubkey length:', addressData.xOnlyPubkey.length, 'bytes');
// Test 9: Create AddressWallet from private key
console.log('9. Creating AddressWallet from private key...');
const addressWallet = CharmsWallet.fromPrivateKey(privateKey, 'testnet');
console.log(' ā
AddressWallet created');
console.log(' Address generated:', addressWallet.getAddress());
console.log(' Address is valid format:', addressWallet.getAddress().startsWith('tb1p') ? 'YES' : 'NO');
// Test 10: Create AddressWallet directly with mock address
console.log('10. Creating AddressWallet directly...');
const mockAddress = 'tb1p' + '0'.repeat(58);
const directAddressWallet = new AddressWallet(privateKey, 'testnet', mockAddress);
console.log(' ā
Direct AddressWallet created');
console.log(' Address used:', directAddressWallet.getAddress());
console.log('\nš All basic tests passed successfully!');
console.log('\nš Test Summary:');
console.log(' - Wallet creation: ā
');
console.log(' - Mnemonic generation: ā
');
console.log(' - Mnemonic validation: ā
');
console.log(' - Address generation: ā
');
console.log(' - Change address generation: ā
');
console.log(' - Address validation: ā
');
console.log(' - Private key extraction: ā
');
console.log(' - Complete address data: ā
');
console.log(' - AddressWallet from private key: ā
');
console.log(' - Direct AddressWallet creation: ā
');
return true;
} catch (error) {
console.error('ā Test failed:', error.message);
console.error(' Stack:', error.stack);
return false;
}
}
// Run the test
testBasicFunctionality()
.then(success => {
if (success) {
console.log('\nā
Library is working correctly!');
process.exit(0);
} else {
console.log('\nā Library has issues!');
process.exit(1);
}
})
.catch(error => {
console.error('ā Unexpected error:', error);
process.exit(1);
});