Skip to content

Commit ef3f5c0

Browse files
committed
Merge branch 'wasm-policy-check' into sepolia-self-fund
2 parents d9e1e73 + d6f745c commit ef3f5c0

File tree

8 files changed

+210
-71
lines changed

8 files changed

+210
-71
lines changed

examples/starknet-react-next/src/components/StarknetProvider.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Chain, mainnet, sepolia } from "@starknet-react/chains";
22
import { Connector, StarknetConfig, starkscan } from "@starknet-react/core";
33
import { PropsWithChildren } from "react";
44
import CartridgeConnector from "@cartridge/connector";
5-
import { RpcProvider } from "starknet";
5+
import { RpcProvider, shortString } from "starknet";
66

77
export function StarknetProvider({ children }: PropsWithChildren) {
88
return (

packages/account-wasm/pkg/account_wasm_bg.js

+9-9
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ function makeMutClosure(arg0, arg1, dtor, f) {
238238
return real;
239239
}
240240
function __wbg_adapter_48(arg0, arg1, arg2) {
241-
wasm._dyn_core__ops__function__FnMut__A____Output___R_as_wasm_bindgen__closure__WasmClosure___describe__invoke__h8b5bf7fd9e323748(arg0, arg1, addHeapObject(arg2));
241+
wasm._dyn_core__ops__function__FnMut__A____Output___R_as_wasm_bindgen__closure__WasmClosure___describe__invoke__hb89c618ba72713f5(arg0, arg1, addHeapObject(arg2));
242242
}
243243

244244
let cachedUint32Memory0 = null;
@@ -268,7 +268,7 @@ function handleError(f, args) {
268268
}
269269
}
270270
function __wbg_adapter_172(arg0, arg1, arg2, arg3) {
271-
wasm.wasm_bindgen__convert__closures__invoke2_mut__h76e0507ed36f9409(arg0, arg1, addHeapObject(arg2), addHeapObject(arg3));
271+
wasm.wasm_bindgen__convert__closures__invoke2_mut__h321310b3fdbfe81e(arg0, arg1, addHeapObject(arg2), addHeapObject(arg3));
272272
}
273273

274274
const CartridgeAccountFinalization = (typeof FinalizationRegistry === 'undefined')
@@ -448,6 +448,11 @@ export function __wbindgen_error_new(arg0, arg1) {
448448
return addHeapObject(ret);
449449
};
450450

451+
export function __wbindgen_object_clone_ref(arg0) {
452+
const ret = getObject(arg0);
453+
return addHeapObject(ret);
454+
};
455+
451456
export function __wbindgen_string_get(arg0, arg1) {
452457
const obj = getObject(arg1);
453458
const ret = typeof(obj) === 'string' ? obj : undefined;
@@ -540,11 +545,6 @@ export function __wbg_set_f975102236d3c502(arg0, arg1, arg2) {
540545
getObject(arg0)[takeObject(arg1)] = takeObject(arg2);
541546
};
542547

543-
export function __wbindgen_object_clone_ref(arg0) {
544-
const ret = getObject(arg0);
545-
return addHeapObject(ret);
546-
};
547-
548548
export function __wbg_String_b9412f8799faab3e(arg0, arg1) {
549549
const ret = String(getObject(arg1));
550550
const ptr1 = passStringToWasm0(ret, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
@@ -1002,8 +1002,8 @@ export function __wbindgen_memory() {
10021002
return addHeapObject(ret);
10031003
};
10041004

1005-
export function __wbindgen_closure_wrapper1507(arg0, arg1, arg2) {
1006-
const ret = makeMutClosure(arg0, arg1, 513, __wbg_adapter_48);
1005+
export function __wbindgen_closure_wrapper1519(arg0, arg1, arg2) {
1006+
const ret = makeMutClosure(arg0, arg1, 512, __wbg_adapter_48);
10071007
return addHeapObject(ret);
10081008
};
10091009

4.27 KB
Binary file not shown.

packages/account-wasm/pkg/account_wasm_bg.wasm.d.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export function cartridgeaccount_delegateAccount(a: number): number;
1414
export function __wbindgen_malloc(a: number, b: number): number;
1515
export function __wbindgen_realloc(a: number, b: number, c: number, d: number): number;
1616
export const __wbindgen_export_2: WebAssembly.Table;
17-
export function _dyn_core__ops__function__FnMut__A____Output___R_as_wasm_bindgen__closure__WasmClosure___describe__invoke__h8b5bf7fd9e323748(a: number, b: number, c: number): void;
17+
export function _dyn_core__ops__function__FnMut__A____Output___R_as_wasm_bindgen__closure__WasmClosure___describe__invoke__hb89c618ba72713f5(a: number, b: number, c: number): void;
1818
export function __wbindgen_add_to_stack_pointer(a: number): number;
1919
export function __wbindgen_exn_store(a: number): void;
20-
export function wasm_bindgen__convert__closures__invoke2_mut__h76e0507ed36f9409(a: number, b: number, c: number, d: number): void;
20+
export function wasm_bindgen__convert__closures__invoke2_mut__h321310b3fdbfe81e(a: number, b: number, c: number, d: number): void;

packages/account-wasm/src/lib.rs

+96-55
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ use types::invocation::JsInvocationsDetails;
4848
use types::outside_execution::JsOutsideExecution;
4949
use types::session::{JsCredentials, JsSession};
5050
use url::Url;
51+
use utils::{policies_match, set_panic_hook};
5152
use wasm_bindgen::prelude::*;
5253

5354
use crate::types::TryFromJsValue;
@@ -87,7 +88,8 @@ impl CartridgeAccount {
8788
credential_id: String,
8889
public_key: String,
8990
) -> Result<CartridgeAccount> {
90-
utils::set_panic_hook();
91+
set_panic_hook();
92+
9193
let rpc_url = Url::parse(&rpc_url)?;
9294
let provider = JsonRpcClient::new(HttpTransport::new(rpc_url.clone()));
9395

@@ -125,7 +127,7 @@ impl CartridgeAccount {
125127
policies: Vec<JsValue>,
126128
expires_at: u64,
127129
) -> Result<JsValue> {
128-
utils::set_panic_hook();
130+
set_panic_hook();
129131

130132
let methods = policies
131133
.into_iter()
@@ -159,26 +161,30 @@ impl CartridgeAccount {
159161
session_details: JsValue,
160162
fee_multiplier: Option<f64>,
161163
) -> Result<JsValue> {
162-
utils::set_panic_hook();
164+
set_panic_hook();
163165

164166
let calls = calls
165167
.into_iter()
166168
.map(Call::try_from_js_value)
167169
.collect::<std::result::Result<Vec<_>, _>>()?;
168170

169-
let result = if let Some(session_details) = from_value(session_details)? {
170-
self.session_account(session_details)
171-
.await?
172-
.execute_v1(calls)
173-
.fee_estimate_multiplier(fee_multiplier.unwrap_or(1.0))
174-
.estimate_fee()
175-
.await
176-
} else {
177-
self.account
178-
.execute_v1(calls)
179-
.fee_estimate_multiplier(fee_multiplier.unwrap_or(1.0))
180-
.estimate_fee()
181-
.await
171+
let multipler = fee_multiplier.unwrap_or(1.0);
172+
let session_details: Option<JsSession> = from_value(session_details)?;
173+
let result = match self.get_session_account(&calls, session_details).await? {
174+
Some(session_account) => {
175+
session_account
176+
.execute_v1(calls)
177+
.fee_estimate_multiplier(multipler)
178+
.estimate_fee()
179+
.await
180+
}
181+
_ => {
182+
self.account
183+
.execute_v1(calls)
184+
.fee_estimate_multiplier(multipler)
185+
.estimate_fee()
186+
.await
187+
}
182188
};
183189

184190
let fee_estimate =
@@ -194,29 +200,32 @@ impl CartridgeAccount {
194200
transaction_details: JsValue,
195201
session_details: JsValue,
196202
) -> Result<JsValue> {
197-
utils::set_panic_hook();
203+
set_panic_hook();
198204

199205
let calls = calls
200206
.into_iter()
201207
.map(Call::try_from_js_value)
202208
.collect::<std::result::Result<Vec<_>, _>>()?;
203209

204210
let details = JsInvocationsDetails::try_from(transaction_details)?;
205-
let result = if let Some(session_details) = from_value(session_details)? {
206-
self.session_account(session_details)
207-
.await?
208-
.execute_v1(calls)
209-
.max_fee(details.max_fee)
210-
.nonce(details.nonce)
211-
.send()
212-
.await
213-
} else {
214-
self.account
215-
.execute_v1(calls)
216-
.max_fee(details.max_fee)
217-
.nonce(details.nonce)
218-
.send()
219-
.await
211+
let session_details: Option<JsSession> = from_value(session_details)?;
212+
let result = match self.get_session_account(&calls, session_details).await? {
213+
Some(session_account) => {
214+
session_account
215+
.execute_v1(calls)
216+
.max_fee(details.max_fee)
217+
.nonce(details.nonce)
218+
.send()
219+
.await
220+
}
221+
_ => {
222+
self.account
223+
.execute_v1(calls)
224+
.max_fee(details.max_fee)
225+
.nonce(details.nonce)
226+
.send()
227+
.await
228+
}
220229
};
221230

222231
let execution = result.map_err(|e| OperationError::Execution(format!("{:#?}", e)))?;
@@ -231,28 +240,39 @@ impl CartridgeAccount {
231240
caller: JsValue,
232241
session_details: JsValue,
233242
) -> Result<JsValue> {
234-
utils::set_panic_hook();
243+
set_panic_hook();
244+
245+
let rs_calls: Vec<Call> = calls
246+
.clone()
247+
.into_iter()
248+
.map(Call::try_from_js_value)
249+
.collect::<std::result::Result<_, _>>()?;
250+
251+
let js_calls: Vec<JsCall> = calls
252+
.into_iter()
253+
.map(JsCall::try_from)
254+
.collect::<std::result::Result<_, _>>()?;
235255

236256
let outside = JsOutsideExecution {
237257
caller: serde_wasm_bindgen::from_value(caller)?,
238258
execute_after: 0_u64,
239259
execute_before: 3000000000_u64,
240-
calls: calls
241-
.into_iter()
242-
.map(JsCall::try_from)
243-
.collect::<std::result::Result<Vec<_>, _>>()?,
260+
calls: js_calls,
244261
nonce: SigningKey::from_random().secret_scalar(),
245262
};
246263

247-
let signed = if let Some(session_details) = from_value(session_details)? {
248-
self.session_account(session_details)
249-
.await?
250-
.sign_outside_execution(outside.clone().try_into()?)
251-
.await?
252-
} else {
253-
self.account
254-
.sign_outside_execution(outside.clone().try_into()?)
255-
.await?
264+
let session_details: Option<JsSession> = from_value(session_details)?;
265+
let signed = match self.get_session_account(&rs_calls, session_details).await? {
266+
Some(session_account) => {
267+
session_account
268+
.sign_outside_execution(outside.clone().try_into()?)
269+
.await?
270+
}
271+
_ => {
272+
self.account
273+
.sign_outside_execution(outside.clone().try_into()?)
274+
.await?
275+
}
256276
};
257277

258278
let response = PaymasterRequest::send(
@@ -274,6 +294,8 @@ impl CartridgeAccount {
274294

275295
#[wasm_bindgen(js_name = signMessage)]
276296
pub async fn sign_message(&self, typed_data: String) -> Result<JsValue> {
297+
set_panic_hook();
298+
277299
let signature = self
278300
.account
279301
.sign_message(serde_json::from_str(&typed_data)?)
@@ -285,6 +307,8 @@ impl CartridgeAccount {
285307

286308
#[wasm_bindgen(js_name = deploySelf)]
287309
pub async fn deploy_self(&self, max_fee: JsValue) -> Result<JsValue> {
310+
set_panic_hook();
311+
288312
let webauthn_calldata = self.device_signer.signer_pub_data();
289313
let mut constructor_calldata =
290314
Vec::<WebauthnSigner>::cairo_serialize(&vec![webauthn_calldata]);
@@ -314,6 +338,8 @@ impl CartridgeAccount {
314338

315339
#[wasm_bindgen(js_name = delegateAccount)]
316340
pub async fn delegate_account(&self) -> Result<JsValue> {
341+
set_panic_hook();
342+
317343
let res = self
318344
.account
319345
.provider()
@@ -331,30 +357,45 @@ impl CartridgeAccount {
331357
Ok(to_value(&res[0])?)
332358
}
333359

334-
async fn session_account(
360+
async fn get_session_account(
335361
&self,
336-
details: JsSession,
337-
) -> Result<SessionAccount<JsonRpcClient<HttpTransport>, SigningKey, SigningKey>> {
338-
let methods = details
362+
calls: &[Call],
363+
session_details: Option<JsSession>,
364+
) -> Result<Option<SessionAccount<JsonRpcClient<HttpTransport>, SigningKey, SigningKey>>> {
365+
set_panic_hook();
366+
367+
let Some(session_details) = session_details else {
368+
return Ok(None);
369+
};
370+
371+
if !policies_match(calls, &session_details.policies) {
372+
return Ok(None);
373+
}
374+
375+
let methods = session_details
339376
.policies
377+
.clone()
340378
.into_iter()
341379
.map(|policy| Ok(AllowedMethod::try_from(policy)?))
342380
.collect::<Result<Vec<AllowedMethod>>>()?;
343381

344382
let dummy_guardian = SigningKey::from_secret_scalar(short_string!("CARTRIDGE_GUARDIAN"));
345-
let session_signer = SigningKey::from_secret_scalar(details.credentials.private_key);
346-
let expires_at: u64 = details.expires_at.parse()?;
383+
let session_signer =
384+
SigningKey::from_secret_scalar(session_details.credentials.private_key);
385+
let expires_at: u64 = session_details.expires_at.parse()?;
347386
let session = Session::new(methods, expires_at, &session_signer.signer())
348387
.map_err(SessionError::Creation)?;
349388

350-
Ok(SessionAccount::new(
389+
let session_account = SessionAccount::new(
351390
JsonRpcClient::new(HttpTransport::new(self.rpc_url.clone())),
352391
session_signer,
353392
dummy_guardian,
354393
self.account.address(),
355394
self.account.chain_id(),
356-
details.credentials.authorization,
395+
session_details.credentials.authorization,
357396
session,
358-
))
397+
);
398+
399+
Ok(Some(session_account))
359400
}
360401
}

0 commit comments

Comments
 (0)