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;
40use std::borrow::BorrowMut;
41
42pub use convert::{ConvertError, TryFromBytes};
43
44pub mod client;
45pub mod protos {
46	pub mod core {
47		tonic::include_proto!("core");
48	}
49	pub use core::*;
50	pub mod bark_server {
51		tonic::include_proto!("bark_server");
52	}
53	pub use bark_server::*;
54	pub mod intman {
55		tonic::include_proto!("intman");
56	}
57	pub mod mailbox_server {
58		tonic::include_proto!("mailbox_server");
59	}
60}
61
62pub use client::ServerConnection;
63pub use crate::protos::bark_server::ark_service_client::ArkServiceClient;
64
65pub mod admin {
66	pub use crate::protos::bark_server::wallet_admin_service_client::WalletAdminServiceClient;
67	pub use crate::protos::bark_server::round_admin_service_client::RoundAdminServiceClient;
68	pub use crate::protos::bark_server::lightning_admin_service_client::LightningAdminServiceClient;
69	pub use crate::protos::bark_server::sweep_admin_service_client::SweepAdminServiceClient;
70	pub use crate::protos::bark_server::ban_admin_service_client::BanAdminServiceClient;
71}
72
73#[cfg(feature = "intman")]
74pub mod intman {
75	pub use crate::protos::intman::integration_service_client::IntegrationServiceClient;
76}
77
78#[cfg(feature = "server")]
79pub mod server {
80	pub use crate::protos::bark_server::ark_service_server::{ArkService, ArkServiceServer};
81	pub use crate::protos::bark_server::wallet_admin_service_server::{WalletAdminService, WalletAdminServiceServer};
82	pub use crate::protos::bark_server::round_admin_service_server::{RoundAdminService, RoundAdminServiceServer};
83	pub use crate::protos::bark_server::lightning_admin_service_server::{LightningAdminService, LightningAdminServiceServer};
84	pub use crate::protos::bark_server::sweep_admin_service_server::{SweepAdminService, SweepAdminServiceServer};
85	pub use crate::protos::bark_server::ban_admin_service_server::{BanAdminService, BanAdminServiceServer};
86	pub use crate::protos::intman::integration_service_server::{IntegrationService, IntegrationServiceServer};
87	pub use crate::protos::mailbox_server::mailbox_service_server::{MailboxService, MailboxServiceServer};
88}
89
90pub mod mailbox {
91	pub use crate::protos::mailbox_server::mailbox_service_client::MailboxServiceClient;
92}
93
94
95use std::str::FromStr;
96
97use bitcoin::{Address, Amount, OutPoint};
98use bitcoin::address::NetworkUnchecked;
99
100/// The string used in the gRPC HTTP header for the protocol version.
101pub const PROTOCOL_VERSION_HEADER: &str = "pver";
102
103#[derive(Debug, Clone)]
104pub struct WalletStatus {
105	pub address: Address<NetworkUnchecked>,
106	pub total_balance: Amount,
107	pub trusted_balance: Amount,
108	pub untrusted_balance: Amount,
109	pub confirmed_utxos: Vec<OutPoint>,
110	pub unconfirmed_utxos: Vec<OutPoint>,
111}
112
113/// Extension trait on [tonic::Request].
114pub trait RequestExt<T>: BorrowMut<tonic::Request<T>> {
115	/// Check for the protocol version header.
116	///
117	/// Returns None in case of missing header.
118	fn try_pver(&self) -> Result<Option<u64>, tonic::Status> {
119		self.borrow().metadata().get(PROTOCOL_VERSION_HEADER).map(|v| {
120			v.to_str().ok().and_then(|s| u64::from_str(s).ok())
121				.ok_or_else(|| tonic::Status::invalid_argument("invalid protocol version header"))
122		}).transpose()
123	}
124
125	/// Check for the protocol version header.
126	///
127	/// Returns error in case of missing header.
128	fn pver(&self) -> Result<u64, tonic::Status> {
129		self.try_pver()?.ok_or_else(|| tonic::Status::invalid_argument("missing pver header"))
130	}
131
132	/// Set the protocol version header.
133	fn set_pver(&mut self, pver: u64) {
134		self.borrow_mut().metadata_mut().insert(PROTOCOL_VERSION_HEADER, pver.into());
135	}
136}
137impl<T> RequestExt<T> for tonic::Request<T> {}