From 7e0420c715587d17a796246c0c12ace40af546c5 Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Tue, 23 Jan 2024 23:51:51 +0000 Subject: [PATCH] Add manual implementation of the new `WitnessVersion` struct --- csharp_strings.py | 22 ++++++++++++++++++++++ gen_type_mapping.py | 2 +- genbindings.py | 8 +++++++- java_strings.py | 28 ++++++++++++++++++++++++++++ typescript_strings.py | 22 ++++++++++++++++++++++ 5 files changed, 80 insertions(+), 2 deletions(-) diff --git a/csharp_strings.py b/csharp_strings.py index c1873f3d..b25549a9 100644 --- a/csharp_strings.py +++ b/csharp_strings.py @@ -150,6 +150,28 @@ public class CommonBase { } }""" + self.witness_program_defn = """public class WitnessProgram : CommonBase { + /** The witness program bytes themselves */ + public readonly byte[] program; + /** The witness version */ + public readonly WitnessVersion version; + + internal WitnessProgram(object _dummy, long ptr) : base(ptr) { + this.program = InternalUtils.decodeUint8Array(bindings.WitnessProgram_get_program(ptr)); + this.version = new WitnessVersion(bindings.WitnessProgram_get_version(ptr)); + } + static private long check_args(byte[] program, WitnessVersion version) { + if (program.Length < 2 || program.Length > 40) throw new ArgumentException(); + if (version.getVal() == 0 && program.Length != 20 && program.Length != 32) throw new ArgumentException(); + return InternalUtils.encodeUint8Array(program); + } + public WitnessProgram(byte[] program, WitnessVersion version) : + this(null, bindings.WitnessProgram_new(version.getVal(), check_args(program, version))) {} + + ~WitnessProgram() { + if (ptr != 0) { bindings.WitnessProgram_free(ptr); } + } +}""" self.c_file_pfx = """ // On OSX jlong (ie long long) is not equivalent to int64_t, so we override here diff --git a/gen_type_mapping.py b/gen_type_mapping.py index f68ce6de..ba77c087 100644 --- a/gen_type_mapping.py +++ b/gen_type_mapping.py @@ -517,7 +517,7 @@ class TypeMappingGenerator: 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 + ")") + ";", to_hu_conv_name = ty_info.var_name + "_conv", from_hu_conv = (ty_info.var_name + ".getVal()", "")) - assert ty_info.rust_obj == "LDKTxOut" or ty_info.rust_obj == "LDKTxIn" or ty_info.rust_obj == "LDKBigEndianScalar" + assert ty_info.rust_obj == "LDKWitnessProgram" or ty_info.rust_obj == "LDKTxOut" or ty_info.rust_obj == "LDKTxIn" or ty_info.rust_obj == "LDKBigEndianScalar" if not ty_info.is_ptr and not holds_ref: 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 = ", ";") ret_conv_name = "tag_ptr(" + ty_info.var_name + "_ref, true)" diff --git a/genbindings.py b/genbindings.py index 67de2821..f5e53aad 100755 --- a/genbindings.py +++ b/genbindings.py @@ -655,7 +655,8 @@ with open(sys.argv[1]) as in_h, open(f"{sys.argv[2]}/bindings{consts.file_ext}", expected_struct in result_types or expected_struct in tuple_types) and not is_free impl_on_utils = not impl_on_struct and (not is_free and not method_name.endswith("_clone") and not method_name.startswith("TxOut") and not method_name.startswith("TxIn") and - not method_name.startswith("BigEndianScalar") and not method_name.startswith("_") and + not method_name.startswith("BigEndianScalar") and not method_name.startswith("WitnessProgram") and + not method_name.startswith("_") and method_name != "check_platform" and method_name != "Result_read" and not expected_struct in unitary_enums and ((not method_name.startswith("C2Tuple_") and not method_name.startswith("C3Tuple_")) @@ -1159,6 +1160,11 @@ with open(sys.argv[1]) as in_h, open(f"{sys.argv[2]}/bindings{consts.file_ext}", fn_line = "static void BigEndianScalar_free (struct LDKBigEndianScalar thing)" write_c(fn_line + " {}\n") map_fn(fn_line + "\n", re.compile("static (.*) (BigEndianScalar_free) \((.*)\)").match(fn_line), None, None, None) + elif struct_name == "LDKWitnessProgram": + with open(f"{sys.argv[3]}/structs/WitnessProgram{consts.file_ext}", "w") as out_java_struct: + out_java_struct.write(consts.hu_struct_file_prefix) + out_java_struct.write(consts.witness_program_defn) + out_java_struct.write(consts.hu_struct_file_suffix) else: pass # Everything remaining is a byte[] of some form cur_block_obj = None diff --git a/java_strings.py b/java_strings.py index 33326cd4..fe3b4981 100644 --- a/java_strings.py +++ b/java_strings.py @@ -184,6 +184,34 @@ class CommonBase { } }""" + self.witness_program_defn = """public class WitnessProgram extends CommonBase { + /** The witness program bytes themselves */ + public final byte[] program; + /** The witness version */ + public final WitnessVersion version; + + WitnessProgram(java.lang.Object _dummy, long ptr) { + super(ptr); + this.program = bindings.WitnessProgram_get_program(ptr); + this.version = new WitnessVersion(bindings.WitnessProgram_get_version(ptr)); + } + static byte check_args(byte[] program, WitnessVersion version) { + if (program.length < 2 || program.length > 40) throw new IllegalArgumentException(); + if (version.getVal() == 0 && program.length != 20 && program.length != 32) throw new IllegalArgumentException(); + return version.getVal(); + } + public WitnessProgram(byte[] program, WitnessVersion version) { + super(bindings.WitnessProgram_new(check_args(program, version), program)); + this.program = bindings.WitnessProgram_get_program(ptr); + this.version = new WitnessVersion(bindings.WitnessProgram_get_version(ptr)); + } + + @Override @SuppressWarnings(\"deprecation\") + protected void finalize() throws Throwable { + super.finalize(); + if (ptr != 0) { bindings.WitnessProgram_free(ptr); } + } +}""" self.c_file_pfx = """#include // On OSX jlong (ie long long) is not equivalent to int64_t, so we override here diff --git a/typescript_strings.py b/typescript_strings.py index a7035633..afb2b67f 100644 --- a/typescript_strings.py +++ b/typescript_strings.py @@ -489,6 +489,28 @@ export class UnqualifiedError { }""" self.obj_defined(["BigEndianScalar"], "structs") + self.witness_program_defn = """export class WitnessProgram extends CommonBase { + /** The witness program bytes themselves */ + public program: Uint8Array; + /** The witness program version */ + public version: WitnessVersion; + + /* @internal */ + public constructor(_dummy: null, ptr: bigint) { + super(ptr, bindings.WitnessProgram_free); + this.program = bindings.decodeUint8Array(bindings.WitnessProgram_get_program(ptr)); + this.version = new WitnessVersion(bindings.WitnessProgram_get_version(ptr)); + } + public static constructor_new(program: Uint8Array, version: WitnessVersion): WitnessProgram { + if (program.length < 2 || program.length > 40) + throw new Error("WitnessProgram must be between 2 and 40 bytes long"); + if (version.getVal() == 0 && program.length != 20 && program.length != 32) + throw new Error("WitnessProgram for version 0 must be between either 20 or 30 bytes"); + return new WitnessProgram(null, bindings.WitnessProgram_new(version.getVal(), bindings.encodeUint8Array(program))); + } +}""" + self.obj_defined(["WitnessProgram"], "structs") + self.c_file_pfx = """#include "js-wasm.h" #include #include -- 2.30.2