Skip to content

Commit 00133c0

Browse files
committed
feat: example app channel funding
1 parent 31e22d3 commit 00133c0

File tree

3 files changed

+93
-21
lines changed

3 files changed

+93
-21
lines changed

example/Dev.tsx

Lines changed: 80 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,12 @@ import lm, {
2525
TChannelUpdate,
2626
} from '@synonymdev/react-native-ldk';
2727
import { backupServerDetails, peers } from './utils/constants';
28-
import { createNewAccount, getAccount, getAddress } from './utils/helpers';
28+
import {
29+
createNewAccount,
30+
getAccount,
31+
getAddress,
32+
getAddressFromScriptPubKey,
33+
} from './utils/helpers';
2934
import RNFS from 'react-native-fs';
3035

3136
let logSubscription: EmitterSubscription | undefined;
@@ -41,6 +46,7 @@ const Dev = (): ReactElement => {
4146
const [nodeStarted, setNodeStarted] = useState(false);
4247
const [showLogs, setShowLogs] = useState(false);
4348
const [logContent, setLogContent] = useState('');
49+
const [temporaryChannelId, setTemporaryChannelId] = useState(''); //For funding channels locally
4450

4551
useEffect(() => {
4652
//Restarting LDK on each code update causes constant errors.
@@ -231,7 +237,6 @@ const Dev = (): ReactElement => {
231237
}
232238
}}
233239
/>
234-
235240
<Button
236241
title={'Get Node ID'}
237242
onPress={async (): Promise<void> => {
@@ -246,7 +251,6 @@ const Dev = (): ReactElement => {
246251
setMessage(`Node ID: ${nodeIdRes.value}`);
247252
}}
248253
/>
249-
250254
<Button
251255
title={'Sync LDK'}
252256
onPress={async (): Promise<void> => {
@@ -258,7 +262,6 @@ const Dev = (): ReactElement => {
258262
setMessage(syncRes.value);
259263
}}
260264
/>
261-
262265
<View style={styles.buttonRow}>
263266
<Button
264267
title={'Add Peers'}
@@ -299,7 +302,6 @@ const Dev = (): ReactElement => {
299302
}}
300303
/>
301304
</View>
302-
303305
<View style={styles.buttonRow}>
304306
<Button
305307
title={'List channels'}
@@ -393,7 +395,6 @@ const Dev = (): ReactElement => {
393395
}}
394396
/>
395397
</View>
396-
397398
<View style={styles.buttonRow}>
398399
<Button
399400
title={'List watch transactions'}
@@ -409,7 +410,6 @@ const Dev = (): ReactElement => {
409410
}}
410411
/>
411412
</View>
412-
413413
<Button
414414
title={'🤑Get Address Balance'}
415415
onPress={async (): Promise<void> => {
@@ -421,7 +421,6 @@ const Dev = (): ReactElement => {
421421
);
422422
}}
423423
/>
424-
425424
<Button
426425
title={'Recover all outputs attempt'}
427426
onPress={async (): Promise<void> => {
@@ -435,7 +434,6 @@ const Dev = (): ReactElement => {
435434
setMessage(res.value);
436435
}}
437436
/>
438-
439437
<View style={styles.buttonRow}>
440438
<Button
441439
title={'Get claimed payments'}
@@ -454,7 +452,6 @@ const Dev = (): ReactElement => {
454452
}}
455453
/>
456454
</View>
457-
458455
<View style={styles.buttonRow}>
459456
<Button
460457
title={'Create invoice'}
@@ -548,7 +545,6 @@ const Dev = (): ReactElement => {
548545
}}
549546
/>
550547
</View>
551-
552548
<Button
553549
title={'Get network graph nodes'}
554550
onPress={async (): Promise<void> => {
@@ -574,7 +570,6 @@ const Dev = (): ReactElement => {
574570
setMessage(`${msg}`);
575571
}}
576572
/>
577-
578573
<Button
579574
title={'Show claimable balances for closed/closing channels'}
580575
onPress={async (): Promise<void> => {
@@ -586,7 +581,6 @@ const Dev = (): ReactElement => {
586581
setMessage(JSON.stringify(balances.value));
587582
}}
588583
/>
589-
590584
<Button
591585
title={'List channel monitors'}
592586
onPress={async (): Promise<void> => {
@@ -604,6 +598,79 @@ const Dev = (): ReactElement => {
604598
}}
605599
/>
606600

601+
<Button
602+
title={'Start manual channel open ⛓️'}
603+
onPress={async (): Promise<void> => {
604+
try {
605+
const res = await lm.createChannel({
606+
counterPartyNodeId: peers.lnd.pubKey,
607+
channelValueSats: 20000,
608+
pushSats: 5000,
609+
});
610+
if (res.isErr()) {
611+
setMessage(res.error.message);
612+
return;
613+
}
614+
615+
const { value_satoshis, output_script, temp_channel_id } =
616+
res.value;
617+
618+
setTemporaryChannelId(temp_channel_id);
619+
620+
console.log(res.value);
621+
622+
const address = getAddressFromScriptPubKey(output_script);
623+
const btc = value_satoshis / 100000000;
624+
625+
console.log('***BITCOIND CLI***');
626+
console.log('bitcoin-cli listunspent');
627+
console.log(
628+
`bitcoin-cli createrawtransaction '[{"txid":"<tx-id>","vout":<index>}]' '{"${address}":${btc}}'`,
629+
);
630+
console.log(
631+
`bitcoin-cli signrawtransactionwithwallet "<raw-transaction-hex>"`,
632+
);
633+
console.log('********');
634+
635+
setMessage(
636+
`Create tx with ${value_satoshis} for address ${address}. See logs for bitcoin-cli commands needed to create funding tx.`,
637+
);
638+
} catch (e) {
639+
setMessage(JSON.stringify(e));
640+
}
641+
}}
642+
/>
643+
644+
{temporaryChannelId ? (
645+
<Button
646+
title={'Fund channel (paste psbt)'}
647+
onPress={async (): Promise<void> => {
648+
try {
649+
if (!temporaryChannelId) {
650+
return setMessage('Create channel first');
651+
}
652+
653+
const pastedSignedTx = await Clipboard.getString();
654+
655+
const res = await ldk.fundChannel({
656+
temporaryChannelId,
657+
counterPartyNodeId: peers.lnd.pubKey,
658+
fundingTransaction: pastedSignedTx,
659+
});
660+
if (res.isErr()) {
661+
setMessage(res.error.message);
662+
return;
663+
}
664+
665+
setMessage('Channel funded');
666+
setTemporaryChannelId('');
667+
} catch (e) {
668+
setMessage(JSON.stringify(e));
669+
}
670+
}}
671+
/>
672+
) : null}
673+
607674
<Button
608675
title={'Show version'}
609676
onPress={async (): Promise<void> => {
@@ -615,7 +682,6 @@ const Dev = (): ReactElement => {
615682
setMessage(ldkVersion.value.ldk);
616683
}}
617684
/>
618-
619685
<Button
620686
title={'Show LDK logs'}
621687
onPress={async (): Promise<void> => {
@@ -631,7 +697,6 @@ const Dev = (): ReactElement => {
631697
}
632698
}}
633699
/>
634-
635700
<Button
636701
title={'Restore backup from server'}
637702
onPress={async (): Promise<void> => {
@@ -658,7 +723,6 @@ const Dev = (): ReactElement => {
658723
setMessage('Successfully restored wallet');
659724
}}
660725
/>
661-
662726
<Button
663727
title={'Backup self check'}
664728
onPress={async (): Promise<void> => {
@@ -671,7 +735,6 @@ const Dev = (): ReactElement => {
671735
setMessage('Backup server check passed ✅');
672736
}}
673737
/>
674-
675738
<Button
676739
title={'Restart node'}
677740
onPress={async (): Promise<void> => {
@@ -699,7 +762,6 @@ const Dev = (): ReactElement => {
699762
setMessage(res.value);
700763
}}
701764
/>
702-
703765
<Button
704766
title={'Node state'}
705767
onPress={async (): Promise<void> => {
@@ -712,7 +774,6 @@ const Dev = (): ReactElement => {
712774
setMessage(res.value);
713775
}}
714776
/>
715-
716777
<Button
717778
title={'List backed up files'}
718779
onPress={async (): Promise<void> => {
@@ -726,7 +787,6 @@ const Dev = (): ReactElement => {
726787
setMessage(JSON.stringify(res.value));
727788
}}
728789
/>
729-
730790
<Button
731791
title={'Test file backup'}
732792
onPress={async (): Promise<void> => {

lib/android/src/main/java/com/reactnativeldk/LdkModule.kt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -813,6 +813,18 @@ class LdkModule(reactContext: ReactApplicationContext) : ReactContextBaseJavaMod
813813
return
814814
}
815815

816+
val error = res as Result_NoneAPIErrorZ.Result_NoneAPIErrorZ_Err
817+
818+
if (error.err is APIError.APIMisuseError) {
819+
return handleReject(promise, LdkErrors.channel_accept_fail, Error((error.err as APIError.APIMisuseError).err))
820+
}
821+
822+
if (error.err is APIError.ChannelUnavailable) {
823+
return handleReject(promise, LdkErrors.channel_accept_fail, Error((error.err as APIError.ChannelUnavailable).err))
824+
}
825+
826+
LdkEventEmitter.send(EventTypes.native_log, "Fund channel failed. ${error.err}")
827+
816828
handleReject(promise, LdkErrors.fund_channel_fail)
817829
}
818830

lib/android/src/main/java/com/reactnativeldk/classes/LdkChannelManagerPersister.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class LdkChannelManagerPersister: ChannelManagerConstructor.EventHandler {
2020
val body = Arguments.createMap()
2121
body.putHexString("temp_channel_id", fundingGenerationReady.temporary_channel_id._a)
2222
body.putHexString("output_script", fundingGenerationReady.output_script)
23-
body.putString("user_channel_id", fundingGenerationReady.user_channel_id.leBytes.hexEncodedString())
23+
body.putHexString("user_channel_id", fundingGenerationReady.user_channel_id.leBytes)
2424
body.putInt("value_satoshis", fundingGenerationReady.channel_value_satoshis.toInt())
2525
return LdkEventEmitter.send(EventTypes.channel_manager_funding_generation_ready, body)
2626
}

0 commit comments

Comments
 (0)