Skip to content

Commit

Permalink
nostr: add NIP-31 support
Browse files Browse the repository at this point in the history
Signed-off-by: Yuki Kishimoto <[email protected]>
  • Loading branch information
yukibtc committed Jul 22, 2024
1 parent 743d711 commit fc16960
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

### Added

* nostr: add NIP-31 support ([Yuki Kishimoto])
* nostr: add NIP-70 support ([Yuki Kishimoto])
* nostr: add `EventId::LEN` const ([Yuki Kishimoto])
* nostr: add `UnsignedEvent::ensure_id` method ([Yuki Kishimoto])
Expand Down
27 changes: 27 additions & 0 deletions bindings/nostr-ffi/src/event/tag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ pub enum TagKind {
///
/// <https://github.com/nostr-protocol/nips/blob/master/70.md>
Protected,
/// Human-readable plaintext summary of what that event is about
///
/// <https://github.com/nostr-protocol/nips/blob/master/31.md>
Alt,
/// Relay
RelayUrl,
/// Nonce
Expand Down Expand Up @@ -125,6 +129,7 @@ impl<'a> From<tag::TagKind<'a>> for TagKind {
single_letter: Arc::new(single_letter.into()),
},
tag::TagKind::Protected => Self::Protected,
tag::TagKind::Alt => Self::Alt,
tag::TagKind::Relay => Self::RelayUrl,
tag::TagKind::Nonce => Self::Nonce,
tag::TagKind::Delegation => Self::Delegation,
Expand Down Expand Up @@ -177,6 +182,7 @@ impl<'a> From<TagKind> for tag::TagKind<'a> {
match value {
TagKind::SingleLetter { single_letter } => Self::SingleLetter(**single_letter),
TagKind::Protected => Self::Protected,
TagKind::Alt => Self::Alt,
TagKind::RelayUrl => Self::Relay,
TagKind::Nonce => Self::Nonce,
TagKind::Delegation => Self::Delegation,
Expand Down Expand Up @@ -443,6 +449,19 @@ impl Tag {
}
}

/// A short human-readable plaintext summary of what that event is about
///
/// JSON: `["alt", "<summary>"]`
///
/// <https://github.com/nostr-protocol/nips/blob/master/31.md>
#[inline]
#[uniffi::constructor]
pub fn alt(summary: &str) -> Self {
Self {
inner: tag::Tag::alt(summary),
}
}

/// Compose custom tag
///
/// JSON: `["<kind>", "<value-1>", "<value-2>", ...]`
Expand Down Expand Up @@ -680,6 +699,12 @@ pub enum TagStandard {
///
/// <https://github.com/nostr-protocol/nips/blob/master/70.md>
Protected,
/// A short human-readable plaintext summary of what that event is about
///
/// <https://github.com/nostr-protocol/nips/blob/master/31.md>
Alt {
summary: String,
},
}

impl From<tag::TagStandard> for TagStandard {
Expand Down Expand Up @@ -853,6 +878,7 @@ impl From<tag::TagStandard> for TagStandard {
tag::TagStandard::LabelNamespace(label) => Self::LabelNamespace { namespace: label },
tag::TagStandard::Label(labels) => Self::Label { label: labels },
tag::TagStandard::Protected => Self::Protected,
tag::TagStandard::Alt(summary) => Self::Alt { summary },
}
}
}
Expand Down Expand Up @@ -1000,6 +1026,7 @@ impl TryFrom<TagStandard> for tag::TagStandard {
TagStandard::LabelNamespace { namespace } => Ok(Self::LabelNamespace(namespace)),
TagStandard::Label { label } => Ok(Self::Label(label)),
TagStandard::Protected => Ok(Self::Protected),
TagStandard::Alt { summary } => Ok(Self::Alt(summary)),
}
}
}
12 changes: 12 additions & 0 deletions bindings/nostr-js/src/event/tag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,18 @@ impl JsTag {
}
}

/// A short human-readable plaintext summary of what that event is about
///
/// JSON: `["alt", "<summary>"]`
///
/// <https://github.com/nostr-protocol/nips/blob/master/31.md>
#[inline]
pub fn alt(summary: &str) -> Self {
Self {
inner: Tag::alt(summary),
}
}

/// Check if is a standard event tag with `root` marker
#[inline]
#[wasm_bindgen(js_name = isRoot)]
Expand Down
2 changes: 1 addition & 1 deletion crates/nostr/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ The following crate feature flags are available:
|| [28 - Public Chat](https://github.com/nostr-protocol/nips/blob/master/28.md) |
|| [29 - Relay-based Groups](https://github.com/nostr-protocol/nips/blob/master/29.md) |
|| [30 - Custom Emoji](https://github.com/nostr-protocol/nips/blob/master/30.md) |
| | [31 - Dealing with Unknown Events](https://github.com/nostr-protocol/nips/blob/master/31.md) |
| | [31 - Dealing with Unknown Events](https://github.com/nostr-protocol/nips/blob/master/31.md) |
|| [32 - Labeling](https://github.com/nostr-protocol/nips/blob/master/32.md) |
|| [34 - `git` stuff](https://github.com/nostr-protocol/nips/blob/master/34.md) |
|| [35 - Torrents](https://github.com/nostr-protocol/nips/blob/master/35.md) |
Expand Down
6 changes: 6 additions & 0 deletions crates/nostr/src/event/tag/kind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ pub enum TagKind<'a> {
///
/// <https://github.com/nostr-protocol/nips/blob/master/70.md>
Protected,
/// Human-readable plaintext summary of what that event is about
///
/// <https://github.com/nostr-protocol/nips/blob/master/31.md>
Alt,
/// Relay
Relay,
/// Nonce
Expand Down Expand Up @@ -108,6 +112,7 @@ impl<'a> fmt::Display for TagKind<'a> {
match self {
Self::SingleLetter(s) => write!(f, "{s}"),
Self::Protected => write!(f, "-"),
Self::Alt => write!(f, "alt"),
Self::Relay => write!(f, "relay"),
Self::Nonce => write!(f, "nonce"),
Self::Delegation => write!(f, "delegation"),
Expand Down Expand Up @@ -157,6 +162,7 @@ impl<'a> From<&'a str> for TagKind<'a> {
fn from(kind: &'a str) -> Self {
match kind {
"-" => Self::Protected,
"alt" => Self::Alt,
"relay" => Self::Relay,
"nonce" => Self::Nonce,
"delegation" => Self::Delegation,
Expand Down
20 changes: 20 additions & 0 deletions crates/nostr/src/event/tag/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,19 @@ impl Tag {
Self::from_standardized_without_cell(TagStandard::Protected)
}

/// A short human-readable plaintext summary of what that event is about
///
/// JSON: `["alt", "<summary>"]`
///
/// <https://github.com/nostr-protocol/nips/blob/master/31.md>
#[inline]
pub fn alt<T>(summary: T) -> Self
where
T: Into<String>,
{
Self::from_standardized_without_cell(TagStandard::Alt(summary.into()))
}

/// Compose custom tag
///
/// JSON: `["<kind>", "<value-1>", "<value-2>", ...]`
Expand Down Expand Up @@ -500,6 +513,8 @@ mod tests {
Tag::from_standardized_without_cell(TagStandard::Protected).to_vec()
);

assert_eq!(vec!["alt", "something"], Tag::alt("something").to_vec());

assert_eq!(
vec!["content-warning"],
Tag::from_standardized_without_cell(TagStandard::ContentWarning { reason: None })
Expand Down Expand Up @@ -877,6 +892,11 @@ mod tests {

assert_eq!(Tag::parse(&["-"]).unwrap(), Tag::protected());

assert_eq!(
Tag::parse(&["alt", "something"]).unwrap(),
Tag::alt("something")
);

assert_eq!(
Tag::parse(&["content-warning"]).unwrap(),
Tag::from_standardized_without_cell(TagStandard::ContentWarning { reason: None })
Expand Down
7 changes: 7 additions & 0 deletions crates/nostr/src/event/tag/standard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,10 @@ pub enum TagStandard {
///
/// <https://github.com/nostr-protocol/nips/blob/master/70.md>
Protected,
/// A short human-readable plaintext summary of what that event is about
///
/// <https://github.com/nostr-protocol/nips/blob/master/31.md>
Alt(String),
}

impl TagStandard {
Expand Down Expand Up @@ -339,6 +343,7 @@ impl TagStandard {
character: Alphabet::L,
uppercase: true,
}) => Ok(Self::LabelNamespace(tag_1.to_string())),
TagKind::Alt => Ok(Self::Alt(tag_1.to_string())),
TagKind::Dim => Ok(Self::Dim(ImageDimensions::from_str(tag_1)?)),
_ => Err(Error::UnknownStardardizedTag),
};
Expand Down Expand Up @@ -552,6 +557,7 @@ impl TagStandard {
uppercase: false,
}),
Self::Protected => TagKind::Protected,
Self::Alt(..) => TagKind::Alt,
}
}

Expand Down Expand Up @@ -786,6 +792,7 @@ impl From<TagStandard> for Vec<String> {
tag
}
TagStandard::Protected => vec![tag_kind],
TagStandard::Alt(summary) => vec![tag_kind, summary],
}
}
}
Expand Down

0 comments on commit fc16960

Please sign in to comment.