use std::fmt;
use std::str::FromStr;
use thiserror::Error;
use crate::event::{
Alphabet, Coordinate, Event, EventBuilder, Kind, SingleLetterTag, Tag, TagKind,
};
use crate::types::RelayUrl;
pub const KIND_CASHU_MINT: Kind = Kind::CASHU_MINT_ANNOUNCEMENT;
pub const KIND_FEDIMINT: Kind = Kind::FEDIMINT_ANNOUNCEMENT;
pub const KIND_MINT_RECOMMENDATION: Kind = Kind::MINT_RECOMMENDATION;
const NUTS_TAG: &str = "nuts";
const MODULES_TAG: &str = "modules";
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum MintNetwork {
Mainnet,
Testnet,
Signet,
Regtest,
Other(String),
}
impl MintNetwork {
#[must_use]
pub fn parse(raw: &str) -> Self {
match raw {
"mainnet" => Self::Mainnet,
"testnet" => Self::Testnet,
"signet" => Self::Signet,
"regtest" => Self::Regtest,
other => Self::Other(other.to_owned()),
}
}
#[must_use]
pub fn as_str(&self) -> &str {
match self {
Self::Mainnet => "mainnet",
Self::Testnet => "testnet",
Self::Signet => "signet",
Self::Regtest => "regtest",
Self::Other(raw) => raw,
}
}
}
impl fmt::Display for MintNetwork {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
impl FromStr for MintNetwork {
type Err = std::convert::Infallible;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(Self::parse(s))
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum MintKind {
Cashu,
Fedimint,
}
impl MintKind {
#[must_use]
pub const fn kind(self) -> Kind {
match self {
Self::Cashu => KIND_CASHU_MINT,
Self::Fedimint => KIND_FEDIMINT,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct MintAnnouncement {
pub mint_kind: MintKind,
pub identifier: String,
pub connections: Vec<String>,
pub capabilities: Vec<String>,
pub network: Option<MintNetwork>,
pub metadata: String,
}
#[derive(Debug, Error)]
#[non_exhaustive]
pub enum MintError {
#[error("unexpected kind for NIP-87: {}", .0.as_u16())]
WrongKind(Kind),
#[error("NIP-87 event missing required `d` tag")]
MissingIdentifier,
#[error("NIP-87 announcement missing required `u` tag")]
MissingConnection,
}
impl MintAnnouncement {
pub fn new(
mint_kind: MintKind,
identifier: impl Into<String>,
connections: Vec<String>,
) -> Result<Self, MintError> {
if connections.is_empty() {
return Err(MintError::MissingConnection);
}
Ok(Self {
mint_kind,
identifier: identifier.into(),
connections,
capabilities: Vec::new(),
network: None,
metadata: String::new(),
})
}
const fn capability_tag(&self) -> &'static str {
match self.mint_kind {
MintKind::Cashu => NUTS_TAG,
MintKind::Fedimint => MODULES_TAG,
}
}
pub fn from_event(event: &Event) -> Result<Self, MintError> {
let mint_kind = match event.kind {
KIND_CASHU_MINT => MintKind::Cashu,
KIND_FEDIMINT => MintKind::Fedimint,
other => return Err(MintError::WrongKind(other)),
};
let identifier = event
.tags
.identifier()
.ok_or(MintError::MissingIdentifier)?
.to_owned();
let connections: Vec<String> = event
.tags
.iter()
.filter(|tag| tag.name() == "u")
.filter_map(|tag| tag.content().map(str::to_owned))
.collect();
if connections.is_empty() {
return Err(MintError::MissingConnection);
}
let capability_tag = match mint_kind {
MintKind::Cashu => NUTS_TAG,
MintKind::Fedimint => MODULES_TAG,
};
let capabilities = event
.tags
.find_first(&TagKind::custom(capability_tag))
.and_then(Tag::content)
.map(|raw| raw.split(',').map(|s| s.trim().to_owned()).collect())
.unwrap_or_default();
let network = event
.tags
.find_first(&TagKind::from_wire("n"))
.and_then(Tag::content)
.map(MintNetwork::parse);
Ok(Self {
mint_kind,
identifier,
connections,
capabilities,
network,
metadata: event.content.clone(),
})
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RecommendedMint {
pub coordinate: Coordinate,
pub relay_hint: Option<RelayUrl>,
pub mint_type: Option<String>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct MintRecommendation {
pub identifier: String,
pub recommended_kind: Kind,
pub connections: Vec<(String, Option<String>)>,
pub mints: Vec<RecommendedMint>,
pub review: String,
}
impl MintRecommendation {
#[must_use]
pub fn new(mint_kind: MintKind, identifier: impl Into<String>) -> Self {
Self {
identifier: identifier.into(),
recommended_kind: mint_kind.kind(),
connections: Vec::new(),
mints: Vec::new(),
review: String::new(),
}
}
pub fn from_event(event: &Event) -> Result<Self, MintError> {
if event.kind != KIND_MINT_RECOMMENDATION {
return Err(MintError::WrongKind(event.kind));
}
let identifier = event
.tags
.identifier()
.ok_or(MintError::MissingIdentifier)?
.to_owned();
let recommended_kind = event
.tags
.find_first(&TagKind::from_wire("k"))
.and_then(Tag::content)
.and_then(|raw| raw.parse().ok())
.unwrap_or(KIND_CASHU_MINT);
let connections = event
.tags
.iter()
.filter(|tag| tag.name() == "u")
.filter_map(|tag| {
let value = tag.content()?.to_owned();
let mint_type = tag.get(2).map(str::to_owned);
Some((value, mint_type))
})
.collect();
let mints = event
.tags
.iter()
.filter(|tag| tag.name() == "a")
.filter_map(|tag| {
let coordinate = Coordinate::parse(tag.content()?).ok()?;
let relay_hint = tag.get(2).and_then(|raw| RelayUrl::parse(raw).ok());
let mint_type = tag.get(3).map(str::to_owned);
Some(RecommendedMint {
coordinate,
relay_hint,
mint_type,
})
})
.collect();
Ok(Self {
identifier,
recommended_kind,
connections,
mints,
review: event.content.clone(),
})
}
}
impl EventBuilder {
#[must_use]
pub fn mint_announcement(announcement: &MintAnnouncement) -> Self {
let mut builder = Self::new(announcement.mint_kind.kind(), announcement.metadata.clone())
.tag(Tag::d(announcement.identifier.clone()));
let u_head = TagKind::from_wire("u");
for connection in &announcement.connections {
builder = builder.tag(Tag::with(&u_head, [connection.clone()]));
}
if !announcement.capabilities.is_empty() {
builder = builder.tag(Tag::with(
&TagKind::custom(announcement.capability_tag()),
[announcement.capabilities.join(",")],
));
}
if let Some(network) = &announcement.network {
builder = builder.tag(Tag::with(
&TagKind::from_wire("n"),
[network.as_str().to_owned()],
));
}
builder
}
#[must_use]
pub fn mint_recommendation(recommendation: &MintRecommendation) -> Self {
let mut builder = Self::new(KIND_MINT_RECOMMENDATION, recommendation.review.clone())
.tag(Tag::d(recommendation.identifier.clone()))
.tag(Tag::k(recommendation.recommended_kind));
let u_head = TagKind::from_wire("u");
for (connection, mint_type) in &recommendation.connections {
let mut u_args = vec![connection.clone()];
if let Some(label) = mint_type {
u_args.push(label.clone());
}
builder = builder.tag(Tag::with(&u_head, u_args));
}
for mint in &recommendation.mints {
let a_head = TagKind::single_letter(SingleLetterTag::lowercase(Alphabet::A));
let mut a_args = vec![mint.coordinate.to_wire()];
if let Some(relay) = &mint.relay_hint {
a_args.push(relay.as_str().to_owned());
}
if let Some(label) = &mint.mint_type {
a_args.push(label.clone());
}
builder = builder.tag(Tag::with(&a_head, a_args));
}
builder
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::Keys;
use crate::key::PublicKey;
fn keys() -> Keys {
Keys::parse("0000000000000000000000000000000000000000000000000000000000000003").unwrap()
}
fn mint_pubkey() -> PublicKey {
*keys().public_key()
}
#[test]
fn cashu_announcement_round_trip() {
let mut announcement = MintAnnouncement::new(
MintKind::Cashu,
mint_pubkey().to_hex(),
vec!["https://cashu.example.com".to_owned()],
)
.unwrap();
announcement.capabilities = vec!["1", "2", "3", "4"]
.into_iter()
.map(str::to_owned)
.collect();
announcement.network = Some(MintNetwork::Mainnet);
let event = EventBuilder::mint_announcement(&announcement)
.sign_with_keys(&keys())
.unwrap();
assert_eq!(event.kind, KIND_CASHU_MINT);
let parsed = MintAnnouncement::from_event(&event).unwrap();
assert_eq!(parsed, announcement);
}
#[test]
fn fedimint_announcement_round_trip() {
let mut announcement = MintAnnouncement::new(
MintKind::Fedimint,
"fed-id-1",
vec!["fed11abc".to_owned(), "fed11xyz".to_owned()],
)
.unwrap();
announcement.capabilities = vec!["lightning", "wallet", "mint"]
.into_iter()
.map(str::to_owned)
.collect();
announcement.network = Some(MintNetwork::Signet);
let event = EventBuilder::mint_announcement(&announcement)
.sign_with_keys(&keys())
.unwrap();
assert_eq!(event.kind, KIND_FEDIMINT);
let parsed = MintAnnouncement::from_event(&event).unwrap();
assert_eq!(parsed, announcement);
}
#[test]
fn unknown_network_round_trips() {
let network: MintNetwork = "mutinynet".parse().unwrap();
assert_eq!(network, MintNetwork::Other("mutinynet".to_owned()));
assert_eq!(network.as_str(), "mutinynet");
}
#[test]
fn recommendation_round_trip() {
let coordinate =
Coordinate::parse(format!("38172:{}:mint-d-id", mint_pubkey().to_hex())).unwrap();
let mut recommendation = MintRecommendation::new(MintKind::Cashu, "mint-d-id");
recommendation.review = "I trust this mint with my life".to_owned();
recommendation.connections = vec![
(
"https://cashu.example.com".to_owned(),
Some("cashu".to_owned()),
),
("fed11abc".to_owned(), Some("fedimint".to_owned())),
];
recommendation.mints = vec![RecommendedMint {
coordinate,
relay_hint: Some(RelayUrl::parse("wss://relay1.example/").unwrap()),
mint_type: Some("cashu".to_owned()),
}];
let event = EventBuilder::mint_recommendation(&recommendation)
.sign_with_keys(&keys())
.unwrap();
let parsed = MintRecommendation::from_event(&event).unwrap();
assert_eq!(parsed, recommendation);
assert_eq!(parsed.recommended_kind, KIND_CASHU_MINT);
}
#[test]
fn recommendation_without_optional_labels_round_trip() {
let coordinate =
Coordinate::parse(format!("38172:{}:mint-d-id", mint_pubkey().to_hex())).unwrap();
let mut recommendation = MintRecommendation::new(MintKind::Cashu, "mint-d-id");
recommendation.connections = vec![("https://cashu.example.com".to_owned(), None)];
recommendation.mints = vec![RecommendedMint {
coordinate,
relay_hint: None,
mint_type: None,
}];
let event = EventBuilder::mint_recommendation(&recommendation)
.sign_with_keys(&keys())
.unwrap();
let parsed = MintRecommendation::from_event(&event).unwrap();
assert_eq!(parsed, recommendation);
}
#[test]
fn missing_connection_is_rejected() {
assert!(matches!(
MintAnnouncement::new(MintKind::Cashu, "id", Vec::new()),
Err(MintError::MissingConnection)
));
}
#[test]
fn wrong_kind_is_rejected() {
let event = EventBuilder::text_note("nope")
.sign_with_keys(&keys())
.unwrap();
assert!(matches!(
MintAnnouncement::from_event(&event),
Err(MintError::WrongKind(_))
));
assert!(matches!(
MintRecommendation::from_event(&event),
Err(MintError::WrongKind(_))
));
}
}