Skip to content

Commit

Permalink
Add controller contracts
Browse files Browse the repository at this point in the history
  • Loading branch information
tarrencev committed Jun 12, 2024
1 parent c747ec5 commit d314bee
Show file tree
Hide file tree
Showing 93 changed files with 27,931 additions and 26,446 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/quality.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
target: wasm32-unknown-unknown
- run: |
cd packages/account_sdk
cargo build -r --target wasm32-unknown-unknown -p account-sdk
cargo build -r --target wasm32-unknown-unknown -p account_sdk
clippy:
runs-on: ubuntu-latest
Expand Down
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
[workspace]
resolver = "2"
members = ["packages/account_sdk", "packages/account-wasm"]
members = ["packages/account_sdk", "packages/account_wasm"]

[workspace.package]
edition = "2021"
version = "0.1.0"

[workspace.dependencies]
account-sdk = { path = "packages/account_sdk" }
account_wasm = { path = "packages/account-wasm" }
account_sdk = { path = "packages/account_sdk" }
account_wasm = { path = "packages/account_wasm" }

anyhow = "1"
async-trait = "0.1"
Expand Down
41 changes: 41 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Starkli config.
config := --account katana-0 \
--rpc http://0.0.0.0:5050

# Build files helpers.
build := ./target/dev/controller_
sierra := .contract_class.json
compiled := .compiled_contract_class.json
store := ./packages/account_sdk/compiled/

# Contract params for deploy.
test_pubkey = 0x1234
katana_0 = 0x517ececd29116499f4a1b64b094da79ba08dfd54a3edaa316134c41f8160973

generate_artifacts:
scarb --manifest-path ./packages/contracts/controller/Scarb.toml build
mkdir -p ${store}

jq . ${build}CartridgeAccount${sierra} > ${store}controller${sierra}
jq . ${build}ERC20${sierra} > ${store}erc20${sierra}

cp ${build}CartridgeAccount${compiled} ${store}controller${compiled}
cp ${build}ERC20${compiled} ${store}erc20${compiled}

cainome --artifacts-path packages/account_sdk/compiled --parser-config packages/account_sdk/parser_config.json --output-dir packages/account_sdk/src/abigen --rust

deploy-katana:
@set -x; \
erc20_class=$$(starkli class-hash ${build}ERC20${sierra}); \
account_class=$$(starkli class-hash ${build}Account${sierra}); \
starkli declare ${build}Account${sierra} ${config}; \
starkli deploy "$${account_class}" ${test_pubkey} --salt 0x1234 ${config}; \
starkli declare ${build}ERC20${sierra} ${config}; \
starkli deploy "$${erc20_class}" str:token str:tkn u256:1 ${katana_0} --salt 0x1234 ${config};

test-session: generate_artifacts
rm -rf ./account_sdk/log
cargo test --manifest-path account_sdk/Cargo.toml session -- --nocapture

clean:
rm -rf ./target
61 changes: 50 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,19 @@ Controller is a gaming specific smart contract wallet that enables seamless play

It supports transaction signing using Passkeys and Session Tokens.

It consists of the [`keychain`](packages/keychain) which is a simple, sandboxed
application hosted at https://x.cartridge.gg/ and responsible for sensitive
operations, such as signing transactions. When an application requests to sign
or execute a transaction, keychain enforces client side authorization logic and
displays UI for user approval if necessary.

Interaction with the `keychain` is done throught the
[`controller`](packages/controller) sdk. Controller implements the account
interfaces required by [starknet.js](https://github.com/0xs34n/starknet.js).
Underneath, the implementation communicates with an embedded sandboxed keychain
iframe.
## Project structure
The project consists of several subfolders located in the ```packages``` directory:

- **[account_sdk](packages/controller)** - a rust sdk for interacting interacting with the controller account contract.
- **[account_wasm](packages/controller)** - a wasm version of the rust account sdk for usage in the browser.
- **[contracts](packages/controller)** - a cairo project containing the controller account contract, signer, and session implementations.
- **[keychain](packages/controller)** - a sandboxed application hosted at https://x.cartridge.gg/ and responsible for sensitive operations, such as signing transactions. When an application requests to sign or execute a transaction, keychain enforces client side authorization logic and displays UI for user approval if necessary.
- **[controller](packages/controller)** sdk. Controller implements the account interfaces required by [starknet.js](https://github.com/0xs34n/starknet.js). Underneath, the implementation communicates with an embedded sandboxed keychain iframe.

## Development

### Frontend

Install pnpm via corepack:

```sh
Expand Down Expand Up @@ -58,3 +57,43 @@ from the production keychain:
```js
window.cartridge.importAccount("EXPORTED ACCOUNT");
```

### Contracts

#### Compiling the cairo code

To build rust you first have to compile cairo. Run

```bash
make
```

in the root directory.

#### Commiting changes

The compiled account is stored in the git repository in the `crates/account_sdk/compiled/` folder. To make sure that the tests are run against the most reacent version of the code run `make` in the root repository. The `make` command should also be run before commiting any changes to ensure a valid state of the compiled code.

#### Running the tests

Note, that to run the tests you first have to [compile](#compiling-the-cairo-code) (to sierra and casm) the contract in the `controller` folder.

Starknet Foundry tests:

```bash
snforge test -p controller
```

Scarb tests:

```bash
scarb test -p webauthn_*
```

After the contract is compiled run the tests using `cargo`:

```bash
cargo test
```

The scarb builds the contract and saves the compiled code in the `controller/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.
21 changes: 21 additions & 0 deletions Scarb.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[workspace]
members = [
"packages/contracts/controller",
"packages/contracts/auth",
"packages/contracts/session",
]

[workspace.package]
edition = "2023_10"
version = "0.1.0"

[workspace.dependencies]
starknet = "2.6.3"
alexandria_math = { git = "https://github.com/keep-starknet-strange/alexandria.git", rev = "cairo-v2.6.0" }
alexandria_encoding = { git = "https://github.com/keep-starknet-strange/alexandria.git", rev = "cairo-v2.6.0" }
alexandria_merkle_tree = { git = "https://github.com/keep-starknet-strange/alexandria.git", tag = "cairo-v2.6.0" }
alexandria_data_structures = { git = "https://github.com/keep-starknet-strange/alexandria.git", tag = "cairo-v2.6.0" }
snforge_std = { git = "https://github.com/foundry-rs/starknet-foundry.git", tag = "v0.20.0" }
openzeppelin = { git = "https://github.com/OpenZeppelin/cairo-contracts.git", tag = "v0.10.0" }
controller_auth = { path = "packages/contracts/auth" }
controller_session = { path = "packages/contracts/session" }
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"keychain": "pnpm --filter @cartridge/keychain",
"controller": "pnpm --filter @cartridge/controller",
"connector": "pnpm --filter @cartridge/connector",
"account-wasm": "pnpm --filter @cartridge/account-wasm",
"account_wasm": "pnpm --filter @cartridge/account_wasm",
"book": "pnpm --filter @cartridge/docs",
"ui": "pnpm --filter @cartridge/ui",
"ui:next": "pnpm --filter @cartridge/ui-next",
Expand Down
Loading

0 comments on commit d314bee

Please sign in to comment.