b18198869c36351686441741a4cb67c473ab29c7
[ldk-java] / src / main / java / org / ldk / structs / NodeId.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 /**
12  * Represents the compressed public key of a node
13  */
14 @SuppressWarnings("unchecked") // We correctly assign various generic arrays
15 public class NodeId extends CommonBase {
16         NodeId(Object _dummy, long ptr) { super(ptr); }
17         @Override @SuppressWarnings("deprecation")
18         protected void finalize() throws Throwable {
19                 super.finalize();
20                 if (ptr != 0) { bindings.NodeId_free(ptr); }
21         }
22
23         long clone_ptr() {
24                 long ret = bindings.NodeId_clone_ptr(this.ptr);
25                 Reference.reachabilityFence(this);
26                 return ret;
27         }
28
29         /**
30          * Creates a copy of the NodeId
31          */
32         public NodeId clone() {
33                 long ret = bindings.NodeId_clone(this.ptr);
34                 Reference.reachabilityFence(this);
35                 if (ret >= 0 && ret <= 4096) { return null; }
36                 org.ldk.structs.NodeId ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new org.ldk.structs.NodeId(null, ret); }
37                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(this); };
38                 return ret_hu_conv;
39         }
40
41         /**
42          * Create a new NodeId from a public key
43          */
44         public static NodeId from_pubkey(byte[] pubkey) {
45                 long ret = bindings.NodeId_from_pubkey(InternalUtils.check_arr_len(pubkey, 33));
46                 Reference.reachabilityFence(pubkey);
47                 if (ret >= 0 && ret <= 4096) { return null; }
48                 org.ldk.structs.NodeId ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new org.ldk.structs.NodeId(null, ret); }
49                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(ret_hu_conv); };
50                 return ret_hu_conv;
51         }
52
53         /**
54          * Get the public key slice from this NodeId
55          */
56         public byte[] as_slice() {
57                 byte[] ret = bindings.NodeId_as_slice(this.ptr);
58                 Reference.reachabilityFence(this);
59                 return ret;
60         }
61
62         /**
63          * Get the public key from this NodeId
64          */
65         public Result_PublicKeySecp256k1ErrorZ as_pubkey() {
66                 long ret = bindings.NodeId_as_pubkey(this.ptr);
67                 Reference.reachabilityFence(this);
68                 if (ret >= 0 && ret <= 4096) { return null; }
69                 Result_PublicKeySecp256k1ErrorZ ret_hu_conv = Result_PublicKeySecp256k1ErrorZ.constr_from_ptr(ret);
70                 return ret_hu_conv;
71         }
72
73         /**
74          * Generates a non-cryptographic 64-bit hash of the NodeId.
75          */
76         public long hash() {
77                 long ret = bindings.NodeId_hash(this.ptr);
78                 Reference.reachabilityFence(this);
79                 return ret;
80         }
81
82         @Override public int hashCode() {
83                 return (int)this.hash();
84         }
85         /**
86          * Serialize the NodeId object into a byte array which can be read by NodeId_read
87          */
88         public byte[] write() {
89                 byte[] ret = bindings.NodeId_write(this.ptr);
90                 Reference.reachabilityFence(this);
91                 return ret;
92         }
93
94         /**
95          * Read a NodeId from a byte array, created by NodeId_write
96          */
97         public static Result_NodeIdDecodeErrorZ read(byte[] ser) {
98                 long ret = bindings.NodeId_read(ser);
99                 Reference.reachabilityFence(ser);
100                 if (ret >= 0 && ret <= 4096) { return null; }
101                 Result_NodeIdDecodeErrorZ ret_hu_conv = Result_NodeIdDecodeErrorZ.constr_from_ptr(ret);
102                 return ret_hu_conv;
103         }
104
105 }