| … | … | @@ -0,0 +1,337 @@ |
| 1 | #!/usr/bin/env node |
| 2 | /** |
| 3 | * Deep Debugging: Mnemonic vs Storage Address Mismatch |
| 4 | * |
| 5 | * This script performs a systematic analysis of: |
| 6 | * 1. What addresses are stored in the database |
| 7 | * 2. What addresses the current mnemonic derives to |
| 8 | * 3. Where the mismatch occurs |
| 9 | * 4. Device and timestamp information |
| 10 | */ |
| 11 | |
| 12 | // CRITICAL: Load bare-node-runtime FIRST |
| 13 | require('bare-node-runtime/global') |
| 14 | |
| 15 | const fs = require('bare-fs') |
| 16 | const path = require('bare-path') |
| 17 | const os = require('bare-os') |
| 18 | const Corestore = require('corestore') |
| 19 | const Autobase = require('autobase') |
| 20 | const Hyperbee = require('hyperbee') |
| 21 | const WDK = require('@tetherto/wdk') |
| 22 | const WalletManagerEvm = require('@tetherto/wdk-wallet-evm') |
| 23 | const WalletManagerBtc = require('@tetherto/wdk-wallet-btc') |
| 24 | const { decrypt } = require('./lib/encryption.cjs') |
| 25 | |
| 26 | const STORAGE_DIR = path.join(os.homedir(), '.hyper-wallet') |
| 27 | const DB_DIR = path.join(STORAGE_DIR, 'storage') |
| 28 | const WALLET_MNEMONIC_FILE = path.join(STORAGE_DIR, '.wallet-mnemonic') |
| 29 | const IDENTITY_MNEMONIC_FILE = path.join(STORAGE_DIR, '.mnemonic') |
| 30 | |
| 31 | // Colors for terminal output |
| 32 | const colors = { |
| 33 | reset: '\x1b[0m', |
| 34 | red: '\x1b[31m', |
| 35 | green: '\x1b[32m', |
| 36 | yellow: '\x1b[33m', |
| 37 | blue: '\x1b[34m', |
| 38 | magenta: '\x1b[35m', |
| 39 | cyan: '\x1b[36m', |
| 40 | bold: '\x1b[1m' |
| 41 | } |
| 42 | |
| 43 | function log(msg, color = 'reset') { |
| 44 | console.log(`${colors[color]}${msg}${colors.reset}`) |
| 45 | } |
| 46 | |
| 47 | function header(msg) { |
| 48 | console.log() |
| 49 | log('='.repeat(80), 'cyan') |
| 50 | log(msg, 'bold') |
| 51 | log('='.repeat(80), 'cyan') |
| 52 | console.log() |
| 53 | } |
| 54 | |
| 55 | async function debugMnemonicMismatch(password) { |
| 56 | header('DEEP DEBUGGING: Mnemonic vs Storage Mismatch Analysis') |
| 57 | |
| 58 | // ======================================== |
| 59 | // STEP 1: Read stored mnemonics |
| 60 | // ======================================== |
| 61 | header('STEP 1: Read Stored Mnemonics') |
| 62 | |
| 63 | let walletMnemonic = null |
| 64 | let identityMnemonic = null |
| 65 | |
| 66 | try { |
| 67 | const walletEncrypted = fs.readFileSync(WALLET_MNEMONIC_FILE, 'utf8').trim() |
| 68 | walletMnemonic = decrypt(walletEncrypted, password) |
| 69 | log(`✓ Wallet mnemonic loaded: ${walletMnemonic.split(' ').length} words`, 'green') |
| 70 | log(` First 3 words: ${walletMnemonic.split(' ').slice(0, 3).join(' ')}...`, 'blue') |
| 71 | } catch (err) { |
| 72 | log(`✗ Failed to load wallet mnemonic: ${err.message}`, 'red') |
| 73 | process.exit(1) |
| 74 | } |
| 75 | |
| 76 | try { |
| 77 | const identityEncrypted = fs.readFileSync(IDENTITY_MNEMONIC_FILE, 'utf8').trim() |
| 78 | identityMnemonic = decrypt(identityEncrypted, password) |
| 79 | log(`✓ Identity mnemonic loaded: ${identityMnemonic.split(' ').length} words`, 'green') |
| 80 | log(` First 3 words: ${identityMnemonic.split(' ').slice(0, 3).join(' ')}...`, 'blue') |
| 81 | } catch (err) { |
| 82 | log(`⚠ No identity mnemonic found`, 'yellow') |
| 83 | } |
| 84 | |
| 85 | // ======================================== |
| 86 | // STEP 2: Derive expected addresses from wallet mnemonic |
| 87 | // ======================================== |
| 88 | header('STEP 2: Derive Expected Addresses from Wallet Mnemonic') |
| 89 | |
| 90 | const wdk = new WDK.default(walletMnemonic) |
| 91 | .registerWallet('ethereum', WalletManagerEvm.default, { |
| 92 | provider: 'https://ethereum-sepolia-rpc.publicnode.com' |
| 93 | }) |
| 94 | .registerWallet('bitcoin', WalletManagerBtc.default, { |
| 95 | network: 'testnet', |
| 96 | host: 'electrum.blockstream.info', |
| 97 | port: 60001 |
| 98 | }) |
| 99 | |
| 100 | const expectedAddresses = {} |
| 101 | |
| 102 | // Derive Ethereum addresses (indices 0-5) |
| 103 | log('Deriving Ethereum addresses...', 'cyan') |
| 104 | expectedAddresses.ethereum = [] |
| 105 | for (let i = 0; i < 5; i++) { |
| 106 | const account = await wdk.getAccount('ethereum', i) |
| 107 | const address = await account.getAddress() |
| 108 | expectedAddresses.ethereum.push({ index: i, address }) |
| 109 | log(` [${i}] ${address}`, i === 0 ? 'green' : 'blue') |
| 110 | } |
| 111 | |
| 112 | // Derive Bitcoin addresses (indices 0-5) |
| 113 | log('\nDeriving Bitcoin addresses...', 'cyan') |
| 114 | expectedAddresses.bitcoin = [] |
| 115 | for (let i = 0; i < 5; i++) { |
| 116 | const account = await wdk.getAccount('bitcoin', i) |
| 117 | const address = await account.getAddress() |
| 118 | expectedAddresses.bitcoin.push({ index: i, address }) |
| 119 | log(` [${i}] ${address}`, i === 0 ? 'green' : 'blue') |
| 120 | } |
| 121 | |
| 122 | // ======================================== |
| 123 | // STEP 3: Read stored addresses from database |
| 124 | // ======================================== |
| 125 | header('STEP 3: Read Stored Addresses from Database') |
| 126 | |
| 127 | const store = new Corestore(DB_DIR) |
| 128 | const autobase = new Autobase(store, null, { |
| 129 | valueEncoding: 'json', |
| 130 | open: () => store.get({ name: 'main' }) |
| 131 | }) |
| 132 | const db = new Hyperbee(autobase.view, { |
| 133 | keyEncoding: 'utf-8', |
| 134 | valueEncoding: 'json' |
| 135 | }) |
| 136 | |
| 137 | await db.ready() |
| 138 | |
| 139 | const storedAddresses = { |
| 140 | ethereum: [], |
| 141 | 'ethereum-usdt': [], |
| 142 | bitcoin: [] |
| 143 | } |
| 144 | |
| 145 | // Read all stored addresses |
| 146 | log('Reading Ethereum addresses from storage...', 'cyan') |
| 147 | for await (const { key, value } of db.createReadStream({ |
| 148 | gte: 'wallet:address:ethereum:', |
| 149 | lt: 'wallet:address:ethereum:\xFF' |
| 150 | })) { |
| 151 | storedAddresses.ethereum.push({ key, ...value }) |
| 152 | const current = value.isCurrent ? ' [CURRENT]' : '' |
| 153 | const color = value.isCurrent ? 'green' : 'blue' |
| 154 | log(` [${value.index}] ${value.address}${current}`, color) |
| 155 | log(` Device: ${value.device || 'unknown'} | Created: ${new Date(value.createdAt).toISOString()}`, 'blue') |
| 156 | } |
| 157 | |
| 158 | log('\nReading Ethereum-USDT addresses from storage...', 'cyan') |
| 159 | for await (const { key, value } of db.createReadStream({ |
| 160 | gte: 'wallet:address:ethereum-usdt:', |
| 161 | lt: 'wallet:address:ethereum-usdt:\xFF' |
| 162 | })) { |
| 163 | storedAddresses['ethereum-usdt'].push({ key, ...value }) |
| 164 | const current = value.isCurrent ? ' [CURRENT]' : '' |
| 165 | const color = value.isCurrent ? 'green' : 'blue' |
| 166 | log(` [${value.index}] ${value.address}${current}`, color) |
| 167 | log(` Device: ${value.device || 'unknown'} | Created: ${new Date(value.createdAt).toISOString()}`, 'blue') |
| 168 | } |
| 169 | |
| 170 | log('\nReading Bitcoin addresses from storage...', 'cyan') |
| 171 | for await (const { key, value } of db.createReadStream({ |
| 172 | gte: 'wallet:address:bitcoin:', |
| 173 | lt: 'wallet:address:bitcoin:\xFF' |
| 174 | })) { |
| 175 | storedAddresses.bitcoin.push({ key, ...value }) |
| 176 | const current = value.isCurrent ? ' [CURRENT]' : '' |
| 177 | const color = value.isCurrent ? 'green' : 'blue' |
| 178 | log(` [${value.index}] ${value.address}${current}`, color) |
| 179 | log(` Device: ${value.device || 'unknown'} | Created: ${new Date(value.createdAt).toISOString()}`, 'blue') |
| 180 | } |
| 181 | |
| 182 | // ======================================== |
| 183 | // STEP 4: Compare derived vs stored |
| 184 | // ======================================== |
| 185 | header('STEP 4: Validation - Compare Derived vs Stored') |
| 186 | |
| 187 | const mismatches = [] |
| 188 | |
| 189 | // Check Ethereum |
| 190 | log('Validating Ethereum addresses...', 'cyan') |
| 191 | for (const stored of storedAddresses.ethereum) { |
| 192 | const expected = expectedAddresses.ethereum.find(e => e.index === stored.index) |
| 193 | if (!expected) { |
| 194 | log(` [${stored.index}] WARNING: Stored address has no expected derivation`, 'yellow') |
| 195 | mismatches.push({ chain: 'ethereum', index: stored.index, stored: stored.address, expected: null }) |
| 196 | continue |
| 197 | } |
| 198 | |
| 199 | const match = stored.address.toLowerCase() === expected.address.toLowerCase() |
| 200 | if (match) { |
| 201 | log(` [${stored.index}] ✓ MATCH: ${stored.address}`, 'green') |
| 202 | } else { |
| 203 | log(` [${stored.index}] ✗ MISMATCH:`, 'red') |
| 204 | log(` Stored: ${stored.address}`, 'red') |
| 205 | log(` Expected: ${expected.address}`, 'green') |
| 206 | mismatches.push({ |
| 207 | chain: 'ethereum', |
| 208 | index: stored.index, |
| 209 | stored: stored.address, |
| 210 | expected: expected.address, |
| 211 | device: stored.device, |
| 212 | createdAt: new Date(stored.createdAt).toISOString() |
| 213 | }) |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | // Check Ethereum-USDT (should use same addresses as Ethereum) |
| 218 | log('\nValidating Ethereum-USDT addresses...', 'cyan') |
| 219 | for (const stored of storedAddresses['ethereum-usdt']) { |
| 220 | const expected = expectedAddresses.ethereum.find(e => e.index === stored.index) |
| 221 | if (!expected) { |
| 222 | log(` [${stored.index}] WARNING: Stored address has no expected derivation`, 'yellow') |
| 223 | mismatches.push({ chain: 'ethereum-usdt', index: stored.index, stored: stored.address, expected: null }) |
| 224 | continue |
| 225 | } |
| 226 | |
| 227 | const match = stored.address.toLowerCase() === expected.address.toLowerCase() |
| 228 | if (match) { |
| 229 | log(` [${stored.index}] ✓ MATCH: ${stored.address}`, 'green') |
| 230 | } else { |
| 231 | log(` [${stored.index}] ✗ MISMATCH:`, 'red') |
| 232 | log(` Stored: ${stored.address}`, 'red') |
| 233 | log(` Expected: ${expected.address}`, 'green') |
| 234 | mismatches.push({ |
| 235 | chain: 'ethereum-usdt', |
| 236 | index: stored.index, |
| 237 | stored: stored.address, |
| 238 | expected: expected.address, |
| 239 | device: stored.device, |
| 240 | createdAt: new Date(stored.createdAt).toISOString() |
| 241 | }) |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | // Check Bitcoin |
| 246 | log('\nValidating Bitcoin addresses...', 'cyan') |
| 247 | for (const stored of storedAddresses.bitcoin) { |
| 248 | const expected = expectedAddresses.bitcoin.find(e => e.index === stored.index) |
| 249 | if (!expected) { |
| 250 | log(` [${stored.index}] WARNING: Stored address has no expected derivation`, 'yellow') |
| 251 | continue |
| 252 | } |
| 253 | |
| 254 | const match = stored.address === expected.address |
| 255 | if (match) { |
| 256 | log(` [${stored.index}] ✓ MATCH: ${stored.address}`, 'green') |
| 257 | } else { |
| 258 | log(` [${stored.index}] ✗ MISMATCH:`, 'red') |
| 259 | log(` Stored: ${stored.address}`, 'red') |
| 260 | log(` Expected: ${expected.address}`, 'green') |
| 261 | mismatches.push({ |
| 262 | chain: 'bitcoin', |
| 263 | index: stored.index, |
| 264 | stored: stored.address, |
| 265 | expected: expected.address, |
| 266 | device: stored.device, |
| 267 | createdAt: new Date(stored.createdAt).toISOString() |
| 268 | }) |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | // ======================================== |
| 273 | // STEP 5: Check devices |
| 274 | // ======================================== |
| 275 | header('STEP 5: Check Devices') |
| 276 | |
| 277 | const devices = [] |
| 278 | for await (const { key, value } of db.createReadStream({ |
| 279 | gte: 'wallet:device:', |
| 280 | lt: 'wallet:device:\xFF' |
| 281 | })) { |
| 282 | devices.push(value) |
| 283 | log(`Device: ${value.name}`, 'cyan') |
| 284 | log(` Writer Key: ${Buffer.from(value.key).toString('hex')}`, 'blue') |
| 285 | log(` Added: ${new Date(value.addedAt).toISOString()}`, 'blue') |
| 286 | } |
| 287 | |
| 288 | if (devices.length === 0) { |
| 289 | log('No devices found in storage', 'yellow') |
| 290 | } |
| 291 | |
| 292 | // ======================================== |
| 293 | // STEP 6: Summary |
| 294 | // ======================================== |
| 295 | header('SUMMARY') |
| 296 | |
| 297 | log(`Total stored addresses:`, 'cyan') |
| 298 | log(` Ethereum: ${storedAddresses.ethereum.length}`, 'blue') |
| 299 | log(` Ethereum-USDT: ${storedAddresses['ethereum-usdt'].length}`, 'blue') |
| 300 | log(` Bitcoin: ${storedAddresses.bitcoin.length}`, 'blue') |
| 301 | |
| 302 | log(`\nTotal mismatches: ${mismatches.length}`, mismatches.length > 0 ? 'red' : 'green') |
| 303 | |
| 304 | if (mismatches.length > 0) { |
| 305 | log('\nMismatch Details:', 'red') |
| 306 | for (const m of mismatches) { |
| 307 | log(` ${m.chain} [${m.index}]:`, 'red') |
| 308 | log(` Stored: ${m.stored}`, 'red') |
| 309 | log(` Expected: ${m.expected || 'N/A'}`, 'green') |
| 310 | if (m.device) log(` Device: ${m.device}`, 'yellow') |
| 311 | if (m.createdAt) log(` Created: ${m.createdAt}`, 'yellow') |
| 312 | } |
| 313 | |
| 314 | log('\n⚠️ CRITICAL: Storage contains addresses that do NOT match the current wallet mnemonic!', 'red') |
| 315 | log('This means the exported mnemonic will NOT recover these addresses.', 'red') |
| 316 | } else { |
| 317 | log('\n✓ All stored addresses match the wallet mnemonic!', 'green') |
| 318 | } |
| 319 | |
| 320 | await db.close() |
| 321 | await store.close() |
| 322 | } |
| 323 | |
| 324 | // Main |
| 325 | const password = process.argv[2] |
| 326 | |
| 327 | if (!password) { |
| 328 | console.error('Usage: node debug-mnemonic-mismatch.cjs <password>') |
| 329 | console.error('Example: node debug-mnemonic-mismatch.cjs mypassword') |
| 330 | process.exit(1) |
| 331 | } |
| 332 | |
| 333 | debugMnemonicMismatch(password).catch(err => { |
| 334 | console.error('\n❌ Error:', err.message) |
| 335 | console.error(err.stack) |
| 336 | process.exit(1) |
| 337 | }) |