Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
benthecarman committed Jan 5, 2024
1 parent 4bd3be9 commit 3bb5939
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions mutiny-core/src/nostr/dlc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ use crate::dlc::DlcHandler;
use crate::error::MutinyError;
use crate::logging::MutinyLogger;
use crate::storage::MutinyStorage;
use crate::utils::{now, oracle_announcement_from_hex};
use bitcoin::hashes::hex::ToHex;
use dlc::secp256k1_zkp::PublicKey;
use dlc_manager::Storage;
use dlc_messages::message_handler::read_dlc_message;
use dlc_messages::oracle_msgs::OracleAnnouncement;
use dlc_messages::{Message, WireMessage};
use lightning::ln::wire::Type;
use lightning::util::logger::Logger;
Expand All @@ -15,10 +17,13 @@ use nostr::key::{Keys, XOnlyPublicKey};
use nostr::prelude::{decrypt, encrypt, Parity};
use nostr::Url;
use nostr::{Event, EventBuilder, EventId, Filter, Kind, Tag};
use nostr_sdk::Client;
use std::io::Read;
use std::sync::Arc;

pub const DLC_WIRE_MESSAGE_KIND: Kind = Kind::Ephemeral(28_888);
pub const DLC_OFFER_MESSAGE_KIND: Kind = Kind::ParameterizedReplaceable(30_088);
pub const ORACLE_ANNOUNCEMENT_KIND: Kind = Kind::Custom(88);

/// A wrapper around a DLC message that indicates if it is an error
pub(crate) enum DlcMessageType {
Expand Down Expand Up @@ -56,6 +61,33 @@ impl<S: MutinyStorage> NostrDlcHandler<S> {
.pubkey(self.key.public_key())
}

/// Gets oracle announcements from the relay
pub async fn get_oracle_announcements(&self) -> Result<Vec<OracleAnnouncement>, MutinyError> {
let client = Client::new(&Keys::generate());
client.add_relay("wss://relay.damus.io").await?;
client.connect().await;

let filter = Filter::new().kind(ORACLE_ANNOUNCEMENT_KIND);
let events = client.get_events_of(vec![filter], None).await?;
client.disconnect().await?;

let ann = events
.into_iter()
.flat_map(|e| match oracle_announcement_from_hex(&e.content) {
Ok(ann) => {
if (ann.oracle_event.event_maturity_epoch as u64) < now().as_secs() {
Some(ann)
} else {
None
}
}
Err(_) => None,
})
.collect();

Ok(ann)
}

/// Turns an DLC message into a Nostr event
pub(crate) fn create_wire_msg_event(
&self,
Expand Down

0 comments on commit 3bb5939

Please sign in to comment.