Skip to content

Commit a205c22

Browse files
committed
feat(lightning): debug function to manual set tx as confirmed
1 parent 1f8c588 commit a205c22

File tree

1 file changed

+163
-1
lines changed

1 file changed

+163
-1
lines changed

src/screens/Settings/DevSettings/LdkDebug.tsx

Lines changed: 163 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import Clipboard from '@react-native-clipboard/clipboard';
2-
import lm from '@synonymdev/react-native-ldk';
2+
import lm, { ldk } from '@synonymdev/react-native-ldk';
33
import React, { ReactElement, memo, useState } from 'react';
44
import { useTranslation } from 'react-i18next';
55
import { ScrollView, StyleSheet, TouchableOpacity, View } from 'react-native';
@@ -46,11 +46,13 @@ const LdkDebug = (): ReactElement => {
4646
const dispatch = useAppDispatch();
4747
const sheetRef = useSheetRef('forceTransfer');
4848
const [peer, setPeer] = useState('');
49+
const [txid, setTxid] = useState('');
4950
const [payingInvoice, setPayingInvoice] = useState(false);
5051
const [refreshingLdk, setRefreshingLdk] = useState(false);
5152
const [restartingLdk, setRestartingLdk] = useState(false);
5253
const [rebroadcastingLdk, setRebroadcastingLdk] = useState(false);
5354
const [spendingStuckOutputs, setSpendingStuckOutputs] = useState(false);
55+
const [settingConfirmedTx, setSettingConfirmedTx] = useState(false);
5456

5557
const { localBalance, remoteBalance } = useLightningBalance();
5658
const selectedWallet = useAppSelector(selectedWalletSelector);
@@ -324,6 +326,141 @@ const LdkDebug = (): ReactElement => {
324326
setPayingInvoice(false);
325327
};
326328

329+
const onSetConfirmedTx = async (): Promise<void> => {
330+
if (!txid) {
331+
// Attempt to grab and set txid string from clipboard.
332+
const clipboardStr = await Clipboard.getString();
333+
setTxid(clipboardStr);
334+
return;
335+
}
336+
337+
setSettingConfirmedTx(true);
338+
try {
339+
// Get network endpoint
340+
const baseUrl = 'https://mempool.space/api';
341+
342+
// Fetch transaction details
343+
const txResponse = await fetch(`${baseUrl}/tx/${txid.trim()}`);
344+
if (!txResponse.ok) {
345+
showToast({
346+
type: 'error',
347+
title: 'Transaction Not Found',
348+
description: 'Unable to find transaction on mempool.space',
349+
});
350+
setSettingConfirmedTx(false);
351+
return;
352+
}
353+
354+
const tx = await txResponse.json();
355+
356+
// Check if transaction is confirmed
357+
if (!tx.status?.confirmed || !tx.status?.block_height) {
358+
showToast({
359+
type: 'error',
360+
title: 'Transaction Not Confirmed',
361+
description: 'Transaction is not yet confirmed on the blockchain',
362+
});
363+
setSettingConfirmedTx(false);
364+
return;
365+
}
366+
367+
// Fetch transaction hex data
368+
const txHexResponse = await fetch(`${baseUrl}/tx/${txid.trim()}/hex`);
369+
if (!txHexResponse.ok) {
370+
showToast({
371+
type: 'error',
372+
title: 'Transaction Hex Error',
373+
description: 'Unable to fetch transaction hex data',
374+
});
375+
setSettingConfirmedTx(false);
376+
return;
377+
}
378+
379+
const txHex = await txHexResponse.text();
380+
381+
// Fetch block header
382+
const blockHash = tx.status.block_hash;
383+
const blockResponse = await fetch(`${baseUrl}/block/${blockHash}/header`);
384+
if (!blockResponse.ok) {
385+
showToast({
386+
type: 'error',
387+
title: 'Block Header Error',
388+
description: 'Unable to fetch block header',
389+
});
390+
setSettingConfirmedTx(false);
391+
return;
392+
}
393+
394+
const blockHeader = await blockResponse.text();
395+
396+
// Validate all required parameters
397+
if (!blockHeader) {
398+
showToast({
399+
type: 'error',
400+
title: 'Missing Block Header',
401+
description: 'Block header is empty or invalid',
402+
});
403+
setSettingConfirmedTx(false);
404+
return;
405+
}
406+
407+
if (!txHex) {
408+
showToast({
409+
type: 'error',
410+
title: 'Missing Transaction Data',
411+
description: 'Transaction hex data is missing',
412+
});
413+
setSettingConfirmedTx(false);
414+
return;
415+
}
416+
417+
if (!tx.status.block_height) {
418+
showToast({
419+
type: 'error',
420+
title: 'Missing Block Height',
421+
description: 'Block height is missing or invalid',
422+
});
423+
setSettingConfirmedTx(false);
424+
return;
425+
}
426+
427+
// Call ldk.setTxConfirmed
428+
const setTxConfirmedRes = await ldk.setTxConfirmed({
429+
header: blockHeader,
430+
txData: [
431+
{
432+
transaction: txHex,
433+
pos: tx.status.block_time || 0, // Using block_time as position fallback
434+
},
435+
],
436+
height: tx.status.block_height,
437+
});
438+
439+
if (setTxConfirmedRes.isErr()) {
440+
showToast({
441+
type: 'error',
442+
title: 'Set Confirmed Failed',
443+
description: setTxConfirmedRes.error.message,
444+
});
445+
} else {
446+
showToast({
447+
type: 'success',
448+
title: 'Transaction Confirmed',
449+
description: `Transaction ${txid.slice(0, 8)}... set as confirmed at height ${tx.status.block_height}`,
450+
});
451+
}
452+
} catch (error) {
453+
showToast({
454+
type: 'error',
455+
title: 'Error',
456+
description:
457+
error instanceof Error ? error.message : 'Unknown error occurred',
458+
});
459+
} finally {
460+
setSettingConfirmedTx(false);
461+
}
462+
};
463+
327464
return (
328465
<ThemedView style={styles.root}>
329466
<SafeAreaInset type="top" />
@@ -356,6 +493,31 @@ const LdkDebug = (): ReactElement => {
356493
onPress={onAddPeer}
357494
/>
358495

496+
<Caption13Up style={styles.sectionTitle} color="secondary">
497+
Set Confirmed Transaction
498+
</Caption13Up>
499+
<TextInput
500+
style={styles.textInput}
501+
autoCapitalize="none"
502+
autoComplete="off"
503+
autoCorrect={false}
504+
autoFocus={false}
505+
value={txid}
506+
placeholder="Transaction ID"
507+
returnKeyType="done"
508+
testID="TxidInput"
509+
onChangeText={setTxid}
510+
/>
511+
<Button
512+
style={styles.button}
513+
text={
514+
txid ? 'Set Confirmed Tx' : 'Paste Transaction ID From Clipboard'
515+
}
516+
loading={settingConfirmedTx}
517+
testID="SetConfirmedTxButton"
518+
onPress={onSetConfirmedTx}
519+
/>
520+
359521
<Caption13Up style={styles.sectionTitle} color="secondary">
360522
Debug
361523
</Caption13Up>

0 commit comments

Comments
 (0)