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
|
import { expect, test } from "@playwright/test";
import {
aliceUsername,
alicePassword,
bobUsername,
bobPassword,
testSecret,
arkWalletPassword,
apiBaseUrl,
login,
loginNewContext,
ensureArkAccount,
createArkInvoiceViaUI,
sendArkFromTestEndpoint,
waitForInvoicePaid,
fillNumpadAmount,
waitForSendComplete,
waitForDashboard,
waitForArkSync,
dockerExec,
} from "./helpers";
// All ark tests share the server wallet and must run serially
test.describe.serial("ark vault tests", () => {
test("bob receives 100 sats in ark vault after server wallet send", async ({ page }) => {
test.setTimeout(90_000);
test.skip(!testSecret, "E2E_TEST_SECRET is required for /test/ark/send");
await login(page, bobUsername, bobPassword);
await ensureArkAccount(page, arkWalletPassword);
await page.goto(`/${bobUsername}`);
await waitForDashboard(page);
const { invoiceId, address } = await createArkInvoiceViaUI(page, arkWalletPassword);
console.log(`[e2e] Bob ark address: ${address}, invoice: ${invoiceId}`);
await sendArkFromTestEndpoint(page, address, 100, invoiceId);
const status = await waitForInvoicePaid(page, invoiceId, 100);
expect(status.received).toBeGreaterThanOrEqual(100);
console.log(
`[e2e] ark invoice ${invoiceId} funded with 100 sats (received=${status?.received || 0})`,
);
});
test("vault snapshot is cached after receiving funds", async ({ page }) => {
test.setTimeout(90_000);
test.skip(!testSecret, "E2E_TEST_SECRET is required for /test/ark/send");
await login(page, bobUsername, bobPassword);
await ensureArkAccount(page, arkWalletPassword);
await page.goto(`/${bobUsername}`);
await waitForDashboard(page);
const { invoiceId, address } = await createArkInvoiceViaUI(page, arkWalletPassword);
console.log(`[e2e] Bob ark address: ${address}, invoice: ${invoiceId}`);
await sendArkFromTestEndpoint(page, address, 100, invoiceId);
const status = await waitForInvoicePaid(page, invoiceId, 100);
expect(status.received).toBeGreaterThanOrEqual(100);
// Navigate to dashboard to trigger arkSync which calls cacheVaultSnapshot
await page.goto(`/${bobUsername}`);
await waitForDashboard(page);
await page.waitForTimeout(5_000);
// Poll localStorage for the snapshot
const snapshot = await page.evaluate(async () => {
for (let i = 0; i < 30; i++) {
for (let j = 0; j < localStorage.length; j++) {
const k = localStorage.key(j)!;
if (k.startsWith("arkVaultSnapshot")) {
const snap = JSON.parse(localStorage.getItem(k)!);
if (snap?.vtxos?.length > 0) return snap;
}
}
await new Promise((r) => setTimeout(r, 1000));
}
return null;
});
expect(snapshot, "Snapshot should exist in localStorage").toBeTruthy();
expect(snapshot.version).toBe(1);
expect(snapshot.arkServerUrl).toBeTruthy();
expect(snapshot.arkInfoRaw).toBeTruthy();
expect(Object.keys(snapshot.arkInfoRaw).length).toBeGreaterThan(0);
expect(Array.isArray(snapshot.vtxos)).toBe(true);
expect(snapshot.vtxos.length).toBeGreaterThanOrEqual(1);
expect(snapshot.vtxoChains).toBeTruthy();
expect(typeof snapshot.vtxoChains).toBe("object");
expect(snapshot.virtualTxs).toBeTruthy();
expect(typeof snapshot.virtualTxs).toBe("object");
console.log(
`[e2e] Snapshot cached: ${snapshot.vtxos.length} vtxos, ${Object.keys(snapshot.vtxoChains).length} chains, ${Object.keys(snapshot.virtualTxs).length} virtualTxs`,
);
});
test("custodial account receives ark payment via vault forward", async ({ page }) => {
test.setTimeout(120_000);
test.skip(!testSecret, "E2E_TEST_SECRET is required");
await login(page, bobUsername, bobPassword);
await ensureArkAccount(page, arkWalletPassword);
await page.goto(`/${bobUsername}`);
await waitForDashboard(page);
const { invoiceId, address } = await createArkInvoiceViaUI(page, arkWalletPassword);
console.log(`[e2e] Created ark invoice ${invoiceId}, address: ${address}`);
await page.goto(`/invoice/${invoiceId}`);
await page.waitForURL(new RegExp(`/invoice/${invoiceId}`));
await sendArkFromTestEndpoint(page, address, 1000, invoiceId);
console.log(`[e2e] Sent 1000 sats to vault address`);
// Wait for custodial forward: vault receives → arkSync detects → client forwards
let paid = false;
let status: any;
for (let i = 0; i < 60; i++) {
const res = await page.request.get(`${apiBaseUrl}/invoice/${invoiceId}`);
if (res.ok()) {
status = await res.json();
if ((status?.received || 0) >= 1000) {
paid = true;
break;
}
}
await page.waitForTimeout(2000);
}
expect(paid, `Custodial ark forward did not complete: ${JSON.stringify(status)}`).toBeTruthy();
console.log(`[e2e] Custodial ark receive complete: received=${status?.received}`);
});
test("vault sends to own custodial account", async ({ page }) => {
test.slow();
test.setTimeout(180_000);
test.skip(!testSecret, "E2E_TEST_SECRET is required for ark tests");
await login(page, bobUsername, bobPassword);
await ensureArkAccount(page, arkWalletPassword);
await page.goto(`/${bobUsername}`);
await waitForDashboard(page);
const { invoiceId, address } = await createArkInvoiceViaUI(page, arkWalletPassword);
console.log(`[e2e] Bob vault address: ${address}, invoice: ${invoiceId}`);
const fundAmount = 5000;
await sendArkFromTestEndpoint(page, address, fundAmount, invoiceId);
await waitForInvoicePaid(page, invoiceId, fundAmount);
console.log(`[e2e] Vault funded with ${fundAmount} sats`);
await waitForArkSync(page, bobUsername);
const balance = await page.evaluate(async () => {
const { getBalance } = await import("/src/lib/ark.ts");
return await getBalance();
});
console.log(`[e2e] Vault balance: ${JSON.stringify(balance)}`);
expect(balance.available + balance.preconfirmed).toBeGreaterThanOrEqual(fundAmount);
const accountsRes = await page.request.get(`${apiBaseUrl}/accounts`);
const accounts = await accountsRes.json();
const arkAccount = accounts.find((a: any) => a.type === "ark");
expect(arkAccount, "Ark account should exist").toBeTruthy();
const accountRes = await page.request.get(`${apiBaseUrl}/account/${arkAccount.id}`);
const accountInfo = await accountRes.json();
const serverArkAddress = accountInfo.arkAddress;
const sendAmount = 1000;
const txid = await page.evaluate(
async ({ serverArkAddress, amount }) => {
const { sendArk } = await import("/src/lib/ark.ts");
return await sendArk(serverArkAddress, amount);
},
{ serverArkAddress, amount: sendAmount },
);
console.log(`[e2e] Ark send txid: ${txid}`);
expect(txid).toBeTruthy();
const frontendBase = "http://127.0.0.1:4173";
const vaultSendRes = await page.request.post(`${frontendBase}/api/ark/vault-send`, {
data: { hash: txid, amount: sendAmount, aid: arkAccount.id },
});
expect(vaultSendRes.ok(), `vault-send: ${await vaultSendRes.text()}`).toBeTruthy();
const invoiceRes = await page.request.post(`${frontendBase}/api/invoice`, {
data: { invoice: { type: "ark", amount: sendAmount, aid: arkAccount.id } },
});
expect(invoiceRes.ok(), `invoice: ${await invoiceRes.text()}`).toBeTruthy();
const inv = await invoiceRes.json();
const receiveRes = await page.request.post(`${frontendBase}/api/ark/receive`, {
data: { amount: sendAmount, hash: txid, iid: inv.id },
});
expect(receiveRes.ok(), `receive: ${await receiveRes.text()}`).toBeTruthy();
const payment = await receiveRes.json();
console.log(`[e2e] Vault→custodial payment complete, id: ${payment.id}`);
expect(payment.id).toBeTruthy();
});
test("vault sends to external Liquid address via forward", async ({ page }) => {
test.slow();
test.setTimeout(180_000);
test.skip(!testSecret, "E2E_TEST_SECRET is required for ark tests");
await login(page, aliceUsername, alicePassword);
await ensureArkAccount(page, arkWalletPassword);
await page.goto(`/${aliceUsername}`);
await waitForDashboard(page);
const { invoiceId, address: arkAddress } = await createArkInvoiceViaUI(
page,
arkWalletPassword,
);
console.log(`[e2e] Alice vault address: ${arkAddress}, invoice: ${invoiceId}`);
const fundAmount = 2000;
await sendArkFromTestEndpoint(page, arkAddress, fundAmount, invoiceId);
await waitForInvoicePaid(page, invoiceId, fundAmount);
console.log(`[e2e] Vault funded with ${fundAmount} sats`);
await waitForArkSync(page, aliceUsername);
const balance = await page.evaluate(async () => {
const { getBalance } = await import("/src/lib/ark.ts");
return await getBalance();
});
console.log(`[e2e] Vault balance: ${JSON.stringify(balance)}`);
expect(balance.available + balance.preconfirmed).toBeGreaterThanOrEqual(fundAmount);
const accountsRes = await page.request.get(`${apiBaseUrl}/accounts`);
const accounts = await accountsRes.json();
const arkAccount = accounts.find((a: any) => a.type === "ark");
expect(arkAccount).toBeTruthy();
const accountRes = await page.request.get(`${apiBaseUrl}/account/${arkAccount.id}`);
const accountInfo = await accountRes.json();
const serverArkAddress = accountInfo.arkAddress;
const liquidAddress = dockerExec("lq", "elements-cli getnewaddress");
console.log(`[e2e] External Liquid address: ${liquidAddress}`);
const sendAmount = 1000;
const txid = await page.evaluate(
async ({ serverArkAddress, amount }) => {
const { sendArk } = await import("/src/lib/ark.ts");
return await sendArk(serverArkAddress, amount);
},
{ serverArkAddress, amount: sendAmount },
);
console.log(`[e2e] Ark send txid: ${txid}`);
expect(txid).toBeTruthy();
const frontendBase = "http://127.0.0.1:4173";
const vaultSendRes = await page.request.post(`${frontendBase}/api/ark/vault-send`, {
data: { hash: txid, amount: sendAmount, aid: arkAccount.id },
});
expect(vaultSendRes.ok(), `vault-send: ${await vaultSendRes.text()}`).toBeTruthy();
const invoiceRes = await page.request.post(`${frontendBase}/api/invoice`, {
data: {
invoice: { type: "ark", amount: sendAmount, aid: arkAccount.id, forward: liquidAddress },
},
});
expect(invoiceRes.ok(), `invoice: ${await invoiceRes.text()}`).toBeTruthy();
const inv = await invoiceRes.json();
const receiveRes = await page.request.post(`${frontendBase}/api/ark/receive`, {
data: { amount: sendAmount, hash: txid, iid: inv.id },
});
expect(receiveRes.ok(), `receive: ${await receiveRes.text()}`).toBeTruthy();
const payment = await receiveRes.json();
console.log(`[e2e] Vault→Liquid forward payment complete, id: ${payment.id}`);
expect(payment.id).toBeTruthy();
});
test("custodial sends to external ark address", async ({ browser }) => {
test.setTimeout(120_000);
test.skip(!testSecret, "E2E_TEST_SECRET is required for ark tests");
// Alice: get her ark vault address
const { context: aliceContext, page: alicePage } = await loginNewContext(
browser,
aliceUsername,
alicePassword,
);
await ensureArkAccount(alicePage, arkWalletPassword);
await alicePage.goto(`/${aliceUsername}`);
await waitForDashboard(alicePage);
const { address: arkAddress } = await createArkInvoiceViaUI(alicePage, arkWalletPassword);
console.log(`[e2e] Alice ark address: ${arkAddress}`);
await aliceContext.close();
// Bob: send to alice's ark address from custodial
const { context: bobContext, page: bobPage } = await loginNewContext(
browser,
bobUsername,
bobPassword,
);
await bobPage.goto(`/send/ark/${arkAddress}`);
await bobPage.waitForLoadState("domcontentloaded");
console.log(`[e2e] Bob navigated to: ${bobPage.url()}`);
await fillNumpadAmount(bobPage, 100);
await bobPage.waitForURL(/\/send\/ark\/[^/]+\/\d+/, { timeout: 10_000 });
console.log(`[e2e] Bob on ark send confirmation: ${bobPage.url()}`);
const sendButton = bobPage.locator('button[type="submit"]');
await expect(sendButton).toBeVisible({ timeout: 5_000 });
await sendButton.click();
try {
await waitForSendComplete(bobPage, 60_000);
} catch (e: any) {
if (/VTXO_RECOVERABLE|insufficient funds|not enough liquidity/i.test(e.message)) {
test.skip(true, `Ark server has no liquidity: ${e.message}`);
}
throw e;
}
console.log(`[e2e] Bob ark payment sent: ${bobPage.url()}`);
await bobContext.close();
});
});
|