Skip to content

Commit 7b54590

Browse files
authored
Merge pull request #881 from LIT-Protocol/feature/jss-85-bug-fix-custom-auth-e2e-tests
[Bug] Fix False Positive Custom Auth Tests
2 parents 9b94223 + 2bf0d96 commit 7b54590

File tree

13 files changed

+298
-69
lines changed

13 files changed

+298
-69
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,4 +90,5 @@ lit-cache
9090
lit-auth-local
9191
artillery-state.json
9292
artillery-pkp-tokens
93-
lit-auth-artillery
93+
lit-auth-artillery
94+
alice-auth-manager-data

e2e/src/e2e.spec.ts

Lines changed: 39 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,15 @@ describe('all', () => {
2525

2626
// Auth contexts for testing
2727
let alicePkpAuthContext: any;
28-
let aliceCustomAuthContext: any;
28+
let eveCustomAuthContext: any;
2929

3030
beforeAll(async () => {
3131
try {
3232
ctx = await init();
3333

3434
// Create PKP and custom auth contexts using helper functions
3535
// alicePkpAuthContext = await createPkpAuthContext(ctx);
36-
aliceCustomAuthContext = await createCustomAuthContext(ctx);
36+
eveCustomAuthContext = await createCustomAuthContext(ctx);
3737
} catch (e) {
3838
console.error(e);
3939
process.exit(1);
@@ -124,34 +124,51 @@ describe('all', () => {
124124

125125
describe('endpoints', () => {
126126
it('pkpSign', () =>
127-
createPkpSignTest(ctx, () => aliceCustomAuthContext)());
127+
createPkpSignTest(
128+
ctx,
129+
() => eveCustomAuthContext,
130+
ctx.eveViemAccountPkp.pubkey
131+
)());
128132
it('executeJs', () =>
129-
createExecuteJsTest(ctx, () => aliceCustomAuthContext)());
130-
it('viewPKPsByAddress', () =>
131-
createViewPKPsByAddressTest(ctx, () => aliceCustomAuthContext)());
133+
createExecuteJsTest(
134+
ctx,
135+
() => eveCustomAuthContext,
136+
ctx.eveViemAccountPkp.pubkey
137+
)());
138+
it('viewPKPsByAddress', () => createViewPKPsByAddressTest(ctx)());
132139
it('viewPKPsByAuthData', () =>
133-
createViewPKPsByAuthDataTest(ctx, () => aliceCustomAuthContext)());
140+
createViewPKPsByAuthDataTest(ctx, ctx.eveCustomAuthData)());
134141
it('pkpEncryptDecrypt', () =>
135-
createPkpEncryptDecryptTest(ctx, () => aliceCustomAuthContext)());
142+
createPkpEncryptDecryptTest(
143+
ctx,
144+
() => eveCustomAuthContext,
145+
ctx.eveViemAccountPkp.ethAddress
146+
)());
136147
it('encryptDecryptFlow', () =>
137-
createEncryptDecryptFlowTest(ctx, () => aliceCustomAuthContext)());
138-
it('pkpPermissionsManagerFlow', () =>
139-
createPkpPermissionsManagerFlowTest(
148+
createEncryptDecryptFlowTest(
140149
ctx,
141-
() => aliceCustomAuthContext
150+
() => eveCustomAuthContext,
151+
ctx.eveViemAccountPkp.pubkey
142152
)());
143-
});
144153

145-
describe('integrations', () => {
146-
describe('pkp viem account', () => {
147-
it('sign message', () =>
148-
createViemSignMessageTest(ctx, () => aliceCustomAuthContext)());
149-
it('sign transaction', () =>
150-
createViemSignTransactionTest(ctx, () => aliceCustomAuthContext)());
151-
it('sign typed data', () =>
152-
createViemSignTypedDataTest(ctx, () => aliceCustomAuthContext)());
153-
});
154+
// Disable for now because it requires a different flow
155+
// it('pkpPermissionsManagerFlow', () =>
156+
// createPkpPermissionsManagerFlowTest(
157+
// ctx,
158+
// () => eveCustomAuthContext, ctx.eveViemAccountPkp.pubkey
159+
// )());
154160
});
161+
162+
// describe('integrations', () => {
163+
// describe('pkp viem account', () => {
164+
// it('sign message', () =>
165+
// createViemSignMessageTest(ctx, () => eveCustomAuthContext, ctx.eveViemAccountPkp.pubkey)());
166+
// it('sign transaction', () =>
167+
// createViemSignTransactionTest(ctx, () => eveCustomAuthContext, ctx.eveViemAccountPkp.pubkey)());
168+
// it('sign typed data', () =>
169+
// createViemSignTypedDataTest(ctx, () => eveCustomAuthContext, ctx.eveViemAccountPkp.pubkey)());
170+
// });
171+
// });
155172
});
156173

157174
describe('EOA Native', () => {

e2e/src/helper/auth-contexts.ts

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,12 @@ export const createCustomAuthContext = async (
3939
ctx: Awaited<ReturnType<typeof init>>
4040
) => {
4141
console.log('🔁 Creating Custom Auth Context');
42+
4243
try {
4344
// Set up custom auth method type and validation IPFS CID (from custom-auth-flow example)
44-
const uniqueDappName = 'e2e-test-dapp';
45-
const uniqueAuthMethodType = hexToBigInt(
46-
keccak256(toBytes(uniqueDappName))
47-
);
48-
const uniqueUserId = `${uniqueDappName}-alice`;
49-
const authMethodId = keccak256(toBytes(uniqueUserId));
50-
const validationIpfsCid = 'QmYLeVmwJPVs7Uebk85YdVPivMyrvoeKR6X37kyVRZUXW4';
5145

5246
const customAuthContext = await ctx.authManager.createCustomAuthContext({
53-
pkpPublicKey: ctx.aliceViemAccountPkp.publicKey,
47+
pkpPublicKey: ctx.eveViemAccountPkp.pubkey,
5448
authConfig: {
5549
resources: [
5650
['pkp-signing', '*'],
@@ -61,17 +55,17 @@ export const createCustomAuthContext = async (
6155
},
6256
litClient: ctx.litClient,
6357
customAuthParams: {
64-
litActionIpfsId: validationIpfsCid,
58+
litActionIpfsId: ctx.eveValidationIpfsCid,
6559
jsParams: {
66-
pkpPublicKey: ctx.aliceViemAccountPkp.publicKey,
67-
username: 'alice',
60+
pkpPublicKey: ctx.eveViemAccountPkp.pubkey,
61+
username: 'eve',
6862
password: 'lit',
69-
authMethodId: authMethodId,
63+
authMethodId: ctx.eveCustomAuthData.authMethodId,
7064
},
7165
},
7266
});
7367

74-
console.log('✅ Custom Auth Context created');
68+
console.log('✅ Custom Auth Context created', customAuthContext);
7569
return customAuthContext;
7670
} catch (e) {
7771
console.error('❌ Error creating Custom Auth Context', e);

e2e/src/helper/tests/encrypt-decrypt-flow.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ import { assert } from '../assertions';
33

44
export const createEncryptDecryptFlowTest = (
55
ctx: Awaited<ReturnType<typeof init>>,
6-
getAuthContext: () => any
6+
getAuthContext: () => any,
7+
address?: string
78
) => {
89
return async () => {
910
const { createAccBuilder } = await import(
@@ -17,7 +18,7 @@ export const createEncryptDecryptFlowTest = (
1718
if (authContext === ctx.aliceEoaAuthContext) {
1819
aliceAddress = ctx.aliceViemAccount.address;
1920
} else {
20-
aliceAddress = ctx.aliceViemAccountPkp.ethAddress;
21+
aliceAddress = address || ctx.aliceViemAccountPkp.ethAddress;
2122
}
2223

2324
// Set up access control conditions requiring Bob's wallet ownership

e2e/src/helper/tests/execute-js.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ import { assert } from '../assertions';
33

44
export const createExecuteJsTest = (
55
ctx: Awaited<ReturnType<typeof init>>,
6-
getAuthContext: () => any
6+
getAuthContext: () => any,
7+
pubkey?: string
78
) => {
89
return async () => {
910
const litActionCode = `
@@ -29,7 +30,7 @@ export const createExecuteJsTest = (
2930
message: 'Test message from e2e executeJs',
3031
sigName: 'e2e-test-sig',
3132
toSign: 'Test message from e2e executeJs',
32-
publicKey: ctx.aliceViemAccountPkp.publicKey,
33+
publicKey: pubkey || ctx.aliceViemAccountPkp.publicKey,
3334
},
3435
});
3536

e2e/src/helper/tests/pkp-encrypt-decrypt.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ import { assert } from '../assertions';
33

44
export const createPkpEncryptDecryptTest = (
55
ctx: Awaited<ReturnType<typeof init>>,
6-
getAuthContext: () => any
6+
getAuthContext: () => any,
7+
address?: string
78
) => {
89
return async () => {
910
const { createAccBuilder } = await import(
@@ -19,7 +20,7 @@ export const createPkpEncryptDecryptTest = (
1920
addressToUse = ctx.aliceViemAccount.address;
2021
} else {
2122
// PKP or Custom auth contexts
22-
addressToUse = ctx.aliceViemAccountPkp.ethAddress;
23+
addressToUse = address || ctx.aliceViemAccountPkp.ethAddress;
2324
}
2425

2526
// Set up access control conditions requiring wallet ownership

e2e/src/helper/tests/pkp-sign.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@ import { assert } from '../assertions';
33

44
export const createPkpSignTest = (
55
ctx: Awaited<ReturnType<typeof init>>,
6-
getAuthContext: () => any
6+
getAuthContext: () => any,
7+
pubkey?: string
78
) => {
89
return async () => {
910
const res = await ctx.litClient.chain.ethereum.pkpSign({
1011
authContext: getAuthContext(),
11-
pubKey: ctx.aliceViemAccountPkp.publicKey,
12+
pubKey: pubkey || ctx.aliceViemAccountPkp.publicKey,
1213
toSign: 'Hello, world!',
1314
});
1415

e2e/src/helper/tests/view-pkps-by-address.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ import { init } from '../../init';
22
import { assert } from '../assertions';
33

44
export const createViewPKPsByAddressTest = (
5-
ctx: Awaited<ReturnType<typeof init>>,
6-
getAuthContext: () => any
5+
ctx: Awaited<ReturnType<typeof init>>
76
) => {
87
return async () => {
98
const pkps = await ctx.litClient.viewPKPsByAddress({

e2e/src/helper/tests/view-pkps-by-auth-data.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,18 @@ import { assert } from '../assertions';
33

44
export const createViewPKPsByAuthDataTest = (
55
ctx: Awaited<ReturnType<typeof init>>,
6-
getAuthContext: () => any
6+
authData?: any
77
) => {
88
return async () => {
99
const { ViemAccountAuthenticator } = await import('@lit-protocol/auth');
10-
const authData = await ViemAccountAuthenticator.authenticate(
11-
ctx.aliceViemAccount
12-
);
10+
const _authData =
11+
authData ||
12+
(await ViemAccountAuthenticator.authenticate(ctx.aliceViemAccount));
1313

1414
const pkps = await ctx.litClient.viewPKPsByAuthData({
1515
authData: {
16-
authMethodType: authData.authMethodType,
17-
authMethodId: authData.authMethodId,
16+
authMethodType: _authData.authMethodType,
17+
authMethodId: _authData.authMethodId,
1818
},
1919
pagination: {
2020
limit: 10,

e2e/src/init.ts

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ import {
33
storagePlugins,
44
ViemAccountAuthenticator,
55
} from '@lit-protocol/auth';
6-
import { createLitClient } from '@lit-protocol/lit-client';
7-
import { Account, PrivateKeyAccount } from 'viem';
6+
import { createLitClient, utils as litUtils } from '@lit-protocol/lit-client';
87
import { generatePrivateKey, privateKeyToAccount } from 'viem/accounts';
98
import { z } from 'zod';
109
import { fundAccount } from './helper/fundAccount';
1110
import { getOrCreatePkp } from './helper/pkp-utils';
11+
1212
// import { createPkpAuthContext } from './helper/auth-contexts';
1313

1414
const SupportedNetworkSchema = z.enum([
@@ -17,6 +17,7 @@ const SupportedNetworkSchema = z.enum([
1717
'naga-local',
1818
'naga-staging',
1919
]);
20+
2021
type SupportedNetwork = z.infer<typeof SupportedNetworkSchema>;
2122

2223
const LogLevelSchema = z.enum(['silent', 'info', 'debug']);
@@ -27,6 +28,9 @@ const LIVE_NETWORK_FUNDING_AMOUNT = '0.01';
2728
const LOCAL_NETWORK_FUNDING_AMOUNT = '1';
2829
const LIVE_NETWORK_LEDGER_DEPOSIT_AMOUNT = '2';
2930

31+
const EVE_VALIDATION_IPFS_CID =
32+
'QmcxWmo3jefFsPUnskJXYBwsJYtiFuMAH1nDQEs99AwzDe';
33+
3034
export const init = async (
3135
network?: SupportedNetwork,
3236
logLevel?: LogLevel
@@ -42,6 +46,10 @@ export const init = async (
4246
bobViemAccountPkp: any;
4347
aliceEoaAuthContext: any;
4448
alicePkpAuthContext: any;
49+
eveViemAccount: any;
50+
eveCustomAuthData: Awaited<ReturnType<typeof litUtils.generateAuthData>>;
51+
eveViemAccountPkp: Awaited<ReturnType<typeof litClient.mintWithCustomAuth>>['pkpData']['data'];
52+
eveValidationIpfsCid: string;
4553
masterDepositForUser: (userAddress: string) => Promise<void>;
4654
// alicePkpViemAccountPermissionsManager: any,
4755
}> => {
@@ -66,6 +74,8 @@ export const init = async (
6674
bobViemAccount
6775
);
6876

77+
const eveViemAccount = privateKeyToAccount(generatePrivateKey());
78+
6979
/**
7080
* ====================================
7181
* Environment settings
@@ -129,6 +139,11 @@ export const init = async (
129139
thenFundWith: fundingAmount,
130140
});
131141

142+
await fundAccount(eveViemAccount, masterAccount, _networkModule, {
143+
ifLessThan: fundingAmount,
144+
thenFundWith: fundingAmount,
145+
});
146+
132147
/**
133148
* ====================================
134149
* Initialise the LitClient
@@ -184,6 +199,27 @@ export const init = async (
184199
_network
185200
),
186201
]);
202+
// Use custom auth to create a PKP for Eve
203+
const uniqueDappName = 'e2e-test-dapp';
204+
205+
const authMethodConfig = litUtils.generateUniqueAuthMethodType({
206+
uniqueDappName: uniqueDappName,
207+
});
208+
209+
const eveCustomAuthData = litUtils.generateAuthData({
210+
uniqueDappName: uniqueDappName,
211+
uniqueAuthMethodType: authMethodConfig.bigint,
212+
userId: 'eve',
213+
});
214+
215+
const { pkpData } = await litClient.mintWithCustomAuth({
216+
account: eveViemAccount,
217+
authData: eveCustomAuthData,
218+
scope: 'sign-anything',
219+
validationIpfsCid: EVE_VALIDATION_IPFS_CID,
220+
});
221+
222+
const eveViemAccountPkp = pkpData.data;
187223

188224
/**
189225
* ====================================
@@ -270,6 +306,12 @@ export const init = async (
270306
// Deposit to the Bob PKP Ledger
271307
await masterDepositForUser(bobViemAccountPkp.ethAddress);
272308

309+
// Deposit to the Eve EOA Ledger
310+
await masterDepositForUser(eveViemAccount.address);
311+
312+
// Deposit to the Eve PKP Ledger
313+
await masterDepositForUser(eveViemAccountPkp.ethAddress);
314+
273315
// const alicePkpViemAccountPermissionsManager = await litClient.getPKPPermissionsManager({
274316
// pkpIdentifier: {
275317
// tokenId: aliceViemAccountPkp.tokenId,
@@ -292,6 +334,10 @@ export const init = async (
292334
bobViemAccount,
293335
bobViemAccountAuthData,
294336
bobViemAccountPkp,
337+
eveViemAccount,
338+
eveCustomAuthData,
339+
eveViemAccountPkp,
340+
eveValidationIpfsCid: EVE_VALIDATION_IPFS_CID,
295341
aliceEoaAuthContext,
296342
alicePkpAuthContext,
297343
masterDepositForUser,

0 commit comments

Comments
 (0)