Skip to content

Commit

Permalink
feat: add session key plugin abi and plugingen
Browse files Browse the repository at this point in the history
  • Loading branch information
denniswon committed Dec 15, 2023
1 parent abd4558 commit ec3de5b
Show file tree
Hide file tree
Showing 9 changed files with 1,301 additions and 6 deletions.
54 changes: 50 additions & 4 deletions examples/aa-simple-dapp/src/hooks/useAlchemyProvider.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
import { chain, gasManagerPolicyId } from "@/config/client";
import { getRpcUrl } from "@/config/rpc";
import {
MSCA,
MultiOwnerPlugin,
SessionKeyPlugin,
createMultiOwnerMSCA,
getDefaultLightAccountFactoryAddress,
encodeFunctionReference,
getDefaultMultiOwnerMSCAFactoryAddress,
installPlugin,
} from "@alchemy/aa-accounts";
import { AlchemyProvider } from "@alchemy/aa-alchemy";
import {
SmartAccountSigner,
getDefaultEntryPointAddress,
} from "@alchemy/aa-core";
import { useCallback, useState } from "react";
import { Address } from "viem";
import { Address, encodeAbiParameters, parseAbiParameters } from "viem";

export enum PluginType {
SESSION_KEY,
}

export const useAlchemyProvider = () => {
const [provider, setProvider] = useState<AlchemyProvider>(
Expand All @@ -29,7 +38,7 @@ export const useAlchemyProvider = () => {
owner: signer,
chain,
entryPointAddress: getDefaultEntryPointAddress(chain),
factoryAddress: getDefaultLightAccountFactoryAddress(chain),
factoryAddress: getDefaultMultiOwnerMSCAFactoryAddress(chain),
accountAddress: account,
});
})
Expand All @@ -50,5 +59,42 @@ export const useAlchemyProvider = () => {
return disconnectedProvider;
}, [provider]);

return { provider, connectProviderToAccount, disconnectProviderFromAccount };
const pluginInstall = useCallback(
async (type: PluginType) => {
if (!provider.isConnected<MSCA>()) {
return;
}

switch (type) {
case PluginType.SESSION_KEY:
return installPlugin(provider, {
pluginAddress: SessionKeyPlugin.meta.address[chain.id],
pluginInitData: encodeAbiParameters(
parseAbiParameters("address[]"),
[[]]
),
dependencies: [
encodeFunctionReference(
MultiOwnerPlugin.meta.address[chain.id],
"0x0"
),
encodeFunctionReference(
MultiOwnerPlugin.meta.address[chain.id],
"0x1"
),
],
});
default:
throw new Error("Unexpected plugin type", type);
}
},
[provider]
);

return {
provider,
connectProviderToAccount,
disconnectProviderFromAccount,
pluginInstall,
};
};
1 change: 1 addition & 0 deletions packages/accounts/plugindefs/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export { MultiOwnerPluginGenConfig } from "./multi-owner/config.js";
export { SessionKeyPluginGenConfig } from "./session-key/config.js";
export { TokenReceiverPluginGenConfig } from "./token-receiver/config.js";
Loading

0 comments on commit ec3de5b

Please sign in to comment.