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;
11 * A trait that can return signer instances for individual channels.
13 @SuppressWarnings("unchecked") // We correctly assign various generic arrays
14 public class SignerProvider extends CommonBase {
15 final bindings.LDKSignerProvider bindings_instance;
16 SignerProvider(Object _dummy, long ptr) { super(ptr); bindings_instance = null; }
17 private SignerProvider(bindings.LDKSignerProvider arg) {
18 super(bindings.LDKSignerProvider_new(arg));
19 this.ptrs_to.add(arg);
20 this.bindings_instance = arg;
22 @Override @SuppressWarnings("deprecation")
23 protected void finalize() throws Throwable {
24 if (ptr != 0) { bindings.SignerProvider_free(ptr); } super.finalize();
27 * Destroys the object, freeing associated resources. After this call, any access
28 * to this object may result in a SEGFAULT or worse.
30 * You should generally NEVER call this method. You should let the garbage collector
31 * do this for you when it finalizes objects. However, it may be useful for types
32 * which represent locks and should be closed immediately to avoid holding locks
35 public void destroy() {
36 if (ptr != 0) { bindings.SignerProvider_free(ptr); }
39 public static interface SignerProviderInterface {
41 * Generates a unique `channel_keys_id` that can be used to obtain a [`Self::EcdsaSigner`] through
42 * [`SignerProvider::derive_channel_signer`]. The `user_channel_id` is provided to allow
43 * implementations of [`SignerProvider`] to maintain a mapping between itself and the generated
46 * This method must return a different value each time it is called.
48 byte[] generate_channel_keys_id(boolean inbound, long channel_value_satoshis, UInt128 user_channel_id);
50 * Derives the private key material backing a `Signer`.
52 * To derive a new `Signer`, a fresh `channel_keys_id` should be obtained through
53 * [`SignerProvider::generate_channel_keys_id`]. Otherwise, an existing `Signer` can be
54 * re-derived from its `channel_keys_id`, which can be obtained through its trait method
55 * [`ChannelSigner::channel_keys_id`].
57 WriteableEcdsaChannelSigner derive_channel_signer(long channel_value_satoshis, byte[] channel_keys_id);
59 * Reads a [`Signer`] for this [`SignerProvider`] from the given input stream.
60 * This is only called during deserialization of other objects which contain
61 * [`WriteableEcdsaChannelSigner`]-implementing objects (i.e., [`ChannelMonitor`]s and [`ChannelManager`]s).
62 * The bytes are exactly those which `<Self::Signer as Writeable>::write()` writes, and
63 * contain no versioning scheme. You may wish to include your own version prefix and ensure
64 * you've read all of the provided bytes to ensure no corruption occurred.
66 * This method is slowly being phased out -- it will only be called when reading objects
67 * written by LDK versions prior to 0.0.113.
69 * [`Signer`]: Self::EcdsaSigner
70 * [`ChannelMonitor`]: crate::chain::channelmonitor::ChannelMonitor
71 * [`ChannelManager`]: crate::ln::channelmanager::ChannelManager
73 Result_WriteableEcdsaChannelSignerDecodeErrorZ read_chan_signer(byte[] reader);
75 * Get a script pubkey which we send funds to when claiming on-chain contestable outputs.
77 * If this function returns an error, this will result in a channel failing to open.
79 * This method should return a different value each time it is called, to avoid linking
80 * on-chain funds across channels as controlled to the same user. `channel_keys_id` may be
81 * used to derive a unique value for each channel.
83 Result_CVec_u8ZNoneZ get_destination_script(byte[] channel_keys_id);
85 * Get a script pubkey which we will send funds to when closing a channel.
87 * If this function returns an error, this will result in a channel failing to open or close.
88 * In the event of a failure when the counterparty is initiating a close, this can result in a
89 * channel force close.
91 * This method should return a different value each time it is called, to avoid linking
92 * on-chain funds across channels as controlled to the same user.
94 Result_ShutdownScriptNoneZ get_shutdown_scriptpubkey();
96 private static class LDKSignerProviderHolder { SignerProvider held; }
97 public static SignerProvider new_impl(SignerProviderInterface arg) {
98 final LDKSignerProviderHolder impl_holder = new LDKSignerProviderHolder();
99 impl_holder.held = new SignerProvider(new bindings.LDKSignerProvider() {
100 @Override public byte[] generate_channel_keys_id(boolean inbound, long channel_value_satoshis, byte[] user_channel_id) {
101 org.ldk.util.UInt128 user_channel_id_conv = new org.ldk.util.UInt128(user_channel_id);
102 byte[] ret = arg.generate_channel_keys_id(inbound, channel_value_satoshis, user_channel_id_conv);
103 Reference.reachabilityFence(arg);
104 byte[] result = InternalUtils.check_arr_len(ret, 32);
107 @Override public long derive_channel_signer(long channel_value_satoshis, byte[] channel_keys_id) {
108 WriteableEcdsaChannelSigner ret = arg.derive_channel_signer(channel_value_satoshis, channel_keys_id);
109 Reference.reachabilityFence(arg);
110 long result = ret.clone_ptr();
111 if (impl_holder.held != null) { impl_holder.held.ptrs_to.add(ret); };
114 @Override public long read_chan_signer(byte[] reader) {
115 Result_WriteableEcdsaChannelSignerDecodeErrorZ ret = arg.read_chan_signer(reader);
116 Reference.reachabilityFence(arg);
117 long result = ret.clone_ptr();
120 @Override public long get_destination_script(byte[] channel_keys_id) {
121 Result_CVec_u8ZNoneZ ret = arg.get_destination_script(channel_keys_id);
122 Reference.reachabilityFence(arg);
123 long result = ret.clone_ptr();
126 @Override public long get_shutdown_scriptpubkey() {
127 Result_ShutdownScriptNoneZ ret = arg.get_shutdown_scriptpubkey();
128 Reference.reachabilityFence(arg);
129 long result = ret.clone_ptr();
133 return impl_holder.held;
136 * Generates a unique `channel_keys_id` that can be used to obtain a [`Self::EcdsaSigner`] through
137 * [`SignerProvider::derive_channel_signer`]. The `user_channel_id` is provided to allow
138 * implementations of [`SignerProvider`] to maintain a mapping between itself and the generated
141 * This method must return a different value each time it is called.
143 public byte[] generate_channel_keys_id(boolean inbound, long channel_value_satoshis, org.ldk.util.UInt128 user_channel_id) {
144 byte[] ret = bindings.SignerProvider_generate_channel_keys_id(this.ptr, inbound, channel_value_satoshis, user_channel_id.getLEBytes());
145 Reference.reachabilityFence(this);
146 Reference.reachabilityFence(inbound);
147 Reference.reachabilityFence(channel_value_satoshis);
148 Reference.reachabilityFence(user_channel_id);
153 * Derives the private key material backing a `Signer`.
155 * To derive a new `Signer`, a fresh `channel_keys_id` should be obtained through
156 * [`SignerProvider::generate_channel_keys_id`]. Otherwise, an existing `Signer` can be
157 * re-derived from its `channel_keys_id`, which can be obtained through its trait method
158 * [`ChannelSigner::channel_keys_id`].
160 public WriteableEcdsaChannelSigner derive_channel_signer(long channel_value_satoshis, byte[] channel_keys_id) {
161 long ret = bindings.SignerProvider_derive_channel_signer(this.ptr, channel_value_satoshis, InternalUtils.check_arr_len(channel_keys_id, 32));
162 Reference.reachabilityFence(this);
163 Reference.reachabilityFence(channel_value_satoshis);
164 Reference.reachabilityFence(channel_keys_id);
165 if (ret >= 0 && ret <= 4096) { return null; }
166 WriteableEcdsaChannelSigner ret_hu_conv = new WriteableEcdsaChannelSigner(null, ret);
167 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(this); };
172 * Reads a [`Signer`] for this [`SignerProvider`] from the given input stream.
173 * This is only called during deserialization of other objects which contain
174 * [`WriteableEcdsaChannelSigner`]-implementing objects (i.e., [`ChannelMonitor`]s and [`ChannelManager`]s).
175 * The bytes are exactly those which `<Self::Signer as Writeable>::write()` writes, and
176 * contain no versioning scheme. You may wish to include your own version prefix and ensure
177 * you've read all of the provided bytes to ensure no corruption occurred.
179 * This method is slowly being phased out -- it will only be called when reading objects
180 * written by LDK versions prior to 0.0.113.
182 * [`Signer`]: Self::EcdsaSigner
183 * [`ChannelMonitor`]: crate::chain::channelmonitor::ChannelMonitor
184 * [`ChannelManager`]: crate::ln::channelmanager::ChannelManager
186 public Result_WriteableEcdsaChannelSignerDecodeErrorZ read_chan_signer(byte[] reader) {
187 long ret = bindings.SignerProvider_read_chan_signer(this.ptr, reader);
188 Reference.reachabilityFence(this);
189 Reference.reachabilityFence(reader);
190 if (ret >= 0 && ret <= 4096) { return null; }
191 Result_WriteableEcdsaChannelSignerDecodeErrorZ ret_hu_conv = Result_WriteableEcdsaChannelSignerDecodeErrorZ.constr_from_ptr(ret);
196 * Get a script pubkey which we send funds to when claiming on-chain contestable outputs.
198 * If this function returns an error, this will result in a channel failing to open.
200 * This method should return a different value each time it is called, to avoid linking
201 * on-chain funds across channels as controlled to the same user. `channel_keys_id` may be
202 * used to derive a unique value for each channel.
204 public Result_CVec_u8ZNoneZ get_destination_script(byte[] channel_keys_id) {
205 long ret = bindings.SignerProvider_get_destination_script(this.ptr, InternalUtils.check_arr_len(channel_keys_id, 32));
206 Reference.reachabilityFence(this);
207 Reference.reachabilityFence(channel_keys_id);
208 if (ret >= 0 && ret <= 4096) { return null; }
209 Result_CVec_u8ZNoneZ ret_hu_conv = Result_CVec_u8ZNoneZ.constr_from_ptr(ret);
214 * Get a script pubkey which we will send funds to when closing a channel.
216 * If this function returns an error, this will result in a channel failing to open or close.
217 * In the event of a failure when the counterparty is initiating a close, this can result in a
218 * channel force close.
220 * This method should return a different value each time it is called, to avoid linking
221 * on-chain funds across channels as controlled to the same user.
223 public Result_ShutdownScriptNoneZ get_shutdown_scriptpubkey() {
224 long ret = bindings.SignerProvider_get_shutdown_scriptpubkey(this.ptr);
225 Reference.reachabilityFence(this);
226 if (ret >= 0 && ret <= 4096) { return null; }
227 Result_ShutdownScriptNoneZ ret_hu_conv = Result_ShutdownScriptNoneZ.constr_from_ptr(ret);