8af8752f4624b3bd45c285724aec759d75eb2449
[ldk-java] / c_sharp / src / org / ldk / structs / CounterpartyCommitmentSecrets.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  * Implements the per-commitment secret storage scheme from
11  * [BOLT 3](https://github.com/lightning/bolts/blob/dcbf8583976df087c79c3ce0b535311212e6812d/03-transactions.md#efficient-per-commitment-secret-storage).
12  * 
13  * Allows us to keep track of all of the revocation secrets of our counterparty in just 50*32 bytes
14  * or so.
15  */
16 public class CounterpartyCommitmentSecrets : CommonBase {
17         internal CounterpartyCommitmentSecrets(object _dummy, long ptr) : base(ptr) { }
18         ~CounterpartyCommitmentSecrets() {
19                 if (ptr != 0) { bindings.CounterpartyCommitmentSecrets_free(ptr); }
20         }
21
22         internal long clone_ptr() {
23                 long ret = bindings.CounterpartyCommitmentSecrets_clone_ptr(this.ptr);
24                 GC.KeepAlive(this);
25                 return ret;
26         }
27
28         /**
29          * Creates a copy of the CounterpartyCommitmentSecrets
30          */
31         public CounterpartyCommitmentSecrets clone() {
32                 long ret = bindings.CounterpartyCommitmentSecrets_clone(this.ptr);
33                 GC.KeepAlive(this);
34                 if (ret >= 0 && ret <= 4096) { return null; }
35                 org.ldk.structs.CounterpartyCommitmentSecrets ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new org.ldk.structs.CounterpartyCommitmentSecrets(null, ret); }
36                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(this); };
37                 return ret_hu_conv;
38         }
39
40         /**
41          * Creates a new empty `CounterpartyCommitmentSecrets` structure.
42          */
43         public static CounterpartyCommitmentSecrets of() {
44                 long ret = bindings.CounterpartyCommitmentSecrets_new();
45                 if (ret >= 0 && ret <= 4096) { return null; }
46                 org.ldk.structs.CounterpartyCommitmentSecrets ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new org.ldk.structs.CounterpartyCommitmentSecrets(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         /**
52          * Returns the minimum index of all stored secrets. Note that indexes start
53          * at 1 << 48 and get decremented by one for each new secret.
54          */
55         public long get_min_seen_secret() {
56                 long ret = bindings.CounterpartyCommitmentSecrets_get_min_seen_secret(this.ptr);
57                 GC.KeepAlive(this);
58                 return ret;
59         }
60
61         /**
62          * Inserts the `secret` at `idx`. Returns `Ok(())` if the secret
63          * was generated in accordance with BOLT 3 and is consistent with previous secrets.
64          */
65         public Result_NoneNoneZ provide_secret(long idx, byte[] secret) {
66                 long ret = bindings.CounterpartyCommitmentSecrets_provide_secret(this.ptr, idx, InternalUtils.check_arr_len(secret, 32));
67                 GC.KeepAlive(this);
68                 GC.KeepAlive(idx);
69                 GC.KeepAlive(secret);
70                 if (ret >= 0 && ret <= 4096) { return null; }
71                 Result_NoneNoneZ ret_hu_conv = Result_NoneNoneZ.constr_from_ptr(ret);
72                 return ret_hu_conv;
73         }
74
75         /**
76          * Returns the secret at `idx`.
77          * Returns `None` if `idx` is < [`CounterpartyCommitmentSecrets::get_min_seen_secret`].
78          * 
79          * Note that the return value (or a relevant inner pointer) may be NULL or all-0s to represent None
80          */
81         public byte[] get_secret(long idx) {
82                 byte[] ret = bindings.CounterpartyCommitmentSecrets_get_secret(this.ptr, idx);
83                 GC.KeepAlive(this);
84                 GC.KeepAlive(idx);
85                 return ret;
86         }
87
88         /**
89          * Serialize the CounterpartyCommitmentSecrets object into a byte array which can be read by CounterpartyCommitmentSecrets_read
90          */
91         public byte[] write() {
92                 byte[] ret = bindings.CounterpartyCommitmentSecrets_write(this.ptr);
93                 GC.KeepAlive(this);
94                 return ret;
95         }
96
97         /**
98          * Read a CounterpartyCommitmentSecrets from a byte array, created by CounterpartyCommitmentSecrets_write
99          */
100         public static Result_CounterpartyCommitmentSecretsDecodeErrorZ read(byte[] ser) {
101                 long ret = bindings.CounterpartyCommitmentSecrets_read(ser);
102                 GC.KeepAlive(ser);
103                 if (ret >= 0 && ret <= 4096) { return null; }
104                 Result_CounterpartyCommitmentSecretsDecodeErrorZ ret_hu_conv = Result_CounterpartyCommitmentSecretsDecodeErrorZ.constr_from_ptr(ret);
105                 return ret_hu_conv;
106         }
107
108 }
109 } } }