Skip to content

Commit

Permalink
adds pubkey for creating entries
Browse files Browse the repository at this point in the history
  • Loading branch information
tee8z committed Aug 27, 2024
1 parent 0c45195 commit 9aabf2c
Show file tree
Hide file tree
Showing 14 changed files with 61 additions and 89 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion oracle/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,12 @@ rustix = "0.38.19"
scooby = "0.5.0"
serde = { version = "1.0.188", features = ["derive"] }
serde_json = "1.0.117"
time = { version = "0.3.36", features = ["parsing", "formatting", "macros"] }
time = { version = "0.3.36", features = [
"parsing",
"formatting",
"macros",
"serde",
] }
thiserror = "1.0.62"
tokio = { version = "1.35.1", features = [
"macros",
Expand Down
4 changes: 4 additions & 0 deletions oracle/src/db/event_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,10 @@ impl EventData {
.collect();

info!("insert values: {:?}", insert_values);
if insert_values.is_empty() {
debug!("entry values were emtpy, skipping creating entry");
return Ok(());
}

let conn = self.new_write_connection_retry().await?;
let mut weather_stmt = conn.prepare(&query_str)?;
Expand Down
49 changes: 23 additions & 26 deletions oracle/src/db/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use anyhow::anyhow;
use dlctix::bitcoin::hashes::sha256;
use dlctix::bitcoin::{hashes::sha256, XOnlyPublicKey};
use dlctix::musig2::secp256k1::schnorr::Signature;
use dlctix::musig2::secp256k1::{self, Message, PublicKey};
use dlctix::musig2::secp256k1::{Message, PublicKey};
use dlctix::secp::{MaybeScalar, Scalar};
use dlctix::EventAnnouncement;
use duckdb::arrow::datatypes::ToByteSlice;
Expand All @@ -23,16 +23,14 @@ pub use event_data::*;
pub use event_db_migrations::*;
pub use weather_data::{Forecast, Observation, Station, WeatherData};

use crate::utc_datetime;

#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
pub struct CreateEvent {
/// Client needs to provide a valid Uuidv7
pub id: Uuid,
#[serde(with = "utc_datetime")]
#[serde(with = "time::serde::rfc3339")]
/// Time at which the attestation will be added to the event, needs to be after the observation date
pub signing_date: OffsetDateTime,
#[serde(with = "utc_datetime")]
#[serde(with = "time::serde::rfc3339")]
/// Date of when the weather observations occured (midnight UTC), all entries must be made before this time
pub observation_date: OffsetDateTime,
/// NOAA observation stations used in this event
Expand All @@ -51,10 +49,10 @@ pub struct CreateEvent {
pub struct CreateEventMessage {
/// Client needs to provide a valid Uuidv7
pub id: Uuid,
#[serde(with = "utc_datetime")]
#[serde(with = "time::serde::rfc3339")]
/// Time at which the attestation will be added to the event, needs to be after the observation date
pub signing_date: OffsetDateTime,
#[serde(with = "utc_datetime")]
#[serde(with = "time::serde::rfc3339")]
/// Date of when the weather observations occured (midnight UTC), all entries must be made before this time
pub observation_date: OffsetDateTime,
/// NOAA observation stations used in this event
Expand Down Expand Up @@ -101,10 +99,10 @@ pub struct CoordinatorInfo {
pub struct CreateEventData {
/// Provide UUIDv7 to use for looking up the event
pub id: Uuid,
#[serde(with = "utc_datetime")]
#[serde(with = "time::serde::rfc3339")]
/// Time at which the attestation will be added to the event
pub signing_date: OffsetDateTime,
#[serde(with = "utc_datetime")]
#[serde(with = "time::serde::rfc3339")]
/// Date of when the weather observations occured (midnight UTC), all entries must be made before this time
pub observation_date: OffsetDateTime,
// NOAA observation stations used in this event
Expand Down Expand Up @@ -208,15 +206,14 @@ impl CreateEventData {
}

/// Validates the received messages was created by the provided pubkey
pub fn validate(message: Message, pubkey: &str, signature: &str) -> Result<(), anyhow::Error> {
debug!("pubkey: {} signature: {}", pubkey, signature);
pub fn validate(message: Message, pubkey_str: &str, signature: &str) -> Result<(), anyhow::Error> {
info!("pubkey: {} signature: {}", pubkey_str, signature);
let raw_signature: Vec<u8> = hex::decode(signature).unwrap();
let raw_pubkey: Vec<u8> = hex::decode(pubkey).unwrap();
let sig: Signature = Signature::from_slice(raw_signature.as_slice())
.map_err(|e| anyhow!("invalid signature: {}", e))?;
let pubkey: secp256k1::XOnlyPublicKey =
secp256k1::XOnlyPublicKey::from_slice(raw_pubkey.as_slice())
.map_err(|e| anyhow!("invalid pubkey: {}", e))?;
let raw_pubkey: Vec<u8> = hex::decode(pubkey_str).unwrap();
let pubkey: XOnlyPublicKey = XOnlyPublicKey::from_slice(raw_pubkey.as_slice())
.map_err(|e| anyhow!("invalid pubkey: {}", e))?;
sig.verify(&message, &pubkey).map_err(|e| {
anyhow!(
"invalid signature {} for pubkey {} {}",
Expand Down Expand Up @@ -268,9 +265,9 @@ impl Default for EventFilter {
#[derive(Debug, Clone, Serialize, Deserialize, ToSchema, PartialEq, Eq)]
pub struct SignEvent {
pub id: Uuid,
#[serde(with = "utc_datetime")]
#[serde(with = "time::serde::rfc3339")]
pub signing_date: OffsetDateTime,
#[serde(with = "utc_datetime")]
#[serde(with = "time::serde::rfc3339")]
pub observation_date: OffsetDateTime,
pub status: EventStatus,
pub nonce: Scalar,
Expand Down Expand Up @@ -355,9 +352,9 @@ impl<'a> TryFrom<&Row<'a>> for SignEvent {
pub struct ActiveEvent {
pub id: Uuid,
pub locations: Vec<String>,
#[serde(with = "utc_datetime")]
#[serde(with = "time::serde::rfc3339")]
pub signing_date: OffsetDateTime,
#[serde(with = "utc_datetime")]
#[serde(with = "time::serde::rfc3339")]
pub observation_date: OffsetDateTime,
pub status: EventStatus,
pub total_allowed_entries: i64,
Expand Down Expand Up @@ -491,10 +488,10 @@ impl<'a> TryFrom<&Row<'a>> for ActiveEvent {
#[derive(Debug, Clone, Serialize, Deserialize, ToSchema, PartialEq, Eq)]
pub struct EventSummary {
pub id: Uuid,
#[serde(with = "utc_datetime")]
#[serde(with = "time::serde::rfc3339")]
/// Time at which the attestation will be added to the event
pub signing_date: OffsetDateTime,
#[serde(with = "utc_datetime")]
#[serde(with = "time::serde::rfc3339")]
/// Date of when the weather observations occured
pub observation_date: OffsetDateTime,
/// NOAA observation stations used in this event
Expand Down Expand Up @@ -626,10 +623,10 @@ impl<'a> TryFrom<&Row<'a>> for EventSummary {
#[derive(Debug, Clone, Serialize, Deserialize, ToSchema, PartialEq, Eq)]
pub struct Event {
pub id: Uuid,
#[serde(with = "utc_datetime")]
#[serde(with = "time::serde::rfc3339")]
/// Time at which the attestation will be added to the event
pub signing_date: OffsetDateTime,
#[serde(with = "utc_datetime")]
#[serde(with = "time::serde::rfc3339")]
/// Date of when the weather observations occured
pub observation_date: OffsetDateTime,
/// NOAA observation stations used in this event
Expand Down Expand Up @@ -918,7 +915,7 @@ impl TryInto<Weather> for &OrderedMap<String, Value> {

#[derive(Debug, Clone, Serialize, Deserialize, ToSchema, PartialEq, Eq)]
pub struct Observed {
#[serde(with = "utc_datetime")]
#[serde(with = "time::serde::rfc3339")]
pub date: OffsetDateTime,
pub temp_low: i64,
pub temp_high: i64,
Expand Down Expand Up @@ -1109,7 +1106,7 @@ impl ToRawSql for Observed {

#[derive(Debug, Clone, Serialize, Deserialize, ToSchema, PartialEq, Eq)]
pub struct Forecasted {
#[serde(with = "utc_datetime")]
#[serde(with = "time::serde::rfc3339")]
pub date: OffsetDateTime,
pub temp_low: i64,
pub temp_high: i64,
Expand Down
6 changes: 3 additions & 3 deletions oracle/src/file_access.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ use time::{
use tokio::fs;
use utoipa::IntoParams;

use crate::{create_folder, subfolder_exists, utc_option_datetime};
use crate::{create_folder, subfolder_exists};

#[derive(Clone, Deserialize, Serialize, IntoParams)]
pub struct FileParams {
#[serde(with = "utc_option_datetime")]
#[serde(with = "time::serde::rfc3339::option")]
pub start: Option<OffsetDateTime>,
#[serde(with = "utc_option_datetime")]
#[serde(with = "time::serde::rfc3339::option")]
pub end: Option<OffsetDateTime>,
pub observations: Option<bool>,
pub forecasts: Option<bool>,
Expand Down
2 changes: 0 additions & 2 deletions oracle/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@ mod db;
mod file_access;
pub mod oracle;
pub mod routes;
mod ser;
mod startup;
mod utils;

pub use app_error::AppError;
pub use db::*;
pub use file_access::{drop_suffix, Error, FileAccess, FileData, FileParams};
pub use routes::*;
pub use ser::*;
pub use startup::*;
pub use utils::*;
10 changes: 5 additions & 5 deletions oracle/src/routes/stations/weather_routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use serde::Serialize;
use time::OffsetDateTime;
use utoipa::IntoParams;

use crate::{utc_option_datetime, AppError, AppState, FileParams, Forecast, Observation, Station};
use crate::{AppError, AppState, FileParams, Forecast, Observation, Station};

#[utoipa::path(
get,
Expand All @@ -36,10 +36,10 @@ pub async fn forecasts(

#[derive(Clone, Serialize, Deserialize, IntoParams)]
pub struct ForecastRequest {
#[serde(with = "utc_option_datetime")]
#[serde(with = "time::serde::rfc3339::option")]
#[serde(default)]
pub start: Option<OffsetDateTime>,
#[serde(with = "utc_option_datetime")]
#[serde(with = "time::serde::rfc3339::option")]
#[serde(default)]
pub end: Option<OffsetDateTime>,
pub station_ids: String,
Expand Down Expand Up @@ -67,10 +67,10 @@ impl From<&ForecastRequest> for FileParams {

#[derive(Clone, Serialize, Deserialize, IntoParams)]
pub struct ObservationRequest {
#[serde(with = "utc_option_datetime")]
#[serde(with = "time::serde::rfc3339::option")]
#[serde(default)]
pub start: Option<OffsetDateTime>,
#[serde(with = "utc_option_datetime")]
#[serde(with = "time::serde::rfc3339::option")]
#[serde(default)]
pub end: Option<OffsetDateTime>,
pub station_ids: String,
Expand Down
2 changes: 0 additions & 2 deletions oracle/src/ser/mod.rs

This file was deleted.

19 changes: 0 additions & 19 deletions oracle/src/ser/utc_datetime.rs

This file was deleted.

31 changes: 0 additions & 31 deletions oracle/src/ser/utc_option_datetime.rs

This file was deleted.

2 changes: 2 additions & 0 deletions oracle/tests/api/create_event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ async fn can_create_oracle_event() {
total_allowed_entries: 100,
number_of_places_win: 3,
number_of_values_per_entry: 6,
coordinator: None,
};
let body_json = to_string(&new_event).unwrap();
let request = Request::builder()
Expand Down Expand Up @@ -89,6 +90,7 @@ async fn can_create_and_get_oracle_event() {
total_allowed_entries: 100,
number_of_places_win: 3,
number_of_values_per_entry: 6,
coordinator: None,
};
let body_json = to_string(&new_event).unwrap();
let request_post = Request::builder()
Expand Down
4 changes: 4 additions & 0 deletions oracle/tests/api/create_event_entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ async fn can_create_entry_into_event() {
total_allowed_entries: 100,
number_of_places_win: 3,
number_of_values_per_entry: 6,
coordinator: None,
};
let oracle_event = test_app.oracle.create_event(new_event).await.unwrap();
let new_entry = AddEventEntry {
Expand All @@ -54,6 +55,7 @@ async fn can_create_entry_into_event() {
wind_speed: None,
},
],
coordinator: None,
};
let body_json = to_string(&new_entry).unwrap();
let request = Request::builder()
Expand Down Expand Up @@ -94,6 +96,7 @@ async fn can_create_and_get_event_entry() {
total_allowed_entries: 100,
number_of_places_win: 3,
number_of_values_per_entry: 6,
coordinator: None,
};
let oracle_event = test_app.oracle.create_event(new_event).await.unwrap();
let new_entry = AddEventEntry {
Expand All @@ -119,6 +122,7 @@ async fn can_create_and_get_event_entry() {
wind_speed: None,
},
],
coordinator: None,
};
let body_json = to_string(&new_entry).unwrap();
let request = Request::builder()
Expand Down
Loading

0 comments on commit 9aabf2c

Please sign in to comment.