Skip to content

Commit ef28044

Browse files
committed
add cloneability for bech32 and parse errors
1 parent fe0ea5b commit ef28044

File tree

13 files changed

+273
-159
lines changed

13 files changed

+273
-159
lines changed

bindings/LDK/Bindings.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6490,7 +6490,7 @@ withUnsafePointer(to: Bindings.array_to_tuple32(array: random_seed_bytes)) { (ra
64906490
*/
64916491

64926492
public class func get_ldk_swift_bindings_version() -> String {
6493-
return "8e00d9af56a2d5df10febaa37026399f925b414b"
6493+
return "fe0ea5b41ca6eb7ef88a4d2fbd7dc1f647c89112"
64946494
}
64956495

64966496
}

bindings/LDK/options/Bech32Error.swift

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,40 @@ public class Bech32Error: NativeTypeWrapper {
5757
}
5858

5959

60+
public func clone() -> Bech32Error {
61+
62+
return Bech32Error(pointer: withUnsafePointer(to: self.cOpaqueStruct!) { (origPointer: UnsafePointer<LDKBech32Error>) in
63+
Bech32Error_clone(origPointer)
64+
});
65+
}
66+
67+
internal func danglingClone() -> Bech32Error {
68+
let dangledClone = self.clone()
69+
dangledClone.dangling = true
70+
return dangledClone
71+
}
72+
73+
74+
internal func free() -> Void {
75+
76+
return Bech32Error_free(self.cOpaqueStruct!);
77+
}
78+
79+
internal func dangle() -> Bech32Error {
80+
self.dangling = true
81+
return self
82+
}
83+
84+
deinit {
85+
if !self.dangling {
86+
Bindings.print("Freeing Bech32Error \(self.instanceNumber).")
87+
self.free()
88+
} else {
89+
Bindings.print("Not freeing Bech32Error \(self.instanceNumber) due to dangle.")
90+
}
91+
}
92+
93+
6094
/* OPTION_METHODS_END */
6195

6296
/* TYPE_CLASSES */

bindings/LDK/options/ParseError.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ ParseError_clone(origPointer)
120120

121121
public class func bech32_error(a: Bech32Error) -> ParseError {
122122

123-
return ParseError(pointer: ParseError_bech32_error(a.cOpaqueStruct!));
123+
return ParseError(pointer: ParseError_bech32_error(a.danglingClone().cOpaqueStruct!));
124124
}
125125

126126
public class func malformed_signature(a: LDKSecp256k1Error) -> ParseError {

bindings/batteries/ChannelManagerConstructor.swift

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import Foundation
1010
enum InvalidSerializedDataError: Error {
1111
case invalidSerializedChannelMonitor
1212
case invalidSerializedChannelManager
13+
case invalidSerializedNetworkGraph
1314
case duplicateSerializedChannelMonitor
1415
case badNodeSecret
1516
}
@@ -49,7 +50,7 @@ public class ChannelManagerConstructor: NativeTypeWrapper {
4950
private let chain_monitor: ChainMonitor
5051

5152

52-
public init(channel_manager_serialized: [UInt8], channel_monitors_serialized: [[UInt8]], keys_interface: KeysInterface, fee_estimator: FeeEstimator, chain_monitor: ChainMonitor, filter: Filter?, net_graph: NetworkGraph?, tx_broadcaster: BroadcasterInterface, logger: Logger) throws {
53+
public init(channel_manager_serialized: [UInt8], channel_monitors_serialized: [[UInt8]], keys_interface: KeysInterface, fee_estimator: FeeEstimator, chain_monitor: ChainMonitor, filter: Filter?, net_graph_serialized: [UInt8]?, tx_broadcaster: BroadcasterInterface, logger: Logger) throws {
5354

5455
var monitors: [LDKChannelMonitor] = []
5556
self.channel_monitors = []
@@ -104,7 +105,14 @@ public class ChannelManagerConstructor: NativeTypeWrapper {
104105

105106
let random_data = keys_interface.get_secure_random_bytes();
106107

107-
self.net_graph = net_graph
108+
if let serializedNetworkGraph = net_graph_serialized {
109+
let netGraphResult = NetworkGraph.read(ser: serializedNetworkGraph)
110+
if !netGraphResult.isOk(){
111+
throw InvalidSerializedDataError.invalidSerializedNetworkGraph
112+
}
113+
self.net_graph = netGraphResult.getValue()
114+
}
115+
108116
let noCustomMessages = IgnoringMessageHandler()
109117
var messageHandler: MessageHandler!
110118
if let netGraph = net_graph {
@@ -263,6 +271,10 @@ fileprivate class CustomChannelManagerPersister: Persister {
263271
override func persist_manager(channel_manager: ChannelManager) -> Result_NoneErrorZ {
264272
return self.handler.persist_manager(channel_manager: channel_manager)
265273
}
274+
275+
override func persist_graph(network_graph: NetworkGraph) -> Result_NoneErrorZ {
276+
return self.handler.persist_graph(network_graph: network_graph)
277+
}
266278
}
267279

268280
fileprivate class CustomEventHandler: EventHandler {
@@ -278,7 +290,6 @@ fileprivate class CustomEventHandler: EventHandler {
278290
self.handler.handle_event(event: event)
279291
}
280292

281-
282293
}
283294

284295
public protocol ExtendedChannelManagerPersister: Persister {

xcode/DirectBindingsApp/DirectBindingsAppTests/DirectBindingsAppTests.swift

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,15 @@ class DirectBindingsAppTests: XCTestCase {
6868
var monitors: [LDKChannelMonitor] = []
6969

7070
let graph = NetworkGraph(genesis_hash: [UInt8](repeating: 0, count: 32))
71+
let graphSerialization = graph.write()
7172
let channel_manager_constructor = try ChannelManagerConstructor(
7273
channel_manager_serialized: serialized_channel_manager,
7374
channel_monitors_serialized: serializedChannelMonitors,
7475
keys_interface: keysInterface,
7576
fee_estimator: feeEstimator,
7677
chain_monitor: chainMonitor,
7778
filter: filter,
78-
net_graph: graph,
79+
net_graph_serialized: graphSerialization,
7980
tx_broadcaster: broadcaster,
8081
logger: logger
8182
)
@@ -101,15 +102,47 @@ class DirectBindingsAppTests: XCTestCase {
101102
assert(parsedInvoice.isOk())
102103
let parsedInvoiceValue = parsedInvoice.getValue()!
103104

105+
/*
104106
let amtSat: NSNumber = 2
105107
let sendRes = payer.pay_invoice(invoice: parsedInvoiceValue)
106108
if amtSat != 0 {
107109
let sendRes = payer.pay_zero_value_invoice(invoice: parsedInvoiceValue, amount_msats: UInt64(truncating: amtSat) * 1000)
110+
if let sendError = sendRes.getError(){
111+
print("pay_zero_value_invoice error type: \(sendError.getValueType())")
112+
if let sendRoutingError = sendError.getValueAsRouting() {
113+
print("pay_zero_value_invoice routing error: \(sendRoutingError.get_err())")
114+
} else if let sendInvoiceError = sendError.getValueAsInvoice() {
115+
print("pay_zero_value_invoice invoice error: \(sendInvoiceError)")
116+
} else if let sendSendingError = sendError.getValueAsSending() {
117+
print("pay_zero_value_invoice sending error type: \(sendSendingError.getValueType())")
118+
if let sendingParameterError = sendSendingError.getValueAsParameterError() {
119+
print("pay_zero_value_invoice sending parameter error type: \(sendingParameterError.getValueType())")
120+
if let parameterRouteError = sendingParameterError.getValueAsRouteError() {
121+
print("pay_zero_value_invoice sending parameter route error: \(parameterRouteError.getErr())")
122+
} else if let parameterChannelUnavailableError = sendingParameterError.getValueAsChannelUnavailable() {
123+
print("pay_zero_value_invoice sending parameter channel unavailable error: \(parameterChannelUnavailableError.getErr())")
124+
} else if let parameterAPIMisuseError = sendingParameterError.getValueAsAPIMisuseError() {
125+
print("pay_zero_value_invoice sending parameter API misuse error: \(parameterAPIMisuseError.getErr())")
126+
} else if let parameterFeeRateTooHighError = sendingParameterError.getValueAsFeeRateTooHigh() {
127+
print("pay_zero_value_invoice sending parameter excessive fee rate error: \(parameterFeeRateTooHighError.getErr())")
128+
} else if let parameterIncompatibleShutdownScriptError = sendingParameterError.getValueAsIncompatibleShutdownScript() {
129+
print("pay_zero_value_invoice sending parameter incompatible shutdown script error: \(parameterIncompatibleShutdownScriptError.getScript().write())")
130+
}
131+
} else if let sendingPartialFailureError = sendSendingError.getValueAsPartialFailure() {
132+
print("pay_zero_value_invoice sending parameter error payment id: \(sendingPartialFailureError.getPayment_id())")
133+
} else if let sendingPathParameterError = sendSendingError.getValueAsPathParameterError() {
134+
print("pay_zero_value_invoice sending path parameter errors: \(sendingPathParameterError.count)")
135+
} else if let sendingAllFailedError = sendSendingError.getValueAsAllFailedRetrySafe() {
136+
print("pay_zero_value_invoice sending all failed retry safe errors: \(sendingAllFailedError.count)")
137+
}
138+
}
139+
}
108140
assert(sendRes.isOk())
109141
} else {
110142
let sendRes = payer.pay_invoice(invoice: parsedInvoiceValue)
111143
assert(sendRes.isOk())
112144
}
145+
*/
113146

114147
channel_manager_constructor.interrupt()
115148

xcode/DirectBindingsApp/DirectBindingsAppTests/test-batteries/TestChannelManagerPersister.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ class TestChannelManagerPersister : Persister, ExtendedChannelManagerPersister {
2424
return Result_NoneErrorZ.ok()
2525
}
2626

27+
override func persist_graph(network_graph: NetworkGraph) -> Result_NoneErrorZ {
28+
return Result_NoneErrorZ.ok()
29+
}
30+
2731
deinit {
2832
print("deiniting TestChannelmanagerPersister")
2933
}

xcode/DirectBindingsApp/ldk_ver.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ static inline int _ldk_strncmp(const char *s1, const char *s2, uint64_t n) {
99
}
1010

1111
#define _LDK_HEADER_VER "v0.0.106-10-ga86d8b78fedd014a"
12-
#define _LDK_C_BINDINGS_HEADER_VER "v0.0.106.0"
12+
#define _LDK_C_BINDINGS_HEADER_VER "v0.0.106.1"
1313
static inline const char* check_get_ldk_version() {
1414
LDKStr bin_ver = _ldk_get_compiled_version();
1515
if (_ldk_strncmp(_LDK_HEADER_VER, (const char*)bin_ver.chars, bin_ver.len) != 0) {

xcode/DirectBindingsApp/lightning.h

Lines changed: 60 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,56 @@ typedef struct LDKStr {
459459
bool chars_is_owned;
460460
} LDKStr;
461461

462+
/**
463+
* Represents an error returned from the bech32 library during validation of some bech32 data
464+
*/
465+
typedef enum LDKBech32Error_Tag {
466+
/**
467+
* String does not contain the separator character
468+
*/
469+
LDKBech32Error_MissingSeparator,
470+
/**
471+
* The checksum does not match the rest of the data
472+
*/
473+
LDKBech32Error_InvalidChecksum,
474+
/**
475+
* The data or human-readable part is too long or too short
476+
*/
477+
LDKBech32Error_InvalidLength,
478+
/**
479+
* Some part of the string contains an invalid character
480+
*/
481+
LDKBech32Error_InvalidChar,
482+
/**
483+
* Some part of the data has an invalid value
484+
*/
485+
LDKBech32Error_InvalidData,
486+
/**
487+
* The bit conversion failed due to a padding issue
488+
*/
489+
LDKBech32Error_InvalidPadding,
490+
/**
491+
* The whole string must be of one case
492+
*/
493+
LDKBech32Error_MixedCase,
494+
/**
495+
* Must be last for serialization purposes
496+
*/
497+
LDKBech32Error_Sentinel,
498+
} LDKBech32Error_Tag;
499+
500+
typedef struct LDKBech32Error {
501+
LDKBech32Error_Tag tag;
502+
union {
503+
struct {
504+
uint32_t invalid_char;
505+
};
506+
struct {
507+
uint8_t invalid_data;
508+
};
509+
};
510+
} LDKBech32Error;
511+
462512
/**
463513
* A serialized transaction, in (pointer, length) form.
464514
*
@@ -7137,56 +7187,6 @@ typedef struct LDKCResult_PaymentIdPaymentErrorZ {
71377187
bool result_ok;
71387188
} LDKCResult_PaymentIdPaymentErrorZ;
71397189

7140-
/**
7141-
* Represents an error returned from the bech32 library during validation of some bech32 data
7142-
*/
7143-
typedef enum LDKBech32Error_Tag {
7144-
/**
7145-
* String does not contain the separator character
7146-
*/
7147-
LDKBech32Error_MissingSeparator,
7148-
/**
7149-
* The checksum does not match the rest of the data
7150-
*/
7151-
LDKBech32Error_InvalidChecksum,
7152-
/**
7153-
* The data or human-readable part is too long or too short
7154-
*/
7155-
LDKBech32Error_InvalidLength,
7156-
/**
7157-
* Some part of the string contains an invalid character
7158-
*/
7159-
LDKBech32Error_InvalidChar,
7160-
/**
7161-
* Some part of the data has an invalid value
7162-
*/
7163-
LDKBech32Error_InvalidData,
7164-
/**
7165-
* The bit conversion failed due to a padding issue
7166-
*/
7167-
LDKBech32Error_InvalidPadding,
7168-
/**
7169-
* The whole string must be of one case
7170-
*/
7171-
LDKBech32Error_MixedCase,
7172-
/**
7173-
* Must be last for serialization purposes
7174-
*/
7175-
LDKBech32Error_Sentinel,
7176-
} LDKBech32Error_Tag;
7177-
7178-
typedef struct LDKBech32Error {
7179-
LDKBech32Error_Tag tag;
7180-
union {
7181-
struct {
7182-
uint32_t invalid_char;
7183-
};
7184-
struct {
7185-
uint8_t invalid_data;
7186-
};
7187-
};
7188-
} LDKBech32Error;
7189-
71907190
/**
71917191
* Sub-errors which don't have specific information in them use this type.
71927192
*/
@@ -12479,6 +12479,16 @@ struct LDKStr _ldk_get_compiled_version(void);
1247912479

1248012480
struct LDKStr _ldk_c_bindings_get_compiled_version(void);
1248112481

12482+
/**
12483+
* Creates a new Bech32Error which has the same data as `orig`
12484+
*/
12485+
struct LDKBech32Error Bech32Error_clone(const struct LDKBech32Error *NONNULL_PTR orig);
12486+
12487+
/**
12488+
* Releases any memory held by the given `Bech32Error` (which is currently none)
12489+
*/
12490+
void Bech32Error_free(struct LDKBech32Error o);
12491+
1248212492
/**
1248312493
* Frees the data buffer, if data_is_owned is set and datalen > 0.
1248412494
*/

xcode/LDKFramework/ldk_ver.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ static inline int _ldk_strncmp(const char *s1, const char *s2, uint64_t n) {
99
}
1010

1111
#define _LDK_HEADER_VER "v0.0.106-10-ga86d8b78fedd014a"
12-
#define _LDK_C_BINDINGS_HEADER_VER "v0.0.106.0"
12+
#define _LDK_C_BINDINGS_HEADER_VER "v0.0.106.1"
1313
static inline const char* check_get_ldk_version() {
1414
LDKStr bin_ver = _ldk_get_compiled_version();
1515
if (_ldk_strncmp(_LDK_HEADER_VER, (const char*)bin_ver.chars, bin_ver.len) != 0) {

0 commit comments

Comments
 (0)