-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
move mocks to separate modules and add unit tests
- Loading branch information
Showing
12 changed files
with
524 additions
and
178 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
use ink::env::hash::{CryptoHash, Sha2x256}; | ||
|
||
use super::{ops::Operation, traits::Hashable, USDT_TOKEN}; | ||
use crate::{contract::OpPub, types::Scalar}; | ||
|
||
#[derive(Default, Clone, Copy, scale::Encode, scale::Decode)] | ||
#[cfg_attr(feature = "std", derive(scale_info::TypeInfo))] | ||
pub struct Account { | ||
balance_aleph: Scalar, | ||
balance_usdt: Scalar, | ||
} | ||
|
||
impl Hashable for Account { | ||
fn hash(&self) -> Scalar { | ||
let mut res = [0x0; 32]; | ||
Sha2x256::hash( | ||
[self.balance_aleph.bytes, self.balance_usdt.bytes] | ||
.concat() | ||
.as_slice(), | ||
&mut res, | ||
); | ||
Scalar { bytes: res } | ||
} | ||
} | ||
|
||
impl Account { | ||
pub fn new() -> Self { | ||
Self { | ||
balance_aleph: 0_u128.into(), | ||
balance_usdt: 0_u128.into(), | ||
} | ||
} | ||
pub fn update(&self, operation: Operation) -> Self { | ||
match operation.op_pub { | ||
OpPub::Deposit { amount, token, .. } => { | ||
let mut balance_usdt = self.balance_usdt; | ||
if token.as_ref() == USDT_TOKEN { | ||
balance_usdt = (u128::from(balance_usdt) + amount).into(); | ||
} | ||
Self { | ||
balance_aleph: self.balance_aleph, | ||
balance_usdt, | ||
} | ||
} | ||
OpPub::Withdraw { amount, token, .. } => { | ||
let mut balance_usdt = self.balance_usdt; | ||
if token.as_ref() == USDT_TOKEN { | ||
balance_usdt = (u128::from(balance_usdt) - amount).into(); | ||
} | ||
Self { | ||
balance_aleph: self.balance_aleph, | ||
balance_usdt, | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
mod account; | ||
mod note; | ||
mod ops; | ||
pub mod relations; | ||
#[cfg(test)] | ||
mod tests; | ||
mod traits; | ||
|
||
const USDT_TOKEN: [u8; 32] = [0x2_u8; 32]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
use ink::env::hash::{CryptoHash, Sha2x256}; | ||
|
||
use super::traits::Hashable; | ||
use crate::types::Scalar; | ||
|
||
#[derive(Clone, Copy, scale::Encode, scale::Decode)] | ||
#[cfg_attr(feature = "std", derive(scale_info::TypeInfo))] | ||
pub struct Note { | ||
id: Scalar, | ||
trapdoor: Scalar, | ||
nullifier: Scalar, | ||
account_hash: Scalar, | ||
} | ||
|
||
impl Note { | ||
pub fn new(id: Scalar, trapdoor: Scalar, nullifier: Scalar, account_hash: Scalar) -> Self { | ||
Self { | ||
id, | ||
trapdoor, | ||
nullifier, | ||
account_hash, | ||
} | ||
} | ||
} | ||
|
||
impl Hashable for Note { | ||
fn hash(&self) -> Scalar { | ||
let mut res = [0x0; 32]; | ||
Sha2x256::hash( | ||
[ | ||
self.id.bytes, | ||
self.trapdoor.bytes, | ||
self.nullifier.bytes, | ||
self.account_hash.bytes, | ||
] | ||
.concat() | ||
.as_slice(), | ||
&mut res, | ||
); | ||
Scalar { bytes: res } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
use ink::primitives::AccountId; | ||
|
||
use crate::{contract::OpPub, errors::ShielderError}; | ||
|
||
/// empty private operation | ||
#[derive(Clone, Copy, scale::Encode, scale::Decode)] | ||
#[cfg_attr(feature = "std", derive(scale_info::TypeInfo))] | ||
pub struct OpPriv { | ||
pub user: AccountId, | ||
} | ||
|
||
#[derive(Clone, Copy, scale::Encode, scale::Decode)] | ||
#[cfg_attr(feature = "std", derive(scale_info::TypeInfo))] | ||
pub struct Operation { | ||
pub op_pub: OpPub, | ||
} | ||
|
||
impl Operation { | ||
pub fn combine(op_pub: OpPub, _op_priv: OpPriv) -> Result<Self, ShielderError> { | ||
match op_pub { | ||
OpPub::Deposit { user, .. } => { | ||
if user != _op_priv.user { | ||
return Err(ShielderError::ZkpVerificationFail); | ||
} | ||
} | ||
OpPub::Withdraw { user, .. } => { | ||
if user != _op_priv.user { | ||
return Err(ShielderError::ZkpVerificationFail); | ||
} | ||
} | ||
} | ||
Ok(Operation { op_pub }) | ||
} | ||
} |
Empty file.
Oops, something went wrong.