From 4f95adad9cbb821f489d89f73d3e493933ba60fe Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Mon, 27 Jun 2022 18:28:59 +0000 Subject: [PATCH] Add handling for the new WitnessVersion type, similar to u5 but 4-bits --- gen_type_mapping.py | 4 ++-- genbindings.py | 13 ++++++---- java_strings.py | 4 +++- .../java/org/ldk/util/WitnessVersion.java | 21 ++++++++++++++++ typescript_strings.py | 24 +++++++++++++++++-- 5 files changed, 57 insertions(+), 9 deletions(-) create mode 100644 src/main/java/org/ldk/util/WitnessVersion.java diff --git a/gen_type_mapping.py b/gen_type_mapping.py index 0f2babf0..2ed6faa3 100644 --- a/gen_type_mapping.py +++ b/gen_type_mapping.py @@ -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()", "")) diff --git a/genbindings.py b/genbindings.py index 2d69168e..b9538114 100755 --- a/genbindings.py +++ b/genbindings.py @@ -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] diff --git a/java_strings.py b/java_strings.py index 6abfe427..9513db8e 100644 --- a/java_strings.py +++ b/java_strings.py @@ -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 index 00000000..47299f55 --- /dev/null +++ b/src/main/java/org/ldk/util/WitnessVersion.java @@ -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; + } +} diff --git a/typescript_strings.py b/typescript_strings.py index f181f56f..092a9e15 100644 --- a/typescript_strings.py +++ b/typescript_strings.py @@ -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): Uint8Array { return arr; } +/* @internal */ +export function WitnessVersionArrToBytes(inputArray: Array): 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' """ -- 2.39.5