1 package org.ldk.structs;
3 import org.ldk.impl.bindings;
4 import org.ldk.enums.*;
6 import java.util.Arrays;
7 import javax.annotation.Nullable;
11 * Simple KeysInterface implementor that takes a 32-byte seed for use as a BIP 32 extended key
12 * and derives keys from that.
14 * Your node_id is seed/0'
15 * ChannelMonitor closes may use seed/1'
16 * Cooperative closes may use seed/2'
17 * The two close keys may be needed to claim on-chain funds!
19 @SuppressWarnings("unchecked") // We correctly assign various generic arrays
20 public class KeysManager extends CommonBase {
21 KeysManager(Object _dummy, long ptr) { super(ptr); }
22 @Override @SuppressWarnings("deprecation")
23 protected void finalize() throws Throwable {
25 if (ptr != 0) { bindings.KeysManager_free(ptr); }
29 * Constructs a KeysManager from a 32-byte seed. If the seed is in some way biased (eg your
30 * CSRNG is busted) this may panic (but more importantly, you will possibly lose funds).
31 * starting_time isn't strictly required to actually be a time, but it must absolutely,
32 * without a doubt, be unique to this instance. ie if you start multiple times with the same
33 * seed, starting_time must be unique to each run. Thus, the easiest way to achieve this is to
34 * simply use the current time (with very high precision).
36 * The seed MUST be backed up safely prior to use so that the keys can be re-created, however,
37 * obviously, starting_time should be unique every time you reload the library - it is only
38 * used to generate new ephemeral key data (which will be stored by the individual channel if
41 * Note that the seed is required to recover certain on-chain funds independent of
42 * ChannelMonitor data, though a current copy of ChannelMonitor data is also required for any
43 * channel, and some on-chain during-closing funds.
45 * Note that until the 0.1 release there is no guarantee of backward compatibility between
46 * versions. Once the library is more fully supported, the docs will be updated to include a
47 * detailed description of the guarantee.
49 public static KeysManager of(byte[] seed, long starting_time_secs, int starting_time_nanos) {
50 long ret = bindings.KeysManager_new(seed, starting_time_secs, starting_time_nanos);
51 if (ret < 1024) { return null; }
52 KeysManager ret_hu_conv = new KeysManager(null, ret);
53 ret_hu_conv.ptrs_to.add(ret_hu_conv);
58 * Derive an old Sign containing per-channel secrets based on a key derivation parameters.
60 * Key derivation parameters are accessible through a per-channel secrets
61 * Sign::channel_keys_id and is provided inside DynamicOuputP2WSH in case of
62 * onchain output detection for which a corresponding delayed_payment_key must be derived.
64 public InMemorySigner derive_channel_keys(long channel_value_satoshis, byte[] params) {
65 long ret = bindings.KeysManager_derive_channel_keys(this.ptr, channel_value_satoshis, params);
66 if (ret < 1024) { return null; }
67 InMemorySigner ret_hu_conv = new InMemorySigner(null, ret);
68 ret_hu_conv.ptrs_to.add(this);
73 * Creates a Transaction which spends the given descriptors to the given outputs, plus an
74 * output to the given change destination (if sufficient change value remains). The
75 * transaction will have a feerate, at least, of the given value.
77 * Returns `Err(())` if the output value is greater than the input value minus required fee or
78 * if a descriptor was duplicated.
80 * We do not enforce that outputs meet the dust limit or that any output scripts are standard.
82 * May panic if the `SpendableOutputDescriptor`s were not generated by Channels which used
83 * this KeysManager or one of the `InMemorySigner` created by this KeysManager.
85 public Result_TransactionNoneZ spend_spendable_outputs(SpendableOutputDescriptor[] descriptors, TxOut[] outputs, byte[] change_destination_script, int feerate_sat_per_1000_weight) {
86 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);
87 if (ret < 1024) { return null; }
88 Result_TransactionNoneZ ret_hu_conv = Result_TransactionNoneZ.constr_from_ptr(ret);
93 * Constructs a new KeysInterface which calls the relevant methods on this_arg.
94 * This copies the `inner` pointer in this_arg and thus the returned KeysInterface must be freed before this_arg is
96 public KeysInterface as_KeysInterface() {
97 long ret = bindings.KeysManager_as_KeysInterface(this.ptr);
98 if (ret < 1024) { return null; }
99 KeysInterface ret_hu_conv = new KeysInterface(null, ret);
100 ret_hu_conv.ptrs_to.add(this);