Skip to content

Commit

Permalink
Add delegate account (#436)
Browse files Browse the repository at this point in the history
* add register session

* skip session_authorization when registering session

* remove duplicate of assert_only_self

* add register session tests

* external owners

* add external owners test

* rebuild account

* add delegate account

---------

Co-authored-by: Szymon Wojtulewicz <[email protected]>
  • Loading branch information
notV4l and piniom authored Jul 2, 2024
1 parent 9467094 commit 1619a9b
Show file tree
Hide file tree
Showing 42 changed files with 19,027 additions and 16,155 deletions.
1 change: 0 additions & 1 deletion Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
members = [
"packages/contracts/controller",
"packages/contracts/auth",
"packages/contracts/session",
]

[workspace.package]
Expand Down
84 changes: 84 additions & 0 deletions examples/starknet-react-next/src/components/DelegateAccount.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import { useAccount } from "@starknet-react/core";
import { useCallback, useEffect, useState } from "react";
import { constants } from "starknet";
import CartridgeConnector from "@cartridge/connector";

export const DelegateAccount = () => {
const [chainId] = useState<constants.StarknetChainId>(
constants.StarknetChainId.SN_SEPOLIA,
);
const [submitted, setSubmitted] = useState<boolean>(false);
const { account, connector } = useAccount();

const [delegateAddress, setDelegateAddress] = useState("");
const [delegateAddressInput, setDelegateAddressInput] = useState("");
const [isDelegateSupported, setIsDelegateSupported] = useState(false);

const cartridgeConnector = connector as unknown as CartridgeConnector;

const load = useCallback(async () => {
if (!account) {
return;
}

try {
const delegate = await cartridgeConnector.delegateAccount();
setDelegateAddress(delegate?.toString() || "");
setIsDelegateSupported(true);
} catch (e: any) {
// controller doesnt support delegateAccount, ignore
}
}, [account, chainId]);

const execute = useCallback(async () => {
if (!account) {
return;
}
setSubmitted(true);

account
.execute([
{
contractAddress: account.address,
entrypoint: "set_delegate_account",
calldata: [delegateAddressInput],
},
])
.catch((e) => console.error(e))
.finally(() => setSubmitted(false));
}, [account, chainId, delegateAddressInput]);

useEffect(() => {
load();
}, [account, chainId, load]);

if (!account) {
return null;
}

return (
<>
<h2>Delegate account</h2>
{isDelegateSupported ? (
<>
<p>
Address: {delegateAddress}
<button onClick={() => load()}>Load</button>
</p>

<input
type="text"
min-width="420px"
value={delegateAddressInput}
onChange={(e) => setDelegateAddressInput(e.target.value)}
/>
<button onClick={() => execute()} disabled={submitted}>
Set Delegate
</button>
</>
) : (
<p>Not supported!</p>
)}
</>
);
};
2 changes: 2 additions & 0 deletions examples/starknet-react-next/src/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { ConnectWallet } from "components/ConnectWallet";
import { InvalidTxn } from "components/InvalidTxn";
import { SignMessage } from "components/SignMessage";
import { DojoSpawnAndMove } from "components/DojoSpawnAndMove";
import { DelegateAccount } from "components/DelegateAccount";

const Home: NextPage = () => {
return (
Expand All @@ -12,6 +13,7 @@ const Home: NextPage = () => {
<ConnectWallet />
<DojoSpawnAndMove />
<TransferEth />
<DelegateAccount />
<InvalidTxn />
<SignMessage />
</div>
Expand Down
8 changes: 7 additions & 1 deletion packages/account-wasm/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,10 @@

set -ex

pnpx wasm-pack build --out-dir ./pkg --release --features console-error-panic
pnpm dlx wasm-pack build --target bundler --out-dir ./pkg --release --features console-error-panic

# Workaround for ESM `import` error in NextJS.
# This removes `"type": "module"` field from `./pkg/packages.json`
mv pkg/package.json pkg/temp.json
jq -r 'del(.type)' pkg/temp.json > pkg/package.json
rm pkg/temp.json
4 changes: 4 additions & 0 deletions packages/account-wasm/pkg/account_wasm.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,8 @@ export class CartridgeAccount {
* @returns {Promise<any>}
*/
signMessage(typed_data: string): Promise<any>;
/**
* @returns {Promise<any>}
*/
delegateAccount(): Promise<any>;
}
39 changes: 23 additions & 16 deletions packages/account-wasm/pkg/account_wasm_bg.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ function makeMutClosure(arg0, arg1, dtor, f) {
return real;
}
function __wbg_adapter_48(arg0, arg1, arg2) {
wasm._dyn_core__ops__function__FnMut__A____Output___R_as_wasm_bindgen__closure__WasmClosure___describe__invoke__h684b586f178b72d6(arg0, arg1, addHeapObject(arg2));
wasm._dyn_core__ops__function__FnMut__A____Output___R_as_wasm_bindgen__closure__WasmClosure___describe__invoke__h9bd69eb5ef7eea47(arg0, arg1, addHeapObject(arg2));
}

let cachedUint32Memory0 = null;
Expand Down Expand Up @@ -267,8 +267,8 @@ function handleError(f, args) {
wasm.__wbindgen_exn_store(addHeapObject(e));
}
}
function __wbg_adapter_169(arg0, arg1, arg2, arg3) {
wasm.wasm_bindgen__convert__closures__invoke2_mut__h3dc4acd37607db0e(arg0, arg1, addHeapObject(arg2), addHeapObject(arg3));
function __wbg_adapter_170(arg0, arg1, arg2, arg3) {
wasm.wasm_bindgen__convert__closures__invoke2_mut__h641cd87b11e04d71(arg0, arg1, addHeapObject(arg2), addHeapObject(arg3));
}

const CartridgeAccountFinalization = (typeof FinalizationRegistry === 'undefined')
Expand Down Expand Up @@ -406,6 +406,13 @@ export class CartridgeAccount {
const ret = wasm.cartridgeaccount_signMessage(this.__wbg_ptr, ptr0, len0);
return takeObject(ret);
}
/**
* @returns {Promise<any>}
*/
delegateAccount() {
const ret = wasm.cartridgeaccount_delegateAccount(this.__wbg_ptr);
return takeObject(ret);
}
}

export function __wbindgen_object_drop_ref(arg0) {
Expand All @@ -421,6 +428,16 @@ export function __wbindgen_string_get(arg0, arg1) {
getInt32Memory0()[arg0 / 4 + 0] = ptr1;
};

export function __wbindgen_cb_drop(arg0) {
const obj = takeObject(arg0).original;
if (obj.cnt-- == 1) {
obj.a = 0;
return true;
}
const ret = false;
return ret;
};

export function __wbindgen_error_new(arg0, arg1) {
const ret = new Error(getStringFromWasm0(arg0, arg1));
return addHeapObject(ret);
Expand All @@ -441,16 +458,6 @@ export function __wbindgen_jsval_eq(arg0, arg1) {
return ret;
};

export function __wbindgen_cb_drop(arg0) {
const obj = takeObject(arg0).original;
if (obj.cnt-- == 1) {
obj.a = 0;
return true;
}
const ret = false;
return ret;
};

export function __wbindgen_boolean_get(arg0) {
const v = getObject(arg0);
const ret = typeof(v) === 'boolean' ? (v ? 1 : 0) : 2;
Expand Down Expand Up @@ -860,7 +867,7 @@ export function __wbg_new_81740750da40724f(arg0, arg1) {
const a = state0.a;
state0.a = 0;
try {
return __wbg_adapter_169(a, state0.b, arg0, arg1);
return __wbg_adapter_170(a, state0.b, arg0, arg1);
} finally {
state0.a = a;
}
Expand Down Expand Up @@ -971,8 +978,8 @@ export function __wbindgen_memory() {
return addHeapObject(ret);
};

export function __wbindgen_closure_wrapper1498(arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 465, __wbg_adapter_48);
export function __wbindgen_closure_wrapper1526(arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 473, __wbg_adapter_48);
return addHeapObject(ret);
};

Binary file modified packages/account-wasm/pkg/account_wasm_bg.wasm
Binary file not shown.
5 changes: 3 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 @@ -8,10 +8,11 @@ export function cartridgeaccount_execute(a: number, b: number, c: number, d: num
export function cartridgeaccount_executeFromOutside(a: number, b: number, c: number, d: number, e: number): number;
export function cartridgeaccount_revokeSession(a: number, b: number): void;
export function cartridgeaccount_signMessage(a: number, b: number, c: number): number;
export function cartridgeaccount_delegateAccount(a: number): number;
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__h684b586f178b72d6(a: number, b: number, c: number): void;
export function _dyn_core__ops__function__FnMut__A____Output___R_as_wasm_bindgen__closure__WasmClosure___describe__invoke__h9bd69eb5ef7eea47(a: number, b: number, c: number): void;
export function __wbindgen_add_to_stack_pointer(a: number): number;
export function __wbindgen_exn_store(a: number): void;
export function wasm_bindgen__convert__closures__invoke2_mut__h3dc4acd37607db0e(a: number, b: number, c: number, d: number): void;
export function wasm_bindgen__convert__closures__invoke2_mut__h641cd87b11e04d71(a: number, b: number, c: number, d: number): void;
4 changes: 2 additions & 2 deletions packages/account-wasm/pkg/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
"account_wasm_bg.js",
"account_wasm.d.ts"
],
"module": "account_wasm.js",
"main": "account_wasm.js",
"types": "account_wasm.d.ts",
"sideEffects": [
"./account_wasm.js",
"./snippets/*"
]
}
}
23 changes: 21 additions & 2 deletions packages/account-wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ use base64::{engine::general_purpose, Engine};
use coset::{CborSerializable, CoseKey};
use paymaster::PaymasterRequest;
use serde_wasm_bindgen::{from_value, to_value};
use starknet::accounts::Account;
use starknet::macros::short_string;
use starknet::accounts::{Account, ConnectedAccount};
use starknet::core::types::{BlockId, BlockTag, FunctionCall};
use starknet::macros::{selector, short_string};
use starknet::providers::Provider;
use starknet::signers::SigningKey;
use starknet::{
accounts::Call,
Expand Down Expand Up @@ -238,4 +240,21 @@ impl CartridgeAccount {
session,
))
}

#[wasm_bindgen(js_name = delegateAccount)]
pub async fn delegate_account(&self) -> Result<JsValue> {
let res = self
.account
.provider()
.call(
FunctionCall {
contract_address: self.account.address(),
entry_point_selector: selector!("delegate_account"),
calldata: vec![],
},
BlockId::Tag(BlockTag::Pending),
)
.await?;
Ok(to_value(&res[0])?)
}
}

Large diffs are not rendered by default.

Loading

0 comments on commit 1619a9b

Please sign in to comment.