Skip to content

feat: add domain and statement support to LitProtocolCipherProvider #1569

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

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 18 additions & 10 deletions packages/lit-protocol-cipher/src/lit-protocol-cipher-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,16 @@ export default class LitProtocolCipherProvider implements CipherProviderTypes.IC
*/
private decryptionEnabled = false;

/**
* @property {string|undefined} domain - The domain to use for generating the auth sig.
*/
private domain?: string;

/**
* @property {string|undefined} statement - The statement to use for generating the auth sig.
*/
private statement?: string;

/**
* @constructor
* @param {LitNodeClient|LitNodeClientNodeJs} litClient - An instance of a Lit Protocol client (either client-side or Node.js).
Expand All @@ -59,10 +69,14 @@ export default class LitProtocolCipherProvider implements CipherProviderTypes.IC
litClient: LitNodeClient | LitNodeClientNodeJs,
nodeConnectionConfig: NodeConnectionConfig,
chain = 'ethereum',
domain?: string,
statement?: string,
) {
this.litClient = litClient;
this.chain = chain;
this.dataAccess = new HttpDataAccess({ nodeConnectionConfig });
this.domain = domain;
this.statement = statement;
}

/**
Expand Down Expand Up @@ -109,16 +123,9 @@ export default class LitProtocolCipherProvider implements CipherProviderTypes.IC
* @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<void>}
*/
public async getSessionSignatures(
signer: Signer,
walletAddress: string,
domain?: string,
statement?: string,
): Promise<void> {
public async getSessionSignatures(signer: Signer, walletAddress: string): Promise<void> {
if (!this.litClient) {
throw new Error('Lit client not initialized');
}
Expand Down Expand Up @@ -147,8 +154,9 @@ export default class LitProtocolCipherProvider implements CipherProviderTypes.IC

// Create the SIWE message
const toSign = await createSiweMessage({
domain,
statement,
// Use class properties instead of function parameters
domain: this.domain,
statement: this.statement,
uri: params.uri,
expiration: params.expiration,
resources: params.resourceAbilityRequests,
Expand Down
70 changes: 35 additions & 35 deletions packages/lit-protocol-cipher/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ describe('LitProvider', () => {
},
];

const mockDomain = 'localhost';
const mockStatement = 'Sign in with Ethereum';

beforeEach(async () => {
jest.clearAllMocks();

Expand Down Expand Up @@ -71,7 +74,13 @@ describe('LitProvider', () => {
signMessage: jest.fn().mockReturnValue(Promise.resolve('mock-signature')),
} as unknown as jest.Mocked<Signer>;

litProvider = new LitProvider(mockLitClient, mockNodeConnectionConfig, mockChain);
litProvider = new LitProvider(
mockLitClient,
mockNodeConnectionConfig,
mockChain,
mockDomain,
mockStatement,
);
await litProvider.initializeClient();
});

Expand Down Expand Up @@ -189,18 +198,11 @@ describe('LitProvider', () => {
derivedVia: 'mock',
signedMessage: 'mock',
};
const mockDomain = 'localhost';
const mockStatement = 'Sign in with Ethereum';

(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,
);
await litProvider.getSessionSignatures(mockSigner, mockWalletAddress);

expect(mockLitClient.connect).toHaveBeenCalled();
expect(mockLitClient.getLatestBlockhash).toHaveBeenCalled();
Expand All @@ -226,10 +228,17 @@ describe('LitProvider', () => {
signedMessage: 'mock',
};

// Create new provider instance without domain and statement
const providerWithoutDomainStatement = new LitProvider(
mockLitClient,
mockNodeConnectionConfig,
mockChain,
);

(generateAuthSig as jest.Mock).mockReturnValue(Promise.resolve(mockAuthSig));
(createSiweMessage as jest.Mock).mockReturnValue(Promise.resolve('mock-siwe-message'));

await litProvider.getSessionSignatures(mockSigner, mockWalletAddress);
await providerWithoutDomainStatement.getSessionSignatures(mockSigner, mockWalletAddress);

expect(mockLitClient.connect).toHaveBeenCalled();
expect(mockLitClient.getLatestBlockhash).toHaveBeenCalled();
Expand All @@ -253,24 +262,28 @@ describe('LitProvider', () => {
derivedVia: 'mock',
signedMessage: 'mock',
};
const mockDomain = 'custom.domain';
const mockStatement = 'Custom statement for signing';
const customDomain = 'custom.domain';
const customStatement = 'Custom statement for signing';

// Create new provider instance with custom domain and statement
const providerWithCustomParams = new LitProvider(
mockLitClient,
mockNodeConnectionConfig,
mockChain,
customDomain,
customStatement,
);

(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,
);
await providerWithCustomParams.getSessionSignatures(mockSigner, mockWalletAddress);

expect(mockLitClient.connect).toHaveBeenCalled();
expect(mockLitClient.getLatestBlockhash).toHaveBeenCalled();
expect(createSiweMessage).toHaveBeenCalledWith({
domain: mockDomain,
statement: mockStatement,
domain: customDomain,
statement: customStatement,
uri: expect.any(String),
expiration: expect.any(String),
resources: expect.any(Array),
Expand All @@ -282,27 +295,14 @@ describe('LitProvider', () => {
});

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,
mockDomain,
mockStatement,
);
await litProvider.getSessionSignatures(mockSigner, mockWalletAddress);

// Reset mocks
jest.clearAllMocks();

// Call again, should not call Lit SDK methods
await litProvider.getSessionSignatures(
mockSigner,
mockWalletAddress,
mockDomain,
mockStatement,
);
await litProvider.getSessionSignatures(mockSigner, mockWalletAddress);

expect(mockLitClient.connect).not.toHaveBeenCalled();
expect(mockLitClient.getLatestBlockhash).not.toHaveBeenCalled();
Expand Down