accept NIP19

This commit is contained in:
kieran 2024-08-08 22:28:46 +01:00
parent 45a3004128
commit 70e0d70d8b
No known key found for this signature in database
GPG Key ID: DE71CEB3925BE941
2 changed files with 13 additions and 6 deletions

View File

@ -1,4 +1,5 @@
use nostr::{Event, EventId, PublicKey};
use nostr::{Event, FromBech32, PublicKey};
use nostr::prelude::Nip19;
use rocket::{Route, State};
use rocket::http::Status;
use rocket::serde::json::Json;
@ -32,11 +33,11 @@ fn get_event(
db: &State<SledDatabase>,
id: &str,
) -> Option<Json<Event>> {
let id = match EventId::parse(id) {
let id = match Nip19::from_bech32(id) {
Ok(i) => i,
_ => return None
};
match db.event_by_id(id) {
match db.event_by_id(&id) {
Ok(ev) => Some(Json::from(ev)),
_ => None
}

View File

@ -2,6 +2,7 @@ use std::fmt::Debug;
use std::sync::Arc;
use nostr::{Event, EventId, PublicKey};
use nostr::prelude::Nip19;
use nostr::util::hex;
use nostr_database::{FlatBufferBuilder, FlatBufferDecode, FlatBufferEncode};
use sled::{Db, IVec};
@ -85,8 +86,13 @@ impl SledDatabase {
new_val
}
pub fn event_by_id(&self, event_id: EventId) -> Result<Event, anyhow::Error> {
match self.db.get(event_id.as_bytes()) {
pub fn event_by_id(&self, event_id: &Nip19) -> Result<Event, anyhow::Error> {
let id_key = match event_id {
Nip19::EventId(e) => e.as_bytes(),
Nip19::Event(e) => e.event_id.as_bytes(),
_ => return Err(anyhow::Error::msg("Not supported ID format"))
};
match self.db.get(id_key) {
Ok(v) => match v {
Some(v) => match Event::decode(&v) {
Ok(v) => Ok(v),
@ -103,7 +109,7 @@ impl SledDatabase {
match self.db.get(rpk) {
Ok(v) => match v {
Some(v) => {
self.event_by_id(EventId::from_slice(v[8..].as_ref())?)
self.event_by_id(&Nip19::EventId(EventId::from_slice(v[8..].as_ref())?))
}
None => Err(anyhow::Error::msg("Not Found"))
}