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 * A script pubkey for shutting down a channel as defined by [BOLT #2].
14 * [BOLT #2]: https://github.com/lightning/bolts/blob/master/02-peer-protocol.md
16 @SuppressWarnings("unchecked") // We correctly assign various generic arrays
17 public class ShutdownScript extends CommonBase {
18 ShutdownScript(Object _dummy, long ptr) { super(ptr); }
19 @Override @SuppressWarnings("deprecation")
20 protected void finalize() throws Throwable {
22 if (ptr != 0) { bindings.ShutdownScript_free(ptr); }
26 long ret = bindings.ShutdownScript_clone_ptr(this.ptr);
27 Reference.reachabilityFence(this);
32 * Creates a copy of the ShutdownScript
34 public ShutdownScript clone() {
35 long ret = bindings.ShutdownScript_clone(this.ptr);
36 Reference.reachabilityFence(this);
37 if (ret >= 0 && ret <= 4096) { return null; }
38 org.ldk.structs.ShutdownScript ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new org.ldk.structs.ShutdownScript(null, ret); }
39 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(this); };
44 * Serialize the ShutdownScript object into a byte array which can be read by ShutdownScript_read
46 public byte[] write() {
47 byte[] ret = bindings.ShutdownScript_write(this.ptr);
48 Reference.reachabilityFence(this);
53 * Read a ShutdownScript from a byte array, created by ShutdownScript_write
55 public static Result_ShutdownScriptDecodeErrorZ read(byte[] ser) {
56 long ret = bindings.ShutdownScript_read(ser);
57 Reference.reachabilityFence(ser);
58 if (ret >= 0 && ret <= 4096) { return null; }
59 Result_ShutdownScriptDecodeErrorZ ret_hu_conv = Result_ShutdownScriptDecodeErrorZ.constr_from_ptr(ret);
64 * Generates a P2WPKH script pubkey from the given [`WPubkeyHash`].
66 public static ShutdownScript new_p2wpkh(byte[] pubkey_hash) {
67 long ret = bindings.ShutdownScript_new_p2wpkh(InternalUtils.check_arr_len(pubkey_hash, 20));
68 Reference.reachabilityFence(pubkey_hash);
69 if (ret >= 0 && ret <= 4096) { return null; }
70 org.ldk.structs.ShutdownScript ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new org.ldk.structs.ShutdownScript(null, ret); }
71 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(ret_hu_conv); };
76 * Generates a P2WSH script pubkey from the given [`WScriptHash`].
78 public static ShutdownScript new_p2wsh(byte[] script_hash) {
79 long ret = bindings.ShutdownScript_new_p2wsh(InternalUtils.check_arr_len(script_hash, 32));
80 Reference.reachabilityFence(script_hash);
81 if (ret >= 0 && ret <= 4096) { return null; }
82 org.ldk.structs.ShutdownScript ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new org.ldk.structs.ShutdownScript(null, ret); }
83 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(ret_hu_conv); };
88 * Generates a witness script pubkey from the given segwit version and program.
90 * Note for version-zero witness scripts you must use [`ShutdownScript::new_p2wpkh`] or
91 * [`ShutdownScript::new_p2wsh`] instead.
95 * This function may return an error if `program` is invalid for the segwit `version`.
97 public static Result_ShutdownScriptInvalidShutdownScriptZ new_witness_program(WitnessVersion version, byte[] program) {
98 long ret = bindings.ShutdownScript_new_witness_program(version.getVal(), program);
99 Reference.reachabilityFence(version);
100 Reference.reachabilityFence(program);
101 if (ret >= 0 && ret <= 4096) { return null; }
102 Result_ShutdownScriptInvalidShutdownScriptZ ret_hu_conv = Result_ShutdownScriptInvalidShutdownScriptZ.constr_from_ptr(ret);
107 * Converts the shutdown script into the underlying [`Script`].
109 public byte[] into_inner() {
110 byte[] ret = bindings.ShutdownScript_into_inner(this.ptr);
111 Reference.reachabilityFence(this);
112 if (this != null) { this.ptrs_to.add(this); };
117 * Returns the [`PublicKey`] used for a P2WPKH shutdown script if constructed directly from it.
119 * Note that the return value (or a relevant inner pointer) may be NULL or all-0s to represent None
122 public byte[] as_legacy_pubkey() {
123 byte[] ret = bindings.ShutdownScript_as_legacy_pubkey(this.ptr);
124 Reference.reachabilityFence(this);
129 * Returns whether the shutdown script is compatible with the features as defined by BOLT #2.
131 * Specifically, checks for compliance with feature `option_shutdown_anysegwit`.
133 public boolean is_compatible(InitFeatures features) {
134 boolean ret = bindings.ShutdownScript_is_compatible(this.ptr, features == null ? 0 : features.ptr);
135 Reference.reachabilityFence(this);
136 Reference.reachabilityFence(features);
137 if (this != null) { this.ptrs_to.add(features); };