95f006552a5cde36797854c8e6dc7bb277cbbf2d
[ldk-java] / src / main / java / org / ldk / structs / EntropySource.java
1 package org.ldk.structs;
2
3 import org.ldk.impl.bindings;
4 import org.ldk.enums.*;
5 import org.ldk.util.*;
6 import java.util.Arrays;
7 import java.lang.ref.Reference;
8 import javax.annotation.Nullable;
9
10 /**
11  * A trait that describes a source of entropy.
12  */
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;
21         }
22         @Override @SuppressWarnings("deprecation")
23         protected void finalize() throws Throwable {
24                 if (ptr != 0) { bindings.EntropySource_free(ptr); } super.finalize();
25         }
26         /**
27          * Destroys the object, freeing associated resources. After this call, any access
28          * to this object may result in a SEGFAULT or worse.
29          *
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
33          * until the GC runs.
34          */
35         public void destroy() {
36                 if (ptr != 0) { bindings.EntropySource_free(ptr); }
37                 ptr = 0;
38         }
39         public static interface EntropySourceInterface {
40                 /**
41                  * Gets a unique, cryptographically-secure, random 32-byte value. This method must return a
42                  * different value each time it is called.
43                  */
44                 byte[] get_secure_random_bytes();
45         }
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);
54                                 return result;
55                         }
56                 });
57                 return impl_holder.held;
58         }
59         /**
60          * Gets a unique, cryptographically-secure, random 32-byte value. This method must return a
61          * different value each time it is called.
62          */
63         public byte[] get_secure_random_bytes() {
64                 byte[] ret = bindings.EntropySource_get_secure_random_bytes(this.ptr);
65                 Reference.reachabilityFence(this);
66                 return ret;
67         }
68
69 }