split notedeck into crates

This splits notedeck into crates, separating the browser chrome and
individual apps:

* notedeck: binary file, browser chrome
* notedeck_columns: our columns app
* enostr: same as before

We still need to do more work to cleanly separate the chrome apis
from the app apis. Soon I will create notedeck-notebook to see what
makes sense to be shared between the apps.

Some obvious ones that come to mind:

1. ImageCache

We will likely want to move this to the notedeck crate, as most apps
will want some kind of image cache. In web browsers, web pages do not
need to worry about this, so we will likely have to do something similar

2. Ndb

Since NdbRef is threadsafe and Ndb is an Arc<NdbRef>, it can be safely
copied to each app. This will simplify things. In the future we might
want to create an abstraction over this? Maybe each app shouldn't have
access to the same database... we assume the data in DBs are all public
anyways, but if we have unwrapped giftwraps that could be a problem.

3. RelayPool / Subscription Manager

The browser should probably maintain these. Then apps can use ken's
high level subscription manager api and not have to worry about
connection pool details

4. Accounts

Accounts and key management should be handled by the chrome. Apps should
only have a simple signer interface.

That's all for now, just something to think about!

Signed-off-by: William Casarin <jb55@jb55.com>
This commit is contained in:
William Casarin
2024-12-11 02:53:05 -08:00
parent 10cbdf15f0
commit 74c5f0c748
156 changed files with 194 additions and 252 deletions

135
crates/enostr/src/note.rs Normal file
View File

@@ -0,0 +1,135 @@
use crate::{Error, Pubkey};
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use std::fmt;
use std::hash::{Hash, Hasher};
#[derive(Clone, Copy, Eq, PartialEq, Hash)]
pub struct NoteId([u8; 32]);
impl fmt::Debug for NoteId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.hex())
}
}
static HRP_NOTE: nostr::bech32::Hrp = nostr::bech32::Hrp::parse_unchecked("note");
impl NoteId {
pub fn new(bytes: [u8; 32]) -> Self {
NoteId(bytes)
}
pub fn bytes(&self) -> &[u8; 32] {
&self.0
}
pub fn hex(&self) -> String {
hex::encode(self.bytes())
}
pub fn from_hex(hex_str: &str) -> Result<Self, Error> {
let evid = NoteId(hex::decode(hex_str)?.as_slice().try_into().unwrap());
Ok(evid)
}
pub fn to_bech(&self) -> Option<String> {
nostr::bech32::encode::<nostr::bech32::Bech32>(HRP_NOTE, &self.0).ok()
}
}
/// Event is the struct used to represent a Nostr event
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Note {
/// 32-bytes sha256 of the the serialized event data
pub id: NoteId,
/// 32-bytes hex-encoded public key of the event creator
pub pubkey: Pubkey,
/// unix timestamp in seconds
pub created_at: u64,
/// integer
/// 0: NostrEvent
pub kind: u64,
/// Tags
pub tags: Vec<Vec<String>>,
/// arbitrary string
pub content: String,
/// 64-bytes signature of the sha256 hash of the serialized event data, which is the same as the "id" field
pub sig: String,
}
// Implement Hash trait
impl Hash for Note {
fn hash<H: Hasher>(&self, state: &mut H) {
self.id.0.hash(state);
}
}
impl PartialEq for Note {
fn eq(&self, other: &Self) -> bool {
self.id == other.id
}
}
impl Eq for Note {}
impl Note {
pub fn from_json(s: &str) -> Result<Self, Error> {
serde_json::from_str(s).map_err(Into::into)
}
pub fn verify(&self) -> Result<Self, Error> {
Err(Error::InvalidSignature)
}
/// This is just for serde sanity checking
#[allow(dead_code)]
pub(crate) fn new_dummy(
id: &str,
pubkey: &str,
created_at: u64,
kind: u64,
tags: Vec<Vec<String>>,
content: &str,
sig: &str,
) -> Result<Self, Error> {
Ok(Note {
id: NoteId::from_hex(id)?,
pubkey: Pubkey::from_hex(pubkey)?,
created_at,
kind,
tags,
content: content.to_string(),
sig: sig.to_string(),
})
}
}
impl std::str::FromStr for Note {
type Err = Error;
fn from_str(s: &str) -> Result<Self, Error> {
Note::from_json(s)
}
}
// Custom serialize function for Pubkey
impl Serialize for NoteId {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_str(&self.hex())
}
}
// Custom deserialize function for Pubkey
impl<'de> Deserialize<'de> for NoteId {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
let s = String::deserialize(deserializer)?;
NoteId::from_hex(&s).map_err(serde::de::Error::custom)
}
}