Skip to content

Commit 1cdb98d

Browse files
committed
update docs for simplified return types
1 parent f1f8106 commit 1cdb98d

File tree

1 file changed

+60
-30
lines changed

1 file changed

+60
-30
lines changed

docs/setup.md

+60-30
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,8 @@ import Foundation
6060

6161
class MyLogger: Logger {
6262

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)")
6865
}
6966

7067
}
@@ -89,8 +86,7 @@ import Foundation
8986

9087
class MyBroadcasterInterface: BroadcasterInterface {
9188

92-
override func broadcast_transaction(tx: LDKTransaction) {
93-
let txBytes: [UInt8] = Bindings.LDKTransaction_to_array(nativeType: tx)
89+
override func broadcast_transaction(tx: [UInt8]) {
9490
// insert code to broadcast transaction
9591
}
9692

@@ -116,27 +112,23 @@ import Foundation
116112

117113
class MyPersister: Persist {
118114

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)
125118

126119
// persist monitorBytes to disk, keyed by idBytes
127120

121+
// simplified result instantiation calls coming shortly!
128122
return Result_NoneChannelMonitorUpdateErrZ(pointer: LDKCResult_NoneChannelMonitorUpdateErrZ())
129123
}
130124

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)
137128

138129
// modify persisted monitorBytes keyed by idBytes on disk
139130

131+
// simplified result instantiation calls coming shortly!
140132
return Result_NoneChannelMonitorUpdateErrZ(pointer: LDKCResult_NoneChannelMonitorUpdateErrZ())
141133
}
142134

@@ -161,26 +153,23 @@ Define the subclass:
161153
import Foundation
162154

163155
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]) {
168158
// watch this transaction on-chain
169159
}
170160

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()
175164
let txidBytes = Bindings.tuple32_to_array(nativeType: outpoint.get_txid())
176165
let outputIndex = outpoint.get_index()
177166

178167
// watch for any transactions that spend this output on-chain
179168

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
182171

183-
return Option_C2Tuple_usizeTransactionZZ(pointer: LDKCOption_C2Tuple_usizeTransactionZZ())
172+
return Option_C2Tuple_usizeTransactionZZ(value: nil)
184173
}
185174
}
186175
```
@@ -202,3 +191,44 @@ let filter = MyFilter()
202191

203192
let chainMonitor = ChainMonitor.init(chain_source: filter, broadcaster: broadcaster, logger: logger, feeest: feeEstimator, persister: persister)
204193
```
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

Comments
 (0)