]> git.bitcoin.ninja Git - ldk-java/commitdiff
Add handling for the new WitnessVersion type, similar to u5 but 4-bits
authorMatt Corallo <git@bluematt.me>
Mon, 27 Jun 2022 18:28:59 +0000 (18:28 +0000)
committerMatt Corallo <git@bluematt.me>
Tue, 28 Jun 2022 22:45:50 +0000 (22:45 +0000)
gen_type_mapping.py
genbindings.py
java_strings.py
src/main/java/org/ldk/util/WitnessVersion.java [new file with mode: 0644]
typescript_strings.py

index 0f2babf063690f50611db2ae53ba6c0d87f7e047..2ed6faa375f9d70ebbdbd985495a66e7c9dbc1a6 100644 (file)
@@ -504,10 +504,10 @@ 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 = ("0", ""))
 
-                if ty_info.rust_obj == "LDKu5":
+                if ty_info.rust_obj == "LDKu5" or ty_info.rust_obj == "LDKWitnessVersion":
                     assert from_hu_conv is None
                     return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name,
-                        arg_conv = "", arg_conv_name = "(LDKu5){ ._0 = " + ty_info.var_name + " }", arg_conv_cleanup = None,
+                        arg_conv = "", arg_conv_name = "(" + ty_info.rust_obj + "){ ._0 = " + ty_info.var_name + " }", arg_conv_cleanup = None,
                         ret_conv = ("uint8_t " + ty_info.var_name + "_val = ", "._0;"), ret_conv_name = ty_info.var_name + "_val",
                         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()", ""))
index 2d69168ef89fef95411b4a608a9120c7e2cb323e..b9538114ed9b3b3bbfbbde870620fb6078e5feb9 100755 (executable)
@@ -263,14 +263,19 @@ def java_c_types(fn_arg, ret_arr_len):
         arr_ty = "uint8_t"
         fn_arg = fn_arg[7:].strip()
         is_primitive = True
-    elif fn_arg.startswith("LDKu5"):
+    elif fn_arg.startswith("LDKu5") or fn_arg.startswith("LDKWitnessVersion"):
         java_ty = consts.c_type_map['uint8_t'][0]
-        java_hu_ty = "UInt5"
-        rust_obj = "LDKu5"
+        if fn_arg.startswith("LDKu5"):
+            java_hu_ty = "UInt5"
+            rust_obj = "LDKu5"
+            fn_arg = fn_arg[6:].strip()
+        else:
+            java_hu_ty = "WitnessVersion"
+            rust_obj = "LDKWitnessVersion"
+            fn_arg = fn_arg[18:].strip()
         c_ty = "int8_t"
         arr_ty = "uint8_t"
         fn_ty_arg = "B"
-        fn_arg = fn_arg[6:].strip()
     elif fn_arg.startswith("uint16_t"):
         mapped_type = consts.c_type_map['uint16_t']
         java_ty = mapped_type[0]
index 6abfe42701edaea931a92e7593b4d948b48c34d9..9513db8ec8c9898c841206aca374a1ed8cabe4a4 100644 (file)
@@ -632,6 +632,8 @@ import javax.annotation.Nullable;
             return arr_name + " != null ? Arrays.stream(" + arr_name + ").map(" + conv_name + " -> " + elem_ty.from_hu_conv[0] + ").toArray() : null"
         elif elem_ty.java_hu_ty == "UInt5":
             return arr_name + " != null ? InternalUtils.convUInt5Array(" + arr_name + ") : null"
+        elif elem_ty.java_hu_ty == "WitnessVersion":
+            return arr_name + " != null ? InternalUtils.convWitnessVersionArray(" + arr_name + ") : null"
         else:
             return arr_name + " != null ? Arrays.stream(" + arr_name + ").map(" + conv_name + " -> " + elem_ty.from_hu_conv[0] + ").toArray(" + arr_ty.java_ty + "::new) : null"
 
@@ -704,7 +706,7 @@ import javax.annotation.Nullable;
     def fully_qualified_hu_ty_path(self, ty):
         if ty.java_fn_ty_arg.startswith("L") and ty.java_fn_ty_arg.endswith(";"):
             return ty.java_fn_ty_arg.strip("L;").replace("/", ".")
-        if ty.java_hu_ty == "UnqualifiedError" or ty.java_hu_ty == "UInt5":
+        if ty.java_hu_ty == "UnqualifiedError" or ty.java_hu_ty == "UInt5" or ty.java_hu_ty == "WitnessVersion":
             return "org.ldk.util." + ty.java_hu_ty
         if ty.rust_obj is not None and not "[]" in ty.java_hu_ty:
             return "org.ldk.structs." + ty.java_hu_ty
diff --git a/src/main/java/org/ldk/util/WitnessVersion.java b/src/main/java/org/ldk/util/WitnessVersion.java
new file mode 100644 (file)
index 0000000..47299f5
--- /dev/null
@@ -0,0 +1,21 @@
+package org.ldk.util;
+
+/**
+ * A 4-bit unsigned integer representing a Bitcoin SegWit version
+ */
+public class WitnessVersion {
+    byte val;
+    public WitnessVersion(byte val) {
+        if (val > 16 || val < 0) {
+            throw new IllegalArgumentException();
+        }
+        this.val = val;
+    }
+
+    /**
+     * @return the value represented
+     */
+    public byte getVal() {
+        return val;
+    }
+}
index f181f56f83ba9388f04add3ecb2e5750ea00906f..092a9e152631388dbd97c99f8d3ea064db7f989e 100644 (file)
@@ -36,7 +36,7 @@ class Consts:
 
         self.bindings_header = """
 import * as version from './version.mjs';
-import { UInt5 } from './structs/CommonBase.mjs';
+import { UInt5, WitnessVersion } from './structs/CommonBase.mjs';
 
 const imports: any = {};
 imports.env = {};
@@ -149,6 +149,17 @@ export function uint5ArrToBytes(inputArray: Array<UInt5>): Uint8Array {
        return arr;
 }
 
+/* @internal */
+export function WitnessVersionArrToBytes(inputArray: Array<WitnessVersion>): Uint8Array {
+       const arr = new Uint8Array(inputArray.length);
+       for (var i = 0; i < inputArray.length; i++) {
+               arr[i] = inputArray[i].getVal();
+       }
+       return arr;
+}
+
+
+
 /* @internal */
 export function encodeUint8Array (inputArray: Uint8Array): number {
        const cArrayPointer = wasm.TS_malloc(inputArray.length + 4);
@@ -327,6 +338,15 @@ export class UInt5 {
        }
 }
 
+export class WitnessVersion {
+       public constructor(private val: number) {
+               if (val > 16 || val < 0) throw new Error("WitnessVersion value is out of range");
+       }
+       public getVal(): number {
+               return this.val;
+       }
+}
+
 export class UnqualifiedError {
        public constructor(val: number) {}
 }
@@ -596,7 +616,7 @@ jstring __attribute__((export_name("TS_get_ldk_version"))) get_ldk_version() {
 }"""
 
         self.hu_struct_file_prefix = """
-import { CommonBase, UInt5, UnqualifiedError } from './CommonBase.mjs';
+import { CommonBase, UInt5, WitnessVersion, UnqualifiedError } from './CommonBase.mjs';
 import * as bindings from '../bindings.mjs'
 
 """