840f8a63162454c007a017b5aaa06a774938e35a
[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 [`KeysInterface`] implementation that takes a 32-byte seed for use as a BIP 32 extended
13  * 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          * Derive an old [`Sign`] containing per-channel secrets based on a key derivation parameters.
67          */
68         public InMemorySigner derive_channel_keys(long channel_value_satoshis, byte[] params) {
69                 long ret = bindings.KeysManager_derive_channel_keys(this.ptr, channel_value_satoshis, InternalUtils.check_arr_len(params, 32));
70                 Reference.reachabilityFence(this);
71                 Reference.reachabilityFence(channel_value_satoshis);
72                 Reference.reachabilityFence(params);
73                 if (ret >= 0 && ret <= 4096) { return null; }
74                 org.ldk.structs.InMemorySigner ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new org.ldk.structs.InMemorySigner(null, ret); }
75                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(this); };
76                 return ret_hu_conv;
77         }
78
79         /**
80          * Creates a [`Transaction`] which spends the given descriptors to the given outputs, plus an
81          * output to the given change destination (if sufficient change value remains). The
82          * transaction will have a feerate, at least, of the given value.
83          * 
84          * Returns `Err(())` if the output value is greater than the input value minus required fee,
85          * if a descriptor was duplicated, or if an output descriptor `script_pubkey`
86          * does not match the one we can spend.
87          * 
88          * We do not enforce that outputs meet the dust limit or that any output scripts are standard.
89          * 
90          * May panic if the [`SpendableOutputDescriptor`]s were not generated by channels which used
91          * this [`KeysManager`] or one of the [`InMemorySigner`] created by this [`KeysManager`].
92          */
93         public Result_TransactionNoneZ spend_spendable_outputs(SpendableOutputDescriptor[] descriptors, TxOut[] outputs, byte[] change_destination_script, int feerate_sat_per_1000_weight) {
94                 long ret = bindings.KeysManager_spend_spendable_outputs(this.ptr, descriptors != null ? Arrays.stream(descriptors).mapToLong(descriptors_conv_27 -> descriptors_conv_27.ptr).toArray() : null, outputs != null ? Arrays.stream(outputs).mapToLong(outputs_conv_7 -> outputs_conv_7.ptr).toArray() : null, change_destination_script, feerate_sat_per_1000_weight);
95                 Reference.reachabilityFence(this);
96                 Reference.reachabilityFence(descriptors);
97                 Reference.reachabilityFence(outputs);
98                 Reference.reachabilityFence(change_destination_script);
99                 Reference.reachabilityFence(feerate_sat_per_1000_weight);
100                 if (ret >= 0 && ret <= 4096) { return null; }
101                 Result_TransactionNoneZ ret_hu_conv = Result_TransactionNoneZ.constr_from_ptr(ret);
102                 return ret_hu_conv;
103         }
104
105         /**
106          * Constructs a new KeysInterface which calls the relevant methods on this_arg.
107          * This copies the `inner` pointer in this_arg and thus the returned KeysInterface must be freed before this_arg is
108          */
109         public KeysInterface as_KeysInterface() {
110                 long ret = bindings.KeysManager_as_KeysInterface(this.ptr);
111                 Reference.reachabilityFence(this);
112                 if (ret >= 0 && ret <= 4096) { return null; }
113                 KeysInterface ret_hu_conv = new KeysInterface(null, ret);
114                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(this); };
115                 return ret_hu_conv;
116         }
117
118 }