|
| 1 | +import { EthereumPrivateKeySignatureProvider } from '@requestnetwork/epk-signature'; |
| 2 | +import { LitProtocolCipherProvider } from '@requestnetwork/lit-protocol-cipher'; |
| 3 | +import { RequestNetwork, Types, Utils } from '@requestnetwork/request-client.js'; |
| 4 | +import { ethers } from 'ethers'; |
| 5 | +import { LitNodeClient } from '@lit-protocol/lit-node-client'; |
| 6 | + |
| 7 | +jest.setTimeout(30000); |
| 8 | +const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms)); |
| 9 | + |
| 10 | +async function waitForConfirmation(request: any, maxAttempts = 10, delayMs = 1000): Promise<void> { |
| 11 | + let attempts = 0; |
| 12 | + while (attempts < maxAttempts) { |
| 13 | + try { |
| 14 | + const data = await request.getData(); |
| 15 | + if (data.state === Types.RequestLogic.STATE.CREATED) { |
| 16 | + console.log(`Request confirmed with state: ${data.state}`); |
| 17 | + return; |
| 18 | + } |
| 19 | + console.log( |
| 20 | + `Attempt ${attempts + 1}: Request not confirmed yet. Current state: ${data.state}`, |
| 21 | + ); |
| 22 | + } catch (error) { |
| 23 | + console.log(`Attempt ${attempts + 1} failed:`, error); |
| 24 | + } |
| 25 | + await sleep(delayMs); |
| 26 | + attempts++; |
| 27 | + } |
| 28 | + throw new Error(`Request not confirmed after ${maxAttempts} attempts`); |
| 29 | +} |
| 30 | + |
| 31 | +describe('Lit Protocol Integration Tests', () => { |
| 32 | + let requestNetwork: RequestNetwork; |
| 33 | + let litProvider: LitProtocolCipherProvider; |
| 34 | + let epkSignatureProvider: EthereumPrivateKeySignatureProvider; |
| 35 | + let userWallet: ethers.Wallet; |
| 36 | + let litClient: LitNodeClient; |
| 37 | + |
| 38 | + const nodeConnectionConfig = { |
| 39 | + baseURL: 'http://localhost:3000', |
| 40 | + headers: { |
| 41 | + 'Content-Type': 'application/json', |
| 42 | + }, |
| 43 | + }; |
| 44 | + |
| 45 | + beforeAll(async () => { |
| 46 | + // Create wallet |
| 47 | + userWallet = new ethers.Wallet( |
| 48 | + '0x7b595b2bb732edddc4d4fe758ae528c7a748c40f0f6220f4494e214f15c5bfeb', |
| 49 | + ); |
| 50 | + |
| 51 | + // Initialize signature provider |
| 52 | + epkSignatureProvider = new EthereumPrivateKeySignatureProvider({ |
| 53 | + method: Types.Signature.METHOD.ECDSA, |
| 54 | + privateKey: userWallet.privateKey, |
| 55 | + }); |
| 56 | + |
| 57 | + // Initialize Lit Protocol client |
| 58 | + litClient = new LitNodeClient({ |
| 59 | + litNetwork: 'datil-dev', |
| 60 | + alertWhenUnauthorized: false, |
| 61 | + debug: false, |
| 62 | + }); |
| 63 | + |
| 64 | + // Initialize Lit Protocol provider |
| 65 | + litProvider = new LitProtocolCipherProvider(litClient, nodeConnectionConfig); |
| 66 | + await litProvider.initializeClient(); |
| 67 | + await litProvider.enableDecryption(true); |
| 68 | + await litProvider.getSessionSignatures(userWallet, userWallet.address); |
| 69 | + |
| 70 | + // Initialize Request Network client |
| 71 | + requestNetwork = new RequestNetwork({ |
| 72 | + nodeConnectionConfig, |
| 73 | + signatureProvider: epkSignatureProvider, |
| 74 | + cipherProvider: litProvider, |
| 75 | + }); |
| 76 | + }, 30000); |
| 77 | + |
| 78 | + afterAll(async () => { |
| 79 | + try { |
| 80 | + // Get all pending promises |
| 81 | + const promises = []; |
| 82 | + if (litProvider) { |
| 83 | + promises.push(litProvider.disconnectClient()); |
| 84 | + promises.push(litProvider.disconnectWallet()); |
| 85 | + } |
| 86 | + if (litClient) { |
| 87 | + promises.push(litClient.disconnect()); |
| 88 | + } |
| 89 | + |
| 90 | + // Wait for all cleanup operations to complete |
| 91 | + await Promise.all(promises); |
| 92 | + } catch (error) { |
| 93 | + console.error('Cleanup error:', error); |
| 94 | + } |
| 95 | + }); |
| 96 | + |
| 97 | + it('should encrypt and decrypt data directly', async () => { |
| 98 | + const testData = 'test encryption'; |
| 99 | + const encryptionParams = [ |
| 100 | + { |
| 101 | + key: userWallet.address, |
| 102 | + method: Types.Encryption.METHOD.KMS, |
| 103 | + }, |
| 104 | + ]; |
| 105 | + |
| 106 | + const encrypted = await litProvider.encrypt(testData, { encryptionParams }); |
| 107 | + expect(encrypted).toBeDefined(); |
| 108 | + expect(encrypted?.ciphertext).toBeDefined(); |
| 109 | + expect(encrypted?.dataToEncryptHash).toBeDefined(); |
| 110 | + |
| 111 | + const decrypted = await litProvider.decrypt(encrypted!, { encryptionParams }); |
| 112 | + expect(decrypted).toBe(testData); |
| 113 | + }); |
| 114 | + |
| 115 | + it('should create and encrypt a request', async () => { |
| 116 | + const requestParams = { |
| 117 | + requestInfo: { |
| 118 | + currency: { |
| 119 | + type: Types.RequestLogic.CURRENCY.ETH, |
| 120 | + value: '0x0000000000000000000000000000000000000000', |
| 121 | + network: 'sepolia', |
| 122 | + }, |
| 123 | + expectedAmount: ethers.utils.parseEther('0.1').toString(), |
| 124 | + payee: { |
| 125 | + type: Types.Identity.TYPE.ETHEREUM_ADDRESS, |
| 126 | + value: userWallet.address, |
| 127 | + }, |
| 128 | + payer: { |
| 129 | + type: Types.Identity.TYPE.ETHEREUM_ADDRESS, |
| 130 | + value: '0xb07D2398d2004378cad234DA0EF14f1c94A530e4', |
| 131 | + }, |
| 132 | + timestamp: Utils.getCurrentTimestampInSecond(), |
| 133 | + }, |
| 134 | + paymentNetwork: { |
| 135 | + id: Types.Extension.PAYMENT_NETWORK_ID.ETH_FEE_PROXY_CONTRACT, |
| 136 | + parameters: { |
| 137 | + paymentNetworkName: 'sepolia', |
| 138 | + paymentAddress: userWallet.address, |
| 139 | + feeAddress: '0x0000000000000000000000000000000000000000', |
| 140 | + feeAmount: '0', |
| 141 | + tokenAddress: '0x0000000000000000000000000000000000000000', |
| 142 | + }, |
| 143 | + }, |
| 144 | + contentData: { |
| 145 | + meta: { |
| 146 | + format: 'rnf_invoice', |
| 147 | + version: '0.0.3', |
| 148 | + }, |
| 149 | + creationDate: new Date().toISOString(), |
| 150 | + invoiceNumber: 'INV-2023-001', |
| 151 | + invoiceItems: [ |
| 152 | + { |
| 153 | + name: 'Test Service', |
| 154 | + quantity: 1, |
| 155 | + unitPrice: ethers.utils.parseEther('0.1').toString(), |
| 156 | + discount: '0', |
| 157 | + tax: { |
| 158 | + type: 'percentage', |
| 159 | + amount: '0', |
| 160 | + }, |
| 161 | + currency: 'ETH', |
| 162 | + }, |
| 163 | + ], |
| 164 | + }, |
| 165 | + signer: { |
| 166 | + type: Types.Identity.TYPE.ETHEREUM_ADDRESS, |
| 167 | + value: userWallet.address, |
| 168 | + }, |
| 169 | + }; |
| 170 | + |
| 171 | + const encryptionParams = [ |
| 172 | + { |
| 173 | + key: userWallet.address, |
| 174 | + method: Types.Encryption.METHOD.KMS, |
| 175 | + }, |
| 176 | + ]; |
| 177 | + |
| 178 | + const encryptedRequest = await requestNetwork._createEncryptedRequest( |
| 179 | + requestParams as Types.ICreateRequestParameters, |
| 180 | + encryptionParams, |
| 181 | + ); |
| 182 | + |
| 183 | + await waitForConfirmation(encryptedRequest, 15, 2000); |
| 184 | + |
| 185 | + const requestData = await encryptedRequest.getData(); |
| 186 | + expect(requestData).toBeDefined(); |
| 187 | + expect([Types.RequestLogic.STATE.CREATED]).toContain(requestData.state); |
| 188 | + }); |
| 189 | + |
| 190 | + it('should handle encryption errors gracefully', async () => { |
| 191 | + const invalidEncryptionParams = [ |
| 192 | + { |
| 193 | + key: '', |
| 194 | + method: Types.Encryption.METHOD.KMS, |
| 195 | + }, |
| 196 | + ]; |
| 197 | + |
| 198 | + await expect( |
| 199 | + litProvider.encrypt('test data', { encryptionParams: invalidEncryptionParams }), |
| 200 | + ).rejects.toThrow(/invalid.*key/i); |
| 201 | + }); |
| 202 | + |
| 203 | + it('should handle decryption errors gracefully', async () => { |
| 204 | + const invalidEncryptedData = { |
| 205 | + ciphertext: 'invalid-ciphertext', |
| 206 | + dataToEncryptHash: 'invalid-hash', |
| 207 | + }; |
| 208 | + |
| 209 | + await expect( |
| 210 | + litProvider.decrypt(invalidEncryptedData, { |
| 211 | + encryptionParams: [ |
| 212 | + { |
| 213 | + key: userWallet.address, |
| 214 | + method: Types.Encryption.METHOD.KMS, |
| 215 | + }, |
| 216 | + ], |
| 217 | + }), |
| 218 | + ).rejects.toThrow(); |
| 219 | + }); |
| 220 | +}); |
0 commit comments