UNPKG

@lit-protocol/vincent-scaffold-sdk

Version:

Shared build configuration and utilities for Vincent abilities and policies

143 lines (142 loc) • 8.03 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.init = void 0; const constants_1 = require("@lit-protocol/constants"); const contracts_sdk_1 = require("@lit-protocol/contracts-sdk"); const vincent_contracts_sdk_1 = require("@lit-protocol/vincent-contracts-sdk"); const ethers_1 = require("ethers"); const managers_1 = require("../managers"); const chain_client_1 = require("../managers/chain-client"); const utils_1 = require("../utils"); const setupFunderWallet = async (provider) => { const funderWallet = new ethers_1.ethers.Wallet(managers_1.ENV.TEST_FUNDER_PRIVATE_KEY, provider); console.log("āœ… Funder Wallet Ethers Wallet:", funderWallet.address); const funderBalance = await provider.getBalance(funderWallet.address); console.log(" ↳ Balance:", ethers_1.ethers.utils.formatEther(funderBalance)); if (funderBalance.lt(ethers_1.ethers.utils.parseEther("0.13"))) { const errorMessage = `āŒ Insufficient funder balance. Current balance is below the required 0.13 threshold. Please top up your funder wallet at: https://chronicle-yellowstone-faucet.getlit.dev/`; console.log(errorMessage); throw new Error(errorMessage); } return funderWallet; }; const setupAccountManagers = async (stateManager, funderWallet, provider, network, appManagerFundAmount, agentWalletPkpOwnerFundAmount) => { const appManagerResult = await (0, utils_1.setupAccount)("appManager", "App Manager Ethers Wallet", managers_1.ENV.TEST_APP_MANAGER_PRIVATE_KEY, stateManager, funderWallet, provider, appManagerFundAmount); const agentWalletPkpOwnerResult = await (0, utils_1.setupAccount)("agentWalletPkpOwner", "Agent Wallet PKP Owner Ethers Wallet", managers_1.ENV.TEST_AGENT_WALLET_PKP_OWNER_PRIVATE_KEY, stateManager, funderWallet, provider, agentWalletPkpOwnerFundAmount); const agentWalletContractsClient = new contracts_sdk_1.LitContracts({ signer: agentWalletPkpOwnerResult.wallet, network: constants_1.LIT_NETWORK[network] || constants_1.LIT_NETWORK.Datil, }); await agentWalletContractsClient.connect(); return { appManagerResult, agentWalletPkpOwnerResult, agentWalletContractsClient, }; }; const setupDelegateeWallet = async (stateManager, funderWallet, provider, delegateeFundAmount) => { const delegateeResult = await (0, utils_1.setupAccount)("appDelegatee", "Delegatee Ethers wallet", managers_1.ENV.TEST_APP_DELEGATEE_PRIVATE_KEY, stateManager, funderWallet, provider, delegateeFundAmount); return { delegateeAccount: delegateeResult.account, delegateeEthersWallet: delegateeResult.wallet }; }; const init = async ({ network, fundAmount = "0.01", fundingAmounts, deploymentStatus = "dev", }) => { const _fundingAmounts = { appManager: ethers_1.ethers.utils.parseEther(fundingAmounts?.appManager || fundAmount), agentWalletPkpOwner: ethers_1.ethers.utils.parseEther(fundingAmounts?.agentWalletPkpOwner || fundAmount), delegatee: ethers_1.ethers.utils.parseEther(fundingAmounts?.delegatee || fundAmount), pkp: ethers_1.ethers.utils.parseEther(fundingAmounts?.pkp || fundAmount), }; /** * ==================================== * Initialize State Manager * ==================================== */ // Auto-detect test filename and use a temporary config hash that will be updated later const detectedTestFileName = managers_1.StateManager.autoDetectTestFileName(); const temporaryConfigHash = 'pending'; // Will be updated when configuration is provided console.log(`\nšŸ—ļø Initialising E2E environment for ${detectedTestFileName} (configuration will be detected during app registration)`); const stateManager = new managers_1.StateManager(network, detectedTestFileName, temporaryConfigHash); await stateManager.loadState(); /** * ==================================== * Setup all components using helper functions * ==================================== */ // Create ethers provider for Yellowstone const provider = new ethers_1.ethers.providers.JsonRpcProvider(managers_1.ENV.YELLOWSTONE_RPC_URL); const funderWallet = await setupFunderWallet(provider); const { appManagerResult, agentWalletPkpOwnerResult, agentWalletContractsClient, } = await setupAccountManagers(stateManager, funderWallet, provider, network, _fundingAmounts.appManager, _fundingAmounts.agentWalletPkpOwner); const { delegateeAccount, delegateeEthersWallet } = await setupDelegateeWallet(stateManager, funderWallet, provider, _fundingAmounts.delegatee); /** * ==================================== * Create Vincent contracts SDK instances * ==================================== */ const appManagerContractClient = (0, vincent_contracts_sdk_1.getClient)({ signer: appManagerResult.wallet }); const agentWalletPkpOwnerContractClient = (0, vincent_contracts_sdk_1.getClient)({ signer: agentWalletPkpOwnerResult.wallet }); /** * ==================================== * Mint capacity credits for the delegatee * ==================================== */ const { capacityCredits: mintCCRes, isNew: isNewCC } = await stateManager.getOrMintCapacityCredits(async () => { return await (0, utils_1.mintCapacityCredits)(delegateeEthersWallet, network); }); console.log("āœ… Delegatee capacity credits:", mintCCRes, isNewCC ? "(newly minted)" : "(from cache)"); /** * ==================================== * Create and return a function to mint an Agent Wallet PKP * ==================================== */ const mintAgentWalletPkp = async ({ abilityAndPolicyIpfsCids, }) => { const { pkp, isNew } = await stateManager.getOrMintPKP(async () => { return await (0, utils_1.mintNewPkp)(agentWalletPkpOwnerResult.privateKey, network, ...abilityAndPolicyIpfsCids); }); // Fund the PKP if it's newly minted await (0, utils_1.fundPKPIfNeeded)(pkp.ethAddress, isNew, funderWallet, provider, _fundingAmounts.pkp); // Save state after PKP operations await stateManager.saveState(); return pkp; }; // Save state if any new accounts were generated await stateManager.saveState(); console.log("\n----- (init.ts) Initialisation Completed \n"); return { accounts: { funder: { ethersWallet: funderWallet, }, agentWalletPkpOwner: { ethersWallet: agentWalletPkpOwnerResult.wallet, mintAgentWalletPkp: mintAgentWalletPkp, permittedAuthMethods: async ({ agentWalletPkp, abilityAndPolicyIpfsCids, }) => { const results = []; for (const ipfsCid of abilityAndPolicyIpfsCids) { const result = await agentWalletContractsClient.addPermittedAction({ pkpTokenId: agentWalletPkp.tokenId, ipfsId: ipfsCid, authMethodScopes: [ 1, // Sign Anything ], }); results.push(result.transactionHash); } return results; }, permitAppVersion: (0, chain_client_1.createPermitAppVersionFunction)(stateManager, agentWalletPkpOwnerContractClient), }, appManager: { ethersWallet: appManagerResult.wallet, registerApp: (0, chain_client_1.createRegisterAppFunction)(stateManager, appManagerContractClient, delegateeAccount), validateAbilityExecution: (0, chain_client_1.createValidateAbilityExecutionFunction)(appManagerContractClient), }, delegatee: { ethersWallet: delegateeEthersWallet, }, }, ethersAccounts: {}, }; }; exports.init = init;