1 // This file is Copyright its original authors, visible in version control
2 // history and in the source files from which this was generated.
4 // This file is licensed under the license available in the LICENSE or LICENSE.md
5 // file in the root of this repository or, if no such file exists, the same
6 // license as that which applies to the original source files from which this
7 // source was automatically generated.
9 //! Utilities for creating and parsing short channel ids.
11 use alloc::str::FromStr;
12 use alloc::string::String;
13 use core::ffi::c_void;
14 use core::convert::Infallible;
15 use bitcoin::hashes::Hash;
16 use crate::c_types::*;
17 #[cfg(feature="no-std")]
18 use alloc::{vec::Vec, boxed::Box};
20 /// Maximum block height that can be used in a `short_channel_id`. This
21 /// value is based on the 3-bytes available for block height.
24 pub static MAX_SCID_BLOCK: u64 = lightning::util::scid_utils::MAX_SCID_BLOCK;
25 /// Maximum transaction index that can be used in a `short_channel_id`.
26 /// This value is based on the 3-bytes available for tx index.
29 pub static MAX_SCID_TX_INDEX: u64 = lightning::util::scid_utils::MAX_SCID_TX_INDEX;
30 /// Maximum vout index that can be used in a `short_channel_id`. This
31 /// value is based on the 2-bytes available for the vout index.
34 pub static MAX_SCID_VOUT_INDEX: u64 = lightning::util::scid_utils::MAX_SCID_VOUT_INDEX;
35 /// A `short_channel_id` construction error
39 pub enum ShortChannelIdError {
40 /// Block height too high
44 /// Vout index too high
47 use lightning::util::scid_utils::ShortChannelIdError as ShortChannelIdErrorImport;
48 pub(crate) type nativeShortChannelIdError = ShortChannelIdErrorImport;
50 impl ShortChannelIdError {
52 pub(crate) fn to_native(&self) -> nativeShortChannelIdError {
54 ShortChannelIdError::BlockOverflow => nativeShortChannelIdError::BlockOverflow,
55 ShortChannelIdError::TxIndexOverflow => nativeShortChannelIdError::TxIndexOverflow,
56 ShortChannelIdError::VoutIndexOverflow => nativeShortChannelIdError::VoutIndexOverflow,
60 pub(crate) fn into_native(self) -> nativeShortChannelIdError {
62 ShortChannelIdError::BlockOverflow => nativeShortChannelIdError::BlockOverflow,
63 ShortChannelIdError::TxIndexOverflow => nativeShortChannelIdError::TxIndexOverflow,
64 ShortChannelIdError::VoutIndexOverflow => nativeShortChannelIdError::VoutIndexOverflow,
68 pub(crate) fn from_native(native: &ShortChannelIdErrorImport) -> Self {
69 let native = unsafe { &*(native as *const _ as *const c_void as *const nativeShortChannelIdError) };
71 nativeShortChannelIdError::BlockOverflow => ShortChannelIdError::BlockOverflow,
72 nativeShortChannelIdError::TxIndexOverflow => ShortChannelIdError::TxIndexOverflow,
73 nativeShortChannelIdError::VoutIndexOverflow => ShortChannelIdError::VoutIndexOverflow,
77 pub(crate) fn native_into(native: nativeShortChannelIdError) -> Self {
79 nativeShortChannelIdError::BlockOverflow => ShortChannelIdError::BlockOverflow,
80 nativeShortChannelIdError::TxIndexOverflow => ShortChannelIdError::TxIndexOverflow,
81 nativeShortChannelIdError::VoutIndexOverflow => ShortChannelIdError::VoutIndexOverflow,
85 /// Creates a copy of the ShortChannelIdError
87 pub extern "C" fn ShortChannelIdError_clone(orig: &ShortChannelIdError) -> ShortChannelIdError {
91 /// Used only if an object of this type is returned as a trait impl by a method
92 pub(crate) extern "C" fn ShortChannelIdError_clone_void(this_ptr: *const c_void) -> *mut c_void {
93 Box::into_raw(Box::new(unsafe { (*(this_ptr as *const ShortChannelIdError)).clone() })) as *mut c_void
96 /// Used only if an object of this type is returned as a trait impl by a method
97 pub(crate) extern "C" fn ShortChannelIdError_free_void(this_ptr: *mut c_void) {
98 let _ = unsafe { Box::from_raw(this_ptr as *mut ShortChannelIdError) };
101 /// Utility method to constructs a new BlockOverflow-variant ShortChannelIdError
102 pub extern "C" fn ShortChannelIdError_block_overflow() -> ShortChannelIdError {
103 ShortChannelIdError::BlockOverflow}
105 /// Utility method to constructs a new TxIndexOverflow-variant ShortChannelIdError
106 pub extern "C" fn ShortChannelIdError_tx_index_overflow() -> ShortChannelIdError {
107 ShortChannelIdError::TxIndexOverflow}
109 /// Utility method to constructs a new VoutIndexOverflow-variant ShortChannelIdError
110 pub extern "C" fn ShortChannelIdError_vout_index_overflow() -> ShortChannelIdError {
111 ShortChannelIdError::VoutIndexOverflow}
112 /// Get a string which allows debug introspection of a ShortChannelIdError object
113 pub extern "C" fn ShortChannelIdError_debug_str_void(o: *const c_void) -> Str {
114 alloc::format!("{:?}", unsafe { o as *const crate::lightning::util::scid_utils::ShortChannelIdError }).into()}
115 /// Checks if two ShortChannelIdErrors contain equal inner contents.
116 /// This ignores pointers and is_owned flags and looks at the values in fields.
118 pub extern "C" fn ShortChannelIdError_eq(a: &ShortChannelIdError, b: &ShortChannelIdError) -> bool {
119 if &a.to_native() == &b.to_native() { true } else { false }
121 /// Extracts the block height (most significant 3-bytes) from the `short_channel_id`
123 pub extern "C" fn block_from_scid(mut short_channel_id: u64) -> u32 {
124 let mut ret = lightning::util::scid_utils::block_from_scid(short_channel_id);
128 /// Extracts the tx index (bytes [2..4]) from the `short_channel_id`
130 pub extern "C" fn tx_index_from_scid(mut short_channel_id: u64) -> u32 {
131 let mut ret = lightning::util::scid_utils::tx_index_from_scid(short_channel_id);
135 /// Extracts the vout (bytes [0..2]) from the `short_channel_id`
137 pub extern "C" fn vout_from_scid(mut short_channel_id: u64) -> u16 {
138 let mut ret = lightning::util::scid_utils::vout_from_scid(short_channel_id);
142 /// Constructs a `short_channel_id` using the components pieces. Results in an error
143 /// if the block height, tx index, or vout index overflow the maximum sizes.
145 pub extern "C" fn scid_from_parts(mut block: u64, mut tx_index: u64, mut vout_index: u64) -> crate::c_types::derived::CResult_u64ShortChannelIdErrorZ {
146 let mut ret = lightning::util::scid_utils::scid_from_parts(block, tx_index, vout_index);
147 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::scid_utils::ShortChannelIdError::native_into(e) }).into() };
153 use alloc::str::FromStr;
154 use alloc::string::String;
155 use core::ffi::c_void;
156 use core::convert::Infallible;
157 use bitcoin::hashes::Hash;
158 use crate::c_types::*;
159 #[cfg(feature="no-std")]
160 use alloc::{vec::Vec, boxed::Box};