Skip to content

Commit f27c36c

Browse files
author
Conor Okus
committed
Remove Java code snippets
1 parent 964c93a commit f27c36c

File tree

5 files changed

+56
-398
lines changed

5 files changed

+56
-398
lines changed

docs/tutorials/building-a-node-with-ldk/connect-to-peers.md

Lines changed: 5 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
In this section you'll learn how to join the lightning network.
44

5-
Firstly we need to have the ability to do high performance I/O operations. LDK provides default implementations for initializing all of your networking needs. If you are using Rust, you can use our simple socket handling library `lightning_net_tokio`. In Java you can use the `NioPeerHandler` which uses Java's NIO I/O interface.
5+
Firstly we need to have the ability to do high performance I/O operations. LDK provides default implementations for initializing all of your networking needs. If you are using Rust, you can use our simple socket handling library `lightning_net_tokio`. In Kotlin/Java you can use the `NioPeerHandler` which uses Java's NIO I/O interface.
66

77
**What it's used for**: making peer connections, facilitating peer data to and from LDK
88

9-
<CodeSwitcher :languages="{rust:'Rust', java:'Java', kotlin:'Kotlin'}">
9+
<CodeSwitcher :languages="{rust:'Rust', kotlin:'Kotlin'}">
1010
<template v-slot:rust>
1111

1212
```rust
@@ -31,17 +31,7 @@ loop {
3131

3232
</template>
3333

34-
<template v-slot:java>
35-
36-
```java
37-
final NioPeerHandler peerHandler = channelManagerConstructor.nio_peer_handler;
38-
final int port = 9730;
39-
peerHandler.bind_listener(new InetSocketAddress("0.0.0.0", port));
40-
```
41-
42-
</template>
43-
44-
<template v-slot:kotlin>
34+
<template v-slot:kotlin>
4535

4636
```kotlin
4737
val nioPeerHandler = channelManagerConstructor.nio_peer_handler
@@ -53,10 +43,9 @@ nioPeerHandler.bind_listener(InetSocketAddress("127.0.0.1", port))
5343
</CodeSwitcher>
5444

5545

56-
5746
Connections to other peers are established with `PeerManager`. You'll need to know the pubkey and address of another node that you want as a peer. Once the connection is established and the handshake is complete, `PeerManager` will show the peer's pubkey in its list of peers.
5847

59-
<CodeSwitcher :languages="{rust:'Rust', java:'Java', kotlin:'Kotlin'}">
48+
<CodeSwitcher :languages="{rust:'Rust', kotlin:'Kotlin'}">
6049
<template v-slot:rust>
6150

6251
```rust
@@ -85,25 +74,7 @@ match lightning_net_tokio::connect_outbound(Arc::clone(&peer_manager), pubkey, a
8574

8675
</template>
8776

88-
<template v-slot:java>
89-
90-
```java
91-
try {
92-
// Connect and wait for the handshake to complete.
93-
SocketAddress address = new InetSocketAddress(host, port);
94-
nio_peer_handler.connect(pubkey, address);
95-
96-
// The peer's pubkey will be present in the list of peer ids.
97-
final PeerManager peer_manager = channel_manager_constructor.peer_manager;
98-
byte[][] peer_node_ids = peer_manager.get_peer_node_ids();
99-
} catch (java.io.IOException e) {
100-
// Handle failure to successfully connect to a peer.
101-
}
102-
```
103-
104-
</template>
105-
106-
<template v-slot:kotlin>
77+
<template v-slot:kotlin>
10778

10879
```kotlin
10980
try {

docs/tutorials/building-a-node-with-ldk/handling-events.md

Lines changed: 14 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ LDK requires that you handle many different events throughout your app's life cy
44

55
To start handling events in your application, run:
66

7-
<CodeSwitcher :languages="{rust:'Rust', java:'Java', kotlin:'Kotlin'}">
7+
<CodeSwitcher :languages="{rust:'Rust', kotlin:'Kotlin'}">
88
<template v-slot:rust>
99

1010
```rust
@@ -22,44 +22,23 @@ To start handling events in your application, run:
2222
}
2323
```
2424
</template>
25-
<template v-slot:java>
26-
27-
```java
28-
import org.ldk.batteries.ChannelManagerConstructor
29-
30-
ChannelManagerConstructor channelManagerConstructor = new ChannelManagerConstructor(
31-
Network.LDKNetwork_Bitcoin,
32-
UserConfig.default(),
33-
latestBlockHash,
34-
latestBlockHeight,
35-
keysManager.as_KeysInterface(),
36-
feeEstimator,
37-
chainMonitor,
38-
router,
39-
txBroadcaster,
40-
logger
41-
);
42-
```
4325

44-
</template>
45-
46-
<template v-slot:kotlin>
26+
<template v-slot:kotlin>
4727

4828
```kotlin
49-
import org.ldk.batteries.ChannelManagerConstructor
29+
import org.ldk.structs.Event
30+
31+
if (event is Event.PaymentSent) {
32+
// Handle successful payment
33+
}
34+
35+
if (event is Event.PaymentFailed) {
36+
// Handle failed payment
37+
}
5038

51-
val channelManagerConstructor = ChannelManagerConstructor(
52-
Network.LDKNetwork_Regtest,
53-
userConfig,
54-
latestBlockHash,
55-
latestBlockHeight,
56-
keysManager.as_KeysInterface(),
57-
feeEstimator,
58-
chainMonitor,
59-
router,
60-
txBroadcaster,
61-
logger
62-
);
39+
if (event is Event.FundingGenerationReady) {
40+
// Create a funding tx to be broadcast
41+
}
6342
```
6443

6544
</template>

docs/tutorials/building-a-node-with-ldk/opening-a-channel.md

Lines changed: 18 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Now that you have a peer, you can open a channel with them using `ChannelManager
1111

1212
Channels can be announced to the network or can remain private, which is controlled via `UserConfig::announced_channel`.
1313

14-
<CodeSwitcher :languages="{rust:'Rust', java:'Java', kotlin:'Kotlin'}">
14+
<CodeSwitcher :languages="{rust:'Rust', kotlin:'Kotlin'}">
1515
<template v-slot:rust>
1616

1717
```rust
@@ -29,21 +29,7 @@ match channel_manager.create_channel(pubkey, amount, push_msat, user_id, Some(co
2929
```
3030

3131
</template>
32-
<template v-slot:java>
33-
34-
```java
35-
long amount = 10_000L;
36-
long push_msat = 1_000L;
37-
long user_id = 42L;
38-
UserConfig config = UserConfig.with_defaults();
39-
config.get_channel_options().set_announced_channel(true);
40-
41-
Result_NoneAPIErrorZ create_channel_result = channel_manager.create_channel(
42-
pubkey, amount, push_msat, user_id, config);
43-
assert create_channel_result instanceof Result_NoneAPIErrorZ.Result_NoneAPIErrorZ_OK;
44-
```
4532

46-
</template>
4733
<template v-slot:kotlin>
4834

4935
```kotlin
@@ -72,7 +58,7 @@ val createChannelResult = channelManager.create_channel(
7258
At this point, an outbound channel has been initiated with your peer and it will appear in `ChannelManager::list_channels`. However, the channel is not yet funded. Once your peer accepts the channel, you will be notified with a `FundingGenerationReady` event. It's then your responsibility to construct the funding transaction and pass it to ChannelManager, which will broadcast it once it receives your channel counterparty's signature.
7359

7460

75-
<CodeSwitcher :languages="{rust:'Rust', java:'Java', kotlin:'Kotlin'}">
61+
<CodeSwitcher :languages="{rust:'Rust', kotlin:'Kotlin'}">
7662
<template v-slot:rust>
7763

7864
```rust
@@ -114,51 +100,6 @@ match event {
114100

115101
</template>
116102

117-
<template v-slot:java>
118-
119-
```java
120-
// After the peer responds with an `accept_channel` message, an
121-
// Event.FundingGenerationReady event will be generated.
122-
123-
// In the `handle_event` method of ChannelManagerPersister implementation
124-
if (e instanceof Event.FundingGenerationReady) {
125-
Event.FundingGenerationReady event = (Event.FundingGenerationReady) e;
126-
byte[] funding_scriptpubkey = event.output_script;
127-
long output_value = event.channel_value_satoshis;
128-
129-
// This is the same channel created earler
130-
assert event.user_channel_id == 42;
131-
132-
// The output is always a P2WSH:
133-
assert funding_scriptpubkey.length == 34 && funding_scriptpubkey[0] == 0 &&
134-
funding_scriptpubkey[1] == 32;
135-
136-
// Generate the funding transaction for the channel based on the channel amount
137-
// The following uses the bitcoinj library to do so, but you can use any
138-
// standard Bitcoin library for on-chain logic.
139-
NetworkParameters bitcoinj_net =
140-
NetworkParameters.fromID(NetworkParameters.ID_MAINNET);
141-
Transaction funding_tx = new Transaction(bitcoinj_net);
142-
funding_tx.addInput(new TransactionInput(bitcoinj_net, funding, new byte[0]));
143-
// Note that all inputs in the funding transaction MUST spend SegWit outputs
144-
// (and have witnesses)
145-
funding_tx.getInputs().get(0).setWitness(new TransactionWitness(2));
146-
funding_tx.getInput(0).getWitness().setPush(0, new byte[]{0x1});
147-
funding_tx.addOutput(Coin.SATOSHI.multiply(output_value),
148-
new Script(funding_scriptpubkey));
149-
150-
// Give the funding transaction back to the ChannelManager.
151-
Result_NoneAPIErrorZ funding_res = channel_manager.funding_transaction_generated(
152-
event.temporary_channel_id, funding_tx.bitcoinSerialize());
153-
// funding_transaction_generated should only generate an error if the
154-
// transaction didn't meet the required format (or the counterparty already
155-
// closed the channel on us):
156-
assert funding_res instanceof Result_NoneAPIErrorZ.Result_NoneAPIErrorZ_OK;
157-
}
158-
```
159-
160-
</template>
161-
162103
<template v-slot:kotlin>
163104

164105
```kotlin
@@ -170,8 +111,8 @@ if (event is Event.FundingGenerationReady) {
170111

171112
if (funding_spk.size == 34 && funding_spk[0].toInt() == 0 && funding_spk[1].toInt() == 32) {
172113
// Generate the funding transaction for the channel based on the channel amount
173-
// The following uses BDK for on-chain logic
174-
val rawTx = OnchainWallet.buildFundingTx(event.channel_value_satoshis, event.output_script)
114+
// The following uses BDK (Bitcoin Dev Kit) for on-chain logic
115+
val rawTx = buildFundingTx(event.channel_value_satoshis, event.output_script)
175116

176117
channelManager.funding_transaction_generated(
177118
event.temporary_channel_id,
@@ -180,15 +121,26 @@ if (event is Event.FundingGenerationReady) {
180121
)
181122
}
182123
}
124+
125+
fun buildFundingTx(value: Long, script: ByteArray): ByteArray {
126+
val scriptListUByte: List<UByte> = script.toUByteArray().asList()
127+
val outputScript = Script(scriptListUByte)
128+
val (psbt, _) = TxBuilder()
129+
.addRecipient(outputScript, value.toULong())
130+
.feeRate(4.0F)
131+
.finish(onchainWallet)
132+
sign(psbt)
133+
val rawTx = psbt.extractTx().toUByteArray().toByteArray()
134+
return rawTx
135+
}
183136
```
184137

185138
</template>
186139

187-
188-
189-
190140
</CodeSwitcher>
191141

142+
**References:** [Rust `FundingGenerationReady` docs](https://docs.rs/lightning/*/lightning/util/events/enum.Event.html#variant.FundingGenerationReady),
143+
192144

193145

194146

0 commit comments

Comments
 (0)