Skip to content

Commit 640c186

Browse files
feat: lit session domain and statement (#1536)
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
1 parent 0bfa87a commit 640c186

2 files changed

Lines changed: 147 additions & 11 deletions

File tree

packages/lit-protocol-cipher/src/lit-protocol-cipher-provider.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,12 +106,19 @@ export default class LitProtocolCipherProvider implements CipherProviderTypes.IC
106106
/**
107107
* @async
108108
* @function getSessionSignatures
109-
* @description Gets the session signatures required for encryption and decryption.
109+
* @description Gets the session signatures required for decryption.
110110
* @param {any} signer - The signer object to use for generating the auth sig.
111111
* @param {string} walletAddress - The wallet address to use for generating the auth sig.
112+
* @param {string} domain - The domain to use for generating the auth sig.
113+
* @param {string} statement - The statement to use for generating the auth sig.
112114
* @returns {Promise<void>}
113115
*/
114-
public async getSessionSignatures(signer: Signer, walletAddress: string): Promise<void> {
116+
public async getSessionSignatures(
117+
signer: Signer,
118+
walletAddress: string,
119+
domain?: string,
120+
statement?: string,
121+
): Promise<void> {
115122
if (!this.litClient) {
116123
throw new Error('Lit client not initialized');
117124
}
@@ -134,13 +141,14 @@ export default class LitProtocolCipherProvider implements CipherProviderTypes.IC
134141
if (!params.expiration) {
135142
throw new Error('expiration is required');
136143
}
137-
138144
if (!params.resourceAbilityRequests) {
139145
throw new Error('resourceAbilityRequests is required');
140146
}
141147

142148
// Create the SIWE message
143149
const toSign = await createSiweMessage({
150+
domain,
151+
statement,
144152
uri: params.uri,
145153
expiration: params.expiration,
146154
resources: params.resourceAbilityRequests,

packages/lit-protocol-cipher/test/index.test.ts

Lines changed: 136 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,25 @@ import { Signer } from 'ethers';
33
import LitProvider from '../src/lit-protocol-cipher-provider';
44
import { disconnectWeb3, LitNodeClientNodeJs } from '@lit-protocol/lit-node-client';
55
import { HttpDataAccess, NodeConnectionConfig } from '@requestnetwork/request-client.js';
6-
import { generateAuthSig } from '@lit-protocol/auth-helpers';
6+
import {
7+
generateAuthSig,
8+
createSiweMessage,
9+
LitAccessControlConditionResource,
10+
} from '@lit-protocol/auth-helpers';
711
import { EncryptionTypes } from '@requestnetwork/types';
8-
import { createSiweMessageWithRecaps } from '@lit-protocol/auth-helpers';
912

1013
// Mock dependencies
1114
jest.mock('@lit-protocol/lit-node-client');
1215
jest.mock('@requestnetwork/request-client.js');
13-
jest.mock('@lit-protocol/auth-helpers');
16+
jest.mock('@lit-protocol/auth-helpers', () => ({
17+
generateAuthSig: jest.fn(),
18+
createSiweMessage: jest.fn(),
19+
LitAccessControlConditionResource: jest.fn().mockImplementation((path) => ({
20+
path,
21+
toString: () => path,
22+
toWildcardPath: () => path,
23+
})),
24+
}));
1425
jest.mock('@lit-protocol/encryption');
1526

1627
describe('LitProvider', () => {
@@ -147,34 +158,151 @@ describe('LitProvider', () => {
147158
});
148159

149160
describe('getSessionSignatures', () => {
150-
it('should get session signatures successfully', async () => {
161+
beforeEach(() => {
162+
(LitAccessControlConditionResource as unknown as jest.Mock).mockClear();
163+
(createSiweMessage as jest.Mock).mockClear();
164+
165+
// Mock getSessionSigs to call the authNeededCallback
166+
(mockLitClient.getSessionSigs as jest.Mock).mockImplementation(async (args: unknown) => {
167+
const { authNeededCallback } = args as {
168+
authNeededCallback: (params: {
169+
uri: string;
170+
expiration: string;
171+
resourceAbilityRequests: unknown[];
172+
}) => Promise<unknown>;
173+
};
174+
if (authNeededCallback) {
175+
await authNeededCallback({
176+
uri: 'test://uri',
177+
expiration: '2024-12-31T23:59:59.999Z',
178+
resourceAbilityRequests: [],
179+
});
180+
}
181+
return { 'mock-session': 'mock-sig' };
182+
});
183+
});
184+
185+
it('should get session signatures successfully with default params', async () => {
151186
const mockAuthSig = {
152187
sig: 'mock-auth-sig',
153188
address: mockWalletAddress,
154189
derivedVia: 'mock',
155190
signedMessage: 'mock',
156191
};
192+
const mockDomain = 'localhost';
193+
const mockStatement = 'Sign in with Ethereum';
194+
157195
(generateAuthSig as jest.Mock).mockReturnValue(Promise.resolve(mockAuthSig));
158-
(createSiweMessageWithRecaps as jest.Mock).mockReturnValue(
159-
Promise.resolve('mock-siwe-message'),
196+
(createSiweMessage as jest.Mock).mockReturnValue(Promise.resolve('mock-siwe-message'));
197+
198+
await litProvider.getSessionSignatures(
199+
mockSigner,
200+
mockWalletAddress,
201+
mockDomain,
202+
mockStatement,
160203
);
161204

205+
expect(mockLitClient.connect).toHaveBeenCalled();
206+
expect(mockLitClient.getLatestBlockhash).toHaveBeenCalled();
207+
expect(LitAccessControlConditionResource).toHaveBeenCalledWith('*');
208+
expect(createSiweMessage).toHaveBeenCalledWith({
209+
domain: mockDomain,
210+
statement: mockStatement,
211+
uri: 'test://uri',
212+
expiration: '2024-12-31T23:59:59.999Z',
213+
resources: [],
214+
walletAddress: mockWalletAddress,
215+
nonce: 'mock-blockhash',
216+
litNodeClient: mockLitClient,
217+
});
218+
expect(mockLitClient.getSessionSigs).toHaveBeenCalled();
219+
});
220+
221+
it('should get session signatures successfully with undefined domain and statement', async () => {
222+
const mockAuthSig = {
223+
sig: 'mock-auth-sig',
224+
address: mockWalletAddress,
225+
derivedVia: 'mock',
226+
signedMessage: 'mock',
227+
};
228+
229+
(generateAuthSig as jest.Mock).mockReturnValue(Promise.resolve(mockAuthSig));
230+
(createSiweMessage as jest.Mock).mockReturnValue(Promise.resolve('mock-siwe-message'));
231+
162232
await litProvider.getSessionSignatures(mockSigner, mockWalletAddress);
163233

164234
expect(mockLitClient.connect).toHaveBeenCalled();
165235
expect(mockLitClient.getLatestBlockhash).toHaveBeenCalled();
236+
expect(createSiweMessage).toHaveBeenCalledWith({
237+
domain: undefined,
238+
statement: undefined,
239+
uri: 'test://uri',
240+
expiration: '2024-12-31T23:59:59.999Z',
241+
resources: [],
242+
walletAddress: mockWalletAddress,
243+
nonce: 'mock-blockhash',
244+
litNodeClient: mockLitClient,
245+
});
246+
expect(mockLitClient.getSessionSigs).toHaveBeenCalled();
247+
});
248+
249+
it('should get session signatures successfully with all custom params', async () => {
250+
const mockAuthSig = {
251+
sig: 'mock-auth-sig',
252+
address: mockWalletAddress,
253+
derivedVia: 'mock',
254+
signedMessage: 'mock',
255+
};
256+
const mockDomain = 'custom.domain';
257+
const mockStatement = 'Custom statement for signing';
258+
259+
(generateAuthSig as jest.Mock).mockReturnValue(Promise.resolve(mockAuthSig));
260+
(createSiweMessage as jest.Mock).mockReturnValue(Promise.resolve('mock-siwe-message'));
261+
262+
await litProvider.getSessionSignatures(
263+
mockSigner,
264+
mockWalletAddress,
265+
mockDomain,
266+
mockStatement,
267+
);
268+
269+
expect(mockLitClient.connect).toHaveBeenCalled();
270+
expect(mockLitClient.getLatestBlockhash).toHaveBeenCalled();
271+
expect(createSiweMessage).toHaveBeenCalledWith({
272+
domain: mockDomain,
273+
statement: mockStatement,
274+
uri: expect.any(String),
275+
expiration: expect.any(String),
276+
resources: expect.any(Array),
277+
walletAddress: mockWalletAddress,
278+
nonce: 'mock-blockhash',
279+
litNodeClient: mockLitClient,
280+
});
166281
expect(mockLitClient.getSessionSigs).toHaveBeenCalled();
167282
});
168283

169284
it('should not get new signatures if they already exist', async () => {
285+
const mockDomain = 'localhost';
286+
const mockStatement = 'Sign in with Ethereum';
287+
170288
// Set session signatures
171-
await litProvider.getSessionSignatures(mockSigner, mockWalletAddress);
289+
await litProvider.getSessionSignatures(
290+
mockSigner,
291+
mockWalletAddress,
292+
mockDomain,
293+
mockStatement,
294+
);
172295

173296
// Reset mocks
174297
jest.clearAllMocks();
175298

176299
// Call again, should not call Lit SDK methods
177-
await litProvider.getSessionSignatures(mockSigner, mockWalletAddress);
300+
await litProvider.getSessionSignatures(
301+
mockSigner,
302+
mockWalletAddress,
303+
mockDomain,
304+
mockStatement,
305+
);
178306

179307
expect(mockLitClient.connect).not.toHaveBeenCalled();
180308
expect(mockLitClient.getLatestBlockhash).not.toHaveBeenCalled();

0 commit comments

Comments
 (0)