Skip to content

Commit 12815eb

Browse files
authored
Merge pull request #11 from lightningdevkit/option_array_support
Add binary option array support
2 parents dfce1b3 + 8710fe7 commit 12815eb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+1719
-198
lines changed

.github/workflows/swift.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@ jobs:
2020
cd rust-lightning
2121
# git checkout origin/2021-03-java-bindings-base
2222
# git checkout v0.0.100
23-
git checkout e9c4fa6c6959f2cdba29b4a3b47088053fb4822e
23+
git checkout 22280286ed38a4753c63c7163fb8414eb1fd3177
2424
echo "rust-lightning commit hash:"
2525
git rev-parse HEAD
2626
cd ..
2727
# git clone https://github.com/lightningdevkit/ldk-c-bindings
2828
git clone https://github.com/TheBlueMatt/ldk-c-bindings
2929
cd ldk-c-bindings
30-
git checkout 9e501cbbcde7b6624ec03372452e915967e84064
30+
git checkout fee27c4b40d1d25609a38bb220dd64b297af929a
3131
echo "ldk-c-bindings commit hash:"
3232
git rev-parse HEAD
3333
cd ..

bindings/LDK/Bindings.swift

Lines changed: 176 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
//
77

88
import Foundation
9-
#if canImport(os)
10-
import os
11-
#endif
9+
// #if canImport(os)
10+
// import os
11+
// #endif
1212

1313
public typealias LDKTransactionOutputs = LDKC2Tuple_TxidCVec_C2Tuple_u32TxOutZZZ
1414
public typealias TransactionOutputs = C2Tuple_TxidCVec_C2Tuple_u32TxOutZZZ
@@ -67,9 +67,9 @@ open class NativeTypeWrapper: Hashable {
6767
public class Bindings {
6868

6969
internal static var minimumPrintSeverity: PrintSeverity = .WARNING
70-
#if canImport(os)
71-
internal static let logger = os.Logger(subsystem: Bundle.main.bundleIdentifier!, category: "ldk")
72-
#endif
70+
// #if canImport(os)
71+
// internal static let logger = os.Logger(subsystem: Bundle.main.bundleIdentifier!, category: "ldk")
72+
// #endif
7373

7474
public enum PrintSeverity: UInt {
7575
case DEBUG = 0
@@ -79,19 +79,25 @@ public class Bindings {
7979

8080
internal class func print(_ string: String, severity: PrintSeverity = .DEBUG) {
8181
if severity.rawValue >= Self.minimumPrintSeverity.rawValue {
82-
#if canImport(os)
83-
if severity == Self.PrintSeverity.DEBUG {
84-
logger.debug("\(string)")
85-
}else if severity == Self.PrintSeverity.WARNING {
86-
logger.warning("\(string)")
87-
}else if severity == Self.PrintSeverity.ERROR {
88-
logger.error("\(string)")
89-
}else {
90-
logger.log("\(string)")
91-
}
92-
#else
93-
Swift.print(string)
94-
#endif
82+
Swift.print(string)
83+
// if #available(iOS 14.0, *) {
84+
// #if canImport(os)
85+
// if severity == Self.PrintSeverity.DEBUG {
86+
// logger.debug("\(string)")
87+
// }else if severity == Self.PrintSeverity.WARNING {
88+
// logger.warning("\(string)")
89+
// }else if severity == Self.PrintSeverity.ERROR {
90+
// logger.error("\(string)")
91+
// }else {
92+
// logger.log("\(string)")
93+
// }
94+
// #else
95+
// Swift.print(string)
96+
// #endif
97+
// } else {
98+
// // Fallback on earlier versions
99+
// Swift.print(string)
100+
// }
95101
}
96102
}
97103

@@ -927,7 +933,7 @@ public class Bindings {
927933
/* RUST_TO_SWIFT_END */
928934
public class func extractNativeLDKC2Tuple_BlockHashChannelMonitorZArray(array: [C2Tuple_BlockHashChannelMonitorZ]) -> [LDKC2Tuple_BlockHashChannelMonitorZ] {
929935
return array.map { entry -> LDKC2Tuple_BlockHashChannelMonitorZ in
930-
entry.cOpaqueStruct!
936+
entry.danglingClone().cOpaqueStruct!
931937
}
932938
}
933939

@@ -943,6 +949,13 @@ public class Bindings {
943949
}
944950
}
945951

952+
internal class func cloneNativeLDKC2Tuple_BlockHashChannelMonitorZArray(array: [LDKC2Tuple_BlockHashChannelMonitorZ]) -> [LDKC2Tuple_BlockHashChannelMonitorZ] {
953+
return array.map { entry -> LDKC2Tuple_BlockHashChannelMonitorZ in
954+
// create a wrapper around the native object, dangle it to make it non-destructive, clone it, and then dangle the clone
955+
C2Tuple_BlockHashChannelMonitorZ(pointer: entry).dangle().clone().dangle().cOpaqueStruct!
956+
}
957+
}
958+
946959

947960
/* SWIFT_TO_RUST_START */
948961
public class func new_LDKCVec_C2Tuple_PublicKeyTypeZZWrapper(array: [LDKC2Tuple_PublicKeyTypeZ]) -> LDKCVec_C2Tuple_PublicKeyTypeZZWrapper {
@@ -2558,6 +2571,108 @@ public class Bindings {
25582571
}
25592572

25602573

2574+
/* SWIFT_TO_RUST_START */
2575+
public class func new_LDKCVec_OutPointZWrapper(array: [LDKOutPoint]) -> LDKCVec_OutPointZWrapper {
2576+
/* DIMENSION_REDUCTION_PREP */
2577+
2578+
/*
2579+
let dataContainer = array.withUnsafeBufferPointer { (pointer: UnsafeBufferPointer<LDKOutPoint>) -> UnsafeMutablePointer<LDKOutPoint> in
2580+
let mutablePointer = UnsafeMutablePointer<LDKOutPoint>(mutating: pointer.baseAddress!)
2581+
return mutablePointer
2582+
}
2583+
*/
2584+
2585+
let dataContainer = UnsafeMutablePointer<LDKOutPoint>.allocate(capacity: array.count)
2586+
dataContainer.initialize(from: array, count: array.count)
2587+
2588+
let vector = LDKCVec_OutPointZ(data: dataContainer, datalen: UInt(array.count))
2589+
let wrapper = LDKCVec_OutPointZWrapper(pointer: vector)
2590+
return wrapper
2591+
}
2592+
2593+
public class LDKCVec_OutPointZWrapper: NativeTypeWrapper {
2594+
private static var instanceCounter: UInt = 0
2595+
internal let instanceNumber: UInt
2596+
2597+
internal var cOpaqueStruct: LDKCVec_OutPointZ?
2598+
internal var subdimensionWrapper: [AnyObject]? = nil
2599+
2600+
public init(pointer: LDKCVec_OutPointZ){
2601+
Self.instanceCounter += 1
2602+
self.instanceNumber = Self.instanceCounter
2603+
self.cOpaqueStruct = pointer
2604+
super.init(conflictAvoidingVariableName: 0)
2605+
}
2606+
2607+
internal init(pointer: LDKCVec_OutPointZ, subdimensionWrapper: [AnyObject]){
2608+
Self.instanceCounter += 1
2609+
self.instanceNumber = Self.instanceCounter
2610+
self.subdimensionWrapper = subdimensionWrapper
2611+
self.cOpaqueStruct = pointer
2612+
super.init(conflictAvoidingVariableName: 0)
2613+
}
2614+
2615+
public func noOpRetain(){}
2616+
2617+
internal func dangle(dangleSubdimensions: Bool = true) -> LDKCVec_OutPointZWrapper {
2618+
self.dangling = true
2619+
/* SUBDIMENSION_DANGLE_PREP */
2620+
return self
2621+
}
2622+
2623+
deinit {
2624+
if !self.dangling {
2625+
print("Freeing LDKCVec_OutPointZWrapper \(self.instanceNumber).")
2626+
self.cOpaqueStruct!.data.deallocate()
2627+
} else {
2628+
print("Not freeing LDKCVec_OutPointZWrapper \(self.instanceNumber) due to dangle.")
2629+
}
2630+
}
2631+
}
2632+
/* SWIFT_TO_RUST_END */
2633+
2634+
/* RUST_TO_SWIFT_START */
2635+
public class func LDKCVec_OutPointZ_to_array(nativeType: LDKCVec_OutPointZ, deallocate: Bool = true) -> [LDKOutPoint] {
2636+
var array = [LDKOutPoint]()
2637+
for index in 0..<Int(nativeType.datalen) {
2638+
let currentEntry = nativeType.data[index]
2639+
/* CONVERSION_PREP */
2640+
array.append(currentEntry)
2641+
}
2642+
2643+
if deallocate && nativeType.datalen > 0 {
2644+
nativeType.data.deallocate()
2645+
}
2646+
2647+
return array
2648+
}
2649+
/* RUST_TO_SWIFT_END */
2650+
public class func extractNativeLDKOutPointArray(array: [OutPoint]) -> [LDKOutPoint] {
2651+
return array.map { entry -> LDKOutPoint in
2652+
entry.danglingClone().cOpaqueStruct!
2653+
}
2654+
}
2655+
2656+
public class func wrapNativeLDKOutPointArray(array: [LDKOutPoint]) -> [OutPoint] {
2657+
return array.map { entry -> OutPoint in
2658+
OutPoint(pointer: entry)
2659+
}
2660+
}
2661+
2662+
public class func wrapDanglingNativeLDKOutPointArray(array: [LDKOutPoint]) -> [OutPoint] {
2663+
return array.map { entry -> OutPoint in
2664+
OutPoint(pointer: entry).dangle()
2665+
}
2666+
}
2667+
2668+
internal class func cloneNativeLDKOutPointArray(array: [LDKOutPoint]) -> [LDKOutPoint] {
2669+
return array.map { entry -> LDKOutPoint in
2670+
// create a wrapper around the native object, dangle it to make it non-destructive, clone it, and then dangle the clone
2671+
OutPoint(pointer: entry).dangle().clone().dangle().cOpaqueStruct!
2672+
}
2673+
}
2674+
2675+
25612676
/* SWIFT_TO_RUST_START */
25622677
public class func new_LDKCVec_PrivateRouteZWrapper(array: [LDKPrivateRoute]) -> LDKCVec_PrivateRouteZWrapper {
25632678
/* DIMENSION_REDUCTION_PREP */
@@ -4362,7 +4477,7 @@ withUnsafePointer(to: htlc.cOpaqueStruct!) { (htlcPointer: UnsafePointer<LDKHTLC
43624477

43634478

43644479
}
4365-
public class func swift_get_keysend_route(our_node_id: [UInt8], network: NetworkGraph, payee: [UInt8], first_hops: [ChannelDetails]?, last_hops: [RouteHint], final_value_msat: UInt64, final_cltv: UInt32, logger: Logger) -> Result_RouteLightningErrorZ {
4480+
public class func swift_get_keysend_route(our_node_pubkey: [UInt8], network: NetworkGraph, payee: [UInt8], first_hops: [ChannelDetails]?, last_hops: [RouteHint], final_value_msat: UInt64, final_cltv: UInt32, logger: Logger, scorer: Score) -> Result_RouteLightningErrorZ {
43664481

43674482

43684483
var first_hopsPointer: UnsafeMutablePointer<LDKCVec_ChannelDetailsZ>? = nil
@@ -4388,9 +4503,45 @@ withUnsafePointer(to: htlc.cOpaqueStruct!) { (htlcPointer: UnsafePointer<LDKHTLC
43884503
}
43894504

43904505
return withUnsafePointer(to: network.cOpaqueStruct!) { (networkPointer: UnsafePointer<LDKNetworkGraph>) in
4506+
withUnsafePointer(to: scorer.cOpaqueStruct!) { (scorerPointer: UnsafePointer<LDKScore>) in
43914507

4392-
Result_RouteLightningErrorZ(pointer: get_keysend_route(Bindings.new_LDKPublicKey(array: our_node_id), networkPointer, Bindings.new_LDKPublicKey(array: payee), first_hopsPointer, last_hopsWrapper.dangle().cOpaqueStruct!, final_value_msat, final_cltv, logger.cOpaqueStruct!))
4508+
Result_RouteLightningErrorZ(pointer: get_keysend_route(Bindings.new_LDKPublicKey(array: our_node_pubkey), networkPointer, Bindings.new_LDKPublicKey(array: payee), first_hopsPointer, last_hopsWrapper.dangle().cOpaqueStruct!, final_value_msat, final_cltv, logger.cOpaqueStruct!, scorerPointer))
43934509

4510+
}
4511+
}
4512+
4513+
}
4514+
public class func swift_get_route(our_node_pubkey: [UInt8], network: NetworkGraph, payee: [UInt8], payee_features: InvoiceFeatures, first_hops: [ChannelDetails]?, last_hops: [RouteHint], final_value_msat: UInt64, final_cltv: UInt32, logger: Logger, scorer: Score) -> Result_RouteLightningErrorZ {
4515+
4516+
4517+
var first_hopsPointer: UnsafeMutablePointer<LDKCVec_ChannelDetailsZ>? = nil
4518+
if let first_hopsUnwrapped = first_hops {
4519+
4520+
let first_hopsUnwrapped = first_hopsUnwrapped.map { (first_hopsCurrentValue) in
4521+
first_hopsCurrentValue
4522+
.danglingClone().cOpaqueStruct!
4523+
}
4524+
4525+
first_hopsPointer = UnsafeMutablePointer<LDKCVec_ChannelDetailsZ>.allocate(capacity: 1)
4526+
first_hopsPointer!.initialize(to: Bindings.new_LDKCVec_ChannelDetailsZWrapper(array: first_hopsUnwrapped).cOpaqueStruct!)
4527+
}
4528+
4529+
let last_hopsUnwrapped = last_hops.map { (last_hopsCurrentValue) in
4530+
last_hopsCurrentValue
4531+
.danglingClone().cOpaqueStruct!
4532+
}
4533+
4534+
let last_hopsWrapper = Bindings.new_LDKCVec_RouteHintZWrapper(array: last_hopsUnwrapped)
4535+
defer {
4536+
last_hopsWrapper.noOpRetain()
4537+
}
4538+
4539+
return withUnsafePointer(to: network.cOpaqueStruct!) { (networkPointer: UnsafePointer<LDKNetworkGraph>) in
4540+
withUnsafePointer(to: scorer.cOpaqueStruct!) { (scorerPointer: UnsafePointer<LDKScore>) in
4541+
4542+
Result_RouteLightningErrorZ(pointer: get_route(Bindings.new_LDKPublicKey(array: our_node_pubkey), networkPointer, Bindings.new_LDKPublicKey(array: payee), payee_features.danglingClone().cOpaqueStruct!, first_hopsPointer, last_hopsWrapper.dangle().cOpaqueStruct!, final_value_msat, final_cltv, logger.cOpaqueStruct!, scorerPointer))
4543+
4544+
}
43944545
}
43954546

43964547
}
@@ -4573,6 +4724,7 @@ withUnsafePointer(to: htlc.cOpaqueStruct!) { (htlcPointer: UnsafePointer<LDKHTLC
45734724
}
45744725
}
45754726

4727+
/*
45764728
public class func getRoute(our_node_id: [UInt8], network: NetworkGraph, payee: [UInt8], payee_features: InvoiceFeatures, first_hops: [LDKChannelDetails], last_hops: [LDKRouteHint], final_value_msat: UInt64, final_cltv: UInt32, logger: Logger) -> Result_RouteLightningErrorZ {
45774729
return withUnsafePointer(to: network.cOpaqueStruct!) { (networkPointer: UnsafePointer<LDKNetworkGraph>) in
45784730
var mutableHops = Bindings.new_LDKCVec_ChannelDetailsZWrapper(array: first_hops).cOpaqueStruct!
@@ -4581,9 +4733,10 @@ withUnsafePointer(to: htlc.cOpaqueStruct!) { (htlcPointer: UnsafePointer<LDKHTLC
45814733
}
45824734
}
45834735
}
4736+
*/
45844737

45854738
public class func get_ldk_swift_bindings_version() -> String {
4586-
return "034c20c8e2a658f53ade8a8350a1aa5f36118992"
4739+
return "49993985ba4f2dd197deef5f62758b2f8bfa11cc"
45874740
}
45884741

45894742
}

0 commit comments

Comments
 (0)