Skip to content

Commit

Permalink
feat: implement signature for seq and signature verification for lc
Browse files Browse the repository at this point in the history
  • Loading branch information
sebasti810 authored and distractedm1nd committed Mar 18, 2024
1 parent 8cc2b7c commit c55a818
Show file tree
Hide file tree
Showing 8 changed files with 317 additions and 194 deletions.
118 changes: 118 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,4 @@ ahash = "0.8.7"
celestia-rpc = "0.1.0"
celestia-types = "0.1.0"
mockall = "0.12.1"
keystore = { git = "https://github.com/deltadevsde/keystore", rev = "3a4b448819efe535d035aff5366a48d46fbc4612" }
40 changes: 35 additions & 5 deletions src/da.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
use crate::error::{GeneralError, DataAvailabilityError};
use crate::error::{DataAvailabilityError, DatabaseError, DeimosError, GeneralError};
use crate::utils::Signable;
use crate::zk_snark::{Bls12Proof, VerifyingKey};
use celestia_types::blob::SubmitOptions;
use ed25519::Signature;
use fs2::FileExt;
use tokio::task::spawn;
use async_trait::async_trait;
use celestia_rpc::{Client, BlobClient, HeaderClient};
use celestia_types::{nmt::Namespace, Blob};
use serde::{Deserialize, Serialize};
use std::str::FromStr;
use std::{self, sync::Arc};
use tokio::sync::mpsc;
use serde_json::json;
Expand All @@ -15,14 +18,14 @@ use std::io::{Read, Write, Seek};
use serde_json::Value;


// TODO: Add signature from sequencer for lc to verify (#2)
#[derive(Serialize, Deserialize)]
#[derive(Serialize, Deserialize, Clone)]
pub struct EpochJson {
pub height: u64,
pub prev_commitment: String,
pub current_commitment: String,
pub proof: Bls12Proof,
pub verifying_key: VerifyingKey,
pub signature: Option<String>,
}

impl TryFrom<&Blob> for EpochJson {
Expand All @@ -48,6 +51,31 @@ impl TryFrom<&Blob> for EpochJson {

}

impl Signable for EpochJson {
fn get_signature(&self) -> Result<Signature, DeimosError> {
match &self.signature {
Some(signature) => {
let signature = Signature::from_str(signature).map_err(|_| DeimosError::General(GeneralError::ParsingError("Cannot parse signature".to_string())))?;
Ok(signature)
},
None => Err(DeimosError::General(GeneralError::MissingArgumentError))
}
}

fn get_content_to_sign(&self) -> Result<String, DeimosError> {
let mut copy = self.clone();
copy.signature = None;
serde_json::to_string(&copy).map_err(|_| DeimosError::General(GeneralError::ParsingError("Cannot serialize".to_string())))
}

fn get_public_key(&self) -> Result<String, DeimosError> {
// for epoch json the public key to verify is the one from the sequencer which should be already be public and known from every light client
// so if we use this function there should be an error
Err(DeimosError::Database(DatabaseError::NotFoundError("Public key not found".to_string())))
}
}


enum Message {
UpdateTarget(u64),
}
Expand Down Expand Up @@ -424,7 +452,8 @@ mod da_tests {
prev_commitment: prev_commitment,
current_commitment: tree.get_commitment().unwrap(),
proof: bls12proof,
verifying_key: vk
verifying_key: vk,
signature: None
}).await.unwrap();
tokio::time::sleep(tokio::time::Duration::from_secs(65)).await;

Expand All @@ -448,7 +477,8 @@ mod da_tests {
prev_commitment: prev_commitment,
current_commitment: tree.get_commitment().unwrap(),
proof: proof,
verifying_key: vk
verifying_key: vk,
signature: None
}).await.unwrap();
tokio::time::sleep(tokio::time::Duration::from_secs(65)).await;
});
Expand Down
4 changes: 4 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ pub enum GeneralError {
DecodingError(String),
#[error("Required argument missing")]
MissingArgumentError,
#[error("Invalid public key")]
InvalidPublicKey,
#[error("Invalid signature")]
InvalidSignature,
}


Expand Down
13 changes: 12 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ pub mod storage;
mod utils;
mod webserver;
pub mod zk_snark;
extern crate keystore;

use clap::{Parser, Subcommand};
use config::{builder::DefaultState, ConfigBuilder, File, FileFormat};
#[allow(unused_imports)]
use da::{LocalDataAvailabilityLayer, DataAvailabilityLayer, CelestiaConnection};
use serde::Deserialize;
use keystore::{KeyChain, KeyStore, KeyStoreType};

use dotenvy::dotenv;
use std::sync::Arc;
Expand Down Expand Up @@ -47,8 +49,13 @@ struct CommandLineArgs {
#[arg(short, long)]
port: Option<u16>,

/// Public key
#[arg(short, long)]
public_key: Option<String>,

#[command(subcommand)]
command: Commands,

}

#[derive(Debug, Default, Clone, Eq, PartialEq, Deserialize)]
Expand Down Expand Up @@ -76,6 +83,7 @@ pub struct Config {
#[serde(skip_serializing_if = "Option::is_none")]
celestia_config: Option<CelestiaConfig>,
epoch_time: u64,
public_key: Option<String>,
}

#[derive(Debug, Deserialize, Clone)]
Expand Down Expand Up @@ -116,6 +124,7 @@ impl Default for Config {
da_layer: DALayerOption::default(),
celestia_config: Some(CelestiaConfig::default()),
epoch_time: 60,
public_key: None,
}
}
}
Expand Down Expand Up @@ -165,6 +174,7 @@ fn load_config(args: CommandLineArgs) -> Result<Config, config::ConfigError> {
.epoch_time
.map(|e| e as u64)
.unwrap_or(default_config.epoch_time),
public_key: args.public_key.or(default_config.public_key),
})
}

Expand Down Expand Up @@ -217,12 +227,13 @@ async fn main() -> std::io::Result<()> {

let node: Arc<dyn NodeType> = match args.command {
// LightClients need a DA layer, so we can unwrap here
Commands::LightClient {} => Arc::new(LightClient::new(da.unwrap())),
Commands::LightClient {} => Arc::new(LightClient::new(da.unwrap(), config.public_key)),
Commands::Sequencer {} => Arc::new(Sequencer::new(
// TODO: convert error to std::io::Error...is there a better solution?
Arc::new(RedisConnections::new().map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e.to_string()))?),
da,
config,
KeyStoreType::KeyChain(KeyChain).get_signing_key().unwrap(),
)),
};

Expand Down
Loading

0 comments on commit c55a818

Please sign in to comment.