1- import { createAuthManager , storagePlugins } from "@lit-protocol/auth" ;
2- import { createLitClient } from "@lit-protocol/lit-client" ;
3- import { z } from "zod" ;
4- import * as StateManager from "../StateManager" ;
5- import * as NetworkManager from "../../../src/helper/NetworkManager" ;
6- import * as AccountManager from "../AccountManager" ;
1+ import { createAuthManager , storagePlugins } from '@lit-protocol/auth' ;
2+ import { createLitClient } from '@lit-protocol/lit-client' ;
3+ import { z } from 'zod' ;
4+ import * as StateManager from '../StateManager' ;
5+ import * as NetworkManager from '../../../src/helper/NetworkManager' ;
6+ import * as AccountManager from '../AccountManager' ;
7+ import { createAccBuilder } from '@lit-protocol/access-control-conditions' ;
78
89// PKP Sign Result Schema
910const PkpSignResultSchema = z . object ( {
10- signature : z . string ( ) . regex ( / ^ 0 x [ a - f A - F 0 - 9 ] + $ / , "Invalid hex signature" ) ,
11- verifyingKey : z . string ( ) . regex ( / ^ 0 x [ a - f A - F 0 - 9 ] + $ / , "Invalid hex verifying key" ) ,
12- signedData : z . string ( ) . regex ( / ^ 0 x [ a - f A - F 0 - 9 ] + $ / , "Invalid hex signed data" ) ,
13- recoveryId : z . number ( ) . int ( ) . min ( 0 ) . max ( 3 , "Recovery ID must be 0-3" ) ,
14- publicKey : z . string ( ) . regex ( / ^ 0 x [ a - f A - F 0 - 9 ] + $ / , "Invalid hex public key" ) ,
15- sigType : z . string ( ) . min ( 1 , "Signature type cannot be empty" ) ,
11+ signature : z . string ( ) . regex ( / ^ 0 x [ a - f A - F 0 - 9 ] + $ / , 'Invalid hex signature' ) ,
12+ verifyingKey : z
13+ . string ( )
14+ . regex ( / ^ 0 x [ a - f A - F 0 - 9 ] + $ / , 'Invalid hex verifying key' ) ,
15+ signedData : z . string ( ) . regex ( / ^ 0 x [ a - f A - F 0 - 9 ] + $ / , 'Invalid hex signed data' ) ,
16+ recoveryId : z . number ( ) . int ( ) . min ( 0 ) . max ( 3 , 'Recovery ID must be 0-3' ) ,
17+ publicKey : z . string ( ) . regex ( / ^ 0 x [ a - f A - F 0 - 9 ] + $ / , 'Invalid hex public key' ) ,
18+ sigType : z . string ( ) . min ( 1 , 'Signature type cannot be empty' ) ,
1619} ) ;
1720
1821// Global variables to cache expensive operations
@@ -57,11 +60,15 @@ const createAuthContextFromState = async () => {
5760
5861 // Validate that master account authData and PKP exist
5962 if ( ! state . masterAccount . authData ) {
60- throw new Error ( '❌ Master account authData not found in state. Run init.ts first.' ) ;
63+ throw new Error (
64+ '❌ Master account authData not found in state. Run init.ts first.'
65+ ) ;
6166 }
6267
6368 if ( ! state . masterAccount . pkp ) {
64- throw new Error ( '❌ Master account PKP not found in state. Run init.ts first.' ) ;
69+ throw new Error (
70+ '❌ Master account PKP not found in state. Run init.ts first.'
71+ ) ;
6572 }
6673
6774 // Get the master account from environment (same as init.ts)
@@ -126,12 +133,78 @@ export async function runPkpSignTest() {
126133
127134 // For Artillery, just return - no need to call next()
128135 return ;
136+ } catch ( error ) {
137+ const endTime = Date . now ( ) ;
138+ const duration = endTime - startTime ;
139+
140+ console . error (
141+ `❌ pkpSign failed in ${ duration } ms:` ,
142+ error instanceof Error ? error . message : String ( error )
143+ ) ;
144+
145+ // Throw the error to let Artillery handle it
146+ throw error ;
147+ }
148+ }
129149
150+ // test '/web/encryption/sign/v2' endpoint
151+ export async function runEncryptDecryptTest ( ) {
152+ const startTime = Date . now ( ) ;
153+
154+ try {
155+ // 1. Initialise shared resources (only happens once)
156+ await initialiseSharedResources ( ) ;
157+
158+ // 2. Read state
159+ const state = await StateManager . readFile ( ) ;
160+
161+ // Create auth context
162+ const authContext = await createAuthContextFromState ( ) ;
163+
164+ // Set up access control conditions requiring wallet ownership
165+ const addressToUse = authContext . account . address ;
166+ const builder = createAccBuilder ( ) ;
167+ const accs = builder
168+ . requireWalletOwnership ( addressToUse )
169+ . on ( 'ethereum' )
170+ . build ( ) ;
171+
172+ // Encrypt data with the access control conditions
173+ const dataToEncrypt = 'Hello from PKP encrypt-decrypt test!' ;
174+ const encryptedData = await litClient . encrypt ( {
175+ dataToEncrypt,
176+ unifiedAccessControlConditions : accs ,
177+ chain : 'ethereum' ,
178+ } ) ;
179+
180+ // Decrypt the data using the appropriate auth context
181+ const decryptedData = await litClient . decrypt ( {
182+ data : encryptedData ,
183+ unifiedAccessControlConditions : accs ,
184+ chain : 'ethereum' ,
185+ authContext,
186+ } ) ;
187+
188+ // Assert that the decrypted data is the same as the original data
189+ if ( decryptedData . convertedData !== dataToEncrypt ) {
190+ throw new Error ( '❌ Decrypted data does not match the original data' ) ;
191+ }
192+
193+ const endTime = Date . now ( ) ;
194+ const duration = endTime - startTime ;
195+
196+ console . log ( `✅ encrypt & decrypt successful in ${ duration } ms` ) ;
197+
198+ // For Artillery, just return - no need to call next()
199+ return ;
130200 } catch ( error ) {
131201 const endTime = Date . now ( ) ;
132202 const duration = endTime - startTime ;
133203
134- console . error ( `❌ pkpSign failed in ${ duration } ms:` , error instanceof Error ? error . message : String ( error ) ) ;
204+ console . error (
205+ `❌ encrypt & decrypt failed in ${ duration } ms:` ,
206+ error instanceof Error ? error . message : String ( error )
207+ ) ;
135208
136209 // Throw the error to let Artillery handle it
137210 throw error ;
@@ -140,7 +213,6 @@ export async function runPkpSignTest() {
140213
141214// test '/web/sign_session_key' endpoint
142215export async function runSignSessionKeyTest ( ) {
143-
144216 // ❗️ IT'S IMPORTANT TO SET THIS TO FALSE FOR TESTING
145217 const DELEGATION_AUTH_SIG_CACHE = false ;
146218
@@ -167,18 +239,20 @@ export async function runSignSessionKeyTest() {
167239 litClient : litClient ,
168240 cache : {
169241 delegationAuthSig : DELEGATION_AUTH_SIG_CACHE ,
170- }
242+ } ,
171243 } ) ;
172244
173245 // console.log('✅ Master Account PKP Auth Context:', masterAccountPkpAuthContext);
174-
175246 } catch ( error ) {
176247 const endTime = Date . now ( ) ;
177248 const duration = endTime - startTime ;
178249
179- console . error ( `❌ signSessionKey failed in ${ duration } ms:` , error instanceof Error ? error . message : String ( error ) ) ;
250+ console . error (
251+ `❌ signSessionKey failed in ${ duration } ms:` ,
252+ error instanceof Error ? error . message : String ( error )
253+ ) ;
180254
181255 // Throw the error to let Artillery handle it
182256 throw error ;
183257 }
184- }
258+ }
0 commit comments