-
Notifications
You must be signed in to change notification settings - Fork 140
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add plugin manager decorator for MSCA
- Loading branch information
Showing
6 changed files
with
343 additions
and
0 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
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,223 @@ | ||
export const IPluginManagerAbi = [ | ||
{ | ||
anonymous: false, | ||
inputs: [ | ||
{ | ||
indexed: true, | ||
internalType: "address", | ||
name: "plugin", | ||
type: "address", | ||
}, | ||
{ | ||
indexed: true, | ||
internalType: "address", | ||
name: "providingPlugin", | ||
type: "address", | ||
}, | ||
], | ||
name: "PluginIgnoredHookUnapplyCallbackFailure", | ||
type: "event", | ||
}, | ||
{ | ||
anonymous: false, | ||
inputs: [ | ||
{ | ||
indexed: true, | ||
internalType: "address", | ||
name: "plugin", | ||
type: "address", | ||
}, | ||
], | ||
name: "PluginIgnoredUninstallCallbackFailure", | ||
type: "event", | ||
}, | ||
{ | ||
anonymous: false, | ||
inputs: [ | ||
{ | ||
indexed: true, | ||
internalType: "address", | ||
name: "plugin", | ||
type: "address", | ||
}, | ||
{ | ||
indexed: false, | ||
internalType: "bytes32", | ||
name: "manifestHash", | ||
type: "bytes32", | ||
}, | ||
{ | ||
indexed: false, | ||
internalType: "FunctionReference[]", | ||
name: "dependencies", | ||
type: "bytes21[]", | ||
}, | ||
{ | ||
components: [ | ||
{ | ||
internalType: "address", | ||
name: "providingPlugin", | ||
type: "address", | ||
}, | ||
{ | ||
internalType: "bytes4", | ||
name: "selector", | ||
type: "bytes4", | ||
}, | ||
{ | ||
components: [ | ||
{ | ||
internalType: "uint8", | ||
name: "preExecHookFunctionId", | ||
type: "uint8", | ||
}, | ||
{ | ||
internalType: "bool", | ||
name: "isPostHookUsed", | ||
type: "bool", | ||
}, | ||
{ | ||
internalType: "uint8", | ||
name: "postExecHookFunctionId", | ||
type: "uint8", | ||
}, | ||
], | ||
internalType: "struct IPluginManager.InjectedHooksInfo", | ||
name: "injectedHooksInfo", | ||
type: "tuple", | ||
}, | ||
{ | ||
internalType: "bytes", | ||
name: "hookApplyData", | ||
type: "bytes", | ||
}, | ||
], | ||
indexed: false, | ||
internalType: "struct IPluginManager.InjectedHook[]", | ||
name: "injectedHooks", | ||
type: "tuple[]", | ||
}, | ||
], | ||
name: "PluginInstalled", | ||
type: "event", | ||
}, | ||
{ | ||
anonymous: false, | ||
inputs: [ | ||
{ | ||
indexed: true, | ||
internalType: "address", | ||
name: "plugin", | ||
type: "address", | ||
}, | ||
{ | ||
indexed: true, | ||
internalType: "bool", | ||
name: "callbacksSucceeded", | ||
type: "bool", | ||
}, | ||
], | ||
name: "PluginUninstalled", | ||
type: "event", | ||
}, | ||
{ | ||
inputs: [ | ||
{ | ||
internalType: "address", | ||
name: "plugin", | ||
type: "address", | ||
}, | ||
{ | ||
internalType: "bytes32", | ||
name: "manifestHash", | ||
type: "bytes32", | ||
}, | ||
{ | ||
internalType: "bytes", | ||
name: "pluginInitData", | ||
type: "bytes", | ||
}, | ||
{ | ||
internalType: "FunctionReference[]", | ||
name: "dependencies", | ||
type: "bytes21[]", | ||
}, | ||
{ | ||
components: [ | ||
{ | ||
internalType: "address", | ||
name: "providingPlugin", | ||
type: "address", | ||
}, | ||
{ | ||
internalType: "bytes4", | ||
name: "selector", | ||
type: "bytes4", | ||
}, | ||
{ | ||
components: [ | ||
{ | ||
internalType: "uint8", | ||
name: "preExecHookFunctionId", | ||
type: "uint8", | ||
}, | ||
{ | ||
internalType: "bool", | ||
name: "isPostHookUsed", | ||
type: "bool", | ||
}, | ||
{ | ||
internalType: "uint8", | ||
name: "postExecHookFunctionId", | ||
type: "uint8", | ||
}, | ||
], | ||
internalType: "struct IPluginManager.InjectedHooksInfo", | ||
name: "injectedHooksInfo", | ||
type: "tuple", | ||
}, | ||
{ | ||
internalType: "bytes", | ||
name: "hookApplyData", | ||
type: "bytes", | ||
}, | ||
], | ||
internalType: "struct IPluginManager.InjectedHook[]", | ||
name: "injectedHooks", | ||
type: "tuple[]", | ||
}, | ||
], | ||
name: "installPlugin", | ||
outputs: [], | ||
stateMutability: "nonpayable", | ||
type: "function", | ||
}, | ||
{ | ||
inputs: [ | ||
{ | ||
internalType: "address", | ||
name: "plugin", | ||
type: "address", | ||
}, | ||
{ | ||
internalType: "bytes", | ||
name: "config", | ||
type: "bytes", | ||
}, | ||
{ | ||
internalType: "bytes", | ||
name: "pluginUninstallData", | ||
type: "bytes", | ||
}, | ||
{ | ||
internalType: "bytes[]", | ||
name: "hookUnapplyData", | ||
type: "bytes[]", | ||
}, | ||
], | ||
name: "uninstallPlugin", | ||
outputs: [], | ||
stateMutability: "nonpayable", | ||
type: "function", | ||
}, | ||
] as const; |
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,18 @@ | ||
import type { ISmartAccountProvider } from "@alchemy/aa-core"; | ||
import type { MSCA } from "../../builder"; | ||
import { installPlugin, type InstallPluginParams } from "./installPlugin.js"; | ||
import { | ||
uninstallPlugin, | ||
type UninstallPluginParams, | ||
} from "./uninstallPlugin.js"; | ||
|
||
export const pluginManagerDecorator = < | ||
P extends ISmartAccountProvider & { account: MSCA } | ||
>( | ||
provider: P | ||
) => ({ | ||
installPlugin: (params: InstallPluginParams) => | ||
installPlugin<P>(provider, params), | ||
uninstallPlugin: (params: UninstallPluginParams) => | ||
uninstallPlugin<P>(provider, params), | ||
}); |
54 changes: 54 additions & 0 deletions
54
packages/accounts/src/msca/plugins/manager/installPlugin.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,54 @@ | ||
import type { ISmartAccountProvider } from "@alchemy/aa-core"; | ||
import { | ||
encodeFunctionData, | ||
encodeFunctionResult, | ||
keccak256, | ||
type Address, | ||
type Hash, | ||
} from "viem"; | ||
import { IPluginAbi } from "../../abis/IPlugin.js"; | ||
import { IPluginManagerAbi } from "../../abis/IPluginManager.js"; | ||
import type { MSCA } from "../../builder"; | ||
import type { InjectedHook } from "./types"; | ||
|
||
export type InstallPluginParams = { | ||
pluginAddress: Address; | ||
manifestHash?: Hash; | ||
pluginInitData?: Hash; | ||
dependencies?: Address[]; | ||
injectedHooks?: InjectedHook[]; | ||
}; | ||
|
||
export async function installPlugin< | ||
P extends ISmartAccountProvider & { account: MSCA } | ||
>(provider: P, params: InstallPluginParams) { | ||
const pluginManifest = await provider.rpcClient.readContract({ | ||
abi: IPluginAbi, | ||
address: params.pluginAddress, | ||
functionName: "pluginManifest", | ||
}); | ||
// use the manifest hash passed in or get it from the plugin | ||
const manifestHash: Hash = | ||
params.manifestHash ?? | ||
keccak256( | ||
encodeFunctionResult({ | ||
abi: IPluginAbi, | ||
functionName: "pluginManifest", | ||
result: pluginManifest, | ||
}) | ||
); | ||
|
||
const callData = encodeFunctionData({ | ||
abi: IPluginManagerAbi, | ||
functionName: "installPlugin", | ||
args: [ | ||
params.pluginAddress, | ||
manifestHash, | ||
params.pluginInitData ?? "0x", | ||
params.dependencies ?? [], | ||
params.injectedHooks ?? [], | ||
], | ||
}); | ||
|
||
return provider.sendUserOperation(callData); | ||
} |
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,16 @@ | ||
import type { Address, Hash } from "viem"; | ||
|
||
export type InjectedHooksInfo = { | ||
preExecHookFunctionId: number; | ||
isPostHookUsed: boolean; | ||
postExecHookFunctionId: number; | ||
}; | ||
|
||
export type InjectedHook = { | ||
// The plugin that provides the hook | ||
providingPlugin: Address; | ||
// Either a plugin-defined execution function, or the native function executeFromPluginExternal | ||
selector: Hash; | ||
injectedHooksInfo: InjectedHooksInfo; | ||
hookApplyData: Hash; | ||
}; |
28 changes: 28 additions & 0 deletions
28
packages/accounts/src/msca/plugins/manager/uninstallPlugin.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,28 @@ | ||
import type { ISmartAccountProvider } from "@alchemy/aa-core"; | ||
import { encodeFunctionData, type Address, type Hash } from "viem"; | ||
import { IPluginManagerAbi } from "../../abis/IPluginManager.js"; | ||
import type { MSCA } from "../../builder"; | ||
|
||
export type UninstallPluginParams = { | ||
pluginAddress: Address; | ||
config?: Hash; | ||
pluginUninstallData?: Hash; | ||
hookUnapplyData?: Hash[]; | ||
}; | ||
|
||
export async function uninstallPlugin< | ||
P extends ISmartAccountProvider & { account: MSCA } | ||
>(provider: P, params: UninstallPluginParams) { | ||
const callData = encodeFunctionData({ | ||
abi: IPluginManagerAbi, | ||
functionName: "uninstallPlugin", | ||
args: [ | ||
params.pluginAddress, | ||
params.config ?? "0x", | ||
params.pluginUninstallData ?? "0x", | ||
params.hookUnapplyData ?? [], | ||
], | ||
}); | ||
|
||
return provider.sendUserOperation(callData); | ||
} |