Skip to content

Commit

Permalink
Merge branch 'feat/5/3/tx-waiter' into device-signer
Browse files Browse the repository at this point in the history
  • Loading branch information
broody committed Mar 15, 2024
2 parents 56b04cb + 82d90e7 commit 95c07d7
Show file tree
Hide file tree
Showing 65 changed files with 1,075 additions and 11,865 deletions.
4 changes: 2 additions & 2 deletions .tool-versions
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
scarb 2.4.3
starknet-foundry 0.14.0
scarb 2.5.4
starknet-foundry 0.18.0
python 3.9.18
dojo 0.4.4
5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[workspace]
resolver = "2"
members = ["crates/account_sdk"]
members = ["crates/account_sdk", "crates/webauthn/tests"]

[workspace.package]
edition = "2021"
Expand All @@ -26,11 +26,12 @@ sha2 = "0.10"
starknet = "0.8"
starknet-crypto = "0.6"
thiserror = "1"
tokio = { version = "1", features = ["macros", "time"] }
toml = "0.8"
u256-literal = "1"
url = "2"
wasm-bindgen = "0.2"
wasm-bindgen-futures = "0.4.42"
wasm-webauthn = "0.1.0"
webauthn-rs-proto = "0.4"
account-sdk = { path = "crates/account_sdk" }
tokio = { version = "1", features = ["macros", "time"] }
34 changes: 4 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ The project consists of several subfolders located in the ```crates``` directory

- **account_sdk** - a rust project to export and test functions for interacting with the custom account contract.
- **cartidge_account** - a cairo project with a custom account contract.
- **test_gen_scripts** - a python library to programatically generate cryptographic cairo tests.
- **webauthn** - cairo components to use in contracts.

The project has a global rust workspace.
Expand Down Expand Up @@ -45,39 +44,14 @@ cargo test

The scarb builds the contract and saves the compiled code in the `cartridge_account/target` folder. The tests then fetch (at compile time) the comipled code and deploy it to the local network. Note that obviously the contract needs to be recompiled for any changes to be applied in the compiled code.

## src
## webauthn

This is a cairo project with backend methods that will allow for various authentication methods in the custom contact.
Written based on [this specification](https://www.w3.org/TR/webauthn/).

### Running the tests

Some of the tests are auto-generated using python scripts.
See the `test_gen_scripts` for details.
The tests in `src/tests/` which end in `_gen_test.cairo` are auto-generated and might be changed by the python script. You can write your tests manually, but make sure the name of the test file doesn't end in `_gen_test.cairo`, and you place the import "`use ...;`" below the auto-generated imports in `src/tests.cairo`.

To run the tests:

```shell
scarb test
```

To again generate the tests:

```shell
python test_gen_scripts/main.py
```

## test_gen_scripts

This is a python library to programatically generate cryptographic cairo tests.

### Python Enviroment

Use pyenv to have specific version of python easily.
We use python3.9

```sh
pip install -r requirements.txt
python test_gen_scripts/main.py
You can run scarb test to run a few hand-written tests inside each ```auth``` and ```session``` crates. The bulk of the tests are located in the ```tests``` directory. These tests are written in rust and use property based testing provided by the [proptest](https://docs.rs/proptest/latest/proptest/) crate and use [cairo args runner](https://github.com/neotheprogramist/cairo-args-runner) to interface with and call cairo functions. To run these tests ```cd``` to the ```crates/webauthn/tests``` directory and run:
```bash
cargo test
```
12 changes: 6 additions & 6 deletions Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ edition = "2023_10"
version = "0.1.0"

[workspace.dependencies]
alexandria_data_structures = { git = "https://github.com/keep-starknet-strange/alexandria", rev = "085f17c87cf6d168032ef5840c39b8e18012284f" }
alexandria_encoding = { git = "https://github.com/keep-starknet-strange/alexandria", rev = "085f17c87cf6d168032ef5840c39b8e18012284f" }
alexandria_merkle_tree = { git = "https://github.com/keep-starknet-strange/alexandria", rev = "085f17c87cf6d168032ef5840c39b8e18012284f" }
openzeppelin = { git = "https://github.com/OpenZeppelin/cairo-contracts", tag = "v0.8.0" }
snforge_std = { git = "https://github.com/foundry-rs/starknet-foundry", tag = "v0.14.0" }
starknet = "2.4.3"
alexandria_data_structures = { git = "https://github.com/keep-starknet-strange/alexandria", tag="cairo-v2.5.4" }
alexandria_encoding = { git = "https://github.com/keep-starknet-strange/alexandria", tag="cairo-v2.5.4" }
alexandria_merkle_tree = { git = "https://github.com/keep-starknet-strange/alexandria", tag="cairo-v2.5.4" }
openzeppelin = { git = "https://github.com/OpenZeppelin/cairo-contracts", tag = "v0.9.0" }
snforge_std = { git = "https://github.com/foundry-rs/starknet-foundry", tag = "v0.18.0" }
starknet = "2.5.3"
webauthn_auth = { path = "crates/webauthn/auth" }
webauthn_session = { path = "crates/webauthn/session" }
2 changes: 1 addition & 1 deletion crates/account_sdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ version.workspace = true
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[lib]
crate-type = ["cdylib"]
crate-type = ["lib"]

[dependencies]
anyhow.workspace = true
Expand Down
4 changes: 2 additions & 2 deletions crates/account_sdk/src/abigen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ pub mod account {

abigen!(
CartridgeAccount,
"./target/dev/cartridge_account_Account.contract_class.json",
"/home/broody/development/cartridge/cairo-webauthn/target/dev/cartridge_account_Account.contract_class.json",
type_aliases {
webauthn_session::session_component::Event as SessionComponentEvent;
webauthn_auth::component::webauthn_component::Event as WebauthnComponentEvent;
Expand All @@ -16,7 +16,7 @@ pub mod erc20 {

abigen!(
Erc20Contract,
"./target/dev/cartridge_account_ERC20.contract_class.json",
"/home/broody/development/cartridge/cairo-webauthn/target/dev/cartridge_account_ERC20.contract_class.json",
type_aliases {
openzeppelin::token::erc20::erc20::ERC20Component::Event as ERC20ComponentEvent;
openzeppelin::access::ownable::ownable::OwnableComponent::Event as OwnableComponentEvent;
Expand Down
5 changes: 3 additions & 2 deletions crates/account_sdk/src/deploy_contract/pending.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use super::deployment::DeployResult;

pub struct PendingTransaction<'a, P, T>
where
&'a P: Provider + Send,
&'a P: Provider + Send + Sync,
{
transaction_result: T,
transaction_hash: FieldElement,
Expand All @@ -18,7 +18,7 @@ where

impl<'a, P, T> PendingTransaction<'a, P, T>
where
&'a P: Provider + Send,
&'a P: Provider + Send + Sync,
{
pub fn new(transaction_result: T, transaction_hash: FieldElement, client: &'a P) -> Self {
PendingTransaction {
Expand All @@ -29,6 +29,7 @@ where
}
pub async fn wait_for_completion(self) -> T {
TransactionWaiter::new(self.transaction_hash, &self.client)
.wait()
.await
.unwrap();
self.transaction_result
Expand Down
23 changes: 8 additions & 15 deletions crates/account_sdk/src/session_token/hash.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use starknet::{core::types::FieldElement, macros::felt};
use starknet_crypto::PoseidonHasher;
use starknet_crypto::{poseidon_hash, PoseidonHasher};

use crate::abigen::account::{Call, SessionSignature};

Expand All @@ -18,13 +18,6 @@ const POLICY_TYPE_HASH: FieldElement =

const STARKNET_MESSAGE_FELT: FieldElement = felt!("0x537461726b4e6574204d657373616765");

fn hash_two_elements(a: FieldElement, b: FieldElement) -> FieldElement {
let mut hasher = PoseidonHasher::new();
hasher.update(a);
hasher.update(b);
hasher.finalize()
}

pub fn compute_session_hash(
signature: SessionSignature,
chain_id: FieldElement,
Expand Down Expand Up @@ -89,9 +82,9 @@ pub fn compute_root(mut current_node: FieldElement, mut proof: Vec<FieldElement>
// We need to check if the current node is smaller than the current element of the proof.
// If it is, we need to swap the order of the hash.
current_node = if current_node < proof_element {
hash_two_elements(current_node, proof_element)
poseidon_hash(current_node, proof_element)
} else {
hash_two_elements(proof_element, current_node)
poseidon_hash(proof_element, current_node)
};
}
}
Expand Down Expand Up @@ -122,16 +115,16 @@ fn compute_proof(mut nodes: Vec<FieldElement>, index: usize, proof: &mut Vec<Fie
compute_proof(next_level, index_parent, proof)
}

fn get_next_level(nodes: &Vec<FieldElement>) -> Vec<FieldElement> {
fn get_next_level(nodes: &[FieldElement]) -> Vec<FieldElement> {
let mut next_level: Vec<FieldElement> = Vec::with_capacity(nodes.len() / 2);
for i in 0..nodes.len() / 2 {
let left = nodes[i * 2];
let right = nodes[i * 2 + 1];

let node = if left < right {
hash_two_elements(left, right)
poseidon_hash(left, right)
} else {
hash_two_elements(right, left)
poseidon_hash(right, left)
};
next_level.push(node);
}
Expand All @@ -142,11 +135,11 @@ fn get_next_level(nodes: &Vec<FieldElement>) -> Vec<FieldElement> {
#[test]
fn merkle_tree_poseidon_test() {
// [Setup] Merkle tree.
let root = felt!("0x7abc09d19c8a03abd4333a23f7823975c7bdd325170f0d32612b8baa1457d47");
let root = felt!("0x48924a3b2a7a7b7cc1c9371357e95e322899880a6534bdfe24e96a828b9d780");
let leaf = felt!("0x1");
let valid_proof = vec![
felt!("0x2"),
felt!("0x47ef3ad11ad3f8fc055281f1721acd537563ec134036bc4bd4de2af151f0832"),
felt!("0x338eb608d7e48306d01f5a8d4275dd85a52ba79aaf7a1a7b35808ba573c3669"),
];
let leaves = vec![felt!("0x1"), felt!("0x2"), felt!("0x3")];

Expand Down
8 changes: 3 additions & 5 deletions crates/account_sdk/src/session_token/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,9 @@ impl Session {
.map(|c| self.call_proof(c).ok_or(SessionError::CallNotInPolicy))
.collect::<Result<Vec<_>, SessionError>>()?;

let proofs_flat = proofs.into_iter().fold(Ok(vec![]), |acc, proof| {
acc.and_then(|mut acc| {
acc.extend(proof.1.clone());
Ok(acc)
})
let proofs_flat = proofs.into_iter().try_fold(vec![], |mut acc, proof| {
acc.extend(proof.1.clone());
Ok(acc)
})?;

assert!(!self.session_token().is_empty(), "Session token is empty");
Expand Down
7 changes: 1 addition & 6 deletions crates/account_sdk/src/tests/runners_test.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
use super::runners::{devnet_runner::DevnetRunner, katana_runner::KatanaRunner, TestnetRunner};
use super::runners::{katana_runner::KatanaRunner, TestnetRunner};

#[test]
fn test_katana_runner() {
KatanaRunner::load();
}

#[test]
fn test_devnet_runner() {
DevnetRunner::load();
}
1 change: 1 addition & 0 deletions crates/account_sdk/src/tests/webauthn/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ where
set_execution.send().await.unwrap();

TransactionWaiter::new(set_tx, self.runner.client())
.wait()
.await
.unwrap();
}
Expand Down
Loading

0 comments on commit 95c07d7

Please sign in to comment.