breez_sdk_core/lib.rs
1//! # Breez SDK
2//!
3//! The Breez SDK enables mobile developers to integrate Lightning and bitcoin payments into their
4//! apps with a very shallow learning curve. The use cases are endless – from social apps that want
5//! to integrate tipping between users to content-creation apps interested in adding bitcoin monetization.
6//! Crucially, this SDK is an end-to-end, non-custodial, drop-in solution powered by Greenlight,
7//! a built-in LSP, on-chain interoperability, third-party fiat on-ramps, and other services users
8//! and operators need.
9//!
10//! The Breez SDK provides the following services:
11//! * Sending payments (via various protocols such as: bolt11, keysend, lnurl-pay, lightning address, etc.)
12//! * Receiving payments (via various protocols such as: bolt11, lnurl-withdraw, etc.)
13//! * Fetching node status (e.g. balance, max allow to pay, max allow to receive, on-chain balance, etc.)
14//! * Connecting to a new or existing node.
15//!
16//! ## Getting Started
17//!
18//! First, make sure you have your API Key and Invite Code ready (see [API Key and Invite Code](#api-key-and-invite-code) section below).
19//!
20//! The following code initialize the SDK and make it ready to be used:
21//!
22//! ```ignore
23//! let mnemonic = Mnemonic::generate_in(Language::English, 12)?;
24//! let seed = mnemonic.to_seed("");
25//! let invite_code = Some("...".into());
26//!
27//! let mut config = BreezServices::default_config(
28//! EnvironmentType::Production,
29//! "your API key".into(),
30//! breez_sdk_core::NodeConfig::Greenlight {
31//! config: GreenlightNodeConfig { partner_credentials: None, invite_code },
32//! },
33//! );
34//!
35//! // Customize the config object according to your needs
36//! config.working_dir = "path to an existing directory".into();
37//!
38//! // Connect to the Breez SDK make it ready for use
39//! let sdk = BreezServices::connect(
40//! config,
41//! seed.to_vec(),
42//! Box::new(AppEventListener {}),
43//! )
44//! .await?;
45//!
46//! ```
47//!
48//! We can now receive payments
49//!
50//! ```ignore
51//! let invoice = sdk.receive_payment(3000, "Invoice for 3000 sats".into()).await?;
52//! ```
53//!
54//! or make payments
55//! ```ignore
56//! let bolt11 = "...";
57//! sdk.send_payment(bolt11.into(), Some(3000)).await?;
58//! ```
59//!
60//! At any point we can fetch our balance from the Greenlight node
61//! ```ignore
62//! if let Some(node_state) = sdk.node_info()? {
63//! let balance_ln = node_state.channels_balance_msat;
64//! let balance_onchain = node_state.onchain_balance_msat;
65//! }
66//! ```
67//!
68//! or fetch other useful infos, like the current mempool [RecommendedFees]
69//! ```ignore
70//! let fees: RecommendedFees = sdk.recommended_fees().await?;
71//! ```
72//!
73//! These different types of operations are described below in more detail.
74//!
75//! ### A. Initializing the SDK
76//!
77//! There are two simple steps necessary to initialize the SDK:
78//!
79//! 1. [BreezServices::default_config] to construct the sdk configuration
80//! 2. [BreezServices::connect] to connect to your node and start all required Breez SDK services
81//!
82//! The first step takes the [EnvironmentType] and [NodeConfig] as arguments. Although you can create
83//! your own config from scratch it is recommended to use the [BreezServices::default_config] method and
84//! customize it according to your needs.
85//! Once the [NodeConfig] is created it is passed to the [BreezServices::connect] method along with the seed and and implementation of [EventListener] which is used to
86//! notify the caller of SDK events.
87//!
88//! Now your SDK is ready to be used.
89//!
90//! ### B. Sending and receiving Lightning payments
91//!
92//! Supported BOLT11 operations are
93//!
94//! * [BreezServices::receive_payment] to create an invoice
95//! * [BreezServices::send_payment] to pay an invoice
96//! * [BreezServices::send_spontaneous_payment] for keysend payments
97//!
98//! ### C. Receiving an on-chain transaction (swap-in)
99//!
100//! * [BreezServices::receive_onchain] accepting an optional user-selected [OpeningFeeParams] for
101//! the case when the operation requires a new channel with the LSP
102//! * [BreezServices::in_progress_swap]
103//! * [BreezServices::list_refundables] to get a list of swaps
104//! * [BreezServices::refund] to broadcast a transaction for failed or expired swaps
105//!
106//! ### D. Sending to an on-chain address (swap-out)
107//!
108//! * [BreezServices::fetch_reverse_swap_fees] to get the current swap-out fees
109//! * [BreezServices::prepare_onchain_payment] to prepare the swap-out
110//! * [BreezServices::pay_onchain] to start the swap-out
111//! * [BreezServices::in_progress_onchain_payments] to see any in-progress swaps
112//!
113//! ### E. Using LNURL
114//!
115//! 1. [parse] the LNURL endpoint URL to get the workflow parameters.
116//! 2. After getting the user input or confirmation, complete the workflow with [BreezServices::lnurl_pay] or
117//! [BreezServices::lnurl_withdraw].
118//!
119//! ### F. Supporting fiat currencies
120//!
121//! * [BreezServices::list_fiat_currencies] to get the supported fiat currencies
122//! * [BreezServices::fetch_fiat_rates] to get the current exchange rates
123//! * [BreezServices::recommended_fees] for the recommended mempool fees
124//!
125//! ### G. Connecting to an LSP
126//!
127//! * [BreezServices::list_lsps] to get a list of available LSPs
128//! * [BreezServices::connect_lsp] to connect to a chosen LSP
129//! * [BreezServices::lsp_info] to get [LspInformation] on the currently selected LSP
130//!
131//! ### H. Utilities
132//!
133//! Use [parse] to parse generic input. The input can come from the user, from a clicked link or from a QR code.
134//! The resulting [InputType] will tell you what the input is and how to treat it, as well as present relevant payload data
135//! in a structured form.
136//!
137//!
138//! ## Bindings
139//!
140//! * C#
141//! * Dart
142//! * Go
143//! * Kotlin
144//! * Python
145//! * React-Native
146//! * Swift
147//!
148//!
149//! ## API Key and Invite Code
150//!
151//! You will need an API Key to use the SDK, as well as an Invite Code when you create a new node.
152//!
153//! To get both of them, please contact Breez via email at <contact@breez.technology> or at <https://breez.technology/#contact-us-form>
154//!
155//! ## Support
156//!
157//! Join this [telegram group](https://t.me/breezsdk).
158
159#[allow(clippy::all)]
160mod bridge_generated; /* AUTO INJECTED BY flutter_rust_bridge. This line may not be accurate, and you can change it according to your needs. */
161
162#[macro_use]
163extern crate log;
164
165mod backup;
166pub mod binding;
167mod breez_services;
168mod chain;
169mod crypt;
170pub mod error;
171#[rustfmt::skip]
172mod node_api; // flutter_rust_bridge_codegen: has to be defined before greenlight; greenlight::node_api
173mod greenlight;
174#[rustfmt::skip]
175pub mod lnurl;
176mod buy;
177mod lsp;
178mod lsps0;
179mod lsps2;
180mod models;
181mod persist;
182mod serializer;
183mod support;
184mod swap_in;
185mod swap_out;
186#[allow(clippy::all)]
187#[allow(unused_mut)]
188#[allow(dead_code)]
189mod test_utils;
190
191pub use breez_services::{
192 mnemonic_to_seed, BackupFailedData, BreezEvent, BreezServices, CheckMessageRequest,
193 CheckMessageResponse, EventListener, InvoicePaidDetails, LogStream, PaymentFailedData,
194 SignMessageRequest, SignMessageResponse,
195};
196pub use chain::RecommendedFees;
197pub use lsp::LspInformation;
198pub use models::*;
199pub use sdk_common::prelude::*;
200pub use swap_out::reverseswap::{ESTIMATED_CLAIM_TX_VSIZE, ESTIMATED_LOCKUP_TX_VSIZE};