From 7169016fc6502d4622d2ebbc986249d523ff9861 Mon Sep 17 00:00:00 2001 From: rodrigopavezi Date: Mon, 16 Dec 2024 16:19:20 -0300 Subject: [PATCH 1/8] feat: enhance getSessionSignatures method with optional parameters - Updated the getSessionSignatures method to accept optional domain and statement parameters for improved flexibility. - Modified the SIWE message creation to incorporate the new parameters. - Enhanced unit tests to cover scenarios with default, undefined, and custom parameters for session signatures. This change improves the usability of the LitProtocolCipherProvider by allowing more customizable session signature requests. --- .../src/lit-protocol-cipher-provider.ts | 10 +- .../lit-protocol-cipher/test/index.test.ts | 138 +++++++++++++++++- 2 files changed, 138 insertions(+), 10 deletions(-) diff --git a/packages/lit-protocol-cipher/src/lit-protocol-cipher-provider.ts b/packages/lit-protocol-cipher/src/lit-protocol-cipher-provider.ts index 2637cd8688..4867da360f 100644 --- a/packages/lit-protocol-cipher/src/lit-protocol-cipher-provider.ts +++ b/packages/lit-protocol-cipher/src/lit-protocol-cipher-provider.ts @@ -111,7 +111,12 @@ export default class LitProtocolCipherProvider implements CipherProviderTypes.IC * @param {string} walletAddress - The wallet address to use for generating the auth sig. * @returns {Promise} */ - public async getSessionSignatures(signer: Signer, walletAddress: string): Promise { + public async getSessionSignatures( + signer: Signer, + walletAddress: string, + domain?: string, + statement?: string, + ): Promise { if (!this.litClient) { throw new Error('Lit client not initialized'); } @@ -134,13 +139,14 @@ export default class LitProtocolCipherProvider implements CipherProviderTypes.IC if (!params.expiration) { throw new Error('expiration is required'); } - if (!params.resourceAbilityRequests) { throw new Error('resourceAbilityRequests is required'); } // Create the SIWE message const toSign = await createSiweMessage({ + domain, + statement, uri: params.uri, expiration: params.expiration, resources: params.resourceAbilityRequests, diff --git a/packages/lit-protocol-cipher/test/index.test.ts b/packages/lit-protocol-cipher/test/index.test.ts index f3cfbb919a..f65ac6cbf7 100644 --- a/packages/lit-protocol-cipher/test/index.test.ts +++ b/packages/lit-protocol-cipher/test/index.test.ts @@ -3,14 +3,25 @@ import { Signer } from 'ethers'; import LitProvider from '../src/lit-protocol-cipher-provider'; import { disconnectWeb3, LitNodeClientNodeJs } from '@lit-protocol/lit-node-client'; import { HttpDataAccess, NodeConnectionConfig } from '@requestnetwork/request-client.js'; -import { generateAuthSig } from '@lit-protocol/auth-helpers'; +import { + generateAuthSig, + createSiweMessage, + LitAccessControlConditionResource, +} from '@lit-protocol/auth-helpers'; import { EncryptionTypes } from '@requestnetwork/types'; -import { createSiweMessageWithRecaps } from '@lit-protocol/auth-helpers'; // Mock dependencies jest.mock('@lit-protocol/lit-node-client'); jest.mock('@requestnetwork/request-client.js'); -jest.mock('@lit-protocol/auth-helpers'); +jest.mock('@lit-protocol/auth-helpers', () => ({ + generateAuthSig: jest.fn(), + createSiweMessage: jest.fn(), + LitAccessControlConditionResource: jest.fn().mockImplementation((path) => ({ + path, + toString: () => path, + toWildcardPath: () => path, + })), +})); jest.mock('@lit-protocol/encryption'); describe('LitProvider', () => { @@ -147,34 +158,145 @@ describe('LitProvider', () => { }); describe('getSessionSignatures', () => { - it('should get session signatures successfully', async () => { + beforeEach(() => { + (LitAccessControlConditionResource as unknown as jest.Mock).mockClear(); + (createSiweMessage as jest.Mock).mockClear(); + + // Mock getSessionSigs to call the authNeededCallback + (mockLitClient.getSessionSigs as jest.Mock).mockImplementation(async (args: unknown) => { + const { authNeededCallback } = args as { authNeededCallback: Function }; + if (authNeededCallback) { + await authNeededCallback({ + uri: 'test://uri', + expiration: '2024-12-31T23:59:59.999Z', + resourceAbilityRequests: [], + }); + } + return { 'mock-session': 'mock-sig' }; + }); + }); + + it('should get session signatures successfully with default params', async () => { const mockAuthSig = { sig: 'mock-auth-sig', address: mockWalletAddress, derivedVia: 'mock', signedMessage: 'mock', }; + const mockDomain = 'localhost'; + const mockStatement = 'Sign in with Ethereum'; + (generateAuthSig as jest.Mock).mockReturnValue(Promise.resolve(mockAuthSig)); - (createSiweMessageWithRecaps as jest.Mock).mockReturnValue( - Promise.resolve('mock-siwe-message'), + (createSiweMessage as jest.Mock).mockReturnValue(Promise.resolve('mock-siwe-message')); + + await litProvider.getSessionSignatures( + mockSigner, + mockWalletAddress, + mockDomain, + mockStatement, ); + expect(mockLitClient.connect).toHaveBeenCalled(); + expect(mockLitClient.getLatestBlockhash).toHaveBeenCalled(); + expect(LitAccessControlConditionResource).toHaveBeenCalledWith('*'); + expect(createSiweMessage).toHaveBeenCalledWith({ + domain: mockDomain, + statement: mockStatement, + uri: 'test://uri', + expiration: '2024-12-31T23:59:59.999Z', + resources: [], + walletAddress: mockWalletAddress, + nonce: 'mock-blockhash', + litNodeClient: mockLitClient, + }); + expect(mockLitClient.getSessionSigs).toHaveBeenCalled(); + }); + + it('should get session signatures successfully with undefined domain and statement', async () => { + const mockAuthSig = { + sig: 'mock-auth-sig', + address: mockWalletAddress, + derivedVia: 'mock', + signedMessage: 'mock', + }; + + (generateAuthSig as jest.Mock).mockReturnValue(Promise.resolve(mockAuthSig)); + (createSiweMessage as jest.Mock).mockReturnValue(Promise.resolve('mock-siwe-message')); + await litProvider.getSessionSignatures(mockSigner, mockWalletAddress); expect(mockLitClient.connect).toHaveBeenCalled(); expect(mockLitClient.getLatestBlockhash).toHaveBeenCalled(); + expect(createSiweMessage).toHaveBeenCalledWith({ + domain: undefined, + statement: undefined, + uri: 'test://uri', + expiration: '2024-12-31T23:59:59.999Z', + resources: [], + walletAddress: mockWalletAddress, + nonce: 'mock-blockhash', + litNodeClient: mockLitClient, + }); + expect(mockLitClient.getSessionSigs).toHaveBeenCalled(); + }); + + it('should get session signatures successfully with all custom params', async () => { + const mockAuthSig = { + sig: 'mock-auth-sig', + address: mockWalletAddress, + derivedVia: 'mock', + signedMessage: 'mock', + }; + const mockDomain = 'custom.domain'; + const mockStatement = 'Custom statement for signing'; + + (generateAuthSig as jest.Mock).mockReturnValue(Promise.resolve(mockAuthSig)); + (createSiweMessage as jest.Mock).mockReturnValue(Promise.resolve('mock-siwe-message')); + + await litProvider.getSessionSignatures( + mockSigner, + mockWalletAddress, + mockDomain, + mockStatement, + ); + + expect(mockLitClient.connect).toHaveBeenCalled(); + expect(mockLitClient.getLatestBlockhash).toHaveBeenCalled(); + expect(createSiweMessage).toHaveBeenCalledWith({ + domain: mockDomain, + statement: mockStatement, + uri: expect.any(String), + expiration: expect.any(String), + resources: expect.any(Array), + walletAddress: mockWalletAddress, + nonce: 'mock-blockhash', + litNodeClient: mockLitClient, + }); expect(mockLitClient.getSessionSigs).toHaveBeenCalled(); }); it('should not get new signatures if they already exist', async () => { + const mockDomain = 'localhost'; + const mockStatement = 'Sign in with Ethereum'; + // Set session signatures - await litProvider.getSessionSignatures(mockSigner, mockWalletAddress); + await litProvider.getSessionSignatures( + mockSigner, + mockWalletAddress, + mockDomain, + mockStatement, + ); // Reset mocks jest.clearAllMocks(); // Call again, should not call Lit SDK methods - await litProvider.getSessionSignatures(mockSigner, mockWalletAddress); + await litProvider.getSessionSignatures( + mockSigner, + mockWalletAddress, + mockDomain, + mockStatement, + ); expect(mockLitClient.connect).not.toHaveBeenCalled(); expect(mockLitClient.getLatestBlockhash).not.toHaveBeenCalled(); From 26ff8ccd12fab6112421a15f93e9fa2a023e2c04 Mon Sep 17 00:00:00 2001 From: Rodrigo Serviuc Pavezi Date: Mon, 16 Dec 2024 17:34:50 -0300 Subject: [PATCH 2/8] Update packages/lit-protocol-cipher/test/index.test.ts Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> --- packages/lit-protocol-cipher/test/index.test.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/packages/lit-protocol-cipher/test/index.test.ts b/packages/lit-protocol-cipher/test/index.test.ts index f65ac6cbf7..e34aef405e 100644 --- a/packages/lit-protocol-cipher/test/index.test.ts +++ b/packages/lit-protocol-cipher/test/index.test.ts @@ -164,7 +164,13 @@ describe('LitProvider', () => { // Mock getSessionSigs to call the authNeededCallback (mockLitClient.getSessionSigs as jest.Mock).mockImplementation(async (args: unknown) => { - const { authNeededCallback } = args as { authNeededCallback: Function }; + const { authNeededCallback } = args as { + authNeededCallback: (params: { + uri: string; + expiration: string; + resourceAbilityRequests: unknown[] + }) => Promise + }; if (authNeededCallback) { await authNeededCallback({ uri: 'test://uri', From 14989118ab68fd631bbc3a0bc19aea95a83de4e5 Mon Sep 17 00:00:00 2001 From: rodrigopavezi Date: Tue, 17 Dec 2024 07:54:30 -0300 Subject: [PATCH 3/8] feat: update getSessionSignatures method to include domain and statement parameters - Modified the getSessionSignatures method to accept additional parameters: domain and statement, enhancing its functionality for generating session signatures. - Updated the method description to reflect the changes in parameters. This update improves the flexibility and usability of the LitProtocolCipherProvider. --- .../lit-protocol-cipher/src/lit-protocol-cipher-provider.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/lit-protocol-cipher/src/lit-protocol-cipher-provider.ts b/packages/lit-protocol-cipher/src/lit-protocol-cipher-provider.ts index 4867da360f..4aaf2b6e9f 100644 --- a/packages/lit-protocol-cipher/src/lit-protocol-cipher-provider.ts +++ b/packages/lit-protocol-cipher/src/lit-protocol-cipher-provider.ts @@ -106,9 +106,11 @@ export default class LitProtocolCipherProvider implements CipherProviderTypes.IC /** * @async * @function getSessionSignatures - * @description Gets the session signatures required for encryption and decryption. + * @description Gets the session signatures required for decryption. * @param {any} signer - The signer object to use for generating the auth sig. * @param {string} walletAddress - The wallet address to use for generating the auth sig. + * @param {string} domain - The domain to use for generating the auth sig. + * @param {string} statement - The statement to use for generating the auth sig. * @returns {Promise} */ public async getSessionSignatures( From 07914ee0fd3e67d372ca14cfb250ab040cbcba1c Mon Sep 17 00:00:00 2001 From: rodrigopavezi Date: Tue, 17 Dec 2024 07:56:03 -0300 Subject: [PATCH 4/8] fix: format --- packages/lit-protocol-cipher/test/index.test.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/packages/lit-protocol-cipher/test/index.test.ts b/packages/lit-protocol-cipher/test/index.test.ts index e34aef405e..c12306fb86 100644 --- a/packages/lit-protocol-cipher/test/index.test.ts +++ b/packages/lit-protocol-cipher/test/index.test.ts @@ -164,12 +164,12 @@ describe('LitProvider', () => { // Mock getSessionSigs to call the authNeededCallback (mockLitClient.getSessionSigs as jest.Mock).mockImplementation(async (args: unknown) => { - const { authNeededCallback } = args as { - authNeededCallback: (params: { - uri: string; - expiration: string; - resourceAbilityRequests: unknown[] - }) => Promise + const { authNeededCallback } = args as { + authNeededCallback: (params: { + uri: string; + expiration: string; + resourceAbilityRequests: unknown[]; + }) => Promise; }; if (authNeededCallback) { await authNeededCallback({ From 55bdeb4efde5a42859e36f37880fe29c996da7e3 Mon Sep 17 00:00:00 2001 From: rodrigopavezi Date: Tue, 17 Dec 2024 10:05:08 -0300 Subject: [PATCH 5/8] chore: update lerna configuration to specify npm registry for publishing - Added a registry URL to the publish command in the lerna.json file, ensuring that packages are published to the correct npm registry. - This change enhances the configuration for package management and publishing workflows. --- lerna.json | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lerna.json b/lerna.json index b5c29e2395..6ad347ce35 100644 --- a/lerna.json +++ b/lerna.json @@ -3,5 +3,10 @@ "useWorkspaces": true, "packages": ["packages/*"], "version": "independent", - "npmClient": "yarn" + "npmClient": "yarn", + "command": { + "publish": { + "registry": "https://registry.npmjs.org/" + } + } } From 008e47a76ecbee4d48345c9e2cf28bc51cf96303 Mon Sep 17 00:00:00 2001 From: rodrigopavezi Date: Tue, 17 Dec 2024 10:05:51 -0300 Subject: [PATCH 6/8] chore: remove independent versioning from lerna configuration - Removed the "version" field set to "independent" in the lerna.json file, streamlining the versioning strategy for the project. - This change aligns the project with a more consistent versioning approach, simplifying package management. --- lerna.json | 1 - 1 file changed, 1 deletion(-) diff --git a/lerna.json b/lerna.json index 6ad347ce35..7cb7feb0e1 100644 --- a/lerna.json +++ b/lerna.json @@ -2,7 +2,6 @@ "lerna": "6.6.2", "useWorkspaces": true, "packages": ["packages/*"], - "version": "independent", "npmClient": "yarn", "command": { "publish": { From 6ffcb070ed49490ee71662ea927bb1c79ebf1480 Mon Sep 17 00:00:00 2001 From: rodrigopavezi Date: Tue, 17 Dec 2024 10:06:44 -0300 Subject: [PATCH 7/8] chore: set versioning strategy to independent in lerna configuration - Added the "version" field set to "independent" in the lerna.json file, allowing for independent versioning of packages. - This change enhances flexibility in managing package versions across the project. --- lerna.json | 1 + 1 file changed, 1 insertion(+) diff --git a/lerna.json b/lerna.json index 7cb7feb0e1..6ad347ce35 100644 --- a/lerna.json +++ b/lerna.json @@ -2,6 +2,7 @@ "lerna": "6.6.2", "useWorkspaces": true, "packages": ["packages/*"], + "version": "independent", "npmClient": "yarn", "command": { "publish": { From af3dbdd5890279712bf6935e3e500dbac335471c Mon Sep 17 00:00:00 2001 From: rodrigopavezi Date: Tue, 17 Dec 2024 11:30:25 -0300 Subject: [PATCH 8/8] fix: revert changes --- lerna.json | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/lerna.json b/lerna.json index 6ad347ce35..b5c29e2395 100644 --- a/lerna.json +++ b/lerna.json @@ -3,10 +3,5 @@ "useWorkspaces": true, "packages": ["packages/*"], "version": "independent", - "npmClient": "yarn", - "command": { - "publish": { - "registry": "https://registry.npmjs.org/" - } - } + "npmClient": "yarn" }