Skip to content

Commit

Permalink
Merge #174: add NIP15 support
Browse files Browse the repository at this point in the history
  • Loading branch information
yukibtc committed Oct 11, 2023
2 parents fc4d4ea + 37f59e2 commit 2b273c9
Show file tree
Hide file tree
Showing 7 changed files with 450 additions and 9 deletions.
22 changes: 13 additions & 9 deletions crates/nostr/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,22 @@ default = ["std", "all-nips"]
std = [
"dep:once_cell",
"cbc?/std",
"base64?/std",
"bitcoin/std",
"bitcoin/rand-std",
"bip39?/std",
"chacha20?/std",
"base64?/std",
"bitcoin/std",
"bitcoin/rand-std",
"bip39?/std",
"chacha20?/std",
"negentropy/std",
"serde/std",
"serde_json/std",
"tracing/std",
"serde/std",
"serde_json/std",
"tracing/std",
"url-fork/std",
]
alloc = [
"cbc?/alloc",
"base64?/alloc",
"bitcoin/no-std",
"serde/alloc",
"serde/alloc",
"serde_json/alloc",
]
blocking = ["reqwest?/blocking"]
Expand Down Expand Up @@ -113,3 +113,7 @@ required-features = ["std"]
[[example]]
name = "vanity"
required-features = ["std"]

[[example]]
name = "nip15"
required-features = ["std"]
25 changes: 25 additions & 0 deletions crates/nostr/examples/nip15.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use nostr::prelude::*;
const ALICE_SK: &str = "6b911fd37cdf5c81d4c0adb1ab7fa822ed253ab0ad9aa18d77257c88b29b718e";
fn main() -> Result<()> {
let alice_keys = Keys::from_sk_str(ALICE_SK)?;
let shipping = ShippingMethod::new("123", 5.50).name("DHL");

let stall = StallData::new("123", "my test stall", "USD")
.description("this is a test stall")
.shipping(vec![shipping.clone()]);

let stall_event = EventBuilder::new_stall_data(stall).to_event(&alice_keys)?;
println!("{}", stall_event.as_json());

let product = ProductData::new("1", "123", "my test product", "USD")
.description("this is a test product")
.price(5.50)
.shipping(vec![shipping.get_shipping_cost()])
.images(vec!["https://example.com/image.png".into()])
.categories(vec!["test".into()]);

let product_event = EventBuilder::new_product_data(product).to_event(&alice_keys)?;
println!("{}", product_event.as_json());

Ok(())
}
17 changes: 17 additions & 0 deletions crates/nostr/src/event/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use super::{Event, EventId, UnsignedEvent};
use crate::key::{self, Keys};
#[cfg(feature = "nip04")]
use crate::nips::nip04;
use crate::nips::nip15::{ProductData, StallData};
#[cfg(all(feature = "std", feature = "nip46"))]
use crate::nips::nip46::Message as NostrConnectMessage;
use crate::nips::nip53::LiveEvent;
Expand Down Expand Up @@ -880,6 +881,22 @@ impl EventBuilder {
let tags: Vec<Tag> = data.into();
Self::new(Kind::HttpAuth, "", &tags)
}

/// Set stall data
///
/// <https://github.com/nostr-protocol/nips/blob/master/15.md>
pub fn new_stall_data(data: StallData) -> Self {
let tags: Vec<Tag> = data.clone().into();
Self::new(Kind::SetStall, data, &tags)
}

/// Set product data
///
/// <https://github.com/nostr-protocol/nips/blob/master/15.md>
pub fn new_product_data(data: ProductData) -> Self {
let tags: Vec<Tag> = data.clone().into();
Self::new(Kind::SetProduct, data, &tags)
}
}

#[cfg(test)]
Expand Down
10 changes: 10 additions & 0 deletions crates/nostr/src/event/kind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@ pub enum Kind {
FileMetadata,
/// HTTP Auth (NIP98)
HttpAuth,
/// Set stall (NIP15)
SetStall,
/// Set product (NIP15)
SetProduct,
/// Regular Events (must be between 1000 and <=9999)
Regular(u16),
/// Replaceable event (must be between 10000 and <20000)
Expand Down Expand Up @@ -193,6 +197,8 @@ impl From<u64> for Kind {
1311 => Self::LiveEventMessage,
30008 => Self::ProfileBadges,
30009 => Self::BadgeDefinition,
30017 => Self::SetStall,
30018 => Self::SetProduct,
30023 => Self::LongFormTextNote,
30078 => Self::ApplicationSpecificData,
1063 => Self::FileMetadata,
Expand Down Expand Up @@ -247,6 +253,8 @@ impl From<Kind> for u64 {
Kind::LiveEventMessage => 1311,
Kind::ProfileBadges => 30008,
Kind::BadgeDefinition => 30009,
Kind::SetStall => 30017,
Kind::SetProduct => 30018,
Kind::LongFormTextNote => 30023,
Kind::ApplicationSpecificData => 30078,
Kind::FileMetadata => 1063,
Expand Down Expand Up @@ -327,6 +335,8 @@ mod tests {
assert_eq!(Kind::Custom(20100), Kind::Custom(20100));
assert_eq!(Kind::Custom(20100), Kind::Ephemeral(20100));
assert_eq!(Kind::TextNote, Kind::Custom(1));
assert_eq!(Kind::ParameterizedReplaceable(30017), Kind::SetStall);
assert_eq!(Kind::ParameterizedReplaceable(30018), Kind::SetProduct);
}

#[test]
Expand Down
1 change: 1 addition & 0 deletions crates/nostr/src/nips/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub mod nip06;
#[cfg(all(feature = "std", feature = "nip11"))]
pub mod nip11;
pub mod nip13;
pub mod nip15;
pub mod nip19;
pub mod nip21;
pub mod nip26;
Expand Down
Loading

0 comments on commit 2b273c9

Please sign in to comment.