UNPKG

@lit-protocol/e2e

Version:

Lit Protocol E2E testing package for running comprehensive integration tests

163 lines 6.94 kB
import { ViemAccountAuthenticator } from '@lit-protocol/auth'; import { generatePrivateKey, privateKeyToAccount } from 'viem/accounts'; import { fundAccount } from './fundAccount'; import { persistGeneratedAccount } from './generated-accounts'; import { getOrCreatePkp } from './pkp-utils'; export async function createTestAccount(testEnv, opts) { console.log(`--- ${`[${opts.label}]`} Creating test account ---`); // 1. store result const privateKey = generatePrivateKey(); persistGeneratedAccount({ label: `createTestAccount:${opts.label}`, privateKey, network: typeof testEnv.networkModule.getNetworkName === 'function' ? testEnv.networkModule.getNetworkName() : process.env['NETWORK'], }); let person = { account: privateKeyToAccount(privateKey), pkp: undefined, eoaAuthContext: undefined, pkpAuthContext: undefined, pkpViemAccount: undefined, paymentManager: undefined, authData: undefined, }; const personAccountAuthData = await ViemAccountAuthenticator.authenticate(person.account); person.authData = personAccountAuthData; console.log(`Address`, person.account.address); console.log(`opts:`, { ...opts, privateKey: opts.privateKey ? opts.privateKey.slice(0, 6) + '...' : undefined, }); // 3. fund it if (opts.fundAccount) { const nativeFundingAmount = process.env['LIVE_NETWORK_FUNDING_AMOUNT'] ?? testEnv.config.nativeFundingAmount; await fundAccount(person.account, testEnv.masterAccount, testEnv.networkModule, { label: 'owner', ifLessThan: nativeFundingAmount, thenFund: nativeFundingAmount, }); } // -- create EOA auth context if (opts.hasEoaAuthContext) { person.eoaAuthContext = await testEnv.authManager.createEoaAuthContext({ config: { account: person.account, }, authConfig: { statement: 'I authorize the Lit Protocol to execute this Lit Action.', domain: 'example.com', resources: [ ['lit-action-execution', '*'], ['pkp-signing', '*'], ['access-control-condition-decryption', '*'], ], expiration: new Date(Date.now() + 1000 * 60 * 15).toISOString(), }, litClient: testEnv.litClient, }); } // 4. also fund the ledger if (opts.fundLedger) { const ledgerDepositAmount = process.env['LIVE_NETWORK_LEDGER_DEPOSIT_AMOUNT'] ?? testEnv.config.ledgerDepositAmount; await testEnv.masterPaymentManager.depositForUser({ userAddress: person.account.address, amountInLitkey: testEnv.config.ledgerDepositAmount, }); } // 5. create PKP if (opts.hasPKP) { person.pkp = await getOrCreatePkp(testEnv.litClient, personAccountAuthData, person.account); // 7. fund the PKP if (opts.fundPKP) { const nativeFundingAmount = process.env['LIVE_NETWORK_FUNDING_AMOUNT'] ?? testEnv.config.nativeFundingAmount; await fundAccount(person.pkp.ethAddress, testEnv.masterAccount, testEnv.networkModule, { label: 'PKP', ifLessThan: nativeFundingAmount, thenFund: nativeFundingAmount, }); } // 8. also fund PKP Ledger if (opts.fundPKPLedger) { const ledgerDepositAmount = process.env['LIVE_NETWORK_LEDGER_DEPOSIT_AMOUNT'] ?? testEnv.config.ledgerDepositAmount; await testEnv.masterPaymentManager.depositForUser({ userAddress: person.pkp.ethAddress, amountInLitkey: testEnv.config.ledgerDepositAmount, }); } // -- Create PKP auth context if (opts.hasPKPAuthContext) { person.pkpAuthContext = await testEnv.authManager.createPkpAuthContext({ authData: personAccountAuthData, pkpPublicKey: person.pkp.pubkey, authConfig: { resources: [ ['pkp-signing', '*'], ['lit-action-execution', '*'], ['access-control-condition-decryption', '*'], ], // 30m expiration expiration: new Date(Date.now() + 1000 * 60 * 30).toISOString(), }, litClient: testEnv.litClient, }); } // Create PKP viem account person.pkpViemAccount = await testEnv.litClient.getPkpViemAccount({ pkpPublicKey: person.pkp.pubkey, authContext: person.pkpAuthContext, chainConfig: testEnv.networkModule.getChainConfig(), }); } // ... end if hasPKP if (opts.sponsor) { // 1. get payment manager person.paymentManager = await testEnv.litClient.getPaymentManager({ account: person.account, }); // 2. Set Restrictions // Convert to Wei using Viem // const wei = parseEther(opts.sponsor.restrictions.totalMaxPriceInEth); // console.log(`- Setting sponsorship restrictions:`, { // totalMaxPriceInEth: opts.sponsor.restrictions.totalMaxPriceInEth, // totalMaxPriceInWei: wei.toString(), // requestsPerPeriod: opts.sponsor.restrictions.requestsPerPeriod, // periodSeconds: opts.sponsor.restrictions.periodSeconds, // }); try { const tx = await person.paymentManager.setRestriction({ // totalMaxPrice: wei.toString(), totalMaxPrice: opts.sponsor.restrictions.totalMaxPriceInWei, requestsPerPeriod: opts.sponsor.restrictions.requestsPerPeriod, periodSeconds: opts.sponsor.restrictions.periodSeconds, }); console.log(`- [setRestriction] TX Hash: ${tx.hash}`); } catch (e) { throw new Error(`❌ Failed to set sponsorship restrictions: ${e}`); } // 3. Sponsor users const userAddresses = opts.sponsor.userAddresses; if (!userAddresses || userAddresses.length === 0) { throw new Error('❌ User addresses are required for the sponsor to fund.'); } try { console.log(`- Sponsoring users:`, userAddresses); const tx = await person.paymentManager.delegatePaymentsBatch({ userAddresses: userAddresses, }); console.log(`[delegatePaymentsBatch] TX Hash: ${tx.hash}`); } catch (e) { throw new Error(`❌ Failed to delegate sponsorship to users: ${e}`); } } return person; } //# sourceMappingURL=createTestAccount.js.map