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
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
|
// Ark feature — off-chain payments via an ASP (Second's bark/captaind
// protocol, spoken natively by ../ark). Receive address + boarding, instant
// sends, history entries, refresh/consolidation, server settings.
import * as btc from '@scure/btc-signer';
import { HDKey } from '@scure/bip32';
import { hex, base32nopad, bech32 } from '@scure/base';
import { sha256 } from '@noble/hashes/sha256';
import { ArkManager } from '../ark/manager.js';
import { loadBg, saveBg, buildBg } from '../nwc-bg.js';
import { boardFee } from '../ark/board.js';
import { maybeBolt11, lnSendFee } from '../ark/lightning.js';
import { decodeVtxo, getVtxoStatus, VTXO_STATE_SPENT, concatBytes } from '../ark/proto.js';
import { signedExitTxs, buildBumpChild, buildExitClaim, submitPackage } from '../ark/exit.js';
import { utxoId } from '../wallet.js';
import {
getNetwork, setNetwork, arkPresets, getArkProviderId, setArkProviderId,
getArkCustom, setArkCustom, getArkConfig,
} from '../api.js';
import { t } from '../i18n.js';
import { qrSvg } from '../qr.js';
import { shortAddr, shortTxid, timeAgo, ARK_ICON, ARK_MARK } from '../format.js';
// t?ark1… bech32m — an Ark address for this or another ASP.
export function isArkAddress(a) { return /^t?ark1[a-z0-9]{20,}$/i.test((a || '').trim()); }
// Wallet storage/key helpers for this feature, installed onto the core
// wallet instance so a build without the feature ships none of it.
export function installArkWallet(wallet) {
if (wallet.loadArkState) return; // already installed
Object.assign(wallet, {
// ---- Ark support -------------------------------------------------------
// Persisted ArkManager state (vtxos, in-flight action checkpoints, movement
// history — no secrets: vtxo keys are re-derived from the seed).
// Ark state is per WALLET *and* per ASP: vtxos are cosigned by one
// specific server, so showing another server's coins (or resuming its
// in-flight board) is meaningless and alarming. Switching providers must
// therefore switch state, and switching back must restore it.
_arkNs(url) {
// short, stable id for an ASP endpoint
let h = 5381;
for (const c of String(url || '')) h = ((h * 33) ^ c.charCodeAt(0)) >>> 0;
return h.toString(36);
},
_arkKey(url) { return this._cacheKey() + ':ark:' + this._arkNs(url); },
_arkLegacyKey() { return this._cacheKey() + ':ark'; },
// Who really cosigned this state's coins? A vtxo names its server in its
// own bytes, which is cryptographic fact — unlike the stamp, which is
// just something we wrote and could have written wrongly.
_arkVtxoOwner(st) {
const v = (st?.vtxos || []).find((x) => x.bytes);
if (!v) return null;
try { return decodeVtxo(hex.decode(v.bytes)).serverPubkey; } catch { return null; }
},
// Park state under the server that actually owns it, until the URL for
// that server is selected and can adopt it.
_arkHoldKey(pubkey) { return this._cacheKey() + ':ark:pk:' + String(pubkey).slice(0, 16); },
loadArkState(url) {
// NB deliberately no fallback to the legacy key: the UI renders from
// this before any server is contacted, so returning another ASP's state
// here is exactly how you get a phantom balance. Pre-namespacing state
// is claimed via adoptArkState() once we know who cosigned it.
try {
const cur = localStorage.getItem(this._arkKey(url));
if (!cur) return null;
const st = JSON.parse(cur);
// Self-repair: an earlier build could stamp state with whichever
// server happened to be connected, filing one ASP's coins under
// another. The vtxos themselves settle it — if they disagree with the
// stamp, this state is misfiled. Move it to its real owner and show
// nothing here.
const owner = this._arkVtxoOwner(st);
if (owner && st.serverPubkey && owner !== st.serverPubkey) {
st.serverPubkey = owner;
try {
localStorage.setItem(this._arkHoldKey(owner), JSON.stringify(st));
localStorage.removeItem(this._arkKey(url));
} catch {}
return null;
}
return st;
} catch { return null; }
},
// Offer up pre-namespacing state, but only to the ASP that actually
// cosigned it. Its owner is read from the state's own stamp, or failing
// that decoded straight out of a vtxo — the vtxo wire format carries the
// server pubkey, so this needs no network call and cannot guess wrong.
adoptArkState(serverPubkey, url) {
try {
// state parked by the self-repair above belongs to exactly one server
const held = localStorage.getItem(this._arkHoldKey(serverPubkey));
if (held) {
const st = JSON.parse(held);
this.saveArkState(st, url);
localStorage.removeItem(this._arkHoldKey(serverPubkey));
return st;
}
const raw = localStorage.getItem(this._arkLegacyKey());
if (!raw) return null;
const st = JSON.parse(raw);
let owner = st.serverPubkey || null;
if (!owner) {
const v = (st.vtxos || []).find((x) => x.bytes);
if (v) { try { owner = decodeVtxo(hex.decode(v.bytes)).serverPubkey; } catch {} }
}
if (owner && owner !== serverPubkey) return null; // belongs to another ASP
if (!owner) return null; // can't prove ownership: leave it
st.serverPubkey = owner;
this.saveArkState(st, url);
localStorage.removeItem(this._arkLegacyKey()); // now safely namespaced
return st;
} catch { return null; }
},
saveArkState(state, url) {
try { localStorage.setItem(this._arkKey(url), JSON.stringify(state)); } catch {}
},
});
}
// A slim projection of ark state for the sync snapshot — the sync relay caps
// events at 64 KB and the full state (deep-genesis vtxo bytes + done-action
// hex) blew past it, so publishes silently failed and nothing synced. Another
// device needs, per vtxo: id/amount/state (+ bytes ONLY for spendable/pending,
// which it might spend/exit); the movement log (capped) for ark history; and
// done board/offboard/exit actions (stripped of hex) so on-chain rows still
// label. In-flight actions and their signing material stay device-local.
export function slimArkForSync(s) {
if (!s) return null;
const HEAVY = ['bytes', 'destBytes', 'changeBytes', 'vtxoBytes', 'txHex', 'fundingTxHex', 'outputVtxos'];
return {
v: s.v, serverPubkey: s.serverPubkey, mailboxCheckpoint: s.mailboxCheckpoint, nextKeyIndex: s.nextKeyIndex, receiveAckTs: s.receiveAckTs,
// Spent stubs and done actions are pure history and grow forever — cap
// them (newest kept) so the sync event's size is bounded for life.
vtxos: (() => {
const live = (s.vtxos || []).filter((v) => v.state !== 'spent');
const stubs = (s.vtxos || []).filter((v) => v.state === 'spent').slice(-200)
.map((v) => ({ id: v.id, amountSat: v.amountSat, state: 'spent', keyIndex: v.keyIndex, expiryHeight: v.expiryHeight }));
return [...live, ...stubs];
})(),
actions: (s.actions || [])
.filter((a) => a.step === 'done' && ['board', 'offboard', 'exit'].includes(a.type))
.slice(-50)
.map((a) => { const c = { ...a }; for (const k of HEAVY) delete c[k]; return c; }),
movements: (s.movements || []).slice(-200).map((m, i, arr) =>
// Older rows only need to render a history line; the bolt11 (~400 chars
// each) and preimage are what pushed snapshots past relay size limits.
(arr.length - i <= 50 ? m : (({ invoice, preimage, ...rest }) => rest)(m))),
gifts: s.gifts,
};
}
// Merge two ark states so devices can't clobber each other through the sync
// snapshot: additive union of vtxos (a device keeps its own full copy, gains
// ones it lacks), actions/movements/gifts union by id, counters take the max
// (so two devices never reuse a key index).
export function mergeArkStates(a, b) {
if (!a) return b;
if (!b) return a;
// never fuse state from two different ASPs
if (a.serverPubkey && b.serverPubkey && a.serverPubkey !== b.serverPubkey) return a;
const out = { ...a };
// ADDITIVE union only: bring in vtxos this device is missing (the whole
// point — board/change coins that live only where they were created), but
// NEVER let a remote snapshot change the state of a vtxo we already track.
// Spend detection is each device's own job via reconcile() against the
// server (the authority), so a stale-or-wrong "spent" on one device can't
// propagate and permanently poison the others. A newly-merged-in vtxo that
// was actually spent elsewhere gets caught by the reconcile on next connect
// / send-intent, exactly like any other drift.
const vtxos = new Map((a.vtxos || []).map((v) => [v.id, { ...v }]));
for (const rv of b.vtxos || []) if (!vtxos.has(rv.id)) vtxos.set(rv.id, { ...rv });
out.vtxos = [...vtxos.values()];
const unionById = (x = [], y = []) => {
const ids = new Set(x.map((i) => i.id));
return [...x, ...y.filter((i) => !ids.has(i.id))];
};
out.actions = unionById(a.actions, b.actions);
out.movements = unionById(a.movements, b.movements).sort((m, n) => (m.ts || 0) - (n.ts || 0));
const gifts = new Map((a.gifts || []).map((g) => [g.id, { ...g }]));
for (const rg of b.gifts || []) {
const lg = gifts.get(rg.id);
if (!lg) gifts.set(rg.id, { ...rg });
else { lg.claimed = lg.claimed || rg.claimed; lg.revoked = lg.revoked || rg.revoked; }
}
out.gifts = [...gifts.values()];
out.mailboxCheckpoint = Math.max(a.mailboxCheckpoint || 0, b.mailboxCheckpoint || 0);
out.nextKeyIndex = Math.max(a.nextKeyIndex || 1, b.nextKeyIndex || 1);
out.nextLnRecvIndex = Math.max(a.nextLnRecvIndex || 0, b.nextLnRecvIndex || 0);
out.receiveAckTs = Math.max(a.receiveAckTs || 0, b.receiveAckTs || 0);
out.serverPubkey = a.serverPubkey || b.serverPubkey || null;
return out;
}
export function arkFeature(ctx) {
const { h, ui, render, wallet, blankSend, fmtAmount, unitLabel, unitTag, copyBtn, toast, openExternal } = ctx;
installArkWallet(wallet); // ark state storage lives outside the core wallet
// Ride the encrypted sync snapshot so ark funds follow the seed across
// devices. Board/change vtxo bytes exist only where they were created — a
// second device can't otherwise see them. All devices share ONE replaceable
// event slot (same seed -> same nostr key), so a naive publish can clobber
// another device's coins. Anti-entropy fixes that: on receiving a snapshot,
// union it with local; if it was missing coins we know (or brought coins we
// didn't), re-publish the superset. The slot converges to the union no
// matter who publishes when.
wallet.registerCacheExtension({
domain: 'ark', // published as its own sync slot — see splitSnapshotDomains
mergeAlways: true, // load() is a commutative merge — apply older snapshots too
save: () => {
const cfg = getArkConfig();
const s = cfg && wallet.loadArkState(cfg.ark);
// tag the snapshot with the ASP it belongs to (see load())
return s ? { arkState: slimArkForSync(s), arkServer: wallet._arkNs(cfg.ark) } : {};
},
load: (d) => {
if (!d.arkState) return;
const cfg = getArkConfig();
if (!cfg) return;
if (d.arkServer && d.arkServer !== wallet._arkNs(cfg.ark)) return;
// A snapshot from an older build carries no tag, so the tag alone can't
// be trusted — ask the vtxos who cosigned them. If they belong to a
// different ASP (or we can't yet prove they belong to this one), park
// the snapshot under its real owner instead of merging it in. Merging
// blind is exactly how another server's balance appears here.
const owner = d.arkState.serverPubkey || wallet._arkVtxoOwner(d.arkState);
const mine = (ark && ark.info && ark.info.serverPubkey)
|| (wallet.loadArkState(cfg.ark) || {}).serverPubkey || null;
if (owner && owner !== mine) {
try {
localStorage.setItem(wallet._arkHoldKey(owner),
JSON.stringify({ ...d.arkState, serverPubkey: owner }));
} catch {}
// `mine` is null on a FRESH restore (no local state, not yet
// connected), so even our own snapshot lands in the hold. Connecting
// is what resolves it: the manager learns the server's pubkey and
// adopts the held state if it matches — a seed imported on a new
// device would otherwise show no ark balance until some manual poke.
if (!mine) setTimeout(() => maybeInitArk(), 0);
return;
}
const local = wallet.loadArkState(cfg.ark);
const merged = mergeArkStates(local, d.arkState);
wallet.saveArkState(merged, cfg.ark);
const mergedN = (merged.vtxos || []).length;
const remoteN = (d.arkState.vtxos || []).length;
const localN = (local && local.vtxos || []).length;
// We know vtxos this snapshot lacked — push our superset back up so the
// sender (and the shared slot) learns them. Guarded by mergedN>remoteN so
// it can't loop once every device has converged to the union.
if (mergedN > remoteN) { try { wallet.saveCache(); } catch {} }
// The merge brought vtxos our live manager doesn't have — (re)connect so
// the balance actually shows.
const novel = mergedN > localN || (mergedN && (!ark || !ark.state));
if (mergedN && novel) setTimeout(() => maybeInitArk(), 0);
},
});
// One manager per open wallet; null when Ark is off in Settings, watch-only,
// or not yet dialed (lazy: fresh wallets connect on first use).
let ark = null;
let arkTimer = null;
let arkConnectPromise = null;
let arkInitGen = 0; // guards against a stale init() resolving after a wallet switch
function stopArk() {
arkInitGen++;
if (arkTimer) clearInterval(arkTimer);
arkTimer = null;
if (ark) ark.stopMailboxStream();
stopNwcFunding();
for (const timer of arkLnTimers.values()) clearInterval(timer);
arkLnTimers.clear();
ark = null;
arkConnectPromise = null;
}
function arkAvailable() {
return !!getArkConfig() && !wallet.watchOnly && !!wallet.account;
}
function arkWanted() {
const cfg = getArkConfig();
const st = cfg && wallet.loadArkState(cfg.ark);
return !!(st && ((st.vtxos || []).length || (st.movements || []).length
|| (st.actions || []).some((a) => !['done', 'failed'].includes(a.step))));
}
function initArk() {
stopArk();
ui.arkError = '';
lastAutoInit = Date.now();
if (arkAvailable() && arkWanted()) connectArk().catch(() => {});
}
// Automatic re-inits (a synced snapshot arriving) must not restart a
// connection that is already in flight, or retry in a tight loop when the
// server is unreachable: each one calls stopArk() first, so the settings
// card's "connecting" indicator flickers on every snapshot. User-driven
// calls to initArk() stay immediate.
let lastAutoInit = 0;
function maybeInitArk() {
if (ark || arkConnectPromise) return;
if (Date.now() - lastAutoInit < 20000) return;
initArk();
}
function connectArk() {
if (ark) return Promise.resolve(ark);
if (arkConnectPromise) return arkConnectPromise;
if (!arkAvailable()) return Promise.reject(new Error(t('arkNotConnected')));
const cfg = getArkConfig();
const gen = arkInitGen;
const mgr = new ArkManager({
account: wallet.account(),
storage: {
load: () => wallet.loadArkState(cfg.ark),
adopt: (serverPubkey) => wallet.adoptArkState(serverPubkey, cfg.ark),
// merge-on-save: the MANAGER is authoritative for the state of the
// vtxos it tracks (its reconcile/spends must persist), so its state
// is the first arg and wins; storage only contributes vtxos the
// manager hasn't loaded yet (a snapshot merged in from another device
// between init and the next reinit). saveCache() then carries ark
// state into the snapshot -> debounced nostr publish.
save: (s) => {
wallet.saveArkState(mergeArkStates(s, wallet.loadArkState(cfg.ark)), cfg.ark);
try { wallet.saveCache(); } catch {}
},
},
arkUrl: cfg.ark,
esploraUrl: cfg.esplora,
network: getNetwork(),
onUpdate: () => render(),
});
ui.arkError = '';
arkConnectPromise = mgr.init().then(() => {
if (gen !== arkInitGen) throw new Error('superseded'); // wallet switched mid-connect
ark = mgr;
announceArkAddress(mgr); // ark zaps: tell nostr where our mailbox lives
startNwcFunding(mgr); // NWC bridge: honor funding requests within the allowance
// Publish this device's ark state to its per-device sync slot on connect.
// Cross-device sync only re-published on a state CHANGE (a send/board), so
// an idle device never shared its coins — another device would never see
// them. Publishing on connect guarantees every device's ark balance
// reaches the relay (and gets merged) without needing a transaction first.
if (mgr.state && (mgr.state.vtxos || []).length) { try { wallet.saveCache(); } catch {} }
const tick = () => mgr.sync().catch(() => {}).then(() => driveExits(mgr)).catch(() => {});
tick();
// Reconcile once on connect: a vtxo synced in from another device (or
// one this device held while a spend happened elsewhere) is checked
// against the server, so a stale spendable is caught here rather than
// only at send time.
mgr.reconcile().catch(() => {});
mergeBgWorkerState(mgr).catch(() => {}); // absorb what the SW did while we were closed
// Receives arrive in real time over the mailbox stream; the poll is the
// fallback and what drives in-flight boards/refreshes forward.
mgr.startMailboxStream();
arkTimer = setInterval(() => { if (ark === mgr) tick(); }, getNetwork() === 'regtest' ? 5000 : 30000);
render();
return mgr;
}).catch((e) => {
if (gen === arkInitGen) { ui.arkError = e.message; render(); }
throw e;
}).finally(() => { arkConnectPromise = null; });
render();
return arkConnectPromise;
}
// The current ark state for READ-ONLY rendering: the live manager if it's
// connected, else the persisted state straight from storage. Lets the
// balance and history icons paint immediately on refresh instead of
// flickering (empty → populated) while the manager connects asynchronously.
function arkStateNow() {
if (ark && ark.state) return ark.state;
// Read persisted state whenever Ark is configured for this network — even
// watch-only (seed not loaded this session): the balance/history should
// show read-only, just like the on-chain balance does. Acting on it
// (send/exit) still requires the seed and is gated separately.
const cfg = getArkConfig();
if (!cfg) return null;
return wallet.loadArkState(cfg.ark);
}
function arkBalance() {
const s = arkStateNow();
if (!s) return null;
const sum = (st) => (s.vtxos || []).filter((v) => v.state === st).reduce((n, v) => n + v.amountSat, 0);
const boardingSat = (s.actions || [])
.filter((a) => a.type === 'board' && a.fundingTxid && !['done', 'failed'].includes(a.step))
.reduce((n, a) => n + (a.amountSat - a.feeSat), 0);
return { spendableSat: sum('spendable'), pendingSat: sum('pending'), boardingSat };
}
// An ark address in the send form signals a send is coming: verify our
// spendable vtxos against the server now, so a stale one (same seed active
// elsewhere, restored state) is dropped before coin selection instead of
// failing at cosign time. Throttled — this fires from the render path.
let arkReconciledAt = 0;
function maybeReconcile() {
if (Date.now() - arkReconciledAt < 30_000) return;
arkReconciledAt = Date.now();
connectArk().then((mgr) => mgr.reconcile()).catch(() => {});
}
function arkSendReview() {
if (ui.arkSent) {
return h(
'div',
{
class: 'card col',
style: 'align-items:center;text-align:center;gap:14px;cursor:pointer;padding:48px 20px',
onClick: () => { ui.arkSent = null; ui.send = blankSend(); render(); },
},
h('div', { class: 'check-badge' }, '✓'),
h('h2', { style: 'margin:0' }, t('arkSentTitle')),
h('div', { class: 'amount-neg', style: 'font-size:18px' }, '-' + fmtAmount(ui.arkSent.amountSat) + ' ' + unitLabel()),
h('div', { class: 'small muted' }, t('tapToProceed'))
);
}
const a = ui.arkSend;
const row = (k, v) => h('div', { class: 'row between' }, h('span', { class: 'small muted' }, k), h('span', { class: 'small' }, v));
return h(
'div',
{ class: 'card col', style: 'gap:12px' },
h('h3', {}, t('arkSendTitle')),
row(t('lnPayAmount'), fmtAmount(a.amountSat) + ' ' + unitLabel()),
row(t('arkPayTo'), shortAddr(a.address, 14)),
row(t('networkFee'), t('arkNoFee')),
ui.sendError ? h('div', { class: 'notice err' }, ui.sendError) : null,
h('div', { class: 'row gap6' },
h('button', { class: 'btn-ghost', onClick: () => { ui.arkSend = null; ui.sendError = ''; render(); } }, t('back')),
ui.busy
? h('button', { class: 'btn-primary grow', disabled: true }, h('span', { class: 'spinner' }))
: h('button', { class: 'btn-primary grow', onClick: doArkSend }, t('arkSendBtn'))
)
);
}
async function doArkSend() {
const a = ui.arkSend;
ui.busy = true; ui.sendError = ''; render();
try {
const mgr = await connectArk();
await mgr.send(a.address, a.amountSat);
ui.arkSent = { amountSat: a.amountSat };
ui.arkSend = null;
} catch (e) {
ui.sendError = e.message;
}
ui.busy = false; render();
}
// ---- lightning via the ASP (captaind pays/holds invoices natively) -------
// One poll timer per in-flight lightning action; cleared on settle.
const arkLnTimers = new Map();
function stopArkLnPoll(id) {
const timer = arkLnTimers.get(id);
if (timer) { clearInterval(timer); arkLnTimers.delete(id); }
}
function pollArkLn(id, onSettle) {
stopArkLnPoll(id);
arkLnTimers.set(id, setInterval(async () => {
if (!ark) return;
const a = await ark.driveLn(id).catch(() => null);
if (a && ['done', 'failed'].includes(a.step)) {
stopArkLnPoll(id);
onSettle(a);
}
render();
}, 2500));
}
// ---- background NWC mirror ---------------------------------------------
// The service worker answers wallet-connect requests while the app is
// closed, spending the SAME ark balance the app does. The mirror is the
// full manager state plus the chain-3 keys for its live coins and a window
// of upcoming change indices (the worker cannot derive). nwc.js decides
// WHEN to mirror (background answering on) and supplies the connections.
async function writeBgMirror(connections, offer) {
const cfg = getArkConfig();
if (!cfg || !ark || !ark.info || !ark.state) return;
const prev = await loadBg(wallet._cacheKey());
const rec = buildBg({
ark: { arkUrl: cfg.ark, esploraUrl: cfg.esplora, network: getNetwork(), serverPubkey: ark.info.serverPubkey },
mgrState: ark.state,
keyFor: (chain, i) => hex.encode(ark.account.deriveChild(chain).deriveChild(i).privateKey),
connections,
offer,
});
if (prev) rec.spends = prev.spends; // the worker's log survives rewrites
await saveBg(wallet._cacheKey(), rec);
}
// On connect, fold whatever the worker did while we were closed back into
// the live state: its spent flips and change vtxos merge in (the additive
// merge never resurrects anything reconcile() disproves), its key
// allocations advance ours, and its in-flight ln actions become resumable.
async function mergeBgWorkerState(mgr) {
const rec = await loadBg(wallet._cacheKey());
if (!rec) return;
if (rec.v < 3) {
await sweepLegacyPouch(rec).catch((e) => console.warn('ark: pouch sweep failed', e.message));
return;
}
if (!rec.mgr) return;
const merged = mergeArkStates(mgr.state, rec.mgr);
// the worker's view of OUR vtxos is newer for anything it spent
const workerState = new Map((rec.mgr.vtxos || []).map((v) => [v.id, v.state]));
for (const v of merged.vtxos) {
if (workerState.get(v.id) === 'spent') v.state = 'spent';
}
merged.nextKeyIndex = Math.max(merged.nextKeyIndex || 1, rec.mgr.nextKeyIndex || 1);
mgr.state = merged;
mgr._save();
await mgr.reconcile().catch(() => {});
await mgr.resumePending().catch(() => {});
}
// A leftover chain-6 pouch from before whole-balance background spending:
// sweep its coins home using the keys stored in the record itself, then
// let the next mirror write replace it.
async function sweepLegacyPouch(rec) {
if (!(rec.vtxos || []).some((v) => (v.state || 'spendable') === 'spendable')) return;
const cfg = getArkConfig();
const mgr = await connectArk();
const shim = { deriveChild: () => ({ deriveChild: (i) => ({ privateKey: hex.decode(rec.keys[String(i)] || rec.keys['0']) }) }) };
let state = {
v: 1, serverPubkey: rec.ark?.serverPubkey || null, mailboxCheckpoint: 0,
nextKeyIndex: rec.nextKeyIndex || 20,
vtxos: (rec.mgr?.vtxos || rec.vtxos || []).map((v) => ({ ...v, state: v.state || 'spendable' })),
actions: [], movements: [],
};
const pm = await new ArkManager({
account: shim,
storage: { load: () => state, save: (s2) => { state = s2; } },
arkUrl: cfg.ark, esploraUrl: cfg.esplora, network: getNetwork(),
}).init();
const dest = mgr.address();
for (let guard = 0; guard < 30; guard++) {
const v = (pm.state.vtxos || []).find((x) => x.state === 'spendable');
if (!v) break;
await pm.send(dest, v.amountSat);
}
await saveBg(wallet._cacheKey(), { ...rec, vtxos: [], mgr: state, sweptAt: Date.now() });
}
// Pay a bolt11 with no UI at all — used by the NWC wallet service. Same
// routing as the interactive path: the cheaper bridge first, the ASP's own
// lightning-send as fallback. Resolves { preimage, feeSat, amountSat }.
async function payInvoiceHeadless(invoice, { maxAmountSat } = {}) {
const dec = maybeBolt11(invoice);
if (!dec) throw new Error('not a bolt11 invoice');
if (!dec.amountSat) throw new Error('zero-amount invoices are not supported');
if (maxAmountSat && dec.amountSat > maxAmountSat) {
throw new Error(`amount ${dec.amountSat} exceeds the limit of ${maxAmountSat} sat`);
}
const mgr = await connectArk();
// The sync merge is additive, so a vtxo spent on another device can sit
// in local state looking spendable until someone checks. The interactive
// send path reconciles on intent; this headless path must too, or an NWC
// payment picks the stale coin and the ASP rejects it ("state: spent").
await mgr.reconcile().catch(() => {});
const id = await mgr.payLnInvoice(invoice);
for (let i = 0; i < 60; i++) {
const a = mgr.lnAction(id);
if (!a || a.step === 'done') break;
if (a.step === 'failed') throw new Error(a.error || 'payment failed');
await new Promise((r) => setTimeout(r, 2000));
await mgr.driveLn(id).catch(() => {});
}
const a = mgr.lnAction(id);
if (!a || a.step !== 'done') throw new Error(a?.error || a?.lastError || 'payment did not complete');
return { preimage: a.preimage, feeSat: a.feeSat, amountSat: a.amountSat };
}
// A bolt11 lands in Send (via the swaps feature's delegation hook, or
// directly when swaps is absent): take over when the ark balance can
// plausibly cover it, else decline so the Boltz path handles it.
function startArkLnPay(invoice, meta) {
const dec = maybeBolt11(invoice);
if (!dec || !arkAvailable() || wallet.watchOnly) return false;
const s = arkStateNow();
const covers = (state, sats) =>
!!s && (s.vtxos || []).some((v) => v.state === state && v.amountSat >= sats);
// pending funds count for the take-over decision: an in-flight payment or
// revocation frees them in seconds, and the Boltz fallback can't do small
// amounts at all — better to wait for ark than bounce off Boltz's minimum
if (!covers('spendable', dec.amountSat || 1) && !covers('pending', dec.amountSat || 1)) return false;
ui.arkLnPay = { invoice, meta: meta || null, amountSat: dec.amountSat, amount: '', feeSat: null, status: 'quote' };
ui.sendError = '';
render();
quoteArkLnPay(invoice, meta).catch((e) => {
if (ui.arkLnPay && ui.arkLnPay.invoice === invoice) { ui.sendError = e.message; render(); }
});
return true;
}
// Quote (or re-quote) an in-form lightning pay: smallest vtxo that covers
// amount + its expiry-based fee. Funds locked by a settling action retry
// themselves free; genuinely insufficient funds hand off to Boltz.
async function quoteArkLnPay(invoice, meta) {
const mgr = await connectArk();
const p = ui.arkLnPay;
if (!p || p.invoice !== invoice) return;
if (!p.amountSat) { p.status = 'ready'; render(); return; }
const tip = await mgr.chain.tipHeight();
const candidates = mgr.vtxos().filter((v) => v.state === 'spendable')
.sort((a, b) => a.amountSat - b.amountSat);
let fee = null;
for (const v of candidates) {
const f = lnSendFee(p.amountSat, mgr.info.lnSendFees, [v], tip);
if (v.amountSat >= p.amountSat + f) { fee = f; break; }
}
if (fee == null) {
const pendingCover = mgr.vtxos().some((v) => v.state === 'pending' && v.amountSat >= p.amountSat);
if (pendingCover) {
p.status = 'fundsPending';
render();
mgr.resumePending().catch(() => {}); // nudge whatever holds them
setTimeout(() => {
if (ui.arkLnPay === p) quoteArkLnPay(invoice, meta).catch(() => {});
}, 4000);
return;
}
// can't cover once fees are in
ui.arkLnPay = null;
ui.sendError = t('arkLnPayFailed');
render();
return;
}
p.feeSat = fee;
p.status = 'ready';
render();
}
async function doArkLnPay() {
const p = ui.arkLnPay;
const sats = p.amountSat || ctx.parseAmount(p.amount, ctx.getUnit());
if (!sats || sats <= 0) { ui.sendError = t('enterValidAmtForN', { n: 1 }); render(); return; }
ui.busy = true; ui.sendError = ''; render();
try {
const mgr = await connectArk();
const id = await mgr.payLnInvoice(p.invoice, { amountSat: sats });
const settle = (a) => {
if (a.step === 'done') {
ui.arkLnPaid = { amountSat: a.amountSat, meta: p.meta };
ui.arkLnPay = null;
} else if (ui.arkLnPay) {
ui.arkLnPay.status = 'ready';
ui.sendError = a.error === 'payment failed' ? t('arkLnRefunded') : (a.error || t('arkLnPayFailed'));
}
render();
};
const a = mgr.lnAction(id);
if (['done', 'failed'].includes(a.step)) settle(a);
else { p.actionId = id; p.status = 'paying'; pollArkLn(id, settle); }
} catch (e) {
ui.sendError = e.message;
}
ui.busy = false; render();
}
function arkLnPayView() {
const u = ' ' + unitLabel();
if (ui.arkLnPaid) {
const zap = ui.arkLnPaid.meta;
return h('div', {
class: 'card col',
style: 'align-items:center;text-align:center;gap:14px;cursor:pointer;padding:48px 20px',
onClick: () => { ui.arkLnPaid = null; ui.send = blankSend(); render(); },
},
h('div', { class: 'check-badge' }, '⚡'),
h('h2', { style: 'margin:0' }, zap ? t('arkZapSentTitle') : t('arkLnPaidTitle')),
zap && zap.name ? h('div', { class: 'small muted' }, zap.name) : null,
h('div', { class: 'amount-neg', style: 'font-size:18px' }, '-' + fmtAmount(ui.arkLnPaid.amountSat) + u),
h('div', { class: 'small muted' }, t('tapToProceed')));
}
const p = ui.arkLnPay;
if (!p) return null;
const zap = p.meta;
const row = (label, sats, bold) => h('div', { class: 'row between' + (bold ? '' : ''), style: bold ? 'font-weight:600' : '' },
h('span', { class: bold ? '' : 'small muted' }, label), h('span', {}, fmtAmount(sats) + u));
const total = (p.amountSat || 0) + (p.feeSat || 0);
return h('div', { class: 'col', style: 'gap:12px' },
h('div', { class: 'card col', style: 'gap:10px' },
h('h3', { style: 'margin:0' }, '⚡ ' + (zap ? t('lnZapReviewTitle') : t('lnPayTitle'))),
h('div', { class: 'small muted' }, t('arkLnPayVia')),
zap && (zap.name || zap.address) ? h('div', { class: 'small muted', style: 'word-break:break-all' }, zap.name || zap.address) : null,
zap && zap.comment ? h('div', { class: 'small faint', style: 'font-style:italic;word-break:break-word' }, '“' + zap.comment + '”') : null,
p.status === 'quote'
? h('div', { class: 'row gap6', style: 'align-items:center' }, h('span', { class: 'spinner sm' }), h('span', { class: 'small muted' }, t('lnQuoting')))
: null,
p.status === 'fundsPending'
? h('div', { class: 'row gap6', style: 'align-items:center' }, h('span', { class: 'spinner sm' }), h('span', { class: 'small muted' }, t('arkLnFundsPending')))
: null,
p.amountSat == null && p.status !== 'quote'
? h('div', { class: 'input-group' },
h('input', { type: 'number', min: '0', inputmode: 'decimal', placeholder: t('lnPayAmount'), value: p.amount,
onInput: (e) => { p.amount = e.target.value; } }),
h('div', { style: 'display:flex;align-items:center' }, unitTag()))
: null,
p.amountSat != null ? row(t('lnPayAmount'), p.amountSat) : null,
p.feeSat != null && p.feeSat > 0 ? row(t('arkLnFee'), p.feeSat) : null,
p.amountSat != null && p.feeSat != null
? [h('div', { style: 'border-top:1px solid var(--line, #ddd);margin:2px 0' }), row(t('lnPayTotal'), total, true)]
: null),
ui.sendError ? h('div', { class: 'notice err' }, ui.sendError) : null,
p.status === 'paying'
? h('div', { class: 'card row gap6', style: 'align-items:center' },
h('span', { class: 'spinner sm' }), h('span', { class: 'small muted' }, t('arkLnPaying')))
: h('button', { class: 'btn-primary btn-block', disabled: !!ui.busy || p.status !== 'ready', onClick: doArkLnPay },
ui.busy ? h('span', { class: 'spinner' }) : t('lnPayConfirm')),
p.status !== 'paying'
? h('button', { class: 'btn-ghost btn-block', onClick: () => { ui.arkLnPay = null; ui.sendError = ''; ui.send = blankSend(); render(); } }, t('back'))
: null);
}
async function doArkLnInvoice() {
const sats = parseInt((ui.arkLnRecvAmt || '').trim(), 10);
if (!sats) return;
ui.arkBusy = 'lninvoice'; ui.arkError = ''; render();
try {
const mgr = await connectArk();
const a = await mgr.createLnInvoice(sats);
ui.arkLnRecvId = a.id;
pollArkLn(a.id, () => render()); // 'done' -> celebration via unseenReceives
} catch (e) {
ui.arkError = e.message;
}
ui.arkBusy = null; render();
}
// Lightning-receive block on the Ark receive pane: amount form -> invoice QR.
function arkLnReceiveSection() {
if (!ark || !ark.info || !ark.info.htlcExpiryDelta) return null; // server without lightning
const s = arkStateNow();
let rec = ui.arkLnRecvId ? ((s && s.actions) || []).find((a) => a.id === ui.arkLnRecvId) : null;
if (rec && ['done', 'failed'].includes(rec.step)) {
// settled (celebration takes over) or dead — reset to the form
if (rec.step === 'failed' && rec.error && rec.error !== 'canceled') ui.arkError = rec.error;
ui.arkLnRecvId = null;
ui.arkLnRecvAmt = '';
rec = null;
}
const divider = { class: 'col', style: 'width:100%;gap:8px;border-top:1px solid var(--border,rgba(128,128,128,.2));padding-top:14px' };
if (rec) {
let svg = null;
try { svg = qrSvg(rec.invoice.toUpperCase(), { ec: 'L', mode: 'Alphanumeric' }); } catch {}
return h('div', divider,
h('div', { class: 'small muted', style: 'text-align:center' }, t('lnReceiveAwaiting')),
svg ? h('div', { style: 'align-self:center', html: svg }) : null,
h('div', { class: 'addr-box break', style: 'width:100%;font-size:11px' }, rec.invoice),
h('div', { class: 'row gap6', style: 'justify-content:center' }, copyBtn(rec.invoice, t('copyInvoice'))),
h('div', { class: 'row gap6', style: 'align-items:center;justify-content:center' },
h('span', { class: 'spinner sm' }), h('span', { class: 'small muted' }, t('lnReceiveWatching'))),
rec.step === 'awaiting'
? h('button', { class: 'btn-ghost btn-block', onClick: async () => {
stopArkLnPoll(rec.id);
try { await ark.cancelLnInvoice(rec.id); } catch {}
ui.arkLnRecvId = null; ui.arkLnRecvAmt = ''; render();
} }, t('arkLnCancelInvoice'))
: null);
}
return h('div', divider,
h('div', { class: 'small muted', style: 'text-align:center' }, t('arkLnInvoiceIntro')),
h('div', { class: 'input-group' },
h('input', {
type: 'number', inputmode: 'numeric', min: '0',
placeholder: t('amountSatsLabel'),
value: ui.arkLnRecvAmt || '',
onInput: (e) => { ui.arkLnRecvAmt = e.target.value; render(); },
}),
h('button', { class: 'btn-sm', disabled: !!ui.arkBusy || !parseInt(ui.arkLnRecvAmt || '', 10), onClick: doArkLnInvoice },
ui.arkBusy === 'lninvoice' ? h('span', { class: 'spinner sm' }) : '⚡ ' + t('lnCreateInvoice'))));
}
function arkHistoryItem(m) {
const incoming = !['send', 'offboard', 'exit', 'ln-send'].includes(m.type);
const label = m.type === 'receive' ? t('received') : m.type === 'board' ? t('arkBoarded')
: m.type === 'ln-send' ? t('arkLnPaidHistory') : m.type === 'ln-receive' ? t('arkLnReceivedHistory')
: m.type === 'offboard' ? t('arkOffboarded') : m.type === 'exit' ? t('arkExited') : t('sent');
return h(
'div',
{ class: 'item', style: 'cursor:pointer', onClick: () => { ui.arkMoveDetail = m.id; render(); } },
// Ark mark in the (direction-colored) circle carries the rail; the label
// + signed amount carry direction, so no redundant "Ark" text chip.
m.type.startsWith('ln-')
? h('div', { class: `ico ${incoming ? 'in' : 'out'}` }, '⚡')
: h('div', { class: `ico ${incoming ? 'in' : 'out'}`, html: ARK_MARK(15) }),
h('div', { class: 'grow' },
h('div', { class: 'row gap6' },
label,
m.status !== 'complete' ? h('span', { class: 'tag pending' }, m.status) : null),
h('div', { class: 'small faint' }, timeAgo(m.ts / 1000))),
h('div', { style: 'text-align:right' },
h('div', { class: incoming ? 'amount-pos' : 'amount-neg' }, (incoming ? '+' : '-') + fmtAmount(m.amountSat)))
);
}
function arkMoveDetailView(m) {
const incoming = !['send', 'offboard', 'exit', 'ln-send'].includes(m.type);
const label = m.type === 'receive' ? t('received') : m.type === 'board' ? t('arkBoarded')
: m.type === 'ln-send' ? t('arkLnPaidHistory') : m.type === 'ln-receive' ? t('arkLnReceivedHistory')
: m.type === 'offboard' ? t('arkOffboarded') : m.type === 'exit' ? t('arkExited') : t('sent');
const row = (k, v) => h('div', { class: 'row between', style: 'gap:12px' },
h('span', { class: 'small muted', style: 'flex-shrink:0' }, k), h('span', { class: 'small', style: 'text-align:right;word-break:break-all' }, v));
const url = m.txid ? wallet.api.explorerTx(m.txid) : null;
return h(
'div',
{ class: 'card col', style: 'gap:10px' },
h('div', { class: 'row gap6', style: 'align-items:center' },
h('span', { html: ARK_ICON(18) }),
h('h3', { style: 'margin:0' }, label),
m.status !== 'complete' ? h('span', { class: 'tag pending' }, m.status) : null),
h('div', { class: incoming ? 'amount-pos' : 'amount-neg', style: 'font-size:20px' },
(incoming ? '+' : '-') + fmtAmount(m.amountSat) + ' ' + unitLabel()),
row(t('dateLabel'), new Date(m.ts).toLocaleString()),
m.to ? row(t('arkPayTo'), shortAddr(m.to, 16, 12)) : null,
m.vtxoId ? row(t('arkVtxoId'), shortTxid(m.vtxoId)) : null,
m.detail ? row(t('detailsLabel'), m.detail) : null,
m.txid
? h('div', { class: 'col', style: 'gap:6px' },
h('div', { class: 'small muted' }, t('transactionId')),
h('div', { class: 'addr-box', style: 'width:100%' }, m.txid),
h('div', { class: 'row gap6' },
copyBtn(m.txid, t('copyTxid')),
h('a', { class: 'btn btn-sm', href: url, target: '_blank', rel: 'noopener', onClick: (e) => { e.preventDefault(); openExternal(url); } }, t('viewOnMempool'))))
: null,
m.to ? copyBtn(m.to, t('copyAddress')) : null,
h('button', { class: 'btn-ghost btn-block', onClick: () => { ui.arkMoveDetail = null; render(); } }, t('back'))
);
}
function arkCard() {
const net = getNetwork();
const id = getArkProviderId(net);
const custom = getArkCustom(net);
const presets = arkPresets(net);
const applyProvider = (v) => {
setArkProviderId(v, net);
ui.receiveType = null;
initArk();
render();
};
const head = [
h('h3', { class: 'row gap6', style: 'align-items:center' }, h('span', { html: ARK_MARK(18) }), 'Ark'),
h('p', { class: 'small muted', style: 'margin:0' }, t('arkDesc')),
h('select', { onChange: (e) => applyProvider(e.target.value) },
presets.map((p) => h('option', { value: p.id, selected: p.id === id }, p.label))),
id === 'custom'
? h('div', { class: 'col', style: 'gap:8px' },
h('label', { class: 'field' },
h('span', { class: 'lab' }, t('arkServerUrl')),
h('input', {
type: 'text', class: 'mono-input', placeholder: 'https://ark.example.com',
autocapitalize: 'none', autocomplete: 'off', spellcheck: 'false', value: custom.ark,
onChange: (e) => { setArkCustom({ ark: e.target.value.trim(), esplora: custom.esplora }, net); initArk(); render(); },
})),
h('label', { class: 'field' },
h('span', { class: 'lab' }, t('arkEsploraUrl')),
h('input', {
type: 'text', class: 'mono-input', placeholder: 'https://mempool.example.com/api',
autocapitalize: 'none', autocomplete: 'off', spellcheck: 'false', value: custom.esplora,
onChange: (e) => { setArkCustom({ ark: custom.ark, esplora: e.target.value.trim() }, net); initArk(); render(); },
})))
: null,
ui.arkError ? h('div', { class: 'notice err' }, ui.arkError) : null,
];
if (id === 'off' || wallet.watchOnly) {
return h('div', { class: 'card col' }, ...head,
wallet.watchOnly && id !== 'off' ? h('div', { class: 'small faint' }, t('arkWatchOnly')) : null);
}
if (!ark || !ark.state) {
return h('div', { class: 'card col' }, ...head,
arkConnectPromise
? h('div', { class: 'row gap6', style: 'align-items:center' },
h('span', { class: 'spinner sm' }), h('span', { class: 'small muted' }, t('arkConnecting')))
: h('button', { class: 'btn-ghost btn-block', onClick: () => connectArk().catch(() => {}) }, t('arkConnectBtn')));
}
const spendables = ark.vtxos().filter((v) => v.state === 'spendable');
const pendingActions = ark.pendingActions();
const row = (k, v) => h('div', { class: 'row between' }, h('span', { class: 'small muted' }, k), h('span', { class: 'small' }, v));
return h(
'div',
{ class: 'card col', style: 'gap:10px' },
...head,
spendables.length ? row(t('arkVtxos'), String(spendables.length)) : null,
// in-flight operations, summarized in plain words (exits get their own
// per-exit status lines below)
(() => {
const counts = {};
for (const a of pendingActions) { if (a.type !== 'exit') counts[a.type] = (counts[a.type] || 0) + 1; }
const LABEL = { board: 'arkOpsBoard', send: 'arkOpsSend', refresh: 'arkOpsRefresh', offboard: 'arkOpsOffboard', 'ln-pay': 'arkOpsLnPay', 'ln-recv': 'arkOpsLnRecv' };
const parts = Object.entries(counts).map(([type, n]) => t(LABEL[type] || 'arkPendingActions', { n }));
return parts.length ? h('div', { class: 'small muted' }, parts.join(' · ')) : null;
})(),
spendables.length >= 1
? h('button', { class: 'btn-ghost btn-block', disabled: !!ui.arkBusy, onClick: doArkRefresh },
ui.arkBusy === 'refresh' ? h('span', { class: 'spinner sm' }) : t('arkRefreshBtn', { n: spendables.length }))
: null,
// Moving funds out (cooperative offboard / unilateral exit) lives on its
// own page, reached from the "Exit" link on the Ark balance line.
spendables.length >= 1
? h('button', { class: 'btn-ghost btn-block', onClick: () => { ui.arkExitPage = true; ui.arkError = ''; render(); } }, t('arkExitPageTitle'))
: null
);
}
async function doArkBoard() {
const sats = parseInt((ui.arkBoardAmt || '').trim(), 10);
if (!sats) return;
ui.arkBusy = 'board'; ui.arkError = ''; render();
try {
const { actionId, fundingAddress, feeSat } = await ark.startBoard(sats);
const feeRate = (wallet.feeRates && wallet.feeRates.halfHourFee) || 5;
const draft = wallet.buildTx({ recipients: [{ address: fundingAddress, amount: sats }], feeRate, noSort: true });
const hexTx = wallet.sign(draft.tx);
const txid = await wallet.broadcast(hexTx);
// Like the main send flow: reflect the spend locally and let the
// poll/watcher reconcile. A scan here would race the explorer's indexing
// and could resurrect the just-spent coin.
wallet.applySentTx(draft.tx);
await ark.completeBoard(actionId, txid);
ui.arkBoardAmt = '';
ui.arkBoarded = { txid, netSat: sats - feeSat };
} catch (e) {
ui.arkError = e.message;
}
ui.arkBusy = null; render();
}
// ---- ark zaps (draft protocol, NIP-61-shaped) ----------------------------
// Lightning zaps (NIP-57) are built around LNURL + bolt11; ark needs
// neither. Mirroring nutzaps (NIP-61): the recipient announces their ark
// address as a replaceable event, the sender pays with a plain instant
// arkoor send and publishes a receipt referencing the delivered vtxo.
// Kinds are provisional until a NIP lands.
const ARK_INFO_KIND = 10037; // replaceable: ["ark", <address>], ["network", <net>]
const ARK_ZAP_KIND = 9737; // receipt: ["p", pk], ["e", note?], ["amount", sats], ["vtxo", id], ["network", net]
// npub decode without importing the nostr stack (keeps ark-only builds lean)
function npubToHex(s) {
try {
const { prefix, words } = bech32.decode(String(s || '').trim(), 200);
if (prefix !== 'npub') return null;
return hex.encode(new Uint8Array(bech32.fromWords(words)));
} catch { return null; }
}
// Tell the world where our ark mailbox lives (once per address, and only
// when the sync/nostr feature is present in this build).
function announceArkAddress(mgr) {
if (!wallet.nostrPublish) return;
const addr = mgr.address();
if (mgr._zapAnnounced === addr) return;
mgr._zapAnnounced = addr;
wallet.nostrPublish({ kind: ARK_INFO_KIND, tags: [['ark', addr], ['network', getNetwork()]] }).catch(() => {});
}
// Resolve an npub to a same-network ark address via their announcement.
async function lookupArkZapTarget(pk) {
const events = await wallet.nostrFetch({ kinds: [ARK_INFO_KIND], authors: [pk] }, 6000);
const ev = (events || []).sort((a, b) => b.created_at - a.created_at)[0];
if (!ev) return { status: 'noark' };
const addr = (ev.tags.find((t) => t[0] === 'ark') || [])[1];
const net = (ev.tags.find((t) => t[0] === 'network') || [])[1];
if (!addr) return { status: 'noark' };
if (net && net !== getNetwork()) return { status: 'wrongnet', net };
return { status: 'ready', address: addr };
}
async function doArkZap() {
const z = ui.arkZap;
const sats = ctx.parseAmount(z.amount, ctx.getUnit());
if (!sats || sats <= 0) { ui.sendError = t('enterValidAmtForN', { n: 1 }); render(); return; }
ui.busy = true; ui.sendError = ''; render();
try {
const mgr = await connectArk();
const actionId = await mgr.send(z.address, sats);
const action = mgr.state.actions.find((a) => a.id === actionId);
if (!action || action.step === 'failed') throw new Error(action?.error || t('claimFailed'));
const vtxoId = decodeVtxo(hex.decode((action.destBytesList || [action.destBytes])[0])).id;
// the receipt is best-effort: the sats are already delivered via mailbox
await wallet.nostrPublish({
kind: ARK_ZAP_KIND,
content: (z.comment || '').slice(0, 280),
tags: [['p', z.pk], ['amount', String(sats)], ['vtxo', vtxoId], ['network', getNetwork()]],
}).catch(() => {});
ui.arkZapped = { amountSat: sats, npub: z.npub };
ui.arkZap = null;
} catch (e) {
ui.sendError = e.message;
}
ui.busy = false; render();
}
function arkZapView() {
if (ui.arkZapped) {
return h('div', {
class: 'card col',
style: 'align-items:center;text-align:center;gap:14px;cursor:pointer;padding:48px 20px',
onClick: () => { ui.arkZapped = null; ui.send = blankSend(); render(); },
},
h('div', { class: 'check-badge' }, '⚡'),
h('h2', { style: 'margin:0' }, t('arkZapSentTitle')),
h('div', { class: 'amount-neg', style: 'font-size:18px' }, '-' + fmtAmount(ui.arkZapped.amountSat) + ' ' + unitLabel()),
h('div', { class: 'small muted' }, t('tapToProceed')));
}
const z = ui.arkZap;
if (!z) return null;
return h('div', { class: 'card col', style: 'gap:12px' },
h('h3', {}, '⚡ ' + t('arkZapTitle')),
h('div', { class: 'small muted', style: 'word-break:break-all' }, z.npub),
z.status === 'lookup' ? h('div', { class: 'row gap6', style: 'align-items:center' }, h('span', { class: 'spinner sm' }), h('span', { class: 'small muted' }, t('arkZapLookup'))) : null,
z.status === 'noark' ? h('div', { class: 'notice err' }, t('arkZapNoArk')) : null,
// No Ark address, but they may still take a Lightning zap — hand off to
// the zaps feature (present alongside swaps).
(z.status === 'noark' || z.status === 'wrongnet') && ctx.hook('canLnZap')
? h('button', { class: 'btn-primary btn-block', onClick: () => { ctx.hook('lnZapNpub', z.pk, z.npub); } }, '⚡ ' + t('lnZapFallback'))
: null,
z.status === 'wrongnet' ? h('div', { class: 'notice err' }, t('arkGiftWrongNet', { net: z.net })) : null,
z.status === 'ready'
? h('div', { class: 'col gap6' },
h('div', { class: 'input-group' },
h('input', { type: 'number', min: '0', inputmode: 'decimal', placeholder: t('lnPayAmount'), value: z.amount,
onInput: (e) => { z.amount = e.target.value; } }),
h('div', { style: 'display:flex;align-items:center' }, unitTag())),
h('input', { type: 'text', class: 'mono-input', placeholder: t('arkZapCommentPh'), value: z.comment,
onInput: (e) => { z.comment = e.target.value; } }),
h('div', { class: 'small faint' }, t('arkZapHint')))
: null,
ui.sendError ? h('div', { class: 'notice err' }, ui.sendError) : null,
h('div', { class: 'row gap6' },
h('button', { class: 'btn-ghost', onClick: () => { ui.arkZap = null; ui.sendError = ''; ui.send = blankSend(); render(); } }, t('back')),
z.status === 'ready'
? (ui.busy
? h('button', { class: 'btn-primary grow', disabled: true }, h('span', { class: 'spinner' }))
: h('button', { class: 'btn-primary grow', onClick: doArkZap }, t('arkZapBtn')))
: null));
}
// ---- NWC bridge funding (PoC) --------------------------------------------
// An external NWC wallet service (e.g. coinos) can bridge ark -> lightning:
// a nostr client asks IT to pay_invoice, it asks US (kind 23196, funding
// request) to cover the amount over ark, we auto-pay within a user-set
// allowance and ack (kind 23197). Custody window = seconds in flight.
// PoC config in localStorage 'btc-wallet-arknwc': { bridgePk, budgetSat }.
const NWC_FUND_KIND = 23196;
const NWC_FUND_ACK_KIND = 23197;
let nwcUnsub = null;
let nwcSpent = 0;
function stopNwcFunding() {
if (nwcUnsub) { try { nwcUnsub(); } catch {} nwcUnsub = null; }
}
function startNwcFunding(mgr) {
if (nwcUnsub || !wallet.nostrSubscribe || !wallet.nostrPubkey || !wallet.nostrPubkey()) return;
let cfg = null;
try { cfg = JSON.parse(localStorage.getItem('btc-wallet-arknwc') || 'null'); } catch {}
if (!cfg || !cfg.bridgePk || !(cfg.budgetSat > 0)) return;
const seen = new Set();
nwcUnsub = wallet.nostrSubscribe(
{ kinds: [NWC_FUND_KIND], authors: [cfg.bridgePk], '#p': [wallet.nostrPubkey()] },
async (ev) => {
if (seen.has(ev.id)) return;
seen.add(ev.id);
let req;
try { req = JSON.parse(ev.content); } catch { return; }
const sats = Math.round(req.amountSat || 0);
if (!sats || !req.address || nwcSpent + sats > cfg.budgetSat) return; // over allowance: bridge times out
try {
const actionId = await mgr.send(req.address, sats);
const action = mgr.state.actions.find((a) => a.id === actionId);
if (!action || action.step === 'failed') return;
nwcSpent += sats;
await wallet.nostrPublish({
kind: NWC_FUND_ACK_KIND,
tags: [['p', cfg.bridgePk], ['e', ev.id]],
content: JSON.stringify({ id: req.id }),
});
} catch {}
});
}
// ---- unilateral exit (trustless: no server cooperation) ------------------
const addrScript = (address) => btc.OutScript.encode(btc.Address(wallet.netCfg.net).decode(address));
// Smallest confirmed, unreserved coin that can fund a CPFP bump.
function pickFeeCoin(minSat) {
return wallet.utxos
.filter((u) => u.confirmed && !wallet.isReserved(utxoId(u)) && u.value >= minSat)
.sort((a, b) => a.value - b.value)[0] || null;
}
async function doArkExit() {
ui.arkBusy = 'exit'; ui.arkError = ''; render();
try {
const mgr = await connectArk();
const spendables = mgr.vtxos().filter((v) => v.state === 'spendable');
if (!spendables.length) throw new Error(t('arkNotConnected'));
for (const v of spendables) mgr.startExit(v.id);
toast(t('arkExitStarted', { n: spendables.length }));
driveExits(mgr).catch(() => {});
} catch (e) {
ui.arkError = e.message;
}
ui.arkBusy = null; render();
}
async function driveExits(mgr) {
const open = mgr.state.actions.filter((a) => a.type === 'exit' && !['done', 'failed'].includes(a.step));
for (const a of open) {
try {
await driveExit(mgr, a);
if (a.lastError) { delete a.lastError; delete a.actionable; mgr._save(); } // resolved
} catch (e) {
a.lastError = e.message;
a.actionable = !!e.actionable; // user must act (e.g. fund fees) vs. plain retry
mgr._save();
}
}
}
// One tick of an exit's state machine: broadcast the next unconfirmed hop
// (with its fee child) as a package, then wait out the CSV, then claim.
async function driveExit(mgr, action) {
const rec = mgr._vtxo(action.vtxoId);
const decoded = mgr._decoded(rec);
if (action.step === 'chain') {
const txs = signedExitTxs(decoded, mgr.serverPub);
let lastConfirmedHeight = 0;
let hopsDone = 0;
for (const txi of txs) {
const st = await mgr.chain.getTxStatus(txi.txid);
if (st?.confirmed) { lastConfirmedHeight = st.block_height; hopsDone++; if (action.hopsDone !== hopsDone) { action.hopsDone = hopsDone; mgr._save(); } continue; }
// /tx/:txid/status answers {confirmed:false} even for UNKNOWN txids
// (electrs + mempool.space) — only /tx/:txid 404s definitively
if (await mgr.chain.getTxHex(txi.txid)) return; // in mempool — wait
// unknown to the chain: submit this hop + CPFP child as a package
const feeRate = Math.max(1, (wallet.feeRates && wallet.feeRates.halfHourFee) || 2);
// the child pays for the whole package (the parent is zero-fee)
const feeSat = Math.ceil((txi.vsize + 130) * feeRate); // child ≈ 130 vB
const coin = pickFeeCoin(Math.max(294, feeSat - txi.anchorValue + 294));
if (!coin) {
// No on-chain coin for the fee child. Try the hop bare — a node
// that relays zero-fee txs (regtest with minrelaytxfee=0) needs no
// bump. If it refuses, this is a PRECONDITION the user must fix,
// not a transient to retry behind a vague message.
try {
await submitPackage(mgr.esploraUrl, [txi.hex]);
mgr._save();
return;
} catch {
const e = new Error(t('arkExitNoFeeCoin'));
e.actionable = true;
throw e;
}
}
const changeAddr = wallet.freshChange().address;
const child = buildBumpChild({
parentTxidInternal: txi.txidInternal, anchorVout: txi.anchorVout, anchorValue: txi.anchorValue,
coin: {
txid: coin.txid, vout: coin.vout, value: coin.value,
pubkey: wallet.derive(coin.chain, coin.index).pubkey,
privkey: wallet.node(coin.chain, coin.index).privateKey,
},
changeScript: addrScript(changeAddr), feeSat,
});
await submitPackage(mgr.esploraUrl, [txi.hex, child.hex]);
// reflect the spent fee coin locally so nothing double-spends it
wallet.utxos = wallet.utxos.filter((u) => !(u.txid === coin.txid && u.vout === coin.vout));
const ce = wallet.addrMap.get(changeAddr);
if (ce) wallet.utxos.push({ txid: child.txid, vout: 0, value: child.changeSat, address: changeAddr, chain: ce.chain, index: ce.index, confirmed: false });
wallet._recomputeBalanceFromUtxos();
wallet.saveCache();
action.bumpTxid = child.txid;
mgr._save();
return; // one hop per tick (TRUC: single unconfirmed parent)
}
// whole chain confirmed — start the CSV clock from the vtxo tx's height
action.claimableAt = lastConfirmedHeight + decoded.exitDelta;
action.step = 'timelock';
mgr._save();
}
if (action.step === 'timelock') {
const tip = await mgr.chain.tipHeight();
if (action.tipSeen !== tip) { action.tipSeen = tip; mgr._save(); } // for the blocks-left display
if (tip < action.claimableAt) return;
const keys = mgr._keyForVtxo(rec);
const feeRate = Math.max(1, (wallet.feeRates && wallet.feeRates.halfHourFee) || 2);
const claim = buildExitClaim({
vtxo: decoded, keys, serverPub: mgr.serverPub,
destScript: addrScript(wallet.freshReceive().address), feeRate,
});
await mgr.chain.broadcastTx(claim.hex);
rec.state = 'spent';
action.claimTxid = claim.txid;
action.step = 'claiming';
mgr._movement({ type: 'exit', amountSat: claim.amountSat, status: 'complete', txid: claim.txid, detail: `fee ${claim.feeSat} sat` });
mgr._save();
wallet.scan().catch(() => {});
}
if (action.step === 'claiming') {
const st = await mgr.chain.getTxStatus(action.claimTxid);
if (!st?.confirmed) return;
action.step = 'done';
mgr._save();
}
}
// ---- ark gifts: bearer-key vtxos ----------------------------------------
// Ark can't presign a bearer spend (every arkoor needs a live server cosign),
// so an ark gift is a vtxo sent to an ephemeral ark identity whose seed IS
// the link. The claimer rebuilds the identity from the code, reads the
// gift's mailbox for the vtxo, and sweeps it to their own ark address —
// instant and free. The sender keeps the secret too, which is what makes
// revoke possible (sweep it back before it's claimed; first cosign wins).
const AG_MAGIC = 0x11;
const AG_NET = { mainnet: 0, testnet: 1, signet: 2, mutinynet: 3, regtest: 4 };
const AG_NET_BY = Object.fromEntries(Object.entries(AG_NET).map(([k, v]) => [v, k]));
function encodeArkGiftCode(net, amountSat, secret) {
const b = new Uint8Array(42);
b[0] = AG_MAGIC;
b[1] = AG_NET[net] ?? 0;
new DataView(b.buffer).setBigUint64(2, BigInt(amountSat), true);
b.set(secret, 10);
return base32nopad.encode(b);
}
function decodeArkGiftCode(code) {
try {
const b = base32nopad.decode(String(code || '').toUpperCase());
if (b.length !== 42 || b[0] !== AG_MAGIC || !(b[1] in AG_NET_BY)) return null;
return {
net: AG_NET_BY[b[1]],
amountSat: Number(new DataView(b.buffer, b.byteOffset).getBigUint64(2, true)),
secretHex: hex.encode(b.slice(10)),
};
} catch { return null; }
}
// A manager over the gift identity. State persists per device (keyed by the
// secret's hash) so a claim interrupted mid-sweep resumes checkpointed.
async function giftManager(secretHex) {
const cfg = getArkConfig();
if (!cfg) throw new Error(t('arkNotConnected'));
const key = 'btc-wallet-arkgift:' + hex.encode(sha256(hex.decode(secretHex))).slice(0, 24);
const mgr = new ArkManager({
account: HDKey.fromMasterSeed(hex.decode(secretHex)),
storage: {
load: () => { try { return JSON.parse(localStorage.getItem(key) || 'null'); } catch { return null; } },
save: (s) => { try { localStorage.setItem(key, JSON.stringify(s)); } catch {} },
},
arkUrl: cfg.ark, esploraUrl: cfg.esplora, network: getNetwork(),
});
await mgr.init();
return mgr;
}
// The gift vtxo's key/outpoint for direct status checks (receive key 0).
const giftKey0 = (secretHex) => {
const n = HDKey.fromMasterSeed(hex.decode(secretHex)).deriveChild(3).deriveChild(0);
return n.privateKey;
};
const pointRawFromId = (id) => {
const [txid, vout] = id.split(':');
const raw = new Uint8Array(36);
raw.set(hex.decode(txid).reverse(), 0);
new DataView(raw.buffer).setUint32(32, Number(vout), true);
return raw;
};
function arkGiftRecords() {
if (!ark || !ark.state) return [];
return (ark.state.gifts = ark.state.gifts || []);
}
// Lazily mark records whose vtxo the server reports spent (claimed — or our
// own revoke). Throttled: this is called from the gift card's render path.
let arkGiftsCheckedAt = 0;
function refreshArkGiftRecords() {
if (!ark || Date.now() - arkGiftsCheckedAt < 30_000) return;
arkGiftsCheckedAt = Date.now();
const open = arkGiftRecords().filter((g) => !g.revoked && !g.claimed);
Promise.all(open.map(async (g) => {
try {
const st = await getVtxoStatus(ark.arkUrl, pointRawFromId(g.id), giftKey0(g.secretHex));
if (st === VTXO_STATE_SPENT) { g.claimed = true; return true; }
} catch {}
return false;
})).then((flags) => { if (flags.some(Boolean)) ark._save(); });
}
// Sweep the gift identity's balance to `destAddress`. Shared by claim
// (recipient's address) and revoke (sender's own address).
async function sweepArkGift(code, destAddress) {
const g = decodeArkGiftCode(code);
if (!g) throw new Error('not an ark gift');
if (g.net !== getNetwork()) throw new Error(t('arkGiftWrongNet', { net: g.net }));
const gm = await giftManager(g.secretHex);
await gm.sync();
await gm.reconcile().catch(() => {});
const amount = gm.balance().spendableSat;
if (!amount) throw Object.assign(new Error(t('giftTakenTitle')), { giftTaken: true });
await gm.send(destAddress, amount);
return amount;
}
// Offboard the whole ark balance back into this wallet's own on-chain
// receive address — the mirror image of boarding.
async function doArkOffboard() {
ui.arkBusy = 'offboard'; ui.arkError = ''; render();
try {
const mgr = await connectArk();
const address = wallet.freshReceive().address;
const spk = btc.OutScript.encode(btc.Address(wallet.netCfg.net).decode(address));
const action = await mgr.startOffboard(spk, address);
ui.arkOffboarded = { txid: action.txid, netSat: action.netSat, feeSat: action.feeSat };
wallet.scan().catch(() => {}); // surface the incoming pending tx promptly
} catch (e) {
ui.arkError = e.message;
}
ui.arkBusy = null; render();
}
async function doArkRefresh() {
ui.arkBusy = 'refresh'; ui.arkError = ''; render();
try {
await ark.refresh();
toast(t('arkRefreshStarted'), 5000);
} catch (e) {
ui.arkError = e.message;
}
ui.arkBusy = null; render();
}
// The Ark receive pane: connect-on-demand, then address + board form.
function arkReceivePane(seg) {
if (true) {
if (!ark) {
connectArk().catch(() => {});
return h(
'div',
{ class: 'card col', style: 'align-items:center;gap:14px' },
seg,
ui.arkError
? h('div', { class: 'notice err', style: 'width:100%' }, ui.arkError)
: h('div', { class: 'row gap6', style: 'align-items:center;padding:24px 0' },
h('span', { class: 'spinner sm' }), h('span', { class: 'small muted' }, t('arkConnecting')))
);
}
const arkAddr = ark.address();
const minBoard = ark.info.minBoardAmountSat || 0;
const canBoard = wallet.spendable >= minBoard;
return h(
'div',
{ class: 'card col', style: 'align-items:center;gap:14px' },
seg,
h('p', { class: 'small muted', style: 'margin:0;text-align:center' }, t('arkReceiveIntro')),
h('div', { html: qrSvg(arkAddr) }),
h('div', { class: 'addr-box break', style: 'width:100%;font-size:11px' }, arkAddr),
copyBtn(arkAddr, t('copyAddress')),
// Board: fund your Ark balance from this wallet's on-chain coins.
h('div', { class: 'col', style: 'width:100%;gap:8px;border-top:1px solid var(--border,rgba(128,128,128,.2));padding-top:14px' },
h('div', { class: 'small muted', style: 'text-align:center' }, t('arkBoardIntro')),
h('div', { class: 'input-group' },
h('input', {
type: 'number', inputmode: 'numeric', min: '0',
placeholder: t('arkBoardPlaceholder', { n: minBoard.toLocaleString() }),
value: ui.arkBoardAmt || '',
onInput: (e) => { ui.arkBoardAmt = e.target.value; render(); },
}),
h('button', { class: 'btn-sm', disabled: !!ui.arkBusy || !canBoard, onClick: doArkBoard },
ui.arkBusy === 'board' ? h('span', { class: 'spinner sm' }) : t('arkBoardBtn'))),
// show the server's board fee BEFORE the money moves, not after
(() => {
const sats = parseInt((ui.arkBoardAmt || '').trim(), 10);
if (!sats || sats < minBoard) return null;
const fee = boardFee(sats, ark.info.boardFees);
return h('div', { class: 'small muted', style: 'text-align:center' },
t('arkBoardFeeNote', { fee: fmtAmount(fee), net: fmtAmount(sats - fee) }));
})(),
canBoard
? h('div', { class: 'small faint', style: 'text-align:center' }, t('arkBoardAvailable', { n: fmtAmount(wallet.spendable) + ' ' + unitLabel() }))
: h('div', { class: 'small faint', style: 'text-align:center' }, t('arkBoardNoFunds', { n: minBoard.toLocaleString() })),
ui.arkError ? h('div', { class: 'notice err' }, ui.arkError) : null),
// Lightning receive straight into the ark balance (ASP-native swap)
arkLnReceiveSection()
);
}
throw new Error('unreachable');
}
// "Payment received!" takeover for unseen Ark receives.
function arkCelebration() {
// An Ark payment landed — same celebration as an on-chain receive, dismissed
// with a tap (the ack persists in the ark state so it shows exactly once).
const arkUnseen = ark && ark.state ? ark.unseenReceives() : [];
if (arkUnseen.length) {
const amt = arkUnseen.reduce((n, m) => n + m.amountSat, 0);
return h(
'div',
{
class: 'card col',
style: 'align-items:center;text-align:center;gap:14px;cursor:pointer;padding:48px 20px',
onClick: () => { ark.ackReceives(); render(); },
},
h('div', { class: 'check-badge' }, '✓'),
h('h2', { style: 'margin:0' }, t('paymentReceived')),
amt ? h('div', { class: 'amount-pos', style: 'font-size:18px' }, '+' + fmtAmount(amt) + ' ' + unitLabel()) : null,
h('div', { class: 'small muted' }, t('tapToProceed'))
);
}
return null;
}
// Offboard success screen — mirror of the boarded screen.
function arkOffboardedScreen() {
if (!ui.arkOffboarded) return null;
const o = ui.arkOffboarded;
const url = wallet.api.explorerTx(o.txid);
return h(
'div',
{ class: 'card col', style: 'align-items:center;text-align:center;gap:14px;padding:40px 20px' },
h('div', { class: 'check-badge' }, '✓'),
h('h2', { style: 'margin:0' }, t('arkOffboardedTitle')),
h('div', { class: 'amount-pos', style: 'font-size:18px' }, '+' + fmtAmount(o.netSat) + ' ' + unitLabel()),
h('p', { class: 'small muted', style: 'margin:0' }, t('arkOffboardedNote')),
o.feeSat ? h('div', { class: 'small faint' }, t('feeShort', { x: fmtAmount(o.feeSat) })) : null,
h('div', { class: 'addr-box', style: 'width:100%' }, o.txid),
h('div', { class: 'row gap6' },
copyBtn(o.txid, t('copyTxid')),
h('a', { class: 'btn btn-sm', href: url, target: '_blank', rel: 'noopener', onClick: (e) => { e.preventDefault(); openExternal(url); } }, t('viewOnMempool'))),
h('button', { class: 'btn-primary btn-block', onClick: () => { ui.arkOffboarded = null; ui.arkExitPage = null; render(); } }, t('done'))
);
}
// Boarding success screen (txid + explorer link) until dismissed.
function arkBoardedScreen() {
// A board just broadcast — show its success screen until dismissed.
if (ui.arkBoarded) {
const b = ui.arkBoarded;
const url = wallet.api.explorerTx(b.txid);
return h(
'div',
{ class: 'card col', style: 'align-items:center;text-align:center;gap:14px;padding:40px 20px' },
h('div', { class: 'check-badge' }, '✓'),
h('h2', { style: 'margin:0' }, t('arkBoardedTitle')),
h('div', { class: 'amount-pos', style: 'font-size:18px' }, '+' + fmtAmount(b.netSat) + ' ' + unitLabel()),
h('p', { class: 'small muted', style: 'margin:0' }, t('arkBoardedNote')),
h('div', { class: 'addr-box', style: 'width:100%' }, b.txid),
h('div', { class: 'row gap6' },
copyBtn(b.txid, t('copyTxid')),
h('a', { class: 'btn btn-sm', href: url, target: '_blank', rel: 'noopener', onClick: (e) => { e.preventDefault(); openExternal(url); } }, t('viewOnMempool'))),
h('button', { class: 'btn-primary btn-block', onClick: () => { ui.arkBoarded = null; render(); } }, t('done'))
);
}
return null;
}
// Per-exit progress lines (chain hops / timelock countdown / claiming), with
// a cancel for an exit that never published. Shown on the exit page.
function arkExitStatusLines() {
const s = arkStateNow();
return ((s && s.actions) || []).filter((a) => a.type === 'exit' && !['done', 'failed'].includes(a.step)).map((a) => {
const left = a.step === 'timelock' ? Math.max(0, a.claimableAt - (a.tipSeen || 0)) : 0;
const mins = left * 10;
const eta = getNetwork() === 'regtest' ? ''
: mins >= 2880 ? ` (≈ ${Math.round(mins / 1440)} d)`
: mins >= 120 ? ` (≈ ${Math.round(mins / 60)} h)`
: ` (≈ ${mins} min)`;
return h('div', { class: 'small muted', style: 'margin-top:4px' },
a.step === 'chain' ? t('arkExitChainStatus', { n: fmtAmount(a.amountSat), done: String(a.hopsDone || 0), total: String((a.txids || []).length) })
: a.step === 'timelock' ? t('arkExitTimelockStatus', { n: fmtAmount(a.amountSat), blocks: String(left), eta })
: t('arkExitClaimingStatus', { n: fmtAmount(a.amountSat) }),
a.lastError ? h('div', { class: a.actionable ? 'small err' : 'small faint' }, a.actionable ? a.lastError : t('arkExitRetrying')) : null,
a.actionable && !(a.hopsDone > 0)
? h('button', { class: 'linklike small', onClick: () => {
a.step = 'failed';
const v = ark && ark._vtxo(a.vtxoId);
if (v && v.state === 'pending') v.state = 'spendable';
if (ark) ark._save();
render();
} }, t('arkExitCancel'))
: null);
});
}
// The exit page: cooperative offboard vs unilateral exit, with explanation.
// Reached from the "Exit" link on the Ark balance line.
function arkExitPage() {
if (ui.arkOffboarded) return arkOffboardedScreen(); // cooperative success takeover
const b = arkBalance() || { spendableSat: 0, pendingSat: 0 };
const spendable = b.spendableSat;
const total = b.spendableSat + b.pendingSat;
const nSpend = ((arkStateNow() && arkStateNow().vtxos) || []).filter((v) => v.state === 'spendable').length;
const exits = arkExitStatusLines();
const back = () => { ui.arkExitPage = null; ui.arkError = ''; render(); };
return h('div', { class: 'col', style: 'gap:16px' },
h('div', { class: 'card col', style: 'gap:8px' },
h('h3', { class: 'row gap6', style: 'align-items:center;margin:0' }, h('span', { html: ARK_ICON(18) }), t('arkExitPageTitle')),
h('p', { class: 'small muted', style: 'margin:0' }, t('arkExitPageIntro')),
total > 0 ? h('div', { class: 'row between', style: 'margin-top:4px' },
h('span', { class: 'small muted' }, t('arkBalance')),
h('span', { class: 'small' }, fmtAmount(total) + ' ' + unitLabel())) : null),
// cooperative
h('div', { class: 'card col', style: 'gap:8px' },
h('h4', { style: 'margin:0' }, t('arkCoopTitle')),
h('p', { class: 'small muted', style: 'margin:0' }, t('arkCoopDesc')),
spendable > 0
? h('button', { class: 'btn-primary btn-block', disabled: !!ui.arkBusy, onClick: doArkOffboard },
ui.arkBusy === 'offboard' ? h('span', { class: 'spinner sm' }) : t('arkOffboardBtn', { n: fmtAmount(spendable) + ' ' + unitLabel() }))
: h('div', { class: 'small faint' }, t('arkExitNoBalance'))),
// unilateral
h('div', { class: 'card col', style: 'gap:8px' },
h('h4', { style: 'margin:0' }, t('arkUniTitle')),
h('p', { class: 'small muted', style: 'margin:0' }, t('arkUniDesc')),
nSpend > 0
? h('button', { class: 'btn-ghost btn-block', disabled: !!ui.arkBusy, onClick: doArkExit },
ui.arkBusy === 'exit' ? h('span', { class: 'spinner sm' }) : t('arkExitBtn', { n: nSpend }))
: null,
...exits),
ui.arkError ? h('div', { class: 'notice err' }, ui.arkError) : null,
h('button', { class: 'btn-ghost btn-block', onClick: back }, t('back'))
);
}
return {
id: 'ark',
init() { initArk(); },
stop() { stopArk(); },
screenView() { return ui.arkExitPage ? arkExitPage() : null; },
receiveModes() {
if (!arkAvailable()) return [];
return [{ id: 'ark', label: t('receiveArkTab'), icon: ARK_MARK(18), render: (seg) => arkReceivePane(seg) }];
},
receiveTakeover() {
const offboarded = arkOffboardedScreen();
if (offboarded) return offboarded;
const boarded = arkBoardedScreen();
if (boarded) return boarded;
return arkCelebration();
},
isSendDest(a) { return isArkAddress(a) && arkAvailable(); },
hideSendControls(a) { return isArkAddress(a); },
sendFormNote(a) {
if (!isArkAddress(a)) return null;
maybeReconcile();
return h('div', { class: 'small faint' }, t('arkSendHint'));
},
interceptReview(s) {
if (s.recipients.length === 1) {
const inv = (s.recipients[0].address || '').trim().replace(/^lightning:/i, '');
if (maybeBolt11(inv) && startArkLnPay(inv)) return true;
}
if (s.recipients.length === 1 && isArkAddress(s.recipients[0].address)) {
if (!arkAvailable()) throw new Error(t('arkNotConnected'));
const sats = ctx.parseAmount(s.recipients[0].amount, ctx.getUnit());
if (!sats || sats <= 0) throw new Error(t('enterValidAmtForN', { n: 1 }));
ui.arkSend = { address: s.recipients[0].address.trim(), amountSat: sats };
render();
return true;
}
return false;
},
sendView() {
if (ui.arkLnPaid || ui.arkLnPay) return arkLnPayView();
if (ui.arkZapped || ui.arkZap) return arkZapView();
if (ui.arkSent || ui.arkSend) return arkSendReview();
return null;
},
// A bolt11 pays from the ark balance when it can cover it. Reached via the
// swaps feature's delegation (its matcher runs first) or directly in
// builds without the swaps feature.
startArkLnPay(invoice, meta) { return startArkLnPay(invoice, meta); },
// ---- headless seam for the NWC wallet service ----
arkPayInvoice(invoice, opts) { return payInvoiceHeadless(invoice, opts); },
arkSpendableSat() { const b = arkBalance(); return b ? b.spendableSat : 0; },
arkReady() { return arkAvailable(); },
async arkMakeInvoice(amountSat, description) {
const mgr = await connectArk();
const a = await mgr.createLnInvoice(amountSat, description);
return { invoice: a.invoice, paymentHash: a.paymentHash, amountSat };
},
arkMovements() { const s = arkStateNow(); return s ? (s.movements || []) : []; },
// The wallet's reusable ark receive address (BIP-353 names publish it).
async arkStaticAddress() {
const mgr = await connectArk();
return mgr.address();
},
// Whether background answering can mirror right now.
arkBgReady() { return !!(ark && ark.info && ark.state); },
// nwc.js calls this (with its connections) whenever the mirror should
// refresh: on enable, on connection changes, after payments, on a timer.
async arkBgWrite(connections, offer) { return writeBgMirror(connections, offer); },
async arkBgSpendableSat() {
const rec = await loadBg(wallet._cacheKey());
return ((rec && rec.mgr && rec.mgr.vtxos) || [])
.filter((v) => v.state === 'spendable').reduce((n, v) => n + (v.amountSat || 0), 0);
},
// Worker payments become ordinary history lines once the app opens.
async arkBgNoteSpends(spends) {
const mgr = await connectArk();
for (const sp of spends) {
mgr._movement({
type: 'ln-send', amountSat: sp.amountSat, status: 'complete',
detail: `while closed · fee ${sp.feeSat || 0} sat`, invoice: sp.invoice || '', preimage: sp.preimage || '',
});
}
mgr._save();
},
// An npub pasted into Send becomes an ark zap (needs the nostr seam from
// the sync feature and a connected-able ark). A bolt11 is handled here
// only in builds without the swaps feature (whose matcher runs first and
// delegates back via the startArkLnPay hook).
matchSendText(text) {
const inv = (text || '').trim().replace(/^lightning:/i, '');
if (maybeBolt11(inv)) return startArkLnPay(inv);
const pk = npubToHex(text);
if (!pk || !arkAvailable() || !wallet.nostrFetch) return false;
ui.arkZap = { npub: String(text).trim(), pk, amount: '', comment: '', status: 'lookup' };
ui.sendError = '';
render();
Promise.all([connectArk(), lookupArkZapTarget(pk)])
.then(([, res]) => { if (ui.arkZap && ui.arkZap.pk === pk) { Object.assign(ui.arkZap, res); render(); } })
.catch((e) => { if (ui.arkZap && ui.arkZap.pk === pk) { ui.arkZap.status = 'noark'; ui.sendError = e.message; render(); } });
return true;
},
historyEntries() {
const s = arkStateNow();
if (!s) return [];
return (s.movements || [])
.filter((m) => ['receive', 'send', 'board', 'offboard', 'exit', 'ln-send', 'ln-receive'].includes(m.type) && m.status === 'complete')
.map((m) => ({ time: m.ts, render: () => arkHistoryItem(m) }));
},
historyDetail() {
if (!ui.arkMoveDetail) return null;
const s = arkStateNow();
const m = s ? (s.movements || []).find((x) => x.id === ui.arkMoveDetail) : null;
if (m) return arkMoveDetailView(m);
ui.arkMoveDetail = null;
return null;
},
balanceLines() {
const b = arkBalance();
if (!b) return [];
const lines = [];
if (b.spendableSat + b.pendingSat > 0) {
// "Ark balance … Exit" — the exit link opens the offboard/exit page.
// Watch-only wallets can't move funds, so they just show the balance.
const label = wallet.watchOnly
? t('arkBalance')
: h('span', { class: 'row gap6', style: 'align-items:center' },
t('arkBalance'),
h('span', { class: 'linklike', style: 'font-size:12px', onClick: () => { ui.arkExitPage = true; ui.arkError = ''; render(); } }, t('arkExitLink')));
lines.push({ label, sat: b.spendableSat + b.pendingSat });
}
// A board in flight: the sats already left the on-chain balance, so show
// where they went instead of having them silently disappear.
if (b.boardingSat > 0) lines.push({ label: t('arkBoarding'), sat: b.boardingSat });
return lines;
},
decorateTxRow(tx) {
const s = arkStateNow();
if (!s) return null;
const acts = s.actions || [];
if (tx.net < 0) {
const a = acts.find((x) => x.type === 'board' && x.fundingTxid === tx.txid);
return a ? { icon: h('span', { html: ARK_MARK(16) }), label: h('span', {}, t('arkBoardHistory')) } : null;
}
const o = acts.find((x) => x.type === 'offboard' && x.txid === tx.txid);
if (o) return { icon: h('span', { html: ARK_MARK(16) }), label: h('span', {}, t('arkOffboardHistory')) };
const e = acts.find((x) => x.type === 'exit' && x.claimTxid === tx.txid);
return e ? { icon: h('span', { html: ARK_MARK(16) }), label: h('span', {}, t('arkExitHistory')) } : null;
},
txDetailSection(tx) {
const s = arkStateNow();
if (!s) return null;
const a = (s.actions || []).find((x) => x.type === 'board' && x.fundingTxid === tx.txid);
if (!a) return null;
const done = a.step === 'done';
return h('div', { class: 'summary col', style: 'gap:0' },
h('div', { class: 'row gap6', style: 'font-weight:600;margin:12px 0 2px;align-items:center' }, h('span', { html: ARK_ICON(16) }), t('arkBoardHistory')),
h('div', { class: 'line' },
h('span', { class: 'k' }, t('arkBalance')),
h('span', { class: 'v' }, '+' + fmtAmount(a.amountSat - a.feeSat) + ' ' + unitLabel())),
a.feeSat > 0
? h('div', { class: 'line' },
h('span', { class: 'k' }, t('arkBoardFeeLabel')),
h('span', { class: 'v' }, fmtAmount(a.feeSat) + ' ' + unitLabel()))
: null,
done ? null : h('div', { class: 'small muted', style: 'margin-top:2px' }, t('arkBoardedNote')));
},
settingsCards() { return [arkCard()]; },
// ---- ark-gift hooks (called by the gifts feature via ctx.hook) ----
arkGiftInfo() {
if (!arkAvailable() || !ark || !ark.state) return null;
maybeReconcile(); // the gift form is a send-intent signal too (throttled)
return { spendableSat: ark.balance().spendableSat };
},
arkGiftDecode(code) { return decodeArkGiftCode(code); },
// A fresh visitor (no wallets) opening a gift link lands on the gift's
// network automatically instead of being told to change Settings.
arkGiftAdoptNetwork(code) {
const g = decodeArkGiftCode(code);
if (!g || g.net === getNetwork()) return false;
setNetwork(g.net);
return true;
},
async arkGiftCreate(amountSat) {
const mgr = await connectArk();
const secret = crypto.getRandomValues(new Uint8Array(32));
const secretHex = hex.encode(secret);
const gm = await giftManager(secretHex);
const actionId = await mgr.send(gm.address(), amountSat);
const action = mgr.state.actions.find((a) => a.id === actionId);
if (!action || action.step === 'failed') throw new Error(action?.error || t('claimFailed'));
const vtxoId = decodeVtxo(hex.decode((action.destBytesList || [action.destBytes])[0])).id;
arkGiftRecords().push({ id: vtxoId, amountSat, secretHex, created: Date.now(), revoked: false, claimed: false });
mgr._save();
return { code: encodeArkGiftCode(getNetwork(), amountSat, secret), amount: amountSat };
},
// Claim-side status: claimable (with the live amount), taken, wrongnet, or
// unknown (not visible here — wrong server or not yet delivered).
async arkGiftStatus(code) {
const g = decodeArkGiftCode(code);
if (!g) return null;
if (g.net !== getNetwork()) return { state: 'wrongnet', net: g.net, amountSat: g.amountSat };
const gm = await giftManager(g.secretHex);
await gm.sync().catch(() => {});
await gm.reconcile().catch(() => {});
const amt = gm.balance().spendableSat;
if (amt > 0) return { state: 'claimable', amountSat: amt };
const seen = gm.state.vtxos.length > 0;
return { state: seen ? 'taken' : 'unknown', amountSat: g.amountSat };
},
async arkGiftClaim(code) {
if (wallet.watchOnly) throw new Error(t('arkWatchOnly'));
const mine = await connectArk();
const amount = await sweepArkGift(code, mine.address());
await mine.sync().catch(() => {}); // pull the swept vtxo in right away
mine.ackReceives(); // the gift UI celebrates; don't double-celebrate here
return amount;
},
arkGiftOutstanding() {
refreshArkGiftRecords();
return arkGiftRecords().filter((g) => !g.revoked && !g.claimed)
.map((g) => ({ id: g.id, amountSat: g.amountSat, created: g.created }));
},
async arkGiftRevoke(id) {
const g = arkGiftRecords().find((x) => x.id === id);
if (!g) throw new Error('unknown gift');
const mine = await connectArk();
const code = encodeArkGiftCode(getNetwork(), g.amountSat, hex.decode(g.secretHex));
let amount;
try {
amount = await sweepArkGift(code, mine.address());
} catch (e) {
// beaten by the claimer — record it so the row self-heals immediately
if (e && e.giftTaken) { g.claimed = true; mine._save(); }
throw e;
}
g.revoked = true;
mine._save();
await mine.sync().catch(() => {});
mine.ackReceives(); // our own sweep-back isn't a "payment received"
return amount;
},
};
}
|