Skip to content

Commit 3df541d

Browse files
committed
feat(lit-client): fix and add NagaLitClient with improved type definitions and error handling
1 parent 243d4a1 commit 3df541d

File tree

5 files changed

+164
-76
lines changed

5 files changed

+164
-76
lines changed

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

Lines changed: 86 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ import {
3939
LitNodeSignature,
4040
PkpIdentifierRaw,
4141
RequestItem,
42+
AuthSig,
43+
ExecuteJsResponse,
4244
} from '@lit-protocol/types';
4345
import {
4446
uint8arrayFromString,
@@ -61,6 +63,10 @@ import {
6163
MintWithCustomAuthSchema,
6264
} from './schemas/MintWithCustomAuthSchema';
6365
import { NagaNetworkModule } from './type';
66+
import type {
67+
NagaLitClient,
68+
NagaLitClientContext,
69+
} from './types/NagaLitClient.type';
6470

6571
const _logger = getChildLogger({
6672
module: 'createLitClient',
@@ -155,7 +161,7 @@ export const createLitClient = async ({
155161
*/
156162
export const _createNagaLitClient = async (
157163
networkModule: NagaNetworkModule
158-
) => {
164+
): Promise<NagaLitClient> => {
159165
const _stateManager = await networkModule.createStateManager<
160166
Awaited<ReturnType<typeof orchestrateHandshake>>,
161167
NagaNetworkModule
@@ -195,12 +201,18 @@ export const _createNagaLitClient = async (
195201
const currentHandshakeResult = _stateManager.getCallbackResult();
196202
const currentConnectionInfo = _stateManager.getLatestConnectionInfo();
197203

198-
if (!currentHandshakeResult || !currentConnectionInfo) {
204+
if (!currentHandshakeResult) {
199205
throw new Error(
200206
'Handshake result is not available from state manager at the time of pkpSign.'
201207
);
202208
}
203209

210+
if (!currentConnectionInfo) {
211+
throw new Error(
212+
'Connection info is not available from state manager at the time of pkpSign.'
213+
);
214+
}
215+
204216
const jitContext = await networkModule.api.createJitContext(
205217
currentConnectionInfo,
206218
currentHandshakeResult
@@ -270,17 +282,23 @@ export const _createNagaLitClient = async (
270282
async function _signSessionKey(params: {
271283
nodeUrls: string[];
272284
requestBody: z.infer<typeof JsonSignSessionKeyRequestForPkpReturnSchema>;
273-
}) {
285+
}): Promise<AuthSig> {
274286
// 1. 🟩 get the fresh handshake results
275287
const currentHandshakeResult = _stateManager.getCallbackResult();
276288
const currentConnectionInfo = _stateManager.getLatestConnectionInfo();
277289

278-
if (!currentHandshakeResult || !currentConnectionInfo) {
290+
if (!currentHandshakeResult) {
279291
throw new Error(
280292
'Handshake result is not available from state manager at the time of pkpSign.'
281293
);
282294
}
283295

296+
if (!currentConnectionInfo) {
297+
throw new Error(
298+
'Connection info is not available from state manager at the time of pkpSign.'
299+
);
300+
}
301+
284302
const jitContext = await networkModule.api.createJitContext(
285303
currentConnectionInfo,
286304
currentHandshakeResult
@@ -316,28 +334,28 @@ export const _createNagaLitClient = async (
316334
requestBody: z.infer<
317335
typeof JsonSignCustomSessionKeyRequestForPkpReturnSchema
318336
>;
319-
}) {
337+
}): Promise<AuthSig> {
320338
// 1. 🟩 get the fresh handshake results
321339
const currentHandshakeResult = _stateManager.getCallbackResult();
322340
const currentConnectionInfo = _stateManager.getLatestConnectionInfo();
323341

324-
if (!currentHandshakeResult || !currentConnectionInfo) {
342+
if (!currentHandshakeResult) {
325343
throw new Error(
326344
'Handshake result is not available from state manager at the time of pkpSign.'
327345
);
328346
}
329347

348+
if (!currentConnectionInfo) {
349+
throw new Error(
350+
'Connection info is not available from state manager at the time of pkpSign.'
351+
);
352+
}
353+
330354
const jitContext = await networkModule.api.createJitContext(
331355
currentConnectionInfo,
332356
currentHandshakeResult
333357
);
334358

335-
if (!currentHandshakeResult || !currentConnectionInfo) {
336-
throw new Error(
337-
'Handshake result is not available from state manager at the time of pkpSign.'
338-
);
339-
}
340-
341359
// 2. 🟪 Create requests
342360
const requestArray =
343361
await networkModule.api.signCustomSessionKey.createRequest(
@@ -367,19 +385,25 @@ export const _createNagaLitClient = async (
367385

368386
async function _executeJs(
369387
params: z.infer<typeof networkModule.api.executeJs.schemas.Input>
370-
) {
388+
): Promise<ExecuteJsResponse> {
371389
_logger.info(`🔥 executing JS with ${params.code ? 'code' : 'ipfsId'}`);
372390

373391
// 🟩 get the fresh handshake results
374392
const currentHandshakeResult = _stateManager.getCallbackResult();
375393
const currentConnectionInfo = _stateManager.getLatestConnectionInfo();
376394

377-
if (!currentHandshakeResult || !currentConnectionInfo) {
395+
if (!currentHandshakeResult) {
378396
throw new Error(
379397
'Handshake result is not available from state manager at the time of executeJs.'
380398
);
381399
}
382400

401+
if (!currentConnectionInfo) {
402+
throw new Error(
403+
'Connection info is not available from state manager at the time of executeJs.'
404+
);
405+
}
406+
383407
const jitContext = await networkModule.api.createJitContext(
384408
currentConnectionInfo,
385409
currentHandshakeResult
@@ -390,15 +414,19 @@ export const _createNagaLitClient = async (
390414
// request array to the `networkModule`. It encapsulates logic specific to the
391415
// active network (e.g., pricing, thresholds, metadata) and returns a set of
392416
// structured requests ready to be dispatched to the nodes.
393-
const requestArray = (await networkModule.api.executeJs.createRequest({
394-
// add pricing context for Lit Actions
395-
pricingContext: {
396-
product: 'LIT_ACTION',
397-
userMaxPrice: params.userMaxPrice,
398-
nodePrices: jitContext.nodePrices,
399-
threshold: currentHandshakeResult.threshold,
400-
},
401-
authContext: params.authContext,
417+
type ExecuteJsCreateRequestParams = Parameters<
418+
typeof networkModule.api.executeJs.createRequest
419+
>[0];
420+
421+
const pricingContext: ExecuteJsCreateRequestParams['pricingContext'] = {
422+
product: 'LIT_ACTION',
423+
userMaxPrice: params.userMaxPrice,
424+
nodePrices: jitContext.nodePrices,
425+
threshold: currentHandshakeResult.threshold,
426+
};
427+
428+
const baseExecuteJsParams = {
429+
pricingContext,
402430
executionContext: {
403431
code: params.code,
404432
ipfsId: params.ipfsId,
@@ -409,7 +437,22 @@ export const _createNagaLitClient = async (
409437
useSingleNode: params.useSingleNode,
410438
responseStrategy: params.responseStrategy,
411439
jitContext,
412-
})) as RequestItem<z.infer<typeof EncryptedVersion1Schema>>[];
440+
};
441+
442+
const executeJsParams: ExecuteJsCreateRequestParams =
443+
'sessionSigs' in params && params.sessionSigs
444+
? {
445+
...baseExecuteJsParams,
446+
sessionSigs: params.sessionSigs,
447+
}
448+
: {
449+
...baseExecuteJsParams,
450+
authContext: params.authContext,
451+
};
452+
453+
const requestArray = (await networkModule.api.executeJs.createRequest(
454+
executeJsParams
455+
)) as RequestItem<z.infer<typeof EncryptedVersion1Schema>>[];
413456

414457
const requestId = requestArray[0].requestId;
415458

@@ -567,10 +610,17 @@ export const _createNagaLitClient = async (
567610
);
568611

569612
// ========== Encrypt ==========
613+
const encryptionPayload =
614+
dataAsUint8Array instanceof Uint8Array
615+
? dataAsUint8Array
616+
: new Uint8Array(dataAsUint8Array);
617+
618+
const identityBytes = uint8arrayFromString(identityParam, 'utf8');
619+
570620
const ciphertext = await blsEncrypt(
571621
currentHandshakeResult.coreNodeConfig.subnetPubKey,
572-
dataAsUint8Array,
573-
uint8arrayFromString(identityParam, 'utf8')
622+
encryptionPayload,
623+
identityBytes
574624
);
575625

576626
return {
@@ -605,23 +655,23 @@ export const _createNagaLitClient = async (
605655
const currentHandshakeResult = _stateManager.getCallbackResult();
606656
const currentConnectionInfo = _stateManager.getLatestConnectionInfo();
607657

608-
if (!currentHandshakeResult || !currentConnectionInfo) {
658+
if (!currentHandshakeResult) {
609659
throw new Error(
610660
'Handshake result is not available from state manager at the time of decrypt.'
611661
);
612662
}
613663

664+
if (!currentConnectionInfo) {
665+
throw new Error(
666+
'Connection info is not available from state manager at the time of decrypt.'
667+
);
668+
}
669+
614670
const jitContext = await networkModule.api.createJitContext(
615671
currentConnectionInfo,
616672
currentHandshakeResult
617673
);
618674

619-
if (!currentHandshakeResult || !currentConnectionInfo) {
620-
throw new Error(
621-
'Handshake result is not available from state manager at the time of decrypt.'
622-
);
623-
}
624-
625675
if (!currentHandshakeResult.coreNodeConfig?.subnetPubKey) {
626676
throw new Error('subnetPubKey cannot be null');
627677
}
@@ -714,13 +764,14 @@ export const _createNagaLitClient = async (
714764
return response;
715765
}
716766

717-
const litClient = {
767+
const litClient: NagaLitClient = {
768+
networkName: networkModule.getNetworkName(),
718769
// This function is likely be used by another module to get the current context, eg. auth manager
719770
// only adding what is required by other modules for now.
720771
// maybe you will need connectionInfo: _stateManager.getLatestConnectionInfo(),
721772
encrypt: _encrypt,
722773
decrypt: _decrypt,
723-
getContext: async () => {
774+
getContext: async (): Promise<NagaLitClientContext> => {
724775
return {
725776
latestBlockhash: await _stateManager.getLatestBlockhash(),
726777
latestConnectionInfo: _stateManager.getLatestConnectionInfo(),

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22
import { NagaLocalModule } from '@lit-protocol/networks';
33
import { NagaDevModule } from '@lit-protocol/networks';
44
import { NagaStagingModule } from '@lit-protocol/networks';
5-
// import { NagaLitClient } from './types/NagaLitClient.type';
5+
import type {
6+
NagaLitClient,
7+
NagaLitClientContext,
8+
} from './types/NagaLitClient.type';
69

710
/**
811
* ========== All Network Modules ==========
@@ -27,5 +30,5 @@ export type NagaNetworkModule =
2730
/**
2831
* Union type for all possible Lit clients
2932
*/
30-
// export type LitClient = NagaLitClient;
31-
// | DatilLitClient;
33+
export type LitClient = NagaLitClient;
34+
export type LitClientContext = NagaLitClientContext;

packages/lit-client/src/lib/LitClient/types/BaseClient.type.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { ChainConfig } from 'viem';
1111
* Base interface shared by all Lit clients
1212
*/
1313
export interface BaseLitClient<T> {
14+
networkName: string;
1415
encrypt: (params: EncryptSdkParams) => Promise<EncryptResponse>;
1516
decrypt: (params: DecryptRequest) => Promise<DecryptResponse>;
1617
getContext: () => Promise<T>;
@@ -20,7 +21,7 @@ export interface BaseLitClient<T> {
2021
};
2122
disconnect: () => void;
2223
getDefault: {
23-
authServiceUrl: string;
24+
authServiceUrl?: string;
2425
loginUrl: string;
2526
};
2627
}

0 commit comments

Comments
 (0)