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