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//! * `4`: ppm fees round up to a satoshi instead of down; ppm expiry fees
33//!   are calculated on the exact total across all VTXOs
34
35#[cfg(all(any(target_os = "android", target_os = "ios"), feature = "tls-native-roots"))]
36compile_error!("feature `tls-native-roots` can't be used on Android or iOS, use `tls-webpki-roots` instead");
37
38pub extern crate tonic;
39
40// Generated gRPC method lookup from proto files (server-only)
41#[cfg(feature = "server")]
42include!(concat!(env!("OUT_DIR"), "/grpc_methods.rs"));
43
44mod convert;
45pub use crate::convert::{ConvertError, TryFromBytes};
46
47mod error;
48pub use crate::error::StatusExt;
49
50pub mod pver;
51
52pub mod client;
53pub mod protos {
54	pub mod core {
55		tonic::include_proto!("core");
56	}
57	pub use self::core::*;
58	pub mod bark_server {
59		tonic::include_proto!("bark_server");
60	}
61	pub use self::bark_server::*;
62	pub mod intman {
63		tonic::include_proto!("intman");
64	}
65	pub mod mailbox_server {
66		tonic::include_proto!("mailbox_server");
67	}
68}
69
70pub use client::ServerConnection;
71pub use crate::protos::bark_server::ark_service_client::ArkServiceClient;
72
73pub mod admin {
74	pub use crate::protos::bark_server::wallet_admin_service_client::WalletAdminServiceClient;
75	pub use crate::protos::bark_server::round_admin_service_client::RoundAdminServiceClient;
76	pub use crate::protos::bark_server::lightning_admin_service_client::LightningAdminServiceClient;
77	pub use crate::protos::bark_server::sweep_admin_service_client::SweepAdminServiceClient;
78	pub use crate::protos::bark_server::ban_admin_service_client::BanAdminServiceClient;
79}
80
81#[cfg(feature = "intman")]
82pub mod intman {
83	pub use crate::protos::intman::integration_service_client::IntegrationServiceClient;
84}
85
86#[cfg(feature = "server")]
87pub mod server {
88	pub use crate::protos::bark_server::ark_service_server::{ArkService, ArkServiceServer};
89	pub use crate::protos::bark_server::wallet_admin_service_server::{WalletAdminService, WalletAdminServiceServer};
90	pub use crate::protos::bark_server::round_admin_service_server::{RoundAdminService, RoundAdminServiceServer};
91	pub use crate::protos::bark_server::lightning_admin_service_server::{LightningAdminService, LightningAdminServiceServer};
92	pub use crate::protos::bark_server::sweep_admin_service_server::{SweepAdminService, SweepAdminServiceServer};
93	pub use crate::protos::bark_server::ban_admin_service_server::{BanAdminService, BanAdminServiceServer};
94	pub use crate::protos::intman::integration_service_server::{IntegrationService, IntegrationServiceServer};
95	pub use crate::protos::mailbox_server::mailbox_service_server::{MailboxService, MailboxServiceServer};
96}
97
98pub mod mailbox {
99	pub use crate::protos::mailbox_server::mailbox_service_client::MailboxServiceClient;
100}
101
102
103use std::borrow::BorrowMut;
104use std::str::FromStr;
105use std::time::Duration;
106
107use bitcoin::{Address, Amount, OutPoint};
108use bitcoin::address::NetworkUnchecked;
109
110
111/// The minimum protocol version supported by the client.
112///
113/// For info on protocol versions, see [server_rpc](crate) module documentation.
114pub const MIN_PROTOCOL_VERSION: u64 = pver::PROTOCOL_VERSION_BASE;
115
116/// The maximum protocol version supported by the client.
117///
118/// For info on protocol versions, see [server_rpc](crate) module documentation.
119pub const MAX_PROTOCOL_VERSION: u64 = pver::PROTOCOL_VERSION_PPM_FEE_TOTAL;
120
121/// The string used in the gRPC HTTP header for the protocol version.
122pub const PROTOCOL_VERSION_HEADER: &str = "pver";
123
124/// The maximum number of recovery IDs that the server accepts per request.
125pub const MAX_NB_MAILBOX_RECOVERY_IDS: usize = 20;
126
127/// The maximum number of vtxo IDs that the server accepts per forfeit nonces request.
128pub const MAX_NB_FORFEIT_NONCE_IDS: usize = 1000;
129
130/// The maximum number of vtxos that the server accepts per arkoor mailbox
131/// post. Generous for a single payment's outputs to one recipient; clients
132/// sending more should split into multiple posts.
133pub const MAX_NB_MAILBOX_ARKOOR_VTXOS: usize = 100;
134
135/// The maximum number of inputs of a board funding tx
136pub const MAX_NB_BOARD_FUNDING_INPUTS: usize = 100;
137
138
139#[derive(Debug, Clone)]
140pub struct WalletStatus {
141	pub address: Address<NetworkUnchecked>,
142	pub total_balance: Amount,
143	pub trusted_balance: Amount,
144	pub untrusted_balance: Amount,
145	pub confirmed_utxos: Vec<OutPoint>,
146	pub unconfirmed_utxos: Vec<OutPoint>,
147}
148
149/// Extension trait on [tonic::Request].
150pub trait RequestExt<T>: BorrowMut<tonic::Request<T>> {
151	/// Check for the protocol version header.
152	///
153	/// Returns None in case of missing header.
154	fn try_pver(&self) -> Result<Option<u64>, tonic::Status> {
155		self.borrow().metadata().get(PROTOCOL_VERSION_HEADER).map(|v| {
156			v.to_str().ok().and_then(|s| u64::from_str(s).ok())
157				.ok_or_else(|| tonic::Status::invalid_argument("invalid protocol version header"))
158		}).transpose()
159	}
160
161	/// Check for the protocol version header.
162	///
163	/// Returns error in case of missing header.
164	fn pver(&self) -> Result<u64, tonic::Status> {
165		self.try_pver()?.ok_or_else(|| tonic::Status::invalid_argument("missing pver header"))
166	}
167
168	/// Set the protocol version header.
169	fn set_pver(&mut self, pver: u64) {
170		self.borrow_mut().metadata_mut().insert(PROTOCOL_VERSION_HEADER, pver.into());
171	}
172
173	/// Sets a request timeout only if no timeout has already been set
174	fn set_default_timeout(&mut self, timeout: Duration) {
175		const GRPC_TIMEOUT_HEADER: &str = "grpc-timeout";
176
177		let slf = self.borrow_mut();
178		if slf.metadata().get(GRPC_TIMEOUT_HEADER).is_none () {
179			slf.set_timeout(timeout);
180		}
181	}
182}
183impl<T> RequestExt<T> for tonic::Request<T> {}