Skip to content

Commit

Permalink
feat: example app channel funding
Browse files Browse the repository at this point in the history
Update lightning-manager.ts
  • Loading branch information
Jasonvdb committed Aug 30, 2024
1 parent 31e22d3 commit 6e19491
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 23 deletions.
100 changes: 80 additions & 20 deletions example/Dev.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,12 @@ import lm, {
TChannelUpdate,
} from '@synonymdev/react-native-ldk';
import { backupServerDetails, peers } from './utils/constants';
import { createNewAccount, getAccount, getAddress } from './utils/helpers';
import {
createNewAccount,
getAccount,
getAddress,
getAddressFromScriptPubKey,
} from './utils/helpers';
import RNFS from 'react-native-fs';

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

useEffect(() => {
//Restarting LDK on each code update causes constant errors.
Expand Down Expand Up @@ -231,7 +237,6 @@ const Dev = (): ReactElement => {
}
}}
/>

<Button
title={'Get Node ID'}
onPress={async (): Promise<void> => {
Expand All @@ -246,7 +251,6 @@ const Dev = (): ReactElement => {
setMessage(`Node ID: ${nodeIdRes.value}`);
}}
/>

<Button
title={'Sync LDK'}
onPress={async (): Promise<void> => {
Expand All @@ -258,7 +262,6 @@ const Dev = (): ReactElement => {
setMessage(syncRes.value);
}}
/>

<View style={styles.buttonRow}>
<Button
title={'Add Peers'}
Expand Down Expand Up @@ -299,7 +302,6 @@ const Dev = (): ReactElement => {
}}
/>
</View>

<View style={styles.buttonRow}>
<Button
title={'List channels'}
Expand Down Expand Up @@ -393,7 +395,6 @@ const Dev = (): ReactElement => {
}}
/>
</View>

<View style={styles.buttonRow}>
<Button
title={'List watch transactions'}
Expand All @@ -409,7 +410,6 @@ const Dev = (): ReactElement => {
}}
/>
</View>

<Button
title={'🤑Get Address Balance'}
onPress={async (): Promise<void> => {
Expand All @@ -421,7 +421,6 @@ const Dev = (): ReactElement => {
);
}}
/>

<Button
title={'Recover all outputs attempt'}
onPress={async (): Promise<void> => {
Expand All @@ -435,7 +434,6 @@ const Dev = (): ReactElement => {
setMessage(res.value);
}}
/>

<View style={styles.buttonRow}>
<Button
title={'Get claimed payments'}
Expand All @@ -454,7 +452,6 @@ const Dev = (): ReactElement => {
}}
/>
</View>

<View style={styles.buttonRow}>
<Button
title={'Create invoice'}
Expand Down Expand Up @@ -548,7 +545,6 @@ const Dev = (): ReactElement => {
}}
/>
</View>

<Button
title={'Get network graph nodes'}
onPress={async (): Promise<void> => {
Expand All @@ -574,7 +570,6 @@ const Dev = (): ReactElement => {
setMessage(`${msg}`);
}}
/>

<Button
title={'Show claimable balances for closed/closing channels'}
onPress={async (): Promise<void> => {
Expand All @@ -586,7 +581,6 @@ const Dev = (): ReactElement => {
setMessage(JSON.stringify(balances.value));
}}
/>

<Button
title={'List channel monitors'}
onPress={async (): Promise<void> => {
Expand All @@ -604,6 +598,79 @@ const Dev = (): ReactElement => {
}}
/>

<Button
title={'Start manual channel open ⛓️'}
onPress={async (): Promise<void> => {
try {
const res = await lm.createChannel({
counterPartyNodeId: peers.lnd.pubKey,
channelValueSats: 20000,
pushSats: 5000,
});
if (res.isErr()) {
setMessage(res.error.message);
return;
}

const { value_satoshis, output_script, temp_channel_id } =
res.value;

setTemporaryChannelId(temp_channel_id);

console.log(res.value);

const address = getAddressFromScriptPubKey(output_script);
const btc = value_satoshis / 100000000;

console.log('***BITCOIND CLI***');
console.log('bitcoin-cli listunspent');
console.log(
`bitcoin-cli createrawtransaction '[{"txid":"<tx-id>","vout":<index>}]' '{"${address}":${btc}}'`,
);
console.log(
`bitcoin-cli signrawtransactionwithwallet "<raw-transaction-hex>"`,

Check warning on line 631 in example/Dev.tsx

View workflow job for this annotation

GitHub Actions / Run lint check

Strings must use singlequote
);
console.log('********');

setMessage(
`Create tx with ${value_satoshis} for address ${address}. See logs for bitcoin-cli commands needed to create funding tx.`,
);
} catch (e) {
setMessage(JSON.stringify(e));
}
}}
/>

{temporaryChannelId ? (
<Button
title={'Fund channel (paste psbt)'}
onPress={async (): Promise<void> => {
try {
if (!temporaryChannelId) {
return setMessage('Create channel first');
}

const pastedSignedTx = await Clipboard.getString();

const res = await ldk.fundChannel({
temporaryChannelId,
counterPartyNodeId: peers.lnd.pubKey,
fundingTransaction: pastedSignedTx,
});
if (res.isErr()) {
setMessage(res.error.message);
return;
}

setMessage('Channel funded');
setTemporaryChannelId('');
} catch (e) {
setMessage(JSON.stringify(e));
}
}}
/>
) : null}

<Button
title={'Show version'}
onPress={async (): Promise<void> => {
Expand All @@ -615,7 +682,6 @@ const Dev = (): ReactElement => {
setMessage(ldkVersion.value.ldk);
}}
/>

<Button
title={'Show LDK logs'}
onPress={async (): Promise<void> => {
Expand All @@ -631,7 +697,6 @@ const Dev = (): ReactElement => {
}
}}
/>

<Button
title={'Restore backup from server'}
onPress={async (): Promise<void> => {
Expand All @@ -658,7 +723,6 @@ const Dev = (): ReactElement => {
setMessage('Successfully restored wallet');
}}
/>

<Button
title={'Backup self check'}
onPress={async (): Promise<void> => {
Expand All @@ -671,7 +735,6 @@ const Dev = (): ReactElement => {
setMessage('Backup server check passed ✅');
}}
/>

<Button
title={'Restart node'}
onPress={async (): Promise<void> => {
Expand Down Expand Up @@ -699,7 +762,6 @@ const Dev = (): ReactElement => {
setMessage(res.value);
}}
/>

<Button
title={'Node state'}
onPress={async (): Promise<void> => {
Expand All @@ -712,7 +774,6 @@ const Dev = (): ReactElement => {
setMessage(res.value);
}}
/>

<Button
title={'List backed up files'}
onPress={async (): Promise<void> => {
Expand All @@ -726,7 +787,6 @@ const Dev = (): ReactElement => {
setMessage(JSON.stringify(res.value));
}}
/>

<Button
title={'Test file backup'}
onPress={async (): Promise<void> => {
Expand Down
12 changes: 12 additions & 0 deletions lib/android/src/main/java/com/reactnativeldk/LdkModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -813,6 +813,18 @@ class LdkModule(reactContext: ReactApplicationContext) : ReactContextBaseJavaMod
return
}

val error = res as Result_NoneAPIErrorZ.Result_NoneAPIErrorZ_Err

if (error.err is APIError.APIMisuseError) {
return handleReject(promise, LdkErrors.channel_accept_fail, Error((error.err as APIError.APIMisuseError).err))
}

if (error.err is APIError.ChannelUnavailable) {
return handleReject(promise, LdkErrors.channel_accept_fail, Error((error.err as APIError.ChannelUnavailable).err))
}

LdkEventEmitter.send(EventTypes.native_log, "Fund channel failed. ${error.err}")

handleReject(promise, LdkErrors.fund_channel_fail)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class LdkChannelManagerPersister: ChannelManagerConstructor.EventHandler {
val body = Arguments.createMap()
body.putHexString("temp_channel_id", fundingGenerationReady.temporary_channel_id._a)
body.putHexString("output_script", fundingGenerationReady.output_script)
body.putString("user_channel_id", fundingGenerationReady.user_channel_id.leBytes.hexEncodedString())
body.putHexString("user_channel_id", fundingGenerationReady.user_channel_id.leBytes)
body.putInt("value_satoshis", fundingGenerationReady.channel_value_satoshis.toInt())
return LdkEventEmitter.send(EventTypes.channel_manager_funding_generation_ready, body)
}
Expand Down
2 changes: 0 additions & 2 deletions lib/src/lightning-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2469,8 +2469,6 @@ class LightningManager {
});
});
}

//TODO: fund channel
}

export default new LightningManager();

0 comments on commit 6e19491

Please sign in to comment.