26个版本
0.1.25 | 2024年5月22日 |
---|---|
0.1.20 | 2024年4月28日 |
0.1.16 | 2024年3月28日 |
0.1.10 | 2023年12月29日 |
0.1.7 | 2023年10月25日 |
#51 in WebSocket
1,912每月下载次数
64KB
1.5K SLoC
NostrO2
这个crate是我们第一次尝试构建与Nostr生态系统交互的简单Rust工具。
特性
该库通过3个基本类型(UserKeys、Notes和Relays)提供基于类的功能。
Notes
Nostr的主要数据结构,如NIP-01中定义。实现分为Notes和SignedNotes,以便与NIP-07等外部应用程序轻松互操作。这两种结构都具有完整的serde
序列化功能,并提供用于中继消息的即时发送输出。
UserKeys
可以从私钥str
创建,并允许您对Nostr Notes进行签名。
let new_user = UserKeys::new("<64-bit hex string>");
let mut unsigned_note = Note::new(
user_key_pair.get_public_key().to_string(),
1,
"Hello World"
);
unsigned_note.tag_note("t", "test");
let signed_note = user_key_pair.sign_nostr_event(unsigned_note); // -> SignedNote
// A note object can also be parsed by a NIP 07 client
NostrRelay
到中继的即时连接。WebSocket协议通过引用计数线程处理,以便您可以轻松处理多个中继。RelayEvents
提供轻松的模式匹配以进行中继/客户端通信和错误处理。
订阅
您可以将任何JSON过滤器传递给subscribe
函数,该函数位于NostrRelay
内,遵循NIP-01中的过滤器协议。
// Open a connection
let ws_connection = NostrRelay::new("relay.roadrunner.lat").await.expect("Failed to connect");
// Subscribe to a filter
ws_connection
.subscribe(json!({"kinds":[1],"limit":1}))
.await
.expect("Failed to subscribe to relay!");
// Send notes in an async manner
ws_connection.send_note(signed_note).await.expect("Unable to send note");
// Read the responses from the relay
loop {
if let Some(Ok(relay_msg)) = ws_connection.read_from_relay().await {
match relay_msg {
RelayEvents::EVENT(_event, _id, signed_note) => {
println!("Message received: {:?}", &signed_note);
// Extract the signed note info
let content = signed_note.get_content();
let specific_tags = signed_note.get_tags_by_id("a");
},
RelayEvents::OK(_event, id, success, _msg) => {
println!("Message received: {} {}", id, success);
},
RelayEvents::EOSE(_event, _sub) => println!("No more events"),
RelayEvents::NOTICE(_event, notice) => println!("Relay says: {}", notice),
}
}
}
Nostr身份验证
SignedNotes
对象还提供内容签名的验证方法。
assert_eq!(signed_note.verify(), true);
安装
运行cargo add nostra2
以获取最新版本。
您还可以将nostro2
添加到您的Cargo.toml
依赖项中
[dependencies]
nostro2 = "0.1.14"
依赖项
~9–22MB
~331K SLoC