assert var_is_arr_regex.match(fn_arg[8:])
rust_obj = "LDKThirtyTwoBytes"
arr_access = "data"
+ elif fn_arg.startswith("LDKU128"):
+ if fn_arg == "LDKU128":
+ fn_arg = "LDKU128 arg"
+ if fn_arg.startswith("LDKU128*") or fn_arg.startswith("LDKU128 *"):
+ fn_arg = "uint8_t (" + fn_arg[8:] + ")[16]"
+ else:
+ fn_arg = "uint8_t (*" + fn_arg[8:] + ")[16]"
+ assert var_is_arr_regex.match(fn_arg[8:])
+ rust_obj = "LDKU128"
+ arr_access = "le_bytes"
elif fn_arg.startswith("LDKTxid"):
fn_arg = "uint8_t (*" + fn_arg[8:] + ")[32]"
assert var_is_arr_regex.match(fn_arg[8:])
else:
java_ty = java_ty + "[]"
java_hu_ty = java_ty
+ if rust_obj == "LDKU128":
+ java_hu_ty = consts.u128_native_ty
c_ty = c_ty + "Array"
subty = java_c_types(arr_ty, None)
self.file_ext = ".java"
self.ptr_c_ty = "int64_t"
self.ptr_native_ty = "long"
+ self.u128_native_ty = "UInt128"
self.usize_c_ty = "int64_t"
self.usize_native_ty = "long"
self.native_zero_ptr = "0"
def primitive_arr_from_hu(self, arr_ty, fixed_len, arr_name):
mapped_ty = arr_ty.subty
+ if arr_ty.rust_obj == "LDKU128":
+ return ("" + arr_name + ".getLEBytes()", "")
if fixed_len is not None:
return ("InternalUtils.check_arr_len(" + arr_name + ", " + fixed_len + ")", "")
return None
def primitive_arr_to_hu(self, arr_ty, fixed_len, arr_name, conv_name):
+ if arr_ty.rust_obj == "LDKU128":
+ return "org.ldk.util.UInt128 " + conv_name + " = new org.ldk.util.UInt128(" + arr_name + ");"
return None
def java_arr_ty_str(self, elem_ty_str):
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" or ty.java_hu_ty == "WitnessVersion":
+ if ty.java_hu_ty == "UnqualifiedError" or ty.java_hu_ty == "UInt128" or ty.java_hu_ty == "UInt5" or ty.java_hu_ty == "WitnessVersion":
return "org.ldk.util." + ty.java_hu_ty
if not ty.is_native_primitive and ty.rust_obj is not None and not "[]" in ty.java_hu_ty:
return "org.ldk.structs." + ty.java_hu_ty
--- /dev/null
+package org.ldk.util;
+
+import java.util.Arrays;
+
+/**
+ * A 5-bit unsigned integer
+ */
+public class UInt128 {
+ private byte[] le_bytes;
+
+ /**
+ * Constructs a 128-bit integer from its little-endian representation
+ */
+ public UInt128(byte[] le_bytes) {
+ if (le_bytes.length != 16) {
+ throw new IllegalArgumentException();
+ }
+ this.le_bytes = le_bytes;
+ }
+
+ /**
+ * Constructs a 128-bit integer from a long, ignoring the sign bit
+ */
+ public UInt128(long val) {
+ byte[] le_bytes = new byte[16];
+ for (int i = 0; i < 8; i++)
+ le_bytes[i] = (byte) ((val >> i*8) & 0xff);
+ this.le_bytes = le_bytes;
+ }
+
+ /**
+ * @return The value as 16 little endian bytes
+ */
+ public byte[] getLEBytes() { return le_bytes; }
+
+ @Override public boolean equals(Object o) {
+ if (o == null || !(o instanceof UInt128)) return false;
+ return Arrays.equals(le_bytes, ((UInt128) o).le_bytes);
+ }
+
+ @Override public int hashCode() { return Arrays.hashCode(le_bytes); }
+}
+/* @internal */
+export function encodeUint128 (inputVal: bigint): number {
+ if (inputVal >= 0x10000000000000000000000000000000n) throw "U128s cannot exceed 128 bits";
+ const cArrayPointer = wasm.TS_malloc(16 + 8);
+ const arrayLengthView = new BigUint64Array(wasm.memory.buffer, cArrayPointer, 1);
+ arrayLengthView[0] = BigInt(16);
+ const arrayMemoryView = new Uint8Array(wasm.memory.buffer, cArrayPointer + 8, 16);
+ for (var i = 0; i < 16; i++) arrayMemoryView[i] = Number((inputVal >> BigInt(i)*8n) & 0xffn);
+ return cArrayPointer;
+}
/* @internal */
export function encodeUint8Array (inputArray: Uint8Array|null): number {
if (inputArray == null) return 0;
return Number(len % (2n ** 32n));
}
/* @internal */
+export function decodeUint128 (arrayPointer: number, free = true): bigint {
+ const arraySize = getArrayLength(arrayPointer);
+ if (arraySize != 16) throw "Need 16 bytes for a uint128";
+ const actualArrayViewer = new Uint8Array(wasm.memory.buffer, arrayPointer + 8, arraySize);
+ var val = 0n;
+ for (var i = 0; i < 16; i++) {
+ val <<= 8n;
+ val |= BigInt(actualArrayViewer[i]!);
+ }
+ if (free) {
+ wasm.TS_free(arrayPointer);
+ }
+ return val;
+}
+/* @internal */
export function decodeUint8Array (arrayPointer: number, free = true): Uint8Array {
const arraySize = getArrayLength(arrayPointer);
const actualArrayViewer = new Uint8Array(wasm.memory.buffer, arrayPointer + 8, arraySize);
self.file_ext = ".mts"
self.ptr_c_ty = "uint64_t"
self.ptr_native_ty = "bigint"
+ self.u128_native_ty = "bigint"
self.usize_c_ty = "uint32_t"
self.usize_native_ty = "number"
self.native_zero_ptr = "0n"
def primitive_arr_from_hu(self, arr_ty, fixed_len, arr_name):
mapped_ty = arr_ty.subty
inner = arr_name
+ if arr_ty.rust_obj == "LDKU128":
+ return ("bindings.encodeUint128(" + inner + ")", "")
if fixed_len is not None:
assert mapped_ty.c_ty == "int8_t"
inner = "bindings.check_arr_len(" + arr_name + ", " + fixed_len + ")"
def primitive_arr_to_hu(self, arr_ty, fixed_len, arr_name, conv_name):
mapped_ty = arr_ty.subty
- if mapped_ty.c_ty == "uint8_t" or mapped_ty.c_ty == "int8_t":
+ if arr_ty.rust_obj == "LDKU128":
+ return "const " + conv_name + ": bigint = bindings.decodeUint128(" + arr_name + ");"
+ elif mapped_ty.c_ty == "uint8_t" or mapped_ty.c_ty == "int8_t":
return "const " + conv_name + ": Uint8Array = bindings.decodeUint8Array(" + arr_name + ");"
elif mapped_ty.c_ty == "uint64_t" or mapped_ty.c_ty == "int64_t":
return "const " + conv_name + ": bigint[] = bindings.decodeUint64Array(" + arr_name + ");"