Skip to main content

server_rpc/
lib.rs

1//!
2//!
3//! # Note on protocol version
4//!
5//! Whenever anything changes related to the interactions between client and
6//! server, including protocol encoding versions, gRPC data fields, expected
7//! behavior etc, that is not automatically backwards compatible, the protocol
8//! version is bumped.
9//!
10//! Both the server and the client can be implemented so as to support multiple
11//! different protocol versions. This gives maximum flexibility to implement
12//! compatibility for both sides.
13//!
14//! The server advertises the protocol versions it supports and the client picks
15//! one and sets it in the header of each subsequent request.
16//!
17//! However, the server will only check the protocol version in cases where
18//! behavior can be different for different versions. This gives outdated or
19//! exotic clients an extra level of flexibility, as calls that have not
20//! changed since the latest supported protocol version might still work.
21//!
22//! This makes the server maximally flexible and puts all the responsibility
23//! on the client. Our own client implementation bails out if it cannot speak
24//! any of the supported protocol versions the server supports.
25//!
26//! ## Protocol version changelog
27//!
28//! * `1`: initial version
29//! * `2`: fixed the offboard sighash for multi-input offboards
30//! * `3`: checkpoint the lightning-receive claim so the watchman can't
31//!   force-exit a freshly claimed VTXO
32
33#[cfg(all(any(target_os = "android", target_os = "ios"), feature = "tls-native-roots"))]
34compile_error!("feature `tls-native-roots` can't be used on Android or iOS, use `tls-webpki-roots` instead");
35
36pub extern crate tonic;
37
38// Generated gRPC method lookup from proto files (server-only)
39#[cfg(feature = "server")]
40include!(concat!(env!("OUT_DIR"), "/grpc_methods.rs"));
41
42mod convert;
43pub use crate::convert::{ConvertError, TryFromBytes};
44
45mod error;
46pub use crate::error::StatusExt;
47
48pub mod pver;
49
50pub mod client;
51pub mod protos {
52	pub mod core {
53		tonic::include_proto!("core");
54	}
55	pub use self::core::*;
56	pub mod bark_server {
57		tonic::include_proto!("bark_server");
58	}
59	pub use self::bark_server::*;
60	pub mod intman {
61		tonic::include_proto!("intman");
62	}
63	pub mod mailbox_server {
64		tonic::include_proto!("mailbox_server");
65	}
66}
67
68pub use client::ServerConnection;
69pub use crate::protos::bark_server::ark_service_client::ArkServiceClient;
70
71pub mod admin {
72	pub use crate::protos::bark_server::wallet_admin_service_client::WalletAdminServiceClient;
73	pub use crate::protos::bark_server::round_admin_service_client::RoundAdminServiceClient;
74	pub use crate::protos::bark_server::lightning_admin_service_client::LightningAdminServiceClient;
75	pub use crate::protos::bark_server::sweep_admin_service_client::SweepAdminServiceClient;
76	pub use crate::protos::bark_server::ban_admin_service_client::BanAdminServiceClient;
77}
78
79#[cfg(feature = "intman")]
80pub mod intman {
81	pub use crate::protos::intman::integration_service_client::IntegrationServiceClient;
82}
83
84#[cfg(feature = "server")]
85pub mod server {
86	pub use crate::protos::bark_server::ark_service_server::{ArkService, ArkServiceServer};
87	pub use crate::protos::bark_server::wallet_admin_service_server::{WalletAdminService, WalletAdminServiceServer};
88	pub use crate::protos::bark_server::round_admin_service_server::{RoundAdminService, RoundAdminServiceServer};
89	pub use crate::protos::bark_server::lightning_admin_service_server::{LightningAdminService, LightningAdminServiceServer};
90	pub use crate::protos::bark_server::sweep_admin_service_server::{SweepAdminService, SweepAdminServiceServer};
91	pub use crate::protos::bark_server::ban_admin_service_server::{BanAdminService, BanAdminServiceServer};
92	pub use crate::protos::intman::integration_service_server::{IntegrationService, IntegrationServiceServer};
93	pub use crate::protos::mailbox_server::mailbox_service_server::{MailboxService, MailboxServiceServer};
94}
95
96pub mod mailbox {
97	pub use crate::protos::mailbox_server::mailbox_service_client::MailboxServiceClient;
98}
99
100
101use std::borrow::BorrowMut;
102use std::str::FromStr;
103use std::time::Duration;
104
105use bitcoin::{Address, Amount, OutPoint};
106use bitcoin::address::NetworkUnchecked;
107
108
109/// The minimum protocol version supported by the client.
110///
111/// For info on protocol versions, see [server_rpc](crate) module documentation.
112pub const MIN_PROTOCOL_VERSION: u64 = pver::PROTOCOL_VERSION_BASE;
113
114/// The maximum protocol version supported by the client.
115///
116/// For info on protocol versions, see [server_rpc](crate) module documentation.
117pub const MAX_PROTOCOL_VERSION: u64 = pver::PROTOCOL_VERSION_LN_RECEIVE_CHECKPOINT;
118
119/// The string used in the gRPC HTTP header for the protocol version.
120pub const PROTOCOL_VERSION_HEADER: &str = "pver";
121
122
123#[derive(Debug, Clone)]
124pub struct WalletStatus {
125	pub address: Address<NetworkUnchecked>,
126	pub total_balance: Amount,
127	pub trusted_balance: Amount,
128	pub untrusted_balance: Amount,
129	pub confirmed_utxos: Vec<OutPoint>,
130	pub unconfirmed_utxos: Vec<OutPoint>,
131}
132
133/// Extension trait on [tonic::Request].
134pub trait RequestExt<T>: BorrowMut<tonic::Request<T>> {
135	/// Check for the protocol version header.
136	///
137	/// Returns None in case of missing header.
138	fn try_pver(&self) -> Result<Option<u64>, tonic::Status> {
139		self.borrow().metadata().get(PROTOCOL_VERSION_HEADER).map(|v| {
140			v.to_str().ok().and_then(|s| u64::from_str(s).ok())
141				.ok_or_else(|| tonic::Status::invalid_argument("invalid protocol version header"))
142		}).transpose()
143	}
144
145	/// Check for the protocol version header.
146	///
147	/// Returns error in case of missing header.
148	fn pver(&self) -> Result<u64, tonic::Status> {
149		self.try_pver()?.ok_or_else(|| tonic::Status::invalid_argument("missing pver header"))
150	}
151
152	/// Set the protocol version header.
153	fn set_pver(&mut self, pver: u64) {
154		self.borrow_mut().metadata_mut().insert(PROTOCOL_VERSION_HEADER, pver.into());
155	}
156
157	/// Sets a request timeout only if no timeout has already been set
158	fn set_default_timeout(&mut self, timeout: Duration) {
159		const GRPC_TIMEOUT_HEADER: &str = "grpc-timeout";
160
161		let slf = self.borrow_mut();
162		if slf.metadata().get(GRPC_TIMEOUT_HEADER).is_none () {
163			slf.set_timeout(timeout);
164		}
165	}
166}
167impl<T> RequestExt<T> for tonic::Request<T> {}