-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathtest.helper.ts
More file actions
72 lines (63 loc) · 2.88 KB
/
test.helper.ts
File metadata and controls
72 lines (63 loc) · 2.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import * as fs from 'fs';
import * as crypto from 'crypto';
import * as bip39 from 'bip39';
import { Credentials, Scheduler, Signer, Node, Handle } from '../index.js';
export const lspInfo = () => ({
rpcSocket: process.env.LSP_RPC_SOCKET!,
nodeId: process.env.LSP_NODE_ID!,
});
export async function fundWallet(node: Node, amountSats = 100_000_000): Promise<boolean> {
const testSetupServerUrl = process.env.TEST_SETUP_SERVER_URL!;
if (!testSetupServerUrl) throw new Error('TEST_SETUP_SERVER_URL not set');
const address = (await node.onchainReceive()).bech32;
const res = await fetch(`${testSetupServerUrl}/fund-wallet`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ address, amount: amountSats }),
});
if (!res.ok) {
const error = await res.text();
throw new Error(`fundNode failed (${res.status}): ${error}`);
}
// Wait for node to detect the confirmed UTXO
const deadline = Date.now() + 30_000;
while (Date.now() < deadline) {
const funds = await node.listFunds();
if ((funds.outputs ?? []).length > 0) return true;
await new Promise((r) => setTimeout(r, 1_000));
}
throw new Error('fundNode timed out waiting for node to detect funds');
}
export async function getGLNode(scheduler: Scheduler, connectToLSP: boolean = true): Promise<{ node: Node; handle: Handle }> {
const mnemonic = bip39.entropyToMnemonic(crypto.randomBytes(16).toString('hex'));
const secret = bip39.mnemonicToSeedSync(mnemonic);
const signer = new Signer(mnemonic);
let credentials: Credentials;
if (connectToLSP) {
const testSetupServerUrl = process.env.TEST_SETUP_SERVER_URL!;
if (!testSetupServerUrl) throw new Error('TEST_SETUP_SERVER_URL not set');
const res = await fetch(`${testSetupServerUrl}/connect-to-lsp`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ secret: secret.toString('hex') }),
});
if (!res.ok) throw new Error(`Failed to connect node to LSP: ${await res.text()}`);
const { creds_path } = await res.json();
credentials = await Credentials.load(fs.readFileSync(creds_path));
} else {
credentials = await scheduler.register(signer);
}
return { node: new Node(credentials), handle: await signer.start() };
}
export async function getLspInvoice(amountMsat: number = 0): Promise<string> {
const testSetupServerUrl = process.env.TEST_SETUP_SERVER_URL!;
if (!testSetupServerUrl) throw new Error('TEST_SETUP_SERVER_URL not set');
const res = await fetch(`${testSetupServerUrl}/lsp-invoice`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ amount_msat: amountMsat, label: `test-${Date.now()}`, description: 'Test payment' }),
});
if (!res.ok) throw new Error(`Failed to get LSP invoice: ${await res.text()}`);
const { bolt11 } = await res.json();
return bolt11;
}