-
Notifications
You must be signed in to change notification settings - Fork 110
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(private-rpc): improved rpc logic
improved rpc logic to pipe non specifically handled requests.
- Loading branch information
1 parent
9f87731
commit aa94397
Showing
7 changed files
with
358 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
import { | ||
delegateCall, | ||
JSONLike, | ||
MethodHandler, | ||
RequestContext, | ||
} from '@/rpc/rpc-service-2'; | ||
import { | ||
isAddressEqual, | ||
parseTransaction, | ||
recoverTransactionAddress, | ||
} from 'viem'; | ||
import { z } from 'zod'; | ||
import { hexSchema } from '@/db/hex-row'; | ||
|
||
const callReqSchema = z.object({ | ||
from: hexSchema.optional(), | ||
to: hexSchema, | ||
gas: hexSchema.optional(), | ||
gas_price: hexSchema.optional(), | ||
max_fee_per_gas: hexSchema.optional(), | ||
max_priority_fee_per_gas: z.number().optional(), | ||
value: hexSchema.optional(), | ||
data: hexSchema.optional(), | ||
input: hexSchema.optional(), | ||
nonce: hexSchema.optional(), | ||
transaction_type: z.number().optional(), | ||
access_list: z.any().optional(), | ||
customData: z.any().optional(), | ||
}); | ||
// type CallRequest = z.infer<typeof callReqSchema>; | ||
|
||
const blockVarianteSchema = z.union([ | ||
hexSchema, | ||
z.literal('earliest'), | ||
z.literal('latest'), | ||
z.literal('safe'), | ||
z.literal('finalized'), | ||
z.literal('pending'), | ||
]); | ||
|
||
const eth_call: MethodHandler = { | ||
name: 'eth_call', | ||
async handle( | ||
context: RequestContext, | ||
method: string, | ||
params: unknown[], | ||
id: number | string, | ||
): Promise<JSONLike> { | ||
const call = callReqSchema.parse(params[0]); | ||
const blockVariant = blockVarianteSchema.optional().parse(params[1]); | ||
|
||
if ( | ||
call.from === undefined || | ||
!isAddressEqual(call.from, context.currentUser) | ||
) { | ||
throw new Error('Wrong caller'); | ||
} | ||
|
||
if (call.data === undefined) { | ||
return delegateCall( | ||
context.targetRpcUrl, | ||
method, | ||
[call, blockVariant], | ||
id, | ||
); | ||
} | ||
|
||
if ( | ||
!context.rules[call.to]?.canRead(context.currentUser, call.data ?? '0x') | ||
) { | ||
throw new Error('Unhautorized'); | ||
} | ||
|
||
const jsonLikePromise = await delegateCall( | ||
context.targetRpcUrl, | ||
method, | ||
[call, blockVariant], | ||
id, | ||
); | ||
return jsonLikePromise; | ||
}, | ||
}; | ||
|
||
const whoAmI = { | ||
name: 'who_am_i', | ||
async handle( | ||
context: RequestContext, | ||
_method: string, | ||
_params: unknown[], | ||
_id: number | string, | ||
) { | ||
return context.currentUser; | ||
}, | ||
}; | ||
|
||
const zks_sendRawTransactionWithDetailedOutput = { | ||
name: 'zks_sendRawTransactionWithDetailedOutput', | ||
async handle( | ||
context: RequestContext, | ||
method: string, | ||
params: unknown[], | ||
id: number | string, | ||
) { | ||
const rawTx = hexSchema.parse(params[0]); | ||
const tx = parseTransaction(rawTx); | ||
const address = await recoverTransactionAddress({ | ||
serializedTransaction: rawTx as any, | ||
}); | ||
|
||
if (!isAddressEqual(address, context.currentUser)) { | ||
throw new Error('Wrong caller'); | ||
} | ||
|
||
if (!tx.to || !tx.data) { | ||
throw new Error('no target or no data'); | ||
} | ||
|
||
if (!context.rules[tx.to]?.canWrite(context.currentUser, tx.data)) { | ||
throw new Error('Unhautorized'); | ||
} | ||
|
||
return delegateCall(context.targetRpcUrl, method, [rawTx], id); | ||
}, | ||
}; | ||
|
||
const eth_sendRawTransaction = { | ||
...zks_sendRawTransactionWithDetailedOutput, | ||
name: 'eth_sendRawTransaction', | ||
}; | ||
|
||
export const allHandlers = [ | ||
eth_call, | ||
whoAmI, | ||
zks_sendRawTransactionWithDetailedOutput, | ||
eth_sendRawTransaction, | ||
]; |
Oops, something went wrong.