Skip to content

Commit 1d9ab28

Browse files
committed
refactor(e2e): enhance createTestAccount function with optional auth contexts and improve code structure
1 parent e8692e1 commit 1d9ab28

File tree

1 file changed

+56
-39
lines changed

1 file changed

+56
-39
lines changed

packages/e2e/src/helper/createTestAccount.ts

Lines changed: 56 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,26 @@
1-
import { generatePrivateKey, privateKeyToAccount } from 'viem/accounts';
2-
import { TestEnv } from './createTestEnv';
31
import { ViemAccountAuthenticator } from '@lit-protocol/auth';
4-
import { getOrCreatePkp } from './pkp-utils';
52
import { PKPData } from '@lit-protocol/schemas';
6-
import { fundAccount } from './fundAccount';
3+
import { generatePrivateKey, privateKeyToAccount } from 'viem/accounts';
74
import { AuthContext } from '../types';
8-
import { parseEther } from 'viem/utils';
5+
import { TestEnv } from './createTestEnv';
6+
import { fundAccount } from './fundAccount';
7+
import { getOrCreatePkp } from './pkp-utils';
98

109
type CreateTestAccountOpts = {
1110
label: string;
1211
fundAccount: boolean;
1312
fundLedger: boolean;
13+
hasEoaAuthContext?: boolean;
1414
hasPKP: boolean;
1515
fundPKP: boolean;
1616
fundPKPLedger: boolean;
17+
hasPKPAuthContext?: boolean;
1718
sponsor?: {
1819
restrictions: {
20+
/**
21+
* This price will be divided by threshold to get per-request price
22+
* Make sure to keep your Ledger Balance high enough to cover this!
23+
*/
1924
totalMaxPriceInWei: string;
2025
requestsPerPeriod: string;
2126
periodSeconds: string;
@@ -32,6 +37,9 @@ export type CreateTestAccountResult = {
3237
pkpViemAccount?: Awaited<
3338
ReturnType<TestEnv['litClient']['getPkpViemAccount']>
3439
>;
40+
paymentManager?: Awaited<
41+
ReturnType<TestEnv['litClient']['getPaymentManager']>
42+
>;
3543
};
3644

3745
export async function createTestAccount(
@@ -42,6 +50,11 @@ export async function createTestAccount(
4250
// 1. store result
4351
let person: CreateTestAccountResult = {
4452
account: privateKeyToAccount(generatePrivateKey()),
53+
pkp: undefined,
54+
eoaAuthContext: undefined,
55+
pkpAuthContext: undefined,
56+
pkpViemAccount: undefined,
57+
paymentManager: undefined,
4558
};
4659

4760
const personAccountAuthData = await ViemAccountAuthenticator.authenticate(
@@ -64,22 +77,24 @@ export async function createTestAccount(
6477
);
6578

6679
// -- create EOA auth context
67-
person.eoaAuthContext = await testEnv.authManager.createEoaAuthContext({
68-
config: {
69-
account: person.account,
70-
},
71-
authConfig: {
72-
statement: 'I authorize the Lit Protocol to execute this Lit Action.',
73-
domain: 'example.com',
74-
resources: [
75-
['lit-action-execution', '*'],
76-
['pkp-signing', '*'],
77-
['access-control-condition-decryption', '*'],
78-
],
79-
expiration: new Date(Date.now() + 1000 * 60 * 15).toISOString(),
80-
},
81-
litClient: testEnv.litClient,
82-
});
80+
if (opts.hasEoaAuthContext) {
81+
person.eoaAuthContext = await testEnv.authManager.createEoaAuthContext({
82+
config: {
83+
account: person.account,
84+
},
85+
authConfig: {
86+
statement: 'I authorize the Lit Protocol to execute this Lit Action.',
87+
domain: 'example.com',
88+
resources: [
89+
['lit-action-execution', '*'],
90+
['pkp-signing', '*'],
91+
['access-control-condition-decryption', '*'],
92+
],
93+
expiration: new Date(Date.now() + 1000 * 60 * 15).toISOString(),
94+
},
95+
litClient: testEnv.litClient,
96+
});
97+
}
8398
} // ... end if fundAccount
8499

85100
// 4. also fund the ledger
@@ -120,21 +135,23 @@ export async function createTestAccount(
120135
});
121136
}
122137

123-
// Create PKP auth context
124-
person.pkpAuthContext = await testEnv.authManager.createPkpAuthContext({
125-
authData: personAccountAuthData,
126-
pkpPublicKey: person.pkp.pubkey,
127-
authConfig: {
128-
resources: [
129-
['pkp-signing', '*'],
130-
['lit-action-execution', '*'],
131-
['access-control-condition-decryption', '*'],
132-
],
133-
// 30m expiration
134-
expiration: new Date(Date.now() + 1000 * 60 * 30).toISOString(),
135-
},
136-
litClient: testEnv.litClient,
137-
});
138+
// -- Create PKP auth context
139+
if (opts.hasPKPAuthContext) {
140+
person.pkpAuthContext = await testEnv.authManager.createPkpAuthContext({
141+
authData: personAccountAuthData,
142+
pkpPublicKey: person.pkp.pubkey,
143+
authConfig: {
144+
resources: [
145+
['pkp-signing', '*'],
146+
['lit-action-execution', '*'],
147+
['access-control-condition-decryption', '*'],
148+
],
149+
// 30m expiration
150+
expiration: new Date(Date.now() + 1000 * 60 * 30).toISOString(),
151+
},
152+
litClient: testEnv.litClient,
153+
});
154+
}
138155

139156
// Create PKP viem account
140157
person.pkpViemAccount = await testEnv.litClient.getPkpViemAccount({
@@ -146,7 +163,7 @@ export async function createTestAccount(
146163

147164
if (opts.sponsor) {
148165
// 1. get payment manager
149-
const personPaymentManager = await testEnv.litClient.getPaymentManager({
166+
person.paymentManager = await testEnv.litClient.getPaymentManager({
150167
account: person.account,
151168
});
152169

@@ -162,7 +179,7 @@ export async function createTestAccount(
162179
// periodSeconds: opts.sponsor.restrictions.periodSeconds,
163180
// });
164181
try {
165-
const tx = await personPaymentManager.setRestriction({
182+
const tx = await person.paymentManager.setRestriction({
166183
// totalMaxPrice: wei.toString(),
167184
totalMaxPrice: opts.sponsor.restrictions.totalMaxPriceInWei,
168185
requestsPerPeriod: opts.sponsor.restrictions.requestsPerPeriod,
@@ -183,7 +200,7 @@ export async function createTestAccount(
183200

184201
try {
185202
console.log(`- Sponsoring users:`, userAddresses);
186-
const tx = await personPaymentManager.delegatePaymentsBatch({
203+
const tx = await person.paymentManager.delegatePaymentsBatch({
187204
userAddresses: userAddresses,
188205
});
189206
console.log(`[delegatePaymentsBatch] TX Hash: ${tx.hash}`);

0 commit comments

Comments
 (0)