|
1 | 1 | import { init } from '../init'; |
2 | | - |
| 2 | +import { generateSessionKeyPair } from '@lit-protocol/auth'; |
3 | 3 | import { hexToBigInt, keccak256, toBytes } from 'viem'; |
4 | 4 |
|
| 5 | +/** |
| 6 | + * Creates a PKP authentication context with pre-generated session materials |
| 7 | + * This simulates a server-side use case where session key pair and delegation |
| 8 | + * signature are generated once and reused for multiple requests |
| 9 | + */ |
| 10 | +export const createPkpAuthContextWithPreGeneratedMaterials = async ( |
| 11 | + ctx: Awaited<ReturnType<typeof init>> |
| 12 | +) => { |
| 13 | + console.log('🔁 Creating PKP Auth Context with Pre-generated Materials'); |
| 14 | + try { |
| 15 | + // Step 1: Generate a session key pair directly |
| 16 | + console.log(' 📝 Step 1: Generating session key pair...'); |
| 17 | + const sessionKeyPair = generateSessionKeyPair(); |
| 18 | + |
| 19 | + // Step 2: Generate PKP delegation signature for the session key pair |
| 20 | + console.log(' 📝 Step 2: Generating PKP delegation signature...'); |
| 21 | + const delegationAuthSig = |
| 22 | + await ctx.authManager.generatePkpDelegationAuthSig({ |
| 23 | + pkpPublicKey: ctx.aliceViemAccountPkp.publicKey, |
| 24 | + authData: ctx.aliceViemAccountAuthData, |
| 25 | + sessionKeyPair, |
| 26 | + authConfig: { |
| 27 | + resources: [ |
| 28 | + ['pkp-signing', '*'], |
| 29 | + ['lit-action-execution', '*'], |
| 30 | + ['access-control-condition-decryption', '*'], |
| 31 | + ], |
| 32 | + expiration: new Date(Date.now() + 1000 * 60 * 15).toISOString(), |
| 33 | + }, |
| 34 | + litClient: ctx.litClient, |
| 35 | + }); |
| 36 | + |
| 37 | + console.log(' 📝 Session materials generated:', { |
| 38 | + hasSessionKeyPair: !!sessionKeyPair, |
| 39 | + hasDelegationAuthSig: !!delegationAuthSig, |
| 40 | + sessionKeyPublicKey: sessionKeyPair?.publicKey?.substring(0, 20) + '...', |
| 41 | + }); |
| 42 | + |
| 43 | + // Step 3: Create auth context using the pre-generated materials |
| 44 | + // Using the dedicated function for pre-generated materials with a clean, minimal signature |
| 45 | + console.log( |
| 46 | + ' 📝 Step 3: Creating auth context with pre-generated materials...' |
| 47 | + ); |
| 48 | + const authContextWithPreGenerated = |
| 49 | + await ctx.authManager.createPkpAuthContextFromPreGenerated({ |
| 50 | + pkpPublicKey: ctx.aliceViemAccountPkp.publicKey, |
| 51 | + sessionKeyPair, |
| 52 | + delegationAuthSig, |
| 53 | + // Optional: can provide authData if needed, otherwise minimal default is used |
| 54 | + authData: ctx.aliceViemAccountAuthData, |
| 55 | + }); |
| 56 | + |
| 57 | + console.log('✅ PKP Auth Context with Pre-generated Materials created'); |
| 58 | + return authContextWithPreGenerated; |
| 59 | + } catch (e) { |
| 60 | + console.error( |
| 61 | + '❌ Error creating PKP Auth Context with Pre-generated Materials', |
| 62 | + e |
| 63 | + ); |
| 64 | + throw e; |
| 65 | + } |
| 66 | +}; |
| 67 | + |
5 | 68 | /** |
6 | 69 | * Creates a PKP authentication context |
7 | 70 | */ |
@@ -78,3 +141,72 @@ export const createCustomAuthContext = async ( |
78 | 141 | throw e; |
79 | 142 | } |
80 | 143 | }; |
| 144 | + |
| 145 | +/** |
| 146 | + * Creates an EOA authentication context with pre-generated session materials |
| 147 | + * This demonstrates how to pre-generate EOA session materials for server-side use |
| 148 | + */ |
| 149 | +export const createEoaAuthContextWithPreGeneratedMaterials = async ( |
| 150 | + ctx: Awaited<ReturnType<typeof init>> |
| 151 | +) => { |
| 152 | + console.log('🔁 Creating EOA Auth Context with Pre-generated Materials'); |
| 153 | + try { |
| 154 | + // Step 1: Generate a session key pair directly |
| 155 | + console.log(' 📝 Step 1: Generating session key pair...'); |
| 156 | + const sessionKeyPair = generateSessionKeyPair(); |
| 157 | + |
| 158 | + // Step 2: Generate EOA delegation signature for the session key pair |
| 159 | + console.log(' 📝 Step 2: Generating EOA delegation signature...'); |
| 160 | + const delegationAuthSig = |
| 161 | + await ctx.authManager.generateEoaDelegationAuthSig({ |
| 162 | + account: ctx.aliceViemAccount, |
| 163 | + sessionKeyPair, |
| 164 | + authConfig: { |
| 165 | + resources: [ |
| 166 | + ['pkp-signing', '*'], |
| 167 | + ['lit-action-execution', '*'], |
| 168 | + ['access-control-condition-decryption', '*'], |
| 169 | + ], |
| 170 | + expiration: new Date(Date.now() + 1000 * 60 * 15).toISOString(), |
| 171 | + }, |
| 172 | + litClient: ctx.litClient, |
| 173 | + }); |
| 174 | + |
| 175 | + console.log(' 📝 EOA session materials generated:', { |
| 176 | + hasSessionKeyPair: !!sessionKeyPair, |
| 177 | + hasDelegationAuthSig: !!delegationAuthSig, |
| 178 | + sessionKeyPublicKey: sessionKeyPair?.publicKey?.substring(0, 20) + '...', |
| 179 | + }); |
| 180 | + |
| 181 | + // Step 3: Create EOA auth context using the pre-generated materials |
| 182 | + console.log( |
| 183 | + ' 📝 Step 3: Creating EOA auth context with pre-generated materials...' |
| 184 | + ); |
| 185 | + const authContextWithPreGenerated = |
| 186 | + await ctx.authManager.createEoaAuthContext({ |
| 187 | + authConfig: { |
| 188 | + resources: [ |
| 189 | + ['pkp-signing', '*'], |
| 190 | + ['lit-action-execution', '*'], |
| 191 | + ['access-control-condition-decryption', '*'], |
| 192 | + ], |
| 193 | + expiration: new Date(Date.now() + 1000 * 60 * 15).toISOString(), |
| 194 | + }, |
| 195 | + config: { |
| 196 | + account: ctx.aliceViemAccount, |
| 197 | + }, |
| 198 | + litClient: ctx.litClient, |
| 199 | + // Note: EOA auth contexts don't currently support pre-generated materials |
| 200 | + // This demonstrates the pattern for when it's implemented |
| 201 | + }); |
| 202 | + |
| 203 | + console.log('✅ EOA Auth Context with Pre-generated Materials created'); |
| 204 | + return authContextWithPreGenerated; |
| 205 | + } catch (e) { |
| 206 | + console.error( |
| 207 | + '❌ Error creating EOA Auth Context with Pre-generated Materials', |
| 208 | + e |
| 209 | + ); |
| 210 | + throw e; |
| 211 | + } |
| 212 | +}; |
0 commit comments