Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(WIP): adding CreateAccount Operation #100

Merged
merged 8 commits into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 81 additions & 0 deletions src/common.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
use indexed_merkle_tree::{sha256_mod, Hash};
use serde::{Deserialize, Serialize};
use std::fmt::Display;

#[derive(Clone, Serialize, Deserialize, Debug, PartialEq)]
// An [`Operation`] represents a state transition in the system.
// In a blockchain analogy, this would be the full set of our transaction types.
pub enum Operation {
// Creates a new account with the given id and value.
CreateAccount {
id: String,
value: String,
source: AccountSource,
},
// Adds a value to an existing account.
Add {
id: String,
value: String,
},
// Revokes a value from an existing account.
Revoke {
id: String,
value: String,
},
}

#[derive(Clone, Serialize, Deserialize, Debug, PartialEq)]
// An [`AccountSource`] represents the source of an account. See adr-002 for more information.
pub enum AccountSource {
SignedBySequencer { signature: String },
}

impl Operation {
pub fn id(&self) -> String {
match self {
Operation::CreateAccount { id, .. } => id.clone(),
Operation::Add { id, .. } => id.clone(),
Operation::Revoke { id, .. } => id.clone(),
}
}

pub fn value(&self) -> String {
match self {
Operation::CreateAccount { value, .. } => value.clone(),
Operation::Add { value, .. } => value.clone(),
Operation::Revoke { value, .. } => value.clone(),
}
}
}

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

#[derive(Clone, Serialize, Deserialize, Debug, PartialEq)]
// A [`HashchainEntry`] represents a single entry in an account's hashchain.
// The value in the leaf of the corresponding account's node in the IMT is the hash of the last node in the hashchain.
pub struct HashchainEntry {
pub hash: Hash,
pub previous_hash: Hash,
pub operation: Operation,
}

impl HashchainEntry {
pub fn new(operation: Operation, previous_hash: Hash) -> Self {
let hash = {
let mut data = Vec::new();
data.extend_from_slice(operation.to_string().as_bytes());
data.extend_from_slice(previous_hash.as_ref());
sha256_mod(&data)
};
Self {
hash,
previous_hash,
operation,
}
}
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod cfg;
pub mod common;
pub mod consts;
pub mod da;
pub mod error;
Expand Down
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
mod cfg;
pub mod common;
pub mod consts;
pub mod da;
pub mod error;
Expand Down
Loading