11# Setup
22
33This 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
55as conversion of Swift types into C types, and parsing C types back into Swift.
66
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
88developer in passing data back and forth.
99
1010The 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
1212abstract classes. However, as Swift does not allow abstract classes, and using protocols
1313would shift both implementation and boilerplate burden on developers, I instead recommend
1414an override pattern, which I will describe in the next step.
@@ -32,11 +32,11 @@ First, define an inheriting class called `MyFeeEstimator`:
3232import Foundation
3333
3434class MyFeeEstimator : FeeEstimator {
35-
36- override func get_est_sat_per_1000_weight ( confirmation_target : LDKConfirmationTarget ) -> UInt32 {
35+
36+ override func getEstSatPer1000Weight ( confirmationTarget : ConfirmationTarget ) -> UInt32
3737 return 253
3838 }
39-
39+
4040}
4141```
4242
@@ -59,11 +59,11 @@ Define the inheriting class:
5959import Foundation
6060
6161class MyLogger : Logger {
62-
62+
6363 override func log (record : Record) {
64- print (" log event: \( record.get_args () ) " )
64+ print (" log event: \( record.getArgs () ) " )
6565 }
66-
66+
6767}
6868```
6969
@@ -85,11 +85,12 @@ Define the subclass:
8585import Foundation
8686
8787class MyBroadcasterInterface : BroadcasterInterface {
88-
89- override func broadcast_transaction (tx : [UInt8 ]) {
88+
89+ override func broadcastTransaction (tx : [UInt8 ]) {
90+
9091 // insert code to broadcast transaction
9192 }
92-
93+
9394}
9495```
9596
@@ -111,25 +112,25 @@ Define the subclass:
111112import Foundation
112113
113114class MyPersister : Persist {
114-
115- override func persist_new_channel ( channel_id : OutPoint, data : ChannelMonitor, update_id : MonitorUpdateId) -> Result_NoneChannelMonitorUpdateErrZ {
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 ()
117118 let monitorBytes: [UInt8 ] = data.write ()
118-
119+
119120 // persist monitorBytes to disk, keyed by idBytes
120-
121- return Result_NoneChannelMonitorUpdateErrZ. ok ()
121+
122+ return ChannelMonitorUpdateStatus. Completed
122123 }
123-
124- override func update_persisted_channel ( channel_id : OutPoint, update : ChannelMonitorUpdate, data : ChannelMonitor, update_id : MonitorUpdateId) -> Result_NoneChannelMonitorUpdateErrZ {
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 ()
126127 let monitorBytes: [UInt8 ] = data.write ()
127-
128+
128129 // modify persisted monitorBytes keyed by idBytes on disk
129-
130- return Result_NoneChannelMonitorUpdateErrZ. ok ()
130+
131+ return ChannelMonitorUpdateStatus. Completed
131132 }
132-
133+
133134}
134135```
135136
@@ -151,23 +152,18 @@ Define the subclass:
151152import Foundation
152153
153154class MyFilter : Filter {
154-
155- override func register_tx (txid : [UInt8 ]? , script_pubkey : [UInt8 ]) {
155+
156+ override func registerTx (txid : [UInt8 ]? , scriptPubkey : [UInt8 ]) {
156157 // watch this transaction on-chain
157158 }
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+
165166 // 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 ()
171167 }
172168}
173169```
@@ -187,8 +183,7 @@ let filter = MyFilter()
187183``` swift
188184// main context (continued)
189185
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)
192187```
193188
194189### KeysManager
@@ -198,27 +193,17 @@ let chainMonitor = ChainMonitor(chain_source: filterOption.dangle(), broadcaster
198193
199194var keyData = Data (count : 32 )
200195keyData.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 )
204199}
205200let 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 ()
210205```
211206
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-
222207### ChannelManager
223208
224209To instantiate the channel manager, we need a couple minor prerequisites.
@@ -234,7 +219,7 @@ let latestBlockHeight = 700123
234219Second, we also need to initialize a default user config, which we simply do like this:
235220
236221``` swift
237- let userConfig = UserConfig ()
222+ let userConfig = UserConfig. initWithDefault ()
238223```
239224
240225Finally, we can proceed by instantiating the ` ChannelManager ` using ` ChannelManagerConstructor ` .
@@ -243,17 +228,18 @@ Finally, we can proceed by instantiating the `ChannelManager` using `ChannelMana
243228// main context (continued)
244229
245230let 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
256241)
242+
257243let channelManager = channelManagerConstructor.channelManager
258244```
259245
@@ -265,7 +251,7 @@ a `NetworkGraph` that can later be passed to the `ChannelManagerConstructor`:
265251``` swift
266252// main context (continued)
267253
268- let networkGraph = NetworkGraph (genesis_hash : [UInt8 ](Data (base64Encoded : " AAAAAAAZ1micCFrhZYMek0/3Y65GoqbBcrPxtgqM4m8=" )! ))
254+ let networkGraph = NetworkGraph (genesisHash : [UInt8 ](Data (base64Encoded : " AAAAAAAZ1micCFrhZYMek0/3Y65GoqbBcrPxtgqM4m8=" )! ), logger : logger )
269255```
270256
271257Note 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
275261If you need to serialize a channel manager, you can simply call its write method on itself:
276262
277263``` swift
278- let serializedChannelManager: [UInt8 ] = channelManager.write (obj : channelManager )
264+ let serializedChannelManager: [UInt8 ] = channelManager.write ()
279265```
280266
281267If you have a channel manager you previously serialized, you can restore it like this:
282268
283269``` 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]
285271let serializedChannelMonitors: [[UInt8 ]] = []
286272let 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
296282)
297283
298284let channelManager = channelManagerConstructor.channelManager
0 commit comments