|
| 1 | +// |
| 2 | +// ChannelManagerConstructor.swift |
| 3 | +// LDKSwiftARC |
| 4 | +// |
| 5 | +// Created by Arik Sosman on 5/19/21. |
| 6 | +// |
| 7 | + |
| 8 | +import Foundation |
| 9 | + |
| 10 | +enum InvalidSerializedDataError: Error { |
| 11 | + case runtimeError |
| 12 | +} |
| 13 | + |
| 14 | +class ChannelManagerConstructor { |
| 15 | + |
| 16 | + public let channelManager: ChannelManager |
| 17 | + /** |
| 18 | + * The latest block has the channel manager saw. If this is non-null it is a 32-byte block hash. |
| 19 | + * You should sync the blockchain starting with the block that builds on this block. |
| 20 | + */ |
| 21 | + public let channel_manager_latest_block_hash: [UInt8] |
| 22 | + /** |
| 23 | + * A list of ChannelMonitors and the last block they each saw. You should sync the blockchain on each individually |
| 24 | + * starting with the block that builds on the hash given. |
| 25 | + * After doing so (and syncing the blockchain on the channel manager as well), you should call chain_sync_completed() |
| 26 | + * and then continue to normal application operation. |
| 27 | + */ |
| 28 | + public private(set) var channel_monitors: [(ChannelMonitor, [UInt8])] |
| 29 | + |
| 30 | + private let chain_monitor: ChainMonitor |
| 31 | + |
| 32 | + init(channel_manager_serialized: [UInt8], channel_monitors_serialized: [[UInt8]], keys_interface: KeysInterface, fee_estimator: FeeEstimator, chain_monitor: ChainMonitor, filter: Filter?, tx_broadcaster: BroadcasterInterface, logger: Logger) throws { |
| 33 | + |
| 34 | + var monitors: [LDKChannelMonitor] = [] |
| 35 | + self.channel_monitors = [] |
| 36 | + for index in 0..<channel_monitors_serialized.count { |
| 37 | + let currentSerializedChannelMonitor = channel_monitors_serialized[index] |
| 38 | + let res: Result_C2Tuple_BlockHashChannelMonitorZDecodeErrorZ = UtilMethods.constructor_BlockHashChannelMonitorZ_read(ser: currentSerializedChannelMonitor, arg: keys_interface) |
| 39 | + if res.cOpaqueStruct?.result_ok != true { |
| 40 | + throw InvalidSerializedDataError.runtimeError |
| 41 | + } |
| 42 | + let value: LDKCResult_C2Tuple_BlockHashChannelMonitorZDecodeErrorZPtr = res.cOpaqueStruct!.contents |
| 43 | + let a: LDKThirtyTwoBytes = value.result!.pointee.a |
| 44 | + let b: LDKChannelMonitor = value.result!.pointee.b |
| 45 | + let currentChannelMonitor = ChannelMonitor(pointer: b) |
| 46 | + monitors.append(b) |
| 47 | + self.channel_monitors.append((currentChannelMonitor, Bindings.LDKThirtyTwoBytes_to_array(nativeType: a))) |
| 48 | + } |
| 49 | + |
| 50 | + let res = UtilMethods.constructor_BlockHashChannelManagerZ_read(ser: channel_manager_serialized, arg_keys_manager: keys_interface, arg_fee_estimator: fee_estimator, arg_chain_monitor: chain_monitor.as_Watch(), arg_tx_broadcaster: tx_broadcaster, arg_logger: logger, arg_default_config: UserConfig(), arg_channel_monitors: monitors) |
| 51 | + if res.cOpaqueStruct?.result_ok != true { |
| 52 | + throw InvalidSerializedDataError.runtimeError |
| 53 | + } |
| 54 | + let latestBlockHash = Bindings.LDKThirtyTwoBytes_to_array(nativeType: res.cOpaqueStruct!.contents.result.pointee.a) |
| 55 | + let channelManager = ChannelManager(pointer: res.cOpaqueStruct!.contents.result.pointee.b) |
| 56 | + |
| 57 | + self.channelManager = channelManager |
| 58 | + self.channel_manager_latest_block_hash = latestBlockHash |
| 59 | + self.chain_monitor = chain_monitor |
| 60 | + |
| 61 | + if let filter = filter { |
| 62 | + for (currentMonitor, _) in self.channel_monitors { |
| 63 | + currentMonitor.load_outputs_to_watch(filter: filter) |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + } |
| 68 | + |
| 69 | +} |
0 commit comments