0876219eeb1e51abcae8fdb7f60b04edae88a028
[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 implementor that takes a 32-byte seed for use as a BIP 32 extended key
13  * and derives keys from that.
14  * 
15  * Your node_id is seed/0'
16  * ChannelMonitor 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 @SuppressWarnings("unchecked") // We correctly assign various generic arrays
21 public class KeysManager extends CommonBase {
22         KeysManager(Object _dummy, long ptr) { super(ptr); }
23         @Override @SuppressWarnings("deprecation")
24         protected void finalize() throws Throwable {
25                 super.finalize();
26                 if (ptr != 0) { bindings.KeysManager_free(ptr); }
27         }
28
29         /**
30          * Constructs a KeysManager from a 32-byte seed. If the seed is in some way biased (eg your
31          * CSRNG is busted) this may panic (but more importantly, you will possibly lose funds).
32          * starting_time isn't strictly required to actually be a time, but it must absolutely,
33          * without a doubt, be unique to this instance. ie if you start multiple times with the same
34          * seed, starting_time must be unique to each run. Thus, the easiest way to achieve this is to
35          * simply use the current time (with very high precision).
36          * 
37          * The seed MUST be backed up safely prior to use so that the keys can be re-created, however,
38          * obviously, starting_time should be unique every time you reload the library - it is only
39          * used to generate new ephemeral key data (which will be stored by the individual channel if
40          * necessary).
41          * 
42          * Note that the seed is required to recover certain on-chain funds independent of
43          * ChannelMonitor data, though a current copy of ChannelMonitor data is also required for any
44          * channel, and some on-chain during-closing funds.
45          * 
46          * Note that until the 0.1 release there is no guarantee of backward compatibility between
47          * versions. Once the library is more fully supported, the docs will be updated to include a
48          * detailed description of the guarantee.
49          */
50         public static KeysManager of(byte[] seed, long starting_time_secs, int starting_time_nanos) {
51                 long ret = bindings.KeysManager_new(InternalUtils.check_arr_len(seed, 32), starting_time_secs, starting_time_nanos);
52                 Reference.reachabilityFence(seed);
53                 Reference.reachabilityFence(starting_time_secs);
54                 Reference.reachabilityFence(starting_time_nanos);
55                 if (ret >= 0 && ret <= 4096) { return null; }
56                 KeysManager ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new KeysManager(null, ret); }
57                 ret_hu_conv.ptrs_to.add(ret_hu_conv);
58                 return ret_hu_conv;
59         }
60
61         /**
62          * Derive an old Sign containing per-channel secrets based on a key derivation parameters.
63          * 
64          * Key derivation parameters are accessible through a per-channel secrets
65          * Sign::channel_keys_id and is provided inside DynamicOuputP2WSH in case of
66          * onchain output detection for which a corresponding delayed_payment_key must be derived.
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                 InMemorySigner ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new InMemorySigner(null, ret); }
75                 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 or
85          * if a descriptor was duplicated.
86          * 
87          * We do not enforce that outputs meet the dust limit or that any output scripts are standard.
88          * 
89          * May panic if the `SpendableOutputDescriptor`s were not generated by Channels which used
90          * this KeysManager or one of the `InMemorySigner` created by this KeysManager.
91          */
92         public Result_TransactionNoneZ spend_spendable_outputs(SpendableOutputDescriptor[] descriptors, TxOut[] outputs, byte[] change_destination_script, int feerate_sat_per_1000_weight) {
93                 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);
94                 Reference.reachabilityFence(this);
95                 Reference.reachabilityFence(descriptors);
96                 Reference.reachabilityFence(outputs);
97                 Reference.reachabilityFence(change_destination_script);
98                 Reference.reachabilityFence(feerate_sat_per_1000_weight);
99                 if (ret >= 0 && ret <= 4096) { return null; }
100                 Result_TransactionNoneZ ret_hu_conv = Result_TransactionNoneZ.constr_from_ptr(ret);
101                 return ret_hu_conv;
102         }
103
104         /**
105          * Constructs a new KeysInterface which calls the relevant methods on this_arg.
106          * This copies the `inner` pointer in this_arg and thus the returned KeysInterface must be freed before this_arg is
107          */
108         public KeysInterface as_KeysInterface() {
109                 long ret = bindings.KeysManager_as_KeysInterface(this.ptr);
110                 Reference.reachabilityFence(this);
111                 if (ret >= 0 && ret <= 4096) { return null; }
112                 KeysInterface ret_hu_conv = new KeysInterface(null, ret);
113                 ret_hu_conv.ptrs_to.add(this);
114                 return ret_hu_conv;
115         }
116
117 }