Skip to content

Commit

Permalink
chore: revert b29222d (#1137)
Browse files Browse the repository at this point in the history
  • Loading branch information
xJonathanLEI committed Dec 17, 2024
1 parent 06f2148 commit 2b97d34
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 87 deletions.
6 changes: 5 additions & 1 deletion packages/keychain/src/utils/connection/execute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
} from "starknet";
import { ConnectionCtx, ControllerError, ExecuteCtx } from "./types";
import { ErrorCode, JsCall } from "@cartridge/account-wasm/controller";
import { mutex } from "./sync";

export const ESTIMATE_FEE_PERCENTAGE = 10;

Expand Down Expand Up @@ -64,6 +65,7 @@ export function execute({
});
}

const release = await mutex.obtain();
return await new Promise<InvokeFunctionResponse | ConnectError>(
async (resolve, reject) => {
if (!account) {
Expand Down Expand Up @@ -158,7 +160,9 @@ export function execute({
});
}
},
);
).finally(() => {
release();
});
};
}

Expand Down
6 changes: 5 additions & 1 deletion packages/keychain/src/utils/connection/sign.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
} from "@cartridge/controller";
import { Signature, TypedData } from "starknet";
import { ConnectionCtx, SignMessageCtx } from "./types";
import { mutex } from "./sync";
import Controller from "utils/controller";
import { parseControllerError } from "./execute";

Expand All @@ -29,6 +30,7 @@ export function signMessageFactory(setContext: (ctx: ConnectionCtx) => void) {
});
}

const release = await mutex.obtain();
return await new Promise<Signature | ConnectError>(
async (resolve, reject) => {
// If a session call and there is no session available
Expand Down Expand Up @@ -62,6 +64,8 @@ export function signMessageFactory(setContext: (ctx: ConnectionCtx) => void) {
});
}
},
);
).finally(() => {
release();
});
};
}
3 changes: 3 additions & 0 deletions packages/keychain/src/utils/connection/sync.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { Mutex } from "utils/mutex";

export const mutex = new Mutex();
118 changes: 33 additions & 85 deletions packages/keychain/src/utils/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,8 @@ import {
Call,
CallData,
} from "starknet";
import { Mutex } from "utils/mutex";
import { toWasmPolicies } from "@cartridge/controller";

const mutex = new Mutex();
import { toWasmPolicies } from "@cartridge/controller";

import {
CartridgeAccount,
Expand Down Expand Up @@ -103,12 +101,7 @@ export default class Controller extends Account {
throw new Error("Account not found");
}

const release = await mutex.obtain();
try {
await this.cartridge.createSession(toWasmPolicies(policies), expiresAt);
} finally {
release();
}
await this.cartridge.createSession(toWasmPolicies(policies), expiresAt);
}

registerSessionCalldata(
Expand All @@ -133,39 +126,24 @@ export default class Controller extends Account {
throw new Error("Account not found");
}

const release = await mutex.obtain();
try {
return await this.cartridge.registerSession(
toWasmPolicies(policies),
expiresAt,
publicKey,
num.toHex(maxFee),
);
} finally {
release();
}
return await this.cartridge.registerSession(
toWasmPolicies(policies),
expiresAt,
publicKey,
num.toHex(maxFee),
);
}

upgrade(new_class_hash: JsFelt): JsCall {
return this.cartridge.upgrade(new_class_hash);
}

async executeFromOutsideV2(calls: Call[]): Promise<InvokeFunctionResponse> {
const release = await mutex.obtain();
try {
return await this.cartridge.executeFromOutsideV2(toJsCalls(calls));
} finally {
release();
}
return await this.cartridge.executeFromOutsideV2(toJsCalls(calls));
}

async executeFromOutsideV3(calls: Call[]): Promise<InvokeFunctionResponse> {
const release = await mutex.obtain();
try {
return await this.cartridge.executeFromOutsideV3(toJsCalls(calls));
} finally {
release();
}
return await this.cartridge.executeFromOutsideV3(toJsCalls(calls));
}

async execute(
Expand All @@ -180,15 +158,10 @@ export default class Controller extends Account {
executionDetails.maxFee = num.toHex(executionDetails.maxFee);
}

const release = await mutex.obtain();
try {
return await this.cartridge.execute(
toJsCalls(calls),
executionDetails as JsInvocationsDetails,
);
} finally {
release();
}
return await this.cartridge.execute(
toJsCalls(calls),
executionDetails as JsInvocationsDetails,
);
}

hasSession(calls: Call[]): boolean {
Expand All @@ -210,27 +183,22 @@ export default class Controller extends Account {
calls: Call[],
_: EstimateFeeDetails = {},
): Promise<EstimateFee> {
const release = await mutex.obtain();
try {
const res = await this.cartridge.estimateInvokeFee(toJsCalls(calls));

// The reason why we set the multiplier unseemingly high is to account
// for the fact that the estimation above is done without validation (ie SKIP_VALIDATE).
//
// Setting it lower might cause the actual transaction to fail due to
// insufficient max fee.
const MULTIPLIER_PERCENTAGE = 170; // x1.7

// This will essentially multiply the estimated fee by 1.7
const suggestedMaxFee = num.addPercent(
BigInt(res.overall_fee),
MULTIPLIER_PERCENTAGE,
);
const res = await this.cartridge.estimateInvokeFee(toJsCalls(calls));

// The reason why we set the multiplier unseemingly high is to account
// for the fact that the estimation above is done without validation (ie SKIP_VALIDATE).
//
// Setting it lower might cause the actual transaction to fail due to
// insufficient max fee.
const MULTIPLIER_PERCENTAGE = 170; // x1.7

// This will essentially multiply the estimated fee by 1.7
const suggestedMaxFee = num.addPercent(
BigInt(res.overall_fee),
MULTIPLIER_PERCENTAGE,
);

return { suggestedMaxFee, ...res };
} finally {
release();
}
return { suggestedMaxFee, ...res };
}

async verifyMessageHash(
Expand All @@ -252,39 +220,19 @@ export default class Controller extends Account {
}

async signMessage(typedData: TypedData): Promise<Signature> {
const release = await mutex.obtain();
try {
return await this.cartridge.signMessage(JSON.stringify(typedData));
} finally {
release();
}
return this.cartridge.signMessage(JSON.stringify(typedData));
}

async getNonce(_?: any): Promise<string> {
const release = await mutex.obtain();
try {
return await this.cartridge.getNonce();
} finally {
release();
}
return await this.cartridge.getNonce();
}

async selfDeploy(maxFee: BigNumberish): Promise<DeployedAccountTransaction> {
const release = await mutex.obtain();
try {
return await this.cartridge.deploySelf(num.toHex(maxFee));
} finally {
release();
}
return await this.cartridge.deploySelf(num.toHex(maxFee));
}

async delegateAccount(): Promise<string> {
const release = await mutex.obtain();
try {
return await this.cartridge.delegateAccount();
} finally {
release();
}
return this.cartridge.delegateAccount();
}

revoke(_origin: string) {
Expand Down

0 comments on commit 2b97d34

Please sign in to comment.