1+ import { getEoaSessionSigs } from "local-tests/setup/session-sigs/get-eoa-session-sigs" ;
2+ import { getPkpSessionSigs } from "local-tests/setup/session-sigs/get-pkp-session-sigs" ;
3+ import { TinnyEnvironment } from "local-tests/setup/tinny-environment"
4+ import { TinnyPerson } from "local-tests/setup/tinny-person" ;
5+ import { LIT_ABILITY } from '@lit-protocol/constants' ;
6+ import { ILitNodeClient } from '@lit-protocol/types' ;
7+ import { AccessControlConditions } from 'local-tests/setup/accs/accs' ;
8+ import { LitAccessControlConditionResource } from '@lit-protocol/auth-helpers' ;
9+ import { encryptString , decryptToString } from '@lit-protocol/encryption' ;
10+
11+ export class DatilHealthManager {
12+
13+ env : TinnyEnvironment ;
14+ alice : TinnyPerson ;
15+ eoaSessionSigs : any ;
16+
17+ constructor ( ) {
18+ this . env = new TinnyEnvironment ( ) ;
19+ }
20+
21+ async init ( ) {
22+ await this . env . init ( ) ;
23+ }
24+
25+ // ========== Person Creation ==========
26+ // create a person
27+ // this action contains chain & rpc interactions
28+ // best to cache it, but for the time being, we will create a new person for each test, since we are only running this test
29+ // once in every 30 minutes.
30+ async initPerson ( ) {
31+ this . alice = await this . env . createNewPerson ( "Alice" ) ;
32+ this . eoaSessionSigs = await getEoaSessionSigs ( this . env , this . alice ) ;
33+ }
34+
35+ validatePrerequisites ( ) {
36+ if ( ! this . alice ) {
37+ throw new Error ( "❌ Person not initialized" ) ;
38+ }
39+ if ( ! this . eoaSessionSigs ) {
40+ throw new Error ( "❌ EOA Session Sigs not initialized" ) ;
41+ }
42+ }
43+
44+
45+ // ========== Endpoint Tests ==========
46+ handshakeTest = async ( ) => {
47+ try {
48+ await this . env . setupLitNodeClient ( ) ;
49+ } catch ( e ) {
50+ console . error ( "❌ Failed to setup Lit Node Client" ) ;
51+ throw e ;
52+ }
53+ }
54+
55+ pkpSignTest = async ( ) => {
56+ this . validatePrerequisites ( ) ;
57+ try {
58+ await this . env . litNodeClient . pkpSign ( {
59+ toSign : this . alice . loveLetter ,
60+ pubKey : this . alice . pkp . publicKey ,
61+ sessionSigs : this . eoaSessionSigs ,
62+ } )
63+ } catch ( e ) {
64+ console . error ( "❌ Failed to run pkpSign" ) ;
65+ throw e ;
66+ }
67+ }
68+
69+ signSessionKeyTest = async ( ) => {
70+ this . validatePrerequisites ( ) ;
71+ try {
72+ await getPkpSessionSigs ( this . env , this . alice ) ;
73+ } catch ( e ) {
74+ console . error ( "❌ Failed to run signSessionKey" ) ;
75+ throw e ;
76+ }
77+ }
78+
79+ executeJsTest = async ( ) => {
80+ this . validatePrerequisites ( ) ;
81+ try {
82+ await this . env . litNodeClient . executeJs ( {
83+ sessionSigs : this . eoaSessionSigs ,
84+ code : `(async () => {
85+ const sigShare = await LitActions.signEcdsa({
86+ toSign: dataToSign,
87+ publicKey,
88+ sigName: "sig",
89+ });
90+ })();` ,
91+ jsParams : {
92+ dataToSign : this . alice . loveLetter ,
93+ publicKey : this . alice . pkp . publicKey ,
94+ }
95+ } )
96+ } catch ( e ) {
97+ console . error ( "❌ Failed to run executeJs" ) ;
98+ throw e ;
99+ }
100+ }
101+
102+ decryptTest = async ( ) => {
103+ this . validatePrerequisites ( ) ;
104+ try {
105+ // Set access control conditions for encrypting and decrypting
106+ const accs = AccessControlConditions . getEmvBasicAccessControlConditions ( {
107+ userAddress : this . alice . wallet . address ,
108+ } ) ;
109+
110+ // First encrypt some test data
111+ const encryptRes = await encryptString (
112+ {
113+ accessControlConditions : accs ,
114+ dataToEncrypt : 'Hello world' ,
115+ } ,
116+ this . env . litNodeClient as unknown as ILitNodeClient
117+ ) ;
118+
119+ if ( ! encryptRes . ciphertext ) {
120+ throw new Error ( `Expected "ciphertext" in encryptRes` ) ;
121+ }
122+
123+ if ( ! encryptRes . dataToEncryptHash ) {
124+ throw new Error ( `Expected "dataToEncryptHash" in encryptRes` ) ;
125+ }
126+
127+ // Generate resource string for the encrypted data
128+ const accsResourceString =
129+ await LitAccessControlConditionResource . generateResourceString (
130+ accs ,
131+ encryptRes . dataToEncryptHash
132+ ) ;
133+
134+ // Get session sigs with decryption capability
135+ const eoaSessionSigs = await getEoaSessionSigs ( this . env , this . alice , [
136+ {
137+ resource : new LitAccessControlConditionResource ( accsResourceString ) ,
138+ ability : LIT_ABILITY . AccessControlConditionDecryption ,
139+ } ,
140+ ] ) ;
141+
142+ // Decrypt the encrypted string
143+ const decryptRes = await decryptToString (
144+ {
145+ accessControlConditions : accs ,
146+ ciphertext : encryptRes . ciphertext ,
147+ dataToEncryptHash : encryptRes . dataToEncryptHash ,
148+ sessionSigs : eoaSessionSigs ,
149+ chain : 'ethereum' ,
150+ } ,
151+ this . env . litNodeClient as unknown as ILitNodeClient
152+ ) ;
153+
154+ if ( decryptRes !== 'Hello world' ) {
155+ throw new Error (
156+ `Expected decryptRes to be 'Hello world' but got ${ decryptRes } `
157+ ) ;
158+ }
159+ } catch ( e ) {
160+ console . error ( "❌ Failed to run decrypt" ) ;
161+ throw e ;
162+ }
163+ }
164+ }
0 commit comments