Skip to content

Commit 59a58e0

Browse files
committed
feat(auth): add optional caching for delegation auth signature
- Introduced a cache parameter in the auth manager and related functions to control caching behavior for delegation auth signatures. - Updated the GetPkpAuthContextSchema to include an optional cache object. - Enhanced tryGetCachedDelegationAuthSig to generate a new signature if caching is disabled, improving flexibility in auth signature management.
1 parent 2f4a32f commit 59a58e0

File tree

5 files changed

+35
-2
lines changed

5 files changed

+35
-2
lines changed

packages/auth/src/lib/AuthManager/auth-manager.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ export const createAuthManager = (authManagerParams: AuthManagerParams) => {
7474
pkpPublicKey: z.infer<typeof HexPrefixedSchema>;
7575
authConfig: AuthConfigV2;
7676
litClient: BaseAuthContext<any>['litClient'];
77+
cache?: {
78+
delegationAuthSig?: boolean;
79+
}
7780
// Optional pre-generated auth materials for server-side usage
7881
// sessionKeyPair?: SessionKeyPair;
7982
// delegationAuthSig?: AuthSig;

packages/auth/src/lib/AuthManager/authAdapters/getPkpAuthContextAdapter.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ export async function getPkpAuthContextAdapter(
6464
litClient: {
6565
getContext: () => Promise<any>;
6666
};
67+
cache?: {
68+
delegationAuthSig?: boolean;
69+
}
6770
// Optional pre-generated auth materials
6871
// sessionKeyPair?: SessionKeyPair;
6972
// delegationAuthSig?: AuthSig;
@@ -172,5 +175,6 @@ export async function getPkpAuthContextAdapter(
172175
storage: upstreamParams.storage,
173176
pkpAddress: pkpAddress,
174177
},
178+
cache: params.cache,
175179
});
176180
}

packages/auth/src/lib/AuthManager/authContexts/getPkpAuthContext.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ export const GetPkpAuthContextSchema = z.object({
5050
// @depreacted - to be removed. testing only.
5151
pkpAddress: z.string(),
5252
}),
53+
cache: z.object({
54+
delegationAuthSig: z.boolean().optional(),
55+
}).optional(),
5356
});
5457

5558
interface PreparePkpAuthRequestBodyParams {
@@ -139,6 +142,7 @@ export const getPkpAuthContext = async (
139142
};
140143

141144
const delegationAuthSig = await tryGetCachedDelegationAuthSig({
145+
cache: _params.cache?.delegationAuthSig,
142146
storage: _params.deps.storage,
143147
address: _params.deps.pkpAddress,
144148
expiration: _params.authConfig.expiration,

packages/auth/src/lib/AuthManager/try-getters/tryGetCachedDelegationAuthSig.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,29 @@ export async function tryGetCachedDelegationAuthSig(params: {
1010
address: string;
1111
expiration: string; // Desired expiration for new sigs, not used for checking existing
1212
signSessionKey: any; // This is assumed to be a Promise resolving to the auth sig
13+
cache?: boolean; // If false, always generate a new auth sig
1314
}) {
15+
// If cache is explicitly disabled, skip cache reading and generate new auth sig
16+
if (!params.cache) {
17+
_logger.info(
18+
'tryGetCachedDelegationAuthSig: Cache disabled, generating new delegation auth sig.',
19+
{
20+
address: params.address,
21+
}
22+
);
23+
24+
const newDelegationAuthSig = await params.signSessionKey();
25+
26+
_logger.info(
27+
'tryGetCachedDelegationAuthSig: Generated new delegation auth sig (cache disabled, not saved).',
28+
{
29+
address: params.address,
30+
}
31+
);
32+
33+
return newDelegationAuthSig;
34+
}
35+
1436
const delegationAuthSigString =
1537
await params.storage.readInnerDelegationAuthSig({
1638
publicKey: params.address,

packages/lit-client/src/lib/LitNodeClient/LitNodeApi/src/helper/sendNodeRequest.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ export async function sendNodeRequest<T>(
6363
const curlCommand = generateCurlCommand(_fullUrl, req);
6464
_logger.info('🔄 CURL command:', curlCommand);
6565

66-
// if (_fullUrl.includes('pkp/sign/v2')) {
67-
// console.log('🔄 req', req);
66+
// if (_fullUrl.includes('sign_session_key')) {
67+
// console.log("Curl command: ", curlCommand);
6868
// process.exit();
6969
// }
7070

0 commit comments

Comments
 (0)