1 package org.ldk.structs;
3 import org.ldk.impl.bindings;
4 import org.ldk.enums.*;
6 import java.util.Arrays;
7 import java.lang.ref.Reference;
8 import javax.annotation.Nullable;
11 * A trait that describes a source of entropy.
13 @SuppressWarnings("unchecked") // We correctly assign various generic arrays
14 public class EntropySource extends CommonBase {
15 final bindings.LDKEntropySource bindings_instance;
16 EntropySource(Object _dummy, long ptr) { super(ptr); bindings_instance = null; }
17 private EntropySource(bindings.LDKEntropySource arg) {
18 super(bindings.LDKEntropySource_new(arg));
19 this.ptrs_to.add(arg);
20 this.bindings_instance = arg;
22 @Override @SuppressWarnings("deprecation")
23 protected void finalize() throws Throwable {
24 if (ptr != 0) { bindings.EntropySource_free(ptr); } super.finalize();
27 * Destroys the object, freeing associated resources. After this call, any access
28 * to this object may result in a SEGFAULT or worse.
30 * You should generally NEVER call this method. You should let the garbage collector
31 * do this for you when it finalizes objects. However, it may be useful for types
32 * which represent locks and should be closed immediately to avoid holding locks
35 public void destroy() {
36 if (ptr != 0) { bindings.EntropySource_free(ptr); }
39 public static interface EntropySourceInterface {
41 * Gets a unique, cryptographically-secure, random 32-byte value. This method must return a
42 * different value each time it is called.
44 byte[] get_secure_random_bytes();
46 private static class LDKEntropySourceHolder { EntropySource held; }
47 public static EntropySource new_impl(EntropySourceInterface arg) {
48 final LDKEntropySourceHolder impl_holder = new LDKEntropySourceHolder();
49 impl_holder.held = new EntropySource(new bindings.LDKEntropySource() {
50 @Override public byte[] get_secure_random_bytes() {
51 byte[] ret = arg.get_secure_random_bytes();
52 Reference.reachabilityFence(arg);
53 byte[] result = InternalUtils.check_arr_len(ret, 32);
57 return impl_holder.held;
60 * Gets a unique, cryptographically-secure, random 32-byte value. This method must return a
61 * different value each time it is called.
63 public byte[] get_secure_random_bytes() {
64 byte[] ret = bindings.EntropySource_get_secure_random_bytes(this.ptr);
65 Reference.reachabilityFence(this);