pub mod audit;
pub mod registry;
use std::collections::{BTreeMap, BTreeSet};
use std::error::Error;
use std::fmt::{self, Debug, Formatter};
use std::marker::PhantomData;
use std::ops;
use std::pin::Pin;
use std::sync::Arc;
use std::sync::atomic::{AtomicU64, Ordering};
use fedimint_logging::LOG_NET_API;
use futures::Future;
use jsonrpsee_core::JsonValue;
use registry::ModuleRegistry;
use serde::{Deserialize, Serialize};
use tracing::Instrument;
mod version;
pub use self::version::*;
use crate::core::{
ClientConfig, Decoder, DecoderBuilder, Input, InputError, ModuleConsensusItem,
ModuleInstanceId, ModuleKind, Output, OutputError, OutputOutcome,
};
use crate::db::{
Database, DatabaseError, DatabaseKey, DatabaseKeyWithNotify, DatabaseRecord,
DatabaseTransaction,
};
use crate::encoding::{Decodable, DecodeError, Encodable};
use crate::fmt_utils::AbbreviateHexBytes;
use crate::task::MaybeSend;
use crate::util::FmtCompact;
use crate::{Amount, apply, async_trait_maybe_send, maybe_add_send, maybe_add_send_sync};
#[derive(Debug, PartialEq, Eq)]
pub struct InputMeta {
pub amount: TransactionItemAmounts,
pub pub_key: secp256k1::PublicKey,
}
#[derive(
Debug,
Clone,
Copy,
Eq,
PartialEq,
Hash,
PartialOrd,
Ord,
Deserialize,
Serialize,
Encodable,
Decodable,
Default,
)]
pub struct AmountUnit(u64);
impl AmountUnit {
pub const BITCOIN: Self = Self(0);
pub fn is_bitcoin(self) -> bool {
self == Self::BITCOIN
}
pub fn new_custom(unit: u64) -> Self {
Self(unit)
}
pub const fn bitcoin() -> Self {
Self::BITCOIN
}
}
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
pub struct AmountWithUnit {
amounts: Amount,
unit: AmountUnit,
}
#[derive(Debug, Clone, Eq, PartialEq, Hash)]
pub struct Amounts(BTreeMap<AmountUnit, Amount>);
impl ops::Deref for Amounts {
type Target = BTreeMap<AmountUnit, Amount>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl Amounts {
pub const ZERO: Self = Self(BTreeMap::new());
pub fn new_bitcoin(amount: Amount) -> Self {
if amount == Amount::ZERO {
Self(BTreeMap::from([]))
} else {
Self(BTreeMap::from([(AmountUnit::BITCOIN, amount)]))
}
}
pub fn new_bitcoin_msats(msats: u64) -> Self {
Self::new_bitcoin(Amount::from_msats(msats))
}
pub fn new_custom(unit: AmountUnit, amount: Amount) -> Self {
if amount == Amount::ZERO {
Self(BTreeMap::from([]))
} else {
Self(BTreeMap::from([(unit, amount)]))
}
}
pub fn checked_add(mut self, rhs: &Self) -> Option<Self> {
self.checked_add_mut(rhs);
Some(self)
}
pub fn checked_add_mut(&mut self, rhs: &Self) -> Option<&mut Self> {
for (unit, amount) in &rhs.0 {
debug_assert!(
*amount != Amount::ZERO,
"`Amounts` must not add (/remove) zero-amount entries"
);
let prev = self.0.entry(*unit).or_default();
*prev = prev.checked_add(*amount)?;
}
Some(self)
}
pub fn checked_add_bitcoin(self, amount: Amount) -> Option<Self> {
self.checked_add_unit(amount, AmountUnit::BITCOIN)
}
pub fn checked_add_unit(mut self, amount: Amount, unit: AmountUnit) -> Option<Self> {
if amount == Amount::ZERO {
return Some(self);
}
let prev = self.0.entry(unit).or_default();
*prev = prev.checked_add(amount)?;
Some(self)
}
pub fn remove(&mut self, unit: &AmountUnit) -> Option<Amount> {
self.0.remove(unit)
}
pub fn get_bitcoin(&self) -> Amount {
self.get(&AmountUnit::BITCOIN).copied().unwrap_or_default()
}
pub fn expect_only_bitcoin(&self) -> Amount {
#[allow(clippy::option_if_let_else)] match self.get(&AmountUnit::BITCOIN) {
Some(amount) => {
assert!(
self.len() == 1,
"Amounts expected to contain only bitcoin and no other currencies"
);
*amount
}
None => Amount::ZERO,
}
}
pub fn iter_units(&self) -> impl Iterator<Item = AmountUnit> {
self.0.keys().copied()
}
pub fn units(&self) -> BTreeSet<AmountUnit> {
self.0.keys().copied().collect()
}
}
impl IntoIterator for Amounts {
type Item = (AmountUnit, Amount);
type IntoIter = <BTreeMap<AmountUnit, Amount> as IntoIterator>::IntoIter;
fn into_iter(self) -> Self::IntoIter {
self.0.into_iter()
}
}
#[derive(Debug, Clone, Eq, PartialEq, Hash)]
pub struct TransactionItemAmounts {
pub amounts: Amounts,
pub fees: Amounts,
}
impl TransactionItemAmounts {
pub fn checked_add(self, rhs: &Self) -> Option<Self> {
Some(Self {
amounts: self.amounts.checked_add(&rhs.amounts)?,
fees: self.fees.checked_add(&rhs.fees)?,
})
}
}
impl TransactionItemAmounts {
pub const ZERO: Self = Self {
amounts: Amounts::ZERO,
fees: Amounts::ZERO,
};
}
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct ApiRequest<T> {
pub auth: Option<ApiAuth>,
pub params: T,
}
pub type ApiRequestErased = ApiRequest<JsonValue>;
impl Default for ApiRequestErased {
fn default() -> Self {
Self {
auth: None,
params: JsonValue::Null,
}
}
}
impl ApiRequestErased {
pub fn new<T: Serialize>(params: T) -> Self {
Self {
auth: None,
params: serde_json::to_value(params)
.expect("parameter serialization error - this should not happen"),
}
}
pub fn to_json(&self) -> JsonValue {
serde_json::to_value(self).expect("parameter serialization error - this should not happen")
}
pub fn with_auth(self, auth: ApiAuth) -> Self {
Self {
auth: Some(auth),
params: self.params,
}
}
pub fn to_typed<T: serde::de::DeserializeOwned>(
self,
) -> Result<ApiRequest<T>, serde_json::Error> {
Ok(ApiRequest {
auth: self.auth,
params: serde_json::from_value::<T>(self.params)?,
})
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ApiMethod {
Core(String),
Module(ModuleInstanceId, String),
}
impl fmt::Display for ApiMethod {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Core(s) => f.write_str(s),
Self::Module(module_id, s) => f.write_fmt(format_args!("{module_id}-{s}")),
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct IrohApiRequest {
pub method: ApiMethod,
pub request: ApiRequestErased,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct IrohGatewayRequest {
pub route: String,
pub params: Option<serde_json::Value>,
pub password: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct IrohGatewayResponse {
pub status: u16,
pub body: serde_json::Value,
}
pub const FEDIMINT_API_ALPN: &[u8] = b"FEDIMINT_API_ALPN";
pub const FEDIMINT_GATEWAY_ALPN: &[u8] = b"FEDIMINT_GATEWAY_ALPN";
#[derive(Clone, Serialize, Deserialize)]
pub struct ApiAuth(String);
impl ApiAuth {
pub fn new(s: String) -> Self {
Self(s)
}
pub fn as_str(&self) -> &str {
&self.0
}
pub fn verify(&self, password: &str) -> bool {
use subtle::ConstantTimeEq as _;
bool::from(self.0.as_bytes().ct_eq(password.as_bytes()))
}
}
impl Debug for ApiAuth {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "ApiAuth(****)")
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ApiError {
pub code: i32,
pub message: String,
}
impl Error for ApiError {}
impl fmt::Display for ApiError {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
f.write_fmt(format_args!("{} {}", self.code, self.message))
}
}
pub type ApiResult<T> = Result<T, ApiError>;
impl ApiError {
pub fn new(code: i32, message: String) -> Self {
Self { code, message }
}
pub fn not_found(message: String) -> Self {
Self::new(404, message)
}
pub fn bad_request(message: String) -> Self {
Self::new(400, message)
}
pub fn unauthorized() -> Self {
Self::new(401, "Invalid authorization".to_string())
}
pub fn server_error(message: String) -> Self {
Self::new(500, message)
}
}
impl From<DatabaseError> for ApiError {
fn from(err: DatabaseError) -> Self {
Self {
code: 500,
message: format!("API server error when writing to database: {err}"),
}
}
}
pub struct ApiEndpointContext {
db: Database,
has_auth: bool,
request_auth: Option<ApiAuth>,
}
impl ApiEndpointContext {
pub fn new(db: Database, has_auth: bool, request_auth: Option<ApiAuth>) -> Self {
Self {
db,
has_auth,
request_auth,
}
}
pub fn request_auth(&self) -> Option<ApiAuth> {
self.request_auth.clone()
}
pub fn has_auth(&self) -> bool {
self.has_auth
}
pub fn db(&self) -> Database {
self.db.clone()
}
pub fn wait_key_exists<K>(&self, key: K) -> impl Future<Output = K::Value> + use<K>
where
K: DatabaseKey + DatabaseRecord + DatabaseKeyWithNotify,
{
let db = self.db.clone();
async move { db.wait_key_exists(&key).await }
}
pub fn wait_value_matches<K>(
&self,
key: K,
matcher: impl Fn(&K::Value) -> bool + Copy,
) -> impl Future<Output = K::Value>
where
K: DatabaseKey + DatabaseRecord + DatabaseKeyWithNotify,
{
let db = self.db.clone();
async move { db.wait_key_check(&key, |v| v.filter(matcher)).await.0 }
}
}
#[apply(async_trait_maybe_send!)]
pub trait TypedApiEndpoint {
type State: Sync;
const PATH: &'static str;
type Param: serde::de::DeserializeOwned + Send;
type Response: serde::Serialize;
async fn handle<'state, 'context>(
state: &'state Self::State,
context: &'context mut ApiEndpointContext,
request: Self::Param,
) -> Result<Self::Response, ApiError>;
}
pub use serde_json;
#[macro_export]
macro_rules! __api_endpoint {
(
$path:expr_2021,
// Api Version this endpoint was introduced in, at the current consensus level
// Currently for documentation purposes only.
$version_introduced:expr_2021,
async |$state:ident: &$state_ty:ty, $context:ident, $param:ident: $param_ty:ty| -> $resp_ty:ty $body:block
) => {{
struct Endpoint;
#[$crate::apply($crate::async_trait_maybe_send!)]
impl $crate::module::TypedApiEndpoint for Endpoint {
#[allow(deprecated)]
const PATH: &'static str = $path;
type State = $state_ty;
type Param = $param_ty;
type Response = $resp_ty;
async fn handle<'state, 'context>(
$state: &'state Self::State,
$context: &'context mut $crate::module::ApiEndpointContext,
$param: Self::Param,
) -> ::std::result::Result<Self::Response, $crate::module::ApiError> {
{
const __API_VERSION: $crate::module::ApiVersion = $version_introduced;
}
$body
}
}
$crate::module::ApiEndpoint::from_typed::<Endpoint>()
}};
}
pub use __api_endpoint as api_endpoint;
use self::registry::ModuleDecoderRegistry;
type HandlerFnReturn<'a> =
Pin<Box<maybe_add_send!(dyn Future<Output = Result<serde_json::Value, ApiError>> + 'a)>>;
type HandlerFn<M> = Box<
maybe_add_send_sync!(
dyn for<'a> Fn(&'a M, ApiEndpointContext, ApiRequestErased) -> HandlerFnReturn<'a>
),
>;
pub struct ApiEndpoint<M> {
pub path: &'static str,
pub handler: HandlerFn<M>,
}
static REQ_ID: AtomicU64 = AtomicU64::new(0);
impl ApiEndpoint<()> {
pub fn from_typed<E: TypedApiEndpoint>() -> ApiEndpoint<E::State>
where
<E as TypedApiEndpoint>::Response: MaybeSend,
E::Param: Debug,
E::Response: Debug,
{
async fn handle_request<'state, 'context, E>(
state: &'state E::State,
context: &'context mut ApiEndpointContext,
request: ApiRequest<E::Param>,
) -> Result<E::Response, ApiError>
where
E: TypedApiEndpoint,
E::Param: Debug,
E::Response: Debug,
{
tracing::debug!(target: LOG_NET_API, path = E::PATH, ?request, "received api request");
let result = E::handle(state, context, request.params).await;
match &result {
Err(err) => {
tracing::warn!(target: LOG_NET_API, path = E::PATH, err = %err.fmt_compact(), "api request error");
}
_ => {
tracing::trace!(target: LOG_NET_API, path = E::PATH, "api request complete");
}
}
result
}
ApiEndpoint {
path: E::PATH,
handler: Box::new(|m, mut context, request| {
Box::pin(async move {
let request = request
.to_typed()
.map_err(|e| ApiError::bad_request(e.to_string()))?;
let span = tracing::info_span!(
target: LOG_NET_API,
"api_req",
id = REQ_ID.fetch_add(1, Ordering::SeqCst),
method = E::PATH,
);
let ret = handle_request::<E>(m, &mut context, request)
.instrument(span)
.await?;
Ok(serde_json::to_value(ret).expect("encoding error"))
})
}),
}
}
}
#[apply(async_trait_maybe_send!)]
pub trait IDynCommonModuleInit: Debug {
fn decoder(&self) -> Decoder;
fn module_kind(&self) -> ModuleKind;
fn to_dyn_common(&self) -> DynCommonModuleInit;
async fn dump_database(
&self,
dbtx: &mut DatabaseTransaction<'_>,
prefix_names: Vec<String>,
) -> Box<dyn Iterator<Item = (String, Box<dyn erased_serde::Serialize + Send>)> + '_>;
}
pub trait ModuleInit: Debug + Clone + Send + Sync + 'static {
type Common: CommonModuleInit;
fn dump_database(
&self,
dbtx: &mut DatabaseTransaction<'_>,
prefix_names: Vec<String>,
) -> maybe_add_send!(
impl Future<
Output = Box<
dyn Iterator<Item = (String, Box<dyn erased_serde::Serialize + Send>)> + '_,
>,
>
);
}
#[apply(async_trait_maybe_send!)]
impl<T> IDynCommonModuleInit for T
where
T: ModuleInit,
{
fn decoder(&self) -> Decoder {
T::Common::decoder()
}
fn module_kind(&self) -> ModuleKind {
T::Common::KIND
}
fn to_dyn_common(&self) -> DynCommonModuleInit {
DynCommonModuleInit::from_inner(Arc::new(self.clone()))
}
async fn dump_database(
&self,
dbtx: &mut DatabaseTransaction<'_>,
prefix_names: Vec<String>,
) -> Box<dyn Iterator<Item = (String, Box<dyn erased_serde::Serialize + Send>)> + '_> {
<Self as ModuleInit>::dump_database(self, dbtx, prefix_names).await
}
}
dyn_newtype_define!(
#[derive(Clone)]
pub DynCommonModuleInit(Arc<IDynCommonModuleInit>)
);
impl AsRef<maybe_add_send_sync!(dyn IDynCommonModuleInit + 'static)> for DynCommonModuleInit {
fn as_ref(&self) -> &(maybe_add_send_sync!(dyn IDynCommonModuleInit + 'static)) {
self.inner.as_ref()
}
}
impl DynCommonModuleInit {
pub fn from_inner(
inner: Arc<maybe_add_send_sync!(dyn IDynCommonModuleInit + 'static)>,
) -> Self {
Self { inner }
}
}
#[apply(async_trait_maybe_send!)]
pub trait CommonModuleInit: Debug + Sized {
const CONSENSUS_VERSION: ModuleConsensusVersion;
const KIND: ModuleKind;
type ClientConfig: ClientConfig;
fn decoder() -> Decoder;
}
pub trait ModuleCommon {
type ClientConfig: ClientConfig;
type Input: Input;
type Output: Output;
type OutputOutcome: OutputOutcome;
type ConsensusItem: ModuleConsensusItem;
type InputError: InputError;
type OutputError: OutputError;
fn decoder_builder() -> DecoderBuilder {
let mut decoder_builder = Decoder::builder();
decoder_builder.with_decodable_type::<Self::ClientConfig>();
decoder_builder.with_decodable_type::<Self::Input>();
decoder_builder.with_decodable_type::<Self::Output>();
decoder_builder.with_decodable_type::<Self::OutputOutcome>();
decoder_builder.with_decodable_type::<Self::ConsensusItem>();
decoder_builder.with_decodable_type::<Self::InputError>();
decoder_builder.with_decodable_type::<Self::OutputError>();
decoder_builder
}
fn decoder() -> Decoder {
Self::decoder_builder().build()
}
}
#[derive(Clone, Eq, PartialEq, Hash, Serialize, Deserialize)]
pub struct SerdeModuleEncoding<T: Encodable + Decodable>(
#[serde(with = "::fedimint_core::encoding::as_hex")] Vec<u8>,
#[serde(skip)] PhantomData<T>,
);
#[derive(Clone, Eq, PartialEq, Hash, Serialize, Deserialize)]
pub struct SerdeModuleEncodingBase64<T: Encodable + Decodable>(
#[serde(with = "::fedimint_core::encoding::as_base64")] Vec<u8>,
#[serde(skip)] PhantomData<T>,
);
impl<T> fmt::Debug for SerdeModuleEncoding<T>
where
T: Encodable + Decodable,
{
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
f.write_str("SerdeModuleEncoding(")?;
fmt::Debug::fmt(&AbbreviateHexBytes(&self.0), f)?;
f.write_str(")")?;
Ok(())
}
}
impl<T: Encodable + Decodable> From<&T> for SerdeModuleEncoding<T> {
fn from(value: &T) -> Self {
let mut bytes = vec![];
fedimint_core::encoding::Encodable::consensus_encode(value, &mut bytes)
.expect("Writing to buffer can never fail");
Self(bytes, PhantomData)
}
}
impl<T: Encodable + Decodable + 'static> SerdeModuleEncoding<T> {
pub fn try_into_inner(&self, modules: &ModuleDecoderRegistry) -> Result<T, DecodeError> {
Decodable::consensus_decode_whole(&self.0, modules)
}
pub fn try_into_inner_known_module_kind(&self, decoder: &Decoder) -> Result<T, DecodeError> {
let mut reader = std::io::Cursor::new(&self.0);
let module_instance = ModuleInstanceId::consensus_decode_partial(
&mut reader,
&ModuleDecoderRegistry::default(),
)?;
let total_len =
u64::consensus_decode_partial(&mut reader, &ModuleDecoderRegistry::default())?;
decoder.decode_complete(
&mut reader,
total_len,
module_instance,
&ModuleRegistry::default(),
)
}
}
impl<T: Encodable + Decodable> Encodable for SerdeModuleEncoding<T> {
fn consensus_encode<W: std::io::Write>(&self, writer: &mut W) -> Result<(), std::io::Error> {
self.0.consensus_encode(writer)
}
}
impl<T: Encodable + Decodable> Decodable for SerdeModuleEncoding<T> {
fn consensus_decode_partial_from_finite_reader<R: std::io::Read>(
reader: &mut R,
modules: &ModuleDecoderRegistry,
) -> Result<Self, DecodeError> {
Ok(Self(
Vec::<u8>::consensus_decode_partial_from_finite_reader(reader, modules)?,
PhantomData,
))
}
}
impl<T> fmt::Debug for SerdeModuleEncodingBase64<T>
where
T: Encodable + Decodable,
{
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
f.write_str("SerdeModuleEncoding2(")?;
fmt::Debug::fmt(&AbbreviateHexBytes(&self.0), f)?;
f.write_str(")")?;
Ok(())
}
}
impl<T: Encodable + Decodable> From<&T> for SerdeModuleEncodingBase64<T> {
fn from(value: &T) -> Self {
let mut bytes = vec![];
fedimint_core::encoding::Encodable::consensus_encode(value, &mut bytes)
.expect("Writing to buffer can never fail");
Self(bytes, PhantomData)
}
}
impl<T: Encodable + Decodable + 'static> SerdeModuleEncodingBase64<T> {
pub fn try_into_inner(&self, modules: &ModuleDecoderRegistry) -> Result<T, DecodeError> {
Decodable::consensus_decode_whole(&self.0, modules)
}
pub fn try_into_inner_known_module_kind(&self, decoder: &Decoder) -> Result<T, DecodeError> {
let mut reader = std::io::Cursor::new(&self.0);
let module_instance = ModuleInstanceId::consensus_decode_partial(
&mut reader,
&ModuleDecoderRegistry::default(),
)?;
let total_len =
u64::consensus_decode_partial(&mut reader, &ModuleDecoderRegistry::default())?;
decoder.decode_complete(
&mut reader,
total_len,
module_instance,
&ModuleRegistry::default(),
)
}
}