Skip to content
This repository was archived by the owner on Feb 9, 2026. It is now read-only.

Commit 57e923d

Browse files
committed
feat(lightning): Implement Reorg Checks
Implements reorg checks where needed. Sets tx as unconfirmed when a reorg or missing tx is detected. Adds unconfirmedTxs to lightning-manager.ts. Removes confirmedTxs from lightning-manager.ts. Removes leftover payWithRoute methods. Bumps version to 0.0.98.
1 parent b4a9ba4 commit 57e923d

9 files changed

Lines changed: 220 additions & 225 deletions

File tree

example/App.tsx

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -508,19 +508,6 @@ const App = (): ReactElement => {
508508
}}
509509
/>
510510

511-
<Button
512-
title={'Build route and pay'}
513-
onPress={async (): Promise<void> => {
514-
const paymentRequest = await Clipboard.getString();
515-
const payRes = await ldk.payWithRoute({ paymentRequest });
516-
if (payRes.isErr()) {
517-
return setMessage(payRes.error.message);
518-
}
519-
520-
setMessage(payRes.value);
521-
}}
522-
/>
523-
524511
<Button
525512
title={'Get network graph nodes'}
526513
onPress={async (): Promise<void> => {

example/ios/Podfile.lock

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ PODS:
302302
- React-jsinspector (0.70.6)
303303
- React-logger (0.70.6):
304304
- glog
305-
- react-native-ldk (0.0.97):
305+
- react-native-ldk (0.0.98):
306306
- React
307307
- react-native-randombytes (3.6.1):
308308
- React-Core
@@ -593,7 +593,7 @@ SPEC CHECKSUMS:
593593
React-jsiexecutor: b4a65947391c658450151275aa406f2b8263178f
594594
React-jsinspector: 60769e5a0a6d4b32294a2456077f59d0266f9a8b
595595
React-logger: 1623c216abaa88974afce404dc8f479406bbc3a0
596-
react-native-ldk: 38eb5291779f3ef181fd6472e7cd5ebf15797f8d
596+
react-native-ldk: 0b2b9c3e57c80b19d94ec306f49765b29530758e
597597
react-native-randombytes: 421f1c7d48c0af8dbcd471b0324393ebf8fe7846
598598
react-native-tcp-socket: c1b7297619616b4c9caae6889bcb0aba78086989
599599
React-perflogger: 8c79399b0500a30ee8152d0f9f11beae7fc36595
@@ -618,4 +618,4 @@ SPEC CHECKSUMS:
618618

619619
PODFILE CHECKSUM: 686e912f33d09c328c075738266ee797003d369e
620620

621-
COCOAPODS: 1.12.0
621+
COCOAPODS: 1.12.1

example/ldk/index.ts

Lines changed: 17 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ export const setupLdk = async (): Promise<Result<string>> => {
176176
*/
177177
export const getTransactionData = async (
178178
txId: string = '',
179-
): Promise<TTransactionData> => {
179+
): Promise<TTransactionData | undefined> => {
180180
let transactionData = DefaultTransactionDataShape;
181181
const data = {
182182
key: 'tx_hash',
@@ -190,6 +190,22 @@ export const getTransactionData = async (
190190
txHashes: data,
191191
network: selectedNetwork,
192192
});
193+
if (
194+
//TODO: Update types for electrum response.
195+
// @ts-ignore
196+
response?.error &&
197+
//TODO: Update types for electrum response.
198+
// @ts-ignore
199+
response?.error?.message &&
200+
/No such mempool or blockchain transaction|Invalid tx hash/.test(
201+
//TODO: Update types for electrum response.
202+
// @ts-ignore
203+
response?.error?.message,
204+
)
205+
) {
206+
//Transaction may have been removed/bumped from the mempool or potentially reorg'd out.
207+
return undefined;
208+
}
193209

194210
if (response.error || !response.data || response.data[0].error) {
195211
return transactionData;
@@ -321,31 +337,3 @@ export const importAccount = async (
321337
await syncLdk();
322338
return ok(importResponse.value);
323339
};
324-
325-
/**
326-
* Iterates over watch transactions for spends. Sets them as confirmed as needed.
327-
* @returns {Promise<boolean>}
328-
*/
329-
export const checkWatchTxs = async (): Promise<boolean> => {
330-
const checkedScriptPubKeys: string[] = [];
331-
const watchTransactionIds = lm.watchTxs.map((tx) => tx.txid);
332-
for (const watchTx of lm.watchTxs) {
333-
if (!checkedScriptPubKeys.includes(watchTx.script_pubkey)) {
334-
const scriptPubKeyHistory: { txid: string; height: number }[] =
335-
await getScriptPubKeyHistory(watchTx.script_pubkey);
336-
for (const data of scriptPubKeyHistory) {
337-
if (!watchTransactionIds.includes(data?.txid)) {
338-
const txData = await getTransactionData(data?.txid);
339-
await ldk.setTxConfirmed({
340-
header: txData.header,
341-
height: txData.height,
342-
txData: [{ transaction: txData.transaction, pos: 0 }],
343-
});
344-
return true;
345-
}
346-
}
347-
checkedScriptPubKeys.push(watchTx.script_pubkey);
348-
}
349-
}
350-
return false;
351-
};

lib/ios/Ldk.m

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -112,17 +112,6 @@ @interface RCT_EXTERN_MODULE(Ldk, NSObject)
112112
timeoutSeconds:(NSInteger *)timeoutSeconds
113113
resolve:(RCTPromiseResolveBlock)resolve
114114
reject:(RCTPromiseRejectBlock)reject)
115-
RCT_EXTERN_METHOD(payWithRoute:(NSArray *)route
116-
destinationNodeId:(NSString *)destinationNodeId
117-
amountSats:(NSInteger *)amountSats
118-
cltvExpiryDelta:(NSInteger *)cltvExpiryDelta
119-
paymentHash:(NSString *)paymentHash
120-
paymentSecret:(NSString *)paymentSecret
121-
resolve:(RCTPromiseResolveBlock)resolve
122-
reject:(RCTPromiseRejectBlock)reject)
123-
RCT_EXTERN_METHOD(payWithRoute2:(NSString *)payReq
124-
resolve:(RCTPromiseResolveBlock)resolve
125-
reject:(RCTPromiseRejectBlock)reject)
126115
RCT_EXTERN_METHOD(abandonPayment:(NSString *)paymentId
127116
resolve:(RCTPromiseResolveBlock)resolve
128117
reject:(RCTPromiseRejectBlock)reject)

lib/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@synonymdev/react-native-ldk",
33
"title": "React Native LDK",
4-
"version": "0.0.97",
4+
"version": "0.0.98",
55
"description": "React Native wrapper for LDK",
66
"main": "./dist/index.js",
77
"types": "./dist/index.d.ts",

lib/src/ldk.ts

Lines changed: 23 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import {
1717
TPaymentReq,
1818
TCreatePaymentReq,
1919
TSetTxConfirmedReq,
20-
TSetTxUnconfirmedReq,
2120
TInitNetworkGraphReq,
2221
TCloseChannelReq,
2322
TSpendOutputsReq,
@@ -166,11 +165,8 @@ class LDK {
166165
* Accepts array of hex encoded channel manager and channel monitors from storage.
167166
* NOTE: If empty channelManagerSerialized string then initChannelManager will create a new channel manager.
168167
* https://docs.rs/lightning/latest/lightning/ln/channelmanager/index.html
169-
* @param network
170-
* @param channelManagerSerialized
171-
* @param channelMonitorsSerialized
172-
* @param bestBlock
173-
* @returns {Promise<Err<unknown> | Ok<Ok<string> | Err<string>>>}
168+
* @param {TInitChannelManagerReq} data
169+
* @returns {Promise<Result<string>>}
174170
*/
175171
async initChannelManager(
176172
data: TInitChannelManagerReq,
@@ -243,9 +239,8 @@ class LDK {
243239

244240
/**
245241
* If set will write all LDK logging to file.
246-
* @param level
247-
* @param active
248-
* @returns {Promise<Err<unknown> | Ok<Ok<string> | Err<string>>>}
242+
* @param {string} path
243+
* @returns {Promise<Result<string>>}
249244
*/
250245
async setLogFilePath(path: string): Promise<Result<string>> {
251246
try {
@@ -258,8 +253,9 @@ class LDK {
258253

259254
/**
260255
* Write a line to current LDK log file
261-
* @param line
262-
* @returns {Promise<Err<unknown> | Ok<Ok<string> | Err<string>>>}
256+
* @param {'error' | 'info' | 'debug'} type
257+
* @param {string} line
258+
* @returns {Promise<Result<string>>}
263259
*/
264260
async writeToLogFile(
265261
type: 'error' | 'info' | 'debug',
@@ -325,9 +321,8 @@ class LDK {
325321

326322
/**
327323
* Sets current best block on channelManager and chainMonitor
328-
* @param header
329-
* @param height
330-
* @returns {Promise<Err<unknown> | Ok<Ok<string> | Err<string>>>}
324+
* @param {THeader} tip
325+
* @returns {Promise<Result<string>>}
331326
*/
332327
async syncToTip(tip: THeader): Promise<Result<string>> {
333328
const { hex, hash, height } = tip;
@@ -343,11 +338,8 @@ class LDK {
343338

344339
/**
345340
* Connect to remote peer
346-
* @param pubKey
347-
* @param address
348-
* @param port
349-
* @param timeout (Android only)
350-
* @returns {Promise<Err<unknown> | Ok<Ok<string> | Err<string>>>}
341+
* @param {TAddPeerReq} peer
342+
* @returns {Promise<Result<string>>}
351343
*/
352344
async addPeer(peer: TAddPeerReq): Promise<Result<string>> {
353345
const { pubKey, address, port, timeout } = peer;
@@ -363,10 +355,9 @@ class LDK {
363355

364356
/**
365357
* Updates a watched transaction as confirmed
366-
* @param txId
367-
* @param transaction
358+
* @param header
359+
* @param txData
368360
* @param height
369-
* @param pos
370361
* @returns {Promise<Err<unknown> | Ok<Ok<string> | Err<string>>>}
371362
*/
372363
async setTxConfirmed({
@@ -386,14 +377,12 @@ class LDK {
386377

387378
/**
388379
* Updates a watched transaction as unconfirmed in the event of a reorg
389-
* @param txId
390-
* @returns {Promise<Err<unknown> | Ok<Ok<string> | Err<string>>>}
380+
* @param {string} txid
381+
* @returns {Promise<Result<string>>}
391382
*/
392-
async setTxUnconfirmed({
393-
txId,
394-
}: TSetTxUnconfirmedReq): Promise<Result<string>> {
383+
async setTxUnconfirmed(txid: string): Promise<Result<string>> {
395384
try {
396-
const res = await NativeLDK.setTxUnconfirmed(txId);
385+
const res = await NativeLDK.setTxUnconfirmed(txid);
397386
this.writeDebugToLog('setTxUnconfirmed');
398387
return ok(res);
399388
} catch (e) {
@@ -480,7 +469,7 @@ class LDK {
480469
/**
481470
* Decodes a bolt11 payment request
482471
* @param paymentRequest
483-
* @returns {Promise<Ok<any> | Err<unknown>>}
472+
* @returns {Promise<Result<TInvoice>>}
484473
*/
485474
async decode({ paymentRequest }: TPaymentReq): Promise<Result<TInvoice>> {
486475
const cleanedPaymentRequest = extractPaymentRequest(paymentRequest);
@@ -496,8 +485,9 @@ class LDK {
496485

497486
/**
498487
* Creates bolt11 payment request
499-
* @param amountSats
500-
* @param description
488+
* @param {number | undefined} amountSats
489+
* @param {string} description
490+
* @param {number} expiryDeltaSeconds
501491
* @returns {Promise<Ok<Ok<TInvoice> | Err<TInvoice>> | Err<unknown>>}
502492
*/
503493
async createPaymentRequest({
@@ -915,8 +905,7 @@ class LDK {
915905

916906
/**
917907
* Fetches full list of nodes and their details
918-
* @param nodeId
919-
* @returns {Promise<Ok<Ok<TNetworkGraphChannelInfo> | Err<string>> | Err<unknown>>}
908+
* @returns {Promise<Result<TNetworkGraphNodeInfo[]>>}
920909
*/
921910
async completeGraphNodes(): Promise<Result<TNetworkGraphNodeInfo[]>> {
922911
try {
@@ -982,8 +971,7 @@ class LDK {
982971

983972
/**
984973
* Fetches full list of channels and their details
985-
* @param shortChannelId
986-
* @returns {Promise<Ok<Ok<TNetworkGraphChannelInfo> | Err<string>> | Err<unknown>>}
974+
* @returns {Promise<Result<TNetworkGraphChannelInfo[]>>}
987975
*/
988976
async completeGraphChannels(): Promise<Result<TNetworkGraphChannelInfo[]>> {
989977
try {

0 commit comments

Comments
 (0)