Skip to content

Commit

Permalink
Revert "Revert "Add controller contracts (#355)""
Browse files Browse the repository at this point in the history
This reverts commit 7ddf37e.
  • Loading branch information
tarrencev authored and broody committed Jun 14, 2024
1 parent b9c89d4 commit c2c4c39
Show file tree
Hide file tree
Showing 77 changed files with 29,538 additions and 24,575 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
Expand Up @@ -7,13 +7,13 @@ 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"
base64 = "0.21"
cainome = { git = "https://github.com/piniom/cainome", rev = "beb1f1f5" }
cainome = { git = "https://github.com/cartridge-gg/cainome", rev = "a84dfe0f" }
cairo-lang-starknet = "2.4.0"
coset = { version = "0.3.4", features = ["std"] }
ecdsa = "0.16.9"
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 packages/account-wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ version.workspace = true
crate-type = ["cdylib", "rlib"]

[dependencies]
account-sdk.workspace = true
account_sdk.workspace = true
base64 = "0.22.0"
coset = { version = "0.3.4", features = ["std"] }
hex = "0.4.3"
Expand Down
8 changes: 4 additions & 4 deletions packages/account-wasm/pkg/account_wasm_bg.js
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ function makeMutClosure(arg0, arg1, dtor, f) {
return real;
}
function __wbg_adapter_38(arg0, arg1, arg2) {
wasm._dyn_core__ops__function__FnMut__A____Output___R_as_wasm_bindgen__closure__WasmClosure___describe__invoke__h48ddaae0efb24961(arg0, arg1, addHeapObject(arg2));
wasm._dyn_core__ops__function__FnMut__A____Output___R_as_wasm_bindgen__closure__WasmClosure___describe__invoke__hf0f5cf13d081514f(arg0, arg1, addHeapObject(arg2));
}

let cachedUint32Memory0 = null;
Expand Down Expand Up @@ -270,7 +270,7 @@ function handleError(f, args) {
}
}
function __wbg_adapter_152(arg0, arg1, arg2, arg3) {
wasm.wasm_bindgen__convert__closures__invoke2_mut__h8496ed3272017b5c(arg0, arg1, addHeapObject(arg2), addHeapObject(arg3));
wasm.wasm_bindgen__convert__closures__invoke2_mut__hc994a4c6af3638a9(arg0, arg1, addHeapObject(arg2), addHeapObject(arg3));
}

const CartridgeAccountFinalization = (typeof FinalizationRegistry === 'undefined')
Expand Down Expand Up @@ -919,8 +919,8 @@ export function __wbindgen_memory() {
return addHeapObject(ret);
};

export function __wbindgen_closure_wrapper1117(arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 395, __wbg_adapter_38);
export function __wbindgen_closure_wrapper1115(arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 394, __wbg_adapter_38);
return addHeapObject(ret);
};

Binary file modified packages/account-wasm/pkg/account_wasm_bg.wasm
Binary file not shown.
4 changes: 2 additions & 2 deletions packages/account-wasm/pkg/account_wasm_bg.wasm.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ export function cartridgeaccount_signMessage(a: number, b: number): void;
export function __wbindgen_malloc(a: number, b: number): number;
export function __wbindgen_realloc(a: number, b: number, c: number, d: number): number;
export const __wbindgen_export_2: WebAssembly.Table;
export function _dyn_core__ops__function__FnMut__A____Output___R_as_wasm_bindgen__closure__WasmClosure___describe__invoke__h48ddaae0efb24961(a: number, b: number, c: number): void;
export function _dyn_core__ops__function__FnMut__A____Output___R_as_wasm_bindgen__closure__WasmClosure___describe__invoke__hf0f5cf13d081514f(a: number, b: number, c: number): void;
export function __wbindgen_add_to_stack_pointer(a: number): number;
export function __wbindgen_free(a: number, b: number, c: number): void;
export function __wbindgen_exn_store(a: number): void;
export function wasm_bindgen__convert__closures__invoke2_mut__h8496ed3272017b5c(a: number, b: number, c: number, d: number): void;
export function wasm_bindgen__convert__closures__invoke2_mut__hc994a4c6af3638a9(a: number, b: number, c: number, d: number): void;
Loading

0 comments on commit c2c4c39

Please sign in to comment.