|
| 1 | +(function () { |
| 2 | + const D = window.LNbitsBoxDashboard; |
| 3 | + if (!D) return; |
| 4 | + |
| 5 | + const state = { |
| 6 | + pendingMnemonic: '', |
| 7 | + }; |
| 8 | + |
| 9 | + function el(id) { |
| 10 | + return document.getElementById(id); |
| 11 | + } |
| 12 | + |
| 13 | + function openModal(id) { |
| 14 | + const modal = el(id); |
| 15 | + if (modal) modal.classList.remove('hidden'); |
| 16 | + } |
| 17 | + |
| 18 | + function closeModal(id) { |
| 19 | + const modal = el(id); |
| 20 | + if (modal) modal.classList.add('hidden'); |
| 21 | + } |
| 22 | + |
| 23 | + function setError(message) { |
| 24 | + const errorEl = el('spark-seed-form-error'); |
| 25 | + if (!errorEl) return; |
| 26 | + if (message) { |
| 27 | + errorEl.textContent = message; |
| 28 | + errorEl.classList.remove('hidden'); |
| 29 | + } else { |
| 30 | + errorEl.textContent = ''; |
| 31 | + errorEl.classList.add('hidden'); |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + function normalizeMnemonic(value) { |
| 36 | + return (value || '').trim().toLowerCase().split(/\s+/).filter(Boolean).join(' '); |
| 37 | + } |
| 38 | + |
| 39 | + function currentMnemonic() { |
| 40 | + const seedValue = el('spark-seed-value'); |
| 41 | + return normalizeMnemonic(seedValue ? seedValue.dataset.seedPhrase || '' : ''); |
| 42 | + } |
| 43 | + |
| 44 | + function validateMnemonic(value) { |
| 45 | + const normalized = normalizeMnemonic(value); |
| 46 | + if (!normalized) return 'Enter a seed phrase.'; |
| 47 | + const words = normalized.split(' '); |
| 48 | + if (words.length !== 12) return 'Enter exactly 12 words.'; |
| 49 | + if (normalized === currentMnemonic()) return 'This is already the current seed phrase.'; |
| 50 | + return ''; |
| 51 | + } |
| 52 | + |
| 53 | + function resetFlow() { |
| 54 | + state.pendingMnemonic = ''; |
| 55 | + const input = el('spark-seed-new-input'); |
| 56 | + if (input) input.value = ''; |
| 57 | + const checkbox = el('spark-seed-backed-up-checkbox'); |
| 58 | + if (checkbox) checkbox.checked = false; |
| 59 | + const confirmBtn = el('spark-seed-backup-confirm-btn'); |
| 60 | + if (confirmBtn) confirmBtn.disabled = true; |
| 61 | + setError(''); |
| 62 | + closeModal('spark-seed-change-modal'); |
| 63 | + closeModal('spark-seed-backup-modal'); |
| 64 | + D.closeModal(); |
| 65 | + } |
| 66 | + |
| 67 | + async function submitMnemonic() { |
| 68 | + const actionBtn = el('spark-seed-change-continue-btn'); |
| 69 | + const originalHtml = actionBtn ? actionBtn.innerHTML : ''; |
| 70 | + if (actionBtn) { |
| 71 | + actionBtn.disabled = true; |
| 72 | + actionBtn.innerHTML = '<span class="inline-flex items-center gap-2"><span class="w-3.5 h-3.5 border-2 border-white/70 border-t-transparent rounded-full animate-spin"></span><span>Updating...</span></span>'; |
| 73 | + } |
| 74 | + |
| 75 | + try { |
| 76 | + const resp = await fetch('/box/api/spark/seed', { |
| 77 | + method: 'POST', |
| 78 | + headers: { 'Content-Type': 'application/json' }, |
| 79 | + body: JSON.stringify({ mnemonic: state.pendingMnemonic }), |
| 80 | + }); |
| 81 | + const data = await resp.json(); |
| 82 | + if (!resp.ok || data.status !== 'ok') { |
| 83 | + closeModal('spark-seed-backup-modal'); |
| 84 | + openModal('spark-seed-change-modal'); |
| 85 | + setError(data.message || 'Failed to update the seed phrase.'); |
| 86 | + return; |
| 87 | + } |
| 88 | + |
| 89 | + const seedValue = el('spark-seed-value'); |
| 90 | + if (seedValue) { |
| 91 | + seedValue.dataset.seedPhrase = state.pendingMnemonic; |
| 92 | + seedValue.dataset.masked = 'true'; |
| 93 | + seedValue.textContent = '••••• ••••• ••••• ••••• ••••• ••••• ••••• ••••• ••••• ••••• ••••• •••••'; |
| 94 | + } |
| 95 | + const toggleBtn = el('spark-seed-toggle-btn'); |
| 96 | + if (toggleBtn) toggleBtn.textContent = 'Show Seed Phrase'; |
| 97 | + |
| 98 | + resetFlow(); |
| 99 | + D.showNotice(data.message || 'Spark seed phrase updated. Spark is restarting automatically.', 'Success'); |
| 100 | + if (typeof D.fetchStats === 'function') { |
| 101 | + setTimeout(D.fetchStats, 1200); |
| 102 | + } |
| 103 | + } catch (error) { |
| 104 | + closeModal('spark-seed-backup-modal'); |
| 105 | + openModal('spark-seed-change-modal'); |
| 106 | + setError('Request failed.'); |
| 107 | + } finally { |
| 108 | + if (actionBtn) { |
| 109 | + actionBtn.disabled = false; |
| 110 | + actionBtn.innerHTML = originalHtml; |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + function openFinalConfirm() { |
| 116 | + closeModal('spark-seed-backup-modal'); |
| 117 | + D.state.pendingAction = null; |
| 118 | + D.state.pendingActionButtonId = null; |
| 119 | + D.setText('confirm-title', 'Final confirmation'); |
| 120 | + D.setText('confirm-message', 'Are you absolutely sure you want to replace the Spark wallet seed phrase now? This cannot be undone from LNbitsBox.'); |
| 121 | + const confirmBtn = el('confirm-btn'); |
| 122 | + if (confirmBtn) { |
| 123 | + confirmBtn.textContent = 'Replace Seed Phrase'; |
| 124 | + confirmBtn.onclick = function () { |
| 125 | + D.closeModal(); |
| 126 | + submitMnemonic(); |
| 127 | + }; |
| 128 | + } |
| 129 | + openModal('confirm-modal'); |
| 130 | + } |
| 131 | + |
| 132 | + const openBtn = el('spark-seed-change-btn'); |
| 133 | + const closeBtn = el('spark-seed-change-close-btn'); |
| 134 | + const cancelBtn = el('spark-seed-change-cancel-btn'); |
| 135 | + const continueBtn = el('spark-seed-change-continue-btn'); |
| 136 | + const backupCancelBtn = el('spark-seed-backup-cancel-btn'); |
| 137 | + const backupConfirmBtn = el('spark-seed-backup-confirm-btn'); |
| 138 | + const backupCheckbox = el('spark-seed-backed-up-checkbox'); |
| 139 | + |
| 140 | + if (openBtn) { |
| 141 | + openBtn.addEventListener('click', function () { |
| 142 | + setError(''); |
| 143 | + openModal('spark-seed-change-modal'); |
| 144 | + const input = el('spark-seed-new-input'); |
| 145 | + if (input) input.focus(); |
| 146 | + }); |
| 147 | + } |
| 148 | + |
| 149 | + [closeBtn, cancelBtn].filter(Boolean).forEach(function (button) { |
| 150 | + button.addEventListener('click', function () { |
| 151 | + resetFlow(); |
| 152 | + }); |
| 153 | + }); |
| 154 | + |
| 155 | + if (continueBtn) { |
| 156 | + continueBtn.addEventListener('click', function () { |
| 157 | + const input = el('spark-seed-new-input'); |
| 158 | + const normalized = normalizeMnemonic(input ? input.value : ''); |
| 159 | + const error = validateMnemonic(normalized); |
| 160 | + if (error) { |
| 161 | + setError(error); |
| 162 | + return; |
| 163 | + } |
| 164 | + state.pendingMnemonic = normalized; |
| 165 | + setError(''); |
| 166 | + closeModal('spark-seed-change-modal'); |
| 167 | + openModal('spark-seed-backup-modal'); |
| 168 | + }); |
| 169 | + } |
| 170 | + |
| 171 | + if (backupCheckbox && backupConfirmBtn) { |
| 172 | + backupCheckbox.addEventListener('change', function () { |
| 173 | + backupConfirmBtn.disabled = !backupCheckbox.checked; |
| 174 | + }); |
| 175 | + } |
| 176 | + |
| 177 | + if (backupCancelBtn) { |
| 178 | + backupCancelBtn.addEventListener('click', function () { |
| 179 | + closeModal('spark-seed-backup-modal'); |
| 180 | + openModal('spark-seed-change-modal'); |
| 181 | + }); |
| 182 | + } |
| 183 | + |
| 184 | + if (backupConfirmBtn) { |
| 185 | + backupConfirmBtn.addEventListener('click', function () { |
| 186 | + if (backupConfirmBtn.disabled) return; |
| 187 | + openFinalConfirm(); |
| 188 | + }); |
| 189 | + } |
| 190 | + |
| 191 | + document.addEventListener('keydown', function (event) { |
| 192 | + if (event.key !== 'Escape') return; |
| 193 | + if (!el('spark-seed-change-modal')?.classList.contains('hidden')) { |
| 194 | + resetFlow(); |
| 195 | + return; |
| 196 | + } |
| 197 | + if (!el('spark-seed-backup-modal')?.classList.contains('hidden')) { |
| 198 | + closeModal('spark-seed-backup-modal'); |
| 199 | + openModal('spark-seed-change-modal'); |
| 200 | + } |
| 201 | + }); |
| 202 | +})(); |
0 commit comments