b3615fe61fc984a4c437dacd23f2ec88b68121b8
[ldk-java] / c_sharp / src / org / ldk / structs / ChannelId.cs
1 using org.ldk.impl;
2 using org.ldk.enums;
3 using org.ldk.util;
4 using System;
5
6 namespace org { namespace ldk { namespace structs {
7
8
9 /**
10  * A unique 32-byte identifier for a channel.
11  * Depending on how the ID is generated, several varieties are distinguished
12  * (but all are stored as 32 bytes):
13  * _v1_ and _temporary_.
14  * A _v1_ channel ID is generated based on funding tx outpoint (txid & index).
15  * A _temporary_ ID is generated randomly.
16  * (Later revocation-point-based _v2_ is a possibility.)
17  * The variety (context) is not stored, it is relevant only at creation.
18  */
19 public class ChannelId : CommonBase {
20         internal ChannelId(object _dummy, long ptr) : base(ptr) { }
21         ~ChannelId() {
22                 if (ptr != 0) { bindings.ChannelId_free(ptr); }
23         }
24
25         public byte[] get_a() {
26                 long ret = bindings.ChannelId_get_a(this.ptr);
27                 GC.KeepAlive(this);
28                 if (ret >= 0 && ret <= 4096) { return null; }
29                 byte[] ret_conv = InternalUtils.decodeUint8Array(ret);
30                 return ret_conv;
31         }
32
33         public void set_a(byte[] val) {
34                 bindings.ChannelId_set_a(this.ptr, InternalUtils.encodeUint8Array(InternalUtils.check_arr_len(val, 32)));
35                 GC.KeepAlive(this);
36                 GC.KeepAlive(val);
37         }
38
39         /**
40          * Constructs a new ChannelId given each field
41          */
42         public static ChannelId of(byte[] a_arg) {
43                 long ret = bindings.ChannelId_new(InternalUtils.encodeUint8Array(InternalUtils.check_arr_len(a_arg, 32)));
44                 GC.KeepAlive(a_arg);
45                 if (ret >= 0 && ret <= 4096) { return null; }
46                 org.ldk.structs.ChannelId ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new org.ldk.structs.ChannelId(null, ret); }
47                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(ret_hu_conv); };
48                 return ret_hu_conv;
49         }
50
51         internal long clone_ptr() {
52                 long ret = bindings.ChannelId_clone_ptr(this.ptr);
53                 GC.KeepAlive(this);
54                 return ret;
55         }
56
57         /**
58          * Creates a copy of the ChannelId
59          */
60         public ChannelId clone() {
61                 long ret = bindings.ChannelId_clone(this.ptr);
62                 GC.KeepAlive(this);
63                 if (ret >= 0 && ret <= 4096) { return null; }
64                 org.ldk.structs.ChannelId ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new org.ldk.structs.ChannelId(null, ret); }
65                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(this); };
66                 return ret_hu_conv;
67         }
68
69         /**
70          * Checks if two ChannelIds contain equal inner contents.
71          * This ignores pointers and is_owned flags and looks at the values in fields.
72          * Two objects with NULL inner values will be considered "equal" here.
73          */
74         public bool eq(org.ldk.structs.ChannelId b) {
75                 bool ret = bindings.ChannelId_eq(this.ptr, b.ptr);
76                 GC.KeepAlive(this);
77                 GC.KeepAlive(b);
78                 if (this != null) { this.ptrs_to.AddLast(b); };
79                 return ret;
80         }
81
82         public override bool Equals(object o) {
83                 if (!(o is ChannelId)) return false;
84                 return this.eq((ChannelId)o);
85         }
86         /**
87          * Generates a non-cryptographic 64-bit hash of the ChannelId.
88          */
89         public long hash() {
90                 long ret = bindings.ChannelId_hash(this.ptr);
91                 GC.KeepAlive(this);
92                 return ret;
93         }
94
95         public override int GetHashCode() {
96                 return (int)this.hash();
97         }
98         /**
99          * Create _v1_ channel ID based on a funding TX ID and output index
100          */
101         public static ChannelId v1_from_funding_txid(byte[] txid, short output_index) {
102                 long ret = bindings.ChannelId_v1_from_funding_txid(InternalUtils.encodeUint8Array(InternalUtils.check_arr_len(txid, 32)), output_index);
103                 GC.KeepAlive(txid);
104                 GC.KeepAlive(output_index);
105                 if (ret >= 0 && ret <= 4096) { return null; }
106                 org.ldk.structs.ChannelId ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new org.ldk.structs.ChannelId(null, ret); }
107                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(ret_hu_conv); };
108                 return ret_hu_conv;
109         }
110
111         /**
112          * Create _v1_ channel ID from a funding tx outpoint
113          */
114         public static ChannelId v1_from_funding_outpoint(org.ldk.structs.OutPoint outpoint) {
115                 long ret = bindings.ChannelId_v1_from_funding_outpoint(outpoint.ptr);
116                 GC.KeepAlive(outpoint);
117                 if (ret >= 0 && ret <= 4096) { return null; }
118                 org.ldk.structs.ChannelId ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new org.ldk.structs.ChannelId(null, ret); }
119                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(ret_hu_conv); };
120                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(outpoint); };
121                 return ret_hu_conv;
122         }
123
124         /**
125          * Create a _temporary_ channel ID randomly, based on an entropy source.
126          */
127         public static ChannelId temporary_from_entropy_source(org.ldk.structs.EntropySource entropy_source) {
128                 long ret = bindings.ChannelId_temporary_from_entropy_source(entropy_source.ptr);
129                 GC.KeepAlive(entropy_source);
130                 if (ret >= 0 && ret <= 4096) { return null; }
131                 org.ldk.structs.ChannelId ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new org.ldk.structs.ChannelId(null, ret); }
132                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(ret_hu_conv); };
133                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(entropy_source); };
134                 return ret_hu_conv;
135         }
136
137         /**
138          * Generic constructor; create a new channel ID from the provided data.
139          * Use a more specific `*_from_*` constructor when possible.
140          */
141         public static ChannelId from_bytes(byte[] data) {
142                 long ret = bindings.ChannelId_from_bytes(InternalUtils.encodeUint8Array(InternalUtils.check_arr_len(data, 32)));
143                 GC.KeepAlive(data);
144                 if (ret >= 0 && ret <= 4096) { return null; }
145                 org.ldk.structs.ChannelId ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new org.ldk.structs.ChannelId(null, ret); }
146                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(ret_hu_conv); };
147                 return ret_hu_conv;
148         }
149
150         /**
151          * Create a channel ID consisting of all-zeros data (e.g. when uninitialized or a placeholder).
152          */
153         public static ChannelId new_zero() {
154                 long ret = bindings.ChannelId_new_zero();
155                 if (ret >= 0 && ret <= 4096) { return null; }
156                 org.ldk.structs.ChannelId ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new org.ldk.structs.ChannelId(null, ret); }
157                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(ret_hu_conv); };
158                 return ret_hu_conv;
159         }
160
161         /**
162          * Check whether ID is consisting of all zeros (uninitialized)
163          */
164         public bool is_zero() {
165                 bool ret = bindings.ChannelId_is_zero(this.ptr);
166                 GC.KeepAlive(this);
167                 return ret;
168         }
169
170         /**
171          * Create _v2_ channel ID by concatenating the holder revocation basepoint with the counterparty
172          * revocation basepoint and hashing the result. The basepoints will be concatenated in increasing
173          * sorted order.
174          */
175         public static ChannelId v2_from_revocation_basepoints(org.ldk.structs.RevocationBasepoint ours, org.ldk.structs.RevocationBasepoint theirs) {
176                 long ret = bindings.ChannelId_v2_from_revocation_basepoints(ours.ptr, theirs.ptr);
177                 GC.KeepAlive(ours);
178                 GC.KeepAlive(theirs);
179                 if (ret >= 0 && ret <= 4096) { return null; }
180                 org.ldk.structs.ChannelId ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new org.ldk.structs.ChannelId(null, ret); }
181                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(ret_hu_conv); };
182                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(ours); };
183                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(theirs); };
184                 return ret_hu_conv;
185         }
186
187         /**
188          * Create temporary _v2_ channel ID by concatenating a zeroed out basepoint with the holder
189          * revocation basepoint and hashing the result.
190          */
191         public static ChannelId temporary_v2_from_revocation_basepoint(org.ldk.structs.RevocationBasepoint our_revocation_basepoint) {
192                 long ret = bindings.ChannelId_temporary_v2_from_revocation_basepoint(our_revocation_basepoint.ptr);
193                 GC.KeepAlive(our_revocation_basepoint);
194                 if (ret >= 0 && ret <= 4096) { return null; }
195                 org.ldk.structs.ChannelId ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new org.ldk.structs.ChannelId(null, ret); }
196                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(ret_hu_conv); };
197                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(our_revocation_basepoint); };
198                 return ret_hu_conv;
199         }
200
201         /**
202          * Serialize the ChannelId object into a byte array which can be read by ChannelId_read
203          */
204         public byte[] write() {
205                 long ret = bindings.ChannelId_write(this.ptr);
206                 GC.KeepAlive(this);
207                 if (ret >= 0 && ret <= 4096) { return null; }
208                 byte[] ret_conv = InternalUtils.decodeUint8Array(ret);
209                 return ret_conv;
210         }
211
212         /**
213          * Read a ChannelId from a byte array, created by ChannelId_write
214          */
215         public static Result_ChannelIdDecodeErrorZ read(byte[] ser) {
216                 long ret = bindings.ChannelId_read(InternalUtils.encodeUint8Array(ser));
217                 GC.KeepAlive(ser);
218                 if (ret >= 0 && ret <= 4096) { return null; }
219                 Result_ChannelIdDecodeErrorZ ret_hu_conv = Result_ChannelIdDecodeErrorZ.constr_from_ptr(ret);
220                 return ret_hu_conv;
221         }
222
223         /**
224          * Get the string representation of a ChannelId object
225          */
226         public string to_str() {
227                 long ret = bindings.ChannelId_to_str(this.ptr);
228                 GC.KeepAlive(this);
229                 if (ret >= 0 && ret <= 4096) { return null; }
230                 string ret_conv = InternalUtils.decodeString(ret);
231                 return ret_conv;
232         }
233
234 }
235 } } }