From 68c220f7ee4fa0b6b4a300f56cd30b9b02ebfbdf Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Tue, 17 Aug 2021 19:50:20 +0000 Subject: [PATCH] Commit latest auto-generated C bindings based on 0.0.100 --- lightning-c-bindings/include/ldk_rust_types.h | 6 + lightning-c-bindings/include/lightning.h | 571 +++++++++++++++++- lightning-c-bindings/include/lightningpp.hpp | 169 ++++-- lightning-c-bindings/src/c_types/derived.rs | 256 ++++++++ .../src/lightning/chain/chaininterface.rs | 6 + .../src/lightning/chain/keysinterface.rs | 25 +- .../src/lightning/ln/channelmanager.rs | 56 +- lightning-c-bindings/src/lightning/ln/mod.rs | 1 + lightning-c-bindings/src/lightning/ln/msgs.rs | 140 ++++- .../src/lightning/ln/peer_handler.rs | 9 +- .../src/lightning/ln/script.rs | 239 ++++++++ .../src/lightning/util/config.rs | 96 ++- .../src/lightning/util/errors.rs | 41 ++ 13 files changed, 1540 insertions(+), 75 deletions(-) create mode 100644 lightning-c-bindings/src/lightning/ln/script.rs diff --git a/lightning-c-bindings/include/ldk_rust_types.h b/lightning-c-bindings/include/ldk_rust_types.h index 4f09cc6..08a46b0 100644 --- a/lightning-c-bindings/include/ldk_rust_types.h +++ b/lightning-c-bindings/include/ldk_rust_types.h @@ -30,6 +30,10 @@ struct nativeCommitmentTransactionOpaque; typedef struct nativeCommitmentTransactionOpaque LDKnativeCommitmentTransaction; struct nativeTrustedCommitmentTransactionOpaque; typedef struct nativeTrustedCommitmentTransactionOpaque LDKnativeTrustedCommitmentTransaction; +struct nativeShutdownScriptOpaque; +typedef struct nativeShutdownScriptOpaque LDKnativeShutdownScript; +struct nativeInvalidShutdownScriptOpaque; +typedef struct nativeInvalidShutdownScriptOpaque LDKnativeInvalidShutdownScript; struct nativeBackgroundProcessorOpaque; typedef struct nativeBackgroundProcessorOpaque LDKnativeBackgroundProcessor; struct nativeRouteHopOpaque; @@ -163,6 +167,8 @@ struct nativeFundingLockedOpaque; typedef struct nativeFundingLockedOpaque LDKnativeFundingLocked; struct nativeShutdownOpaque; typedef struct nativeShutdownOpaque LDKnativeShutdown; +struct nativeClosingSignedFeeRangeOpaque; +typedef struct nativeClosingSignedFeeRangeOpaque LDKnativeClosingSignedFeeRange; struct nativeClosingSignedOpaque; typedef struct nativeClosingSignedOpaque LDKnativeClosingSigned; struct nativeUpdateAddHTLCOpaque; diff --git a/lightning-c-bindings/include/lightning.h b/lightning-c-bindings/include/lightning.h index 2a5f617..ea3e972 100644 --- a/lightning-c-bindings/include/lightning.h +++ b/lightning-c-bindings/include/lightning.h @@ -1184,6 +1184,114 @@ typedef struct LDKCResult_CVec_SignatureZNoneZ { bool result_ok; } LDKCResult_CVec_SignatureZNoneZ; + + +/** + * A script pubkey for shutting down a channel as defined by [BOLT #2]. + * + * [BOLT #2]: https://github.com/lightningnetwork/lightning-rfc/blob/master/02-peer-protocol.md + */ +typedef struct MUST_USE_STRUCT LDKShutdownScript { + /** + * A pointer to the opaque Rust object. + * Nearly everywhere, inner must be non-null, however in places where + * the Rust equivalent takes an Option, it may be set to null to indicate None. + */ + LDKnativeShutdownScript *inner; + /** + * Indicates that this is the only struct which contains the same pointer. + * Rust functions which take ownership of an object provided via an argument require + * this to be true and invalidate the object pointed to by inner. + */ + bool is_owned; +} LDKShutdownScript; + +/** + * The contents of CResult_ShutdownScriptDecodeErrorZ + */ +typedef union LDKCResult_ShutdownScriptDecodeErrorZPtr { + /** + * A pointer to the contents in the success state. + * Reading from this pointer when `result_ok` is not set is undefined. + */ + struct LDKShutdownScript *result; + /** + * A pointer to the contents in the error state. + * Reading from this pointer when `result_ok` is set is undefined. + */ + struct LDKDecodeError *err; +} LDKCResult_ShutdownScriptDecodeErrorZPtr; + +/** + * A CResult_ShutdownScriptDecodeErrorZ represents the result of a fallible operation, + * containing a crate::lightning::ln::script::ShutdownScript on success and a crate::lightning::ln::msgs::DecodeError on failure. + * `result_ok` indicates the overall state, and the contents are provided via `contents`. + */ +typedef struct LDKCResult_ShutdownScriptDecodeErrorZ { + /** + * The contents of this CResult_ShutdownScriptDecodeErrorZ, accessible via either + * `err` or `result` depending on the state of `result_ok`. + */ + union LDKCResult_ShutdownScriptDecodeErrorZPtr contents; + /** + * Whether this CResult_ShutdownScriptDecodeErrorZ represents a success state. + */ + bool result_ok; +} LDKCResult_ShutdownScriptDecodeErrorZ; + + + +/** + * An error occurring when converting from [`Script`] to [`ShutdownScript`]. + */ +typedef struct MUST_USE_STRUCT LDKInvalidShutdownScript { + /** + * A pointer to the opaque Rust object. + * Nearly everywhere, inner must be non-null, however in places where + * the Rust equivalent takes an Option, it may be set to null to indicate None. + */ + LDKnativeInvalidShutdownScript *inner; + /** + * Indicates that this is the only struct which contains the same pointer. + * Rust functions which take ownership of an object provided via an argument require + * this to be true and invalidate the object pointed to by inner. + */ + bool is_owned; +} LDKInvalidShutdownScript; + +/** + * The contents of CResult_ShutdownScriptInvalidShutdownScriptZ + */ +typedef union LDKCResult_ShutdownScriptInvalidShutdownScriptZPtr { + /** + * A pointer to the contents in the success state. + * Reading from this pointer when `result_ok` is not set is undefined. + */ + struct LDKShutdownScript *result; + /** + * A pointer to the contents in the error state. + * Reading from this pointer when `result_ok` is set is undefined. + */ + struct LDKInvalidShutdownScript *err; +} LDKCResult_ShutdownScriptInvalidShutdownScriptZPtr; + +/** + * A CResult_ShutdownScriptInvalidShutdownScriptZ represents the result of a fallible operation, + * containing a crate::lightning::ln::script::ShutdownScript on success and a crate::lightning::ln::script::InvalidShutdownScript on failure. + * `result_ok` indicates the overall state, and the contents are provided via `contents`. + */ +typedef struct LDKCResult_ShutdownScriptInvalidShutdownScriptZ { + /** + * The contents of this CResult_ShutdownScriptInvalidShutdownScriptZ, accessible via either + * `err` or `result` depending on the state of `result_ok`. + */ + union LDKCResult_ShutdownScriptInvalidShutdownScriptZPtr contents; + /** + * Whether this CResult_ShutdownScriptInvalidShutdownScriptZ represents a success state. + */ + bool result_ok; +} LDKCResult_ShutdownScriptInvalidShutdownScriptZ; + /** * The contents of CResult_NoneErrorZ */ @@ -1799,8 +1907,8 @@ typedef struct MUST_USE_STRUCT LDKStaticPaymentOutputDescriptor { typedef enum LDKSpendableOutputDescriptor_Tag { /** * An output to a script which was provided via KeysInterface directly, either from - * `get_destination_script()` or `get_shutdown_pubkey()`, thus you should already know how to - * spend it. No secret keys are provided as rust-lightning was never given any key. + * `get_destination_script()` or `get_shutdown_scriptpubkey()`, thus you should already know + * how to spend it. No secret keys are provided as rust-lightning was never given any key. * These may include outputs from a transaction punishing our counterparty or claiming an HTLC * on-chain using the payment preimage or after it has timed out. */ @@ -3745,6 +3853,17 @@ typedef enum LDKAPIError_Tag { * attempted action to fail. */ LDKAPIError_MonitorUpdateFailed, + /** + * [`KeysInterface::get_shutdown_scriptpubkey`] returned a shutdown scriptpubkey incompatible + * with the channel counterparty as negotiated in [`InitFeatures`]. + * + * Using a SegWit v0 script should resolve this issue. If you cannot, you won't be able to open + * a channel or cooperatively close one with this peer (and will have to force-close instead). + * + * [`KeysInterface::get_shutdown_scriptpubkey`]: crate::chain::keysinterface::KeysInterface::get_shutdown_scriptpubkey + * [`InitFeatures`]: crate::ln::features::InitFeatures + */ + LDKAPIError_IncompatibleShutdownScript, /** * Must be last for serialization purposes */ @@ -3783,6 +3902,13 @@ typedef struct LDKAPIError_LDKChannelUnavailable_Body { struct LDKStr err; } LDKAPIError_LDKChannelUnavailable_Body; +typedef struct LDKAPIError_LDKIncompatibleShutdownScript_Body { + /** + * The incompatible shutdown script. + */ + struct LDKShutdownScript script; +} LDKAPIError_LDKIncompatibleShutdownScript_Body; + typedef struct MUST_USE_STRUCT LDKAPIError { LDKAPIError_Tag tag; union { @@ -3790,6 +3916,7 @@ typedef struct MUST_USE_STRUCT LDKAPIError { LDKAPIError_LDKFeeRateTooHigh_Body fee_rate_too_high; LDKAPIError_LDKRouteError_Body route_error; LDKAPIError_LDKChannelUnavailable_Body channel_unavailable; + LDKAPIError_LDKIncompatibleShutdownScript_Body incompatible_shutdown_script; }; } LDKAPIError; @@ -4330,13 +4457,12 @@ typedef struct LDKKeysInterface { */ struct LDKCVec_u8Z (*get_destination_script)(const void *this_arg); /** - * Get a public key which we will send funds to (in the form of a P2WPKH output) when closing - * a channel. + * Get a script pubkey which we will send funds to when closing a channel. * * This method should return a different value each time it is called, to avoid linking * on-chain funds across channels as controlled to the same user. */ - struct LDKPublicKey (*get_shutdown_pubkey)(const void *this_arg); + struct LDKShutdownScript (*get_shutdown_scriptpubkey)(const void *this_arg); /** * Get a new set of Sign for per-channel secrets. These MUST be unique even if you * restarted with some stale data! @@ -6749,6 +6875,61 @@ typedef struct LDKCResult_ClosingSignedDecodeErrorZ { +/** + * The minimum and maximum fees which the sender is willing to place on the closing transaction. + * This is provided in [`ClosingSigned`] by both sides to indicate the fee range they are willing + * to use. + */ +typedef struct MUST_USE_STRUCT LDKClosingSignedFeeRange { + /** + * A pointer to the opaque Rust object. + * Nearly everywhere, inner must be non-null, however in places where + * the Rust equivalent takes an Option, it may be set to null to indicate None. + */ + LDKnativeClosingSignedFeeRange *inner; + /** + * Indicates that this is the only struct which contains the same pointer. + * Rust functions which take ownership of an object provided via an argument require + * this to be true and invalidate the object pointed to by inner. + */ + bool is_owned; +} LDKClosingSignedFeeRange; + +/** + * The contents of CResult_ClosingSignedFeeRangeDecodeErrorZ + */ +typedef union LDKCResult_ClosingSignedFeeRangeDecodeErrorZPtr { + /** + * A pointer to the contents in the success state. + * Reading from this pointer when `result_ok` is not set is undefined. + */ + struct LDKClosingSignedFeeRange *result; + /** + * A pointer to the contents in the error state. + * Reading from this pointer when `result_ok` is set is undefined. + */ + struct LDKDecodeError *err; +} LDKCResult_ClosingSignedFeeRangeDecodeErrorZPtr; + +/** + * A CResult_ClosingSignedFeeRangeDecodeErrorZ represents the result of a fallible operation, + * containing a crate::lightning::ln::msgs::ClosingSignedFeeRange on success and a crate::lightning::ln::msgs::DecodeError on failure. + * `result_ok` indicates the overall state, and the contents are provided via `contents`. + */ +typedef struct LDKCResult_ClosingSignedFeeRangeDecodeErrorZ { + /** + * The contents of this CResult_ClosingSignedFeeRangeDecodeErrorZ, accessible via either + * `err` or `result` depending on the state of `result_ok`. + */ + union LDKCResult_ClosingSignedFeeRangeDecodeErrorZPtr contents; + /** + * Whether this CResult_ClosingSignedFeeRangeDecodeErrorZ represents a success state. + */ + bool result_ok; +} LDKCResult_ClosingSignedFeeRangeDecodeErrorZ; + + + /** * A commitment_signed message to be sent or received from a peer */ @@ -9549,6 +9730,42 @@ void CResult_CVec_SignatureZNoneZ_free(struct LDKCResult_CVec_SignatureZNoneZ _r */ struct LDKCResult_CVec_SignatureZNoneZ CResult_CVec_SignatureZNoneZ_clone(const struct LDKCResult_CVec_SignatureZNoneZ *NONNULL_PTR orig); +/** + * Creates a new CResult_ShutdownScriptDecodeErrorZ in the success state. + */ +struct LDKCResult_ShutdownScriptDecodeErrorZ CResult_ShutdownScriptDecodeErrorZ_ok(struct LDKShutdownScript o); + +/** + * Creates a new CResult_ShutdownScriptDecodeErrorZ in the error state. + */ +struct LDKCResult_ShutdownScriptDecodeErrorZ CResult_ShutdownScriptDecodeErrorZ_err(struct LDKDecodeError e); + +/** + * Frees any resources used by the CResult_ShutdownScriptDecodeErrorZ. + */ +void CResult_ShutdownScriptDecodeErrorZ_free(struct LDKCResult_ShutdownScriptDecodeErrorZ _res); + +/** + * Creates a new CResult_ShutdownScriptDecodeErrorZ which has the same data as `orig` + * but with all dynamically-allocated buffers duplicated in new buffers. + */ +struct LDKCResult_ShutdownScriptDecodeErrorZ CResult_ShutdownScriptDecodeErrorZ_clone(const struct LDKCResult_ShutdownScriptDecodeErrorZ *NONNULL_PTR orig); + +/** + * Creates a new CResult_ShutdownScriptInvalidShutdownScriptZ in the success state. + */ +struct LDKCResult_ShutdownScriptInvalidShutdownScriptZ CResult_ShutdownScriptInvalidShutdownScriptZ_ok(struct LDKShutdownScript o); + +/** + * Creates a new CResult_ShutdownScriptInvalidShutdownScriptZ in the error state. + */ +struct LDKCResult_ShutdownScriptInvalidShutdownScriptZ CResult_ShutdownScriptInvalidShutdownScriptZ_err(struct LDKInvalidShutdownScript e); + +/** + * Frees any resources used by the CResult_ShutdownScriptInvalidShutdownScriptZ. + */ +void CResult_ShutdownScriptInvalidShutdownScriptZ_free(struct LDKCResult_ShutdownScriptInvalidShutdownScriptZ _res); + /** * Creates a new CResult_NoneErrorZ in the success state. */ @@ -11185,6 +11402,27 @@ void CResult_ClosingSignedDecodeErrorZ_free(struct LDKCResult_ClosingSignedDecod */ struct LDKCResult_ClosingSignedDecodeErrorZ CResult_ClosingSignedDecodeErrorZ_clone(const struct LDKCResult_ClosingSignedDecodeErrorZ *NONNULL_PTR orig); +/** + * Creates a new CResult_ClosingSignedFeeRangeDecodeErrorZ in the success state. + */ +struct LDKCResult_ClosingSignedFeeRangeDecodeErrorZ CResult_ClosingSignedFeeRangeDecodeErrorZ_ok(struct LDKClosingSignedFeeRange o); + +/** + * Creates a new CResult_ClosingSignedFeeRangeDecodeErrorZ in the error state. + */ +struct LDKCResult_ClosingSignedFeeRangeDecodeErrorZ CResult_ClosingSignedFeeRangeDecodeErrorZ_err(struct LDKDecodeError e); + +/** + * Frees any resources used by the CResult_ClosingSignedFeeRangeDecodeErrorZ. + */ +void CResult_ClosingSignedFeeRangeDecodeErrorZ_free(struct LDKCResult_ClosingSignedFeeRangeDecodeErrorZ _res); + +/** + * Creates a new CResult_ClosingSignedFeeRangeDecodeErrorZ which has the same data as `orig` + * but with all dynamically-allocated buffers duplicated in new buffers. + */ +struct LDKCResult_ClosingSignedFeeRangeDecodeErrorZ CResult_ClosingSignedFeeRangeDecodeErrorZ_clone(const struct LDKCResult_ClosingSignedFeeRangeDecodeErrorZ *NONNULL_PTR orig); + /** * Creates a new CResult_CommitmentSignedDecodeErrorZ in the success state. */ @@ -12003,6 +12241,11 @@ struct LDKAPIError APIError_channel_unavailable(struct LDKStr err); */ struct LDKAPIError APIError_monitor_update_failed(void); +/** + * Utility method to constructs a new IncompatibleShutdownScript-variant APIError + */ +struct LDKAPIError APIError_incompatible_shutdown_script(struct LDKShutdownScript script); + /** * Creates a digital signature of a message given a SecretKey, like the node's secret. * A receiver knowing the PublicKey (e.g. the node's id) and the message can be sure that the signature was generated by the caller. @@ -12501,10 +12744,100 @@ bool ChannelConfig_get_commit_upfront_shutdown_pubkey(const struct LDKChannelCon */ void ChannelConfig_set_commit_upfront_shutdown_pubkey(struct LDKChannelConfig *NONNULL_PTR this_ptr, bool val); +/** + * Limit our total exposure to in-flight HTLCs which are burned to fees as they are too + * small to claim on-chain. + * + * When an HTLC present in one of our channels is below a \"dust\" threshold, the HTLC will + * not be claimable on-chain, instead being turned into additional miner fees if either + * party force-closes the channel. Because the threshold is per-HTLC, our total exposure + * to such payments may be sustantial if there are many dust HTLCs present when the + * channel is force-closed. + * + * This limit is applied for sent, forwarded, and received HTLCs and limits the total + * exposure across all three types per-channel. Setting this too low may prevent the + * sending or receipt of low-value HTLCs on high-traffic nodes, and this limit is very + * important to prevent stealing of dust HTLCs by miners. + * + * Default value: 5_000_000 msat. + */ +uint64_t ChannelConfig_get_max_dust_htlc_exposure_msat(const struct LDKChannelConfig *NONNULL_PTR this_ptr); + +/** + * Limit our total exposure to in-flight HTLCs which are burned to fees as they are too + * small to claim on-chain. + * + * When an HTLC present in one of our channels is below a \"dust\" threshold, the HTLC will + * not be claimable on-chain, instead being turned into additional miner fees if either + * party force-closes the channel. Because the threshold is per-HTLC, our total exposure + * to such payments may be sustantial if there are many dust HTLCs present when the + * channel is force-closed. + * + * This limit is applied for sent, forwarded, and received HTLCs and limits the total + * exposure across all three types per-channel. Setting this too low may prevent the + * sending or receipt of low-value HTLCs on high-traffic nodes, and this limit is very + * important to prevent stealing of dust HTLCs by miners. + * + * Default value: 5_000_000 msat. + */ +void ChannelConfig_set_max_dust_htlc_exposure_msat(struct LDKChannelConfig *NONNULL_PTR this_ptr, uint64_t val); + +/** + * The additional fee we're willing to pay to avoid waiting for the counterparty's + * `to_self_delay` to reclaim funds. + * + * When we close a channel cooperatively with our counterparty, we negotiate a fee for the + * closing transaction which both sides find acceptable, ultimately paid by the channel + * funder/initiator. + * + * When we are the funder, because we have to pay the channel closing fee, we bound the + * acceptable fee by our [`Background`] and [`Normal`] fees, with the upper bound increased by + * this value. Because the on-chain fee we'd pay to force-close the channel is kept near our + * [`Normal`] feerate during normal operation, this value represents the additional fee we're + * willing to pay in order to avoid waiting for our counterparty's to_self_delay to reclaim our + * funds. + * + * When we are not the funder, we require the closing transaction fee pay at least our + * [`Background`] fee estimate, but allow our counterparty to pay as much fee as they like. + * Thus, this value is ignored when we are not the funder. + * + * Default value: 1000 satoshis. + * + * [`Normal`]: crate::chain::chaininterface::ConfirmationTarget::Normal + * [`Background`]: crate::chain::chaininterface::ConfirmationTarget::Background + */ +uint64_t ChannelConfig_get_force_close_avoidance_max_fee_satoshis(const struct LDKChannelConfig *NONNULL_PTR this_ptr); + +/** + * The additional fee we're willing to pay to avoid waiting for the counterparty's + * `to_self_delay` to reclaim funds. + * + * When we close a channel cooperatively with our counterparty, we negotiate a fee for the + * closing transaction which both sides find acceptable, ultimately paid by the channel + * funder/initiator. + * + * When we are the funder, because we have to pay the channel closing fee, we bound the + * acceptable fee by our [`Background`] and [`Normal`] fees, with the upper bound increased by + * this value. Because the on-chain fee we'd pay to force-close the channel is kept near our + * [`Normal`] feerate during normal operation, this value represents the additional fee we're + * willing to pay in order to avoid waiting for our counterparty's to_self_delay to reclaim our + * funds. + * + * When we are not the funder, we require the closing transaction fee pay at least our + * [`Background`] fee estimate, but allow our counterparty to pay as much fee as they like. + * Thus, this value is ignored when we are not the funder. + * + * Default value: 1000 satoshis. + * + * [`Normal`]: crate::chain::chaininterface::ConfirmationTarget::Normal + * [`Background`]: crate::chain::chaininterface::ConfirmationTarget::Background + */ +void ChannelConfig_set_force_close_avoidance_max_fee_satoshis(struct LDKChannelConfig *NONNULL_PTR this_ptr, uint64_t val); + /** * Constructs a new ChannelConfig given each field */ -MUST_USE_RES struct LDKChannelConfig ChannelConfig_new(uint32_t forwarding_fee_proportional_millionths_arg, uint32_t forwarding_fee_base_msat_arg, uint16_t cltv_expiry_delta_arg, bool announced_channel_arg, bool commit_upfront_shutdown_pubkey_arg); +MUST_USE_RES struct LDKChannelConfig ChannelConfig_new(uint32_t forwarding_fee_proportional_millionths_arg, uint32_t forwarding_fee_base_msat_arg, uint16_t cltv_expiry_delta_arg, bool announced_channel_arg, bool commit_upfront_shutdown_pubkey_arg, uint64_t max_dust_htlc_exposure_msat_arg, uint64_t force_close_avoidance_max_fee_satoshis_arg); /** * Creates a copy of the ChannelConfig @@ -12766,6 +13099,12 @@ enum LDKConfirmationTarget ConfirmationTarget_normal(void); */ enum LDKConfirmationTarget ConfirmationTarget_high_priority(void); +/** + * Checks if two ConfirmationTargets contain equal inner contents. + * This ignores pointers and is_owned flags and looks at the values in fields. + */ +bool ConfirmationTarget_eq(const enum LDKConfirmationTarget *NONNULL_PTR a, const enum LDKConfirmationTarget *NONNULL_PTR b); + /** * Calls the free function if one is set */ @@ -14044,10 +14383,45 @@ MUST_USE_RES struct LDKCVec_ChannelDetailsZ ChannelManager_list_usable_channels( * will be accepted on the given channel, and after additional timeout/the closing of all * pending HTLCs, the channel will be closed on chain. * + * * If we are the channel initiator, we will pay between our [`Background`] and + * [`ChannelConfig::force_close_avoidance_max_fee_satoshis`] plus our [`Normal`] fee + * estimate. + * * If our counterparty is the channel initiator, we will require a channel closing + * transaction feerate of at least our [`Background`] feerate or the feerate which + * would appear on a force-closure transaction, whichever is lower. We will allow our + * counterparty to pay as much fee as they'd like, however. + * * May generate a SendShutdown message event on success, which should be relayed. + * + * [`ChannelConfig::force_close_avoidance_max_fee_satoshis`]: crate::util::config::ChannelConfig::force_close_avoidance_max_fee_satoshis + * [`Background`]: crate::chain::chaininterface::ConfirmationTarget::Background + * [`Normal`]: crate::chain::chaininterface::ConfirmationTarget::Normal */ MUST_USE_RES struct LDKCResult_NoneAPIErrorZ ChannelManager_close_channel(const struct LDKChannelManager *NONNULL_PTR this_arg, const uint8_t (*channel_id)[32]); +/** + * Begins the process of closing a channel. After this call (plus some timeout), no new HTLCs + * will be accepted on the given channel, and after additional timeout/the closing of all + * pending HTLCs, the channel will be closed on chain. + * + * `target_feerate_sat_per_1000_weight` has different meanings depending on if we initiated + * the channel being closed or not: + * * If we are the channel initiator, we will pay at least this feerate on the closing + * transaction. The upper-bound is set by + * [`ChannelConfig::force_close_avoidance_max_fee_satoshis`] plus our [`Normal`] fee + * estimate (or `target_feerate_sat_per_1000_weight`, if it is greater). + * * If our counterparty is the channel initiator, we will refuse to accept a channel closure + * transaction feerate below `target_feerate_sat_per_1000_weight` (or the feerate which + * will appear on a force-closure transaction, whichever is lower). + * + * May generate a SendShutdown message event on success, which should be relayed. + * + * [`ChannelConfig::force_close_avoidance_max_fee_satoshis`]: crate::util::config::ChannelConfig::force_close_avoidance_max_fee_satoshis + * [`Background`]: crate::chain::chaininterface::ConfirmationTarget::Background + * [`Normal`]: crate::chain::chaininterface::ConfirmationTarget::Normal + */ +MUST_USE_RES struct LDKCResult_NoneAPIErrorZ ChannelManager_close_channel_with_target_feerate(const struct LDKChannelManager *NONNULL_PTR this_arg, const uint8_t (*channel_id)[32], uint32_t target_feerate_sats_per_1000_weight); + /** * Force closes a channel, immediately broadcasting the latest local commitment transaction to * the chain and rejecting new HTLCs on the given channel. Fails if channel_id is unknown to the manager. @@ -14112,9 +14486,13 @@ MUST_USE_RES struct LDKCResult_NonePaymentSendFailureZ ChannelManager_send_payme * would be able to guess -- otherwise, an intermediate node may claim the payment and it will * never reach the recipient. * + * See [`send_payment`] documentation for more details on the return value of this function. + * * Similar to regular payments, you MUST NOT reuse a `payment_preimage` value. See * [`send_payment`] for more information about the risks of duplicate preimage usage. * + * Note that `route` must have exactly one path. + * * [`send_payment`]: Self::send_payment * * Note that payment_preimage (or a relevant inner pointer) may be NULL or all-0s to represent None @@ -14176,13 +14554,16 @@ void ChannelManager_broadcast_node_announcement(const struct LDKChannelManager * void ChannelManager_process_pending_htlc_forwards(const struct LDKChannelManager *NONNULL_PTR this_arg); /** - * If a peer is disconnected we mark any channels with that peer as 'disabled'. - * After some time, if channels are still disabled we need to broadcast a ChannelUpdate - * to inform the network about the uselessness of these channels. + * Performs actions which should happen on startup and roughly once per minute thereafter. * - * This method handles all the details, and must be called roughly once per minute. + * This currently includes: + * * Increasing or decreasing the on-chain feerate estimates for our outbound channels, + * * Broadcasting `ChannelUpdate` messages if we've been disconnected from our peer for more + * than a minute, informing the network that they should no longer attempt to route over + * the channel. * - * Note that in some rare cases this may generate a `chain::Watch::update_channel` call. + * Note that this may cause reentrancy through `chain::Watch::update_channel` calls or feerate + * estimate fetches. */ void ChannelManager_timer_tick_occurred(const struct LDKChannelManager *NONNULL_PTR this_arg); @@ -15110,6 +15491,45 @@ MUST_USE_RES struct LDKShutdown Shutdown_new(struct LDKThirtyTwoBytes channel_id */ struct LDKShutdown Shutdown_clone(const struct LDKShutdown *NONNULL_PTR orig); +/** + * Frees any resources used by the ClosingSignedFeeRange, if is_owned is set and inner is non-NULL. + */ +void ClosingSignedFeeRange_free(struct LDKClosingSignedFeeRange this_obj); + +/** + * The minimum absolute fee, in satoshis, which the sender is willing to place on the closing + * transaction. + */ +uint64_t ClosingSignedFeeRange_get_min_fee_satoshis(const struct LDKClosingSignedFeeRange *NONNULL_PTR this_ptr); + +/** + * The minimum absolute fee, in satoshis, which the sender is willing to place on the closing + * transaction. + */ +void ClosingSignedFeeRange_set_min_fee_satoshis(struct LDKClosingSignedFeeRange *NONNULL_PTR this_ptr, uint64_t val); + +/** + * The maximum absolute fee, in satoshis, which the sender is willing to place on the closing + * transaction. + */ +uint64_t ClosingSignedFeeRange_get_max_fee_satoshis(const struct LDKClosingSignedFeeRange *NONNULL_PTR this_ptr); + +/** + * The maximum absolute fee, in satoshis, which the sender is willing to place on the closing + * transaction. + */ +void ClosingSignedFeeRange_set_max_fee_satoshis(struct LDKClosingSignedFeeRange *NONNULL_PTR this_ptr, uint64_t val); + +/** + * Constructs a new ClosingSignedFeeRange given each field + */ +MUST_USE_RES struct LDKClosingSignedFeeRange ClosingSignedFeeRange_new(uint64_t min_fee_satoshis_arg, uint64_t max_fee_satoshis_arg); + +/** + * Creates a copy of the ClosingSignedFeeRange + */ +struct LDKClosingSignedFeeRange ClosingSignedFeeRange_clone(const struct LDKClosingSignedFeeRange *NONNULL_PTR orig); + /** * Frees any resources used by the ClosingSigned, if is_owned is set and inner is non-NULL. */ @@ -15145,10 +15565,26 @@ struct LDKSignature ClosingSigned_get_signature(const struct LDKClosingSigned *N */ void ClosingSigned_set_signature(struct LDKClosingSigned *NONNULL_PTR this_ptr, struct LDKSignature val); +/** + * The minimum and maximum fees which the sender is willing to accept, provided only by new + * nodes. + * + * Note that the return value (or a relevant inner pointer) may be NULL or all-0s to represent None + */ +struct LDKClosingSignedFeeRange ClosingSigned_get_fee_range(const struct LDKClosingSigned *NONNULL_PTR this_ptr); + +/** + * The minimum and maximum fees which the sender is willing to accept, provided only by new + * nodes. + * + * Note that val (or a relevant inner pointer) may be NULL or all-0s to represent None + */ +void ClosingSigned_set_fee_range(struct LDKClosingSigned *NONNULL_PTR this_ptr, struct LDKClosingSignedFeeRange val); + /** * Constructs a new ClosingSigned given each field */ -MUST_USE_RES struct LDKClosingSigned ClosingSigned_new(struct LDKThirtyTwoBytes channel_id_arg, uint64_t fee_satoshis_arg, struct LDKSignature signature_arg); +MUST_USE_RES struct LDKClosingSigned ClosingSigned_new(struct LDKThirtyTwoBytes channel_id_arg, uint64_t fee_satoshis_arg, struct LDKSignature signature_arg, struct LDKClosingSignedFeeRange fee_range_arg); /** * Creates a copy of the ClosingSigned @@ -16431,6 +16867,16 @@ struct LDKCVec_u8Z ClosingSigned_write(const struct LDKClosingSigned *NONNULL_PT */ struct LDKCResult_ClosingSignedDecodeErrorZ ClosingSigned_read(struct LDKu8slice ser); +/** + * Serialize the ClosingSignedFeeRange object into a byte array which can be read by ClosingSignedFeeRange_read + */ +struct LDKCVec_u8Z ClosingSignedFeeRange_write(const struct LDKClosingSignedFeeRange *NONNULL_PTR obj); + +/** + * Read a ClosingSignedFeeRange from a byte array, created by ClosingSignedFeeRange_write + */ +struct LDKCResult_ClosingSignedFeeRangeDecodeErrorZ ClosingSignedFeeRange_read(struct LDKu8slice ser); + /** * Serialize the CommitmentSigned object into a byte array which can be read by CommitmentSigned_read */ @@ -16948,9 +17394,12 @@ void PeerManager_socket_disconnected(const struct LDKPeerManager *NONNULL_PTR th void PeerManager_disconnect_by_node_id(const struct LDKPeerManager *NONNULL_PTR this_arg, struct LDKPublicKey node_id, bool no_connection_possible); /** - * This function should be called roughly once every 30 seconds. - * It will send pings to each peer and disconnect those which did not respond to the last - * round of pings. + * Send pings to each peer and disconnect those which did not respond to the last round of + * pings. + * + * This may be called on any timescale you want, however, roughly once every five to ten + * seconds is preferred. The call rate determines both how often we send a ping to our peers + * and how much time they have to respond before we disconnect them. * * May call [`send_data`] on all [`SocketDescriptor`]s. Thus, be very careful with reentrancy * issues! @@ -17865,6 +18314,98 @@ struct LDKCResult_ChannelFeaturesDecodeErrorZ ChannelFeatures_read(struct LDKu8s */ struct LDKCResult_InvoiceFeaturesDecodeErrorZ InvoiceFeatures_read(struct LDKu8slice ser); +/** + * Frees any resources used by the ShutdownScript, if is_owned is set and inner is non-NULL. + */ +void ShutdownScript_free(struct LDKShutdownScript this_obj); + +/** + * Creates a copy of the ShutdownScript + */ +struct LDKShutdownScript ShutdownScript_clone(const struct LDKShutdownScript *NONNULL_PTR orig); + +/** + * Frees any resources used by the InvalidShutdownScript, if is_owned is set and inner is non-NULL. + */ +void InvalidShutdownScript_free(struct LDKInvalidShutdownScript this_obj); + +/** + * The script that did not meet the requirements from [BOLT #2]. + * + * [BOLT #2]: https://github.com/lightningnetwork/lightning-rfc/blob/master/02-peer-protocol.md + */ +struct LDKu8slice InvalidShutdownScript_get_script(const struct LDKInvalidShutdownScript *NONNULL_PTR this_ptr); + +/** + * The script that did not meet the requirements from [BOLT #2]. + * + * [BOLT #2]: https://github.com/lightningnetwork/lightning-rfc/blob/master/02-peer-protocol.md + */ +void InvalidShutdownScript_set_script(struct LDKInvalidShutdownScript *NONNULL_PTR this_ptr, struct LDKCVec_u8Z val); + +/** + * Constructs a new InvalidShutdownScript given each field + */ +MUST_USE_RES struct LDKInvalidShutdownScript InvalidShutdownScript_new(struct LDKCVec_u8Z script_arg); + +/** + * Serialize the ShutdownScript object into a byte array which can be read by ShutdownScript_read + */ +struct LDKCVec_u8Z ShutdownScript_write(const struct LDKShutdownScript *NONNULL_PTR obj); + +/** + * Read a ShutdownScript from a byte array, created by ShutdownScript_write + */ +struct LDKCResult_ShutdownScriptDecodeErrorZ ShutdownScript_read(struct LDKu8slice ser); + +/** + * Generates a P2PKH script pubkey from the given [`PubkeyHash`]. + */ +MUST_USE_RES struct LDKShutdownScript ShutdownScript_new_p2pkh(const uint8_t (*pubkey_hash)[20]); + +/** + * Generates a P2SH script pubkey from the given [`ScriptHash`]. + */ +MUST_USE_RES struct LDKShutdownScript ShutdownScript_new_p2sh(const uint8_t (*script_hash)[20]); + +/** + * Generates a P2WPKH script pubkey from the given [`WPubkeyHash`]. + */ +MUST_USE_RES struct LDKShutdownScript ShutdownScript_new_p2wpkh(const uint8_t (*pubkey_hash)[20]); + +/** + * Generates a P2WSH script pubkey from the given [`WScriptHash`]. + */ +MUST_USE_RES struct LDKShutdownScript ShutdownScript_new_p2wsh(const uint8_t (*script_hash)[32]); + +/** + * Generates a P2WSH script pubkey from the given segwit version and program. + * + * # Errors + * + * This function may return an error if `program` is invalid for the segwit `version`. + */ +MUST_USE_RES struct LDKCResult_ShutdownScriptInvalidShutdownScriptZ ShutdownScript_new_witness_program(uint8_t version, struct LDKu8slice program); + +/** + * Converts the shutdown script into the underlying [`Script`]. + */ +MUST_USE_RES struct LDKCVec_u8Z ShutdownScript_into_inner(struct LDKShutdownScript this_arg); + +/** + * Returns the [`PublicKey`] used for a P2WPKH shutdown script if constructed directly from it. + * + * Note that the return value (or a relevant inner pointer) may be NULL or all-0s to represent None + */ +MUST_USE_RES struct LDKPublicKey ShutdownScript_as_legacy_pubkey(const struct LDKShutdownScript *NONNULL_PTR this_arg); + +/** + * Returns whether the shutdown script is compatible with the features as defined by BOLT #2. + * + * Specifically, checks for compliance with feature `option_shutdown_anysegwit`. + */ +MUST_USE_RES bool ShutdownScript_is_compatible(const struct LDKShutdownScript *NONNULL_PTR this_arg, const struct LDKInitFeatures *NONNULL_PTR features); + /** * Frees any resources used by the RouteHop, if is_owned is set and inner is non-NULL. */ diff --git a/lightning-c-bindings/include/lightningpp.hpp b/lightning-c-bindings/include/lightningpp.hpp index 8d53186..a5ee199 100644 --- a/lightning-c-bindings/include/lightningpp.hpp +++ b/lightning-c-bindings/include/lightningpp.hpp @@ -11,6 +11,8 @@ class HolderCommitmentTransaction; class BuiltCommitmentTransaction; class CommitmentTransaction; class TrustedCommitmentTransaction; +class ShutdownScript; +class InvalidShutdownScript; class BackgroundProcessor; class ChannelManagerPersister; class RouteHop; @@ -109,6 +111,7 @@ class FundingCreated; class FundingSigned; class FundingLocked; class Shutdown; +class ClosingSignedFeeRange; class ClosingSigned; class UpdateAddHTLC; class UpdateFulfillHTLC; @@ -172,12 +175,14 @@ class COption_C2Tuple_usizeTransactionZZ; class CResult_TransactionNoneZ; class CResult_SignedRawInvoiceNoneZ; class CResult_ExpiryTimeCreationErrorZ; +class CResult_ClosingSignedFeeRangeDecodeErrorZ; class CResult_PingDecodeErrorZ; class CVec_TransactionOutputsZ; class CResult_ErrorMessageDecodeErrorZ; class CResult_OpenChannelDecodeErrorZ; class CVec_CVec_u8ZZ; class CResult_SecretKeyErrorZ; +class CResult_ShutdownScriptDecodeErrorZ; class CResult_InvoiceNoneZ; class CResult_QueryChannelRangeDecodeErrorZ; class C2Tuple_usizeTransactionZ; @@ -193,11 +198,12 @@ class CResult_boolLightningErrorZ; class CResult_TxCreationKeysErrorZ; class C2Tuple_BlockHashChannelMonitorZ; class CResult_FundingSignedDecodeErrorZ; +class CResult_ShutdownScriptInvalidShutdownScriptZ; class CResult_RecoverableSignatureNoneZ; class CResult_NodeAnnouncementInfoDecodeErrorZ; class CResult_NetAddressu8Z; -class CVec_UpdateFailMalformedHTLCZ; class C3Tuple_RawInvoice_u832InvoiceSignatureZ; +class CVec_UpdateFailMalformedHTLCZ; class CResult_NetworkGraphDecodeErrorZ; class CVec_RouteHopZ; class CVec_C2Tuple_BlockHashChannelMonitorZZ; @@ -250,8 +256,8 @@ class CResult_PayeePubKeyErrorZ; class CResult_QueryShortChannelIdsDecodeErrorZ; class CResult_InvoiceSemanticErrorZ; class CResult_UpdateAddHTLCDecodeErrorZ; -class CResult_NoneAPIErrorZ; class CResult_CounterpartyChannelTransactionParametersDecodeErrorZ; +class CResult_NoneAPIErrorZ; class CVec_NetAddressZ; class CVec_C2Tuple_usizeTransactionZZ; class CVec_PublicKeyZ; @@ -443,6 +449,36 @@ public: const LDKTrustedCommitmentTransaction* operator &() const { return &self; } const LDKTrustedCommitmentTransaction* operator ->() const { return &self; } }; +class ShutdownScript { +private: + LDKShutdownScript self; +public: + ShutdownScript(const ShutdownScript&) = delete; + ShutdownScript(ShutdownScript&& o) : self(o.self) { memset(&o, 0, sizeof(ShutdownScript)); } + ShutdownScript(LDKShutdownScript&& m_self) : self(m_self) { memset(&m_self, 0, sizeof(LDKShutdownScript)); } + operator LDKShutdownScript() && { LDKShutdownScript res = self; memset(&self, 0, sizeof(LDKShutdownScript)); return res; } + ~ShutdownScript() { ShutdownScript_free(self); } + ShutdownScript& operator=(ShutdownScript&& o) { ShutdownScript_free(self); self = o.self; memset(&o, 0, sizeof(ShutdownScript)); return *this; } + LDKShutdownScript* operator &() { return &self; } + LDKShutdownScript* operator ->() { return &self; } + const LDKShutdownScript* operator &() const { return &self; } + const LDKShutdownScript* operator ->() const { return &self; } +}; +class InvalidShutdownScript { +private: + LDKInvalidShutdownScript self; +public: + InvalidShutdownScript(const InvalidShutdownScript&) = delete; + InvalidShutdownScript(InvalidShutdownScript&& o) : self(o.self) { memset(&o, 0, sizeof(InvalidShutdownScript)); } + InvalidShutdownScript(LDKInvalidShutdownScript&& m_self) : self(m_self) { memset(&m_self, 0, sizeof(LDKInvalidShutdownScript)); } + operator LDKInvalidShutdownScript() && { LDKInvalidShutdownScript res = self; memset(&self, 0, sizeof(LDKInvalidShutdownScript)); return res; } + ~InvalidShutdownScript() { InvalidShutdownScript_free(self); } + InvalidShutdownScript& operator=(InvalidShutdownScript&& o) { InvalidShutdownScript_free(self); self = o.self; memset(&o, 0, sizeof(InvalidShutdownScript)); return *this; } + LDKInvalidShutdownScript* operator &() { return &self; } + LDKInvalidShutdownScript* operator ->() { return &self; } + const LDKInvalidShutdownScript* operator &() const { return &self; } + const LDKInvalidShutdownScript* operator ->() const { return &self; } +}; class BackgroundProcessor { private: LDKBackgroundProcessor self; @@ -1229,13 +1265,12 @@ public: */ inline LDK::CVec_u8Z get_destination_script(); /** - * Get a public key which we will send funds to (in the form of a P2WPKH output) when closing - * a channel. + * Get a script pubkey which we will send funds to when closing a channel. * * This method should return a different value each time it is called, to avoid linking * on-chain funds across channels as controlled to the same user. */ - inline LDKPublicKey get_shutdown_pubkey(); + inline LDK::ShutdownScript get_shutdown_scriptpubkey(); /** * Get a new set of Sign for per-channel secrets. These MUST be unique even if you * restarted with some stale data! @@ -2308,6 +2343,21 @@ public: const LDKShutdown* operator &() const { return &self; } const LDKShutdown* operator ->() const { return &self; } }; +class ClosingSignedFeeRange { +private: + LDKClosingSignedFeeRange self; +public: + ClosingSignedFeeRange(const ClosingSignedFeeRange&) = delete; + ClosingSignedFeeRange(ClosingSignedFeeRange&& o) : self(o.self) { memset(&o, 0, sizeof(ClosingSignedFeeRange)); } + ClosingSignedFeeRange(LDKClosingSignedFeeRange&& m_self) : self(m_self) { memset(&m_self, 0, sizeof(LDKClosingSignedFeeRange)); } + operator LDKClosingSignedFeeRange() && { LDKClosingSignedFeeRange res = self; memset(&self, 0, sizeof(LDKClosingSignedFeeRange)); return res; } + ~ClosingSignedFeeRange() { ClosingSignedFeeRange_free(self); } + ClosingSignedFeeRange& operator=(ClosingSignedFeeRange&& o) { ClosingSignedFeeRange_free(self); self = o.self; memset(&o, 0, sizeof(ClosingSignedFeeRange)); return *this; } + LDKClosingSignedFeeRange* operator &() { return &self; } + LDKClosingSignedFeeRange* operator ->() { return &self; } + const LDKClosingSignedFeeRange* operator &() const { return &self; } + const LDKClosingSignedFeeRange* operator ->() const { return &self; } +}; class ClosingSigned { private: LDKClosingSigned self; @@ -3402,6 +3452,21 @@ public: const LDKCResult_ExpiryTimeCreationErrorZ* operator &() const { return &self; } const LDKCResult_ExpiryTimeCreationErrorZ* operator ->() const { return &self; } }; +class CResult_ClosingSignedFeeRangeDecodeErrorZ { +private: + LDKCResult_ClosingSignedFeeRangeDecodeErrorZ self; +public: + CResult_ClosingSignedFeeRangeDecodeErrorZ(const CResult_ClosingSignedFeeRangeDecodeErrorZ&) = delete; + CResult_ClosingSignedFeeRangeDecodeErrorZ(CResult_ClosingSignedFeeRangeDecodeErrorZ&& o) : self(o.self) { memset(&o, 0, sizeof(CResult_ClosingSignedFeeRangeDecodeErrorZ)); } + CResult_ClosingSignedFeeRangeDecodeErrorZ(LDKCResult_ClosingSignedFeeRangeDecodeErrorZ&& m_self) : self(m_self) { memset(&m_self, 0, sizeof(LDKCResult_ClosingSignedFeeRangeDecodeErrorZ)); } + operator LDKCResult_ClosingSignedFeeRangeDecodeErrorZ() && { LDKCResult_ClosingSignedFeeRangeDecodeErrorZ res = self; memset(&self, 0, sizeof(LDKCResult_ClosingSignedFeeRangeDecodeErrorZ)); return res; } + ~CResult_ClosingSignedFeeRangeDecodeErrorZ() { CResult_ClosingSignedFeeRangeDecodeErrorZ_free(self); } + CResult_ClosingSignedFeeRangeDecodeErrorZ& operator=(CResult_ClosingSignedFeeRangeDecodeErrorZ&& o) { CResult_ClosingSignedFeeRangeDecodeErrorZ_free(self); self = o.self; memset(&o, 0, sizeof(CResult_ClosingSignedFeeRangeDecodeErrorZ)); return *this; } + LDKCResult_ClosingSignedFeeRangeDecodeErrorZ* operator &() { return &self; } + LDKCResult_ClosingSignedFeeRangeDecodeErrorZ* operator ->() { return &self; } + const LDKCResult_ClosingSignedFeeRangeDecodeErrorZ* operator &() const { return &self; } + const LDKCResult_ClosingSignedFeeRangeDecodeErrorZ* operator ->() const { return &self; } +}; class CResult_PingDecodeErrorZ { private: LDKCResult_PingDecodeErrorZ self; @@ -3492,6 +3557,21 @@ public: const LDKCResult_SecretKeyErrorZ* operator &() const { return &self; } const LDKCResult_SecretKeyErrorZ* operator ->() const { return &self; } }; +class CResult_ShutdownScriptDecodeErrorZ { +private: + LDKCResult_ShutdownScriptDecodeErrorZ self; +public: + CResult_ShutdownScriptDecodeErrorZ(const CResult_ShutdownScriptDecodeErrorZ&) = delete; + CResult_ShutdownScriptDecodeErrorZ(CResult_ShutdownScriptDecodeErrorZ&& o) : self(o.self) { memset(&o, 0, sizeof(CResult_ShutdownScriptDecodeErrorZ)); } + CResult_ShutdownScriptDecodeErrorZ(LDKCResult_ShutdownScriptDecodeErrorZ&& m_self) : self(m_self) { memset(&m_self, 0, sizeof(LDKCResult_ShutdownScriptDecodeErrorZ)); } + operator LDKCResult_ShutdownScriptDecodeErrorZ() && { LDKCResult_ShutdownScriptDecodeErrorZ res = self; memset(&self, 0, sizeof(LDKCResult_ShutdownScriptDecodeErrorZ)); return res; } + ~CResult_ShutdownScriptDecodeErrorZ() { CResult_ShutdownScriptDecodeErrorZ_free(self); } + CResult_ShutdownScriptDecodeErrorZ& operator=(CResult_ShutdownScriptDecodeErrorZ&& o) { CResult_ShutdownScriptDecodeErrorZ_free(self); self = o.self; memset(&o, 0, sizeof(CResult_ShutdownScriptDecodeErrorZ)); return *this; } + LDKCResult_ShutdownScriptDecodeErrorZ* operator &() { return &self; } + LDKCResult_ShutdownScriptDecodeErrorZ* operator ->() { return &self; } + const LDKCResult_ShutdownScriptDecodeErrorZ* operator &() const { return &self; } + const LDKCResult_ShutdownScriptDecodeErrorZ* operator ->() const { return &self; } +}; class CResult_InvoiceNoneZ { private: LDKCResult_InvoiceNoneZ self; @@ -3717,6 +3797,21 @@ public: const LDKCResult_FundingSignedDecodeErrorZ* operator &() const { return &self; } const LDKCResult_FundingSignedDecodeErrorZ* operator ->() const { return &self; } }; +class CResult_ShutdownScriptInvalidShutdownScriptZ { +private: + LDKCResult_ShutdownScriptInvalidShutdownScriptZ self; +public: + CResult_ShutdownScriptInvalidShutdownScriptZ(const CResult_ShutdownScriptInvalidShutdownScriptZ&) = delete; + CResult_ShutdownScriptInvalidShutdownScriptZ(CResult_ShutdownScriptInvalidShutdownScriptZ&& o) : self(o.self) { memset(&o, 0, sizeof(CResult_ShutdownScriptInvalidShutdownScriptZ)); } + CResult_ShutdownScriptInvalidShutdownScriptZ(LDKCResult_ShutdownScriptInvalidShutdownScriptZ&& m_self) : self(m_self) { memset(&m_self, 0, sizeof(LDKCResult_ShutdownScriptInvalidShutdownScriptZ)); } + operator LDKCResult_ShutdownScriptInvalidShutdownScriptZ() && { LDKCResult_ShutdownScriptInvalidShutdownScriptZ res = self; memset(&self, 0, sizeof(LDKCResult_ShutdownScriptInvalidShutdownScriptZ)); return res; } + ~CResult_ShutdownScriptInvalidShutdownScriptZ() { CResult_ShutdownScriptInvalidShutdownScriptZ_free(self); } + CResult_ShutdownScriptInvalidShutdownScriptZ& operator=(CResult_ShutdownScriptInvalidShutdownScriptZ&& o) { CResult_ShutdownScriptInvalidShutdownScriptZ_free(self); self = o.self; memset(&o, 0, sizeof(CResult_ShutdownScriptInvalidShutdownScriptZ)); return *this; } + LDKCResult_ShutdownScriptInvalidShutdownScriptZ* operator &() { return &self; } + LDKCResult_ShutdownScriptInvalidShutdownScriptZ* operator ->() { return &self; } + const LDKCResult_ShutdownScriptInvalidShutdownScriptZ* operator &() const { return &self; } + const LDKCResult_ShutdownScriptInvalidShutdownScriptZ* operator ->() const { return &self; } +}; class CResult_RecoverableSignatureNoneZ { private: LDKCResult_RecoverableSignatureNoneZ self; @@ -3762,21 +3857,6 @@ public: const LDKCResult_NetAddressu8Z* operator &() const { return &self; } const LDKCResult_NetAddressu8Z* operator ->() const { return &self; } }; -class CVec_UpdateFailMalformedHTLCZ { -private: - LDKCVec_UpdateFailMalformedHTLCZ self; -public: - CVec_UpdateFailMalformedHTLCZ(const CVec_UpdateFailMalformedHTLCZ&) = delete; - CVec_UpdateFailMalformedHTLCZ(CVec_UpdateFailMalformedHTLCZ&& o) : self(o.self) { memset(&o, 0, sizeof(CVec_UpdateFailMalformedHTLCZ)); } - CVec_UpdateFailMalformedHTLCZ(LDKCVec_UpdateFailMalformedHTLCZ&& m_self) : self(m_self) { memset(&m_self, 0, sizeof(LDKCVec_UpdateFailMalformedHTLCZ)); } - operator LDKCVec_UpdateFailMalformedHTLCZ() && { LDKCVec_UpdateFailMalformedHTLCZ res = self; memset(&self, 0, sizeof(LDKCVec_UpdateFailMalformedHTLCZ)); return res; } - ~CVec_UpdateFailMalformedHTLCZ() { CVec_UpdateFailMalformedHTLCZ_free(self); } - CVec_UpdateFailMalformedHTLCZ& operator=(CVec_UpdateFailMalformedHTLCZ&& o) { CVec_UpdateFailMalformedHTLCZ_free(self); self = o.self; memset(&o, 0, sizeof(CVec_UpdateFailMalformedHTLCZ)); return *this; } - LDKCVec_UpdateFailMalformedHTLCZ* operator &() { return &self; } - LDKCVec_UpdateFailMalformedHTLCZ* operator ->() { return &self; } - const LDKCVec_UpdateFailMalformedHTLCZ* operator &() const { return &self; } - const LDKCVec_UpdateFailMalformedHTLCZ* operator ->() const { return &self; } -}; class C3Tuple_RawInvoice_u832InvoiceSignatureZ { private: LDKC3Tuple_RawInvoice_u832InvoiceSignatureZ self; @@ -3792,6 +3872,21 @@ public: const LDKC3Tuple_RawInvoice_u832InvoiceSignatureZ* operator &() const { return &self; } const LDKC3Tuple_RawInvoice_u832InvoiceSignatureZ* operator ->() const { return &self; } }; +class CVec_UpdateFailMalformedHTLCZ { +private: + LDKCVec_UpdateFailMalformedHTLCZ self; +public: + CVec_UpdateFailMalformedHTLCZ(const CVec_UpdateFailMalformedHTLCZ&) = delete; + CVec_UpdateFailMalformedHTLCZ(CVec_UpdateFailMalformedHTLCZ&& o) : self(o.self) { memset(&o, 0, sizeof(CVec_UpdateFailMalformedHTLCZ)); } + CVec_UpdateFailMalformedHTLCZ(LDKCVec_UpdateFailMalformedHTLCZ&& m_self) : self(m_self) { memset(&m_self, 0, sizeof(LDKCVec_UpdateFailMalformedHTLCZ)); } + operator LDKCVec_UpdateFailMalformedHTLCZ() && { LDKCVec_UpdateFailMalformedHTLCZ res = self; memset(&self, 0, sizeof(LDKCVec_UpdateFailMalformedHTLCZ)); return res; } + ~CVec_UpdateFailMalformedHTLCZ() { CVec_UpdateFailMalformedHTLCZ_free(self); } + CVec_UpdateFailMalformedHTLCZ& operator=(CVec_UpdateFailMalformedHTLCZ&& o) { CVec_UpdateFailMalformedHTLCZ_free(self); self = o.self; memset(&o, 0, sizeof(CVec_UpdateFailMalformedHTLCZ)); return *this; } + LDKCVec_UpdateFailMalformedHTLCZ* operator &() { return &self; } + LDKCVec_UpdateFailMalformedHTLCZ* operator ->() { return &self; } + const LDKCVec_UpdateFailMalformedHTLCZ* operator &() const { return &self; } + const LDKCVec_UpdateFailMalformedHTLCZ* operator ->() const { return &self; } +}; class CResult_NetworkGraphDecodeErrorZ { private: LDKCResult_NetworkGraphDecodeErrorZ self; @@ -4572,21 +4667,6 @@ public: const LDKCResult_UpdateAddHTLCDecodeErrorZ* operator &() const { return &self; } const LDKCResult_UpdateAddHTLCDecodeErrorZ* operator ->() const { return &self; } }; -class CResult_NoneAPIErrorZ { -private: - LDKCResult_NoneAPIErrorZ self; -public: - CResult_NoneAPIErrorZ(const CResult_NoneAPIErrorZ&) = delete; - CResult_NoneAPIErrorZ(CResult_NoneAPIErrorZ&& o) : self(o.self) { memset(&o, 0, sizeof(CResult_NoneAPIErrorZ)); } - CResult_NoneAPIErrorZ(LDKCResult_NoneAPIErrorZ&& m_self) : self(m_self) { memset(&m_self, 0, sizeof(LDKCResult_NoneAPIErrorZ)); } - operator LDKCResult_NoneAPIErrorZ() && { LDKCResult_NoneAPIErrorZ res = self; memset(&self, 0, sizeof(LDKCResult_NoneAPIErrorZ)); return res; } - ~CResult_NoneAPIErrorZ() { CResult_NoneAPIErrorZ_free(self); } - CResult_NoneAPIErrorZ& operator=(CResult_NoneAPIErrorZ&& o) { CResult_NoneAPIErrorZ_free(self); self = o.self; memset(&o, 0, sizeof(CResult_NoneAPIErrorZ)); return *this; } - LDKCResult_NoneAPIErrorZ* operator &() { return &self; } - LDKCResult_NoneAPIErrorZ* operator ->() { return &self; } - const LDKCResult_NoneAPIErrorZ* operator &() const { return &self; } - const LDKCResult_NoneAPIErrorZ* operator ->() const { return &self; } -}; class CResult_CounterpartyChannelTransactionParametersDecodeErrorZ { private: LDKCResult_CounterpartyChannelTransactionParametersDecodeErrorZ self; @@ -4602,6 +4682,21 @@ public: const LDKCResult_CounterpartyChannelTransactionParametersDecodeErrorZ* operator &() const { return &self; } const LDKCResult_CounterpartyChannelTransactionParametersDecodeErrorZ* operator ->() const { return &self; } }; +class CResult_NoneAPIErrorZ { +private: + LDKCResult_NoneAPIErrorZ self; +public: + CResult_NoneAPIErrorZ(const CResult_NoneAPIErrorZ&) = delete; + CResult_NoneAPIErrorZ(CResult_NoneAPIErrorZ&& o) : self(o.self) { memset(&o, 0, sizeof(CResult_NoneAPIErrorZ)); } + CResult_NoneAPIErrorZ(LDKCResult_NoneAPIErrorZ&& m_self) : self(m_self) { memset(&m_self, 0, sizeof(LDKCResult_NoneAPIErrorZ)); } + operator LDKCResult_NoneAPIErrorZ() && { LDKCResult_NoneAPIErrorZ res = self; memset(&self, 0, sizeof(LDKCResult_NoneAPIErrorZ)); return res; } + ~CResult_NoneAPIErrorZ() { CResult_NoneAPIErrorZ_free(self); } + CResult_NoneAPIErrorZ& operator=(CResult_NoneAPIErrorZ&& o) { CResult_NoneAPIErrorZ_free(self); self = o.self; memset(&o, 0, sizeof(CResult_NoneAPIErrorZ)); return *this; } + LDKCResult_NoneAPIErrorZ* operator &() { return &self; } + LDKCResult_NoneAPIErrorZ* operator ->() { return &self; } + const LDKCResult_NoneAPIErrorZ* operator &() const { return &self; } + const LDKCResult_NoneAPIErrorZ* operator ->() const { return &self; } +}; class CVec_NetAddressZ { private: LDKCVec_NetAddressZ self; @@ -5317,8 +5412,8 @@ inline LDK::CVec_u8Z KeysInterface::get_destination_script() { LDK::CVec_u8Z ret = (self.get_destination_script)(self.this_arg); return ret; } -inline LDKPublicKey KeysInterface::get_shutdown_pubkey() { - LDKPublicKey ret = (self.get_shutdown_pubkey)(self.this_arg); +inline LDK::ShutdownScript KeysInterface::get_shutdown_scriptpubkey() { + LDK::ShutdownScript ret = (self.get_shutdown_scriptpubkey)(self.this_arg); return ret; } inline LDK::Sign KeysInterface::get_channel_signer(bool inbound, uint64_t channel_value_satoshis) { diff --git a/lightning-c-bindings/src/c_types/derived.rs b/lightning-c-bindings/src/c_types/derived.rs index fef1837..c9f3123 100644 --- a/lightning-c-bindings/src/c_types/derived.rs +++ b/lightning-c-bindings/src/c_types/derived.rs @@ -1220,6 +1220,171 @@ impl Clone for CResult_CVec_SignatureZNoneZ { /// but with all dynamically-allocated buffers duplicated in new buffers. pub extern "C" fn CResult_CVec_SignatureZNoneZ_clone(orig: &CResult_CVec_SignatureZNoneZ) -> CResult_CVec_SignatureZNoneZ { orig.clone() } #[repr(C)] +/// The contents of CResult_ShutdownScriptDecodeErrorZ +pub union CResult_ShutdownScriptDecodeErrorZPtr { + /// A pointer to the contents in the success state. + /// Reading from this pointer when `result_ok` is not set is undefined. + pub result: *mut crate::lightning::ln::script::ShutdownScript, + /// A pointer to the contents in the error state. + /// Reading from this pointer when `result_ok` is set is undefined. + pub err: *mut crate::lightning::ln::msgs::DecodeError, +} +#[repr(C)] +/// A CResult_ShutdownScriptDecodeErrorZ represents the result of a fallible operation, +/// containing a crate::lightning::ln::script::ShutdownScript on success and a crate::lightning::ln::msgs::DecodeError on failure. +/// `result_ok` indicates the overall state, and the contents are provided via `contents`. +pub struct CResult_ShutdownScriptDecodeErrorZ { + /// The contents of this CResult_ShutdownScriptDecodeErrorZ, accessible via either + /// `err` or `result` depending on the state of `result_ok`. + pub contents: CResult_ShutdownScriptDecodeErrorZPtr, + /// Whether this CResult_ShutdownScriptDecodeErrorZ represents a success state. + pub result_ok: bool, +} +#[no_mangle] +/// Creates a new CResult_ShutdownScriptDecodeErrorZ in the success state. +pub extern "C" fn CResult_ShutdownScriptDecodeErrorZ_ok(o: crate::lightning::ln::script::ShutdownScript) -> CResult_ShutdownScriptDecodeErrorZ { + CResult_ShutdownScriptDecodeErrorZ { + contents: CResult_ShutdownScriptDecodeErrorZPtr { + result: Box::into_raw(Box::new(o)), + }, + result_ok: true, + } +} +#[no_mangle] +/// Creates a new CResult_ShutdownScriptDecodeErrorZ in the error state. +pub extern "C" fn CResult_ShutdownScriptDecodeErrorZ_err(e: crate::lightning::ln::msgs::DecodeError) -> CResult_ShutdownScriptDecodeErrorZ { + CResult_ShutdownScriptDecodeErrorZ { + contents: CResult_ShutdownScriptDecodeErrorZPtr { + err: Box::into_raw(Box::new(e)), + }, + result_ok: false, + } +} +#[no_mangle] +/// Frees any resources used by the CResult_ShutdownScriptDecodeErrorZ. +pub extern "C" fn CResult_ShutdownScriptDecodeErrorZ_free(_res: CResult_ShutdownScriptDecodeErrorZ) { } +impl Drop for CResult_ShutdownScriptDecodeErrorZ { + fn drop(&mut self) { + if self.result_ok { + if unsafe { !(self.contents.result as *mut ()).is_null() } { + let _ = unsafe { Box::from_raw(self.contents.result) }; + } + } else { + if unsafe { !(self.contents.err as *mut ()).is_null() } { + let _ = unsafe { Box::from_raw(self.contents.err) }; + } + } + } +} +impl From> for CResult_ShutdownScriptDecodeErrorZ { + fn from(mut o: crate::c_types::CResultTempl) -> Self { + let contents = if o.result_ok { + let result = unsafe { o.contents.result }; + unsafe { o.contents.result = std::ptr::null_mut() }; + CResult_ShutdownScriptDecodeErrorZPtr { result } + } else { + let err = unsafe { o.contents.err }; + unsafe { o.contents.err = std::ptr::null_mut(); } + CResult_ShutdownScriptDecodeErrorZPtr { err } + }; + Self { + contents, + result_ok: o.result_ok, + } + } +} +impl Clone for CResult_ShutdownScriptDecodeErrorZ { + fn clone(&self) -> Self { + if self.result_ok { + Self { result_ok: true, contents: CResult_ShutdownScriptDecodeErrorZPtr { + result: Box::into_raw(Box::new(::clone(unsafe { &*self.contents.result }))) + } } + } else { + Self { result_ok: false, contents: CResult_ShutdownScriptDecodeErrorZPtr { + err: Box::into_raw(Box::new(::clone(unsafe { &*self.contents.err }))) + } } + } + } +} +#[no_mangle] +/// Creates a new CResult_ShutdownScriptDecodeErrorZ which has the same data as `orig` +/// but with all dynamically-allocated buffers duplicated in new buffers. +pub extern "C" fn CResult_ShutdownScriptDecodeErrorZ_clone(orig: &CResult_ShutdownScriptDecodeErrorZ) -> CResult_ShutdownScriptDecodeErrorZ { orig.clone() } +#[repr(C)] +/// The contents of CResult_ShutdownScriptInvalidShutdownScriptZ +pub union CResult_ShutdownScriptInvalidShutdownScriptZPtr { + /// A pointer to the contents in the success state. + /// Reading from this pointer when `result_ok` is not set is undefined. + pub result: *mut crate::lightning::ln::script::ShutdownScript, + /// A pointer to the contents in the error state. + /// Reading from this pointer when `result_ok` is set is undefined. + pub err: *mut crate::lightning::ln::script::InvalidShutdownScript, +} +#[repr(C)] +/// A CResult_ShutdownScriptInvalidShutdownScriptZ represents the result of a fallible operation, +/// containing a crate::lightning::ln::script::ShutdownScript on success and a crate::lightning::ln::script::InvalidShutdownScript on failure. +/// `result_ok` indicates the overall state, and the contents are provided via `contents`. +pub struct CResult_ShutdownScriptInvalidShutdownScriptZ { + /// The contents of this CResult_ShutdownScriptInvalidShutdownScriptZ, accessible via either + /// `err` or `result` depending on the state of `result_ok`. + pub contents: CResult_ShutdownScriptInvalidShutdownScriptZPtr, + /// Whether this CResult_ShutdownScriptInvalidShutdownScriptZ represents a success state. + pub result_ok: bool, +} +#[no_mangle] +/// Creates a new CResult_ShutdownScriptInvalidShutdownScriptZ in the success state. +pub extern "C" fn CResult_ShutdownScriptInvalidShutdownScriptZ_ok(o: crate::lightning::ln::script::ShutdownScript) -> CResult_ShutdownScriptInvalidShutdownScriptZ { + CResult_ShutdownScriptInvalidShutdownScriptZ { + contents: CResult_ShutdownScriptInvalidShutdownScriptZPtr { + result: Box::into_raw(Box::new(o)), + }, + result_ok: true, + } +} +#[no_mangle] +/// Creates a new CResult_ShutdownScriptInvalidShutdownScriptZ in the error state. +pub extern "C" fn CResult_ShutdownScriptInvalidShutdownScriptZ_err(e: crate::lightning::ln::script::InvalidShutdownScript) -> CResult_ShutdownScriptInvalidShutdownScriptZ { + CResult_ShutdownScriptInvalidShutdownScriptZ { + contents: CResult_ShutdownScriptInvalidShutdownScriptZPtr { + err: Box::into_raw(Box::new(e)), + }, + result_ok: false, + } +} +#[no_mangle] +/// Frees any resources used by the CResult_ShutdownScriptInvalidShutdownScriptZ. +pub extern "C" fn CResult_ShutdownScriptInvalidShutdownScriptZ_free(_res: CResult_ShutdownScriptInvalidShutdownScriptZ) { } +impl Drop for CResult_ShutdownScriptInvalidShutdownScriptZ { + fn drop(&mut self) { + if self.result_ok { + if unsafe { !(self.contents.result as *mut ()).is_null() } { + let _ = unsafe { Box::from_raw(self.contents.result) }; + } + } else { + if unsafe { !(self.contents.err as *mut ()).is_null() } { + let _ = unsafe { Box::from_raw(self.contents.err) }; + } + } + } +} +impl From> for CResult_ShutdownScriptInvalidShutdownScriptZ { + fn from(mut o: crate::c_types::CResultTempl) -> Self { + let contents = if o.result_ok { + let result = unsafe { o.contents.result }; + unsafe { o.contents.result = std::ptr::null_mut() }; + CResult_ShutdownScriptInvalidShutdownScriptZPtr { result } + } else { + let err = unsafe { o.contents.err }; + unsafe { o.contents.err = std::ptr::null_mut(); } + CResult_ShutdownScriptInvalidShutdownScriptZPtr { err } + }; + Self { + contents, + result_ok: o.result_ok, + } + } +} +#[repr(C)] /// The contents of CResult_NoneErrorZ pub union CResult_NoneErrorZPtr { /// Note that this value is always NULL, as there are no contents in the OK variant @@ -8617,6 +8782,97 @@ impl Clone for CResult_ClosingSignedDecodeErrorZ { /// but with all dynamically-allocated buffers duplicated in new buffers. pub extern "C" fn CResult_ClosingSignedDecodeErrorZ_clone(orig: &CResult_ClosingSignedDecodeErrorZ) -> CResult_ClosingSignedDecodeErrorZ { orig.clone() } #[repr(C)] +/// The contents of CResult_ClosingSignedFeeRangeDecodeErrorZ +pub union CResult_ClosingSignedFeeRangeDecodeErrorZPtr { + /// A pointer to the contents in the success state. + /// Reading from this pointer when `result_ok` is not set is undefined. + pub result: *mut crate::lightning::ln::msgs::ClosingSignedFeeRange, + /// A pointer to the contents in the error state. + /// Reading from this pointer when `result_ok` is set is undefined. + pub err: *mut crate::lightning::ln::msgs::DecodeError, +} +#[repr(C)] +/// A CResult_ClosingSignedFeeRangeDecodeErrorZ represents the result of a fallible operation, +/// containing a crate::lightning::ln::msgs::ClosingSignedFeeRange on success and a crate::lightning::ln::msgs::DecodeError on failure. +/// `result_ok` indicates the overall state, and the contents are provided via `contents`. +pub struct CResult_ClosingSignedFeeRangeDecodeErrorZ { + /// The contents of this CResult_ClosingSignedFeeRangeDecodeErrorZ, accessible via either + /// `err` or `result` depending on the state of `result_ok`. + pub contents: CResult_ClosingSignedFeeRangeDecodeErrorZPtr, + /// Whether this CResult_ClosingSignedFeeRangeDecodeErrorZ represents a success state. + pub result_ok: bool, +} +#[no_mangle] +/// Creates a new CResult_ClosingSignedFeeRangeDecodeErrorZ in the success state. +pub extern "C" fn CResult_ClosingSignedFeeRangeDecodeErrorZ_ok(o: crate::lightning::ln::msgs::ClosingSignedFeeRange) -> CResult_ClosingSignedFeeRangeDecodeErrorZ { + CResult_ClosingSignedFeeRangeDecodeErrorZ { + contents: CResult_ClosingSignedFeeRangeDecodeErrorZPtr { + result: Box::into_raw(Box::new(o)), + }, + result_ok: true, + } +} +#[no_mangle] +/// Creates a new CResult_ClosingSignedFeeRangeDecodeErrorZ in the error state. +pub extern "C" fn CResult_ClosingSignedFeeRangeDecodeErrorZ_err(e: crate::lightning::ln::msgs::DecodeError) -> CResult_ClosingSignedFeeRangeDecodeErrorZ { + CResult_ClosingSignedFeeRangeDecodeErrorZ { + contents: CResult_ClosingSignedFeeRangeDecodeErrorZPtr { + err: Box::into_raw(Box::new(e)), + }, + result_ok: false, + } +} +#[no_mangle] +/// Frees any resources used by the CResult_ClosingSignedFeeRangeDecodeErrorZ. +pub extern "C" fn CResult_ClosingSignedFeeRangeDecodeErrorZ_free(_res: CResult_ClosingSignedFeeRangeDecodeErrorZ) { } +impl Drop for CResult_ClosingSignedFeeRangeDecodeErrorZ { + fn drop(&mut self) { + if self.result_ok { + if unsafe { !(self.contents.result as *mut ()).is_null() } { + let _ = unsafe { Box::from_raw(self.contents.result) }; + } + } else { + if unsafe { !(self.contents.err as *mut ()).is_null() } { + let _ = unsafe { Box::from_raw(self.contents.err) }; + } + } + } +} +impl From> for CResult_ClosingSignedFeeRangeDecodeErrorZ { + fn from(mut o: crate::c_types::CResultTempl) -> Self { + let contents = if o.result_ok { + let result = unsafe { o.contents.result }; + unsafe { o.contents.result = std::ptr::null_mut() }; + CResult_ClosingSignedFeeRangeDecodeErrorZPtr { result } + } else { + let err = unsafe { o.contents.err }; + unsafe { o.contents.err = std::ptr::null_mut(); } + CResult_ClosingSignedFeeRangeDecodeErrorZPtr { err } + }; + Self { + contents, + result_ok: o.result_ok, + } + } +} +impl Clone for CResult_ClosingSignedFeeRangeDecodeErrorZ { + fn clone(&self) -> Self { + if self.result_ok { + Self { result_ok: true, contents: CResult_ClosingSignedFeeRangeDecodeErrorZPtr { + result: Box::into_raw(Box::new(::clone(unsafe { &*self.contents.result }))) + } } + } else { + Self { result_ok: false, contents: CResult_ClosingSignedFeeRangeDecodeErrorZPtr { + err: Box::into_raw(Box::new(::clone(unsafe { &*self.contents.err }))) + } } + } + } +} +#[no_mangle] +/// Creates a new CResult_ClosingSignedFeeRangeDecodeErrorZ which has the same data as `orig` +/// but with all dynamically-allocated buffers duplicated in new buffers. +pub extern "C" fn CResult_ClosingSignedFeeRangeDecodeErrorZ_clone(orig: &CResult_ClosingSignedFeeRangeDecodeErrorZ) -> CResult_ClosingSignedFeeRangeDecodeErrorZ { orig.clone() } +#[repr(C)] /// The contents of CResult_CommitmentSignedDecodeErrorZ pub union CResult_CommitmentSignedDecodeErrorZPtr { /// A pointer to the contents in the success state. diff --git a/lightning-c-bindings/src/lightning/chain/chaininterface.rs b/lightning-c-bindings/src/lightning/chain/chaininterface.rs index 9ba2256..bd62870 100644 --- a/lightning-c-bindings/src/lightning/chain/chaininterface.rs +++ b/lightning-c-bindings/src/lightning/chain/chaininterface.rs @@ -130,6 +130,12 @@ pub extern "C" fn ConfirmationTarget_normal() -> ConfirmationTarget { /// Utility method to constructs a new HighPriority-variant ConfirmationTarget pub extern "C" fn ConfirmationTarget_high_priority() -> ConfirmationTarget { ConfirmationTarget::HighPriority} +/// Checks if two ConfirmationTargets contain equal inner contents. +/// This ignores pointers and is_owned flags and looks at the values in fields. +#[no_mangle] +pub extern "C" fn ConfirmationTarget_eq(a: &ConfirmationTarget, b: &ConfirmationTarget) -> bool { + if &a.to_native() == &b.to_native() { true } else { false } +} /// A trait which should be implemented to provide feerate information on a number of time /// horizons. /// diff --git a/lightning-c-bindings/src/lightning/chain/keysinterface.rs b/lightning-c-bindings/src/lightning/chain/keysinterface.rs index 812d51a..1f542b4 100644 --- a/lightning-c-bindings/src/lightning/chain/keysinterface.rs +++ b/lightning-c-bindings/src/lightning/chain/keysinterface.rs @@ -333,8 +333,8 @@ pub extern "C" fn StaticPaymentOutputDescriptor_read(ser: crate::c_types::u8slic #[repr(C)] pub enum SpendableOutputDescriptor { /// An output to a script which was provided via KeysInterface directly, either from - /// `get_destination_script()` or `get_shutdown_pubkey()`, thus you should already know how to - /// spend it. No secret keys are provided as rust-lightning was never given any key. + /// `get_destination_script()` or `get_shutdown_scriptpubkey()`, thus you should already know + /// how to spend it. No secret keys are provided as rust-lightning was never given any key. /// These may include outputs from a transaction punishing our counterparty or claiming an HTLC /// on-chain using the payment preimage or after it has timed out. StaticOutput { @@ -914,13 +914,12 @@ pub struct KeysInterface { /// on-chain funds across channels as controlled to the same user. #[must_use] pub get_destination_script: extern "C" fn (this_arg: *const c_void) -> crate::c_types::derived::CVec_u8Z, - /// Get a public key which we will send funds to (in the form of a P2WPKH output) when closing - /// a channel. + /// Get a script pubkey which we will send funds to when closing a channel. /// /// This method should return a different value each time it is called, to avoid linking /// on-chain funds across channels as controlled to the same user. #[must_use] - pub get_shutdown_pubkey: extern "C" fn (this_arg: *const c_void) -> crate::c_types::PublicKey, + pub get_shutdown_scriptpubkey: extern "C" fn (this_arg: *const c_void) -> crate::lightning::ln::script::ShutdownScript, /// Get a new set of Sign for per-channel secrets. These MUST be unique even if you /// restarted with some stale data! /// @@ -960,7 +959,7 @@ pub(crate) extern "C" fn KeysInterface_clone_fields(orig: &KeysInterface) -> Key this_arg: orig.this_arg, get_node_secret: Clone::clone(&orig.get_node_secret), get_destination_script: Clone::clone(&orig.get_destination_script), - get_shutdown_pubkey: Clone::clone(&orig.get_shutdown_pubkey), + get_shutdown_scriptpubkey: Clone::clone(&orig.get_shutdown_scriptpubkey), get_channel_signer: Clone::clone(&orig.get_channel_signer), get_secure_random_bytes: Clone::clone(&orig.get_secure_random_bytes), read_chan_signer: Clone::clone(&orig.read_chan_signer), @@ -980,9 +979,9 @@ impl rustKeysInterface for KeysInterface { let mut ret = (self.get_destination_script)(self.this_arg); ::bitcoin::blockdata::script::Script::from(ret.into_rust()) } - fn get_shutdown_pubkey(&self) -> bitcoin::secp256k1::key::PublicKey { - let mut ret = (self.get_shutdown_pubkey)(self.this_arg); - ret.into_rust() + fn get_shutdown_scriptpubkey(&self) -> lightning::ln::script::ShutdownScript { + let mut ret = (self.get_shutdown_scriptpubkey)(self.this_arg); + *unsafe { Box::from_raw(ret.take_inner()) } } fn get_channel_signer(&self, mut inbound: bool, mut channel_value_satoshis: u64) -> crate::lightning::chain::keysinterface::Sign { let mut ret = (self.get_channel_signer)(self.this_arg, inbound, channel_value_satoshis); @@ -1551,7 +1550,7 @@ pub extern "C" fn KeysManager_as_KeysInterface(this_arg: &KeysManager) -> crate: free: None, get_node_secret: KeysManager_KeysInterface_get_node_secret, get_destination_script: KeysManager_KeysInterface_get_destination_script, - get_shutdown_pubkey: KeysManager_KeysInterface_get_shutdown_pubkey, + get_shutdown_scriptpubkey: KeysManager_KeysInterface_get_shutdown_scriptpubkey, get_channel_signer: KeysManager_KeysInterface_get_channel_signer, get_secure_random_bytes: KeysManager_KeysInterface_get_secure_random_bytes, read_chan_signer: KeysManager_KeysInterface_read_chan_signer, @@ -1570,9 +1569,9 @@ extern "C" fn KeysManager_KeysInterface_get_destination_script(this_arg: *const ret.into_bytes().into() } #[must_use] -extern "C" fn KeysManager_KeysInterface_get_shutdown_pubkey(this_arg: *const c_void) -> crate::c_types::PublicKey { - let mut ret = >::get_shutdown_pubkey(unsafe { &mut *(this_arg as *mut nativeKeysManager) }, ); - crate::c_types::PublicKey::from_rust(&ret) +extern "C" fn KeysManager_KeysInterface_get_shutdown_scriptpubkey(this_arg: *const c_void) -> crate::lightning::ln::script::ShutdownScript { + let mut ret = >::get_shutdown_scriptpubkey(unsafe { &mut *(this_arg as *mut nativeKeysManager) }, ); + crate::lightning::ln::script::ShutdownScript { inner: Box::into_raw(Box::new(ret)), is_owned: true } } #[must_use] extern "C" fn KeysManager_KeysInterface_get_channel_signer(this_arg: *const c_void, mut _inbound: bool, mut channel_value_satoshis: u64) -> crate::lightning::chain::keysinterface::Sign { diff --git a/lightning-c-bindings/src/lightning/ln/channelmanager.rs b/lightning-c-bindings/src/lightning/ln/channelmanager.rs index afdcaa9..0f5d3cc 100644 --- a/lightning-c-bindings/src/lightning/ln/channelmanager.rs +++ b/lightning-c-bindings/src/lightning/ln/channelmanager.rs @@ -996,7 +996,19 @@ pub extern "C" fn ChannelManager_list_usable_channels(this_arg: &ChannelManager) /// will be accepted on the given channel, and after additional timeout/the closing of all /// pending HTLCs, the channel will be closed on chain. /// +/// * If we are the channel initiator, we will pay between our [`Background`] and +/// [`ChannelConfig::force_close_avoidance_max_fee_satoshis`] plus our [`Normal`] fee +/// estimate. +/// * If our counterparty is the channel initiator, we will require a channel closing +/// transaction feerate of at least our [`Background`] feerate or the feerate which +/// would appear on a force-closure transaction, whichever is lower. We will allow our +/// counterparty to pay as much fee as they'd like, however. +/// /// May generate a SendShutdown message event on success, which should be relayed. +/// +/// [`ChannelConfig::force_close_avoidance_max_fee_satoshis`]: crate::util::config::ChannelConfig::force_close_avoidance_max_fee_satoshis +/// [`Background`]: crate::chain::chaininterface::ConfirmationTarget::Background +/// [`Normal`]: crate::chain::chaininterface::ConfirmationTarget::Normal #[must_use] #[no_mangle] pub extern "C" fn ChannelManager_close_channel(this_arg: &ChannelManager, channel_id: *const [u8; 32]) -> crate::c_types::derived::CResult_NoneAPIErrorZ { @@ -1005,6 +1017,33 @@ pub extern "C" fn ChannelManager_close_channel(this_arg: &ChannelManager, channe local_ret } +/// Begins the process of closing a channel. After this call (plus some timeout), no new HTLCs +/// will be accepted on the given channel, and after additional timeout/the closing of all +/// pending HTLCs, the channel will be closed on chain. +/// +/// `target_feerate_sat_per_1000_weight` has different meanings depending on if we initiated +/// the channel being closed or not: +/// * If we are the channel initiator, we will pay at least this feerate on the closing +/// transaction. The upper-bound is set by +/// [`ChannelConfig::force_close_avoidance_max_fee_satoshis`] plus our [`Normal`] fee +/// estimate (or `target_feerate_sat_per_1000_weight`, if it is greater). +/// * If our counterparty is the channel initiator, we will refuse to accept a channel closure +/// transaction feerate below `target_feerate_sat_per_1000_weight` (or the feerate which +/// will appear on a force-closure transaction, whichever is lower). +/// +/// May generate a SendShutdown message event on success, which should be relayed. +/// +/// [`ChannelConfig::force_close_avoidance_max_fee_satoshis`]: crate::util::config::ChannelConfig::force_close_avoidance_max_fee_satoshis +/// [`Background`]: crate::chain::chaininterface::ConfirmationTarget::Background +/// [`Normal`]: crate::chain::chaininterface::ConfirmationTarget::Normal +#[must_use] +#[no_mangle] +pub extern "C" fn ChannelManager_close_channel_with_target_feerate(this_arg: &ChannelManager, channel_id: *const [u8; 32], mut target_feerate_sats_per_1000_weight: u32) -> crate::c_types::derived::CResult_NoneAPIErrorZ { + let mut ret = unsafe { &*this_arg.inner }.close_channel_with_target_feerate(unsafe { &*channel_id}, target_feerate_sats_per_1000_weight); + let mut local_ret = match ret { Ok(mut o) => crate::c_types::CResultTempl::ok( { () /*o*/ }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { crate::lightning::util::errors::APIError::native_into(e) }).into() }; + local_ret +} + /// Force closes a channel, immediately broadcasting the latest local commitment transaction to /// the chain and rejecting new HTLCs on the given channel. Fails if channel_id is unknown to the manager. #[must_use] @@ -1078,9 +1117,13 @@ pub extern "C" fn ChannelManager_send_payment(this_arg: &ChannelManager, route: /// would be able to guess -- otherwise, an intermediate node may claim the payment and it will /// never reach the recipient. /// +/// See [`send_payment`] documentation for more details on the return value of this function. +/// /// Similar to regular payments, you MUST NOT reuse a `payment_preimage` value. See /// [`send_payment`] for more information about the risks of duplicate preimage usage. /// +/// Note that `route` must have exactly one path. +/// /// [`send_payment`]: Self::send_payment /// /// Note that payment_preimage (or a relevant inner pointer) may be NULL or all-0s to represent None @@ -1154,13 +1197,16 @@ pub extern "C" fn ChannelManager_process_pending_htlc_forwards(this_arg: &Channe unsafe { &*this_arg.inner }.process_pending_htlc_forwards() } -/// If a peer is disconnected we mark any channels with that peer as 'disabled'. -/// After some time, if channels are still disabled we need to broadcast a ChannelUpdate -/// to inform the network about the uselessness of these channels. +/// Performs actions which should happen on startup and roughly once per minute thereafter. /// -/// This method handles all the details, and must be called roughly once per minute. +/// This currently includes: +/// * Increasing or decreasing the on-chain feerate estimates for our outbound channels, +/// * Broadcasting `ChannelUpdate` messages if we've been disconnected from our peer for more +/// than a minute, informing the network that they should no longer attempt to route over +/// the channel. /// -/// Note that in some rare cases this may generate a `chain::Watch::update_channel` call. +/// Note that this may cause reentrancy through `chain::Watch::update_channel` calls or feerate +/// estimate fetches. #[no_mangle] pub extern "C" fn ChannelManager_timer_tick_occurred(this_arg: &ChannelManager) { unsafe { &*this_arg.inner }.timer_tick_occurred() diff --git a/lightning-c-bindings/src/lightning/ln/mod.rs b/lightning-c-bindings/src/lightning/ln/mod.rs index abc0e0e..89ee33b 100644 --- a/lightning-c-bindings/src/lightning/ln/mod.rs +++ b/lightning-c-bindings/src/lightning/ln/mod.rs @@ -27,6 +27,7 @@ pub mod msgs; pub mod peer_handler; pub mod chan_utils; pub mod features; +pub mod script; mod peer_channel_encryptor { use std::str::FromStr; diff --git a/lightning-c-bindings/src/lightning/ln/msgs.rs b/lightning-c-bindings/src/lightning/ln/msgs.rs index 619a41d..cdc862d 100644 --- a/lightning-c-bindings/src/lightning/ln/msgs.rs +++ b/lightning-c-bindings/src/lightning/ln/msgs.rs @@ -1345,6 +1345,107 @@ pub extern "C" fn Shutdown_clone(orig: &Shutdown) -> Shutdown { orig.clone() } +use lightning::ln::msgs::ClosingSignedFeeRange as nativeClosingSignedFeeRangeImport; +type nativeClosingSignedFeeRange = nativeClosingSignedFeeRangeImport; + +/// The minimum and maximum fees which the sender is willing to place on the closing transaction. +/// This is provided in [`ClosingSigned`] by both sides to indicate the fee range they are willing +/// to use. +#[must_use] +#[repr(C)] +pub struct ClosingSignedFeeRange { + /// A pointer to the opaque Rust object. + + /// Nearly everywhere, inner must be non-null, however in places where + /// the Rust equivalent takes an Option, it may be set to null to indicate None. + pub inner: *mut nativeClosingSignedFeeRange, + /// Indicates that this is the only struct which contains the same pointer. + + /// Rust functions which take ownership of an object provided via an argument require + /// this to be true and invalidate the object pointed to by inner. + pub is_owned: bool, +} + +impl Drop for ClosingSignedFeeRange { + fn drop(&mut self) { + if self.is_owned && !<*mut nativeClosingSignedFeeRange>::is_null(self.inner) { + let _ = unsafe { Box::from_raw(self.inner) }; + } + } +} +/// Frees any resources used by the ClosingSignedFeeRange, if is_owned is set and inner is non-NULL. +#[no_mangle] +pub extern "C" fn ClosingSignedFeeRange_free(this_obj: ClosingSignedFeeRange) { } +#[allow(unused)] +/// Used only if an object of this type is returned as a trait impl by a method +extern "C" fn ClosingSignedFeeRange_free_void(this_ptr: *mut c_void) { + unsafe { let _ = Box::from_raw(this_ptr as *mut nativeClosingSignedFeeRange); } +} +#[allow(unused)] +/// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy +impl ClosingSignedFeeRange { + pub(crate) fn take_inner(mut self) -> *mut nativeClosingSignedFeeRange { + assert!(self.is_owned); + let ret = self.inner; + self.inner = std::ptr::null_mut(); + ret + } +} +/// The minimum absolute fee, in satoshis, which the sender is willing to place on the closing +/// transaction. +#[no_mangle] +pub extern "C" fn ClosingSignedFeeRange_get_min_fee_satoshis(this_ptr: &ClosingSignedFeeRange) -> u64 { + let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.min_fee_satoshis; + *inner_val +} +/// The minimum absolute fee, in satoshis, which the sender is willing to place on the closing +/// transaction. +#[no_mangle] +pub extern "C" fn ClosingSignedFeeRange_set_min_fee_satoshis(this_ptr: &mut ClosingSignedFeeRange, mut val: u64) { + unsafe { &mut *this_ptr.inner }.min_fee_satoshis = val; +} +/// The maximum absolute fee, in satoshis, which the sender is willing to place on the closing +/// transaction. +#[no_mangle] +pub extern "C" fn ClosingSignedFeeRange_get_max_fee_satoshis(this_ptr: &ClosingSignedFeeRange) -> u64 { + let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.max_fee_satoshis; + *inner_val +} +/// The maximum absolute fee, in satoshis, which the sender is willing to place on the closing +/// transaction. +#[no_mangle] +pub extern "C" fn ClosingSignedFeeRange_set_max_fee_satoshis(this_ptr: &mut ClosingSignedFeeRange, mut val: u64) { + unsafe { &mut *this_ptr.inner }.max_fee_satoshis = val; +} +/// Constructs a new ClosingSignedFeeRange given each field +#[must_use] +#[no_mangle] +pub extern "C" fn ClosingSignedFeeRange_new(mut min_fee_satoshis_arg: u64, mut max_fee_satoshis_arg: u64) -> ClosingSignedFeeRange { + ClosingSignedFeeRange { inner: Box::into_raw(Box::new(nativeClosingSignedFeeRange { + min_fee_satoshis: min_fee_satoshis_arg, + max_fee_satoshis: max_fee_satoshis_arg, + })), is_owned: true } +} +impl Clone for ClosingSignedFeeRange { + fn clone(&self) -> Self { + Self { + inner: if <*mut nativeClosingSignedFeeRange>::is_null(self.inner) { std::ptr::null_mut() } else { + Box::into_raw(Box::new(unsafe { &*self.inner }.clone())) }, + is_owned: true, + } + } +} +#[allow(unused)] +/// Used only if an object of this type is returned as a trait impl by a method +pub(crate) extern "C" fn ClosingSignedFeeRange_clone_void(this_ptr: *const c_void) -> *mut c_void { + Box::into_raw(Box::new(unsafe { (*(this_ptr as *mut nativeClosingSignedFeeRange)).clone() })) as *mut c_void +} +#[no_mangle] +/// Creates a copy of the ClosingSignedFeeRange +pub extern "C" fn ClosingSignedFeeRange_clone(orig: &ClosingSignedFeeRange) -> ClosingSignedFeeRange { + orig.clone() +} + use lightning::ln::msgs::ClosingSigned as nativeClosingSignedImport; type nativeClosingSigned = nativeClosingSignedImport; @@ -1422,14 +1523,35 @@ pub extern "C" fn ClosingSigned_get_signature(this_ptr: &ClosingSigned) -> crate pub extern "C" fn ClosingSigned_set_signature(this_ptr: &mut ClosingSigned, mut val: crate::c_types::Signature) { unsafe { &mut *this_ptr.inner }.signature = val.into_rust(); } +/// The minimum and maximum fees which the sender is willing to accept, provided only by new +/// nodes. +/// +/// Note that the return value (or a relevant inner pointer) may be NULL or all-0s to represent None +#[no_mangle] +pub extern "C" fn ClosingSigned_get_fee_range(this_ptr: &ClosingSigned) -> crate::lightning::ln::msgs::ClosingSignedFeeRange { + let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.fee_range; + let mut local_inner_val = crate::lightning::ln::msgs::ClosingSignedFeeRange { inner: unsafe { (if inner_val.is_none() { std::ptr::null() } else { { (inner_val.as_ref().unwrap()) } } as *const _) as *mut _ }, is_owned: false }; + local_inner_val +} +/// The minimum and maximum fees which the sender is willing to accept, provided only by new +/// nodes. +/// +/// Note that val (or a relevant inner pointer) may be NULL or all-0s to represent None +#[no_mangle] +pub extern "C" fn ClosingSigned_set_fee_range(this_ptr: &mut ClosingSigned, mut val: crate::lightning::ln::msgs::ClosingSignedFeeRange) { + let mut local_val = if val.inner.is_null() { None } else { Some( { *unsafe { Box::from_raw(val.take_inner()) } }) }; + unsafe { &mut *this_ptr.inner }.fee_range = local_val; +} /// Constructs a new ClosingSigned given each field #[must_use] #[no_mangle] -pub extern "C" fn ClosingSigned_new(mut channel_id_arg: crate::c_types::ThirtyTwoBytes, mut fee_satoshis_arg: u64, mut signature_arg: crate::c_types::Signature) -> ClosingSigned { +pub extern "C" fn ClosingSigned_new(mut channel_id_arg: crate::c_types::ThirtyTwoBytes, mut fee_satoshis_arg: u64, mut signature_arg: crate::c_types::Signature, mut fee_range_arg: crate::lightning::ln::msgs::ClosingSignedFeeRange) -> ClosingSigned { + let mut local_fee_range_arg = if fee_range_arg.inner.is_null() { None } else { Some( { *unsafe { Box::from_raw(fee_range_arg.take_inner()) } }) }; ClosingSigned { inner: Box::into_raw(Box::new(nativeClosingSigned { channel_id: channel_id_arg.data, fee_satoshis: fee_satoshis_arg, signature: signature_arg.into_rust(), + fee_range: local_fee_range_arg, })), is_owned: true } } impl Clone for ClosingSigned { @@ -5001,6 +5123,22 @@ pub extern "C" fn ClosingSigned_read(ser: crate::c_types::u8slice) -> crate::c_t local_res } #[no_mangle] +/// Serialize the ClosingSignedFeeRange object into a byte array which can be read by ClosingSignedFeeRange_read +pub extern "C" fn ClosingSignedFeeRange_write(obj: &ClosingSignedFeeRange) -> crate::c_types::derived::CVec_u8Z { + crate::c_types::serialize_obj(unsafe { &*unsafe { &*obj }.inner }) +} +#[no_mangle] +pub(crate) extern "C" fn ClosingSignedFeeRange_write_void(obj: *const c_void) -> crate::c_types::derived::CVec_u8Z { + crate::c_types::serialize_obj(unsafe { &*(obj as *const nativeClosingSignedFeeRange) }) +} +#[no_mangle] +/// Read a ClosingSignedFeeRange from a byte array, created by ClosingSignedFeeRange_write +pub extern "C" fn ClosingSignedFeeRange_read(ser: crate::c_types::u8slice) -> crate::c_types::derived::CResult_ClosingSignedFeeRangeDecodeErrorZ { + let res = crate::c_types::deserialize_obj(ser); + let mut local_res = match res { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::lightning::ln::msgs::ClosingSignedFeeRange { inner: Box::into_raw(Box::new(o)), is_owned: true } }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { crate::lightning::ln::msgs::DecodeError { inner: Box::into_raw(Box::new(e)), is_owned: true } }).into() }; + local_res +} +#[no_mangle] /// Serialize the CommitmentSigned object into a byte array which can be read by CommitmentSigned_read pub extern "C" fn CommitmentSigned_write(obj: &CommitmentSigned) -> crate::c_types::derived::CVec_u8Z { crate::c_types::serialize_obj(unsafe { &*unsafe { &*obj }.inner }) diff --git a/lightning-c-bindings/src/lightning/ln/peer_handler.rs b/lightning-c-bindings/src/lightning/ln/peer_handler.rs index cad0560..c0a61bc 100644 --- a/lightning-c-bindings/src/lightning/ln/peer_handler.rs +++ b/lightning-c-bindings/src/lightning/ln/peer_handler.rs @@ -881,9 +881,12 @@ pub extern "C" fn PeerManager_disconnect_by_node_id(this_arg: &PeerManager, mut unsafe { &*this_arg.inner }.disconnect_by_node_id(node_id.into_rust(), no_connection_possible) } -/// This function should be called roughly once every 30 seconds. -/// It will send pings to each peer and disconnect those which did not respond to the last -/// round of pings. +/// Send pings to each peer and disconnect those which did not respond to the last round of +/// pings. +/// +/// This may be called on any timescale you want, however, roughly once every five to ten +/// seconds is preferred. The call rate determines both how often we send a ping to our peers +/// and how much time they have to respond before we disconnect them. /// /// May call [`send_data`] on all [`SocketDescriptor`]s. Thus, be very careful with reentrancy /// issues! diff --git a/lightning-c-bindings/src/lightning/ln/script.rs b/lightning-c-bindings/src/lightning/ln/script.rs new file mode 100644 index 0000000..7dc4b1d --- /dev/null +++ b/lightning-c-bindings/src/lightning/ln/script.rs @@ -0,0 +1,239 @@ +// This file is Copyright its original authors, visible in version control +// history and in the source files from which this was generated. +// +// This file is licensed under the license available in the LICENSE or LICENSE.md +// file in the root of this repository or, if no such file exists, the same +// license as that which applies to the original source files from which this +// source was automatically generated. + +//! Abstractions for scripts used in the Lightning Network. + +use std::str::FromStr; +use std::ffi::c_void; +use bitcoin::hashes::Hash; +use crate::c_types::*; + + +use lightning::ln::script::ShutdownScript as nativeShutdownScriptImport; +type nativeShutdownScript = nativeShutdownScriptImport; + +/// A script pubkey for shutting down a channel as defined by [BOLT #2]. +/// +/// [BOLT #2]: https://github.com/lightningnetwork/lightning-rfc/blob/master/02-peer-protocol.md +#[must_use] +#[repr(C)] +pub struct ShutdownScript { + /// A pointer to the opaque Rust object. + + /// Nearly everywhere, inner must be non-null, however in places where + /// the Rust equivalent takes an Option, it may be set to null to indicate None. + pub inner: *mut nativeShutdownScript, + /// Indicates that this is the only struct which contains the same pointer. + + /// Rust functions which take ownership of an object provided via an argument require + /// this to be true and invalidate the object pointed to by inner. + pub is_owned: bool, +} + +impl Drop for ShutdownScript { + fn drop(&mut self) { + if self.is_owned && !<*mut nativeShutdownScript>::is_null(self.inner) { + let _ = unsafe { Box::from_raw(self.inner) }; + } + } +} +/// Frees any resources used by the ShutdownScript, if is_owned is set and inner is non-NULL. +#[no_mangle] +pub extern "C" fn ShutdownScript_free(this_obj: ShutdownScript) { } +#[allow(unused)] +/// Used only if an object of this type is returned as a trait impl by a method +extern "C" fn ShutdownScript_free_void(this_ptr: *mut c_void) { + unsafe { let _ = Box::from_raw(this_ptr as *mut nativeShutdownScript); } +} +#[allow(unused)] +/// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy +impl ShutdownScript { + pub(crate) fn take_inner(mut self) -> *mut nativeShutdownScript { + assert!(self.is_owned); + let ret = self.inner; + self.inner = std::ptr::null_mut(); + ret + } +} +impl Clone for ShutdownScript { + fn clone(&self) -> Self { + Self { + inner: if <*mut nativeShutdownScript>::is_null(self.inner) { std::ptr::null_mut() } else { + Box::into_raw(Box::new(unsafe { &*self.inner }.clone())) }, + is_owned: true, + } + } +} +#[allow(unused)] +/// Used only if an object of this type is returned as a trait impl by a method +pub(crate) extern "C" fn ShutdownScript_clone_void(this_ptr: *const c_void) -> *mut c_void { + Box::into_raw(Box::new(unsafe { (*(this_ptr as *mut nativeShutdownScript)).clone() })) as *mut c_void +} +#[no_mangle] +/// Creates a copy of the ShutdownScript +pub extern "C" fn ShutdownScript_clone(orig: &ShutdownScript) -> ShutdownScript { + orig.clone() +} + +use lightning::ln::script::InvalidShutdownScript as nativeInvalidShutdownScriptImport; +type nativeInvalidShutdownScript = nativeInvalidShutdownScriptImport; + +/// An error occurring when converting from [`Script`] to [`ShutdownScript`]. +#[must_use] +#[repr(C)] +pub struct InvalidShutdownScript { + /// A pointer to the opaque Rust object. + + /// Nearly everywhere, inner must be non-null, however in places where + /// the Rust equivalent takes an Option, it may be set to null to indicate None. + pub inner: *mut nativeInvalidShutdownScript, + /// Indicates that this is the only struct which contains the same pointer. + + /// Rust functions which take ownership of an object provided via an argument require + /// this to be true and invalidate the object pointed to by inner. + pub is_owned: bool, +} + +impl Drop for InvalidShutdownScript { + fn drop(&mut self) { + if self.is_owned && !<*mut nativeInvalidShutdownScript>::is_null(self.inner) { + let _ = unsafe { Box::from_raw(self.inner) }; + } + } +} +/// Frees any resources used by the InvalidShutdownScript, if is_owned is set and inner is non-NULL. +#[no_mangle] +pub extern "C" fn InvalidShutdownScript_free(this_obj: InvalidShutdownScript) { } +#[allow(unused)] +/// Used only if an object of this type is returned as a trait impl by a method +extern "C" fn InvalidShutdownScript_free_void(this_ptr: *mut c_void) { + unsafe { let _ = Box::from_raw(this_ptr as *mut nativeInvalidShutdownScript); } +} +#[allow(unused)] +/// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy +impl InvalidShutdownScript { + pub(crate) fn take_inner(mut self) -> *mut nativeInvalidShutdownScript { + assert!(self.is_owned); + let ret = self.inner; + self.inner = std::ptr::null_mut(); + ret + } +} +/// The script that did not meet the requirements from [BOLT #2]. +/// +/// [BOLT #2]: https://github.com/lightningnetwork/lightning-rfc/blob/master/02-peer-protocol.md +#[no_mangle] +pub extern "C" fn InvalidShutdownScript_get_script(this_ptr: &InvalidShutdownScript) -> crate::c_types::u8slice { + let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.script; + crate::c_types::u8slice::from_slice(&inner_val[..]) +} +/// The script that did not meet the requirements from [BOLT #2]. +/// +/// [BOLT #2]: https://github.com/lightningnetwork/lightning-rfc/blob/master/02-peer-protocol.md +#[no_mangle] +pub extern "C" fn InvalidShutdownScript_set_script(this_ptr: &mut InvalidShutdownScript, mut val: crate::c_types::derived::CVec_u8Z) { + unsafe { &mut *this_ptr.inner }.script = ::bitcoin::blockdata::script::Script::from(val.into_rust()); +} +/// Constructs a new InvalidShutdownScript given each field +#[must_use] +#[no_mangle] +pub extern "C" fn InvalidShutdownScript_new(mut script_arg: crate::c_types::derived::CVec_u8Z) -> InvalidShutdownScript { + InvalidShutdownScript { inner: Box::into_raw(Box::new(nativeInvalidShutdownScript { + script: ::bitcoin::blockdata::script::Script::from(script_arg.into_rust()), + })), is_owned: true } +} +#[no_mangle] +/// Serialize the ShutdownScript object into a byte array which can be read by ShutdownScript_read +pub extern "C" fn ShutdownScript_write(obj: &ShutdownScript) -> crate::c_types::derived::CVec_u8Z { + crate::c_types::serialize_obj(unsafe { &*unsafe { &*obj }.inner }) +} +#[no_mangle] +pub(crate) extern "C" fn ShutdownScript_write_void(obj: *const c_void) -> crate::c_types::derived::CVec_u8Z { + crate::c_types::serialize_obj(unsafe { &*(obj as *const nativeShutdownScript) }) +} +#[no_mangle] +/// Read a ShutdownScript from a byte array, created by ShutdownScript_write +pub extern "C" fn ShutdownScript_read(ser: crate::c_types::u8slice) -> crate::c_types::derived::CResult_ShutdownScriptDecodeErrorZ { + let res = crate::c_types::deserialize_obj(ser); + let mut local_res = match res { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::lightning::ln::script::ShutdownScript { inner: Box::into_raw(Box::new(o)), is_owned: true } }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { crate::lightning::ln::msgs::DecodeError { inner: Box::into_raw(Box::new(e)), is_owned: true } }).into() }; + local_res +} +/// Generates a P2PKH script pubkey from the given [`PubkeyHash`]. +#[must_use] +#[no_mangle] +pub extern "C" fn ShutdownScript_new_p2pkh(pubkey_hash: *const [u8; 20]) -> ShutdownScript { + let mut ret = lightning::ln::script::ShutdownScript::new_p2pkh(&bitcoin::hash_types::PubkeyHash::from_hash(bitcoin::hashes::Hash::from_inner(unsafe { *pubkey_hash }.clone()))); + ShutdownScript { inner: Box::into_raw(Box::new(ret)), is_owned: true } +} + +/// Generates a P2SH script pubkey from the given [`ScriptHash`]. +#[must_use] +#[no_mangle] +pub extern "C" fn ShutdownScript_new_p2sh(script_hash: *const [u8; 20]) -> ShutdownScript { + let mut ret = lightning::ln::script::ShutdownScript::new_p2sh(&bitcoin::hash_types::ScriptHash::from_hash(bitcoin::hashes::Hash::from_inner(unsafe { *script_hash }.clone()))); + ShutdownScript { inner: Box::into_raw(Box::new(ret)), is_owned: true } +} + +/// Generates a P2WPKH script pubkey from the given [`WPubkeyHash`]. +#[must_use] +#[no_mangle] +pub extern "C" fn ShutdownScript_new_p2wpkh(pubkey_hash: *const [u8; 20]) -> ShutdownScript { + let mut ret = lightning::ln::script::ShutdownScript::new_p2wpkh(&bitcoin::hash_types::WPubkeyHash::from_hash(bitcoin::hashes::Hash::from_inner(unsafe { *pubkey_hash }.clone()))); + ShutdownScript { inner: Box::into_raw(Box::new(ret)), is_owned: true } +} + +/// Generates a P2WSH script pubkey from the given [`WScriptHash`]. +#[must_use] +#[no_mangle] +pub extern "C" fn ShutdownScript_new_p2wsh(script_hash: *const [u8; 32]) -> ShutdownScript { + let mut ret = lightning::ln::script::ShutdownScript::new_p2wsh(&bitcoin::hash_types::WScriptHash::from_hash(bitcoin::hashes::Hash::from_inner(unsafe { *script_hash }.clone()))); + ShutdownScript { inner: Box::into_raw(Box::new(ret)), is_owned: true } +} + +/// Generates a P2WSH script pubkey from the given segwit version and program. +/// +/// # Errors +/// +/// This function may return an error if `program` is invalid for the segwit `version`. +#[must_use] +#[no_mangle] +pub extern "C" fn ShutdownScript_new_witness_program(mut version: u8, mut program: crate::c_types::u8slice) -> crate::c_types::derived::CResult_ShutdownScriptInvalidShutdownScriptZ { + let mut ret = lightning::ln::script::ShutdownScript::new_witness_program(core::num::NonZeroU8::new(version).expect("Value must be non-zero"), program.to_slice()); + let mut local_ret = match ret { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::lightning::ln::script::ShutdownScript { inner: Box::into_raw(Box::new(o)), is_owned: true } }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { crate::lightning::ln::script::InvalidShutdownScript { inner: Box::into_raw(Box::new(e)), is_owned: true } }).into() }; + local_ret +} + +/// Converts the shutdown script into the underlying [`Script`]. +#[must_use] +#[no_mangle] +pub extern "C" fn ShutdownScript_into_inner(mut this_arg: ShutdownScript) -> crate::c_types::derived::CVec_u8Z { + let mut ret = (*unsafe { Box::from_raw(this_arg.take_inner()) }).into_inner(); + ret.into_bytes().into() +} + +/// Returns the [`PublicKey`] used for a P2WPKH shutdown script if constructed directly from it. +/// +/// Note that the return value (or a relevant inner pointer) may be NULL or all-0s to represent None +#[must_use] +#[no_mangle] +pub extern "C" fn ShutdownScript_as_legacy_pubkey(this_arg: &ShutdownScript) -> crate::c_types::PublicKey { + let mut ret = unsafe { &*this_arg.inner }.as_legacy_pubkey(); + let mut local_ret = if ret.is_none() { crate::c_types::PublicKey::null() } else { { crate::c_types::PublicKey::from_rust(&(ret.unwrap())) } }; + local_ret +} + +/// Returns whether the shutdown script is compatible with the features as defined by BOLT #2. +/// +/// Specifically, checks for compliance with feature `option_shutdown_anysegwit`. +#[must_use] +#[no_mangle] +pub extern "C" fn ShutdownScript_is_compatible(this_arg: &ShutdownScript, features: &crate::lightning::ln::features::InitFeatures) -> bool { + let mut ret = unsafe { &*this_arg.inner }.is_compatible(unsafe { &*features.inner }); + ret +} + diff --git a/lightning-c-bindings/src/lightning/util/config.rs b/lightning-c-bindings/src/lightning/util/config.rs index 28acdca..df0220e 100644 --- a/lightning-c-bindings/src/lightning/util/config.rs +++ b/lightning-c-bindings/src/lightning/util/config.rs @@ -637,16 +637,110 @@ pub extern "C" fn ChannelConfig_get_commit_upfront_shutdown_pubkey(this_ptr: &Ch pub extern "C" fn ChannelConfig_set_commit_upfront_shutdown_pubkey(this_ptr: &mut ChannelConfig, mut val: bool) { unsafe { &mut *this_ptr.inner }.commit_upfront_shutdown_pubkey = val; } +/// Limit our total exposure to in-flight HTLCs which are burned to fees as they are too +/// small to claim on-chain. +/// +/// When an HTLC present in one of our channels is below a \"dust\" threshold, the HTLC will +/// not be claimable on-chain, instead being turned into additional miner fees if either +/// party force-closes the channel. Because the threshold is per-HTLC, our total exposure +/// to such payments may be sustantial if there are many dust HTLCs present when the +/// channel is force-closed. +/// +/// This limit is applied for sent, forwarded, and received HTLCs and limits the total +/// exposure across all three types per-channel. Setting this too low may prevent the +/// sending or receipt of low-value HTLCs on high-traffic nodes, and this limit is very +/// important to prevent stealing of dust HTLCs by miners. +/// +/// Default value: 5_000_000 msat. +#[no_mangle] +pub extern "C" fn ChannelConfig_get_max_dust_htlc_exposure_msat(this_ptr: &ChannelConfig) -> u64 { + let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.max_dust_htlc_exposure_msat; + *inner_val +} +/// Limit our total exposure to in-flight HTLCs which are burned to fees as they are too +/// small to claim on-chain. +/// +/// When an HTLC present in one of our channels is below a \"dust\" threshold, the HTLC will +/// not be claimable on-chain, instead being turned into additional miner fees if either +/// party force-closes the channel. Because the threshold is per-HTLC, our total exposure +/// to such payments may be sustantial if there are many dust HTLCs present when the +/// channel is force-closed. +/// +/// This limit is applied for sent, forwarded, and received HTLCs and limits the total +/// exposure across all three types per-channel. Setting this too low may prevent the +/// sending or receipt of low-value HTLCs on high-traffic nodes, and this limit is very +/// important to prevent stealing of dust HTLCs by miners. +/// +/// Default value: 5_000_000 msat. +#[no_mangle] +pub extern "C" fn ChannelConfig_set_max_dust_htlc_exposure_msat(this_ptr: &mut ChannelConfig, mut val: u64) { + unsafe { &mut *this_ptr.inner }.max_dust_htlc_exposure_msat = val; +} +/// The additional fee we're willing to pay to avoid waiting for the counterparty's +/// `to_self_delay` to reclaim funds. +/// +/// When we close a channel cooperatively with our counterparty, we negotiate a fee for the +/// closing transaction which both sides find acceptable, ultimately paid by the channel +/// funder/initiator. +/// +/// When we are the funder, because we have to pay the channel closing fee, we bound the +/// acceptable fee by our [`Background`] and [`Normal`] fees, with the upper bound increased by +/// this value. Because the on-chain fee we'd pay to force-close the channel is kept near our +/// [`Normal`] feerate during normal operation, this value represents the additional fee we're +/// willing to pay in order to avoid waiting for our counterparty's to_self_delay to reclaim our +/// funds. +/// +/// When we are not the funder, we require the closing transaction fee pay at least our +/// [`Background`] fee estimate, but allow our counterparty to pay as much fee as they like. +/// Thus, this value is ignored when we are not the funder. +/// +/// Default value: 1000 satoshis. +/// +/// [`Normal`]: crate::chain::chaininterface::ConfirmationTarget::Normal +/// [`Background`]: crate::chain::chaininterface::ConfirmationTarget::Background +#[no_mangle] +pub extern "C" fn ChannelConfig_get_force_close_avoidance_max_fee_satoshis(this_ptr: &ChannelConfig) -> u64 { + let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.force_close_avoidance_max_fee_satoshis; + *inner_val +} +/// The additional fee we're willing to pay to avoid waiting for the counterparty's +/// `to_self_delay` to reclaim funds. +/// +/// When we close a channel cooperatively with our counterparty, we negotiate a fee for the +/// closing transaction which both sides find acceptable, ultimately paid by the channel +/// funder/initiator. +/// +/// When we are the funder, because we have to pay the channel closing fee, we bound the +/// acceptable fee by our [`Background`] and [`Normal`] fees, with the upper bound increased by +/// this value. Because the on-chain fee we'd pay to force-close the channel is kept near our +/// [`Normal`] feerate during normal operation, this value represents the additional fee we're +/// willing to pay in order to avoid waiting for our counterparty's to_self_delay to reclaim our +/// funds. +/// +/// When we are not the funder, we require the closing transaction fee pay at least our +/// [`Background`] fee estimate, but allow our counterparty to pay as much fee as they like. +/// Thus, this value is ignored when we are not the funder. +/// +/// Default value: 1000 satoshis. +/// +/// [`Normal`]: crate::chain::chaininterface::ConfirmationTarget::Normal +/// [`Background`]: crate::chain::chaininterface::ConfirmationTarget::Background +#[no_mangle] +pub extern "C" fn ChannelConfig_set_force_close_avoidance_max_fee_satoshis(this_ptr: &mut ChannelConfig, mut val: u64) { + unsafe { &mut *this_ptr.inner }.force_close_avoidance_max_fee_satoshis = val; +} /// Constructs a new ChannelConfig given each field #[must_use] #[no_mangle] -pub extern "C" fn ChannelConfig_new(mut forwarding_fee_proportional_millionths_arg: u32, mut forwarding_fee_base_msat_arg: u32, mut cltv_expiry_delta_arg: u16, mut announced_channel_arg: bool, mut commit_upfront_shutdown_pubkey_arg: bool) -> ChannelConfig { +pub extern "C" fn ChannelConfig_new(mut forwarding_fee_proportional_millionths_arg: u32, mut forwarding_fee_base_msat_arg: u32, mut cltv_expiry_delta_arg: u16, mut announced_channel_arg: bool, mut commit_upfront_shutdown_pubkey_arg: bool, mut max_dust_htlc_exposure_msat_arg: u64, mut force_close_avoidance_max_fee_satoshis_arg: u64) -> ChannelConfig { ChannelConfig { inner: Box::into_raw(Box::new(nativeChannelConfig { forwarding_fee_proportional_millionths: forwarding_fee_proportional_millionths_arg, forwarding_fee_base_msat: forwarding_fee_base_msat_arg, cltv_expiry_delta: cltv_expiry_delta_arg, announced_channel: announced_channel_arg, commit_upfront_shutdown_pubkey: commit_upfront_shutdown_pubkey_arg, + max_dust_htlc_exposure_msat: max_dust_htlc_exposure_msat_arg, + force_close_avoidance_max_fee_satoshis: force_close_avoidance_max_fee_satoshis_arg, })), is_owned: true } } impl Clone for ChannelConfig { diff --git a/lightning-c-bindings/src/lightning/util/errors.rs b/lightning-c-bindings/src/lightning/util/errors.rs index 27d79c2..8f04326 100644 --- a/lightning-c-bindings/src/lightning/util/errors.rs +++ b/lightning-c-bindings/src/lightning/util/errors.rs @@ -50,6 +50,18 @@ pub enum APIError { /// An attempt to call watch/update_channel returned an Err (ie you did this!), causing the /// attempted action to fail. MonitorUpdateFailed, + /// [`KeysInterface::get_shutdown_scriptpubkey`] returned a shutdown scriptpubkey incompatible + /// with the channel counterparty as negotiated in [`InitFeatures`]. + /// + /// Using a SegWit v0 script should resolve this issue. If you cannot, you won't be able to open + /// a channel or cooperatively close one with this peer (and will have to force-close instead). + /// + /// [`KeysInterface::get_shutdown_scriptpubkey`]: crate::chain::keysinterface::KeysInterface::get_shutdown_scriptpubkey + /// [`InitFeatures`]: crate::ln::features::InitFeatures + IncompatibleShutdownScript { + /// The incompatible shutdown script. + script: crate::lightning::ln::script::ShutdownScript, + }, } use lightning::util::errors::APIError as nativeAPIError; impl APIError { @@ -83,6 +95,12 @@ impl APIError { } }, APIError::MonitorUpdateFailed => nativeAPIError::MonitorUpdateFailed, + APIError::IncompatibleShutdownScript {ref script, } => { + let mut script_nonref = (*script).clone(); + nativeAPIError::IncompatibleShutdownScript { + script: *unsafe { Box::from_raw(script_nonref.take_inner()) }, + } + }, } } #[allow(unused)] @@ -110,6 +128,11 @@ impl APIError { } }, APIError::MonitorUpdateFailed => nativeAPIError::MonitorUpdateFailed, + APIError::IncompatibleShutdownScript {mut script, } => { + nativeAPIError::IncompatibleShutdownScript { + script: *unsafe { Box::from_raw(script.take_inner()) }, + } + }, } } #[allow(unused)] @@ -142,6 +165,12 @@ impl APIError { } }, nativeAPIError::MonitorUpdateFailed => APIError::MonitorUpdateFailed, + nativeAPIError::IncompatibleShutdownScript {ref script, } => { + let mut script_nonref = (*script).clone(); + APIError::IncompatibleShutdownScript { + script: crate::lightning::ln::script::ShutdownScript { inner: Box::into_raw(Box::new(script_nonref)), is_owned: true }, + } + }, } } #[allow(unused)] @@ -169,6 +198,11 @@ impl APIError { } }, nativeAPIError::MonitorUpdateFailed => APIError::MonitorUpdateFailed, + nativeAPIError::IncompatibleShutdownScript {mut script, } => { + APIError::IncompatibleShutdownScript { + script: crate::lightning::ln::script::ShutdownScript { inner: Box::into_raw(Box::new(script)), is_owned: true }, + } + }, } } } @@ -213,3 +247,10 @@ pub extern "C" fn APIError_channel_unavailable(err: crate::c_types::Str) -> APIE /// Utility method to constructs a new MonitorUpdateFailed-variant APIError pub extern "C" fn APIError_monitor_update_failed() -> APIError { APIError::MonitorUpdateFailed} +#[no_mangle] +/// Utility method to constructs a new IncompatibleShutdownScript-variant APIError +pub extern "C" fn APIError_incompatible_shutdown_script(script: crate::lightning::ln::script::ShutdownScript) -> APIError { + APIError::IncompatibleShutdownScript { + script, + } +} -- 2.39.5