]> git.bitcoin.ninja Git - ldk-java/commitdiff
Add manual implementation of the new `WitnessVersion` struct
authorMatt Corallo <git@bluematt.me>
Tue, 23 Jan 2024 23:51:51 +0000 (23:51 +0000)
committerMatt Corallo <git@bluematt.me>
Mon, 29 Jan 2024 16:37:42 +0000 (16:37 +0000)
csharp_strings.py
gen_type_mapping.py
genbindings.py
java_strings.py
typescript_strings.py

index c1873f3d8aefdd1a1f584db301d80d725875de72..b25549a9aa80b5862f41f3649b879082e340ee1d 100644 (file)
@@ -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
index f68ce6de5e3bf47457e6ca7467256fdd446df295..ba77c0876cc44f99fd2a740e175b8a5b4b1bbc2e 100644 (file)
@@ -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)"
index 67de282141766d5422cc5911e3a2add7e3a6ddd1..f5e53aadd932240c77777c1c3b27f49f60c84eaf 100755 (executable)
@@ -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
index 33326cd4332c7273753b292591320436ecff8e23..fe3b498181ebf1d67ee054c3f16c47605ea3ebc5 100644 (file)
@@ -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 <jni.h>
 // On OSX jlong (ie long long) is not equivalent to int64_t, so we override here
index a7035633cdabd5dcda86a8fa638235e19d397630..afb2b67f69cc3e99db4a69de29b34521e8383f51 100644 (file)
@@ -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 <stdatomic.h>
 #include <lightning.h>