Skip to content

Commit

Permalink
account source
Browse files Browse the repository at this point in the history
  • Loading branch information
distractedm1nd committed Jul 27, 2024
1 parent 0d967b0 commit 33089b9
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 20 deletions.
32 changes: 20 additions & 12 deletions src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,24 @@ use std::fmt::Display;

#[derive(Clone, Serialize, Deserialize, Debug, PartialEq)]
pub enum Operation {
CreateAccount { id: String, value: String },
Add { id: String, value: String },
Revoke { id: String, value: String },
CreateAccount {
id: String,
value: String,
source: AccountSource,
},
Add {
id: String,
value: String,
},
Revoke {
id: String,
value: String,
},
}

#[derive(Clone, Serialize, Deserialize, Debug, PartialEq)]
pub enum AccountSource {
SignedBySequencer { signature: String },
}

impl Operation {
Expand All @@ -28,16 +43,9 @@ impl Operation {
}

impl Display for Operation {
// just print the debug
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Operation::CreateAccount { id, value } => {
write!(f, "CreateAccount {{ id: {}, value: {} }}", id, value)
}
Operation::Add { id, value } => write!(f, "Add {{ id: {}, value: {} }}", id, value),
Operation::Revoke { id, value } => {
write!(f, "Revoke {{ id: {}, value: {} }}", id, value)
}
}
write!(f, "{:?}", self)
}
}

Expand Down
35 changes: 28 additions & 7 deletions src/node_types/sequencer.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use async_trait::async_trait;
use ed25519::Signature;
use ed25519_dalek::{Signer, SigningKey};
use indexed_merkle_tree::{
node::Node,
sha256_mod,
tree::{IndexedMerkleTree, Proof},
Hash,
};
use std::{self, sync::Arc, time::Duration};
use std::{self, str::FromStr, sync::Arc, time::Duration};
use tokio::{
sync::{
mpsc::{channel, Receiver, Sender},
Expand All @@ -18,14 +19,13 @@ use tokio::{

use crate::{
cfg::Config,
common::{HashchainEntry, Operation},
common::{AccountSource, HashchainEntry, Operation},
consts::{CHANNEL_BUFFER_SIZE, DA_RETRY_COUNT, DA_RETRY_INTERVAL},
da::{DataAvailabilityLayer, FinalizedEpoch},
error::{DataAvailabilityError, DatabaseError, PrismResult},
error::{GeneralError, PrismError},
error::GeneralError,
error::{DataAvailabilityError, DatabaseError, PrismError, PrismResult},
node_types::NodeType,
storage::Database,
utils::verify_signature,
webserver::{OperationInput, WebServer},
zk_snark::BatchMerkleProofCircuit,
};
Expand Down Expand Up @@ -298,7 +298,22 @@ impl Sequencer {
.map(Proof::Update)
.map_err(|e| e.into())
}
Operation::CreateAccount { id, value } => {
Operation::CreateAccount { id, value, source } => {
// validation of account source
match source {
// TODO: use Signature, not String
AccountSource::SignedBySequencer { signature } => {
let sig = Signature::from_str(signature).map_err(|_| {
PrismError::General(GeneralError::ParsingError(
"sequencer's signature on operation".to_string(),
))
})?;
self.key
.verify(format!("{}{}", id, value).as_bytes(), &sig)
.map_err(|e| PrismError::General(GeneralError::InvalidSignature(e)))
}
}?;

let hashchain: PrismResult<Vec<HashchainEntry>> = self.db.get_hashchain(id);
if hashchain.is_ok() {
return Err(DatabaseError::NotFoundError(format!(
Expand Down Expand Up @@ -386,7 +401,13 @@ mod tests {

fn create_new_entry(id: String, value: String) -> OperationInput {
let key = create_signing_key();
let incoming = Operation::CreateAccount { id, value };
let incoming = Operation::CreateAccount {
id: id.clone(),
value: value.clone(),
source: AccountSource::SignedBySequencer {
signature: key.sign(format!("{}{}", id, value).as_bytes()).to_string(),
},
};
let content = serde_json::to_string(&incoming).unwrap();
let sig = key.sign(content.clone().as_bytes());

Expand Down
3 changes: 2 additions & 1 deletion src/webserver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ impl OperationInput {
match self.operation.clone() {
Operation::Add { id, value }
| Operation::Revoke { id, value }
| Operation::CreateAccount { id, value } => {
// we ignore account source as this must be done using internal sequencer data, so inside process_operation
| Operation::CreateAccount { id, value, .. } => {
// basic validation
if id.is_empty() {
return Err(
Expand Down

0 comments on commit 33089b9

Please sign in to comment.