-
Notifications
You must be signed in to change notification settings - Fork 132
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
414 additions
and
173 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
157 changes: 157 additions & 0 deletions
157
account-kit/smart-contracts/src/ma-v2/actions/install-validation/installValidation.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
import { | ||
AccountNotFoundError, | ||
IncompatibleClientError, | ||
isSmartAccountClient, | ||
EntityIdOverrideError, | ||
type GetAccountParameter, | ||
type GetEntryPointFromAccount, | ||
type SendUserOperationResult, | ||
type UserOperationOverridesParameter, | ||
} from "@aa-sdk/core"; | ||
import { | ||
encodeFunctionData, | ||
concatHex, | ||
type Address, | ||
type Hex, | ||
type Chain, | ||
type Transport, | ||
} from "viem"; | ||
import { semiModularAccountBytecodeAbi } from "../../abis/semiModularAccountBytecodeAbi.js"; | ||
import type { HookConfig, ValidationConfig } from "../common/types.js"; | ||
import { | ||
serializeValidationConfig, | ||
serializeHookConfig, | ||
serializeModuleEntity, | ||
} from "../common/utils.js"; | ||
import type { SMAV2Account } from "../../account/semiModularAccountV2.js"; | ||
import type { SMASmartAccountClient } from "../../client/client.js"; | ||
|
||
export type InstallValidationParams< | ||
TSMAV2Account extends SMAV2Account | undefined = SMAV2Account | undefined | ||
> = { | ||
validationConfig: ValidationConfig; | ||
selectors: Hex[]; | ||
installData: Hex; | ||
hooks: { | ||
hookConfig: HookConfig; | ||
initData: Hex; | ||
}[]; | ||
} & UserOperationOverridesParameter<GetEntryPointFromAccount<TSMAV2Account>> & | ||
GetAccountParameter<TSMAV2Account>; | ||
|
||
export type UninstallValidationParams< | ||
TSMAV2Account extends SMAV2Account | undefined = SMAV2Account | undefined | ||
> = { | ||
moduleAddress: Address; | ||
entityId: number; | ||
uninstallData: Hex; | ||
hookUninstallDatas: Hex[]; | ||
} & UserOperationOverridesParameter<GetEntryPointFromAccount<TSMAV2Account>> & | ||
GetAccountParameter<TSMAV2Account>; | ||
|
||
export type InstallValidationActions< | ||
TSMAV2Account extends SMAV2Account | undefined = SMAV2Account | undefined | ||
> = { | ||
installValidation: ( | ||
args: InstallValidationParams<TSMAV2Account> | ||
) => Promise<SendUserOperationResult>; | ||
uninstallValidation: ( | ||
args: UninstallValidationParams<TSMAV2Account> | ||
) => Promise<SendUserOperationResult>; | ||
}; | ||
|
||
export const installValidationActions: < | ||
TTransport extends Transport = Transport, | ||
TChain extends Chain = Chain, | ||
TSMAV2Account extends SMAV2Account = SMAV2Account | ||
>( | ||
client: SMASmartAccountClient<TTransport, TChain, TSMAV2Account> | ||
) => InstallValidationActions<TSMAV2Account> = (client) => ({ | ||
installValidation: async ({ | ||
validationConfig, | ||
selectors, | ||
installData, | ||
hooks, | ||
account = client.account, | ||
overrides, | ||
}) => { | ||
if (!account) { | ||
throw new AccountNotFoundError(); | ||
} | ||
|
||
if (!isSmartAccountClient(client)) { | ||
throw new IncompatibleClientError( | ||
"SmartAccountClient", | ||
"installValidation", | ||
client | ||
); | ||
} | ||
|
||
if (validationConfig.entityId === 0) { | ||
throw new EntityIdOverrideError(); | ||
} | ||
|
||
const callData = await account.encodeCallData( | ||
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, | ||
account, | ||
overrides, | ||
}); | ||
}, | ||
|
||
uninstallValidation: async ({ | ||
moduleAddress, | ||
entityId, | ||
uninstallData, | ||
hookUninstallDatas, | ||
account = client.account, | ||
overrides, | ||
}) => { | ||
if (!account) { | ||
throw new AccountNotFoundError(); | ||
} | ||
|
||
if (!isSmartAccountClient(client)) { | ||
throw new IncompatibleClientError( | ||
"SmartAccountClient", | ||
"uninstallValidation", | ||
client | ||
); | ||
} | ||
|
||
const callData = await account.encodeCallData( | ||
encodeFunctionData({ | ||
abi: semiModularAccountBytecodeAbi, | ||
functionName: "uninstallValidation", | ||
args: [ | ||
serializeModuleEntity({ | ||
moduleAddress, | ||
entityId, | ||
}), | ||
uninstallData, | ||
hookUninstallDatas, | ||
], | ||
}) | ||
); | ||
|
||
return client.sendUserOperation({ | ||
uo: callData, | ||
account, | ||
overrides, | ||
}); | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.