Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consolidates building a node tutorial #156

Merged
Merged
Show file tree
Hide file tree
Changes from 38 commits
Commits
Show all changes
57 commits
Select commit Hold shift + click to select a range
9c54349
Adds tutorial introudction page
Aug 1, 2022
ce32f6e
Consolidate building a node tutorials
Aug 8, 2022
c069152
Adds P2PGossip Sync docs
Aug 22, 2022
7d94135
Adds setting up a PeerManager page
Sep 8, 2022
db4dc1d
Rebase
Sep 14, 2023
c99e9f1
Adds initializing networking to connect peers page
Sep 16, 2022
a9ded9a
Adds opening a channel page
Nov 1, 2022
b3b90e4
Move return type from register_output
Jan 6, 2023
a508b5c
Adds opening a channel page
Jan 9, 2023
b5e80e9
Trigger build
Jan 9, 2023
35a3bcf
Adds FundingGenerationReady Event Handling section
Jan 9, 2023
05e2898
Improve fee estimator code examples
Jan 25, 2023
a619831
Rename variables in broadcaster code samples
Jan 25, 2023
e9549da
Removes channel_watch instructions
Jan 25, 2023
32b076e
Removes channel_watch instructions
Jan 25, 2023
5365b0e
Adds handling events page
Feb 23, 2023
3bba318
Link to Rust API docs
Mar 14, 2023
ad64818
Remove Java code snippets
Mar 14, 2023
30a369a
Temporarily removes java snippets + adds notes on using segwit inputs
Mar 20, 2023
f7020da
Adds tx broadcaster snippets
Mar 21, 2023
9884d1c
Start sending payments page
Mar 24, 2023
b07affa
Adds Swift code snippets
Sep 14, 2023
7ee3e76
Add closing channel Swift example
Sep 14, 2023
9bafe12
Add RGS Swift snippets
Sep 18, 2023
cc966d2
Updated tutorials for LDK 0.0.116
Sep 21, 2023
216fed8
Updated Closing Channel Tutorial
Sep 21, 2023
53e79e1
Remove JavaScript add Router
Sep 25, 2023
40e4ea8
Update broadcaster interface examples
Nov 2, 2023
9bf3bce
Adds BDK create funding transaction rust example
Nov 2, 2023
16f7882
Update MessageHandler parameters
Nov 2, 2023
1aed094
Update KeysManager link
Dec 4, 2023
1fb74d5
Update ChannelManager section
Jan 19, 2024
a3b54e1
Update Handling Events section
Jan 19, 2024
72e25c1
Adds closing channel page
Jan 24, 2024
69e0ce1
Point to API's
Feb 3, 2024
5044bdc
Restructure nav
Feb 3, 2024
4e9d35f
Trigger Build
Feb 3, 2024
5e80f4e
Trigger Build
Feb 3, 2024
39e95b4
Use latest instead of version numbers
Feb 6, 2024
43b12ca
Adds back in background processing
Feb 6, 2024
31f155f
Switch component ordering
Feb 6, 2024
d26a8e8
Copy update for setting up a channel manager
Feb 6, 2024
5787ac6
s/module/crate
Feb 6, 2024
a5504a1
Electrum/Esplora available in transaction-sync
Feb 6, 2024
74a2f49
s/module/crate
Feb 6, 2024
63d3ead
Trigger build
Feb 6, 2024
387ac95
Revert "Trigger build"
Feb 6, 2024
09ee30d
Remove unused files
Feb 7, 2024
7831e7e
Adds missing link
Feb 7, 2024
8f276a0
Removes older version of channel manager initialisation
Feb 7, 2024
d41fecb
Copy update
Feb 8, 2024
5763d65
Copy updates for installation page
Feb 8, 2024
d745489
Update docs/building-a-node-with-ldk/setting-up-a-channel-manager.md
ConorOkus Feb 8, 2024
eb7ca72
Copy update
Feb 8, 2024
88fed6f
Update docs/building-a-node-with-ldk/setting-up-a-peer-manager.md
ConorOkus Feb 8, 2024
9ea1442
Update docs/building-a-node-with-ldk/setting-up-a-peer-manager.md
ConorOkus Feb 8, 2024
cd008da
Update docs/building-a-node-with-ldk/setting-up-a-peer-manager.md
ConorOkus Feb 8, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
167 changes: 83 additions & 84 deletions docs/.vuepress/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const themeConfig = require("@spiralbtc/vuepress-devkit-theme/config");
const title = "Lightning Dev Kit Documentation";
const baseUrl = "https://lightningdevkit.org";
const githubUrl = "https://github.com/lightningdevkit";
const discordUrl = "https://discord.gg/xaYE3pDQpm";
const discordUrl = "https://discord.gg/5AcknnMfBw";
const themeColor = "#ffffff";

const docsSidebar = [
Expand All @@ -16,35 +16,60 @@ const docsSidebar = [
title: "Introduction",
path: "/introduction/",
collapsable: true,
children: [["/introduction/use_cases", "Use Cases"]],
},
"/running-a-sample-ldk-node",
{
title: "Overview",
collapsable: true,
children: [
["/overview/architecture", "Architecture"],
["/overview/peer-management", "Peer Management"],
["/overview/persistent_storage", "Persistent Storage"],
["/overview/blockchain_data", "Blockchain Data"],
["/overview/wallet_management", "Wallet Management"],
["/overview/networking", "Networking"],
["/overview/private_key_management", "Private Key Management"],
["/overview/transactions", "Transactions"],
["/overview/random_number_generation", "Random Number Generation"],
["/introduction/use-cases", "Use Cases"],
["/introduction/architecture", "Architecture"],
["/introduction/peer-management", "Peer Management"],
["/introduction/persistent_storage", "Persistent Storage"],
["/introduction/blockchain_data", "Blockchain Data"],
["/introduction/wallet_management", "Wallet Management"],
["/introduction/networking", "Networking"],
["/introduction/private_key_management", "Private Key Management"],
["/introduction/transactions", "Transactions"],
["/introduction/random_number_generation", "Random Number Generation"],
],
},
{
title: "Payments",
path: "/payments/",
title: "Building a node with LDK",
collapsable: true,
children: [
["/payments/connecting_peers", "Connecting Peers"],
["/payments/managing_channels", "Managing Channels"],
["/payments/sending_payments", "Sending Payments"],
["/payments/receiving_payments", "Receiving Payments"],
["/building-a-node-with-ldk/introduction", "Introduction"],
["/building-a-node-with-ldk/installation", "Installation"],
[
"/building-a-node-with-ldk/setting-up-a-channel-manager",
"Setting up a Channel Manager",
],
[
"/building-a-node-with-ldk/handling-events",
"Handling Events",
],
[
"/building-a-node-with-ldk/setting-up-a-peer-manager",
"Setting up a Peer Manager",
],
[
"/building-a-node-with-ldk/connect-to-peers",
"Connect to Peers",
],
[
"/building-a-node-with-ldk/opening-a-channel",
"Opening a Channel",
],
[
"/building-a-node-with-ldk/sending-payments",
"Sending Payments",
],
[
"/building-a-node-with-ldk/receiving-payments",
"Receiving Payments",
],
[
"/building-a-node-with-ldk/closing-a-channel",
"Closing a Channel",
],
],
},
"/running-a-sample-ldk-node",
{
title: "Blockchain Data",
collapsable: true,
Expand Down Expand Up @@ -109,29 +134,13 @@ const docsSidebar = [
],
},
[
"https://github.com/lightningdevkit/ldk-garbagecollected/tree/main/src/main/java/org/ldk",
"Java/Kotlin",
],
["https://github.com/lightningdevkit/ldk-swift", "Swift"],
[
"https://github.com/lightningdevkit/ldk-garbagecollected/tree/main/ts",
"TypeScript",
"https://github.com/arik-so/SwiftLightning/tree/master/Documentation",
"Swift",
],
],
},
];

const tutorialSidebar = [
{
title: "Tutorials",
collapsable: false,
children: [
"/tutorials/getting-started",
"/tutorials/build_a_node_in_java",
"/tutorials/build_a_node_in_rust",
],
},
];

const blogSidebar = [
{
Expand Down Expand Up @@ -186,13 +195,9 @@ module.exports = {
text: "Docs",
link: "/introduction/",
},
{
text: "Tutorials",
link: "/tutorials/getting-started",
},
{
text: "Case Studies",
link: "/case-studies",
link: "/case-studies/",
},
{
text: "Blog",
Expand All @@ -212,11 +217,43 @@ module.exports = {
sidebar: {
"/_blog/": blogSidebar,
"/blog/": blogSidebar,
"/tutorials/": tutorialSidebar,
"/": docsSidebar,
},
footer: {
links: [
{
title: "Docs",
children: [
{
text: "Introduction",
link: "/introduction/",
},
{
text: "Building a node with LDK",
link: "/building-a-node-with-ldk/introduction",
},
{
text: "Running a sample LDK node",
link: "/running-a-sample-ldk-node/",
},
{
text: "Architecture",
link: "/introduction/architecture/",
},
{
text: "Blockchain Data",
link: "/blockchain_data/introduction/",
},
{
text: "Key Management",
link: "/key_management/",
},
{
text: "Examples",
link: "/examples/",
},
],
},
{
title: "Community",
children: [
Expand Down Expand Up @@ -252,39 +289,6 @@ module.exports = {
},
],
},
{
title: "Docs",
children: [
{
text: "Introduction",
link: "/introduction/",
},
{
text: "Sample LDK node",
link: "/running-a-sample-ldk-node/",
},
{
text: "Architecture",
link: "/overview/architecture/",
},
{
text: "Payments",
link: "/payments/",
},
{
text: "Blockchain Data",
link: "/blockchain_data/introduction/",
},
{
text: "Key Management",
link: "/key_management/",
},
{
text: "Examples",
link: "/examples/",
},
],
},
{
title: "Resources",
children: [
Expand All @@ -301,18 +305,13 @@ module.exports = {
{
title: "Other",
children: [
{
text: "Tutorials",
link: "/tutorials/getting-started/",
},
{
text: "Bitcoin Dev Kit",
link: "https://bitcoindevkit.org/",
rel: "noopener noreferrer",
},
{
text: "Reporting a Vulnerability",
link: "https://github.com/lightningdevkit/rust-lightning/blob/main/SECURITY.md",
rel: "noopener noreferrer",
},
],
Expand Down
150 changes: 150 additions & 0 deletions docs/building-a-node-with-ldk/closing-a-channel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
# Closing a Channel

Begins the process of closing a channel. After this call (plus some timeout), no new HTLCs will be accepted on the given channel, and after additional timeout/the closing of all pending HTLCs, the channel will be closed on chain.

<CodeSwitcher :languages="{rust:'Rust', kotlin:'Kotlin', swift:'Swift'}">
<template v-slot:rust>

```rust
let channel_id = channel_manager
.list_channels()
.iter()
.find(|channel| channel.user_id == user_id)
.expect("ERROR: Channel not found")
.channel_id;

// Example: Cooperative close
channel_manager.close_channel(&channel_id).expect("ERROR: Failed to close channel");

// Example: Unilateral close
channel_manager.force_close_channel(&channel_id).expect("ERROR: Failed to close channel");
```
</template>
<template v-slot:kotlin>

```kotlin
val res = channelManager.close_channel(channelId, pubKey)

if (res is Result_NoneAPIErrorZ.Result_NoneAPIErrorZ_Err) {
// Handle error
}

if (res.is_ok) {
// Handle successful close
}
```

</template>
<template v-slot:swift>

```Swift
let channelId: [UInt8] = // Add Channel Id in bytes
let counterpartyNodeId: [UInt8] = // Add Counterparty Node Id in bytes
let res = channelManager.closeChannel(channelId: channelId, counterpartyNodeId: counterpartyNodeId)
if res!.isOk() {
// Channel Closed
}
```

</template>
</CodeSwitcher>


To claim Funds directly into a custom wallet like BDK wallet using a custom `KeysManager` see the [Key Management](/key_management.md) guide for more info.

# SpendableOutputs Event Handling

<CodeSwitcher :languages="{rust:'Rust', kotlin:'Kotlin', swift:'Swift'}">
<template v-slot:rust>

```rust
Event::SpendableOutputs { outputs, channel_id: _ } => {
// SpendableOutputDescriptors, of which outputs is a vec of, are critical to keep track
// of! While a `StaticOutput` descriptor is just an output to a static, well-known key,
// other descriptors are not currently ever regenerated for you by LDK. Once we return
// from this method, the descriptor will be gone, and you may lose track of some funds.
//
// Here we simply persist them to disk, with a background task running which will try
// to spend them regularly (possibly duplicatively/RBF'ing them). These can just be
// treated as normal funds where possible - they are only spendable by us and there is
// no rush to claim them.
for output in outputs {
let key = hex_utils::hex_str(&keys_manager.get_secure_random_bytes());
// Note that if the type here changes our read code needs to change as well.
let output: SpendableOutputDescriptor = output;
fs_store.write(PENDING_SPENDABLE_OUTPUT_DIR, "", &key, &output.encode()).unwrap();
}
}

```

</template>
<template v-slot:kotlin>

```kotlin
// Example where we spend straight to our BDK based wallet
if (event is Event.SpendableOutputs) {
val outputs = event.outputs
try {
val address = OnchainWallet.getNewAddress()
val script = Address(address).scriptPubkey().toBytes().toUByteArray().toByteArray()
val txOut: Array<TxOut> = arrayOf()
val res = keysManager?.spend_spendable_outputs(
outputs,
txOut,
script,
1000,
Option_u32Z.None.none()
)

if (res != null) {
if (res.is_ok) {
val tx = (res as Result_TransactionNoneZ.Result_TransactionNoneZ_OK).res
val txs: Array<ByteArray> = arrayOf()
txs.plus(tx)

LDKBroadcaster.broadcast_transactions(txs)
}
}

} catch (e: Exception) {
Log.i(LDKTAG, "Error: ${e.message}")
}
}

```

</template>
<template v-slot:swift>

```Swift
// Example where we spend straight to our BDK based wallet
func handleEvent(event: Event) {
if let event = event.getValueAsSpendableOutputs() {
let outputs = event.getOutputs()
do {
let address = ldkManager!.bdkManager.getAddress(addressIndex: .new)!
let script = try Address(address: address).scriptPubkey().toBytes()
let res = ldkManager!.myKeysManager.spendSpendableOutputs(
descriptors: outputs,
outputs: [],
changeDestinationScript: script,
feerateSatPer1000Weight: 1000,
locktime: nil)
if res.isOk() {
var txs: [[UInt8]] = []
txs.append(res.getValue()!)
ldkManager!.broadcaster.broadcastTransactions(txs: txs)
}
} catch {
print(error.localizedDescription)
}
}
}
```

</template>

</CodeSwitcher>

**References:** [Rust `SpendableOutputs` docs](https://docs.rs/lightning/0.0.121/lightning/events/enum.Event.html#variant.SpendableOutputs), [Java/Kotlin `SpendableOutputs` bindings](https://github.com/lightningdevkit/ldk-garbagecollected/blob/main/src/main/java/org/ldk/structs/Event.java#L802)
Loading
Loading