Skip to content

Commit 7e0420c

Browse files
committed
Add manual implementation of the new WitnessVersion struct
1 parent 49be9f3 commit 7e0420c

5 files changed

+80
-2
lines changed

csharp_strings.py

+22
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,28 @@ def __init__(self, DEBUG: bool, target: Target, outdir: str, **kwargs):
150150
}
151151
}"""
152152

153+
self.witness_program_defn = """public class WitnessProgram : CommonBase {
154+
/** The witness program bytes themselves */
155+
public readonly byte[] program;
156+
/** The witness version */
157+
public readonly WitnessVersion version;
158+
159+
internal WitnessProgram(object _dummy, long ptr) : base(ptr) {
160+
this.program = InternalUtils.decodeUint8Array(bindings.WitnessProgram_get_program(ptr));
161+
this.version = new WitnessVersion(bindings.WitnessProgram_get_version(ptr));
162+
}
163+
static private long check_args(byte[] program, WitnessVersion version) {
164+
if (program.Length < 2 || program.Length > 40) throw new ArgumentException();
165+
if (version.getVal() == 0 && program.Length != 20 && program.Length != 32) throw new ArgumentException();
166+
return InternalUtils.encodeUint8Array(program);
167+
}
168+
public WitnessProgram(byte[] program, WitnessVersion version) :
169+
this(null, bindings.WitnessProgram_new(version.getVal(), check_args(program, version))) {}
170+
171+
~WitnessProgram() {
172+
if (ptr != 0) { bindings.WitnessProgram_free(ptr); }
173+
}
174+
}"""
153175

154176
self.c_file_pfx = """
155177
// On OSX jlong (ie long long) is not equivalent to int64_t, so we override here

gen_type_mapping.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -517,7 +517,7 @@ def _do_map_type_with_info(self, ty_info, print_void, ret_arr_len, is_free, hold
517517
to_hu_conv = self.consts.var_decl_statement(ty_info.java_hu_ty, ty_info.var_name + "_conv", "new " + ty_info.java_hu_ty + "(" + ty_info.var_name + ")") + ";",
518518
to_hu_conv_name = ty_info.var_name + "_conv", from_hu_conv = (ty_info.var_name + ".getVal()", ""))
519519

520-
assert ty_info.rust_obj == "LDKTxOut" or ty_info.rust_obj == "LDKTxIn" or ty_info.rust_obj == "LDKBigEndianScalar"
520+
assert ty_info.rust_obj == "LDKWitnessProgram" or ty_info.rust_obj == "LDKTxOut" or ty_info.rust_obj == "LDKTxIn" or ty_info.rust_obj == "LDKBigEndianScalar"
521521
if not ty_info.is_ptr and not holds_ref:
522522
ret_conv = (ty_info.rust_obj + "* " + ty_info.var_name + "_ref = MALLOC(sizeof(" + ty_info.rust_obj + "), \"" + ty_info.rust_obj + "\");\n*" + ty_info.var_name + "_ref = ", ";")
523523
ret_conv_name = "tag_ptr(" + ty_info.var_name + "_ref, true)"

genbindings.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -655,7 +655,8 @@ def map_fn_with_ref_option(line, re_match, ret_arr_len, c_call_string, doc_comme
655655
expected_struct in result_types or expected_struct in tuple_types) and not is_free
656656
impl_on_utils = not impl_on_struct and (not is_free and not method_name.endswith("_clone") and
657657
not method_name.startswith("TxOut") and not method_name.startswith("TxIn") and
658-
not method_name.startswith("BigEndianScalar") and not method_name.startswith("_") and
658+
not method_name.startswith("BigEndianScalar") and not method_name.startswith("WitnessProgram") and
659+
not method_name.startswith("_") and
659660
method_name != "check_platform" and method_name != "Result_read" and
660661
not expected_struct in unitary_enums and
661662
((not method_name.startswith("C2Tuple_") and not method_name.startswith("C3Tuple_"))
@@ -1159,6 +1160,11 @@ def map_tuple(struct_name, field_lines):
11591160
fn_line = "static void BigEndianScalar_free (struct LDKBigEndianScalar thing)"
11601161
write_c(fn_line + " {}\n")
11611162
map_fn(fn_line + "\n", re.compile("static (.*) (BigEndianScalar_free) \((.*)\)").match(fn_line), None, None, None)
1163+
elif struct_name == "LDKWitnessProgram":
1164+
with open(f"{sys.argv[3]}/structs/WitnessProgram{consts.file_ext}", "w") as out_java_struct:
1165+
out_java_struct.write(consts.hu_struct_file_prefix)
1166+
out_java_struct.write(consts.witness_program_defn)
1167+
out_java_struct.write(consts.hu_struct_file_suffix)
11621168
else:
11631169
pass # Everything remaining is a byte[] of some form
11641170
cur_block_obj = None

java_strings.py

+28
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,34 @@ class CommonBase {
184184
}
185185
}"""
186186

187+
self.witness_program_defn = """public class WitnessProgram extends CommonBase {
188+
/** The witness program bytes themselves */
189+
public final byte[] program;
190+
/** The witness version */
191+
public final WitnessVersion version;
192+
193+
WitnessProgram(java.lang.Object _dummy, long ptr) {
194+
super(ptr);
195+
this.program = bindings.WitnessProgram_get_program(ptr);
196+
this.version = new WitnessVersion(bindings.WitnessProgram_get_version(ptr));
197+
}
198+
static byte check_args(byte[] program, WitnessVersion version) {
199+
if (program.length < 2 || program.length > 40) throw new IllegalArgumentException();
200+
if (version.getVal() == 0 && program.length != 20 && program.length != 32) throw new IllegalArgumentException();
201+
return version.getVal();
202+
}
203+
public WitnessProgram(byte[] program, WitnessVersion version) {
204+
super(bindings.WitnessProgram_new(check_args(program, version), program));
205+
this.program = bindings.WitnessProgram_get_program(ptr);
206+
this.version = new WitnessVersion(bindings.WitnessProgram_get_version(ptr));
207+
}
208+
209+
@Override @SuppressWarnings(\"deprecation\")
210+
protected void finalize() throws Throwable {
211+
super.finalize();
212+
if (ptr != 0) { bindings.WitnessProgram_free(ptr); }
213+
}
214+
}"""
187215

188216
self.c_file_pfx = """#include <jni.h>
189217
// On OSX jlong (ie long long) is not equivalent to int64_t, so we override here

typescript_strings.py

+22
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,28 @@ def __init__(self, DEBUG: bool, target: Target, outdir: str, **kwargs):
489489
}"""
490490
self.obj_defined(["BigEndianScalar"], "structs")
491491

492+
self.witness_program_defn = """export class WitnessProgram extends CommonBase {
493+
/** The witness program bytes themselves */
494+
public program: Uint8Array;
495+
/** The witness program version */
496+
public version: WitnessVersion;
497+
498+
/* @internal */
499+
public constructor(_dummy: null, ptr: bigint) {
500+
super(ptr, bindings.WitnessProgram_free);
501+
this.program = bindings.decodeUint8Array(bindings.WitnessProgram_get_program(ptr));
502+
this.version = new WitnessVersion(bindings.WitnessProgram_get_version(ptr));
503+
}
504+
public static constructor_new(program: Uint8Array, version: WitnessVersion): WitnessProgram {
505+
if (program.length < 2 || program.length > 40)
506+
throw new Error("WitnessProgram must be between 2 and 40 bytes long");
507+
if (version.getVal() == 0 && program.length != 20 && program.length != 32)
508+
throw new Error("WitnessProgram for version 0 must be between either 20 or 30 bytes");
509+
return new WitnessProgram(null, bindings.WitnessProgram_new(version.getVal(), bindings.encodeUint8Array(program)));
510+
}
511+
}"""
512+
self.obj_defined(["WitnessProgram"], "structs")
513+
492514
self.c_file_pfx = """#include "js-wasm.h"
493515
#include <stdatomic.h>
494516
#include <lightning.h>

0 commit comments

Comments
 (0)