-
Notifications
You must be signed in to change notification settings - Fork 91
feat: refactor lit protocol provider #1512
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
650095e
feat: integrate Lit Protocol with enhanced testing and configuration …
rodrigopavezi 153667e
fix: readme
rodrigopavezi 0a4a75d
Update .circleci/config.yml
rodrigopavezi a64801f
fix: build
rodrigopavezi 7eb71e1
fix: build giving more memory
rodrigopavezi 1b3f85e
fix: memory issu
rodrigopavezi 11a4e86
fix: out of memory issue
rodrigopavezi 0229d47
chore: upgrade lit protocol sdk version
rodrigopavezi b953258
fix: package lint
rodrigopavezi c48b7f6
fix: as per review
rodrigopavezi 14a6aed
Update packages/lit-protocol-cipher/README.md
rodrigopavezi e1d6c49
fix: revert memory change
rodrigopavezi 42c83d5
Update packages/lit-protocol-cipher/src/lit-protocol-cipher-provider.ts
rodrigopavezi 50aca1a
Update README.md
rodrigopavezi e64f15b
fix: rename LitProtocolCipherProvider
rodrigopavezi c73fb44
fix: update lit integration test
rodrigopavezi c216c24
fix: duplicate files
rodrigopavezi 0a481cc
Merge branch 'master' into refactor/lit-protocol-provider
rodrigopavezi e48d2ee
fix: tests
rodrigopavezi af6c986
Merge branch 'refactor/lit-protocol-provider' of https://github.com/R…
rodrigopavezi 8ba492f
Merge branch 'master' into refactor/lit-protocol-provider
rodrigopavezi 605154e
refactor: add lit-protocol-cipher tsconfig and rename provider class
rodrigopavezi 7fb3bae
refactor: enhance encryption parameter validation and condition creation
rodrigopavezi 586467d
Merge branch 'master' into refactor/lit-protocol-provider
rodrigopavezi 5b89fac
chore: update @lit-protocol packages to version 7.0.2
rodrigopavezi c19db75
fix: add crypto module for Node.js compatibility in jest config
rodrigopavezi f7c4825
chore: reorder test script execution in integration-test package.json
rodrigopavezi c3c771a
Update packages/integration-test/test/lit-protocol.test.ts
rodrigopavezi 4975d24
fix: format
rodrigopavezi 4100d89
fix: test timeouts
rodrigopavezi a847618
fix: per review
rodrigopavezi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,220 @@ | ||
| import { EthereumPrivateKeySignatureProvider } from '@requestnetwork/epk-signature'; | ||
| import { LitProtocolCipherProvider } from '@requestnetwork/lit-protocol-cipher'; | ||
| import { RequestNetwork, Types, Utils } from '@requestnetwork/request-client.js'; | ||
| import { ethers } from 'ethers'; | ||
| import { LitNodeClient } from '@lit-protocol/lit-node-client'; | ||
|
|
||
| jest.setTimeout(30000); | ||
| const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms)); | ||
|
|
||
| async function waitForConfirmation(request: any, maxAttempts = 10, delayMs = 1000): Promise<void> { | ||
| let attempts = 0; | ||
| while (attempts < maxAttempts) { | ||
| try { | ||
| const data = await request.getData(); | ||
| if (data.state === Types.RequestLogic.STATE.CREATED) { | ||
| console.log(`Request confirmed with state: ${data.state}`); | ||
| return; | ||
| } | ||
| console.log( | ||
| `Attempt ${attempts + 1}: Request not confirmed yet. Current state: ${data.state}`, | ||
| ); | ||
| } catch (error) { | ||
| console.log(`Attempt ${attempts + 1} failed:`, error); | ||
| } | ||
| await sleep(delayMs); | ||
| attempts++; | ||
| } | ||
| throw new Error(`Request not confirmed after ${maxAttempts} attempts`); | ||
| } | ||
|
|
||
| describe('Lit Protocol Integration Tests', () => { | ||
| let requestNetwork: RequestNetwork; | ||
| let litProvider: LitProtocolCipherProvider; | ||
| let epkSignatureProvider: EthereumPrivateKeySignatureProvider; | ||
| let userWallet: ethers.Wallet; | ||
| let litClient: LitNodeClient; | ||
|
|
||
| const nodeConnectionConfig = { | ||
| baseURL: 'http://localhost:3000', | ||
| headers: { | ||
| 'Content-Type': 'application/json', | ||
| }, | ||
| }; | ||
|
|
||
| beforeAll(async () => { | ||
| // Create wallet | ||
| userWallet = new ethers.Wallet( | ||
rodrigopavezi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| '0x7b595b2bb732edddc4d4fe758ae528c7a748c40f0f6220f4494e214f15c5bfeb', | ||
| ); | ||
rodrigopavezi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| // Initialize signature provider | ||
| epkSignatureProvider = new EthereumPrivateKeySignatureProvider({ | ||
| method: Types.Signature.METHOD.ECDSA, | ||
| privateKey: userWallet.privateKey, | ||
| }); | ||
|
|
||
| // Initialize Lit Protocol client | ||
| litClient = new LitNodeClient({ | ||
| litNetwork: 'datil-dev', | ||
| alertWhenUnauthorized: false, | ||
| debug: false, | ||
| }); | ||
|
|
||
| // Initialize Lit Protocol provider | ||
| litProvider = new LitProtocolCipherProvider(litClient, nodeConnectionConfig); | ||
| await litProvider.initializeClient(); | ||
| await litProvider.enableDecryption(true); | ||
| await litProvider.getSessionSignatures(userWallet, userWallet.address); | ||
|
|
||
| // Initialize Request Network client | ||
| requestNetwork = new RequestNetwork({ | ||
| nodeConnectionConfig, | ||
| signatureProvider: epkSignatureProvider, | ||
| cipherProvider: litProvider, | ||
| }); | ||
| }, 30000); | ||
|
|
||
| afterAll(async () => { | ||
| try { | ||
| // Get all pending promises | ||
| const promises = []; | ||
| if (litProvider) { | ||
| promises.push(litProvider.disconnectClient()); | ||
| promises.push(litProvider.disconnectWallet()); | ||
| } | ||
| if (litClient) { | ||
| promises.push(litClient.disconnect()); | ||
| } | ||
|
|
||
| // Wait for all cleanup operations to complete | ||
| await Promise.all(promises); | ||
| } catch (error) { | ||
| console.error('Cleanup error:', error); | ||
| } | ||
| }); | ||
|
|
||
| it('should encrypt and decrypt data directly', async () => { | ||
| const testData = 'test encryption'; | ||
| const encryptionParams = [ | ||
| { | ||
| key: userWallet.address, | ||
| method: Types.Encryption.METHOD.KMS, | ||
| }, | ||
| ]; | ||
|
|
||
| const encrypted = await litProvider.encrypt(testData, { encryptionParams }); | ||
| expect(encrypted).toBeDefined(); | ||
| expect(encrypted?.ciphertext).toBeDefined(); | ||
| expect(encrypted?.dataToEncryptHash).toBeDefined(); | ||
|
|
||
| const decrypted = await litProvider.decrypt(encrypted!, { encryptionParams }); | ||
| expect(decrypted).toBe(testData); | ||
| }); | ||
|
|
||
| it('should create and encrypt a request', async () => { | ||
| const requestParams = { | ||
| requestInfo: { | ||
| currency: { | ||
| type: Types.RequestLogic.CURRENCY.ETH, | ||
| value: '0x0000000000000000000000000000000000000000', | ||
| network: 'sepolia', | ||
| }, | ||
| expectedAmount: ethers.utils.parseEther('0.1').toString(), | ||
| payee: { | ||
| type: Types.Identity.TYPE.ETHEREUM_ADDRESS, | ||
| value: userWallet.address, | ||
| }, | ||
| payer: { | ||
| type: Types.Identity.TYPE.ETHEREUM_ADDRESS, | ||
| value: '0xb07D2398d2004378cad234DA0EF14f1c94A530e4', | ||
| }, | ||
| timestamp: Utils.getCurrentTimestampInSecond(), | ||
| }, | ||
| paymentNetwork: { | ||
| id: Types.Extension.PAYMENT_NETWORK_ID.ETH_FEE_PROXY_CONTRACT, | ||
| parameters: { | ||
| paymentNetworkName: 'sepolia', | ||
| paymentAddress: userWallet.address, | ||
| feeAddress: '0x0000000000000000000000000000000000000000', | ||
| feeAmount: '0', | ||
| tokenAddress: '0x0000000000000000000000000000000000000000', | ||
| }, | ||
| }, | ||
| contentData: { | ||
| meta: { | ||
| format: 'rnf_invoice', | ||
| version: '0.0.3', | ||
| }, | ||
| creationDate: new Date().toISOString(), | ||
| invoiceNumber: 'INV-2023-001', | ||
| invoiceItems: [ | ||
| { | ||
| name: 'Test Service', | ||
| quantity: 1, | ||
| unitPrice: ethers.utils.parseEther('0.1').toString(), | ||
| discount: '0', | ||
| tax: { | ||
| type: 'percentage', | ||
| amount: '0', | ||
| }, | ||
| currency: 'ETH', | ||
| }, | ||
| ], | ||
| }, | ||
| signer: { | ||
| type: Types.Identity.TYPE.ETHEREUM_ADDRESS, | ||
| value: userWallet.address, | ||
| }, | ||
| }; | ||
|
|
||
| const encryptionParams = [ | ||
| { | ||
| key: userWallet.address, | ||
| method: Types.Encryption.METHOD.KMS, | ||
| }, | ||
| ]; | ||
|
|
||
| const encryptedRequest = await requestNetwork._createEncryptedRequest( | ||
| requestParams as Types.ICreateRequestParameters, | ||
| encryptionParams, | ||
| ); | ||
|
|
||
| await waitForConfirmation(encryptedRequest, 15, 2000); | ||
|
|
||
| const requestData = await encryptedRequest.getData(); | ||
| expect(requestData).toBeDefined(); | ||
| expect([Types.RequestLogic.STATE.CREATED]).toContain(requestData.state); | ||
| }); | ||
|
|
||
| it('should handle encryption errors gracefully', async () => { | ||
| const invalidEncryptionParams = [ | ||
| { | ||
| key: '', | ||
| method: Types.Encryption.METHOD.KMS, | ||
| }, | ||
| ]; | ||
|
|
||
| await expect( | ||
| litProvider.encrypt('test data', { encryptionParams: invalidEncryptionParams }), | ||
| ).rejects.toThrow(/invalid.*key/i); | ||
| }); | ||
|
|
||
| it('should handle decryption errors gracefully', async () => { | ||
| const invalidEncryptedData = { | ||
| ciphertext: 'invalid-ciphertext', | ||
| dataToEncryptHash: 'invalid-hash', | ||
| }; | ||
|
|
||
| await expect( | ||
| litProvider.decrypt(invalidEncryptedData, { | ||
| encryptionParams: [ | ||
| { | ||
| key: userWallet.address, | ||
| method: Types.Encryption.METHOD.KMS, | ||
| }, | ||
| ], | ||
| }), | ||
| ).rejects.toThrow(); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1 @@ | ||
| export { default as LitProtocolProvider } from './lit-protocol-cipher-provider'; | ||
| export { default as LitProtocolCipherProvider } from './lit-protocol-cipher-provider'; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.