diff --git a/Cargo.lock b/Cargo.lock index 85e8d2da5..00692c5b1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1131,6 +1131,7 @@ version = "0.1.0" dependencies = [ "flatbuffers", "nostr", + "thiserror", ] [[package]] diff --git a/crates/nostr-sdk-fbs/Cargo.toml b/crates/nostr-sdk-fbs/Cargo.toml index 19fbfdaf3..494c0b42c 100644 --- a/crates/nostr-sdk-fbs/Cargo.toml +++ b/crates/nostr-sdk-fbs/Cargo.toml @@ -9,3 +9,4 @@ license.workspace = true [dependencies] flatbuffers = "23.5" nostr = { version = "0.24", path = "../nostr", default-features = false, features = ["std"] } +thiserror = { workspace = true } diff --git a/crates/nostr-sdk-fbs/src/lib.rs b/crates/nostr-sdk-fbs/src/lib.rs index ca18dc077..6d38b888d 100644 --- a/crates/nostr-sdk-fbs/src/lib.rs +++ b/crates/nostr-sdk-fbs/src/lib.rs @@ -5,32 +5,68 @@ use event_generated::event_fbs::{Fixed32Bytes, Fixed64Bytes}; pub use flatbuffers::FlatBufferBuilder; -use nostr::Event; +use flatbuffers::InvalidFlatbuffer; +use nostr::secp256k1::schnorr::Signature; +use nostr::secp256k1::{self, XOnlyPublicKey}; +use nostr::{Event, EventId, Kind, Timestamp}; +use thiserror::Error; #[allow(unused_imports, dead_code)] mod event_generated; pub use self::event_generated::event_fbs; -pub fn serialize_event<'a>(fbb: &'a mut FlatBufferBuilder, event: &Event) -> &'a [u8] { - fbb.reset(); +#[derive(Debug, Error)] +pub enum Error { + #[error(transparent)] + InvalidFlatbuffer(#[from] InvalidFlatbuffer), + #[error(transparent)] + EventId(#[from] nostr::event::id::Error), + #[error(transparent)] + Secp256k1(#[from] secp256k1::Error), + #[error("not found")] + NotFound, +} + +pub trait FlatBufferUtils: Sized { + fn encode<'a>(&self, fbb: &'a mut FlatBufferBuilder) -> &'a [u8]; + fn decode(buf: &[u8]) -> Result; +} + +impl FlatBufferUtils for Event { + fn encode<'a>(&self, fbb: &'a mut FlatBufferBuilder) -> &'a [u8] { + fbb.reset(); + + let id = Fixed32Bytes::new(&self.id.to_bytes()); + let pubkey = Fixed32Bytes::new(&self.pubkey.serialize()); + let sig = Fixed64Bytes::new(self.sig.as_ref()); + let args = event_fbs::EventArgs { + id: Some(&id), + pubkey: Some(&pubkey), + created_at: self.created_at.as_u64(), + kind: self.kind.as_u64(), + tags: None, // TODO + content: None, // TODO + sig: Some(&sig), + }; - let id = Fixed32Bytes::new(&event.id.to_bytes()); - let pubkey = Fixed32Bytes::new(&event.pubkey.serialize()); - let sig = Fixed64Bytes::new(event.sig.as_ref()); - let args = event_fbs::EventArgs { - id: Some(&id), - pubkey: Some(&pubkey), - created_at: event.created_at.as_u64(), - kind: event.kind.as_u64(), - tags: None, // TODO - content: None, // TODO - sig: Some(&sig), - }; + let offset = event_fbs::Event::create(fbb, &args); - let offset = event_fbs::Event::create(fbb, &args); + event_fbs::finish_event_buffer(fbb, offset); - event_fbs::finish_event_buffer(fbb, offset); + fbb.finished_data() + } - fbb.finished_data() + fn decode(buf: &[u8]) -> Result { + let ev = event_fbs::root_as_event(buf)?; + Ok(Self { + id: EventId::from_slice(&ev.id().ok_or(Error::NotFound)?.0)?, + pubkey: XOnlyPublicKey::from_slice(&ev.pubkey().ok_or(Error::NotFound)?.0)?, + created_at: Timestamp::from(ev.created_at()), + kind: Kind::from(ev.kind()), + tags: Vec::new(), // TODO + content: String::new(), // TODO + sig: Signature::from_slice(&ev.sig().ok_or(Error::NotFound)?.0)?, + }) + } } diff --git a/crates/nostr-sdk-redb/src/lib.rs b/crates/nostr-sdk-redb/src/lib.rs index 632444f26..6c63dc0b2 100644 --- a/crates/nostr-sdk-redb/src/lib.rs +++ b/crates/nostr-sdk-redb/src/lib.rs @@ -6,7 +6,7 @@ use std::path::Path; use nostr_sdk_db::nostr::{Event, EventId, Filter, Timestamp, Url}; use nostr_sdk_db::{async_trait, Backend, DatabaseError, DatabaseOptions, NostrDatabase}; -use nostr_sdk_fbs::FlatBufferBuilder; +use nostr_sdk_fbs::{FlatBufferBuilder, FlatBufferUtils}; use redb::{Database, TableDefinition}; use thiserror::Error; @@ -80,7 +80,7 @@ impl NostrDatabase for RedbDatabase { // Serialize event let mut fbb = FlatBufferBuilder::new(); - let ser = nostr_sdk_fbs::serialize_event(&mut fbb, event); + let ser = event.encode(&mut fbb); // Insert table