Skip to content

Commit

Permalink
fix(controller): register session should take in the webauthn signer (#…
Browse files Browse the repository at this point in the history
…636)

* fix: reg session signer

* expose owner guid

* feat: owner guid to session return params
  • Loading branch information
Larkooo authored Aug 29, 2024
1 parent 980fcd2 commit 8940a6f
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 70 deletions.
46 changes: 25 additions & 21 deletions packages/account-wasm/pkg/account_wasm.d.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,5 @@
/* tslint:disable */
/* eslint-disable */
export interface JsInvocationsDetails {
nonce: Felt;
maxFee: Felt;
}

export interface JsPolicy {
target: string;
method: string;
}

export interface JsOutsideExecution {
caller: Felt;
executeBefore: number;
Expand All @@ -18,14 +8,8 @@ export interface JsOutsideExecution {
nonce: Felt;
}

export interface JsSession {
policies: JsPolicy[];
expiresAt: number;
}

export interface JsCredentials {
authorization: Felt[];
privateKey: Felt;
export interface JsEstimateFeeDetails {
nonce: Felt;
}

export interface JsCall {
Expand All @@ -34,14 +18,30 @@ export interface JsCall {
calldata: Felt[];
}

export interface JsEstimateFeeDetails {
export interface JsInvocationsDetails {
nonce: Felt;
maxFee: Felt;
}

export interface JsPolicy {
target: string;
method: string;
}

export type Felts = JsFelt[];

export type JsFelt = Felt;

export interface JsSession {
policies: JsPolicy[];
expiresAt: number;
}

export interface JsCredentials {
authorization: Felt[];
privateKey: Felt;
}

/**
*/
export class CartridgeAccount {
Expand Down Expand Up @@ -71,6 +71,10 @@ export class CartridgeAccount {
*/
static new(app_id: string, rpc_url: string, chain_id: JsFelt, address: JsFelt, rp_id: string, username: string, credential_id: string, public_key: string): CartridgeAccount;
/**
* @returns {JsFelt}
*/
ownerGuid(): JsFelt;
/**
* @param {(JsPolicy)[]} policies
* @param {bigint} expires_at
* @param {JsFelt} public_key
Expand Down Expand Up @@ -146,12 +150,12 @@ export class CartridgeSessionAccount {
* @param {string} rpc_url
* @param {JsFelt} signer
* @param {JsFelt} address
* @param {JsFelt} owner_stark_public_key
* @param {JsFelt} owner_guid
* @param {JsFelt} chain_id
* @param {JsSession} session
* @returns {CartridgeSessionAccount}
*/
static new_as_registered(rpc_url: string, signer: JsFelt, address: JsFelt, owner_stark_public_key: JsFelt, chain_id: JsFelt, session: JsSession): CartridgeSessionAccount;
static new_as_registered(rpc_url: string, signer: JsFelt, address: JsFelt, owner_guid: JsFelt, chain_id: JsFelt, session: JsSession): CartridgeSessionAccount;
/**
* @param {JsFelt} hash
* @param {(JsCall)[]} calls
Expand Down
85 changes: 46 additions & 39 deletions packages/account-wasm/pkg/account_wasm_bg.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@ function addHeapObject(obj) {
return idx;
}

let WASM_VECTOR_LEN = 0;
const lTextDecoder = typeof TextDecoder === 'undefined' ? (0, module.require)('util').TextDecoder : TextDecoder;

let cachedTextDecoder = new lTextDecoder('utf-8', { ignoreBOM: true, fatal: true });

cachedTextDecoder.decode();

let cachedUint8Memory0 = null;

Expand All @@ -44,6 +48,13 @@ function getUint8Memory0() {
return cachedUint8Memory0;
}

function getStringFromWasm0(ptr, len) {
ptr = ptr >>> 0;
return cachedTextDecoder.decode(getUint8Memory0().subarray(ptr, ptr + len));
}

let WASM_VECTOR_LEN = 0;

const lTextEncoder = typeof TextEncoder === 'undefined' ? (0, module.require)('util').TextEncoder : TextEncoder;

let cachedTextEncoder = new lTextEncoder('utf-8');
Expand Down Expand Up @@ -100,10 +111,6 @@ function passStringToWasm0(arg, malloc, realloc) {
return ptr;
}

function isLikeNone(x) {
return x === undefined || x === null;
}

let cachedInt32Memory0 = null;

function getInt32Memory0() {
Expand All @@ -113,15 +120,8 @@ function getInt32Memory0() {
return cachedInt32Memory0;
}

const lTextDecoder = typeof TextDecoder === 'undefined' ? (0, module.require)('util').TextDecoder : TextDecoder;

let cachedTextDecoder = new lTextDecoder('utf-8', { ignoreBOM: true, fatal: true });

cachedTextDecoder.decode();

function getStringFromWasm0(ptr, len) {
ptr = ptr >>> 0;
return cachedTextDecoder.decode(getUint8Memory0().subarray(ptr, ptr + len));
function isLikeNone(x) {
return x === undefined || x === null;
}

let cachedFloat64Memory0 = null;
Expand Down Expand Up @@ -258,7 +258,7 @@ function handleError(f, args) {
wasm.__wbindgen_exn_store(addHeapObject(e));
}
}
function __wbg_adapter_163(arg0, arg1, arg2, arg3) {
function __wbg_adapter_164(arg0, arg1, arg2, arg3) {
wasm.wasm_bindgen__convert__closures__invoke2_mut__h09491c54862df783(arg0, arg1, addHeapObject(arg2), addHeapObject(arg3));
}

Expand Down Expand Up @@ -339,6 +339,13 @@ export class CartridgeAccount {
}
}
/**
* @returns {JsFelt}
*/
ownerGuid() {
const ret = wasm.cartridgeaccount_ownerGuid(this.__wbg_ptr);
return takeObject(ret);
}
/**
* @param {(JsPolicy)[]} policies
* @param {bigint} expires_at
* @param {JsFelt} public_key
Expand Down Expand Up @@ -533,17 +540,17 @@ export class CartridgeSessionAccount {
* @param {string} rpc_url
* @param {JsFelt} signer
* @param {JsFelt} address
* @param {JsFelt} owner_stark_public_key
* @param {JsFelt} owner_guid
* @param {JsFelt} chain_id
* @param {JsSession} session
* @returns {CartridgeSessionAccount}
*/
static new_as_registered(rpc_url, signer, address, owner_stark_public_key, chain_id, session) {
static new_as_registered(rpc_url, signer, address, owner_guid, chain_id, session) {
try {
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
const ptr0 = passStringToWasm0(rpc_url, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
const len0 = WASM_VECTOR_LEN;
wasm.cartridgesessionaccount_new_as_registered(retptr, ptr0, len0, addHeapObject(signer), addHeapObject(address), addHeapObject(owner_stark_public_key), addHeapObject(chain_id), addHeapObject(session));
wasm.cartridgesessionaccount_new_as_registered(retptr, ptr0, len0, addHeapObject(signer), addHeapObject(address), addHeapObject(owner_guid), addHeapObject(chain_id), addHeapObject(session));
var r0 = getInt32Memory0()[retptr / 4 + 0];
var r1 = getInt32Memory0()[retptr / 4 + 1];
var r2 = getInt32Memory0()[retptr / 4 + 2];
Expand Down Expand Up @@ -587,11 +594,16 @@ export function __wbindgen_object_clone_ref(arg0) {
return addHeapObject(ret);
};

export function __wbindgen_string_get(arg0, arg1) {
export function __wbindgen_json_parse(arg0, arg1) {
const ret = JSON.parse(getStringFromWasm0(arg0, arg1));
return addHeapObject(ret);
};

export function __wbindgen_json_serialize(arg0, arg1) {
const obj = getObject(arg1);
const ret = typeof(obj) === 'string' ? obj : undefined;
var ptr1 = isLikeNone(ret) ? 0 : passStringToWasm0(ret, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
var len1 = WASM_VECTOR_LEN;
const ret = JSON.stringify(obj === undefined ? null : obj);
const ptr1 = passStringToWasm0(ret, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
const len1 = WASM_VECTOR_LEN;
getInt32Memory0()[arg0 / 4 + 1] = len1;
getInt32Memory0()[arg0 / 4 + 0] = ptr1;
};
Expand Down Expand Up @@ -624,6 +636,15 @@ export function __wbindgen_number_get(arg0, arg1) {
getInt32Memory0()[arg0 / 4 + 0] = !isLikeNone(ret);
};

export function __wbindgen_string_get(arg0, arg1) {
const obj = getObject(arg1);
const ret = typeof(obj) === 'string' ? obj : undefined;
var ptr1 = isLikeNone(ret) ? 0 : passStringToWasm0(ret, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
var len1 = WASM_VECTOR_LEN;
getInt32Memory0()[arg0 / 4 + 1] = len1;
getInt32Memory0()[arg0 / 4 + 0] = ptr1;
};

export function __wbindgen_is_object(arg0) {
const val = getObject(arg0);
const ret = typeof(val) === 'object' && val !== null;
Expand All @@ -635,20 +656,6 @@ export function __wbindgen_bigint_from_u64(arg0) {
return addHeapObject(ret);
};

export function __wbindgen_json_parse(arg0, arg1) {
const ret = JSON.parse(getStringFromWasm0(arg0, arg1));
return addHeapObject(ret);
};

export function __wbindgen_json_serialize(arg0, arg1) {
const obj = getObject(arg1);
const ret = JSON.stringify(obj === undefined ? null : obj);
const ptr1 = passStringToWasm0(ret, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
const len1 = WASM_VECTOR_LEN;
getInt32Memory0()[arg0 / 4 + 1] = len1;
getInt32Memory0()[arg0 / 4 + 0] = ptr1;
};

export function __wbindgen_jsval_loose_eq(arg0, arg1) {
const ret = getObject(arg0) == getObject(arg1);
return ret;
Expand Down Expand Up @@ -990,7 +997,7 @@ export function __wbg_new_81740750da40724f(arg0, arg1) {
const a = state0.a;
state0.a = 0;
try {
return __wbg_adapter_163(a, state0.b, arg0, arg1);
return __wbg_adapter_164(a, state0.b, arg0, arg1);
} finally {
state0.a = a;
}
Expand Down Expand Up @@ -1094,8 +1101,8 @@ export function __wbindgen_memory() {
return addHeapObject(ret);
};

export function __wbindgen_closure_wrapper2348(arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 509, __wbg_adapter_42);
export function __wbindgen_closure_wrapper2350(arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 514, __wbg_adapter_42);
return addHeapObject(ret);
};

Binary file modified packages/account-wasm/pkg/account_wasm_bg.wasm
Binary file not shown.
1 change: 1 addition & 0 deletions packages/account-wasm/pkg/account_wasm_bg.wasm.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
export const memory: WebAssembly.Memory;
export function __wbg_cartridgeaccount_free(a: number): void;
export function cartridgeaccount_new(a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number, i: number, j: number, k: number, l: number, m: number, n: number, o: number): void;
export function cartridgeaccount_ownerGuid(a: number): number;
export function cartridgeaccount_registerSession(a: number, b: number, c: number, d: number, e: number): number;
export function cartridgeaccount_createSession(a: number, b: number, c: number, d: number): number;
export function cartridgeaccount_estimateInvokeFee(a: number, b: number, c: number, d: number, e: number): number;
Expand Down
18 changes: 9 additions & 9 deletions packages/account-wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,17 @@ mod utils;
use std::str::FromStr;
use std::sync::Arc;

use account_sdk::abigen::controller::{OutsideExecution, Signer, StarknetSigner};
use account_sdk::abigen::controller::OutsideExecution;
use account_sdk::account::outside_execution::OutsideExecutionCaller;
use account_sdk::account::session::hash::Session;
use account_sdk::account::session::SessionAccount;
use account_sdk::account::{AccountHashAndCallsSigner, MessageSignerAccount};
use account_sdk::controller::Controller;
use account_sdk::provider::CartridgeJsonRpcProvider;
use account_sdk::signers::webauthn::{CredentialID, WebauthnSigner};
use account_sdk::signers::{HashSigner, SignerTrait};
use account_sdk::signers::HashSigner;
use base64::engine::general_purpose;
use base64::Engine;
use cainome::cairo_serde::NonZero;
use coset::{CborSerializable, CoseKey};
use serde_wasm_bindgen::{from_value, to_value};
use signer::BrowserBackend;
Expand Down Expand Up @@ -107,6 +106,11 @@ impl CartridgeAccount {
Ok(CartridgeAccount { controller })
}

#[wasm_bindgen(js_name = ownerGuid)]
pub fn owner_guid(&self) -> JsFelt {
JsFelt(self.controller.owner_guid())
}

#[wasm_bindgen(js_name = registerSession)]
pub async fn register_session(
&mut self,
Expand Down Expand Up @@ -342,7 +346,7 @@ impl CartridgeSessionAccount {
rpc_url: String,
signer: JsFelt,
address: JsFelt,
owner_stark_public_key: JsFelt,
owner_guid: JsFelt,
chain_id: JsFelt,
session: JsSession,
) -> Result<CartridgeSessionAccount> {
Expand All @@ -353,10 +357,6 @@ impl CartridgeSessionAccount {
let guardian = SigningKey::from_secret_scalar(short_string!("CARTRIDGE_GUARDIAN"));
let address = address.0;
let chain_id = chain_id.0;
let owner_guid = Signer::Starknet(StarknetSigner {
pubkey: NonZero::new(owner_stark_public_key.0).ok_or(OperationError::ZeroFelt)?,
})
.guid();

let policies = session
.policies
Expand All @@ -372,7 +372,7 @@ impl CartridgeSessionAccount {
guardian,
address,
chain_id,
owner_guid,
owner_guid.0,
session,
)))
}
Expand Down
6 changes: 5 additions & 1 deletion packages/account_sdk/src/controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,10 @@ where
self.factory.deploy_v1(self.salt)
}

pub fn owner_guid(&self) -> Felt {
self.account.signer.signer().guid()
}

pub async fn create_session(
&mut self,
methods: Vec<AllowedMethod>,
Expand Down Expand Up @@ -168,7 +172,7 @@ where
let session = Session::new(methods, expires_at, &signer)?;
let register_execution = self
.contract
.register_session(&session.raw(), &signer.guid());
.register_session(&session.raw(), &self.owner_guid());

let txn = register_execution
// FIXME: est fee is not accurate as it does not account for validation cost, so set to some high multiple for now
Expand Down
3 changes: 3 additions & 0 deletions packages/keychain/src/pages/session.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { LoginMode } from "components/connect/types";
type SessionResponse = {
username: string;
address: string;
ownerGuid: string;
transactionHash?: string;
alreadyRegistered?: boolean;
};
Expand Down Expand Up @@ -104,6 +105,7 @@ export default function RegisterSession() {
onCallback({
username: controller.username,
address: controller.address,
ownerGuid: controller.account.cartridge.ownerGuid(),
transactionHash: transaction_hash,
});
},
Expand Down Expand Up @@ -132,6 +134,7 @@ export default function RegisterSession() {
onCallback({
username: controller.username,
address: controller.address,
ownerGuid: controller.account.cartridge.ownerGuid(),
alreadyRegistered: true,
});
}
Expand Down

0 comments on commit 8940a6f

Please sign in to comment.