Skip to content

Commit f6152f6

Browse files
committed
feat(auth): add test
1 parent c5bc50e commit f6152f6

File tree

1 file changed

+41
-16
lines changed

1 file changed

+41
-16
lines changed
Lines changed: 41 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
import { createPkpSignTest } from '../helper/tests';
21
import { initFast } from '../init';
32
import {
3+
createAuthManager,
44
generateSessionKeyPair,
5+
storagePlugins,
56
validateDelegationAuthSig,
67
} from '@lit-protocol/auth';
8+
import { createLitClient } from '@lit-protocol/lit-client';
9+
import { nagaDev } from '@lit-protocol/networks';
710

811
describe('PKP Auth with Pre-generated Materials', () => {
912
let ctx: Awaited<ReturnType<typeof initFast>>;
@@ -13,11 +16,9 @@ describe('PKP Auth with Pre-generated Materials', () => {
1316
});
1417

1518
test('Try to pregen', async () => {
16-
// Step 1: Generate a session key pair directly
19+
// CLIENT SIDE: generate session materials and delegation
1720
const sessionKeyPair = generateSessionKeyPair();
18-
console.log('Session Key Pair:', sessionKeyPair);
1921

20-
// Step 2: Generate PKP delegation signature for the session key pair
2122
const delegationAuthSig =
2223
await ctx.authManager.generatePkpDelegationAuthSig({
2324
pkpPublicKey: ctx.aliceViemAccountPkp.pubkey,
@@ -34,30 +35,54 @@ describe('PKP Auth with Pre-generated Materials', () => {
3435
litClient: ctx.litClient,
3536
});
3637

37-
console.log('delegationAuthSig:', delegationAuthSig);
38+
const serializedPayload = JSON.stringify({
39+
sessionKeyPair,
40+
delegationAuthSig,
41+
});
42+
43+
console.log('Serialized Payload:', serializedPayload);
44+
45+
// SERVER SIDE: receive over the wire
46+
const {
47+
sessionKeyPair: receivedSessionKeyPair,
48+
delegationAuthSig: receivedDelegationAuthSig,
49+
} = JSON.parse(serializedPayload) as {
50+
sessionKeyPair: typeof sessionKeyPair;
51+
delegationAuthSig: typeof delegationAuthSig;
52+
};
3853

39-
// (Optional) Internally, we also run the `validateDelegationAuthSig` function to ensure the signature is valid before proceeding. However, we can also use this externally in case you want to validate a signature yourself.
4054
validateDelegationAuthSig({
41-
delegationAuthSig,
42-
sessionKeyUri: sessionKeyPair.publicKey,
55+
delegationAuthSig: receivedDelegationAuthSig,
56+
sessionKeyUri: receivedSessionKeyPair.publicKey,
57+
});
58+
59+
const serverAuthManager = createAuthManager({
60+
storage: storagePlugins.localStorageNode({
61+
appName: 'e2e-pre-generated',
62+
networkName: 'naga-dev',
63+
storagePath: './.e2e/pre-generated-storage',
64+
}),
4365
});
4466

45-
// Step 3: Create auth context using the pre-generated materials
4667
const authContextWithPreGenerated =
47-
await ctx.authManager.createPkpAuthContextFromPreGenerated({
68+
await serverAuthManager.createPkpAuthContextFromPreGenerated({
4869
pkpPublicKey: ctx.aliceViemAccountPkp.pubkey,
49-
sessionKeyPair,
50-
delegationAuthSig,
70+
sessionKeyPair: receivedSessionKeyPair,
71+
delegationAuthSig: receivedDelegationAuthSig,
5172
});
5273

53-
console.log('authContextWithPreGenerated:', authContextWithPreGenerated);
54-
55-
const res = await ctx.litClient.chain.ethereum.pkpSign({
74+
const litClient = await createLitClient({ network: nagaDev });
75+
const res = await litClient.chain.ethereum.pkpSign({
5676
authContext: authContextWithPreGenerated,
5777
pubKey: ctx.aliceViemAccountPkp.pubkey,
5878
toSign: 'Hello, world!',
5979
});
6080

61-
console.log('res:', res);
81+
expect(res).toBeTruthy();
82+
83+
console.log(
84+
'ctx.aliceViemAccountPkp.pubkey:',
85+
ctx.aliceViemAccountPkp.pubkey
86+
);
6287
});
6388
});

0 commit comments

Comments
 (0)