Skip to content

Commit 095744a

Browse files
committed
feat: add params validations on lit node client and lit core and slightly improve its testing
1 parent 47f9692 commit 095744a

File tree

6 files changed

+72
-46
lines changed

6 files changed

+72
-46
lines changed

packages/core/src/lib/lit-core.ts

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import {
2121
LIT_CURVE,
2222
LIT_CURVE_VALUES,
2323
LIT_ENDPOINT,
24-
LIT_ERROR,
2524
LIT_ERROR_CODE,
2625
LIT_NETWORK,
2726
LIT_NETWORKS,
@@ -30,7 +29,6 @@ import {
3029
STAKING_STATES_VALUES,
3130
version,
3231
InitError,
33-
InvalidParamType,
3432
NodeError,
3533
UnknownError,
3634
InvalidArgumentException,
@@ -57,7 +55,6 @@ import {
5755
import {
5856
AuthSig,
5957
BlockHashErrorResponse,
60-
CustomNetwork,
6158
EpochInfo,
6259
EthBlockhashInfo,
6360
FormattedMultipleAccs,
@@ -74,6 +71,7 @@ import {
7471
SuccessNodePromises,
7572
SupportedJsonRequests,
7673
} from '@lit-protocol/types';
74+
import { LitNodeClientConfigSchema } from '@lit-protocol/schemas';
7775

7876
import { composeLitUrl } from './endpoint-version';
7977

@@ -166,33 +164,46 @@ export class LitCore {
166164
'https://block-indexer.litgateway.com/get_most_recent_valid_block';
167165

168166
// ========== Constructor ==========
169-
constructor(config: LitNodeClientConfig | CustomNetwork) {
170-
if (!(config.litNetwork in LIT_NETWORKS)) {
171-
const validNetworks = Object.keys(LIT_NETWORKS).join(', ');
172-
throw new InvalidParamType(
173-
{},
174-
'Unsupported network has been provided please use a "litNetwork" option which is supported (%s)',
175-
validNetworks
167+
constructor(config: LitNodeClientConfig) {
168+
let _args: LitNodeClientConfig;
169+
try {
170+
_args = LitNodeClientConfigSchema.parse(config);
171+
} catch (e) {
172+
throw new InvalidArgumentException(
173+
{
174+
cause: e,
175+
info: {
176+
config,
177+
},
178+
},
179+
'must provide LitNodeClient parameters'
176180
);
177181
}
178182

183+
// Zod makes a deep copy hence it might drop the storageProvider class
184+
if ('storageProvider' in config && config.storageProvider?.provider) {
185+
_args.storageProvider = {
186+
provider: config.storageProvider?.provider,
187+
};
188+
}
189+
179190
// Initialize default config based on litNetwork
180-
switch (config?.litNetwork) {
191+
switch (_args?.litNetwork) {
181192
// Official networks; default value for `checkNodeAttestation` according to network provided.
182193
case LIT_NETWORK.DatilDev:
183194
this.config = {
184195
...this.config,
185196
checkNodeAttestation: NETWORKS_REQUIRING_SEV.includes(
186-
config?.litNetwork
197+
_args?.litNetwork
187198
),
188-
...config,
199+
..._args,
189200
};
190201
break;
191202
default:
192203
// `custom`; no opinion about checkNodeAttestation
193204
this.config = {
194205
...this.config,
195-
...config,
206+
..._args,
196207
};
197208
}
198209

packages/lit-node-client-nodejs/src/lib/lit-node-client-nodejs.spec.ts

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,8 @@
1-
// @ts-nocheck
2-
3-
// This will prevent it logging the following
4-
// [Lit-JS-SDK v2.2.39] ✅ [BLS SDK] wasmExports loaded
5-
// [Lit-JS-SDK v2.2.39] ✅ [ECDSA SDK NodeJS] wasmECDSA loaded.
6-
global.jestTesting = true;
7-
8-
import { LIT_NETWORK } from '@lit-protocol/constants';
1+
import { LIT_NETWORK, InvalidArgumentException } from '@lit-protocol/constants';
92

103
import { LitNodeClientNodeJs } from './lit-node-client-nodejs';
114

12-
const isClass = (v) => {
5+
const isClass = (v: any) => {
136
return typeof v === 'function' && /^\s*class\s+/.test(v.toString());
147
};
158

@@ -49,9 +42,32 @@ describe('LitNodeClientNodeJs', () => {
4942
Object.defineProperty(globalThis, 'localStorage', { value: tmp });
5043
});
5144

45+
it('should throw when constructor is passed invalid params', () => {
46+
// @ts-expect-error testing invalid params
47+
expect(() => new LitNodeClientNodeJs({ litNetwork: 'invalid' })).toThrow(
48+
InvalidArgumentException
49+
);
50+
51+
expect(
52+
() =>
53+
new LitNodeClientNodeJs({
54+
litNetwork: LIT_NETWORK.Datil,
55+
// @ts-expect-error testing invalid params
56+
checkNodeAttestation: 1,
57+
// @ts-expect-error testing invalid params
58+
minNodeCount: 'something',
59+
rpcUrl: 'notAnURL',
60+
})
61+
).toThrow(InvalidArgumentException);
62+
});
63+
5264
it('gets expiration', () => {
5365
const expiration = LitNodeClientNodeJs.getExpiration();
5466

55-
expect(expiration).toContain('T');
67+
// Regex pattern for ISO 8601 date format
68+
const dateRegex =
69+
/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d+)?(Z|([+-]\d{2}:\d{2}))?$/;
70+
71+
expect(expiration).toMatch(dateRegex);
5672
});
5773
});

packages/lit-node-client-nodejs/src/lib/lit-node-client-nodejs.ts

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ import {
6363
setStorageItem,
6464
} from '@lit-protocol/misc-browser';
6565
import { nacl } from '@lit-protocol/nacl';
66+
import { LitNodeClientConfigSchema } from '@lit-protocol/schemas';
6667
import {
6768
uint8arrayFromString,
6869
uint8arrayToString,
@@ -89,7 +90,6 @@ import type {
8990
ClaimKeyResponse,
9091
ClaimProcessor,
9192
ClaimRequest,
92-
CustomNetwork,
9393
DecryptRequest,
9494
DecryptResponse,
9595
EncryptRequest,
@@ -110,7 +110,6 @@ import type {
110110
SessionKeyPair,
111111
SessionSigningTemplate,
112112
SessionSigsMap,
113-
SigShare,
114113
SignSessionKeyProp,
115114
SignSessionKeyResponse,
116115
Signature,
@@ -142,15 +141,27 @@ export class LitNodeClientNodeJs
142141
defaultAuthCallback?: (authSigParams: AuthCallbackParams) => Promise<AuthSig>;
143142

144143
// ========== Constructor ==========
145-
constructor(args: LitNodeClientConfig | CustomNetwork) {
146-
if (!args) {
147-
throw new ParamsMissingError({}, 'must provide LitNodeClient parameters');
144+
constructor(args: LitNodeClientConfig) {
145+
let _args: LitNodeClientConfig;
146+
try {
147+
_args = LitNodeClientConfigSchema.parse(args);
148+
} catch (e) {
149+
throw new InvalidArgumentException(
150+
{
151+
cause: e,
152+
info: {
153+
args,
154+
},
155+
},
156+
'must provide LitNodeClient parameters'
157+
);
148158
}
149159

160+
// Passing args instead of _args so parent can make its own assertions without the ones here interfering
150161
super(args);
151162

152-
if ('defaultAuthCallback' in args) {
153-
this.defaultAuthCallback = args.defaultAuthCallback;
163+
if ('defaultAuthCallback' in _args) {
164+
this.defaultAuthCallback = _args.defaultAuthCallback;
154165
}
155166
}
156167

packages/lit-node-client/src/lib/lit-node-client.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { EITHER_TYPE } from '@lit-protocol/constants';
33
import { LitNodeClientNodeJs } from '@lit-protocol/lit-node-client-nodejs';
44
import { isNode, log } from '@lit-protocol/misc';
55
import { getStorageItem } from '@lit-protocol/misc-browser';
6-
import { CustomNetwork, LitNodeClientConfig } from '@lit-protocol/types';
6+
import { LitNodeClientConfig } from '@lit-protocol/types';
77

88
/**
99
* You can find all these available networks in the `constants` package
@@ -19,7 +19,7 @@ import { CustomNetwork, LitNodeClientConfig } from '@lit-protocol/types';
1919
* ```
2020
*/
2121
export class LitNodeClient extends LitNodeClientNodeJs {
22-
constructor(args: LitNodeClientConfig | CustomNetwork) {
22+
constructor(args: LitNodeClientConfig) {
2323
super({
2424
...args,
2525
defaultAuthCallback: checkAndSignAuthMessage,

packages/schemas/src/lib/schemas.ts

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -383,15 +383,6 @@ export const LitNodeClientConfigSchema = z.object({
383383
rpcUrl: z.string().optional(),
384384
});
385385

386-
export const CustomNetworkSchema = z.intersection(
387-
LitNodeClientConfigSchema.pick({
388-
litNetwork: true,
389-
contractContext: true,
390-
checkNodeAttestation: true,
391-
}),
392-
LitNodeClientConfigSchema.pick({ minNodeCount: true }).partial()
393-
);
394-
395386
export const DerivedAddressesSchema = z.object({
396387
publicKey: z.string(),
397388
publicKeyBuffer: z.instanceof(Buffer),
@@ -886,7 +877,7 @@ export const ILitNodeClientSchema = z.object({
886877

887878
// ========== Constructor ==========
888879
// ** IMPORTANT !! You have to create your constructor when implementing this class **
889-
// constructor: z.function().args(z.union([LitNodeClientConfigSchema, CustomNetworkSchema])),
880+
// constructor: z.function().args(LitNodeClientConfigSchema),
890881

891882
// ========== Scoped Class Helpers ==========
892883

packages/types/src/lib/interfaces.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import {
1010
AuthCallbackParamsSchema,
1111
StorageProviderSchema,
1212
LitNodeClientConfigSchema,
13-
CustomNetworkSchema,
1413
EncryptResponseSchema,
1514
JsonHandshakeResponseSchema,
1615
BlsSignatureShareSchema,
@@ -169,8 +168,6 @@ export type StorageProvider = z.infer<typeof StorageProviderSchema>;
169168
/** ---------- Lit Node Client ---------- */
170169
export type LitNodeClientConfig = z.infer<typeof LitNodeClientConfigSchema>;
171170

172-
export type CustomNetwork = z.infer<typeof CustomNetworkSchema>;
173-
174171
export interface Signature {
175172
r: string;
176173
s: string;

0 commit comments

Comments
 (0)