1
1
import { getConnInfo } from "@hono/node-server/conninfo" ;
2
2
import { OpenAPIHono , createRoute } from "@hono/zod-openapi" ;
3
- import { PublicKeySchema } from "@mina-js/utils" ;
3
+ import {
4
+ KlesiaRpcMethod ,
5
+ KlesiaRpcMethodSchema ,
6
+ KlesiaRpcResponseSchema ,
7
+ PublicKeySchema ,
8
+ } from "@mina-js/utils" ;
4
9
import { rateLimiter } from "hono-rate-limiter" ;
5
10
import { cors } from "hono/cors" ;
6
11
import { logger } from "hono/logger" ;
7
12
import { nanoid } from "nanoid" ;
8
13
import { match } from "ts-pattern" ;
9
- import mainDocs from "../docs/index.txt" ;
10
- import rpcDocs from "../docs/rpc.txt" ;
11
14
import { mina } from "./methods/mina" ;
12
- import { RpcMethod , RpcMethodSchema , RpcResponseSchema } from "./schema" ;
13
15
import { buildResponse } from "./utils/build-response" ;
14
16
15
17
export const api = new OpenAPIHono ( ) ;
@@ -35,22 +37,22 @@ api.doc("/api/openapi", {
35
37
info : {
36
38
version : "1.0.0" ,
37
39
title : "Klesia RPC" ,
38
- description : mainDocs ,
39
40
} ,
40
41
} ) ;
41
42
42
43
const rpcRoute = createRoute ( {
43
44
method : "post" ,
44
45
path : "/api" ,
45
- description : rpcDocs ,
46
46
request : {
47
- body : { content : { "application/json" : { schema : RpcMethodSchema } } } ,
47
+ body : {
48
+ content : { "application/json" : { schema : KlesiaRpcMethodSchema } } ,
49
+ } ,
48
50
} ,
49
51
responses : {
50
52
200 : {
51
53
content : {
52
54
"application/json" : {
53
- schema : RpcResponseSchema ,
55
+ schema : KlesiaRpcResponseSchema ,
54
56
} ,
55
57
} ,
56
58
description : "JSON-RPC response." ,
@@ -59,10 +61,10 @@ const rpcRoute = createRoute({
59
61
} ) ;
60
62
61
63
export const klesiaRpcRoute = api . openapi ( rpcRoute , async ( { req, json } ) => {
62
- const body = req . valid ( " json" ) ;
64
+ const body = KlesiaRpcMethodSchema . parse ( await req . json ( ) ) ;
63
65
return match ( body )
64
66
. with (
65
- { method : RpcMethod . enum . mina_getTransactionCount } ,
67
+ { method : KlesiaRpcMethod . enum . mina_getTransactionCount } ,
66
68
async ( { params } ) => {
67
69
const [ publicKey ] = params ;
68
70
const result = await mina . getTransactionCount ( {
@@ -76,14 +78,17 @@ export const klesiaRpcRoute = api.openapi(rpcRoute, async ({ req, json }) => {
76
78
) ;
77
79
} ,
78
80
)
79
- . with ( { method : RpcMethod . enum . mina_getBalance } , async ( { params } ) => {
80
- const [ publicKey ] = params ;
81
- const result = await mina . getBalance ( {
82
- publicKey : PublicKeySchema . parse ( publicKey ) ,
83
- } ) ;
84
- return json ( buildResponse ( { result } ) , 200 ) ;
85
- } )
86
- . with ( { method : RpcMethod . enum . mina_blockHash } , async ( ) => {
81
+ . with (
82
+ { method : KlesiaRpcMethod . enum . mina_getBalance } ,
83
+ async ( { params } ) => {
84
+ const [ publicKey ] = params ;
85
+ const result = await mina . getBalance ( {
86
+ publicKey : PublicKeySchema . parse ( publicKey ) ,
87
+ } ) ;
88
+ return json ( buildResponse ( { result } ) , 200 ) ;
89
+ } ,
90
+ )
91
+ . with ( { method : KlesiaRpcMethod . enum . mina_blockHash } , async ( ) => {
87
92
if ( process . env . MINA_NETWORK === "zeko_devnet" ) {
88
93
return json (
89
94
buildResponse ( {
@@ -98,12 +103,12 @@ export const klesiaRpcRoute = api.openapi(rpcRoute, async ({ req, json }) => {
98
103
const result = await mina . blockHash ( ) ;
99
104
return json ( buildResponse ( { result } ) , 200 ) ;
100
105
} )
101
- . with ( { method : RpcMethod . enum . mina_chainId } , async ( ) => {
106
+ . with ( { method : KlesiaRpcMethod . enum . mina_chainId } , async ( ) => {
102
107
const result = await mina . chainId ( ) ;
103
108
return json ( buildResponse ( { result } ) , 200 ) ;
104
109
} )
105
110
. with (
106
- { method : RpcMethod . enum . mina_sendTransaction } ,
111
+ { method : KlesiaRpcMethod . enum . mina_sendTransaction } ,
107
112
async ( { params } ) => {
108
113
const [ signedTransaction , type ] = params ;
109
114
const result = await mina . sendTransaction ( { signedTransaction, type } ) ;
@@ -115,21 +120,17 @@ export const klesiaRpcRoute = api.openapi(rpcRoute, async ({ req, json }) => {
115
120
) ;
116
121
} ,
117
122
)
118
- . with ( { method : RpcMethod . enum . mina_getAccount } , async ( { params } ) => {
119
- const [ publicKey ] = params ;
120
- const result = await mina . getAccount ( {
121
- publicKey : PublicKeySchema . parse ( publicKey ) ,
122
- } ) ;
123
- return json ( buildResponse ( { result } ) , 200 ) ;
124
- } )
123
+ . with (
124
+ { method : KlesiaRpcMethod . enum . mina_getAccount } ,
125
+ async ( { params } ) => {
126
+ const [ publicKey ] = params ;
127
+ const result = await mina . getAccount ( {
128
+ publicKey : PublicKeySchema . parse ( publicKey ) ,
129
+ } ) ;
130
+ return json ( buildResponse ( { result } ) , 200 ) ;
131
+ } ,
132
+ )
125
133
. exhaustive ( ) ;
126
134
} ) ;
127
135
128
136
export type KlesiaRpc = typeof klesiaRpcRoute ;
129
- export {
130
- KlesiaNetwork ,
131
- RpcMethod ,
132
- type RpcMethodType ,
133
- type RpcResponseType ,
134
- type RpcErrorType ,
135
- } from "./schema" ;
0 commit comments