Skip to content

Commit 7cbc6f2

Browse files
committed
refactor: consolidate AuthData schemas and removing strict types
1 parent aa8c47f commit 7cbc6f2

File tree

7 files changed

+79
-67
lines changed

7 files changed

+79
-67
lines changed

packages/auth-services/src/queue-manager/src/handlers/pkpMint/pkpMint.handler.ts

Lines changed: 8 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,22 @@
1-
import { AuthData } from '@lit-protocol/schemas';
2-
import { Optional } from '@lit-protocol/types';
3-
import { Hex } from 'viem';
1+
import { MintPKPRequest } from '@lit-protocol/schemas';
42

53
/**
64
* Handles PKP minting tasks.
75
* @param jobData The data for the job, expected to contain `requestBody`.
86
* @returns The result of the PKP minting process.
97
*/
108
export async function handlePkpMintTask(jobData: {
11-
requestBody: {
12-
authMethodType: string;
13-
authMethodId: Hex;
14-
pubkey: Hex;
15-
scopes?: ('sign-anything' | 'personal-sign' | 'no-permissions')[];
16-
};
9+
requestBody: MintPKPRequest;
1710
reqId?: string;
1811
}): Promise<any> {
19-
// console.log('[PKP Mint][HANDLER][REQ]', {
20-
// reqId: jobData.reqId,
21-
// authMethodType: jobData.requestBody.authMethodType,
22-
// authMethodId: jobData.requestBody.authMethodId,
23-
// pubkey_len: jobData.requestBody.pubkey?.length ?? 0,
24-
// pubkey_is_0x: jobData.requestBody.pubkey === '0x',
25-
// pubkey_preview: (jobData.requestBody.pubkey ?? '').slice(0, 12),
26-
// scopes: jobData.requestBody.scopes,
27-
// });
28-
29-
if (
30-
// AUTH_METHOD_TYPE.WebAuthn = 3
31-
Number(jobData.requestBody.authMethodType) === 3 &&
32-
(!jobData.requestBody.pubkey || jobData.requestBody.pubkey === '0x')
33-
) {
34-
throw new Error(
35-
`[PKP Mint][HANDLER] WebAuthn requires a non-empty COSE pubkey; got '${jobData.requestBody.pubkey}'. reqId=${jobData.reqId}`
36-
);
37-
}
38-
39-
const userAuthData: Optional<AuthData, 'accessToken'> = {
40-
authMethodId: jobData.requestBody.authMethodId,
41-
authMethodType: Number(jobData.requestBody.authMethodType),
42-
publicKey: jobData.requestBody.pubkey,
43-
};
44-
45-
// console.log('[PKP Mint][HANDLER][MAPPING]', {
46-
// reqId: jobData.reqId,
47-
// authMethodType: userAuthData.authMethodType,
48-
// authMethodId: userAuthData.authMethodId,
49-
// publicKey_len: userAuthData.publicKey?.length ?? 0,
50-
// publicKey_preview: (userAuthData.publicKey ?? '').slice(0, 12),
51-
// });
52-
5312
const result = await globalThis.systemContext.litClient.mintWithAuth({
5413
account: globalThis.systemContext.account,
55-
authData: userAuthData,
56-
scopes: jobData.requestBody.scopes || [],
14+
authData: {
15+
authMethodId: jobData.requestBody.authMethodId,
16+
authMethodType: jobData.requestBody.authMethodType,
17+
publicKey: jobData.requestBody.pubkey,
18+
},
19+
scopes: jobData.requestBody.scopes,
5720
});
5821

5922
console.log(

packages/lit-client/src/lib/LitClient/createLitClient.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ import {
2929
HexPrefixedSchema,
3030
JsonSignCustomSessionKeyRequestForPkpReturnSchema,
3131
JsonSignSessionKeyRequestForPkpReturnSchema,
32-
StrictAuthData,
3332
} from '@lit-protocol/schemas';
3433
import {
3534
DecryptRequest,
@@ -850,7 +849,7 @@ export const _createNagaLitClient = async (
850849
};
851850
},
852851
viewPKPsByAuthData: async (params: {
853-
authData: Partial<StrictAuthData> | Partial<AuthData>;
852+
authData: Partial<AuthData>;
854853
pagination?: { limit?: number; offset?: number };
855854
}) => {
856855
// Use read-only account for viewing PKPs

packages/networks/src/networks/vNaga/shared/factories/BaseChainManagerFactory.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
} from '../interfaces/NetworkContext';
1010
import type { PKPStorageProvider } from '../../../../storage/types';
1111
import { DEV_PRIVATE_KEY } from '@lit-protocol/constants';
12-
import { AuthData, StrictAuthData } from '@lit-protocol/schemas';
12+
import { AuthData } from '@lit-protocol/schemas';
1313

1414
export type CreateChainManagerReturn = {
1515
api: {
@@ -27,7 +27,7 @@ export type CreateChainManagerReturn = {
2727
) => InstanceType<typeof api.PKPPermissionsManager>;
2828
paymentManager: () => InstanceType<typeof api.PaymentManager>;
2929
getPKPsByAuthData: (
30-
authData: Partial<StrictAuthData> | Partial<AuthData>,
30+
authData: Partial<AuthData>,
3131
pagination?: { limit?: number; offset?: number },
3232
storageProvider?: PKPStorageProvider
3333
) => ReturnType<typeof api.PKPPermissionsManager.getPKPsByAuthData>;
@@ -100,7 +100,7 @@ export const createChainManagerFactory = <T, M>(
100100
return new api.PaymentManager(_networkConfig, accountOrWalletClient);
101101
},
102102
getPKPsByAuthData: (
103-
authData: Partial<StrictAuthData> | Partial<AuthData>,
103+
authData: Partial<AuthData>,
104104
pagination?: { limit?: number; offset?: number },
105105
storageProvider?: PKPStorageProvider
106106
) => {

packages/networks/src/networks/vNaga/shared/factories/BaseModuleFactory.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import {
88
HexPrefixedSchema,
99
JsonSignCustomSessionKeyRequestForPkpReturnSchema,
1010
JsonSignSessionKeyRequestForPkpReturnSchema,
11-
StrictAuthData,
1211
} from '@lit-protocol/schemas';
1312
import { Hex, hexToBytes, stringToBytes, bytesToHex } from 'viem';
1413
import { z } from 'zod';
@@ -335,7 +334,7 @@ export function createBaseModule<T, M>(config: BaseModuleConfig<T, M>) {
335334
},
336335

337336
getPKPsByAuthData: async (params: {
338-
authData: Partial<StrictAuthData> | Partial<AuthData>;
337+
authData: Partial<AuthData>;
339338
pagination?: { limit?: number; offset?: number };
340339
storageProvider?: PKPStorageProvider;
341340
account: ExpectedAccountOrWalletClient;

packages/networks/src/networks/vNaga/shared/managers/LitChainClient/apis/highLevelApis/PKPPermissionsManager/PKPPermissionsManager.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ import { ExpectedAccountOrWalletClient } from '../../../../contract-manager/crea
6060
import { ScopeString } from '../../../schemas/shared/ScopeSchema';
6161
import { AuthMethod } from '../../rawContractApis/permissions/read/getPermittedAuthMethods';
6262
import { LitTxVoid } from '../../types';
63-
import { AuthData, StrictAuthData } from '@lit-protocol/schemas';
63+
import { AuthData } from '@lit-protocol/schemas';
6464

6565
// This constant is used for testing purposes
6666
// IPFS CID in v0 format for commonly used test action
@@ -410,7 +410,7 @@ export class PKPPermissionsManager {
410410
* @returns Promise resolving to paginated PKP information
411411
*/
412412
static async getPKPsByAuthData(
413-
authData: Partial<StrictAuthData> | Partial<AuthData>,
413+
authData: Partial<AuthData>,
414414
pagination: { limit?: number; offset?: number } | undefined,
415415
storageProvider: PKPStorageProvider | undefined,
416416
networkContext: DefaultNetworkConfig,
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { z } from 'zod';
2+
3+
/**
4+
* Defines schemas for PKP permission scopes.
5+
* Handles both string inputs and bigint transformations for contract calls.
6+
*/
7+
8+
// Valid scope values
9+
export const SCOPE_VALUES = [
10+
'no-permissions',
11+
'sign-anything',
12+
'personal-sign',
13+
] as const;
14+
export type ScopeString = typeof SCOPE_VALUES[number];
15+
16+
// Mapping from string scopes to their bigint representation
17+
export const SCOPE_MAPPING = {
18+
'no-permissions': 0n,
19+
'sign-anything': 1n,
20+
'personal-sign': 2n,
21+
} as const;
22+
export type ScopeBigInt = typeof SCOPE_MAPPING[ScopeString];
23+
24+
// Schema for string values (used in high-level APIs)
25+
export const ScopeStringSchema = z.enum(SCOPE_VALUES);
26+
27+
// Schema that transforms strings to bigints (used in contract calls)
28+
export const ScopeSchemaRaw = ScopeStringSchema.transform(
29+
(val) => SCOPE_MAPPING[val]
30+
);

packages/schemas/src/lib/auth/auth-schemas.ts

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
NodeSetSchema,
77
SessionKeyUriSchema,
88
} from '../schemas';
9+
import { ScopeSchemaRaw } from './ScopeSchema';
910

1011
export const CustomAuthDataSchema = z.object({
1112
authMethodId: HexPrefixedSchema,
@@ -15,18 +16,6 @@ export const CustomAuthDataSchema = z.object({
1516

1617
export type CustomAuthData = z.infer<typeof CustomAuthDataSchema>;
1718

18-
export const StrictAuthDataSchema = z.object({
19-
authMethodId: HexPrefixedSchema,
20-
authMethodType: z.union([
21-
AuthMethodSchema.shape.authMethodType,
22-
z.number(),
23-
z.bigint(),
24-
]),
25-
accessToken: AuthMethodSchema.shape.accessToken,
26-
});
27-
28-
export type StrictAuthData = z.infer<typeof StrictAuthDataSchema>;
29-
3019
export const AuthDataSchema = z.object({
3120
authMethodId: HexPrefixedSchema,
3221
authMethodType: z.coerce.number().pipe(z.nativeEnum(AUTH_METHOD_TYPE)),
@@ -38,7 +27,8 @@ export const AuthDataSchema = z.object({
3827
metadata: z.any().optional(),
3928
});
4029

41-
export type AuthData = z.infer<typeof AuthDataSchema>;
30+
export type AuthData = z.output<typeof AuthDataSchema>;
31+
export type AuthDataInput = z.input<typeof AuthDataSchema>;
4232

4333
/**
4434
* Return Object Schema
@@ -80,3 +70,34 @@ export const JsonSignCustomSessionKeyRequestForPkpReturnSchema = z
8070
}),
8171
])
8272
);
73+
74+
/**
75+
* Consolidated schema for PKP mint requests.
76+
* This replaces the duplicated schemas across the codebase.
77+
* Handles both string and number authMethodType inputs.
78+
*/
79+
export const MintPKPRequestSchema = z
80+
.object({
81+
authMethodId: HexPrefixedSchema,
82+
authMethodType: z.coerce.number().pipe(z.nativeEnum(AUTH_METHOD_TYPE)),
83+
pubkey: HexPrefixedSchema.default('0x'),
84+
scopes: z.array(ScopeSchemaRaw).optional().default([]),
85+
})
86+
.refine(
87+
(data) => {
88+
// Validate pubkey is present for WebAuthn
89+
// the default has been set to 0x, so we need to check when
90+
// webauthn is used the pubkey should NOT be 0x
91+
if (data.authMethodType === AUTH_METHOD_TYPE.WebAuthn) {
92+
return data.pubkey && data.pubkey !== '0x';
93+
}
94+
return true;
95+
},
96+
{
97+
message: 'pubkey is required for WebAuthn and cannot be 0x',
98+
path: ['pubkey'],
99+
}
100+
);
101+
102+
export type MintPKPRequest = z.input<typeof MintPKPRequestSchema>;
103+
export type MintPKPRequestTransformed = z.output<typeof MintPKPRequestSchema>;

0 commit comments

Comments
 (0)