Skip to content

Commit 28cca59

Browse files
committed
Add automatically generated C wrapper bindings
1 parent 4b626ec commit 28cca59

File tree

21 files changed

+4449
-1
lines changed

21 files changed

+4449
-1
lines changed

lightning-c-bindings/include/lightning.h

Lines changed: 1581 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
struct lnEventOpaque;
2+
typedef struct lnEventOpaque LDKlnEvent;
3+
struct lnMessageSendEventOpaque;
4+
typedef struct lnMessageSendEventOpaque LDKlnMessageSendEvent;
5+
struct lnAPIErrorOpaque;
6+
typedef struct lnAPIErrorOpaque LDKlnAPIError;
7+
struct lnUserConfigOpaque;
8+
typedef struct lnUserConfigOpaque LDKlnUserConfig;
9+
struct lnChannelHandshakeConfigOpaque;
10+
typedef struct lnChannelHandshakeConfigOpaque LDKlnChannelHandshakeConfig;
11+
struct lnChannelHandshakeLimitsOpaque;
12+
typedef struct lnChannelHandshakeLimitsOpaque LDKlnChannelHandshakeLimits;
13+
struct lnChannelConfigOpaque;
14+
typedef struct lnChannelConfigOpaque LDKlnChannelConfig;
15+
struct lnOutPointOpaque;
16+
typedef struct lnOutPointOpaque LDKlnOutPoint;
17+
struct lnSpendableOutputDescriptorOpaque;
18+
typedef struct lnSpendableOutputDescriptorOpaque LDKlnSpendableOutputDescriptor;
19+
struct lnInMemoryChannelKeysOpaque;
20+
typedef struct lnInMemoryChannelKeysOpaque LDKlnInMemoryChannelKeys;
21+
struct lnKeysManagerOpaque;
22+
typedef struct lnKeysManagerOpaque LDKlnKeysManager;
23+
struct lnChannelManagerOpaque;
24+
typedef struct lnChannelManagerOpaque LDKlnChannelManager;
25+
struct lnChannelDetailsOpaque;
26+
typedef struct lnChannelDetailsOpaque LDKlnChannelDetails;
27+
struct lnPaymentSendFailureOpaque;
28+
typedef struct lnPaymentSendFailureOpaque LDKlnPaymentSendFailure;
29+
struct lnChannelMonitorUpdateOpaque;
30+
typedef struct lnChannelMonitorUpdateOpaque LDKlnChannelMonitorUpdate;
31+
struct lnMonitorUpdateErrorOpaque;
32+
typedef struct lnMonitorUpdateErrorOpaque LDKlnMonitorUpdateError;
33+
struct lnHTLCUpdateOpaque;
34+
typedef struct lnHTLCUpdateOpaque LDKlnHTLCUpdate;
35+
struct lnChannelMonitorOpaque;
36+
typedef struct lnChannelMonitorOpaque LDKlnChannelMonitor;
37+
struct lnDecodeErrorOpaque;
38+
typedef struct lnDecodeErrorOpaque LDKlnDecodeError;
39+
struct lnInitOpaque;
40+
typedef struct lnInitOpaque LDKlnInit;
41+
struct lnErrorMessageOpaque;
42+
typedef struct lnErrorMessageOpaque LDKlnErrorMessage;
43+
struct lnPingOpaque;
44+
typedef struct lnPingOpaque LDKlnPing;
45+
struct lnPongOpaque;
46+
typedef struct lnPongOpaque LDKlnPong;
47+
struct lnOpenChannelOpaque;
48+
typedef struct lnOpenChannelOpaque LDKlnOpenChannel;
49+
struct lnAcceptChannelOpaque;
50+
typedef struct lnAcceptChannelOpaque LDKlnAcceptChannel;
51+
struct lnFundingCreatedOpaque;
52+
typedef struct lnFundingCreatedOpaque LDKlnFundingCreated;
53+
struct lnFundingSignedOpaque;
54+
typedef struct lnFundingSignedOpaque LDKlnFundingSigned;
55+
struct lnFundingLockedOpaque;
56+
typedef struct lnFundingLockedOpaque LDKlnFundingLocked;
57+
struct lnShutdownOpaque;
58+
typedef struct lnShutdownOpaque LDKlnShutdown;
59+
struct lnClosingSignedOpaque;
60+
typedef struct lnClosingSignedOpaque LDKlnClosingSigned;
61+
struct lnUpdateAddHTLCOpaque;
62+
typedef struct lnUpdateAddHTLCOpaque LDKlnUpdateAddHTLC;
63+
struct lnUpdateFulfillHTLCOpaque;
64+
typedef struct lnUpdateFulfillHTLCOpaque LDKlnUpdateFulfillHTLC;
65+
struct lnUpdateFailHTLCOpaque;
66+
typedef struct lnUpdateFailHTLCOpaque LDKlnUpdateFailHTLC;
67+
struct lnUpdateFailMalformedHTLCOpaque;
68+
typedef struct lnUpdateFailMalformedHTLCOpaque LDKlnUpdateFailMalformedHTLC;
69+
struct lnCommitmentSignedOpaque;
70+
typedef struct lnCommitmentSignedOpaque LDKlnCommitmentSigned;
71+
struct lnRevokeAndACKOpaque;
72+
typedef struct lnRevokeAndACKOpaque LDKlnRevokeAndACK;
73+
struct lnUpdateFeeOpaque;
74+
typedef struct lnUpdateFeeOpaque LDKlnUpdateFee;
75+
struct lnChannelReestablishOpaque;
76+
typedef struct lnChannelReestablishOpaque LDKlnChannelReestablish;
77+
struct lnAnnouncementSignaturesOpaque;
78+
typedef struct lnAnnouncementSignaturesOpaque LDKlnAnnouncementSignatures;
79+
struct lnNetAddressOpaque;
80+
typedef struct lnNetAddressOpaque LDKlnNetAddress;
81+
struct lnUnsignedNodeAnnouncementOpaque;
82+
typedef struct lnUnsignedNodeAnnouncementOpaque LDKlnUnsignedNodeAnnouncement;
83+
struct lnNodeAnnouncementOpaque;
84+
typedef struct lnNodeAnnouncementOpaque LDKlnNodeAnnouncement;
85+
struct lnUnsignedChannelAnnouncementOpaque;
86+
typedef struct lnUnsignedChannelAnnouncementOpaque LDKlnUnsignedChannelAnnouncement;
87+
struct lnChannelAnnouncementOpaque;
88+
typedef struct lnChannelAnnouncementOpaque LDKlnChannelAnnouncement;
89+
struct lnChannelUpdateOpaque;
90+
typedef struct lnChannelUpdateOpaque LDKlnChannelUpdate;
91+
struct lnLightningErrorOpaque;
92+
typedef struct lnLightningErrorOpaque LDKlnLightningError;
93+
struct lnCommitmentUpdateOpaque;
94+
typedef struct lnCommitmentUpdateOpaque LDKlnCommitmentUpdate;
95+
struct lnHTLCFailChannelUpdateOpaque;
96+
typedef struct lnHTLCFailChannelUpdateOpaque LDKlnHTLCFailChannelUpdate;
97+
struct lnMessageHandlerOpaque;
98+
typedef struct lnMessageHandlerOpaque LDKlnMessageHandler;
99+
struct lnPeerHandleErrorOpaque;
100+
typedef struct lnPeerHandleErrorOpaque LDKlnPeerHandleError;
101+
struct lnPeerManagerOpaque;
102+
typedef struct lnPeerManagerOpaque LDKlnPeerManager;
103+
struct lnTxCreationKeysOpaque;
104+
typedef struct lnTxCreationKeysOpaque LDKlnTxCreationKeys;
105+
struct lnChannelPublicKeysOpaque;
106+
typedef struct lnChannelPublicKeysOpaque LDKlnChannelPublicKeys;
107+
struct lnHTLCOutputInCommitmentOpaque;
108+
typedef struct lnHTLCOutputInCommitmentOpaque LDKlnHTLCOutputInCommitment;
109+
struct lnInitFeaturesOpaque;
110+
typedef struct lnInitFeaturesOpaque LDKlnInitFeatures;
111+
struct lnNodeFeaturesOpaque;
112+
typedef struct lnNodeFeaturesOpaque LDKlnNodeFeatures;
113+
struct lnChannelFeaturesOpaque;
114+
typedef struct lnChannelFeaturesOpaque LDKlnChannelFeatures;

lightning-c-bindings/src/c_types/mod.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,12 @@ pub struct SecretKey {
2222
pub bytes: [u8; 32],
2323
}
2424
impl SecretKey {
25-
// from_rust isn't implemented since we jsut return byte array refs directly
25+
// from_rust isn't implemented for a ref since we just return byte array refs directly
26+
pub(crate) fn from_rust(sk: SecpSecretKey) -> Self {
27+
let mut bytes = [0; 32];
28+
bytes.copy_from_slice(&sk[..]);
29+
Self { bytes }
30+
}
2631
pub(crate) fn into_rust(&self) -> SecpSecretKey {
2732
SecpSecretKey::from_slice(&self.bytes).unwrap()
2833
}
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
//! " Traits and utility impls which allow other parts of rust-lightning to interact with the"
2+
//! " blockchain."
3+
//! ""
4+
//! " Includes traits for monitoring and receiving notifications of new blocks and block"
5+
//! " disconnections, transaction broadcasting, and feerate information requests."
6+
7+
use std::ffi::c_void;
8+
use bitcoin::hashes::Hash;
9+
10+
use bitcoin::blockdata::block::Block as lnBlock;
11+
use bitcoin::blockdata::block::BlockHeader as lnBlockHeader;
12+
use bitcoin::blockdata::transaction::Transaction as lnTransaction;
13+
use bitcoin::blockdata::script::Script as lnScript;
14+
use bitcoin::blockdata::constants::genesis_block as lngenesis_block;
15+
use bitcoin::util::hash::BitcoinHash as lnBitcoinHash;
16+
use bitcoin::network::constants::Network as lnNetwork;
17+
use bitcoin::hash_types::Txid as lnTxid;
18+
use bitcoin::hash_types::BlockHash as lnBlockHash;
19+
/// " Used to give chain error details upstream"
20+
#[repr(C)]
21+
pub enum ChainError {
22+
/// " Client doesn't support UTXO lookup (but the chain hash matches our genesis block hash)"
23+
NotSupported,
24+
/// " Chain isn't the one watched"
25+
NotWatched,
26+
/// " Tx doesn't exist or is unconfirmed"
27+
UnknownTx,
28+
}
29+
use lightning::chain::chaininterface::ChainError as lnChainError;
30+
impl ChainError {
31+
#[allow(unused)]
32+
pub(crate) fn to_ln(&self) -> lnChainError {
33+
match self {
34+
ChainError::NotSupported => lnChainError::NotSupported,
35+
ChainError::NotWatched => lnChainError::NotWatched,
36+
ChainError::UnknownTx => lnChainError::UnknownTx,
37+
}
38+
}
39+
#[allow(unused)]
40+
pub(crate) fn from_ln(lnt: lnChainError) -> Self {
41+
match lnt {
42+
lnChainError::NotSupported => ChainError::NotSupported,
43+
lnChainError::NotWatched => ChainError::NotWatched,
44+
lnChainError::UnknownTx => ChainError::UnknownTx,
45+
}
46+
}
47+
}
48+
/// " An interface to send a transaction to the Bitcoin network."
49+
#[repr(C)]
50+
pub struct BroadcasterInterface {
51+
pub this_arg: *mut c_void,
52+
/// " Sends a transaction out to (hopefully) be mined."
53+
pub broadcast_transaction: extern "C" fn (this_arg: *const c_void, tx: crate::c_types::Transaction),
54+
}
55+
unsafe impl Sync for BroadcasterInterface {}
56+
unsafe impl Send for BroadcasterInterface {}
57+
58+
use lightning::chain::chaininterface::BroadcasterInterface as lnBroadcasterInterface;
59+
impl lnBroadcasterInterface for BroadcasterInterface {
60+
fn broadcast_transaction(&self, tx: &bitcoin::blockdata::transaction::Transaction) {
61+
let c_tx = ::bitcoin::consensus::encode::serialize(tx);
62+
(self.broadcast_transaction)(self.this_arg, crate::c_types::Transaction::from_slice(&c_tx))
63+
}
64+
}
65+
66+
// We're essentially a pointer already, or at least a set of pointers, so allow us to be used
67+
// directly as a Deref trait in higher-level structs:
68+
impl std::ops::Deref for BroadcasterInterface {
69+
type Target = Self;
70+
fn deref(&self) -> &Self {
71+
self
72+
}
73+
}
74+
/// " An enum that represents the speed at which we want a transaction to confirm used for feerate"
75+
/// " estimation."
76+
#[repr(C)]
77+
pub enum ConfirmationTarget {
78+
/// " We are happy with this transaction confirming slowly when feerate drops some."
79+
Background,
80+
/// " We'd like this transaction to confirm without major delay, but 12-18 blocks is fine."
81+
Normal,
82+
/// " We'd like this transaction to confirm in the next few blocks."
83+
HighPriority,
84+
}
85+
use lightning::chain::chaininterface::ConfirmationTarget as lnConfirmationTarget;
86+
impl ConfirmationTarget {
87+
#[allow(unused)]
88+
pub(crate) fn to_ln(&self) -> lnConfirmationTarget {
89+
match self {
90+
ConfirmationTarget::Background => lnConfirmationTarget::Background,
91+
ConfirmationTarget::Normal => lnConfirmationTarget::Normal,
92+
ConfirmationTarget::HighPriority => lnConfirmationTarget::HighPriority,
93+
}
94+
}
95+
#[allow(unused)]
96+
pub(crate) fn from_ln(lnt: lnConfirmationTarget) -> Self {
97+
match lnt {
98+
lnConfirmationTarget::Background => ConfirmationTarget::Background,
99+
lnConfirmationTarget::Normal => ConfirmationTarget::Normal,
100+
lnConfirmationTarget::HighPriority => ConfirmationTarget::HighPriority,
101+
}
102+
}
103+
}
104+
/// " A trait which should be implemented to provide feerate information on a number of time"
105+
/// " horizons."
106+
/// ""
107+
/// " Note that all of the functions implemented here *must* be reentrant-safe (obviously - they're"
108+
/// " called from inside the library in response to ChainListener events, P2P events, or timer"
109+
/// " events)."
110+
#[repr(C)]
111+
pub struct FeeEstimator {
112+
pub this_arg: *mut c_void,
113+
/// " Gets estimated satoshis of fee required per 1000 Weight-Units."
114+
/// ""
115+
/// " Must be no smaller than 253 (ie 1 satoshi-per-byte rounded up to ensure later round-downs"
116+
/// " don't put us below 1 satoshi-per-byte)."
117+
/// ""
118+
/// " This translates to:"
119+
/// " * satoshis-per-byte * 250"
120+
/// " * ceil(satoshis-per-kbyte / 4)"
121+
pub get_est_sat_per_1000_weight: extern "C" fn (this_arg: *const c_void, confirmation_target: ConfirmationTarget) -> u64,
122+
}
123+
unsafe impl Sync for FeeEstimator {}
124+
unsafe impl Send for FeeEstimator {}
125+
126+
use lightning::chain::chaininterface::FeeEstimator as lnFeeEstimator;
127+
impl lnFeeEstimator for FeeEstimator {
128+
fn get_est_sat_per_1000_weight(&self, confirmation_target: lightning::chain::chaininterface::ConfirmationTarget) -> u64 {
129+
(self.get_est_sat_per_1000_weight)(self.this_arg, ConfirmationTarget::from_ln(confirmation_target))
130+
}
131+
}
132+
133+
// We're essentially a pointer already, or at least a set of pointers, so allow us to be used
134+
// directly as a Deref trait in higher-level structs:
135+
impl std::ops::Deref for FeeEstimator {
136+
type Target = Self;
137+
fn deref(&self) -> &Self {
138+
self
139+
}
140+
}

0 commit comments

Comments
 (0)