-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
241 additions
and
172 deletions.
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 |
---|---|---|
@@ -1,11 +1,22 @@ | ||
use nostr_rs::Kind; | ||
use nostr_rs::{Filter, Kind, PublicKey as NostrPublicKey, Timestamp}; | ||
|
||
/// Nostr [dlc_messages::oracle_msgs::OracleAnnouncement] marketplace. | ||
#[cfg(feature = "marketplace")] | ||
pub mod marketplace; | ||
/// Nostr related functions for DLC events. | ||
pub mod util; | ||
|
||
pub const DLC_MESSAGE_KIND: Kind = Kind::Custom(8_888); | ||
pub const ORACLE_ANNOUNCMENT_KIND: Kind = Kind::Custom(88); | ||
pub const ORACLE_ATTESTATION_KIND: Kind = Kind::Custom(89); | ||
|
||
pub fn create_dlc_message_filter(since: Timestamp, public_key: NostrPublicKey) -> Filter { | ||
Filter::new() | ||
.kind(DLC_MESSAGE_KIND) | ||
.since(since) | ||
.pubkey(public_key) | ||
} | ||
|
||
pub fn create_oracle_message_filter(since: Timestamp) -> Filter { | ||
Filter::new() | ||
.kinds([ORACLE_ANNOUNCMENT_KIND, ORACLE_ATTESTATION_KIND]) | ||
.since(since) | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,89 @@ | ||
use crate::nostr::{DLC_MESSAGE_KIND, ORACLE_ANNOUNCMENT_KIND, ORACLE_ATTESTATION_KIND}; | ||
use dlc_messages::message_handler::read_dlc_message; | ||
use dlc_messages::{Message, WireMessage}; | ||
use lightning::ln::wire::Type; | ||
use lightning::util::ser::{Readable, Writeable}; | ||
use nostr_rs::nips::nip04; | ||
use nostr_rs::{ | ||
Event, EventBuilder, EventId, Filter, Keys, Kind, PublicKey, SecretKey, Tag, Timestamp, | ||
}; | ||
|
||
pub fn create_dlc_message_filter(since: Timestamp, public_key: PublicKey) -> Filter { | ||
Filter::new() | ||
.kind(DLC_MESSAGE_KIND) | ||
.since(since) | ||
.pubkey(public_key) | ||
} | ||
|
||
pub fn create_oracle_message_filter(since: Timestamp) -> Filter { | ||
Filter::new() | ||
.kinds([ORACLE_ANNOUNCMENT_KIND, ORACLE_ATTESTATION_KIND]) | ||
.since(since) | ||
} | ||
|
||
pub fn parse_dlc_msg_event(event: &Event, secret_key: &SecretKey) -> anyhow::Result<Message> { | ||
let decrypt = nip04::decrypt(secret_key, &event.pubkey, &event.content)?; | ||
|
||
let bytes = base64::decode(decrypt)?; | ||
|
||
let mut cursor = lightning::io::Cursor::new(bytes); | ||
|
||
let msg_type: u16 = Readable::read(&mut cursor).unwrap(); | ||
|
||
let Some(wire) = read_dlc_message(msg_type, &mut cursor).unwrap() else { | ||
return Err(anyhow::anyhow!("Couldn't read DLC message.")); | ||
}; | ||
|
||
match wire { | ||
WireMessage::Message(msg) => Ok(msg), | ||
WireMessage::SegmentStart(_) | WireMessage::SegmentChunk(_) => { | ||
Err(anyhow::anyhow!("Blah blah, something with a wire")) | ||
} | ||
} | ||
} | ||
|
||
pub fn handle_dlc_msg_event( | ||
event: &Event, | ||
secret_key: &SecretKey, | ||
) -> anyhow::Result<(dlc::secp256k1_zkp::PublicKey, Message, Event)> { | ||
if event.kind != Kind::Custom(8_888) { | ||
return Err(anyhow::anyhow!("Event reveived was not DLC Message event.")); | ||
} | ||
|
||
let message = parse_dlc_msg_event(&event, secret_key)?; | ||
|
||
let pubkey = bitcoin::secp256k1::PublicKey::from_slice( | ||
&event | ||
.pubkey | ||
.public_key(nostr_sdk::secp256k1::Parity::Even) | ||
.serialize(), | ||
) | ||
.expect("converting pubkey between crates should not fail"); | ||
|
||
Ok((pubkey, message, event.clone())) | ||
} | ||
|
||
pub fn create_dlc_msg_event( | ||
to: PublicKey, | ||
event_id: Option<EventId>, | ||
msg: Message, | ||
keys: &Keys, | ||
) -> anyhow::Result<Event> { | ||
let mut bytes = msg.type_id().encode(); | ||
bytes.extend(msg.encode()); | ||
|
||
let content = nip04::encrypt(&keys.secret_key().clone(), &to, base64::encode(&bytes))?; | ||
|
||
let p_tags = Tag::public_key(keys.public_key); | ||
|
||
let e_tags = event_id.map(|e| Tag::event(e)); | ||
|
||
let tags = [Some(p_tags), e_tags] | ||
.into_iter() | ||
.flatten() | ||
.collect::<Vec<_>>(); | ||
|
||
let event = EventBuilder::new(DLC_MESSAGE_KIND, content, tags).to_event(&keys)?; | ||
|
||
Ok(event) | ||
} |
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 |
---|---|---|
@@ -1,5 +1,39 @@ | ||
pub mod dlc_handler; | ||
pub mod relay_handler; | ||
mod messages; | ||
mod relay_handler; | ||
|
||
pub use dlc_handler::NostrDlcHandler; | ||
pub use nostr_relay_pool::RelayPoolNotification; | ||
pub use relay_handler::NostrDlc; | ||
|
||
use crate::{DlcDevKitDlcManager, Oracle, Storage, Transport}; | ||
use bitcoin::secp256k1::PublicKey as BitcoinPublicKey; | ||
use dlc_messages::Message; | ||
use nostr_rs::PublicKey; | ||
use std::sync::Arc; | ||
|
||
#[async_trait::async_trait] | ||
impl Transport for NostrDlc { | ||
fn name(&self) -> String { | ||
"nostr".to_string() | ||
} | ||
|
||
async fn listen(&self) { | ||
self.listen().await.expect("Did not start nostr listener."); | ||
} | ||
|
||
/// Get messages that have not been processed yet. | ||
async fn receive_messages<S: Storage, O: Oracle>( | ||
&self, | ||
manager: Arc<DlcDevKitDlcManager<S, O>>, | ||
) { | ||
self.receive_dlc_messages(manager).await | ||
} | ||
/// Send a message to a specific counterparty. | ||
fn send_message(&self, counterparty: BitcoinPublicKey, message: Message) { | ||
let public_key = PublicKey::from_slice(&counterparty.serialize()) | ||
.expect("Should not fail converting nostr key to bitcoin key."); | ||
let _event = messages::create_dlc_msg_event(public_key, None, message, &self.keys); | ||
} | ||
/// Connect to another peer | ||
async fn connect_outbound(&self, _pubkey: BitcoinPublicKey, _host: &str) { | ||
todo!("Connect outbound") | ||
} | ||
} |
Oops, something went wrong.