Skip to content

Commit c934e2c

Browse files
author
Conor Okus
committed
Add closing channel Swift example
1 parent 1c8da2a commit c934e2c

File tree

3 files changed

+577
-16664
lines changed

3 files changed

+577
-16664
lines changed

docs/.vuepress/config.js

Lines changed: 50 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const themeConfig = require("@spiralbtc/vuepress-devkit-theme/config");
44
const title = "Lightning Dev Kit Documentation";
55
const baseUrl = "https://lightningdevkit.org";
66
const githubUrl = "https://github.com/lightningdevkit";
7-
const discordUrl = "https://discord.gg/xaYE3pDQpm";
7+
const discordUrl = "https://discord.gg/5AcknnMfBw";
88
const themeColor = "#ffffff";
99

1010
const docsSidebar = [
@@ -98,20 +98,11 @@ const docsSidebar = [
9898
"https://docs.rs/lightning-rapid-gossip-sync/*/lightning_rapid_gossip_sync/",
9999
"lightning-rapid-gossip-sync",
100100
],
101-
[
102-
"https://docs.rs/lightning-transaction-sync/*/lightning_transaction_sync/",
103-
"lightning-transaction-sync",
104-
],
105101
],
106102
},
107103
[
108-
"https://github.com/lightningdevkit/ldk-garbagecollected/tree/main/src/main/java/org/ldk",
109-
"Java/Kotlin",
110-
],
111-
["https://github.com/lightningdevkit/ldk-swift", "Swift"],
112-
[
113-
"https://github.com/lightningdevkit/ldk-garbagecollected/tree/main/ts",
114-
"TypeScript",
104+
"https://github.com/arik-so/SwiftLightning/tree/master/Documentation",
105+
"Swift",
115106
],
116107
],
117108
},
@@ -152,6 +143,10 @@ const tutorialSidebar = [
152143
"/tutorials/building-a-node-with-ldk/sending-payments",
153144
"Sending Payments",
154145
],
146+
[
147+
"/tutorials/building-a-node-with-ldk/closing-a-channel",
148+
"Closing a Channel",
149+
],
155150
],
156151
},
157152
],
@@ -215,10 +210,6 @@ module.exports = {
215210
text: "Tutorials",
216211
link: "/tutorials/getting-started",
217212
},
218-
{
219-
text: "Case Studies",
220-
link: "/case-studies",
221-
},
222213
{
223214
text: "Blog",
224215
link: "/blog/",
@@ -242,6 +233,39 @@ module.exports = {
242233
},
243234
footer: {
244235
links: [
236+
{
237+
title: "Docs",
238+
children: [
239+
{
240+
text: "Introduction",
241+
link: "/introduction/",
242+
},
243+
{
244+
text: "Sample LDK node",
245+
link: "/running-a-sample-ldk-node/",
246+
},
247+
{
248+
text: "Architecture",
249+
link: "/overview/architecture/",
250+
},
251+
{
252+
text: "Payments",
253+
link: "/payments/",
254+
},
255+
{
256+
text: "Blockchain Data",
257+
link: "/blockchain_data/introduction/",
258+
},
259+
{
260+
text: "Key Management",
261+
link: "/key_management/",
262+
},
263+
{
264+
text: "Examples",
265+
link: "/examples/",
266+
},
267+
],
268+
},
245269
{
246270
title: "Community",
247271
children: [
@@ -337,13 +361,21 @@ module.exports = {
337361
},
338362
{
339363
text: "Reporting a Vulnerability",
340-
link: "https://github.com/lightningdevkit/rust-lightning/blob/main/SECURITY.md",
341364
rel: "noopener noreferrer",
342365
},
343366
],
344367
},
368+
{
369+
title: "More",
370+
children: [
371+
{
372+
text: "Blog",
373+
link: "/blog/",
374+
},
375+
],
376+
},
345377
],
346-
copyright: "Copyright © 2023 LDK Developers",
378+
copyright: "Copyright © 2022 LDK Developers",
347379
},
348380
},
349381
};
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
# Closing a Channel
2+
3+
Closing Channel using Custom KeysManager. (Single Fees)
4+
5+
<CodeSwitcher :languages="{rust:'Rust', java:'Java', swift:'Swift'}">
6+
<template v-slot:rust>
7+
8+
```rust
9+
// TODO: Add Rust Code Here
10+
```
11+
12+
</template>
13+
<template v-slot:java>
14+
15+
```java
16+
// TODO: Add Java Code Here
17+
```
18+
19+
</template>
20+
<template v-slot:swift>
21+
22+
```Swift
23+
class MyKeysManager {
24+
let keysManager: KeysManager
25+
let signerProvider: MySignerProvider // Use signerProvider instead of asSignerProvider()
26+
let wallet: Wallet
27+
28+
init(seed: [UInt8], startingTimeSecs: UInt64, startingTimeNanos: UInt32, wallet: Wallet) {
29+
self.keysManager = KeysManager(seed: seed, startingTimeSecs: startingTimeSecs, startingTimeNanos: startingTimeNanos)
30+
self.wallet = wallet
31+
signerProvider = MySignerProvider()
32+
signerProvider.myKeysManager = self
33+
}
34+
}
35+
36+
class MySignerProvider: SignerProvider {
37+
weak var myKeysManager: MyKeysManager?
38+
override func deriveChannelSigner(channelValueSatoshis: UInt64, channelKeysId: [UInt8]) -> Bindings.WriteableEcdsaChannelSigner {
39+
return myKeysManager!.keysManager.asSignerProvider().deriveChannelSigner(channelValueSatoshis: channelValueSatoshis, channelKeysId: channelKeysId)
40+
}
41+
42+
override func generateChannelKeysId(inbound: Bool, channelValueSatoshis: UInt64, userChannelId: [UInt8]) -> [UInt8] {
43+
return myKeysManager!.keysManager.asSignerProvider().generateChannelKeysId(inbound: inbound, channelValueSatoshis: channelValueSatoshis, userChannelId: userChannelId)
44+
}
45+
46+
override func readChanSigner(reader: [UInt8]) -> Bindings.Result_WriteableEcdsaChannelSignerDecodeErrorZ {
47+
return myKeysManager!.keysManager.asSignerProvider().readChanSigner(reader: reader)
48+
}
49+
50+
override func getDestinationScript() -> [UInt8] {
51+
do {
52+
let address = try myKeysManager!.wallet.getAddress(addressIndex: .new)
53+
return address.address.scriptPubkey().toBytes()
54+
} catch {
55+
return myKeysManager!.keysManager.asSignerProvider().getDestinationScript()
56+
}
57+
}
58+
59+
override func getShutdownScriptpubkey() -> Bindings.ShutdownScript {
60+
do {
61+
let address = try myKeysManager!.wallet.getAddress(addressIndex: .new).address
62+
let payload = address.payload()
63+
if case let .witnessProgram(`version`, `program`) = payload {
64+
let ver: UInt8
65+
switch version {
66+
case .v0:
67+
ver = 0
68+
case .v1:
69+
ver = 1
70+
case .v2:
71+
ver = 2
72+
case .v3:
73+
ver = 3
74+
case .v4:
75+
ver = 4
76+
case .v5:
77+
ver = 5
78+
case .v6:
79+
ver = 6
80+
case .v7:
81+
ver = 7
82+
case .v8:
83+
ver = 8
84+
case .v9:
85+
ver = 9
86+
case .v10:
87+
ver = 10
88+
case .v11:
89+
ver = 11
90+
case .v12:
91+
ver = 12
92+
case .v13:
93+
ver = 13
94+
case .v14:
95+
ver = 14
96+
case .v15:
97+
ver = 15
98+
case .v16:
99+
ver = 16
100+
}
101+
let res = ShutdownScript.newWitnessProgram(version: ver, program: program)
102+
if res.isOk() {
103+
return res.getValue()!
104+
}
105+
}
106+
return myKeysManager!.keysManager.asSignerProvider().getShutdownScriptpubkey()
107+
} catch {
108+
return myKeysManager!.keysManager.asSignerProvider().getShutdownScriptpubkey()
109+
}
110+
}
111+
}
112+
```
113+
114+
</template>
115+
</CodeSwitcher>
116+
117+
Closing Channel using Events. (Double Fees)
118+
119+
<CodeSwitcher :languages="{rust:'Rust', java:'Java', swift:'Swift'}">
120+
<template v-slot:rust>
121+
122+
```rust
123+
// TODO: Add Rust Code Here
124+
```
125+
126+
</template>
127+
<template v-slot:java>
128+
129+
```java
130+
// TODO: Add Java Code Here
131+
```
132+
133+
</template>
134+
<template v-slot:swift>
135+
136+
```Swift
137+
func handleEvent(event: Event) {
138+
if let event = event.getValueAsSpendableOutputs() {
139+
let outputs = event.getOutputs()
140+
do {
141+
let address = ldkManager!.bdkManager.getAddress(addressIndex: .new)!
142+
let script = try Address(address: address).scriptPubkey().toBytes()
143+
let res = ldkManager?.keysManager?.spendSpendableOutputs(descriptors: outputs, outputs: [], changeDestinationScript: script, feerateSatPer1000Weight: 1000)
144+
if res!.isOk() {
145+
ldkManager.broadcaster.broadcastTransaction(tx: res!.getValue()!)
146+
}
147+
} catch {
148+
print(error)
149+
}
150+
}
151+
}
152+
```
153+
154+
</template>
155+
156+
</CodeSwitcher>

0 commit comments

Comments
 (0)