-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathbackup.html
More file actions
678 lines (631 loc) · 28.5 KB
/
Copy pathbackup.html
File metadata and controls
678 lines (631 loc) · 28.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
<!DOCTYPE html>
<html>
<head>
<title>rgb-lib WASM — Backup & Utilities</title>
<style>
* { box-sizing: border-box; margin: 0; padding: 0; }
body { font-family: system-ui, sans-serif; background: #0d1117; color: #c9d1d9; padding: 2rem; max-width: 960px; margin: 0 auto; }
h1 { color: #58a6ff; margin-bottom: 0.5rem; }
.subtitle { color: #8b949e; margin-bottom: 1rem; font-size: 0.9rem; }
nav { margin-bottom: 2rem; }
nav a { color: #58a6ff; margin-right: 1.5rem; font-size: 0.9rem; text-decoration: none; }
nav a:hover { text-decoration: underline; }
nav a.active { font-weight: 700; border-bottom: 2px solid #58a6ff; padding-bottom: 2px; }
.status { padding: 0.5rem 1rem; border-radius: 6px; margin-bottom: 2rem; font-size: 0.85rem; }
.status.loading { background: #1c1f26; color: #d29922; }
.status.ready { background: #0d2818; color: #3fb950; }
.status.error { background: #2d1117; color: #f85149; }
.section { background: #161b22; border: 1px solid #30363d; border-radius: 8px; padding: 1.5rem; margin-bottom: 1.5rem; }
.section h2 { color: #58a6ff; font-size: 1.1rem; margin-bottom: 1rem; }
.section h3 { color: #8b949e; font-size: 0.95rem; margin-bottom: 0.75rem; margin-top: 0.75rem; }
.hint { color: #8b949e; font-size: 0.85rem; margin-bottom: 1rem; }
label { display: block; color: #8b949e; font-size: 0.85rem; margin-bottom: 0.3rem; }
select, input, textarea {
width: 100%; padding: 0.5rem 0.75rem; background: #0d1117; border: 1px solid #30363d;
border-radius: 6px; color: #c9d1d9; font-family: monospace; font-size: 0.9rem; margin-bottom: 1rem;
}
input[type="checkbox"] { width: auto; margin-bottom: 0; }
input[type="file"] { padding: 0.3rem; }
.checkbox-label {
display: inline-flex; align-items: center; gap: 0.4rem;
color: #8b949e; font-size: 0.85rem; margin-bottom: 1rem; cursor: pointer;
}
textarea { resize: vertical; min-height: 60px; }
select:focus, input:focus, textarea:focus { outline: none; border-color: #58a6ff; }
button {
padding: 0.5rem 1.25rem; background: #238636; color: #fff; border: none;
border-radius: 6px; cursor: pointer; font-size: 0.9rem; font-weight: 600;
}
button:hover { background: #2ea043; }
button:disabled { background: #21262d; color: #484f58; cursor: not-allowed; }
button.danger { background: #da3633; }
button.danger:hover { background: #f85149; }
button.danger:disabled { background: #21262d; color: #484f58; }
button.secondary { background: #30363d; }
button.secondary:hover { background: #484f58; }
button.secondary:disabled { background: #21262d; color: #484f58; }
button.accent { background: #1f6feb; }
button.accent:hover { background: #388bfd; }
button.accent:disabled { background: #21262d; color: #484f58; }
button.auto-btn { background: #8957e5; }
button.auto-btn:hover { background: #a371f7; }
button.auto-btn:disabled { background: #21262d; color: #484f58; }
.output { margin-top: 1rem; }
.output-label { color: #8b949e; font-size: 0.8rem; margin-bottom: 0.3rem; }
pre {
background: #0d1117; border: 1px solid #30363d; border-radius: 6px;
padding: 1rem; overflow-x: auto; font-size: 0.85rem; line-height: 1.5;
max-height: 400px; overflow-y: auto; white-space: pre-wrap; word-break: break-all;
}
.field-row { display: flex; gap: 1rem; align-items: flex-end; }
.field-row > * { flex: 1; }
.field-row > button { flex: 0 0 auto; margin-bottom: 1rem; }
.btn-group { display: flex; gap: 0.5rem; margin-bottom: 1rem; flex-wrap: wrap; }
.copy-btn { background: #30363d; font-size: 0.75rem; padding: 0.3rem 0.6rem; margin-left: 0.5rem; }
.copy-btn:hover { background: #484f58; }
hr.divider { border: none; border-top: 1px solid #30363d; margin: 1rem 0; }
#log {
background: #0d1117; border: 1px solid #30363d; border-radius: 6px;
padding: 1rem; font-size: 0.8rem; line-height: 1.6; max-height: 400px;
overflow-y: auto; white-space: pre-wrap; word-break: break-all; min-height: 60px;
}
.log-ok { color: #3fb950; }
.log-err { color: #f85149; }
.log-warn { color: #d29922; }
.log-info { color: #8b949e; }
.round-trip-result { margin-top: 0.5rem; font-size: 0.85rem; padding: 0.4rem 0.75rem; border-radius: 4px; }
.round-trip-result.pass { background: #0d2818; color: #3fb950; }
.round-trip-result.fail { background: #2d1117; color: #f85149; }
</style>
</head>
<body>
<h1>rgb-lib WASM — Backup & Utilities</h1>
<p class="subtitle">Local backup, VSS cloud backup, proxy check, and error handling tests</p>
<!-- Nav bar -->
<nav>
<a href="index.html">Quick Start</a>
<a href="wallet.html">Keys & Wallet</a>
<a href="bitcoin.html">Bitcoin</a>
<a href="rgb.html">RGB Assets</a>
<a href="backup.html" class="active">Backup & Utilities</a>
</nav>
<!-- WASM status -->
<div id="status" class="status loading">Initializing WASM module...</div>
<!-- ================================================================== -->
<!-- Wallet Setup (quick) -->
<!-- ================================================================== -->
<div class="section">
<h2>Wallet Setup (quick)</h2>
<p class="hint">
Auto-setup generates keys and creates a wallet in one click. Or do it manually below.
</p>
<div class="btn-group">
<button id="btn-auto-setup" class="auto-btn" disabled>Auto-Setup Regtest Wallet</button>
</div>
<hr class="divider" />
<h3>Manual Setup</h3>
<div class="field-row">
<div>
<label for="gen-network">Network</label>
<select id="gen-network">
<option value="regtest" selected>Regtest</option>
<option value="testnet">Testnet</option>
<option value="signet">Signet</option>
<option value="mainnet">Mainnet</option>
</select>
</div>
<button id="btn-generate" disabled>Generate Keys</button>
</div>
<div>
<label for="wallet-data">WalletData JSON</label>
<textarea id="wallet-data" rows="6" placeholder='{"dataDir": ":memory:", "bitcoinNetwork": "Regtest", ...}'></textarea>
</div>
<div class="btn-group">
<button id="btn-create-wallet" disabled>Create Wallet</button>
</div>
<div class="output" id="setup-output" style="display:none;">
<div class="output-label">Result <button class="copy-btn" onclick="copyOutput('setup-result')">Copy</button></div>
<pre id="setup-result"></pre>
</div>
</div>
<!-- ================================================================== -->
<!-- Local Backup -->
<!-- ================================================================== -->
<div class="section">
<h2>Local Backup</h2>
<p class="hint">
Encrypt wallet state and download as a file, or restore from a previously downloaded backup.
</p>
<div>
<label for="backup-password">Backup Password</label>
<input id="backup-password" type="password" placeholder="Enter encryption password" />
</div>
<div class="btn-group">
<button id="btn-backup" disabled>Backup (Download)</button>
<button id="btn-backup-info" class="secondary" disabled>Check Backup Status</button>
</div>
<hr class="divider" />
<h3>Restore from File</h3>
<div>
<label for="restore-file">Backup File</label>
<input id="restore-file" type="file" accept=".rgbbackup,.bin,*/*" />
</div>
<div>
<label for="restore-password">Restore Password</label>
<input id="restore-password" type="password" placeholder="Password used when backup was created" />
</div>
<div class="btn-group">
<button id="btn-restore-backup" disabled>Restore Backup</button>
</div>
<div class="output" id="backup-output" style="display:none;">
<div class="output-label">Result <button class="copy-btn" onclick="copyOutput('backup-result')">Copy</button></div>
<pre id="backup-result"></pre>
</div>
</div>
<!-- ================================================================== -->
<!-- VSS Cloud Backup -->
<!-- ================================================================== -->
<div class="section">
<h2>VSS Cloud Backup</h2>
<p class="hint">
Configure a Versioned Secret Storage server for cloud-based encrypted backups.
</p>
<div>
<label for="vss-url">Server URL</label>
<input id="vss-url" type="text" value="http://127.0.0.1:8082/vss" />
</div>
<div>
<label for="vss-store-id">Store ID</label>
<input id="vss-store-id" type="text" placeholder="my-wallet-store" />
</div>
<div>
<label for="vss-signing-key">Signing Key (32-byte hex)</label>
<input id="vss-signing-key" type="text" placeholder="64 hex characters" />
</div>
<div class="btn-group">
<button id="btn-vss-configure" disabled>Configure VSS</button>
<button id="btn-vss-disable" class="danger" disabled>Disable VSS</button>
</div>
<hr class="divider" />
<div class="btn-group">
<button id="btn-vss-backup" class="accent" disabled>VSS Backup</button>
<button id="btn-vss-restore" class="accent" disabled>VSS Restore</button>
<button id="btn-vss-info" class="secondary" disabled>VSS Info</button>
</div>
<div class="output" id="vss-output" style="display:none;">
<div class="output-label">Result <button class="copy-btn" onclick="copyOutput('vss-result')">Copy</button></div>
<pre id="vss-result"></pre>
</div>
</div>
<!-- ================================================================== -->
<!-- Check Proxy URL -->
<!-- ================================================================== -->
<div class="section">
<h2>Check Proxy URL</h2>
<p class="hint">
Verify that a URL points to a valid RGB proxy server.
</p>
<div class="field-row">
<div>
<label for="proxy-url">Proxy URL</label>
<input id="proxy-url" type="text" value="http://127.0.0.1:3000/json-rpc" />
</div>
<button id="btn-check-proxy" disabled>Check Proxy</button>
</div>
<div class="output" id="proxy-output" style="display:none;">
<div class="output-label">Result <button class="copy-btn" onclick="copyOutput('proxy-result')">Copy</button></div>
<pre id="proxy-result"></pre>
</div>
</div>
<!-- ================================================================== -->
<!-- Validate Consignment Offchain -->
<!-- ================================================================== -->
<div class="section">
<h2>Validate Consignment Offchain</h2>
<p class="hint">
Validate an RGB consignment using witness data bundled inside it (works before the TX is broadcast).
Upload a strict-encoded consignment file and provide the witness transaction ID.
</p>
<div class="field-row">
<div>
<label for="consignment-file">Consignment file (strict-encoded binary)</label>
<input id="consignment-file" type="file" />
</div>
</div>
<div class="field-row">
<div>
<label for="consignment-txid">Witness Transaction ID</label>
<input id="consignment-txid" type="text" placeholder="e.g. abcd1234..." />
</div>
<div>
<label for="consignment-network">Network</label>
<select id="consignment-network">
<option value="Regtest" selected>Regtest</option>
<option value="Testnet">Testnet</option>
<option value="Signet">Signet</option>
<option value="Mainnet">Mainnet</option>
</select>
</div>
<button id="btn-validate-consignment" disabled>Validate</button>
</div>
<div class="output" id="consignment-output" style="display:none;">
<div class="output-label">Result <button class="copy-btn" onclick="copyOutput('consignment-result')">Copy</button></div>
<pre id="consignment-result"></pre>
</div>
</div>
<!-- ================================================================== -->
<!-- Error Handling Tests -->
<!-- ================================================================== -->
<div class="section">
<h2>Error Handling Tests</h2>
<p class="hint">
Runs a suite of tests to verify that invalid inputs are properly rejected and round-trip verification works across all networks.
</p>
<div class="btn-group">
<button id="btn-error-tests" disabled>Run Error Tests</button>
</div>
<div class="output" id="error-output" style="display:none;">
<div class="output-label">Result <button class="copy-btn" onclick="copyOutput('error-result')">Copy</button></div>
<pre id="error-result"></pre>
</div>
</div>
<!-- ================================================================== -->
<!-- Activity Log -->
<!-- ================================================================== -->
<div class="section">
<h2>Activity Log</h2>
<pre id="log"></pre>
</div>
<!-- ================================================================== -->
<!-- Script -->
<!-- ================================================================== -->
<script type="module">
import {
initWasm, state, log, json,
showResult, showJson, showError,
requireWallet, enableBtns, copyOutput,
generateKeys, restoreKeys, checkProxyUrl,
validateConsignmentOffchain,
WasmWallet,
fillWalletData,
PROXY_URL, VSS_URL,
} from './lib/shared.js';
// -----------------------------------------------------------------
// WASM Init
// -----------------------------------------------------------------
const ok = await initWasm();
if (ok) {
enableBtns([
'btn-auto-setup', 'btn-generate', 'btn-create-wallet',
'btn-check-proxy', 'btn-validate-consignment',
'btn-error-tests',
]);
}
// Buttons that require a wallet to be created
const walletBtns = [
'btn-backup', 'btn-backup-info', 'btn-restore-backup',
'btn-vss-configure', 'btn-vss-disable',
'btn-vss-backup', 'btn-vss-restore', 'btn-vss-info',
];
// -----------------------------------------------------------------
// Wallet Setup — Auto
// -----------------------------------------------------------------
document.getElementById('btn-auto-setup').addEventListener('click', async () => {
try {
log('Auto-setup: generating keys...', 'log-info');
const keys = generateKeys('regtest');
const wd = fillWalletData(keys, 'regtest');
log('Auto-setup: creating wallet...', 'log-info');
state.wallet = await WasmWallet.create(JSON.stringify(wd));
const walletData = state.wallet.getWalletData();
showResult('setup-output', 'setup-result',
'Wallet created (auto-setup)\n\nMnemonic: ' + keys.mnemonic + '\n\n' + json(walletData));
enableBtns(walletBtns);
log('Auto-setup complete', 'log-ok');
} catch (e) {
showError('setup-output', 'setup-result', 'Auto-setup failed', e);
}
});
// -----------------------------------------------------------------
// Wallet Setup — Manual: Generate Keys
// -----------------------------------------------------------------
document.getElementById('btn-generate').addEventListener('click', () => {
const network = document.getElementById('gen-network').value;
try {
const keys = generateKeys(network);
fillWalletData(keys, network);
showResult('setup-output', 'setup-result',
'Keys generated for ' + network + '\n\nMnemonic: ' + keys.mnemonic + '\n\nWalletData JSON filled below.');
log('generateKeys(' + network + ') OK', 'log-ok');
} catch (e) {
showError('setup-output', 'setup-result', 'Generate keys failed', e);
}
});
// -----------------------------------------------------------------
// Wallet Setup — Manual: Create Wallet
// -----------------------------------------------------------------
document.getElementById('btn-create-wallet').addEventListener('click', async () => {
const walletDataJson = document.getElementById('wallet-data').value.trim();
if (!walletDataJson) {
showResult('setup-output', 'setup-result', 'Enter WalletData JSON or generate keys first');
return;
}
try {
log('Creating wallet...', 'log-info');
state.wallet = await WasmWallet.create(walletDataJson);
const wd = state.wallet.getWalletData();
showResult('setup-output', 'setup-result',
'Wallet created successfully\n\n' + json(wd));
enableBtns(walletBtns);
log('Wallet created', 'log-ok');
} catch (e) {
showError('setup-output', 'setup-result', 'Wallet creation failed', e);
state.wallet = null;
}
});
// -----------------------------------------------------------------
// Local Backup — Download
// -----------------------------------------------------------------
document.getElementById('btn-backup').addEventListener('click', () => {
if (!requireWallet('backup-output', 'backup-result')) return;
const password = document.getElementById('backup-password').value;
if (!password) {
showResult('backup-output', 'backup-result', 'Please enter a backup password');
log('Backup: no password provided', 'log-warn');
return;
}
try {
log('Creating backup...', 'log-info');
const bytes = state.wallet.backup(password);
const blob = new Blob([bytes], { type: 'application/octet-stream' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = 'rgb-wallet-backup-' + Date.now() + '.rgbbackup';
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
showResult('backup-output', 'backup-result',
'Backup downloaded (' + bytes.length + ' bytes)');
log('Backup created and downloaded (' + bytes.length + ' bytes)', 'log-ok');
} catch (e) {
showError('backup-output', 'backup-result', 'Backup failed', e);
}
});
// -----------------------------------------------------------------
// Local Backup — Check Status
// -----------------------------------------------------------------
document.getElementById('btn-backup-info').addEventListener('click', () => {
if (!requireWallet('backup-output', 'backup-result')) return;
try {
const needsBackup = state.wallet.backupInfo();
const msg = needsBackup
? 'Backup required: wallet has been modified since last backup'
: 'Backup up to date: no changes since last backup';
showResult('backup-output', 'backup-result', msg);
log('backupInfo: needs_backup=' + needsBackup, 'log-ok');
} catch (e) {
showError('backup-output', 'backup-result', 'Backup info failed', e);
}
});
// -----------------------------------------------------------------
// Local Backup — Restore from File
// -----------------------------------------------------------------
document.getElementById('btn-restore-backup').addEventListener('click', async () => {
if (!requireWallet('backup-output', 'backup-result')) return;
const fileInput = document.getElementById('restore-file');
const password = document.getElementById('restore-password').value;
if (!fileInput.files || fileInput.files.length === 0) {
showResult('backup-output', 'backup-result', 'Please select a backup file');
log('Restore: no file selected', 'log-warn');
return;
}
if (!password) {
showResult('backup-output', 'backup-result', 'Please enter the restore password');
log('Restore: no password provided', 'log-warn');
return;
}
try {
log('Reading backup file...', 'log-info');
const file = fileInput.files[0];
const arrayBuffer = await file.arrayBuffer();
const bytes = new Uint8Array(arrayBuffer);
log('Restoring backup (' + bytes.length + ' bytes)...', 'log-info');
state.wallet.restoreBackup(bytes, password);
showResult('backup-output', 'backup-result',
'Backup restored successfully from "' + file.name + '" (' + bytes.length + ' bytes)');
log('Backup restored from "' + file.name + '"', 'log-ok');
} catch (e) {
showError('backup-output', 'backup-result', 'Restore failed', e);
}
});
// -----------------------------------------------------------------
// VSS — Configure
// -----------------------------------------------------------------
document.getElementById('btn-vss-configure').addEventListener('click', () => {
if (!requireWallet('vss-output', 'vss-result')) return;
const url = document.getElementById('vss-url').value.trim();
const storeId = document.getElementById('vss-store-id').value.trim();
const signingKey = document.getElementById('vss-signing-key').value.trim();
if (!url || !storeId || !signingKey) {
showResult('vss-output', 'vss-result', 'Please fill in all VSS fields (URL, Store ID, Signing Key)');
log('VSS configure: missing fields', 'log-warn');
return;
}
try {
state.wallet.configureVssBackup(url, storeId, signingKey);
showResult('vss-output', 'vss-result',
'VSS backup configured\n Server: ' + url + '\n Store ID: ' + storeId);
log('VSS configured: ' + url + ' / ' + storeId, 'log-ok');
} catch (e) {
showError('vss-output', 'vss-result', 'VSS configure failed', e);
}
});
// -----------------------------------------------------------------
// VSS — Disable
// -----------------------------------------------------------------
document.getElementById('btn-vss-disable').addEventListener('click', () => {
if (!requireWallet('vss-output', 'vss-result')) return;
state.wallet.disableVssBackup();
showResult('vss-output', 'vss-result', 'VSS backup disabled');
log('VSS backup disabled', 'log-ok');
});
// -----------------------------------------------------------------
// VSS — Backup
// -----------------------------------------------------------------
document.getElementById('btn-vss-backup').addEventListener('click', async () => {
if (!requireWallet('vss-output', 'vss-result')) return;
try {
log('VSS backup in progress...', 'log-info');
const version = await state.wallet.vssBackup();
showResult('vss-output', 'vss-result',
'VSS backup complete\n Server version: ' + version);
log('VSS backup OK, version=' + version, 'log-ok');
} catch (e) {
showError('vss-output', 'vss-result', 'VSS backup failed', e);
}
});
// -----------------------------------------------------------------
// VSS — Restore
// -----------------------------------------------------------------
document.getElementById('btn-vss-restore').addEventListener('click', async () => {
if (!requireWallet('vss-output', 'vss-result')) return;
try {
log('VSS restore in progress...', 'log-info');
await state.wallet.vssRestoreBackup();
showResult('vss-output', 'vss-result', 'VSS restore complete');
log('VSS restore OK', 'log-ok');
} catch (e) {
showError('vss-output', 'vss-result', 'VSS restore failed', e);
}
});
// -----------------------------------------------------------------
// VSS — Info
// -----------------------------------------------------------------
document.getElementById('btn-vss-info').addEventListener('click', async () => {
if (!requireWallet('vss-output', 'vss-result')) return;
try {
log('Querying VSS info...', 'log-info');
const info = await state.wallet.vssBackupInfo();
showJson('vss-output', 'vss-result', info);
log('VSS info retrieved', 'log-ok');
} catch (e) {
showError('vss-output', 'vss-result', 'VSS info failed', e);
}
});
// -----------------------------------------------------------------
// Check Proxy URL
// -----------------------------------------------------------------
document.getElementById('btn-check-proxy').addEventListener('click', async () => {
const url = document.getElementById('proxy-url').value.trim();
if (!url) {
showResult('proxy-output', 'proxy-result', 'Please enter a proxy URL');
return;
}
try {
log('Checking proxy: ' + url + '...', 'log-info');
await checkProxyUrl(url);
showResult('proxy-output', 'proxy-result',
'Proxy URL is valid: ' + url);
log('checkProxyUrl OK: ' + url, 'log-ok');
} catch (e) {
showError('proxy-output', 'proxy-result', 'Proxy check failed', e);
}
});
// -----------------------------------------------------------------
// Validate Consignment Offchain
// -----------------------------------------------------------------
document.getElementById('btn-validate-consignment').addEventListener('click', async () => {
const fileInput = document.getElementById('consignment-file');
const txid = document.getElementById('consignment-txid').value.trim();
const network = document.getElementById('consignment-network').value;
if (!fileInput.files || fileInput.files.length === 0) {
showResult('consignment-output', 'consignment-result', 'Please select a consignment file');
return;
}
if (!txid) {
showResult('consignment-output', 'consignment-result', 'Please enter a witness transaction ID');
return;
}
try {
log('Reading consignment file...', 'log-info');
const file = fileInput.files[0];
const arrayBuffer = await file.arrayBuffer();
const bytes = new Uint8Array(arrayBuffer);
log('Validating consignment (' + bytes.length + ' bytes, txid=' + txid + ')...', 'log-info');
const result = validateConsignmentOffchain(bytes, txid, network);
showJson('consignment-output', 'consignment-result', result);
if (result.valid) {
log('Consignment is VALID', 'log-ok');
} else {
log('Consignment is INVALID: ' + result.error, 'log-warn');
}
} catch (e) {
showError('consignment-output', 'consignment-result', 'Validation failed', e);
}
});
// -----------------------------------------------------------------
// Error Handling Tests
// -----------------------------------------------------------------
document.getElementById('btn-error-tests').addEventListener('click', () => {
const lines = [];
let passCount = 0;
let failCount = 0;
function record(pass, label, detail) {
const tag = pass ? '[PASS]' : '[FAIL]';
lines.push(tag + ' ' + label + (detail ? ' -- ' + detail : ''));
if (pass) passCount++; else failCount++;
}
// Test 1: invalid network in generateKeys
try {
generateKeys('invalid_network');
record(false, 'generateKeys("invalid_network")', 'expected error but succeeded');
} catch (e) {
record(true, 'generateKeys("invalid_network")', 'rejected: ' + e);
}
// Test 2: bad mnemonic in restoreKeys
try {
restoreKeys('regtest', 'not a valid mnemonic phrase at all');
record(false, 'restoreKeys(bad mnemonic)', 'expected error but succeeded');
} catch (e) {
record(true, 'restoreKeys(bad mnemonic)', 'rejected: ' + e);
}
// Test 3: empty mnemonic in restoreKeys
try {
restoreKeys('regtest', '');
record(false, 'restoreKeys(empty mnemonic)', 'expected error but succeeded');
} catch (e) {
record(true, 'restoreKeys(empty mnemonic)', 'rejected: ' + e);
}
// Test 4: round-trip verification for all networks
const networks = ['regtest', 'testnet', 'signet', 'mainnet'];
for (const net of networks) {
try {
const generated = generateKeys(net);
const restored = restoreKeys(net, generated.mnemonic);
const fields = Object.keys(generated);
let allMatch = true;
for (const field of fields) {
if (JSON.stringify(generated[field]) !== JSON.stringify(restored[field])) {
allMatch = false;
break;
}
}
record(allMatch, 'round-trip ' + net,
allMatch ? 'all fields match' : 'mismatch detected');
} catch (e) {
record(false, 'round-trip ' + net, 'error: ' + e);
}
}
// Summary
lines.push('');
lines.push('Summary: ' + passCount + ' passed, ' + failCount + ' failed, ' + (passCount + failCount) + ' total');
showResult('error-output', 'error-result', lines.join('\n'));
const allPassed = failCount === 0;
log('Error tests: ' + passCount + '/' + (passCount + failCount) + ' passed',
allPassed ? 'log-ok' : 'log-err');
});
</script>
</body>
</html>