Skip to content

Commit

Permalink
feat: add calldata wrapping to install+uninstallvalidation
Browse files Browse the repository at this point in the history
  • Loading branch information
howydev committed Dec 18, 2024
1 parent 800a5ac commit d94699a
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 41 deletions.
25 changes: 16 additions & 9 deletions account-kit/smart-contracts/src/ma-v2/account/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ const executeUserOpSelector: Hex = "0x8DD7712F";

export const DEFAULT_OWNER_ENTITY_ID = 0;

export type CalldataEncoder = {
encodeCallData: (callData: Hex) => Promise<Hex>;
};

export type ExecutionDataView = {
module: Address;
skipRuntimeValidation: boolean;
Expand All @@ -56,13 +60,15 @@ export type ValidationDataParams = {
};

export type SMAV2Account<
TSigner extends SmartAccountSigner = SmartAccountSigner
> = SmartContractAccountWithSigner<"SMAV2Account", TSigner, "0.7.0"> & {
getExecutionData: (selector: Hex) => Promise<ExecutionDataView>;
getValidationData: (
args: ValidationDataParams
) => Promise<ValidationDataView>;
};
TSigner extends SmartAccountSigner = SmartAccountSigner,
TCalldataEncoder extends CalldataEncoder = CalldataEncoder
> = SmartContractAccountWithSigner<"SMAV2Account", TSigner, "0.7.0"> &
TCalldataEncoder & {
getExecutionData: (selector: Hex) => Promise<ExecutionDataView>;
getValidationData: (
args: ValidationDataParams
) => Promise<ValidationDataView>;
};

export type CreateSMAV2AccountParams<
TTransport extends Transport = Transport,
Expand All @@ -83,8 +89,8 @@ export type CreateSMAV2AccountParams<
entityId: number;
}
| {
isGlobalValidation: never;
entityId: never;
isGlobalValidation?: never;
entityId?: never;
}
);

Expand Down Expand Up @@ -258,5 +264,6 @@ export async function createSMAV2Account(
getValidationData,
encodeExecute,
encodeBatchExecute,
encodeCallData,
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import {
type GetAccountParameter,
type GetEntryPointFromAccount,
type SendUserOperationResult,
type SmartContractAccount,
type SmartAccountClient,
type UserOperationOverridesParameter,
type SmartContractAccount,
} from "@aa-sdk/core";
import {
type Address,
Expand All @@ -26,6 +26,7 @@ import {
serializeHookConfig,
serializeModuleEntity,
} from "../common/utils.js";
import type { CalldataEncoder } from "../../account/account.js";

export type InstallValidationParams<
TAccount extends SmartContractAccount | undefined =
Expand Down Expand Up @@ -72,7 +73,7 @@ export const installValidationActions: <
TChain extends Chain | undefined = Chain | undefined,
TAccount extends SmartContractAccount = SmartContractAccount
>(
client: SmartAccountClient<TTransport, TChain, TAccount>
client: SmartAccountClient<TTransport, TChain, TAccount> & CalldataEncoder
) => InstallValidationActions<TAccount> = (client) => ({
installValidation: async ({
validationConfig,
Expand All @@ -98,18 +99,22 @@ export const installValidationActions: <
throw new EntityIdOverrideError();
}

const callData = encodeFunctionData({
abi: semiModularAccountBytecodeAbi,
functionName: "installValidation",
args: [
serializeValidationConfig(validationConfig),
selectors,
installData,
hooks.map((hook: { hookConfig: HookConfig; initData: Hex }) =>
concatHex([serializeHookConfig(hook.hookConfig), hook.initData])
),
],
});
const { encodeCallData } = client;

const callData = await encodeCallData(

Check failure on line 104 in account-kit/smart-contracts/src/ma-v2/actions/install-validation/installValidation.ts

View workflow job for this annotation

GitHub Actions / Build and Test

src/ma-v2/client/client.test.ts > MA v2 Tests > adds a session key with no permissions

TypeError: encodeCallData is not a function ❯ Object.installValidation src/ma-v2/actions/install-validation/installValidation.ts:104:28 ❯ src/ma-v2/client/client.test.ts:76:33

Check failure on line 104 in account-kit/smart-contracts/src/ma-v2/actions/install-validation/installValidation.ts

View workflow job for this annotation

GitHub Actions / Build and Test

src/ma-v2/client/client.test.ts > MA v2 Tests > uninstalls a session key

TypeError: encodeCallData is not a function ❯ Object.installValidation src/ma-v2/actions/install-validation/installValidation.ts:104:28 ❯ src/ma-v2/client/client.test.ts:132:33
encodeFunctionData({
abi: semiModularAccountBytecodeAbi,
functionName: "installValidation",
args: [
serializeValidationConfig(validationConfig),
selectors,
installData,
hooks.map((hook: { hookConfig: HookConfig; initData: Hex }) =>
concatHex([serializeHookConfig(hook.hookConfig), hook.initData])
),
],
})
);

return client.sendUserOperation({
uo: callData,
Expand Down Expand Up @@ -138,18 +143,22 @@ export const installValidationActions: <
);
}

const callData = encodeFunctionData({
abi: semiModularAccountBytecodeAbi,
functionName: "uninstallValidation",
args: [
serializeModuleEntity({
moduleAddress,
entityId,
}),
uninstallData,
hookUninstallDatas,
],
});
const { encodeCallData } = client;

const callData = await encodeCallData(
encodeFunctionData({
abi: semiModularAccountBytecodeAbi,
functionName: "uninstallValidation",
args: [
serializeModuleEntity({
moduleAddress,
entityId,
}),
uninstallData,
hookUninstallDatas,
],
})
);

return client.sendUserOperation({
uo: callData,
Expand Down
5 changes: 3 additions & 2 deletions account-kit/smart-contracts/src/ma-v2/client/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import { SingleSignerValidationModule } from "../modules/single-signer-validatio

describe("MA v2 Tests", async () => {
const instance = local070Instance;
let client: ReturnType<typeof instance.getClient>;
let client: ReturnType<typeof instance.getClient> &
ReturnType<typeof publicActions>;

beforeAll(async () => {
client = instance.getClient().extend(publicActions);
Expand Down Expand Up @@ -165,7 +166,7 @@ describe("MA v2 Tests", async () => {
signer: sessionKey,
transport: custom(instance.getClient()),
accountAddress: provider.getAddress(),
entityId: 1n,
entityId: 1,
isGlobalValidation: true,
});

Expand Down
19 changes: 15 additions & 4 deletions account-kit/smart-contracts/src/ma-v2/client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { type Chain, type CustomTransport, type Transport } from "viem";

import {
createSMAV2Account,
type CalldataEncoder,
type CreateSMAV2AccountParams,
type SMAV2Account,
} from "../account/account.js";
Expand All @@ -24,10 +25,13 @@ export type CreateSMAV2AccountClientParams<

export function createSMAV2AccountClient<
TChain extends Chain = Chain,
TSigner extends SmartAccountSigner = SmartAccountSigner
TSigner extends SmartAccountSigner = SmartAccountSigner,
TCalldataEncoder extends CalldataEncoder = CalldataEncoder
>(
args: CreateSMAV2AccountClientParams<Transport, TChain, TSigner>
): Promise<SmartAccountClient<CustomTransport, Chain, SMAV2Account<TSigner>>>;
): Promise<
SmartAccountClient<CustomTransport, Chain, SMAV2Account> & TCalldataEncoder
>;

/**
* Creates a MAv2 account client using the provided configuration parameters.
Expand Down Expand Up @@ -60,13 +64,20 @@ export function createSMAV2AccountClient<
*/
export async function createSMAV2AccountClient({
...config
}: CreateSMAV2AccountClientParams): Promise<SmartAccountClient> {
}: CreateSMAV2AccountClientParams): Promise<
SmartAccountClient & CalldataEncoder
> {
const maV2Account = await createSMAV2Account({
...config,
});

return createSmartAccountClient({
const client = createSmartAccountClient({
...config,
account: maV2Account,
});

return {
...client,
encodeCallData: maV2Account.encodeCallData,
};
}

0 comments on commit d94699a

Please sign in to comment.