|
| 1 | +import Foundation |
| 2 | + |
| 3 | +#if SWIFT_PACKAGE |
| 4 | + import LDKHeaders |
| 5 | +#endif |
| 6 | + |
| 7 | + |
| 8 | +/// |
| 9 | +public typealias AsyncPaymentsMessage = Bindings.AsyncPaymentsMessage |
| 10 | + |
| 11 | +extension Bindings { |
| 12 | + |
| 13 | + /// Possible async payment messages sent and received via an [`OnionMessage`]. |
| 14 | + /// |
| 15 | + /// [`OnionMessage`]: crate::ln::msgs::OnionMessage |
| 16 | + public class AsyncPaymentsMessage: NativeTypeWrapper { |
| 17 | + |
| 18 | + |
| 19 | + /// Set to false to suppress an individual type's deinit log statements. |
| 20 | + /// Only applicable when log threshold is set to `.Debug`. |
| 21 | + public static var enableDeinitLogging = true |
| 22 | + |
| 23 | + /// Set to true to suspend the freeing of this type's associated Rust memory. |
| 24 | + /// Should only ever be used for debugging purposes, and will likely be |
| 25 | + /// deprecated soon. |
| 26 | + public static var suspendFreedom = false |
| 27 | + |
| 28 | + private static var instanceCounter: UInt = 0 |
| 29 | + internal let instanceNumber: UInt |
| 30 | + |
| 31 | + internal var cType: LDKAsyncPaymentsMessage? |
| 32 | + |
| 33 | + internal init(cType: LDKAsyncPaymentsMessage, instantiationContext: String) { |
| 34 | + Self.instanceCounter += 1 |
| 35 | + self.instanceNumber = Self.instanceCounter |
| 36 | + self.cType = cType |
| 37 | + |
| 38 | + super.init(conflictAvoidingVariableName: 0, instantiationContext: instantiationContext) |
| 39 | + } |
| 40 | + |
| 41 | + internal init(cType: LDKAsyncPaymentsMessage, instantiationContext: String, anchor: NativeTypeWrapper) { |
| 42 | + Self.instanceCounter += 1 |
| 43 | + self.instanceNumber = Self.instanceCounter |
| 44 | + self.cType = cType |
| 45 | + |
| 46 | + super.init(conflictAvoidingVariableName: 0, instantiationContext: instantiationContext) |
| 47 | + self.dangling = true |
| 48 | + try! self.addAnchor(anchor: anchor) |
| 49 | + } |
| 50 | + |
| 51 | + internal init( |
| 52 | + cType: LDKAsyncPaymentsMessage, instantiationContext: String, anchor: NativeTypeWrapper, |
| 53 | + dangle: Bool = false |
| 54 | + ) { |
| 55 | + Self.instanceCounter += 1 |
| 56 | + self.instanceNumber = Self.instanceCounter |
| 57 | + self.cType = cType |
| 58 | + |
| 59 | + super.init(conflictAvoidingVariableName: 0, instantiationContext: instantiationContext) |
| 60 | + self.dangling = dangle |
| 61 | + try! self.addAnchor(anchor: anchor) |
| 62 | + } |
| 63 | + |
| 64 | + |
| 65 | + public enum AsyncPaymentsMessageType { |
| 66 | + |
| 67 | + /// An HTLC is being held upstream for the often-offline recipient, to be released via |
| 68 | + /// [`ReleaseHeldHtlc`]. |
| 69 | + case HeldHtlcAvailable |
| 70 | + |
| 71 | + /// Releases the HTLC corresponding to an inbound [`HeldHtlcAvailable`] message. |
| 72 | + case ReleaseHeldHtlc |
| 73 | + |
| 74 | + } |
| 75 | + |
| 76 | + public func getValueType() -> AsyncPaymentsMessageType { |
| 77 | + switch self.cType!.tag { |
| 78 | + case LDKAsyncPaymentsMessage_HeldHtlcAvailable: |
| 79 | + return .HeldHtlcAvailable |
| 80 | + |
| 81 | + case LDKAsyncPaymentsMessage_ReleaseHeldHtlc: |
| 82 | + return .ReleaseHeldHtlc |
| 83 | + |
| 84 | + default: |
| 85 | + Bindings.print("Error: Invalid value type for AsyncPaymentsMessage! Aborting.", severity: .ERROR) |
| 86 | + abort() |
| 87 | + } |
| 88 | + |
| 89 | + } |
| 90 | + |
| 91 | + |
| 92 | + /// Frees any resources used by the AsyncPaymentsMessage |
| 93 | + internal func free() { |
| 94 | + // native call variable prep |
| 95 | + |
| 96 | + |
| 97 | + // native method call |
| 98 | + let nativeCallResult = AsyncPaymentsMessage_free(self.cType!) |
| 99 | + |
| 100 | + // cleanup |
| 101 | + |
| 102 | + |
| 103 | + // return value (do some wrapping) |
| 104 | + let returnValue = nativeCallResult |
| 105 | + |
| 106 | + |
| 107 | + return returnValue |
| 108 | + } |
| 109 | + |
| 110 | + /// Creates a copy of the AsyncPaymentsMessage |
| 111 | + internal func clone() -> AsyncPaymentsMessage { |
| 112 | + // native call variable prep |
| 113 | + |
| 114 | + |
| 115 | + // native method call |
| 116 | + let nativeCallResult = |
| 117 | + withUnsafePointer(to: self.cType!) { (origPointer: UnsafePointer<LDKAsyncPaymentsMessage>) in |
| 118 | + AsyncPaymentsMessage_clone(origPointer) |
| 119 | + } |
| 120 | + |
| 121 | + |
| 122 | + // cleanup |
| 123 | + |
| 124 | + |
| 125 | + // return value (do some wrapping) |
| 126 | + let returnValue = AsyncPaymentsMessage( |
| 127 | + cType: nativeCallResult, instantiationContext: "AsyncPaymentsMessage.swift::\(#function):\(#line)") |
| 128 | + |
| 129 | + |
| 130 | + return returnValue |
| 131 | + } |
| 132 | + |
| 133 | + /// Utility method to constructs a new HeldHtlcAvailable-variant AsyncPaymentsMessage |
| 134 | + public class func initWithHeldHtlcAvailable(a: Bindings.HeldHtlcAvailable) -> AsyncPaymentsMessage { |
| 135 | + // native call variable prep |
| 136 | + |
| 137 | + |
| 138 | + // native method call |
| 139 | + let nativeCallResult = AsyncPaymentsMessage_held_htlc_available(a.dynamicallyDangledClone().cType!) |
| 140 | + |
| 141 | + // cleanup |
| 142 | + |
| 143 | + |
| 144 | + // return value (do some wrapping) |
| 145 | + let returnValue = AsyncPaymentsMessage( |
| 146 | + cType: nativeCallResult, instantiationContext: "AsyncPaymentsMessage.swift::\(#function):\(#line)") |
| 147 | + |
| 148 | + |
| 149 | + return returnValue |
| 150 | + } |
| 151 | + |
| 152 | + /// Utility method to constructs a new ReleaseHeldHtlc-variant AsyncPaymentsMessage |
| 153 | + public class func initWithReleaseHeldHtlc(a: Bindings.ReleaseHeldHtlc) -> AsyncPaymentsMessage { |
| 154 | + // native call variable prep |
| 155 | + |
| 156 | + |
| 157 | + // native method call |
| 158 | + let nativeCallResult = AsyncPaymentsMessage_release_held_htlc(a.dynamicallyDangledClone().cType!) |
| 159 | + |
| 160 | + // cleanup |
| 161 | + |
| 162 | + |
| 163 | + // return value (do some wrapping) |
| 164 | + let returnValue = AsyncPaymentsMessage( |
| 165 | + cType: nativeCallResult, instantiationContext: "AsyncPaymentsMessage.swift::\(#function):\(#line)") |
| 166 | + |
| 167 | + |
| 168 | + return returnValue |
| 169 | + } |
| 170 | + |
| 171 | + /// Returns whether `tlv_type` corresponds to a TLV record for async payment messages. |
| 172 | + public class func isKnownType(tlvType: UInt64) -> Bool { |
| 173 | + // native call variable prep |
| 174 | + |
| 175 | + |
| 176 | + // native method call |
| 177 | + let nativeCallResult = AsyncPaymentsMessage_is_known_type(tlvType) |
| 178 | + |
| 179 | + // cleanup |
| 180 | + |
| 181 | + |
| 182 | + // return value (do some wrapping) |
| 183 | + let returnValue = nativeCallResult |
| 184 | + |
| 185 | + |
| 186 | + return returnValue |
| 187 | + } |
| 188 | + |
| 189 | + /// Constructs a new OnionMessageContents which calls the relevant methods on this_arg. |
| 190 | + /// This copies the `inner` pointer in this_arg and thus the returned OnionMessageContents must be freed before this_arg is |
| 191 | + public func asOnionMessageContents() -> Bindings.OnionMessageContents { |
| 192 | + // native call variable prep |
| 193 | + |
| 194 | + |
| 195 | + // native method call |
| 196 | + let nativeCallResult = |
| 197 | + withUnsafePointer(to: self.cType!) { (thisArgPointer: UnsafePointer<LDKAsyncPaymentsMessage>) in |
| 198 | + AsyncPaymentsMessage_as_OnionMessageContents(thisArgPointer) |
| 199 | + } |
| 200 | + |
| 201 | + |
| 202 | + // cleanup |
| 203 | + |
| 204 | + |
| 205 | + // return value (do some wrapping) |
| 206 | + let returnValue = NativelyImplementedOnionMessageContents( |
| 207 | + cType: nativeCallResult, instantiationContext: "AsyncPaymentsMessage.swift::\(#function):\(#line)", |
| 208 | + anchor: self) |
| 209 | + |
| 210 | + |
| 211 | + return returnValue |
| 212 | + } |
| 213 | + |
| 214 | + /// Serialize the AsyncPaymentsMessage object into a byte array which can be read by AsyncPaymentsMessage_read |
| 215 | + public func write() -> [UInt8] { |
| 216 | + // native call variable prep |
| 217 | + |
| 218 | + |
| 219 | + // native method call |
| 220 | + let nativeCallResult = |
| 221 | + withUnsafePointer(to: self.cType!) { (objPointer: UnsafePointer<LDKAsyncPaymentsMessage>) in |
| 222 | + AsyncPaymentsMessage_write(objPointer) |
| 223 | + } |
| 224 | + |
| 225 | + |
| 226 | + // cleanup |
| 227 | + |
| 228 | + |
| 229 | + // return value (do some wrapping) |
| 230 | + let returnValue = Vec_u8Z( |
| 231 | + cType: nativeCallResult, instantiationContext: "AsyncPaymentsMessage.swift::\(#function):\(#line)", |
| 232 | + anchor: self |
| 233 | + ) |
| 234 | + .dangle(false).getValue() |
| 235 | + |
| 236 | + |
| 237 | + return returnValue |
| 238 | + } |
| 239 | + |
| 240 | + /// Read a AsyncPaymentsMessage from a byte array, created by AsyncPaymentsMessage_write |
| 241 | + public class func read(ser: [UInt8], arg: UInt64) -> Result_AsyncPaymentsMessageDecodeErrorZ { |
| 242 | + // native call variable prep |
| 243 | + |
| 244 | + let serPrimitiveWrapper = u8slice( |
| 245 | + value: ser, instantiationContext: "AsyncPaymentsMessage.swift::\(#function):\(#line)") |
| 246 | + |
| 247 | + |
| 248 | + // native method call |
| 249 | + let nativeCallResult = AsyncPaymentsMessage_read(serPrimitiveWrapper.cType!, arg) |
| 250 | + |
| 251 | + // cleanup |
| 252 | + |
| 253 | + // for elided types, we need this |
| 254 | + serPrimitiveWrapper.noOpRetain() |
| 255 | + |
| 256 | + |
| 257 | + // return value (do some wrapping) |
| 258 | + let returnValue = Result_AsyncPaymentsMessageDecodeErrorZ( |
| 259 | + cType: nativeCallResult, instantiationContext: "AsyncPaymentsMessage.swift::\(#function):\(#line)") |
| 260 | + |
| 261 | + |
| 262 | + return returnValue |
| 263 | + } |
| 264 | + |
| 265 | + |
| 266 | + public func getValueAsHeldHtlcAvailable() -> Bindings.HeldHtlcAvailable? { |
| 267 | + if self.cType?.tag != LDKAsyncPaymentsMessage_HeldHtlcAvailable { |
| 268 | + return nil |
| 269 | + } |
| 270 | + |
| 271 | + return HeldHtlcAvailable( |
| 272 | + cType: self.cType!.held_htlc_available, |
| 273 | + instantiationContext: "AsyncPaymentsMessage.swift::\(#function):\(#line)", anchor: self) |
| 274 | + } |
| 275 | + |
| 276 | + public func getValueAsReleaseHeldHtlc() -> Bindings.ReleaseHeldHtlc? { |
| 277 | + if self.cType?.tag != LDKAsyncPaymentsMessage_ReleaseHeldHtlc { |
| 278 | + return nil |
| 279 | + } |
| 280 | + |
| 281 | + return ReleaseHeldHtlc( |
| 282 | + cType: self.cType!.release_held_htlc, |
| 283 | + instantiationContext: "AsyncPaymentsMessage.swift::\(#function):\(#line)", anchor: self) |
| 284 | + } |
| 285 | + |
| 286 | + |
| 287 | + internal func danglingClone() -> AsyncPaymentsMessage { |
| 288 | + let dangledClone = self.clone() |
| 289 | + dangledClone.dangling = true |
| 290 | + return dangledClone |
| 291 | + } |
| 292 | + |
| 293 | + deinit { |
| 294 | + if Bindings.suspendFreedom || Self.suspendFreedom { |
| 295 | + return |
| 296 | + } |
| 297 | + |
| 298 | + if !self.dangling { |
| 299 | + if Self.enableDeinitLogging { |
| 300 | + Bindings.print( |
| 301 | + "Freeing AsyncPaymentsMessage \(self.instanceNumber). (Origin: \(self.instantiationContext))") |
| 302 | + } |
| 303 | + |
| 304 | + self.free() |
| 305 | + } else if Self.enableDeinitLogging { |
| 306 | + Bindings.print( |
| 307 | + "Not freeing AsyncPaymentsMessage \(self.instanceNumber) due to dangle. (Origin: \(self.instantiationContext))" |
| 308 | + ) |
| 309 | + } |
| 310 | + } |
| 311 | + |
| 312 | + |
| 313 | + } |
| 314 | + |
| 315 | +} |
0 commit comments