[Java] Print error stack trace when tests fail
[ldk-java] / src / main / java / org / ldk / structs / KeysManager.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  * Simple implementation of [`EntropySource`], [`NodeSigner`], and [`SignerProvider`] that takes a
13  * 32-byte seed for use as a BIP 32 extended key and derives keys from that.
14  * 
15  * Your `node_id` is seed/0'.
16  * Unilateral closes may use seed/1'.
17  * Cooperative closes may use seed/2'.
18  * The two close keys may be needed to claim on-chain funds!
19  * 
20  * This struct cannot be used for nodes that wish to support receiving phantom payments;
21  * [`PhantomKeysManager`] must be used instead.
22  * 
23  * Note that switching between this struct and [`PhantomKeysManager`] will invalidate any
24  * previously issued invoices and attempts to pay previous invoices will fail.
25  */
26 @SuppressWarnings("unchecked") // We correctly assign various generic arrays
27 public class KeysManager extends CommonBase {
28         KeysManager(Object _dummy, long ptr) { super(ptr); }
29         @Override @SuppressWarnings("deprecation")
30         protected void finalize() throws Throwable {
31                 super.finalize();
32                 if (ptr != 0) { bindings.KeysManager_free(ptr); }
33         }
34
35         /**
36          * Constructs a [`KeysManager`] from a 32-byte seed. If the seed is in some way biased (e.g.,
37          * your CSRNG is busted) this may panic (but more importantly, you will possibly lose funds).
38          * `starting_time` isn't strictly required to actually be a time, but it must absolutely,
39          * without a doubt, be unique to this instance. ie if you start multiple times with the same
40          * `seed`, `starting_time` must be unique to each run. Thus, the easiest way to achieve this
41          * is to simply use the current time (with very high precision).
42          * 
43          * The `seed` MUST be backed up safely prior to use so that the keys can be re-created, however,
44          * obviously, `starting_time` should be unique every time you reload the library - it is only
45          * used to generate new ephemeral key data (which will be stored by the individual channel if
46          * necessary).
47          * 
48          * Note that the seed is required to recover certain on-chain funds independent of
49          * [`ChannelMonitor`] data, though a current copy of [`ChannelMonitor`] data is also required
50          * for any channel, and some on-chain during-closing funds.
51          * 
52          * [`ChannelMonitor`]: crate::chain::channelmonitor::ChannelMonitor
53          */
54         public static KeysManager of(byte[] seed, long starting_time_secs, int starting_time_nanos) {
55                 long ret = bindings.KeysManager_new(InternalUtils.check_arr_len(seed, 32), starting_time_secs, starting_time_nanos);
56                 Reference.reachabilityFence(seed);
57                 Reference.reachabilityFence(starting_time_secs);
58                 Reference.reachabilityFence(starting_time_nanos);
59                 if (ret >= 0 && ret <= 4096) { return null; }
60                 org.ldk.structs.KeysManager ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new org.ldk.structs.KeysManager(null, ret); }
61                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(ret_hu_conv); };
62                 return ret_hu_conv;
63         }
64
65         /**
66          * Gets the \"node_id\" secret key used to sign gossip announcements, decode onion data, etc.
67          */
68         public byte[] get_node_secret_key() {
69                 byte[] ret = bindings.KeysManager_get_node_secret_key(this.ptr);
70                 Reference.reachabilityFence(this);
71                 return ret;
72         }
73
74         /**
75          * Derive an old [`WriteableEcdsaChannelSigner`] containing per-channel secrets based on a key derivation parameters.
76          */
77         public InMemorySigner derive_channel_keys(long channel_value_satoshis, byte[] params) {
78                 long ret = bindings.KeysManager_derive_channel_keys(this.ptr, channel_value_satoshis, InternalUtils.check_arr_len(params, 32));
79                 Reference.reachabilityFence(this);
80                 Reference.reachabilityFence(channel_value_satoshis);
81                 Reference.reachabilityFence(params);
82                 if (ret >= 0 && ret <= 4096) { return null; }
83                 org.ldk.structs.InMemorySigner ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new org.ldk.structs.InMemorySigner(null, ret); }
84                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(this); };
85                 return ret_hu_conv;
86         }
87
88         /**
89          * Signs the given [`PartiallySignedTransaction`] which spends the given [`SpendableOutputDescriptor`]s.
90          * The resulting inputs will be finalized and the PSBT will be ready for broadcast if there
91          * are no other inputs that need signing.
92          * 
93          * Returns `Err(())` if the PSBT is missing a descriptor or if we fail to sign.
94          * 
95          * May panic if the [`SpendableOutputDescriptor`]s were not generated by channels which used
96          * this [`KeysManager`] or one of the [`InMemorySigner`] created by this [`KeysManager`].
97          */
98         public Result_CVec_u8ZNoneZ sign_spendable_outputs_psbt(SpendableOutputDescriptor[] descriptors, byte[] psbt) {
99                 long ret = bindings.KeysManager_sign_spendable_outputs_psbt(this.ptr, descriptors != null ? Arrays.stream(descriptors).mapToLong(descriptors_conv_27 -> descriptors_conv_27.ptr).toArray() : null, psbt);
100                 Reference.reachabilityFence(this);
101                 Reference.reachabilityFence(descriptors);
102                 Reference.reachabilityFence(psbt);
103                 if (ret >= 0 && ret <= 4096) { return null; }
104                 Result_CVec_u8ZNoneZ ret_hu_conv = Result_CVec_u8ZNoneZ.constr_from_ptr(ret);
105                 for (SpendableOutputDescriptor descriptors_conv_27: descriptors) { if (this != null) { this.ptrs_to.add(descriptors_conv_27); }; };
106                 return ret_hu_conv;
107         }
108
109         /**
110          * Constructs a new EntropySource which calls the relevant methods on this_arg.
111          * This copies the `inner` pointer in this_arg and thus the returned EntropySource must be freed before this_arg is
112          */
113         public EntropySource as_EntropySource() {
114                 long ret = bindings.KeysManager_as_EntropySource(this.ptr);
115                 Reference.reachabilityFence(this);
116                 if (ret >= 0 && ret <= 4096) { return null; }
117                 EntropySource ret_hu_conv = new EntropySource(null, ret);
118                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(this); };
119                 return ret_hu_conv;
120         }
121
122         /**
123          * Constructs a new NodeSigner which calls the relevant methods on this_arg.
124          * This copies the `inner` pointer in this_arg and thus the returned NodeSigner must be freed before this_arg is
125          */
126         public NodeSigner as_NodeSigner() {
127                 long ret = bindings.KeysManager_as_NodeSigner(this.ptr);
128                 Reference.reachabilityFence(this);
129                 if (ret >= 0 && ret <= 4096) { return null; }
130                 NodeSigner ret_hu_conv = new NodeSigner(null, ret);
131                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(this); };
132                 return ret_hu_conv;
133         }
134
135         /**
136          * Constructs a new OutputSpender which calls the relevant methods on this_arg.
137          * This copies the `inner` pointer in this_arg and thus the returned OutputSpender must be freed before this_arg is
138          */
139         public OutputSpender as_OutputSpender() {
140                 long ret = bindings.KeysManager_as_OutputSpender(this.ptr);
141                 Reference.reachabilityFence(this);
142                 if (ret >= 0 && ret <= 4096) { return null; }
143                 OutputSpender ret_hu_conv = new OutputSpender(null, ret);
144                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(this); };
145                 return ret_hu_conv;
146         }
147
148         /**
149          * Constructs a new SignerProvider which calls the relevant methods on this_arg.
150          * This copies the `inner` pointer in this_arg and thus the returned SignerProvider must be freed before this_arg is
151          */
152         public SignerProvider as_SignerProvider() {
153                 long ret = bindings.KeysManager_as_SignerProvider(this.ptr);
154                 Reference.reachabilityFence(this);
155                 if (ret >= 0 && ret <= 4096) { return null; }
156                 SignerProvider ret_hu_conv = new SignerProvider(null, ret);
157                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(this); };
158                 return ret_hu_conv;
159         }
160
161 }