@@ -60,11 +60,8 @@ import Foundation
60
60
61
61
class MyLogger : Logger {
62
62
63
- override func log (record : UnsafePointer <Int8 >? ) {
64
- if let record = record {
65
- let string = Bindings.UnsafeIntPointer_to_string (nativeType : record)
66
- print (" record: \( string ) " )
67
- }
63
+ override func log (record : String ? ) {
64
+ print (" record: \( record ) " )
68
65
}
69
66
70
67
}
@@ -89,8 +86,7 @@ import Foundation
89
86
90
87
class MyBroadcasterInterface : BroadcasterInterface {
91
88
92
- override func broadcast_transaction (tx : LDKTransaction) {
93
- let txBytes: [UInt8 ] = Bindings.LDKTransaction_to_array (nativeType : tx)
89
+ override func broadcast_transaction (tx : [UInt8 ]) {
94
90
// insert code to broadcast transaction
95
91
}
96
92
@@ -116,27 +112,23 @@ import Foundation
116
112
117
113
class MyPersister : Persist {
118
114
119
- override func persist_new_channel (id : LDKOutPoint, data : UnsafePointer <LDKChannelMonitor>) -> Result_NoneChannelMonitorUpdateErrZ {
120
- let outPoint = OutPoint (pointer : id)
121
- let idBytes: [UInt8 ] = outPoint.write (obj : outPoint)
122
-
123
- let channelMonitor = ChannelMonitor (pointer : data.pointee )
124
- let monitorBytes: [UInt8 ] = channelMonitor.write (obj : channelMonitor)
115
+ override func persist_new_channel (id : OutPoint, data : ChannelMonitor) -> Result_NoneChannelMonitorUpdateErrZ {
116
+ let idBytes: [UInt8 ] = id.write (obj : id)
117
+ let monitorBytes: [UInt8 ] = data.write (obj : data)
125
118
126
119
// persist monitorBytes to disk, keyed by idBytes
127
120
121
+ // simplified result instantiation calls coming shortly!
128
122
return Result_NoneChannelMonitorUpdateErrZ (pointer : LDKCResult_NoneChannelMonitorUpdateErrZ ())
129
123
}
130
124
131
- override func update_persisted_channel (id : LDKOutPoint, update : UnsafePointer <LDKChannelMonitorUpdate>, data : UnsafePointer <LDKChannelMonitor>) -> Result_NoneChannelMonitorUpdateErrZ {
132
- let outPoint = OutPoint (pointer : id)
133
- let idBytes: [UInt8 ] = outPoint.write (obj : outPoint)
134
-
135
- let channelMonitor = ChannelMonitor (pointer : data.pointee )
136
- let monitorBytes: [UInt8 ] = channelMonitor.write (obj : channelMonitor)
125
+ override func update_persisted_channel (id : OutPoint, update : ChannelMonitorUpdate, data : ChannelMonitor) -> Result_NoneChannelMonitorUpdateErrZ {
126
+ let idBytes: [UInt8 ] = id.write (obj : id)
127
+ let monitorBytes: [UInt8 ] = data.write (obj : data)
137
128
138
129
// modify persisted monitorBytes keyed by idBytes on disk
139
130
131
+ // simplified result instantiation calls coming shortly!
140
132
return Result_NoneChannelMonitorUpdateErrZ (pointer : LDKCResult_NoneChannelMonitorUpdateErrZ ())
141
133
}
142
134
@@ -161,26 +153,23 @@ Define the subclass:
161
153
import Foundation
162
154
163
155
class MyFilter : Filter {
164
- override func register_tx (txid : UnsafePointer <(UInt8 , UInt8 , UInt8 , UInt8 , UInt8 , UInt8 , UInt8 , UInt8 , UInt8 , UInt8 , UInt8 , UInt8 , UInt8 , UInt8 , UInt8 , UInt8 , UInt8 , UInt8 , UInt8 , UInt8 , UInt8 , UInt8 , UInt8 , UInt8 , UInt8 , UInt8 , UInt8 , UInt8 , UInt8 , UInt8 , UInt8 , UInt8 )>? , script_pubkey : LDKu8slice) {
165
- let txidBytes = Bindings.tuple32_to_array (nativeType : txid! .pointee )
166
- let scriptPubkeyBytes = Bindings.LDKu8slice_to_array (nativeType : script_pubkey)
167
-
156
+
157
+ override func register_tx (txid : [UInt8 ]? , script_pubkey : [UInt8 ]) {
168
158
// watch this transaction on-chain
169
159
}
170
160
171
- override func register_output (output : LDKWatchedOutput) -> Option_C2Tuple_usizeTransactionZZ {
172
- let watchedOutput = WatchedOutput (pointer : output)
173
- let scriptPubkeyBytes = watchedOutput.get_script_pubkey ()
174
- let outpoint = watchedOutput.get_outpoint ()
161
+ override func register_output (output : WatchedOutput) -> Option_C2Tuple_usizeTransactionZZ {
162
+ let scriptPubkeyBytes = output.get_script_pubkey ()
163
+ let outpoint = output.get_outpoint ()
175
164
let txidBytes = Bindings.tuple32_to_array (nativeType : outpoint.get_txid ())
176
165
let outputIndex = outpoint.get_index ()
177
166
178
167
// watch for any transactions that spend this output on-chain
179
168
180
- let blockHashBytes = watchedOutput .get_block_hash ()
181
- // TODO: 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
+ let blockHashBytes = output .get_block_hash ()
170
+ // if block hash bytes are not null, return any transaction spending the output that is found in the corresponding block along with its index
182
171
183
- return Option_C2Tuple_usizeTransactionZZ (pointer : LDKCOption_C2Tuple_usizeTransactionZZ () )
172
+ return Option_C2Tuple_usizeTransactionZZ (value : nil )
184
173
}
185
174
}
186
175
```
@@ -202,3 +191,44 @@ let filter = MyFilter()
202
191
203
192
let chainMonitor = ChainMonitor.init (chain_source : filter, broadcaster : broadcaster, logger : logger, feeest : feeEstimator, persister : persister)
204
193
```
194
+
195
+ ### KeysManager
196
+
197
+ ``` swift
198
+ // main context (continued)
199
+
200
+ var keyData = Data (count : 32 )
201
+ keyData.withUnsafeMutableBytes {
202
+ SecRandomCopyBytes (kSecRandomDefault, 32 , $0 .baseAddress ! )
203
+ }
204
+ let seed = [UInt8 ](keyData)
205
+ let timestamp_seconds = UInt64 (NSDate ().timeIntervalSince1970 )
206
+ let timestamp_nanos = UInt32 .init (truncating : NSNumber (value : timestamp_seconds * 1000 * 1000 ))
207
+ let keysManager = KeysManager (seed : seed, starting_time_secs : timestamp_seconds, starting_time_nanos : timestamp_nanos)
208
+ ```
209
+
210
+ ### ChannelManager
211
+
212
+ To instantiate the channel manager, we need a couple minor prerequisites.
213
+
214
+ First, we need the current block height and hash. For the sake of this example, we'll just use
215
+ a random block at a height that does not exist at the time of this writing.
216
+
217
+ ``` swift
218
+ let bestBlock = BestBlock (block_hash : [UInt8 ](Data (base64Encoded : " AAAAAAAAAAAABe5Xh25D12zkQuLAJQbBeLoF1tEQqR8=" )! ), height : 700123 )
219
+ let chainParameters = ChainParameters (network_arg : LDKNetwork_Bitcoin, best_block_arg : bestBlock)
220
+ ```
221
+
222
+ Second, we also need to initialize a default user config, which we simply do like this:
223
+
224
+ ``` swift
225
+ let userConfig = UserConfig.init ()
226
+ ```
227
+
228
+ Finally, we can proceed by instantiating the ChannelManager.
229
+
230
+ ``` swift
231
+ // main context (continued)
232
+
233
+ let channelManager = ChannelManager.init (fee_est : feeEstimator, chain_monitor : chainMonitor.as_Watch (this_arg : chainMonitor), tx_broadcaster : broadcaster, logger : logger, keys_manager : keysManager.as_KeysInterface (this_arg : keysManager), config : userConfig, params : chainParameters)
234
+ ```
0 commit comments