Skip to content

Commit

Permalink
Fix normalizing execute calls for wasm (#414)
Browse files Browse the repository at this point in the history
  • Loading branch information
broody authored Jun 24, 2024
1 parent db46b30 commit e386fa5
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 26 deletions.
2 changes: 1 addition & 1 deletion packages/keychain/src/components/Execute/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ export function Execute() {

const onSubmit = useCallback(async () => {
setLoading(true);
const response = await account.execute(calls, null, {
const response = await account.execute(calls, {
maxFee: fees.max,
});
ctx.resolve({
Expand Down
10 changes: 6 additions & 4 deletions packages/keychain/src/utils/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
InvocationsDetails,
shortString,
num,
AllowArray,
} from "starknet";

import {
Expand All @@ -26,6 +27,7 @@ import { selectors, VERSION } from "./selectors";
import Storage from "./storage";
import { CartridgeAccount } from "@cartridge/account-wasm";
import { Session } from "@cartridge/controller";
import { normalizeCalls } from "./connection/execute";

const EST_FEE_MULTIPLIER = 2n;

Expand Down Expand Up @@ -157,9 +159,9 @@ class Account extends BaseAccount {

// @ts-expect-error TODO: fix overload type mismatch
async execute(
calls: Call[],
session: Session,
calls: AllowArray<Call>,
transactionsDetail?: InvocationsDetails,
session?: Session,
): Promise<InvokeFunctionResponse> {
if (this.status === Status.COUNTERFACTUAL) {
throw new Error("Account is not deployed");
Expand All @@ -170,7 +172,7 @@ class Account extends BaseAccount {
transactionsDetail.maxFee = num.toHex(transactionsDetail.maxFee);

const res = await this.cartridge.execute(
calls,
normalizeCalls(calls),
transactionsDetail,
session,
);
Expand All @@ -191,7 +193,7 @@ class Account extends BaseAccount {
}

async estimateInvokeFee(
calls: Call[],
calls: AllowArray<Call>,
details: EstimateFeeDetails = {},
): Promise<EstimateFee> {
details.blockIdentifier = details.blockIdentifier ?? "pending";
Expand Down
38 changes: 19 additions & 19 deletions packages/keychain/src/utils/connection/execute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
import Controller, { diff } from "utils/controller";
import {
Abi,
AllowArray,
Call,
CallData,
InvocationsDetails,
Expand All @@ -23,11 +24,9 @@ export function executeFactory({
}) {
return (controller: Controller, origin: string) =>
async (
transactions: Call | Call[],
transactions: AllowArray<Call>,
abis?: Abi[],
transactionsDetail?: InvocationsDetails & {
chainId?: string;
},
transactionsDetail?: InvocationsDetails,
sync?: boolean,
paymaster?: PaymasterOptions,
): Promise<ExecuteReply | ConnectError> => {
Expand Down Expand Up @@ -56,9 +55,7 @@ export function executeFactory({
throw new Error("No session");
}

const calls = normalizeCalls(transactions);

const missing = diff(mapPolicies(calls), session.policies);
const missing = diff(mapPolicies(transactions), session.policies);
if (missing.length > 0) {
throw new Error(`Missing policies: ${JSON.stringify(missing)}`);
}
Expand All @@ -67,7 +64,7 @@ export function executeFactory({
try {
const { transaction_hash } =
await controller.account.cartridge.executeFromOutside(
calls,
normalizeCalls(transactions),
paymaster.caller,
session,
);
Expand All @@ -81,7 +78,7 @@ export function executeFactory({
}

if (!transactionsDetail.maxFee) {
const estFee = await account.estimateInvokeFee(calls, {
const estFee = await account.estimateInvokeFee(transactions, {
nonce: transactionsDetail.nonce,
});
transactionsDetail.maxFee = estFee.suggestedMaxFee;
Expand All @@ -97,7 +94,11 @@ export function executeFactory({
);
}

const res = await account.execute(calls, session, transactionsDetail);
const res = await account.execute(
transactions,
transactionsDetail,
session,
);

return {
code: ResponseCodes.SUCCESS,
Expand All @@ -112,7 +113,7 @@ export function executeFactory({
};
}

const normalizeCalls = (calls: Call | Call[]): Call[] => {
export const normalizeCalls = (calls: AllowArray<Call>): Call[] => {
return (Array.isArray(calls) ? calls : [calls]).map((call) => {
return {
entrypoint: call.entrypoint,
Expand All @@ -122,12 +123,11 @@ const normalizeCalls = (calls: Call | Call[]): Call[] => {
});
};

const mapPolicies = (calls: Call[]): Policy[] => {
return calls.map(
(txn) =>
({
target: txn.contractAddress,
method: txn.entrypoint,
} as Policy),
);
export const mapPolicies = (calls: AllowArray<Call>): Policy[] => {
return (Array.isArray(calls) ? calls : [calls]).map((call) => {
return {
target: addAddressPadding(call.contractAddress),
method: call.entrypoint,
} as Policy;
});
};
5 changes: 3 additions & 2 deletions packages/keychain/src/utils/controller.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { SignerInterface, BigNumberish } from "starknet";
import { SignerInterface, BigNumberish, addAddressPadding } from "starknet";

import { Policy, Session } from "@cartridge/controller";

Expand Down Expand Up @@ -196,7 +196,8 @@ export function diff(a: Policy[], b: Policy[]): Policy[] {
(prev, policy) =>
b.some(
(approval) =>
approval.target === policy.target &&
addAddressPadding(approval.target) ===
addAddressPadding(policy.target) &&
approval.method === policy.method,
)
? prev
Expand Down

0 comments on commit e386fa5

Please sign in to comment.