1 package org.ldk.structs;
3 import org.ldk.impl.bindings;
4 import org.ldk.enums.*;
6 import java.util.Arrays;
7 import java.lang.ref.Reference;
8 import javax.annotation.Nullable;
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.
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!
20 * This struct cannot be used for nodes that wish to support receiving phantom payments;
21 * [`PhantomKeysManager`] must be used instead.
23 * Note that switching between this struct and [`PhantomKeysManager`] will invalidate any
24 * previously issued invoices and attempts to pay previous invoices will fail.
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 {
32 if (ptr != 0) { bindings.KeysManager_free(ptr); }
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).
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
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.
52 * [`ChannelMonitor`]: crate::chain::channelmonitor::ChannelMonitor
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); };
66 * Gets the \"node_id\" secret key used to sign gossip announcements, decode onion data, etc.
68 public byte[] get_node_secret_key() {
69 byte[] ret = bindings.KeysManager_get_node_secret_key(this.ptr);
70 Reference.reachabilityFence(this);
75 * Derive an old [`WriteableEcdsaChannelSigner`] containing per-channel secrets based on a key derivation parameters.
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); };
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.
93 * Returns `Err(())` if the PSBT is missing a descriptor or if we fail to sign.
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`].
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); }; };
110 * Creates a [`Transaction`] which spends the given descriptors to the given outputs, plus an
111 * output to the given change destination (if sufficient change value remains). The
112 * transaction will have a feerate, at least, of the given value.
114 * The `locktime` argument is used to set the transaction's locktime. If `None`, the
115 * transaction will have a locktime of 0. It it recommended to set this to the current block
116 * height to avoid fee sniping, unless you have some specific reason to use a different
119 * Returns `Err(())` if the output value is greater than the input value minus required fee,
120 * if a descriptor was duplicated, or if an output descriptor `script_pubkey`
121 * does not match the one we can spend.
123 * We do not enforce that outputs meet the dust limit or that any output scripts are standard.
125 * May panic if the [`SpendableOutputDescriptor`]s were not generated by channels which used
126 * this [`KeysManager`] or one of the [`InMemorySigner`] created by this [`KeysManager`].
128 public Result_TransactionNoneZ spend_spendable_outputs(SpendableOutputDescriptor[] descriptors, TxOut[] outputs, byte[] change_destination_script, int feerate_sat_per_1000_weight, org.ldk.structs.Option_u32Z locktime) {
129 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, locktime.ptr);
130 Reference.reachabilityFence(this);
131 Reference.reachabilityFence(descriptors);
132 Reference.reachabilityFence(outputs);
133 Reference.reachabilityFence(change_destination_script);
134 Reference.reachabilityFence(feerate_sat_per_1000_weight);
135 Reference.reachabilityFence(locktime);
136 if (ret >= 0 && ret <= 4096) { return null; }
137 Result_TransactionNoneZ ret_hu_conv = Result_TransactionNoneZ.constr_from_ptr(ret);
138 for (SpendableOutputDescriptor descriptors_conv_27: descriptors) { if (this != null) { this.ptrs_to.add(descriptors_conv_27); }; };
139 if (this != null) { this.ptrs_to.add(locktime); };
144 * Constructs a new EntropySource which calls the relevant methods on this_arg.
145 * This copies the `inner` pointer in this_arg and thus the returned EntropySource must be freed before this_arg is
147 public EntropySource as_EntropySource() {
148 long ret = bindings.KeysManager_as_EntropySource(this.ptr);
149 Reference.reachabilityFence(this);
150 if (ret >= 0 && ret <= 4096) { return null; }
151 EntropySource ret_hu_conv = new EntropySource(null, ret);
152 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(this); };
157 * Constructs a new NodeSigner which calls the relevant methods on this_arg.
158 * This copies the `inner` pointer in this_arg and thus the returned NodeSigner must be freed before this_arg is
160 public NodeSigner as_NodeSigner() {
161 long ret = bindings.KeysManager_as_NodeSigner(this.ptr);
162 Reference.reachabilityFence(this);
163 if (ret >= 0 && ret <= 4096) { return null; }
164 NodeSigner ret_hu_conv = new NodeSigner(null, ret);
165 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(this); };
170 * Constructs a new SignerProvider which calls the relevant methods on this_arg.
171 * This copies the `inner` pointer in this_arg and thus the returned SignerProvider must be freed before this_arg is
173 public SignerProvider as_SignerProvider() {
174 long ret = bindings.KeysManager_as_SignerProvider(this.ptr);
175 Reference.reachabilityFence(this);
176 if (ret >= 0 && ret <= 4096) { return null; }
177 SignerProvider ret_hu_conv = new SignerProvider(null, ret);
178 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(this); };