Skip to content

Commit d02cf1a

Browse files
committed
fix wallet file upload
1 parent 2e5e964 commit d02cf1a

2 files changed

Lines changed: 63 additions & 13 deletions

File tree

app/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "mintlayer-web-gui",
33
"type": "module",
4-
"version": "0.99.4",
4+
"version": "0.99.5",
55
"scripts": {
66
"dev": "astro dev",
77
"build": "astro build",

app/src/pages/setup.astro

Lines changed: 62 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ type SetupResult =
1212
| { action: 'error'; message: string }
1313
| { action: 'create_success'; mnemonic: string; walletPath: string }
1414
| { action: 'import_success' }
15-
| { action: 'upload_success' };
15+
| { action: 'upload_success' }
16+
| { action: 'upload_confirm_needed' };
1617
1718
let result: SetupResult = { action: 'none' };
1819
@@ -63,22 +64,29 @@ if (Astro.request.method === 'POST') {
6364
6465
// ── Upload existing wallet file ─────────────────────────────────────────────
6566
} else if (action === 'upload') {
66-
const fileField = form.get('wallet_file');
67-
const password = form.get('password')?.toString().trim() || undefined;
67+
const fileField = form.get('wallet_file');
68+
const password = form.get('password')?.toString().trim() || undefined;
69+
const confirmOverwrite = form.get('confirm_overwrite') === 'yes';
6870
6971
if (!(fileField instanceof File) || fileField.size === 0) {
7072
result = { action: 'error', message: 'Please select a wallet file.' };
7173
} else if (fileField.size > 50 * 1024 * 1024) {
7274
result = { action: 'error', message: 'Wallet file too large (max 50 MB).' };
7375
} else {
7476
try {
75-
const uploadDir = '/app/uploads';
76-
const { writeFile, mkdir } = await import('node:fs/promises');
77-
await mkdir(uploadDir, { recursive: true });
78-
// Write directly as mintlayer.wallet, overwriting any existing file
79-
await writeFile(`${uploadDir}/mintlayer.wallet`, Buffer.from(await fileField.arrayBuffer()));
80-
await openWallet(WALLET_PATH, password);
81-
result = { action: 'upload_success' };
77+
const destPath = '/app/mintlayer-data/mintlayer.wallet';
78+
const { writeFile, access } = await import('node:fs/promises');
79+
80+
let walletExists = false;
81+
try { await access(destPath); walletExists = true; } catch {}
82+
83+
if (walletExists && !confirmOverwrite) {
84+
result = { action: 'upload_confirm_needed' };
85+
} else {
86+
await writeFile(destPath, Buffer.from(await fileField.arrayBuffer()));
87+
await openWallet(WALLET_PATH, password);
88+
result = { action: 'upload_success' };
89+
}
8290
} catch (err) {
8391
result = { action: 'error', message: (err as Error).message };
8492
}
@@ -383,6 +391,22 @@ if (Astro.request.method === 'POST') {
383391
/>
384392
</div>
385393

394+
<div id="overwrite-section" class="hidden rounded-lg border border-yellow-700/50 bg-yellow-900/10 px-3 py-3 space-y-2">
395+
<p class="text-xs text-yellow-400">
396+
A wallet file already exists. Uploading will permanently replace it.
397+
</p>
398+
<label class="flex items-start gap-2 cursor-pointer">
399+
<input
400+
id="confirm-overwrite-cb"
401+
name="confirm_overwrite"
402+
type="checkbox"
403+
value="yes"
404+
class="accent-yellow-500 w-4 h-4 mt-0.5 shrink-0"
405+
/>
406+
<span class="text-xs text-yellow-300">I understand — overwrite the existing wallet file</span>
407+
</label>
408+
</div>
409+
386410
<div class="flex items-center justify-end gap-3 pt-1">
387411
<button
388412
type="button"
@@ -410,10 +434,36 @@ if (Astro.request.method === 'POST') {
410434
</style>
411435

412436
<script>
413-
document.getElementById('upload-modal')?.addEventListener('click', function (e) {
414-
if (e.target === this) this.close();
437+
const uploadModal = document.getElementById('upload-modal') as HTMLDialogElement | null;
438+
const overwriteSection = document.getElementById('overwrite-section');
439+
const confirmCb = document.getElementById('confirm-overwrite-cb') as HTMLInputElement | null;
440+
441+
uploadModal?.addEventListener('click', function (e) {
442+
if (e.target === this) (this as HTMLDialogElement).close();
443+
});
444+
445+
// Validate overwrite checkbox before submit
446+
uploadModal?.querySelector('form')?.addEventListener('submit', function (e) {
447+
if (overwriteSection && !overwriteSection.classList.contains('hidden')) {
448+
if (confirmCb && !confirmCb.checked) {
449+
e.preventDefault();
450+
confirmCb.setCustomValidity('Please confirm you want to overwrite the existing wallet file.');
451+
confirmCb.reportValidity();
452+
} else if (confirmCb) {
453+
confirmCb.setCustomValidity('');
454+
}
455+
}
415456
});
416457
</script>
458+
459+
{result.action === 'upload_confirm_needed' && (
460+
<script>
461+
const modal = document.getElementById('upload-modal') as HTMLDialogElement | null;
462+
const section = document.getElementById('overwrite-section');
463+
modal?.showModal();
464+
section?.classList.remove('hidden');
465+
</script>
466+
)}
417467
</>
418468
)}
419469

0 commit comments

Comments
 (0)