|
| 1 | +import Foundation |
| 2 | + |
| 3 | +#if SWIFT_PACKAGE |
| 4 | + import LDKHeaders |
| 5 | +#endif |
| 6 | + |
| 7 | + |
| 8 | +/// |
| 9 | +public typealias AsyncPaymentsContext = Bindings.AsyncPaymentsContext |
| 10 | + |
| 11 | +extension Bindings { |
| 12 | + |
| 13 | + /// Contains data specific to an [`AsyncPaymentsMessage`]. |
| 14 | + /// |
| 15 | + /// [`AsyncPaymentsMessage`]: crate::onion_message::async_payments::AsyncPaymentsMessage |
| 16 | + public class AsyncPaymentsContext: 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: LDKAsyncPaymentsContext? |
| 32 | + |
| 33 | + internal init(cType: LDKAsyncPaymentsContext, 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: LDKAsyncPaymentsContext, 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: LDKAsyncPaymentsContext, 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 AsyncPaymentsContextType { |
| 66 | + |
| 67 | + /// Context contained within the reply [`BlindedMessagePath`] we put in outbound |
| 68 | + /// [`HeldHtlcAvailable`] messages, provided back to us in corresponding [`ReleaseHeldHtlc`] |
| 69 | + /// messages. |
| 70 | + /// |
| 71 | + /// [`HeldHtlcAvailable`]: crate::onion_message::async_payments::HeldHtlcAvailable |
| 72 | + /// [`ReleaseHeldHtlc`]: crate::onion_message::async_payments::ReleaseHeldHtlc |
| 73 | + case OutboundPayment |
| 74 | + |
| 75 | + } |
| 76 | + |
| 77 | + public func getValueType() -> AsyncPaymentsContextType { |
| 78 | + switch self.cType!.tag { |
| 79 | + case LDKAsyncPaymentsContext_OutboundPayment: |
| 80 | + return .OutboundPayment |
| 81 | + |
| 82 | + default: |
| 83 | + Bindings.print("Error: Invalid value type for AsyncPaymentsContext! Aborting.", severity: .ERROR) |
| 84 | + abort() |
| 85 | + } |
| 86 | + |
| 87 | + } |
| 88 | + |
| 89 | + |
| 90 | + /// Frees any resources used by the AsyncPaymentsContext |
| 91 | + internal func free() { |
| 92 | + // native call variable prep |
| 93 | + |
| 94 | + |
| 95 | + // native method call |
| 96 | + let nativeCallResult = AsyncPaymentsContext_free(self.cType!) |
| 97 | + |
| 98 | + // cleanup |
| 99 | + |
| 100 | + |
| 101 | + // return value (do some wrapping) |
| 102 | + let returnValue = nativeCallResult |
| 103 | + |
| 104 | + |
| 105 | + return returnValue |
| 106 | + } |
| 107 | + |
| 108 | + /// Creates a copy of the AsyncPaymentsContext |
| 109 | + internal func clone() -> AsyncPaymentsContext { |
| 110 | + // native call variable prep |
| 111 | + |
| 112 | + |
| 113 | + // native method call |
| 114 | + let nativeCallResult = |
| 115 | + withUnsafePointer(to: self.cType!) { (origPointer: UnsafePointer<LDKAsyncPaymentsContext>) in |
| 116 | + AsyncPaymentsContext_clone(origPointer) |
| 117 | + } |
| 118 | + |
| 119 | + |
| 120 | + // cleanup |
| 121 | + |
| 122 | + |
| 123 | + // return value (do some wrapping) |
| 124 | + let returnValue = AsyncPaymentsContext( |
| 125 | + cType: nativeCallResult, instantiationContext: "AsyncPaymentsContext.swift::\(#function):\(#line)") |
| 126 | + |
| 127 | + |
| 128 | + return returnValue |
| 129 | + } |
| 130 | + |
| 131 | + /// Utility method to constructs a new OutboundPayment-variant AsyncPaymentsContext |
| 132 | + public class func initWithOutboundPayment(paymentId: [UInt8], nonce: Bindings.Nonce, hmac: [UInt8]) |
| 133 | + -> AsyncPaymentsContext |
| 134 | + { |
| 135 | + // native call variable prep |
| 136 | + |
| 137 | + let paymentIdPrimitiveWrapper = ThirtyTwoBytes( |
| 138 | + value: paymentId, instantiationContext: "AsyncPaymentsContext.swift::\(#function):\(#line)") |
| 139 | + |
| 140 | + let hmacPrimitiveWrapper = ThirtyTwoBytes( |
| 141 | + value: hmac, instantiationContext: "AsyncPaymentsContext.swift::\(#function):\(#line)") |
| 142 | + |
| 143 | + |
| 144 | + // native method call |
| 145 | + let nativeCallResult = AsyncPaymentsContext_outbound_payment( |
| 146 | + paymentIdPrimitiveWrapper.cType!, nonce.dynamicallyDangledClone().cType!, hmacPrimitiveWrapper.cType!) |
| 147 | + |
| 148 | + // cleanup |
| 149 | + |
| 150 | + // for elided types, we need this |
| 151 | + paymentIdPrimitiveWrapper.noOpRetain() |
| 152 | + |
| 153 | + // for elided types, we need this |
| 154 | + hmacPrimitiveWrapper.noOpRetain() |
| 155 | + |
| 156 | + |
| 157 | + // return value (do some wrapping) |
| 158 | + let returnValue = AsyncPaymentsContext( |
| 159 | + cType: nativeCallResult, instantiationContext: "AsyncPaymentsContext.swift::\(#function):\(#line)") |
| 160 | + |
| 161 | + |
| 162 | + return returnValue |
| 163 | + } |
| 164 | + |
| 165 | + /// Serialize the AsyncPaymentsContext object into a byte array which can be read by AsyncPaymentsContext_read |
| 166 | + public func write() -> [UInt8] { |
| 167 | + // native call variable prep |
| 168 | + |
| 169 | + |
| 170 | + // native method call |
| 171 | + let nativeCallResult = |
| 172 | + withUnsafePointer(to: self.cType!) { (objPointer: UnsafePointer<LDKAsyncPaymentsContext>) in |
| 173 | + AsyncPaymentsContext_write(objPointer) |
| 174 | + } |
| 175 | + |
| 176 | + |
| 177 | + // cleanup |
| 178 | + |
| 179 | + |
| 180 | + // return value (do some wrapping) |
| 181 | + let returnValue = Vec_u8Z( |
| 182 | + cType: nativeCallResult, instantiationContext: "AsyncPaymentsContext.swift::\(#function):\(#line)", |
| 183 | + anchor: self |
| 184 | + ) |
| 185 | + .dangle(false).getValue() |
| 186 | + |
| 187 | + |
| 188 | + return returnValue |
| 189 | + } |
| 190 | + |
| 191 | + /// Read a AsyncPaymentsContext from a byte array, created by AsyncPaymentsContext_write |
| 192 | + public class func read(ser: [UInt8]) -> Result_AsyncPaymentsContextDecodeErrorZ { |
| 193 | + // native call variable prep |
| 194 | + |
| 195 | + let serPrimitiveWrapper = u8slice( |
| 196 | + value: ser, instantiationContext: "AsyncPaymentsContext.swift::\(#function):\(#line)") |
| 197 | + |
| 198 | + |
| 199 | + // native method call |
| 200 | + let nativeCallResult = AsyncPaymentsContext_read(serPrimitiveWrapper.cType!) |
| 201 | + |
| 202 | + // cleanup |
| 203 | + |
| 204 | + // for elided types, we need this |
| 205 | + serPrimitiveWrapper.noOpRetain() |
| 206 | + |
| 207 | + |
| 208 | + // return value (do some wrapping) |
| 209 | + let returnValue = Result_AsyncPaymentsContextDecodeErrorZ( |
| 210 | + cType: nativeCallResult, instantiationContext: "AsyncPaymentsContext.swift::\(#function):\(#line)") |
| 211 | + |
| 212 | + |
| 213 | + return returnValue |
| 214 | + } |
| 215 | + |
| 216 | + |
| 217 | + public func getValueAsOutboundPayment() -> OutboundPayment? { |
| 218 | + if self.cType?.tag != LDKAsyncPaymentsContext_OutboundPayment { |
| 219 | + return nil |
| 220 | + } |
| 221 | + |
| 222 | + return AsyncPaymentsContext_LDKOutboundPayment_Body( |
| 223 | + cType: self.cType!.outbound_payment, |
| 224 | + instantiationContext: "AsyncPaymentsContext.swift::\(#function):\(#line)", anchor: self) |
| 225 | + } |
| 226 | + |
| 227 | + |
| 228 | + internal func danglingClone() -> AsyncPaymentsContext { |
| 229 | + let dangledClone = self.clone() |
| 230 | + dangledClone.dangling = true |
| 231 | + return dangledClone |
| 232 | + } |
| 233 | + |
| 234 | + deinit { |
| 235 | + if Bindings.suspendFreedom || Self.suspendFreedom { |
| 236 | + return |
| 237 | + } |
| 238 | + |
| 239 | + if !self.dangling { |
| 240 | + if Self.enableDeinitLogging { |
| 241 | + Bindings.print( |
| 242 | + "Freeing AsyncPaymentsContext \(self.instanceNumber). (Origin: \(self.instantiationContext))") |
| 243 | + } |
| 244 | + |
| 245 | + self.free() |
| 246 | + } else if Self.enableDeinitLogging { |
| 247 | + Bindings.print( |
| 248 | + "Not freeing AsyncPaymentsContext \(self.instanceNumber) due to dangle. (Origin: \(self.instantiationContext))" |
| 249 | + ) |
| 250 | + } |
| 251 | + } |
| 252 | + |
| 253 | + |
| 254 | + /// |
| 255 | + internal typealias AsyncPaymentsContext_LDKOutboundPayment_Body = OutboundPayment |
| 256 | + |
| 257 | + |
| 258 | + /// |
| 259 | + public class OutboundPayment: NativeTypeWrapper { |
| 260 | + |
| 261 | + |
| 262 | + /// Set to false to suppress an individual type's deinit log statements. |
| 263 | + /// Only applicable when log threshold is set to `.Debug`. |
| 264 | + public static var enableDeinitLogging = true |
| 265 | + |
| 266 | + /// Set to true to suspend the freeing of this type's associated Rust memory. |
| 267 | + /// Should only ever be used for debugging purposes, and will likely be |
| 268 | + /// deprecated soon. |
| 269 | + public static var suspendFreedom = false |
| 270 | + |
| 271 | + private static var instanceCounter: UInt = 0 |
| 272 | + internal let instanceNumber: UInt |
| 273 | + |
| 274 | + internal var cType: LDKAsyncPaymentsContext_LDKOutboundPayment_Body? |
| 275 | + |
| 276 | + internal init(cType: LDKAsyncPaymentsContext_LDKOutboundPayment_Body, instantiationContext: String) { |
| 277 | + Self.instanceCounter += 1 |
| 278 | + self.instanceNumber = Self.instanceCounter |
| 279 | + self.cType = cType |
| 280 | + |
| 281 | + super.init(conflictAvoidingVariableName: 0, instantiationContext: instantiationContext) |
| 282 | + } |
| 283 | + |
| 284 | + internal init( |
| 285 | + cType: LDKAsyncPaymentsContext_LDKOutboundPayment_Body, instantiationContext: String, |
| 286 | + anchor: NativeTypeWrapper |
| 287 | + ) { |
| 288 | + Self.instanceCounter += 1 |
| 289 | + self.instanceNumber = Self.instanceCounter |
| 290 | + self.cType = cType |
| 291 | + |
| 292 | + super.init(conflictAvoidingVariableName: 0, instantiationContext: instantiationContext) |
| 293 | + self.dangling = true |
| 294 | + try! self.addAnchor(anchor: anchor) |
| 295 | + } |
| 296 | + |
| 297 | + internal init( |
| 298 | + cType: LDKAsyncPaymentsContext_LDKOutboundPayment_Body, instantiationContext: String, |
| 299 | + anchor: NativeTypeWrapper, dangle: Bool = false |
| 300 | + ) { |
| 301 | + Self.instanceCounter += 1 |
| 302 | + self.instanceNumber = Self.instanceCounter |
| 303 | + self.cType = cType |
| 304 | + |
| 305 | + super.init(conflictAvoidingVariableName: 0, instantiationContext: instantiationContext) |
| 306 | + self.dangling = dangle |
| 307 | + try! self.addAnchor(anchor: anchor) |
| 308 | + } |
| 309 | + |
| 310 | + |
| 311 | + /// ID used when payment to the originating [`Offer`] was initiated. Useful for us to identify |
| 312 | + /// which of our pending outbound payments should be released to its often-offline payee. |
| 313 | + /// |
| 314 | + /// [`Offer`]: crate::offers::offer::Offer |
| 315 | + public func getPaymentId() -> [UInt8] { |
| 316 | + // return value (do some wrapping) |
| 317 | + let returnValue = ThirtyTwoBytes( |
| 318 | + cType: self.cType!.payment_id, |
| 319 | + instantiationContext: "AsyncPaymentsContext.swift::\(#function):\(#line)", anchor: self |
| 320 | + ) |
| 321 | + .getValue() |
| 322 | + |
| 323 | + return returnValue |
| 324 | + } |
| 325 | + |
| 326 | + /// A nonce used for authenticating that a [`ReleaseHeldHtlc`] message is valid for a preceding |
| 327 | + /// [`HeldHtlcAvailable`] message. |
| 328 | + /// |
| 329 | + /// [`ReleaseHeldHtlc`]: crate::onion_message::async_payments::ReleaseHeldHtlc |
| 330 | + /// [`HeldHtlcAvailable`]: crate::onion_message::async_payments::HeldHtlcAvailable |
| 331 | + public func getNonce() -> Bindings.Nonce { |
| 332 | + // return value (do some wrapping) |
| 333 | + let returnValue = Bindings.Nonce( |
| 334 | + cType: self.cType!.nonce, instantiationContext: "AsyncPaymentsContext.swift::\(#function):\(#line)", |
| 335 | + anchor: self) |
| 336 | + |
| 337 | + return returnValue |
| 338 | + } |
| 339 | + |
| 340 | + /// Authentication code for the [`PaymentId`]. |
| 341 | + /// |
| 342 | + /// Prevents the recipient from being able to deanonymize us by creating a blinded path to us |
| 343 | + /// containing the expected [`PaymentId`]. |
| 344 | + public func getHmac() -> [UInt8] { |
| 345 | + // return value (do some wrapping) |
| 346 | + let returnValue = ThirtyTwoBytes( |
| 347 | + cType: self.cType!.hmac, instantiationContext: "AsyncPaymentsContext.swift::\(#function):\(#line)", |
| 348 | + anchor: self |
| 349 | + ) |
| 350 | + .getValue() |
| 351 | + |
| 352 | + return returnValue |
| 353 | + } |
| 354 | + |
| 355 | + |
| 356 | + } |
| 357 | + |
| 358 | + |
| 359 | + } |
| 360 | + |
| 361 | +} |
0 commit comments