Skip to content

Commit 10f96de

Browse files
committed
fix(networks,schemas): update MintPKPRequest schema validation
1 parent baa51bd commit 10f96de

File tree

2 files changed

+21
-46
lines changed

2 files changed

+21
-46
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { DEV_PRIVATE_KEY, version } from '@lit-protocol/constants';
22
import { verifyAndDecryptWithSignatureShares } from '@lit-protocol/crypto';
33
import {
44
AuthData,
5+
AuthDataInput,
56
EncryptedVersion1Schema,
67
GenericEncryptedPayloadSchema,
78
GenericResultBuilder,
@@ -376,7 +377,7 @@ export function createBaseModule<T, M>(config: BaseModuleConfig<T, M>) {
376377

377378
mintWithAuth: async (params: {
378379
account: ExpectedAccountOrWalletClient;
379-
authData: Optional<AuthData, 'accessToken'>;
380+
authData: Optional<AuthDataInput, 'accessToken'>;
380381
scopes: z.infer<typeof ScopeStringSchema>[];
381382
}): Promise<GenericTxRes<LitTxRes<PKPData>, PKPData>> => {
382383
const chainManager = createChainManager(params.account);

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

Lines changed: 19 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,11 @@ import {
99
NodeSetSchema,
1010
SessionKeyUriSchema,
1111
} from '../schemas';
12-
import { ScopeStringSchema, SCOPE_MAPPING } from './ScopeSchema';
12+
import { ScopeStringSchema } from './ScopeSchema';
1313

1414
export const AuthDataSchema = z.object({
1515
authMethodId: HexPrefixedSchema,
16-
authMethodType: z.union([
17-
AuthMethodSchema.shape.authMethodType,
18-
z.number(),
19-
z.bigint(),
20-
]),
16+
authMethodType: z.coerce.number().pipe(z.nativeEnum(AUTH_METHOD_TYPE)),
2117
accessToken: AuthMethodSchema.shape.accessToken,
2218
publicKey: HexPrefixedSchema.optional(),
2319

@@ -26,7 +22,8 @@ export const AuthDataSchema = z.object({
2622
metadata: z.any().optional(),
2723
});
2824

29-
export type AuthData = z.infer<typeof AuthDataSchema>;
25+
export type AuthData = z.output<typeof AuthDataSchema>;
26+
export type AuthDataInput = z.input<typeof AuthDataSchema>;
3027

3128
/**
3229
* Return Object Schema
@@ -77,48 +74,25 @@ export const JsonSignCustomSessionKeyRequestForPkpReturnSchema = z
7774
export const MintPKPRequestSchema = z
7875
.object({
7976
authMethodId: HexPrefixedSchema,
80-
// Accept string, number, or enum for backwards compatibility
81-
authMethodType: z
82-
.union([
83-
AuthMethodSchema.shape.authMethodType, // z.nativeEnum(AUTH_METHOD_TYPE)
84-
z.string().transform((val) => parseInt(val, 10)),
85-
z.number(),
86-
])
87-
.transform((val) => {
88-
// Ensure it's a valid AUTH_METHOD_TYPE enum value
89-
const numVal = typeof val === 'string' ? parseInt(val, 10) : val;
90-
if (!Object.values(AUTH_METHOD_TYPE).includes(numVal as any)) {
91-
throw new Error(`Invalid authMethodType: ${val}`);
92-
}
93-
return numVal as AUTH_METHOD_TYPE_VALUES;
94-
}),
95-
pubkey: HexPrefixedSchema.optional(),
77+
authMethodType: z.coerce.number().pipe(z.nativeEnum(AUTH_METHOD_TYPE)),
78+
pubkey: HexPrefixedSchema.default('0x'),
9679
scopes: z.array(ScopeStringSchema).optional().default([]),
9780
})
98-
.transform(async (data) => {
99-
let derivedPubkey: z.infer<typeof HexPrefixedSchema>;
100-
101-
// Validate pubkey for WebAuthn
102-
if (data.authMethodType === AUTH_METHOD_TYPE.WebAuthn) {
103-
if (!data.pubkey || data.pubkey === '0x') {
104-
throw new Error(
105-
`pubkey is required for WebAuthn and cannot be 0x. Received pubkey: "${data.pubkey}" and authMethodType: ${data.authMethodType}`
106-
);
81+
.refine(
82+
(data) => {
83+
// Validate pubkey is present for WebAuthn
84+
// the default has been set to 0x, so we need to check when
85+
// webauthn is used the pubkey should NOT be 0x
86+
if (data.authMethodType === AUTH_METHOD_TYPE.WebAuthn) {
87+
return data.pubkey && data.pubkey !== '0x';
10788
}
108-
derivedPubkey = data.pubkey;
109-
} else {
110-
derivedPubkey = '0x' as z.infer<typeof HexPrefixedSchema>;
89+
return true;
90+
},
91+
{
92+
message: 'pubkey is required for WebAuthn and cannot be 0x',
93+
path: ['pubkey'],
11194
}
112-
113-
// Transform scope strings to bigints for contract calls
114-
const scopeBigInts = data.scopes.map(scope => SCOPE_MAPPING[scope]);
115-
116-
return {
117-
...data,
118-
pubkey: derivedPubkey,
119-
scopes: scopeBigInts,
120-
};
121-
});
95+
);
12296

12397
export type MintPKPRequest = z.input<typeof MintPKPRequestSchema>;
12498
export type MintPKPRequestTransformed = z.output<typeof MintPKPRequestSchema>;

0 commit comments

Comments
 (0)