Skip to content

Commit

Permalink
nostr: add support to NIP30
Browse files Browse the repository at this point in the history
  • Loading branch information
yukibtc committed Oct 6, 2023
1 parent 6787760 commit 51276bf
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 1 deletion.
17 changes: 17 additions & 0 deletions bindings/nostr-ffi/src/event/tag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ pub enum TagKindKnown {
Payload,
Anon,
Proxy,
Emoji,
}

impl From<tag::TagKind> for TagKind {
Expand Down Expand Up @@ -260,6 +261,9 @@ impl From<tag::TagKind> for TagKind {
tag::TagKind::Proxy => Self::Known {
known: TagKindKnown::Proxy,
},
tag::TagKind::Emoji => Self::Known {
known: TagKindKnown::Emoji,
},
tag::TagKind::Custom(unknown) => Self::Unknown { unknown },
}
}
Expand Down Expand Up @@ -316,6 +320,7 @@ impl From<TagKind> for tag::TagKind {
TagKindKnown::Payload => Self::Payload,
TagKindKnown::Anon => Self::Anon,
TagKindKnown::Proxy => Self::Proxy,
TagKindKnown::Emoji => Self::Emoji,
},
TagKind::Unknown { unknown } => Self::Custom(unknown),
}
Expand Down Expand Up @@ -505,6 +510,10 @@ pub enum TagEnum {
id: String,
protocol: String,
},
Emoji {
shortcode: String,
url: String,
},
}

impl From<tag::Tag> for TagEnum {
Expand Down Expand Up @@ -663,6 +672,10 @@ impl From<tag::Tag> for TagEnum {
id,
protocol: protocol.to_string(),
},
tag::Tag::Emoji { shortcode, url } => Self::Emoji {
shortcode,
url: url.to_string(),
},
}
}
}
Expand Down Expand Up @@ -811,6 +824,10 @@ impl TryFrom<TagEnum> for tag::Tag {
id,
protocol: Protocol::from(protocol),
}),
TagEnum::Emoji { shortcode, url } => Ok(Self::Emoji {
shortcode,
url: UncheckedUrl::from(url),
}),
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions bindings/nostr-ffi/src/nostr.udl
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,7 @@ enum TagKindKnown {
"Payload",
"Anon",
"Proxy",
"Emoji",
};

[Enum]
Expand Down Expand Up @@ -492,6 +493,7 @@ interface TagEnum {
Payload(string hash);
Anon(string? msg);
Proxy(string id, string protocol);
Emoji(string shortcode, string url);
};

interface Tag {
Expand Down
2 changes: 2 additions & 0 deletions bindings/nostr-sdk-ffi/src/nostr_sdk.udl
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,7 @@ enum TagKindKnown {
"Payload",
"Anon",
"Proxy",
"Emoji",
};

[Enum]
Expand Down Expand Up @@ -512,6 +513,7 @@ interface TagEnum {
Payload(string hash);
Anon(string? msg);
Proxy(string id, string protocol);
Emoji(string shortcode, string url);
};

interface Tag {
Expand Down
2 changes: 1 addition & 1 deletion crates/nostr/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ The following crate feature flags are available:
|| [26 - Delegated Event Signing](https://github.com/nostr-protocol/nips/blob/master/26.md) |
|| [27 - Text Note References](https://github.com/nostr-protocol/nips/blob/master/27.md) |
|| [28 - Public Chat](https://github.com/nostr-protocol/nips/blob/master/28.md) |
| | [30 - Custom Emoji](https://github.com/nostr-protocol/nips/blob/master/30.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) |
|| [32 - Labeling](https://github.com/nostr-protocol/nips/blob/master/32.md) |
|| [33 - Parameterized Replaceable Events](https://github.com/nostr-protocol/nips/blob/master/33.md) |
Expand Down
18 changes: 18 additions & 0 deletions crates/nostr/src/event/tag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,8 @@ pub enum TagKind {
Anon,
/// Proxy
Proxy,
/// Emoji
Emoji,
/// Custom tag kind
Custom(String),
}
Expand Down Expand Up @@ -532,6 +534,7 @@ impl fmt::Display for TagKind {
Self::Payload => write!(f, "payload"),
Self::Anon => write!(f, "anon"),
Self::Proxy => write!(f, "proxy"),
Self::Emoji => write!(f, "emoji"),
Self::Custom(tag) => write!(f, "{tag}"),
}
}
Expand Down Expand Up @@ -591,6 +594,7 @@ where
"payload" => Self::Payload,
"anon" => Self::Anon,
"proxy" => Self::Proxy,
"emoji" => Self::Emoji,
tag => Self::Custom(tag.to_string()),
}
}
Expand Down Expand Up @@ -683,6 +687,12 @@ pub enum Tag {
id: String,
protocol: Protocol,
},
Emoji {
/// Name given for the emoji, which MUST be comprised of only alphanumeric characters and underscores
shortcode: String,
/// URL to the corresponding image file of the emoji
url: UncheckedUrl,
},
}

impl Tag {
Expand Down Expand Up @@ -755,6 +765,7 @@ impl Tag {
Self::Payload(..) => TagKind::Payload,
Self::Anon { .. } => TagKind::Anon,
Self::Proxy { .. } => TagKind::Proxy,
Self::Emoji { .. } => TagKind::Emoji,
}
}
}
Expand Down Expand Up @@ -926,6 +937,10 @@ where
id: tag[1].to_string(),
protocol: Protocol::from(&tag[2]),
}),
TagKind::Emoji => Ok(Self::Emoji {
shortcode: tag[1].to_string(),
url: UncheckedUrl::from(&tag[2]),
}),
_ => Ok(Self::Generic(tag_kind, tag[1..].to_vec())),
}
} else if tag_len == 4 {
Expand Down Expand Up @@ -1172,6 +1187,9 @@ impl From<Tag> for Vec<String> {
Tag::Proxy { id, protocol } => {
vec![TagKind::Proxy.to_string(), id, protocol.to_string()]
}
Tag::Emoji { shortcode, url } => {
vec![TagKind::Emoji.to_string(), shortcode, url.to_string()]
}
}
}
}
Expand Down

0 comments on commit 51276bf

Please sign in to comment.