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
30#[cfg(all(any(target_os = "android", target_os = "ios"), feature = "tls-native-roots"))]
31compile_error!("feature `tls-native-roots` can't be used on Android or iOS, use `tls-webpki-roots` instead");
32
33pub extern crate tonic;
34
35// Generated gRPC method lookup from proto files (server-only)
36#[cfg(feature = "server")]
37include!(concat!(env!("OUT_DIR"), "/grpc_methods.rs"));
38
39mod convert;
40pub use crate::convert::{ConvertError, TryFromBytes};
41
42mod error;
43pub use crate::error::StatusExt;
44
45pub mod pver;
46
47pub mod client;
48pub mod protos {
49	pub mod core {
50		tonic::include_proto!("core");
51	}
52	pub use self::core::*;
53	pub mod bark_server {
54		tonic::include_proto!("bark_server");
55	}
56	pub use self::bark_server::*;
57	pub mod intman {
58		tonic::include_proto!("intman");
59	}
60	pub mod mailbox_server {
61		tonic::include_proto!("mailbox_server");
62	}
63}
64
65pub use client::ServerConnection;
66pub use crate::protos::bark_server::ark_service_client::ArkServiceClient;
67
68pub mod admin {
69	pub use crate::protos::bark_server::wallet_admin_service_client::WalletAdminServiceClient;
70	pub use crate::protos::bark_server::round_admin_service_client::RoundAdminServiceClient;
71	pub use crate::protos::bark_server::lightning_admin_service_client::LightningAdminServiceClient;
72	pub use crate::protos::bark_server::sweep_admin_service_client::SweepAdminServiceClient;
73	pub use crate::protos::bark_server::ban_admin_service_client::BanAdminServiceClient;
74}
75
76#[cfg(feature = "intman")]
77pub mod intman {
78	pub use crate::protos::intman::integration_service_client::IntegrationServiceClient;
79}
80
81#[cfg(feature = "server")]
82pub mod server {
83	pub use crate::protos::bark_server::ark_service_server::{ArkService, ArkServiceServer};
84	pub use crate::protos::bark_server::wallet_admin_service_server::{WalletAdminService, WalletAdminServiceServer};
85	pub use crate::protos::bark_server::round_admin_service_server::{RoundAdminService, RoundAdminServiceServer};
86	pub use crate::protos::bark_server::lightning_admin_service_server::{LightningAdminService, LightningAdminServiceServer};
87	pub use crate::protos::bark_server::sweep_admin_service_server::{SweepAdminService, SweepAdminServiceServer};
88	pub use crate::protos::bark_server::ban_admin_service_server::{BanAdminService, BanAdminServiceServer};
89	pub use crate::protos::intman::integration_service_server::{IntegrationService, IntegrationServiceServer};
90	pub use crate::protos::mailbox_server::mailbox_service_server::{MailboxService, MailboxServiceServer};
91}
92
93pub mod mailbox {
94	pub use crate::protos::mailbox_server::mailbox_service_client::MailboxServiceClient;
95}
96
97
98use std::borrow::BorrowMut;
99use std::str::FromStr;
100
101use bitcoin::{Address, Amount, OutPoint};
102use bitcoin::address::NetworkUnchecked;
103
104
105/// The minimum protocol version supported by the client.
106///
107/// For info on protocol versions, see [server_rpc](crate) module documentation.
108pub const MIN_PROTOCOL_VERSION: u64 = crate::pver::PROTOCOL_VERSION_BASE;
109
110/// The maximum protocol version supported by the client.
111///
112/// For info on protocol versions, see [server_rpc](crate) module documentation.
113pub const MAX_PROTOCOL_VERSION: u64 = crate::pver::PROTOCOL_VERSION_OFFBOARD_FIX;
114
115/// The string used in the gRPC HTTP header for the protocol version.
116pub const PROTOCOL_VERSION_HEADER: &str = "pver";
117
118
119#[derive(Debug, Clone)]
120pub struct WalletStatus {
121	pub address: Address<NetworkUnchecked>,
122	pub total_balance: Amount,
123	pub trusted_balance: Amount,
124	pub untrusted_balance: Amount,
125	pub confirmed_utxos: Vec<OutPoint>,
126	pub unconfirmed_utxos: Vec<OutPoint>,
127}
128
129/// Extension trait on [tonic::Request].
130pub trait RequestExt<T>: BorrowMut<tonic::Request<T>> {
131	/// Check for the protocol version header.
132	///
133	/// Returns None in case of missing header.
134	fn try_pver(&self) -> Result<Option<u64>, tonic::Status> {
135		self.borrow().metadata().get(PROTOCOL_VERSION_HEADER).map(|v| {
136			v.to_str().ok().and_then(|s| u64::from_str(s).ok())
137				.ok_or_else(|| tonic::Status::invalid_argument("invalid protocol version header"))
138		}).transpose()
139	}
140
141	/// Check for the protocol version header.
142	///
143	/// Returns error in case of missing header.
144	fn pver(&self) -> Result<u64, tonic::Status> {
145		self.try_pver()?.ok_or_else(|| tonic::Status::invalid_argument("missing pver header"))
146	}
147
148	/// Set the protocol version header.
149	fn set_pver(&mut self, pver: u64) {
150		self.borrow_mut().metadata_mut().insert(PROTOCOL_VERSION_HEADER, pver.into());
151	}
152}
153impl<T> RequestExt<T> for tonic::Request<T> {}