Skip to content

Commit c4f2c6e

Browse files
committed
add byte array type helpers and a config file
1 parent 8690cb6 commit c4f2c6e

15 files changed

+267
-68
lines changed

bindings/LDK/Bindings.swift

+90-6
Original file line numberDiff line numberDiff line change
@@ -9,41 +9,125 @@ import Foundation
99

1010
class Bindings{
1111

12+
/* BYTE_ARRAY_METHODS_START */
13+
14+
static func new_LDKSecretKey(array: [UInt8]) -> LDKSecretKey {
15+
let byteType = LDKSecretKey(bytes: (array[0], array[1], array[2], array[3], array[4], array[5], array[6], array[7], array[8], array[9], array[10], array[11], array[12], array[13], array[14], array[15], array[16], array[17], array[18], array[19], array[20], array[21], array[22], array[23], array[24], array[25], array[26], array[27], array[28], array[29], array[30], array[31]))
16+
return byteType
17+
}
18+
19+
static func LDKSecretKey_to_array(byteType: LDKSecretKey) -> [UInt8] {
20+
let array = [byteType.bytes.0, byteType.bytes.1, byteType.bytes.2, byteType.bytes.3, byteType.bytes.4, byteType.bytes.5, byteType.bytes.6, byteType.bytes.7, byteType.bytes.8, byteType.bytes.9, byteType.bytes.10, byteType.bytes.11, byteType.bytes.12, byteType.bytes.13, byteType.bytes.14, byteType.bytes.15, byteType.bytes.16, byteType.bytes.17, byteType.bytes.18, byteType.bytes.19, byteType.bytes.20, byteType.bytes.21, byteType.bytes.22, byteType.bytes.23, byteType.bytes.24, byteType.bytes.25, byteType.bytes.26, byteType.bytes.27, byteType.bytes.28, byteType.bytes.29, byteType.bytes.30, byteType.bytes.31]
21+
return array
22+
}
23+
24+
static func new_LDKThirtyTwoBytes(array: [UInt8]) -> LDKThirtyTwoBytes {
25+
let byteType = LDKThirtyTwoBytes(data: (array[0], array[1], array[2], array[3], array[4], array[5], array[6], array[7], array[8], array[9], array[10], array[11], array[12], array[13], array[14], array[15], array[16], array[17], array[18], array[19], array[20], array[21], array[22], array[23], array[24], array[25], array[26], array[27], array[28], array[29], array[30], array[31]))
26+
return byteType
27+
}
28+
29+
static func LDKThirtyTwoBytes_to_array(byteType: LDKThirtyTwoBytes) -> [UInt8] {
30+
let array = [byteType.data.0, byteType.data.1, byteType.data.2, byteType.data.3, byteType.data.4, byteType.data.5, byteType.data.6, byteType.data.7, byteType.data.8, byteType.data.9, byteType.data.10, byteType.data.11, byteType.data.12, byteType.data.13, byteType.data.14, byteType.data.15, byteType.data.16, byteType.data.17, byteType.data.18, byteType.data.19, byteType.data.20, byteType.data.21, byteType.data.22, byteType.data.23, byteType.data.24, byteType.data.25, byteType.data.26, byteType.data.27, byteType.data.28, byteType.data.29, byteType.data.30, byteType.data.31]
31+
return array
32+
}
33+
34+
static func new_LDKTenBytes(array: [UInt8]) -> LDKTenBytes {
35+
let byteType = LDKTenBytes(data: (array[0], array[1], array[2], array[3], array[4], array[5], array[6], array[7], array[8], array[9]))
36+
return byteType
37+
}
38+
39+
static func LDKTenBytes_to_array(byteType: LDKTenBytes) -> [UInt8] {
40+
let array = [byteType.data.0, byteType.data.1, byteType.data.2, byteType.data.3, byteType.data.4, byteType.data.5, byteType.data.6, byteType.data.7, byteType.data.8, byteType.data.9]
41+
return array
42+
}
43+
44+
static func new_LDKFourBytes(array: [UInt8]) -> LDKFourBytes {
45+
let byteType = LDKFourBytes(data: (array[0], array[1], array[2], array[3]))
46+
return byteType
47+
}
48+
49+
static func LDKFourBytes_to_array(byteType: LDKFourBytes) -> [UInt8] {
50+
let array = [byteType.data.0, byteType.data.1, byteType.data.2, byteType.data.3]
51+
return array
52+
}
53+
54+
static func new_LDKPublicKey(array: [UInt8]) -> LDKPublicKey {
55+
let byteType = LDKPublicKey(compressed_form: (array[0], array[1], array[2], array[3], array[4], array[5], array[6], array[7], array[8], array[9], array[10], array[11], array[12], array[13], array[14], array[15], array[16], array[17], array[18], array[19], array[20], array[21], array[22], array[23], array[24], array[25], array[26], array[27], array[28], array[29], array[30], array[31], array[32]))
56+
return byteType
57+
}
58+
59+
static func LDKPublicKey_to_array(byteType: LDKPublicKey) -> [UInt8] {
60+
let array = [byteType.compressed_form.0, byteType.compressed_form.1, byteType.compressed_form.2, byteType.compressed_form.3, byteType.compressed_form.4, byteType.compressed_form.5, byteType.compressed_form.6, byteType.compressed_form.7, byteType.compressed_form.8, byteType.compressed_form.9, byteType.compressed_form.10, byteType.compressed_form.11, byteType.compressed_form.12, byteType.compressed_form.13, byteType.compressed_form.14, byteType.compressed_form.15, byteType.compressed_form.16, byteType.compressed_form.17, byteType.compressed_form.18, byteType.compressed_form.19, byteType.compressed_form.20, byteType.compressed_form.21, byteType.compressed_form.22, byteType.compressed_form.23, byteType.compressed_form.24, byteType.compressed_form.25, byteType.compressed_form.26, byteType.compressed_form.27, byteType.compressed_form.28, byteType.compressed_form.29, byteType.compressed_form.30, byteType.compressed_form.31, byteType.compressed_form.32]
61+
return array
62+
}
63+
64+
static func new_LDKThreeBytes(array: [UInt8]) -> LDKThreeBytes {
65+
let byteType = LDKThreeBytes(data: (array[0], array[1], array[2]))
66+
return byteType
67+
}
68+
69+
static func LDKThreeBytes_to_array(byteType: LDKThreeBytes) -> [UInt8] {
70+
let array = [byteType.data.0, byteType.data.1, byteType.data.2]
71+
return array
72+
}
73+
74+
static func new_LDKSixteenBytes(array: [UInt8]) -> LDKSixteenBytes {
75+
let byteType = LDKSixteenBytes(data: (array[0], array[1], array[2], array[3], array[4], array[5], array[6], array[7], array[8], array[9], array[10], array[11], array[12], array[13], array[14], array[15]))
76+
return byteType
77+
}
78+
79+
static func LDKSixteenBytes_to_array(byteType: LDKSixteenBytes) -> [UInt8] {
80+
let array = [byteType.data.0, byteType.data.1, byteType.data.2, byteType.data.3, byteType.data.4, byteType.data.5, byteType.data.6, byteType.data.7, byteType.data.8, byteType.data.9, byteType.data.10, byteType.data.11, byteType.data.12, byteType.data.13, byteType.data.14, byteType.data.15]
81+
return array
82+
}
83+
84+
static func new_LDKSignature(array: [UInt8]) -> LDKSignature {
85+
let byteType = LDKSignature(compact_form: (array[0], array[1], array[2], array[3], array[4], array[5], array[6], array[7], array[8], array[9], array[10], array[11], array[12], array[13], array[14], array[15], array[16], array[17], array[18], array[19], array[20], array[21], array[22], array[23], array[24], array[25], array[26], array[27], array[28], array[29], array[30], array[31], array[32], array[33], array[34], array[35], array[36], array[37], array[38], array[39], array[40], array[41], array[42], array[43], array[44], array[45], array[46], array[47], array[48], array[49], array[50], array[51], array[52], array[53], array[54], array[55], array[56], array[57], array[58], array[59], array[60], array[61], array[62], array[63]))
86+
return byteType
87+
}
88+
89+
static func LDKSignature_to_array(byteType: LDKSignature) -> [UInt8] {
90+
let array = [byteType.compact_form.0, byteType.compact_form.1, byteType.compact_form.2, byteType.compact_form.3, byteType.compact_form.4, byteType.compact_form.5, byteType.compact_form.6, byteType.compact_form.7, byteType.compact_form.8, byteType.compact_form.9, byteType.compact_form.10, byteType.compact_form.11, byteType.compact_form.12, byteType.compact_form.13, byteType.compact_form.14, byteType.compact_form.15, byteType.compact_form.16, byteType.compact_form.17, byteType.compact_form.18, byteType.compact_form.19, byteType.compact_form.20, byteType.compact_form.21, byteType.compact_form.22, byteType.compact_form.23, byteType.compact_form.24, byteType.compact_form.25, byteType.compact_form.26, byteType.compact_form.27, byteType.compact_form.28, byteType.compact_form.29, byteType.compact_form.30, byteType.compact_form.31, byteType.compact_form.32, byteType.compact_form.33, byteType.compact_form.34, byteType.compact_form.35, byteType.compact_form.36, byteType.compact_form.37, byteType.compact_form.38, byteType.compact_form.39, byteType.compact_form.40, byteType.compact_form.41, byteType.compact_form.42, byteType.compact_form.43, byteType.compact_form.44, byteType.compact_form.45, byteType.compact_form.46, byteType.compact_form.47, byteType.compact_form.48, byteType.compact_form.49, byteType.compact_form.50, byteType.compact_form.51, byteType.compact_form.52, byteType.compact_form.53, byteType.compact_form.54, byteType.compact_form.55, byteType.compact_form.56, byteType.compact_form.57, byteType.compact_form.58, byteType.compact_form.59, byteType.compact_form.60, byteType.compact_form.61, byteType.compact_form.62, byteType.compact_form.63]
91+
return array
92+
}
93+
94+
/* BYTE_ARRAY_METHODS_END */
95+
1296
/* VECTOR_METHODS_START */
1397

14-
static func new_LDKCVec_CVec_u8ZZ(array: [UInt8]) -> LDKCVec_CVec_u8ZZ {
98+
static func new_LDKCVec_u8Z(array: [UInt8]) -> LDKCVec_u8Z {
1599

16100
let dataContainer = array.withUnsafeBufferPointer { (pointer: UnsafeBufferPointer<UInt8>) -> UnsafeMutablePointer<UInt8> in
17101
let mutablePointer = UnsafeMutablePointer<UInt8>(mutating: pointer.baseAddress!)
18102
return mutablePointer
19103
}
20104

21-
let vector = LDKCVec_CVec_u8ZZ(data: dataContainer, datalen: UInt(array.count))
105+
let vector = LDKCVec_u8Z(data: dataContainer, datalen: UInt(array.count))
22106
return vector
23107

24108
}
25109

26-
static func LDKCVec_CVec_u8ZZ_to_array(vector: LDKCVec_CVec_u8ZZ) -> [UInt8] {
110+
static func LDKCVec_u8Z_to_array(vector: LDKCVec_u8Z) -> [UInt8] {
27111
var array = [UInt8]()
28112
for index in 0..<Int(vector.datalen) {
29113
array.append(vector.data[index])
30114
}
31115
return array
32116
}
33117

34-
static func new_LDKCVec_u8Z(array: [UInt8]) -> LDKCVec_u8Z {
118+
static func new_LDKCVec_CVec_u8ZZ(array: [UInt8]) -> LDKCVec_CVec_u8ZZ {
35119

36120
let dataContainer = array.withUnsafeBufferPointer { (pointer: UnsafeBufferPointer<UInt8>) -> UnsafeMutablePointer<UInt8> in
37121
let mutablePointer = UnsafeMutablePointer<UInt8>(mutating: pointer.baseAddress!)
38122
return mutablePointer
39123
}
40124

41-
let vector = LDKCVec_u8Z(data: dataContainer, datalen: UInt(array.count))
125+
let vector = LDKCVec_CVec_u8ZZ(data: dataContainer, datalen: UInt(array.count))
42126
return vector
43127

44128
}
45129

46-
static func LDKCVec_u8Z_to_array(vector: LDKCVec_u8Z) -> [UInt8] {
130+
static func LDKCVec_CVec_u8ZZ_to_array(vector: LDKCVec_CVec_u8ZZ) -> [UInt8] {
47131
var array = [UInt8]()
48132
for index in 0..<Int(vector.datalen) {
49133
array.append(vector.data[index])

src/config.py

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import os
2+
3+
4+
class Config:
5+
# Path to the C header file to parse, typically lightning.h
6+
HEADER_FILE_PATH = f'{os.path.dirname(__file__)}/../input/lightning.h'
7+
# HEADER_FILE_PATH = f'{os.path.dirname(__file__)}/../input/minimal_opaque_struct.h'
8+
# HEADER_FILE_PATH = f'{os.path.dirname(__file__)}/../input/minimal_tuple.h'
9+
# HEADER_FILE_PATH = f'{os.path.dirname(__file__)}/../input/minimal_ping.h'
10+
11+
OUTPUT_DIRECTORY_PATH = f'{os.path.dirname(__file__)}/../bindings/LDK'

src/generators/opaque_struct_generator.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import re
22
import os
33

4+
from config import Config
5+
46

57
class OpaqueStructGenerator:
68

@@ -174,7 +176,7 @@ def generate_opaque_struct(self, struct_name, struct_details):
174176

175177

176178
# store the output
177-
output_path = f'{os.path.dirname(__file__)}/../../bindings/LDK/structs/{swift_struct_name}.swift'
179+
output_path = f'{Config.OUTPUT_DIRECTORY_PATH}/structs/{swift_struct_name}.swift'
178180
output_directory = os.path.dirname(output_path)
179181
if not os.path.exists(output_directory):
180182
os.makedirs(output_directory)

src/generators/trait_generator.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import re
22
import os
33

4+
from config import Config
5+
46

57
class TraitGenerator:
68

@@ -77,7 +79,7 @@ def generate_trait(self, struct_name, struct_details):
7779
opaque_struct_file = method_template_regex.sub(f'\g<1>{struct_methods}\g<3>', opaque_struct_file)
7880

7981
# store the output
80-
output_path = f'{os.path.dirname(__file__)}/../../bindings/LDK/traits/{swift_struct_name}.swift'
82+
output_path = f'{Config.OUTPUT_DIRECTORY_PATH}/traits/{swift_struct_name}.swift'
8183
output_directory = os.path.dirname(output_path)
8284
if not os.path.exists(output_directory):
8385
os.makedirs(output_directory)

src/generators/tuple_generator.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import re
22
import os
33

4+
from config import Config
45

56
class TupleGenerator:
67

@@ -174,7 +175,7 @@ def generate_tuple(self, tuple_name, tuple_details):
174175

175176

176177
# store the output
177-
output_path = f'{os.path.dirname(__file__)}/../../bindings/LDK/tuples/{swift_tuple_name}.swift'
178+
output_path = f'{Config.OUTPUT_DIRECTORY_PATH}/tuples/{swift_tuple_name}.swift'
178179
output_directory = os.path.dirname(output_path)
179180
if not os.path.exists(output_directory):
180181
os.makedirs(output_directory)

src/generators/util_generator.py

-2
This file was deleted.
+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import os
2+
3+
from config import Config
4+
5+
6+
class UtilGenerator:
7+
bindings_templates = ''
8+
mutating_filled_template_set = ''
9+
10+
def __init__(self):
11+
self.template_regex = None
12+
self.template = ''
13+
self.filled_template = ''
14+
15+
def loadTemplate(self):
16+
self.template = self.template_regex.search(UtilGenerator.bindings_templates).group(2)
17+
18+
def finalize(self):
19+
UtilGenerator.mutating_filled_template_set = self.template_regex.sub(f'\g<1>{self.filled_template}\g<3>',
20+
UtilGenerator.mutating_filled_template_set)
21+
self.persist()
22+
23+
def persist(self):
24+
# store the output
25+
output_path = f'{Config.OUTPUT_DIRECTORY_PATH}/Bindings.swift'
26+
output_directory = os.path.dirname(output_path)
27+
if not os.path.exists(output_directory):
28+
os.makedirs(output_directory)
29+
with open(output_path, "w") as f:
30+
f.write(UtilGenerator.mutating_filled_template_set)
31+
32+
33+
template_path = f'{os.path.dirname(__file__)}/../../../templates/BindingsTemplate.swift'
34+
with open(template_path, 'r') as template_handle:
35+
UtilGenerator.bindings_templates = template_handle.read()
36+
UtilGenerator.mutating_filled_template_set = UtilGenerator.bindings_templates
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import re
2+
3+
from generators.util_generators import UtilGenerator
4+
5+
6+
class ByteArrayGenerator(UtilGenerator):
7+
8+
def __init__(self) -> None:
9+
super().__init__()
10+
self.template_regex = re.compile(
11+
"(\/\* BYTE_ARRAY_METHODS_START \*\/\n)(.*)(\n[\t ]*\/\* BYTE_ARRAY_METHODS_END \*\/)",
12+
flags=re.MULTILINE | re.DOTALL)
13+
self.loadTemplate()
14+
15+
def generate_byte_array(self, byte_array_type_name, byte_array_type_details):
16+
assert len(byte_array_type_details.fields) == 1
17+
byte_array_field = byte_array_type_details.fields[0]
18+
array_length = byte_array_field.arr_len
19+
mutating_current_byte_array_methods = self.template
20+
mutating_current_byte_array_methods = mutating_current_byte_array_methods.replace('LDKByteType',
21+
byte_array_type_name)
22+
mutating_current_byte_array_methods = mutating_current_byte_array_methods.replace('fieldName:',
23+
f'{byte_array_field.var_name}:')
24+
tupleArguments = 'array[0]'
25+
tupleReads = f'byteType.{byte_array_field.var_name}.0'
26+
for i in range(1, array_length):
27+
tupleArguments += f', array[{i}]'
28+
tupleReads += f', byteType.{byte_array_field.var_name}.{i}'
29+
mutating_current_byte_array_methods = mutating_current_byte_array_methods.replace('tupleArguments',
30+
tupleArguments)
31+
mutating_current_byte_array_methods = mutating_current_byte_array_methods.replace('tupleReads',
32+
tupleReads)
33+
self.filled_template += "\n" + mutating_current_byte_array_methods + "\n"
34+
35+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import re
2+
3+
from generators.util_generators import UtilGenerator
4+
5+
class VectorGenerator(UtilGenerator):
6+
7+
def __init__(self) -> None:
8+
super().__init__()
9+
self.template_regex = re.compile(
10+
"(\/\* VECTOR_METHODS_START \*\/\n)(.*)(\n[\t ]*\/\* VECTOR_METHODS_END \*\/)",
11+
flags=re.MULTILINE | re.DOTALL)
12+
self.loadTemplate()
13+
14+
def generate_vector(self, vector_name, vector_type_details):
15+
if not vector_type_details.is_primitive:
16+
# TODO: add non-primitive tuple support
17+
return
18+
mutating_current_vector_methods = self.template
19+
mutating_current_vector_methods = mutating_current_vector_methods.replace('LDKCVec_rust_primitive', vector_name)
20+
mutating_current_vector_methods = mutating_current_vector_methods.replace('SwiftPrimitive',
21+
vector_type_details.primitive_swift_counterpart)
22+
self.filled_template += "\n"+mutating_current_vector_methods+"\n"

src/generators/vector_generator.py

-38
This file was deleted.

0 commit comments

Comments
 (0)