Skip to content

Commit

Permalink
feat(providers): add requestAccounts
Browse files Browse the repository at this point in the history
  • Loading branch information
mrcnk committed Oct 27, 2024
1 parent 96ad60e commit f4d34d6
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
36 changes: 35 additions & 1 deletion packages/providers/src/validation.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ZkAppCommandPayload } from "@mina-js/utils";
import { JsonSchema } from "@mina-js/utils";
import {
FieldSchema,
NullifierSchema,
Expand Down Expand Up @@ -27,7 +28,12 @@ export const AddChainRequestParams = z

// Params
export const AccountsRequestParamsSchema = z
.object({ method: z.literal("mina_accounts") })
.object({
method: z.union([
z.literal("mina_accounts"),
z.literal("mina_requestAccounts"),
]),
})
.strict();
export const ChainIdRequestParamsSchema = z
.object({ method: z.literal("mina_chainId") })
Expand Down Expand Up @@ -80,6 +86,18 @@ export const AddChainRequestParamsSchema = z
params: z.array(AddChainRequestParams),
})
.strict();
export const SetStateRequestParamsSchema = z
.object({
method: z.literal("mina_setState"),
params: z.array(JsonSchema),
})
.strict();
export const GetStateRequestParamsSchema = z
.object({
method: z.literal("mina_getState"),
params: z.array(z.string()),
})
.strict();

// Returns
export const AccountsRequestReturnSchema = z
Expand Down Expand Up @@ -148,6 +166,18 @@ export const AddChainRequestReturnSchema = z
result: z.string(),
})
.strict();
export const SetStateRequestReturnSchema = z
.object({
method: z.literal("mina_setState"),
result: z.object({ success: z.boolean() }),
})
.strict();
export const GetStateRequestReturnSchema = z
.object({
method: z.literal("mina_getState"),
result: JsonSchema,
})
.strict();

export const RpcReturnTypesUnion = z.discriminatedUnion("method", [
AccountsRequestReturnSchema,
Expand All @@ -161,6 +191,8 @@ export const RpcReturnTypesUnion = z.discriminatedUnion("method", [
CreateNullifierRequestReturnSchema,
SwitchChainRequestReturnSchema,
AddChainRequestReturnSchema,
SetStateRequestReturnSchema,
GetStateRequestReturnSchema,
]);

export const ProviderRequestParamsUnion = z.discriminatedUnion("method", [
Expand All @@ -175,6 +207,8 @@ export const ProviderRequestParamsUnion = z.discriminatedUnion("method", [
CreateNullifierRequestParamsSchema,
SwitchChainRequestParamsSchema,
AddChainRequestParamsSchema,
SetStateRequestParamsSchema,
GetStateRequestParamsSchema,
]);
export type RpcReturnTypesUnionType = z.infer<typeof RpcReturnTypesUnion>;
export type ResultType<M extends string> = {
Expand Down
2 changes: 1 addition & 1 deletion packages/utils/src/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const LiteralSchema = z.union([
z.boolean(),
z.null(),
]);
const JsonSchema: z.ZodType<Json> = z.lazy(() =>
export const JsonSchema: z.ZodType<Json> = z.lazy(() =>
z.union([LiteralSchema, z.array(JsonSchema), z.record(JsonSchema)]),
);

Expand Down

0 comments on commit f4d34d6

Please sign in to comment.