This repository was archived by the owner on Feb 9, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathclightning.ts
More file actions
257 lines (230 loc) · 5.9 KB
/
Copy pathclightning.ts
File metadata and controls
257 lines (230 loc) · 5.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
import lm, { ENetworks, ldk } from '@synonymdev/react-native-ldk';
import BitcoinJsonRpc from 'bitcoin-json-rpc';
import { describe, it } from 'mocha';
import RNFS from 'react-native-fs';
import {
CL,
TestProfile,
backupServerDetails,
initWaitForElectrumToSync,
skipRemoteBackups,
sleep,
wipeLdkStorage,
} from './utils';
// import {
// broadcastTransaction,
// getBestBlock,
// getTransactionData,
// getTransactionPosition,
// } from '../ldk';
// import {
// connectToElectrum,
// getScriptPubKeyHistory,
// subscribeToHeader,
// } from '../electrum';r
// import { getAccount, getAddress, ldkNetwork } from '../utils/helpers';
// import { selectedNetwork } from '../utils/constants';
const bitcoinURL = 'http://polaruser:polarpass@localhost:9091';
const clConfig = {
host: 'localhost',
port: 18081,
rune: global.environment.lcrune,
};
describe.skip('Clightning', function () {
this.retries(3);
this.timeout(5 * 60 * 1000); // 5 minutes
let waitForElectrum: any;
const rpc = new BitcoinJsonRpc(bitcoinURL);
const cl = new CL(clConfig);
let profile: TestProfile;
before(async () => {
let balance = await rpc.getBalance();
const address = await rpc.getNewAddress();
while (balance < 10) {
await rpc.generateToAddress(10, address);
balance = await rpc.getBalance();
}
const { bech32: lndAddress } = await cl.newAddr();
await rpc.sendToAddress(lndAddress, '1');
await rpc.generateToAddress(1, await rpc.getNewAddress());
const storageRes = await lm.setBaseStoragePath(
`${RNFS.DocumentDirectoryPath}/ldk/`,
);
if (storageRes.isErr()) {
throw storageRes.error;
}
waitForElectrum = await initWaitForElectrumToSync({
electrum: { port: 60001, host: 'localhost' },
bitcoin: bitcoinURL,
clightning: clConfig,
});
await waitForElectrum({ cl: true });
});
after(async () => {
await ldk.stop();
cl.destroy();
waitForElectrum?.close();
});
beforeEach(async () => {
await waitForElectrum({ cl: true });
profile = new TestProfile({
headerCallback: async (): Promise<void> => {
const syncRes = await lm.syncLdk();
if (syncRes.isErr()) {
console.info('SYNC ERROR', syncRes.error);
throw syncRes.error;
}
},
});
await profile.init();
});
afterEach(async function () {
await profile?.cleanup();
if (this.currentTest?.state === 'failed') {
return;
}
await wipeLdkStorage();
});
it('can exchange payments', async function () {
// Test plan:
// - start LDK
// - add CL peer
// - open CL -> LDK channel
// - make a few payments
await ldk.stop();
if (!skipRemoteBackups) {
const backupRes = await ldk.backupSetup({
network: ENetworks.regtest,
seed: profile.getAccount().seed,
details: backupServerDetails,
});
if (backupRes.isErr()) {
throw backupRes.error;
}
}
const lmStart = await lm.start({
...profile.getStartParams(),
// getBestBlock: getBestBlock,
// account: account,
// getAddress: getAddress,
// getScriptPubKeyHistory: getScriptPubKeyHistory,
// getFees: () =>
// Promise.resolve({ highPriority: 10, normal: 5, background: 1 }),
// getTransactionData: getTransactionData,
// getTransactionPosition: getTransactionPosition,
// broadcastTransaction: broadcastTransaction,
// network: ldkNetwork(selectedNetwork),
});
if (lmStart.isErr()) {
throw lmStart.error;
}
const nodeId = await ldk.nodeId();
if (nodeId.isErr()) {
throw nodeId.error;
}
const { id: clPubKey } = await cl.getInfo();
const addPeer = await lm.addPeer({
pubKey: clPubKey,
address: '127.0.0.1',
port: 9736,
timeout: 5000,
});
if (addPeer.isErr()) {
throw addPeer.error;
}
// wait for peer to be connected
let n = 0;
while (true) {
const { peers } = await cl.listPeers();
const peer = peers.find((p) => p.id === nodeId.value);
if (peer && peer.connected) {
break;
}
await sleep(1000);
if (n++ === 20) {
throw new Error('Peer not connected');
}
}
// open a channel
await cl.fundchannel({
id: nodeId.value,
amount: '100000',
announce: false,
});
await rpc.generateToAddress(10, await rpc.getNewAddress());
await waitForElectrum({ cl: true });
// wait for channel to be active
n = 0;
while (true) {
const listChannels = await ldk.listChannels();
if (listChannels.isErr()) {
throw listChannels.error;
}
if (listChannels.value.length > 0 && listChannels.value[0].is_usable) {
break;
}
await sleep(1000);
if (n++ === 20) {
throw new Error('Channel not active');
}
}
// CL -> LDK, 0 sats
const inv1 = await lm.createAndStorePaymentRequest({
amountSats: 0,
description: 'trololo',
expiryDeltaSeconds: 60,
});
if (inv1.isErr()) {
throw inv1.error;
}
await cl.pay({
invoice: inv1.value.to_str,
amount: 10000 * 1000, // milli-sats
});
// CL -> LDK, 1000 sats
const inv2 = await lm.createAndStorePaymentRequest({
amountSats: 1000,
description: 'ololo',
expiryDeltaSeconds: 60,
});
if (inv2.isErr()) {
throw inv2.error;
}
await cl.pay({ invoice: inv2.value.to_str });
// LDK -> CL, 0 sats
const { bolt11: invoice3 } = await cl.genInvoice({
amount: 0,
label: 'payment3' + new Date(),
description: 'payment3',
});
const decoded1 = await ldk.decode({ paymentRequest: invoice3 });
if (decoded1.isErr()) {
throw decoded1.error;
}
const pay3 = await lm.payWithTimeout({
paymentRequest: invoice3,
amountSats: 100,
timeout: 10000,
});
if (pay3.isErr()) {
throw pay3.error;
}
// LDK -> CL, 444 sats
const { bolt11: invoice4 } = await cl.genInvoice({
amount: 444000, // milisats
label: 'payment4' + new Date(),
description: 'payment4',
});
const decoded2 = await ldk.decode({ paymentRequest: invoice4 });
if (decoded2.isErr()) {
throw decoded2.error;
}
const pay4 = await lm.payWithTimeout({
paymentRequest: invoice4,
timeout: 10000,
});
if (pay4.isErr()) {
throw pay4.error;
}
});
});