Skip to content

Commit e08dcfd

Browse files
committed
Update docs to reflect full initialization example up to a peer manager instance.
1 parent b1a6c33 commit e08dcfd

File tree

1 file changed

+73
-1
lines changed

1 file changed

+73
-1
lines changed

docs/setup.md

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,24 @@ Instantiate it:
182182
let filter = MyFilter()
183183
```
184184

185+
### KeysInterface
186+
187+
Define the subclass:
188+
189+
```swift
190+
// MyKeysInterface.swift
191+
192+
import Foundation
193+
194+
class MyKeysInterface: KeysInterface {
195+
override func get_node_secret() -> [UInt8] {
196+
return [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31]
197+
}
198+
}
199+
```
200+
201+
We won't be instantiating it directly, but we will need the keys interface later on.
202+
185203
## Phase 2: Initializations
186204

187205
### ChainMonitor
@@ -207,6 +225,14 @@ let timestamp_nanos = UInt32.init(truncating: NSNumber(value: timestamp_seconds
207225
let keysManager = KeysManager(seed: seed, starting_time_secs: timestamp_seconds, starting_time_nanos: timestamp_nanos)
208226
```
209227

228+
We will keep needing to pass around a keysInterface instance, so let's prepare it right here:
229+
230+
```swift
231+
let keysInterface = MyKeysInterface(pointer: keysManager.as_KeysInterface().cOpaqueStruct!)
232+
```
233+
234+
This is a bit inelegant, but we will be providing simpler casting methods for user-provided types shortly.
235+
210236
### ChannelManager
211237

212238
To instantiate the channel manager, we need a couple minor prerequisites.
@@ -230,5 +256,51 @@ Finally, we can proceed by instantiating the ChannelManager.
230256
```swift
231257
// main context (continued)
232258

233-
let channelManager = ChannelManager.init(fee_est: feeEstimator, chain_monitor: chainMonitor.as_Watch(), tx_broadcaster: broadcaster, logger: logger, keys_manager: keysManager.as_KeysInterface(), config: userConfig, params: chainParameters)
259+
let channelManager = ChannelManager.init(fee_est: feeEstimator, chain_monitor: chainMonitor.as_Watch(), tx_broadcaster: broadcaster, logger: logger, keys_manager: keysInterface, config: userConfig, params: chainParameters)
260+
```
261+
262+
### NetGraphMsgHandler
263+
264+
If you intend to use the LDK's built-in routing algorithm, you will need to instantiate
265+
a graph message handler:
266+
267+
```swift
268+
// main context (continued)
269+
270+
let networkGraph = NetworkGraph(genesis_hash: [UInt8](Data(base64Encoded: "AAAAAAAZ1micCFrhZYMek0/3Y65GoqbBcrPxtgqM4m8=")!))
271+
let router = NetGraphMsgHandler(chain_access: nil, logger: logger, network_graph: networkGraph)
272+
```
273+
274+
Note that a network graph instance needs to be provided upon initialization, which in turn requires the genesis block hash.
275+
276+
### PeerHandler
277+
278+
Finally, let's instantiate the peer handler. It requires a seed for ephemeral key generation,
279+
as well as a hybrid message handler for routing and channel events.
280+
281+
Let's first set up the seed:
282+
283+
```swift
284+
var keyData2 = Data(count: 32)
285+
keyData2.withUnsafeMutableBytes {
286+
SecRandomCopyBytes(kSecRandomDefault, 32, $0.baseAddress!)
287+
}
288+
let peerManagerSeed = [UInt8](keyData2)
289+
```
290+
291+
Next, let's prepare the combined message handler:
292+
293+
```swift
294+
let messageHandler = MessageHandler(chan_handler_arg: channelManager.as_ChannelMessageHandler(), route_handler_arg: router.as_RoutingMessageHandler())
295+
```
296+
297+
And finally, let's instantiate the peer manager itself:
298+
299+
```swift
300+
// main context (continued)
301+
302+
let peerManager = PeerManager(message_handler: messageHandler, our_node_secret: keysInterface.get_node_secret(), ephemeral_random_data: peerManagerSeed, logger: logger)
234303
```
304+
305+
Now, all that remains is setting up the actual syscalls that are necessary within
306+
the host environment, and route them to the appropriate LDK objects.

0 commit comments

Comments
 (0)