-
Notifications
You must be signed in to change notification settings - Fork 14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Nwc sql #28
Merged
Merged
Nwc sql #28
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
-- Migration number: 0000 2023-08-10T16:43:44.275Z | ||
DROP TABLE IF EXISTS event; | ||
|
||
CREATE TABLE IF NOT EXISTS event ( | ||
id BLOB PRIMARY KEY, | ||
created_at INTEGER NOT NULL, | ||
pubkey BLOB NOT NULL, | ||
kind INTEGER NOT NULL, | ||
content TEXT NOT NULL, | ||
sig BLOB NOT NULL, | ||
deleted INTEGER NOT NULL DEFAULT 0 | ||
); | ||
|
||
CREATE INDEX IF NOT EXISTS pubkey_index ON event(pubkey); | ||
CREATE INDEX IF NOT EXISTS kind_index ON event(kind); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
-- Migration number: 0001 2023-08-11T20:22:11.351Z | ||
DROP TABLE IF EXISTS tag; | ||
|
||
CREATE TABLE IF NOT EXISTS tag ( | ||
id INTEGER PRIMARY KEY, | ||
event_id BLOB NOT NULL, | ||
name TEXT NOT NULL, | ||
value TEXT, | ||
FOREIGN KEY(event_id) REFERENCES event(id) ON DELETE CASCADE | ||
UNIQUE(event_id, name, value) | ||
); | ||
|
||
CREATE INDEX IF NOT EXISTS tag_event_index ON tag(event_id); | ||
CREATE INDEX IF NOT EXISTS tag_name_index ON tag(name); | ||
CREATE INDEX IF NOT EXISTS tag_value_index ON tag(value); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,262 @@ | ||
use std::collections::HashMap; | ||
|
||
use ::nostr::{Event, Kind, RelayMessage}; | ||
use nostr::{EventId, Tag, TagKind, Timestamp}; | ||
use serde::Deserialize; | ||
use serde_json::Value; | ||
use wasm_bindgen::prelude::JsValue; | ||
use worker::D1Database; | ||
use worker::*; | ||
|
||
#[derive(Debug, Deserialize)] | ||
struct EventRow { | ||
id: EventId, | ||
pubkey: nostr::secp256k1::XOnlyPublicKey, | ||
created_at: Timestamp, | ||
kind: Kind, | ||
tags: Option<Vec<Tag>>, | ||
content: String, | ||
sig: nostr::secp256k1::schnorr::Signature, | ||
} | ||
|
||
#[derive(Deserialize)] | ||
struct TagRow { | ||
event_id: EventId, | ||
name: String, | ||
value: String, | ||
} | ||
|
||
pub async fn get_nwc_events(keys: &[String], kind: Kind, db: &D1Database) -> Result<Vec<Event>> { | ||
// Determine the event kind | ||
match kind { | ||
Kind::WalletConnectResponse => (), | ||
Kind::WalletConnectRequest => (), | ||
_ => return Ok(vec![]), // skip other event types | ||
}; | ||
|
||
console_log!("querying for ({keys:?}) and {}", kind.as_u32()); | ||
|
||
// Query for the events first, without the tags | ||
let placeholders: String = keys.iter().map(|_| "?").collect::<Vec<_>>().join(", "); | ||
let query_str = format!( | ||
r#" | ||
SELECT * FROM event | ||
WHERE pubkey IN ({}) AND kind = ? AND deleted = 0 | ||
"#, | ||
placeholders | ||
); | ||
let mut stmt = db.prepare(&query_str); | ||
let mut bindings = Vec::with_capacity(keys.len() + 1); // +1 for the kind afterwards | ||
for key in keys.iter() { | ||
bindings.push(JsValue::from_str(key)); | ||
} | ||
bindings.push(JsValue::from_f64(kind.as_u32() as f64)); | ||
stmt = stmt.bind(&bindings)?; | ||
|
||
let result = stmt.all().await.map_err(|e| { | ||
console_log!("Failed to fetch nwc events: {}", e); | ||
format!("Failed to fetch nwc events: {}", e) | ||
})?; | ||
|
||
let mut events: Vec<Event> = result | ||
.results::<Value>()? | ||
.iter() | ||
.map(|row| { | ||
let e: EventRow = serde_json::from_value(row.clone()).map_err(|e| { | ||
console_log!("failed to parse event: {}", e); | ||
worker::Error::from(format!( | ||
"Failed to deserialize event from row ({}): {}", | ||
row, e | ||
)) | ||
})?; | ||
Ok(Event { | ||
id: e.id, | ||
pubkey: e.pubkey, | ||
created_at: e.created_at, | ||
kind: e.kind, | ||
tags: e.tags.unwrap_or_default(), | ||
content: e.content, | ||
sig: e.sig, | ||
}) | ||
}) | ||
.collect::<Result<Vec<Event>>>()?; | ||
|
||
// Now get all the tags for all the events found | ||
let event_ids: Vec<EventId> = events.iter().map(|e| e.id).collect(); | ||
let placeholders: String = event_ids.iter().map(|_| "?").collect::<Vec<_>>().join(", "); | ||
let tag_query_str = format!( | ||
r#" | ||
SELECT event_id, name, value FROM tag | ||
WHERE event_id IN ({}) | ||
ORDER BY id ASC | ||
"#, | ||
placeholders | ||
); | ||
let mut tag_stmt = db.prepare(&tag_query_str); | ||
let bindings: Vec<JsValue> = event_ids | ||
.iter() | ||
.map(|id| JsValue::from_str(&id.to_string())) | ||
.collect(); | ||
tag_stmt = tag_stmt.bind(&bindings)?; | ||
|
||
let tag_result = tag_stmt | ||
.all() | ||
.await | ||
.map_err(|e| format!("Failed to fetch tags: {}", e))?; | ||
|
||
let tags: Vec<TagRow> = tag_result.results::<TagRow>()?; | ||
let mut tags_map: HashMap<EventId, Vec<Tag>> = HashMap::new(); | ||
|
||
for tag_row in tags { | ||
if let Ok(tag) = Tag::parse(vec![tag_row.name, tag_row.value]) { | ||
tags_map | ||
.entry(tag_row.event_id) | ||
.or_insert_with(Vec::new) | ||
.push(tag); | ||
} | ||
} | ||
|
||
for event in &mut events { | ||
if let Some(tags) = tags_map.remove(&event.id) { | ||
event.tags.extend(tags); | ||
} | ||
} | ||
|
||
// Tag ordering could screw up signature, though it shouldn't matter | ||
// for NWC messages because those should only have one tag. | ||
// Also we insert tags in order and do an ORDER BY id so it should be fine. | ||
events.retain(|event| match event.verify() { | ||
Ok(_) => true, | ||
Err(e) => { | ||
console_log!("Verification failed for event with id {}: {}", event.id, e); | ||
false | ||
} | ||
}); | ||
|
||
Ok(events) | ||
} | ||
|
||
pub async fn handle_nwc_event(event: Event, db: &D1Database) -> Result<Option<RelayMessage>> { | ||
// Create the main event insertion query. | ||
let event_insert_query = worker::query!( | ||
db, | ||
r#" | ||
INSERT OR IGNORE INTO event (id, created_at, pubkey, kind, content, sig) | ||
VALUES (?, ?, ?, ?, ?, ?) | ||
"#, | ||
&event.id, | ||
&event.created_at, | ||
&event.pubkey, | ||
&event.kind, | ||
&event.content, | ||
&event.sig | ||
)?; | ||
|
||
// Create a vector of tag insertion queries. | ||
let mut tag_insert_queries: Vec<_> = event | ||
.tags | ||
.iter() | ||
.map(|tag| { | ||
worker::query!( | ||
db, | ||
r#" | ||
INSERT OR IGNORE INTO tag (event_id, name, value) | ||
VALUES (?, ?, ?) | ||
"#, | ||
&event.id, | ||
&tag.kind().to_string(), | ||
&tag.as_vec().get(1) | ||
) | ||
.expect("should compile query") | ||
}) | ||
.collect(); | ||
|
||
// Combine the main event and tag insertion queries. | ||
let mut batch_queries = vec![event_insert_query]; | ||
batch_queries.append(&mut tag_insert_queries); | ||
|
||
// Run the batch queries. | ||
let mut results = db.batch(batch_queries).await?.into_iter(); | ||
|
||
// Check the result of the main event insertion. | ||
if let Some(error_msg) = results.next().and_then(|res| res.error()) { | ||
console_log!("error saving nwc event to event table: {}", error_msg); | ||
let relay_msg = RelayMessage::new_ok(event.id, false, &error_msg); | ||
return Ok(Some(relay_msg)); | ||
} | ||
|
||
// Check the results for the tag insertions. | ||
for tag_insert_result in results { | ||
if let Some(error_msg) = tag_insert_result.error() { | ||
console_log!("error saving tag to tag table: {}", error_msg); | ||
let relay_msg = RelayMessage::new_ok(event.id, false, &error_msg); | ||
return Ok(Some(relay_msg)); | ||
} | ||
} | ||
|
||
let relay_msg = RelayMessage::new_ok(event.id, true, ""); | ||
Ok(Some(relay_msg)) | ||
} | ||
|
||
/// When a NWC request has been fulfilled, soft delete the request from the database | ||
pub async fn delete_nwc_request(event: Event, db: &D1Database) -> Result<()> { | ||
// Filter only relevant events | ||
match event.kind { | ||
Kind::WalletConnectResponse => (), | ||
_ => return Ok(()), // skip other event types | ||
}; | ||
|
||
let p_tag = event.tags.iter().find(|t| t.kind() == TagKind::P).cloned(); | ||
let e_tag = event.tags.iter().find(|t| t.kind() == TagKind::E).cloned(); | ||
|
||
if let Some(Tag::PubKey(pubkey, ..)) = p_tag { | ||
if let Some(Tag::Event(event_id, ..)) = e_tag { | ||
// Soft delete the event based on pubkey and event_id | ||
match worker::query!( | ||
db, | ||
"UPDATE event SET deleted = 1 WHERE pubkey = ? AND id = ?", | ||
&pubkey.to_string(), | ||
&event_id | ||
)? | ||
.run() | ||
.await | ||
{ | ||
Ok(_) => (), | ||
Err(e) => { | ||
console_log!("error soft deleting nwc event from database: {e}"); | ||
return Ok(()); | ||
} | ||
} | ||
} | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
/// When a NWC response has been fulfilled, soft delete the response from the database | ||
pub async fn delete_nwc_response(event: &Event, db: &D1Database) -> Result<()> { | ||
// Filter only relevant events | ||
match event.kind { | ||
Kind::WalletConnectResponse => (), | ||
_ => return Ok(()), // skip other event types | ||
}; | ||
|
||
// Soft delete the event based on pubkey and id | ||
match worker::query!( | ||
db, | ||
"UPDATE event SET deleted = 1 WHERE pubkey = ? AND id = ?", | ||
&event.pubkey.to_string(), | ||
&event.id | ||
)? | ||
.run() | ||
.await | ||
{ | ||
Ok(_) => console_log!("soft deleted nwc response event: {}", event.id), | ||
Err(e) => { | ||
console_log!("error soft deleting nwc event from database: {e}"); | ||
return Ok(()); | ||
} | ||
} | ||
|
||
Ok(()) | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
doing the tags like this might cause the signatures to be invalid, the tags need to be in the same order as before, should at least add a debug_assert to verify the signature is good.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably just need to sort by id to get in the correct order
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nostr is retarded if that's the case
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.