|
| 1 | +import { |
| 2 | + delegator, |
| 3 | + delegatee, |
| 4 | + appManager, |
| 5 | + funder, |
| 6 | + getChainHelpers, |
| 7 | + ensureUnexpiredCapacityToken, |
| 8 | + type PkpInfo, |
| 9 | +} from '../index'; |
| 10 | +import { type PermissionData } from '@lit-protocol/vincent-contracts-sdk'; |
| 11 | +import { type Wallet } from 'ethers'; |
| 12 | + |
| 13 | +export interface SetupResult { |
| 14 | + agentPkpInfo: PkpInfo; |
| 15 | + wallets: { |
| 16 | + appDelegatee: Wallet; |
| 17 | + funder: Wallet; |
| 18 | + appManager: Wallet; |
| 19 | + agentWalletOwner: Wallet; |
| 20 | + }; |
| 21 | + appId: number; |
| 22 | + appVersion: number; |
| 23 | +} |
| 24 | + |
| 25 | +/** |
| 26 | + * Helper function to set up a Vincent development environment. |
| 27 | + * This function handles all the necessary setup steps including: |
| 28 | + * - Checking and funding all required accounts (funder, app delegatee, app manager) |
| 29 | + * - Registering or updating your app with abilities and policies |
| 30 | + * - Creating or using an existing agent PKP |
| 31 | + * - Setting up permissions for the agent PKP |
| 32 | + * - Ensuring a valid capacity token exists |
| 33 | + * |
| 34 | + * @param permissionData permission data containing abilities and their policies |
| 35 | + * @returns the setup result including agent PKP info, wallets, app ID, and app version |
| 36 | + * @example |
| 37 | + * ```typescript |
| 38 | + * const permissionData = { |
| 39 | + * // EVM Transaction Signer Ability has no policies |
| 40 | + * // If yours does, you would add the policies to this object |
| 41 | + * [bundledVincentAbility.ipfsCid]: {}, |
| 42 | + * }; |
| 43 | + * const result = await setupVincentDevelopmentEnvironment({ permissionData }); |
| 44 | + * ``` |
| 45 | + */ |
| 46 | +export const setupVincentDevelopmentEnvironment = async ({ |
| 47 | + permissionData, |
| 48 | +}: { |
| 49 | + permissionData: PermissionData; |
| 50 | +}): Promise<SetupResult> => { |
| 51 | + // Check and fund all required accounts |
| 52 | + await funder.checkFunderBalance(); |
| 53 | + await delegatee.ensureAppDelegateeFunded(); |
| 54 | + await appManager.ensureAppManagerFunded(); |
| 55 | + |
| 56 | + const chainHelpers = await getChainHelpers(); |
| 57 | + const wallets = chainHelpers.wallets; |
| 58 | + |
| 59 | + const abilityIpfsCids: string[] = Object.keys(permissionData); |
| 60 | + const abilityPolicies: string[][] = abilityIpfsCids.map((abilityIpfsCid) => { |
| 61 | + return Object.keys(permissionData[abilityIpfsCid]); |
| 62 | + }); |
| 63 | + |
| 64 | + // If an app exists for the delegatee, we will create a new app version with the new ipfs cids |
| 65 | + // Otherwise, we will create an app w/ version 1 appVersion with the new ipfs cids |
| 66 | + const existingApp = await delegatee.getAppInfo(); |
| 67 | + let appId: number; |
| 68 | + let appVersion: number; |
| 69 | + if (!existingApp) { |
| 70 | + const newApp = await appManager.registerNewApp({ abilityIpfsCids, abilityPolicies }); |
| 71 | + appId = newApp.appId; |
| 72 | + appVersion = newApp.appVersion; |
| 73 | + } else { |
| 74 | + const newAppVersion = await appManager.registerNewAppVersion({ |
| 75 | + abilityIpfsCids, |
| 76 | + abilityPolicies, |
| 77 | + }); |
| 78 | + appId = existingApp.appId; |
| 79 | + appVersion = newAppVersion.appVersion; |
| 80 | + } |
| 81 | + |
| 82 | + const agentPkpInfo = await delegator.getFundedAgentPkp(); |
| 83 | + |
| 84 | + await delegator.permitAppVersionForAgentWalletPkp({ |
| 85 | + permissionData, |
| 86 | + appId, |
| 87 | + appVersion, |
| 88 | + agentPkpInfo, |
| 89 | + }); |
| 90 | + |
| 91 | + await delegator.addPermissionForAbilities( |
| 92 | + wallets.agentWalletOwner, |
| 93 | + agentPkpInfo.tokenId, |
| 94 | + abilityIpfsCids, |
| 95 | + ); |
| 96 | + |
| 97 | + // Ensure capacity token is valid and unexpired |
| 98 | + await ensureUnexpiredCapacityToken(wallets.appDelegatee); |
| 99 | + |
| 100 | + return { |
| 101 | + agentPkpInfo, |
| 102 | + wallets, |
| 103 | + appId, |
| 104 | + appVersion, |
| 105 | + }; |
| 106 | +}; |
0 commit comments