1
1
# Setup
2
2
3
3
This project is essentially a set of auto-generated decorators that call the C methods
4
- defined in lightning.h. The wrappers for the most part take care of conveniences such
4
+ defined in ` lightning.h ` . The wrappers for the most part take care of conveniences such
5
5
as conversion of Swift types into C types, and parsing C types back into Swift.
6
6
7
- In Bindings.swift, there are various additional generic utility methods to aid the
7
+ In ` Bindings.swift ` , there are various additional generic utility methods to aid the
8
8
developer in passing data back and forth.
9
9
10
10
The greatest effort on the part of users of this project comes in when dealing with
11
- traits. All files located within ` bindings/LDK /traits` are meant to be interpreted as
11
+ traits. All files located within ` out /traits` are meant to be interpreted as
12
12
abstract classes. However, as Swift does not allow abstract classes, and using protocols
13
13
would shift both implementation and boilerplate burden on developers, I instead recommend
14
14
an override pattern, which I will describe in the next step.
@@ -32,11 +32,11 @@ First, define an inheriting class called `MyFeeEstimator`:
32
32
import Foundation
33
33
34
34
class MyFeeEstimator : FeeEstimator {
35
-
36
- override func get_est_sat_per_1000_weight ( confirmation_target : LDKConfirmationTarget ) -> UInt32 {
35
+
36
+ override func getEstSatPer1000Weight ( confirmationTarget : ConfirmationTarget ) -> UInt32
37
37
return 253
38
38
}
39
-
39
+
40
40
}
41
41
```
42
42
@@ -59,11 +59,11 @@ Define the inheriting class:
59
59
import Foundation
60
60
61
61
class MyLogger : Logger {
62
-
62
+
63
63
override func log (record : Record) {
64
- print (" log event: \( record.get_args () ) " )
64
+ print (" log event: \( record.getArgs () ) " )
65
65
}
66
-
66
+
67
67
}
68
68
```
69
69
@@ -85,11 +85,12 @@ Define the subclass:
85
85
import Foundation
86
86
87
87
class MyBroadcasterInterface : BroadcasterInterface {
88
-
89
- override func broadcast_transaction (tx : [UInt8 ]) {
88
+
89
+ override func broadcastTransaction (tx : [UInt8 ]) {
90
+
90
91
// insert code to broadcast transaction
91
92
}
92
-
93
+
93
94
}
94
95
```
95
96
@@ -111,25 +112,25 @@ Define the subclass:
111
112
import Foundation
112
113
113
114
class MyPersister : Persist {
114
-
115
- override func persist_new_channel ( channel_id : OutPoint, data : ChannelMonitor, update_id : MonitorUpdateId) -> LDKChannelMonitorUpdateStatus {
116
- let idBytes: [UInt8 ] = channel_id .write ()
115
+
116
+ override func persistNewChannel ( channelId : OutPoint, data : ChannelMonitor, updateId : MonitorUpdateId) -> ChannelMonitorUpdateStatus {
117
+ let idBytes: [UInt8 ] = channelId .write ()
117
118
let monitorBytes: [UInt8 ] = data.write ()
118
-
119
+
119
120
// persist monitorBytes to disk, keyed by idBytes
120
-
121
- return LDKChannelMonitorUpdateStatus_Completed
121
+
122
+ return ChannelMonitorUpdateStatus. Completed
122
123
}
123
-
124
- override func update_persisted_channel ( channel_id : OutPoint, update : ChannelMonitorUpdate, data : ChannelMonitor, update_id : MonitorUpdateId) -> LDKChannelMonitorUpdateStatus {
125
- let idBytes: [UInt8 ] = channel_id .write ()
124
+
125
+ override func updatePersistedChannel ( channelId : OutPoint, update : ChannelMonitorUpdate, data : ChannelMonitor, updateId : MonitorUpdateId) -> ChannelMonitorUpdateStatus {
126
+ let idBytes: [UInt8 ] = channelId .write ()
126
127
let monitorBytes: [UInt8 ] = data.write ()
127
-
128
+
128
129
// modify persisted monitorBytes keyed by idBytes on disk
129
-
130
- return LDKChannelMonitorUpdateStatus_Completed
130
+
131
+ return ChannelMonitorUpdateStatus. Completed
131
132
}
132
-
133
+
133
134
}
134
135
```
135
136
@@ -151,23 +152,18 @@ Define the subclass:
151
152
import Foundation
152
153
153
154
class MyFilter : Filter {
154
-
155
- override func register_tx (txid : [UInt8 ]? , script_pubkey : [UInt8 ]) {
155
+
156
+ override func registerTx (txid : [UInt8 ]? , scriptPubkey : [UInt8 ]) {
156
157
// watch this transaction on-chain
157
158
}
158
-
159
- override func register_output (output : WatchedOutput) -> Option_C2Tuple_usizeTransactionZZ {
160
- let scriptPubkeyBytes = output.get_script_pubkey ()
161
- let outpoint = output.get_outpoint () !
162
- let txid = outpoint.get_txid ()
163
- let outputIndex = outpoint.get_index ()
164
-
159
+
160
+ override func registerOutput (output : WatchedOutput) {
161
+ let scriptPubkeyBytes = output.getScriptPubkey ()
162
+ let outpoint = output.getOutpoint ()
163
+ let txid = outpoint.getTxid ()
164
+ let outputIndex = outpoint.getIndex ()
165
+
165
166
// watch for any transactions that spend this output on-chain
166
-
167
- let blockHashBytes = output.get_block_hash ()
168
- // if block hash bytes are not null, return any transaction spending the output that is found in the corresponding block along with its index
169
-
170
- return Option_C2Tuple_usizeTransactionZZ.none ()
171
167
}
172
168
}
173
169
```
@@ -187,8 +183,7 @@ let filter = MyFilter()
187
183
``` swift
188
184
// main context (continued)
189
185
190
- let filterOption = Option_FilterZ (value : filter)
191
- let chainMonitor = ChainMonitor (chain_source : filterOption.dangle (), broadcaster : broadcaster, logger : logger, feeest : feeEstimator, persister : persister)
186
+ let chainMonitor = ChainMonitor (chainSource : filter, broadcaster : broadcaster, logger : logger, feeest : feeEstimator, persister : persister)
192
187
```
193
188
194
189
### KeysManager
@@ -198,27 +193,17 @@ let chainMonitor = ChainMonitor(chain_source: filterOption.dangle(), broadcaster
198
193
199
194
var keyData = Data (count : 32 )
200
195
keyData.withUnsafeMutableBytes {
201
- // returns 0 on success
202
- let didCopySucceed = SecRandomCopyBytes (kSecRandomDefault, 32 , $0 .baseAddress ! )
203
- assert (didCopySucceed == 0 )
196
+ // returns 0 on success
197
+ let didCopySucceed = SecRandomCopyBytes (kSecRandomDefault, 32 , $0 .baseAddress ! )
198
+ assert (didCopySucceed == 0 )
204
199
}
205
200
let seed = [UInt8 ](keyData)
206
- let timestamp_seconds = UInt64 (NSDate ().timeIntervalSince1970 )
207
- let timestamp_nanos = UInt32 .init (truncating : NSNumber (value : timestamp_seconds * 1000 * 1000 ))
208
- let keysManager = KeysManager (seed : seed, starting_time_secs : timestamp_seconds, starting_time_nanos : timestamp_nanos )
209
- let keysInterface = keysManager.as_KeysInterface ()
201
+ let timestampSeconds = UInt64 (NSDate ().timeIntervalSince1970 )
202
+ let timestampNanos = UInt32 .init (truncating : NSNumber (value : timestampSeconds * 1000 * 1000 ))
203
+ let keysManager = KeysManager (seed : seed, startingTimeSecs : timestampSeconds, startingTimeNanos : timestampNanos )
204
+ let keysInterface = keysManager.asKeysInterface ()
210
205
```
211
206
212
- We will keep needing to pass around a keysInterface instance, and we will also need to
213
- pass its node secret to the peer manager initialization, so let's prepare it right here:
214
-
215
- ``` swift
216
- let keysInterface = keysManager.as_KeysInterface ()
217
- let nodeSecret = self .keysInterface .get_node_secret ()
218
- ```
219
-
220
- This is a bit inelegant, but we will be providing simpler casting methods for user-provided types shortly.
221
-
222
207
### ChannelManager
223
208
224
209
To instantiate the channel manager, we need a couple minor prerequisites.
@@ -234,7 +219,7 @@ let latestBlockHeight = 700123
234
219
Second, we also need to initialize a default user config, which we simply do like this:
235
220
236
221
``` swift
237
- let userConfig = UserConfig ()
222
+ let userConfig = UserConfig. initWithDefault ()
238
223
```
239
224
240
225
Finally, we can proceed by instantiating the ` ChannelManager ` using ` ChannelManagerConstructor ` .
@@ -243,17 +228,18 @@ Finally, we can proceed by instantiating the `ChannelManager` using `ChannelMana
243
228
// main context (continued)
244
229
245
230
let channelManagerConstructor = ChannelManagerConstructor (
246
- network : LDKNetwork_Bitcoin ,
247
- config : userConfig,
248
- current_blockchain_tip_hash : latestBlockHash,
249
- current_blockchain_tip_height : latestBlockHeight,
250
- keys_interface : keysInterface,
251
- fee_estimator : feeEstimator,
252
- chain_monitor : chainMonitor,
253
- net_graph : nil , // see `NetworkGraph`
254
- tx_broadcaster : broadcaster,
255
- logger : logger
231
+ network : Network. Bitcoin ,
232
+ config : userConfig,
233
+ currentBlockchainTipHash : latestBlockHash,
234
+ currentBlockchainTipHeight : latestBlockHeight,
235
+ keysInterface : keysInterface,
236
+ feeEstimator : feeEstimator,
237
+ chainMonitor : chainMonitor,
238
+ netGraph : nil , // see `NetworkGraph`
239
+ txBroadcaster : broadcaster,
240
+ logger : logger
256
241
)
242
+
257
243
let channelManager = channelManagerConstructor.channelManager
258
244
```
259
245
@@ -265,7 +251,7 @@ a `NetworkGraph` that can later be passed to the `ChannelManagerConstructor`:
265
251
``` swift
266
252
// main context (continued)
267
253
268
- let networkGraph = NetworkGraph (genesis_hash : [UInt8 ](Data (base64Encoded : " AAAAAAAZ1micCFrhZYMek0/3Y65GoqbBcrPxtgqM4m8=" )! ))
254
+ let networkGraph = NetworkGraph (genesisHash : [UInt8 ](Data (base64Encoded : " AAAAAAAZ1micCFrhZYMek0/3Y65GoqbBcrPxtgqM4m8=" )! ), logger : logger )
269
255
```
270
256
271
257
Note that a network graph instance needs to be provided upon initialization, which in turn requires the genesis block hash.
@@ -275,24 +261,24 @@ Note that a network graph instance needs to be provided upon initialization, whi
275
261
If you need to serialize a channel manager, you can simply call its write method on itself:
276
262
277
263
``` swift
278
- let serializedChannelManager: [UInt8 ] = channelManager.write (obj : channelManager )
264
+ let serializedChannelManager: [UInt8 ] = channelManager.write ()
279
265
```
280
266
281
267
If you have a channel manager you previously serialized, you can restore it like this:
282
268
283
269
``` swift
284
- let serializedChannelManager: [UInt8 ] = [2 , 1 , 111 , 226 , 140 , 10 , 182 , 241 , 179 , 114 , 193 , 166 , 162 , 70 , 174 , 99 , 247 , 79 , 147 , 30 , 131 , 101 , 225 , 90 , 8 , 156 , 104 , 214 , 25 , 0 , 0 , 0 , 0 , 0 , 0 , 10 , 174 , 219 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 5 , 238 , 87 , 135 , 110 , 67 , 215 , 108 , 228 , 66 , 226 , 192 , 37 , 6 , 193 , 120 , 186 , 5 , 214 , 209 , 16 , 169 , 31 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ] // <insert bytes you would have written in following the later step "Persist channel manager">
270
+ let serializedChannelManager: [UInt8] = [1, 1, 111, 226, 140, 10, 182, 241, 179, 114, 193, 166, 162, 70, 174, 99, 247, 79, 147, 30, 131, 101, 225, 90, 8, 156, 104, 214, 25, 0, 0, 0, 0, 0, 0, 10, 174, 219, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 238, 87, 135, 110, 67, 215, 108, 228, 66, 226, 192, 37, 6, 193, 120, 186, 5, 214, 209, 16, 169, 31, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 113, 1, 2, 0, 0, 3, 2, 0, 0, 5, 33, 2, 89, 251, 100, 20, 141, 129, 167, 164, 253, 12, 110, 225, 21, 14, 42, 17, 23, 170, 54, 168, 175, 191, 155, 92, 7, 230, 198, 17, 219, 93, 1, 98, 7, 32, 227, 238, 107, 153, 58, 23, 23, 190, 44, 19, 147, 84, 4, 108, 20, 65, 184, 73, 193, 61, 62, 208, 250, 205, 198, 250, 214, 79, 148, 156, 191, 174, 9, 0, 11, 32, 134, 110, 74, 49, 160, 200, 160, 145, 147, 82, 141, 56, 13, 26, 225, 152, 160, 215, 152, 117, 30, 242, 250, 8, 119, 235, 144, 54, 177, 235, 97, 60]
285
271
let serializedChannelMonitors: [[UInt8 ]] = []
286
272
let channelManagerConstructor = try ChannelManagerConstructor (
287
- channel_manager_serialized : serializedChannelManager,
288
- channel_monitors_serialized : serializedChannelMonitors,
289
- keys_interface : keysInterface,
290
- fee_estimator : feeEstimator,
291
- chain_monitor : chainMonitor,
292
- filter : filter,
293
- net_graph : nil , // or networkGraph
294
- tx_broadcaster : broadcaster,
295
- logger : logger
273
+ channelManagerSerialized : serializedChannelManager,
274
+ channelMonitorsSerialized : serializedChannelMonitors,
275
+ keysInterface : keysInterface,
276
+ feeEstimator : feeEstimator,
277
+ chainMonitor : chainMonitor,
278
+ filter : filter,
279
+ netGraphSerialized : nil , // or networkGraph
280
+ txBroadcaster : broadcaster,
281
+ logger : logger
296
282
)
297
283
298
284
let channelManager = channelManagerConstructor.channelManager
0 commit comments