Skip to content

Commit 15df0d8

Browse files
committed
feat: properly typified getNodePromises
1 parent 7bf56bd commit 15df0d8

File tree

4 files changed

+104
-65
lines changed

4 files changed

+104
-65
lines changed

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

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ import {
6464
MultipleAccessControlConditions,
6565
NodeClientErrorV0,
6666
NodeClientErrorV1,
67+
NodeCommandResponse,
6768
NodeCommandServerKeysResponse,
6869
RejectedNodePromises,
6970
SendNodeCommand,
@@ -1055,21 +1056,16 @@ export class LitCore {
10551056
};
10561057

10571058
/**
1058-
*
10591059
* Get and gather node promises
10601060
*
1061-
* @param { any } callback
1061+
* @param { function } callback
10621062
*
1063-
* @returns { Array<Promise<any>> }
1063+
* @returns { Array<Promise<NodeCommandResponse>> }
10641064
*
10651065
*/
10661066
getNodePromises = (
1067-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
1068-
callback: (url: string) => Promise<any>
1069-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
1070-
): Promise<any>[] => {
1071-
// FIXME: Replace <any> usage with explicit, strongly typed handlers
1072-
1067+
callback: (url: string) => Promise<NodeCommandResponse>
1068+
): Promise<NodeCommandResponse>[] => {
10731069
const nodePromises = [];
10741070

10751071
for (const url of this.connectedNodes) {
@@ -1080,10 +1076,8 @@ export class LitCore {
10801076
};
10811077

10821078
getRandomNodePromise(
1083-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
1084-
callback: (url: string) => Promise<any>
1085-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
1086-
): Promise<any>[] {
1079+
callback: (url: string) => Promise<NodeCommandResponse>
1080+
): Promise<NodeCommandResponse>[] {
10871081
const randomNodeIndex = Math.floor(
10881082
Math.random() * this.connectedNodes.size
10891083
);

packages/schemas/src/lib/ILitNodeClient.ts

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
JsonHandshakeResponseSchema,
1919
MultipleAccessControlConditionsSchema,
2020
NodeBlsSigningShareSchema,
21+
NodeCommandResponseSchema,
2122
NodeCommandServerKeysResponseSchema,
2223
RejectedNodePromisesSchema,
2324
SendNodeCommandSchema,
@@ -100,18 +101,39 @@ export const ILitNodeClientSchema = z.object({
100101
// ========== Promise Handlers ==========
101102

102103
/**
103-
*
104104
* Get and gather node promises
105105
*
106-
* @param { any } callback
106+
* @param { function } callback
107107
*
108-
* @returns { Array<Promise<any>> }
108+
* @returns { Array<Promise<NodeCommandResponse>> }
109109
*
110110
*/
111111
getNodePromises: z
112112
.function()
113-
.args(z.function().args(z.string()).returns(z.promise(z.any()))) // TODO improve
114-
.returns(z.array(z.promise(z.any()))), // TODO
113+
.args(
114+
z
115+
.function()
116+
.args(z.string().url())
117+
.returns(z.promise(NodeCommandResponseSchema))
118+
)
119+
.returns(z.array(z.promise(NodeCommandResponseSchema))),
120+
/**
121+
* Get one node promise
122+
*
123+
* @param { function } callback
124+
*
125+
* @returns { Array<Promise<NodeCommandResponse>> }
126+
*
127+
*/
128+
getRandomNodePromise: z
129+
.function()
130+
.args(
131+
z
132+
.function()
133+
.args(z.string().url())
134+
.returns(z.promise(NodeCommandResponseSchema))
135+
)
136+
.returns(z.array(z.promise(NodeCommandResponseSchema)).length(1)),
115137
/**
116138
* Handle node promises
117139
*

packages/schemas/src/lib/schemas.ts

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,12 @@ export const LitAbilitySchema = z.enum([
6262
'lit-action-execution',
6363
] as const);
6464

65+
export const CapacityDelegationRequestSchema = z.object({
66+
nft_id: z.array(z.string()).optional(), // Optional array of strings
67+
delegate_to: z.array(z.string()).optional(), // Optional array of modified address strings
68+
uses: z.string().optional(),
69+
});
70+
6571
/**
6672
* Type for a contract resolver instance which will be used
6773
* In place of LitContractContext for loading addresses of lit contracts
@@ -695,3 +701,57 @@ export const SuccessNodePromisesSchema = z.object({
695701
success: z.literal(true),
696702
values: z.array(z.any()), // TODO add back generics
697703
});
704+
705+
/**
706+
* Struct in rust
707+
* -----
708+
pub struct JsonExecutionRequest {
709+
pub auth_sig: AuthSigItem,
710+
#[serde(default = "default_epoch")]
711+
pub epoch: u64,
712+
713+
pub ipfs_id: Option<String>,
714+
pub code: Option<String>,
715+
pub js_params: Option<Value>,
716+
pub auth_methods: Option<Vec<AuthMethod>>,
717+
}
718+
*/
719+
export const JsonExecutionRequestSchema = LitActionSdkParamsSchema.pick({
720+
jsParams: true,
721+
}).extend({
722+
authSig: AuthSigSchema,
723+
/**
724+
* auto-filled before sending each command to the node, but
725+
* in the rust struct, this type is required.
726+
*/
727+
// epoch: z.string(),
728+
ipfsId: z.string().optional(),
729+
code: z.string().optional(),
730+
authMethods: z.array(AuthMethodSchema).optional(),
731+
});
732+
733+
export const CallRequestSchema = z.object({
734+
// to - The address of the contract that will be queried
735+
to: z.string(),
736+
// The address calling the function.
737+
from: z.string().optional(),
738+
// Hex encoded data to send to the contract.
739+
data: z.string(),
740+
});
741+
742+
export const JsonSignChainDataRequestSchema = z.object({
743+
callRequests: z.array(z.string()),
744+
chain: ChainSchema,
745+
iat: z.number(),
746+
exp: z.number(),
747+
});
748+
749+
export const JsonRequestSchema = z.union([
750+
JsonExecutionRequestSchema,
751+
JsonSignChainDataRequestSchema,
752+
]);
753+
754+
export const NodeCommandResponseSchema = z.object({
755+
url: z.string(),
756+
data: JsonRequestSchema,
757+
});

packages/types/src/lib/interfaces.ts

Lines changed: 10 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ import {
4141
RejectedNodePromisesSchema,
4242
NodeAttestationSchema,
4343
ExecuteJsAdvancedOptionsSchema,
44+
CapacityDelegationRequestSchema,
45+
JsonExecutionRequestSchema,
46+
NodeCommandResponseSchema,
47+
CallRequestSchema,
4448
} from '@lit-protocol/schemas';
4549

4650
import { ILitNodeClient } from './ILitNodeClient';
@@ -181,21 +185,6 @@ export interface ClaimKeyResponse {
181185
mintTx: string;
182186
}
183187

184-
/**
185-
* Struct in rust
186-
* -----
187-
pub struct JsonExecutionRequest {
188-
pub auth_sig: AuthSigItem,
189-
#[serde(default = "default_epoch")]
190-
pub epoch: u64,
191-
192-
pub ipfs_id: Option<String>,
193-
pub code: Option<String>,
194-
pub js_params: Option<Value>,
195-
pub auth_methods: Option<Vec<AuthMethod>>,
196-
}
197-
*/
198-
199188
export interface BaseJsonPkpSignRequest {
200189
authMethods?: AuthMethod[];
201190
toSign: ArrayLike<number>;
@@ -365,19 +354,7 @@ export interface JsonExecutionRequestTargetNode extends JsonExecutionRequest {
365354
targetNodeRange: number;
366355
}
367356

368-
export interface JsonExecutionRequest
369-
extends Pick<LitActionSdkParams, 'jsParams'> {
370-
authSig: AuthSig;
371-
372-
/**
373-
* auto-filled before sending each command to the node, but
374-
* in the rust struct, this type is required.
375-
*/
376-
// epoch: number;
377-
ipfsId?: string;
378-
code?: string;
379-
authMethods?: AuthMethod[];
380-
}
357+
export type JsonExecutionRequest = z.infer<typeof JsonExecutionRequestSchema>;
381358

382359
export type SessionSigsOrAuthSig = z.infer<typeof SessionSigsOrAuthSigSchema>;
383360

@@ -563,16 +540,7 @@ export interface NodeLog {
563540
logs: any;
564541
}
565542

566-
export interface CallRequest {
567-
// to - The address of the contract that will be queried
568-
to: string;
569-
570-
// The address calling the function.
571-
from?: string;
572-
573-
// Hex encoded data to send to the contract.
574-
data: string;
575-
}
543+
export type CallRequest = z.infer<typeof CallRequestSchema>;
576544

577545
export interface SignedChainDataToken {
578546
// The call requests to make. The responses will be signed and returned.
@@ -582,10 +550,7 @@ export interface SignedChainDataToken {
582550
chain: Chain;
583551
}
584552

585-
export interface NodeCommandResponse {
586-
url: string;
587-
data: JsonRequest;
588-
}
553+
export type NodeCommandResponse = z.infer<typeof NodeCommandResponseSchema>;
589554

590555
export type NodeCommandServerKeysResponse = z.infer<
591556
typeof NodeCommandServerKeysResponseSchema
@@ -1425,11 +1390,9 @@ export interface CapacityDelegationFields extends BaseSiweMessage {
14251390
uses?: string;
14261391
}
14271392

1428-
export interface CapacityDelegationRequest {
1429-
nft_id?: string[]; // Optional array of strings
1430-
delegate_to?: string[]; // Optional array of modified address strings
1431-
uses?: string;
1432-
}
1393+
export type CapacityDelegationRequest = z.infer<
1394+
typeof CapacityDelegationRequestSchema
1395+
>;
14331396

14341397
export interface CapacityCreditsReq {
14351398
dAppOwnerWallet: SignerLike;

0 commit comments

Comments
 (0)