1- import { Eip5792GetCapabilitiesParams , Eip5792SendCallsParams } from "@toruslabs/ethereum-controllers" ;
1+ import { Eip5792GetCapabilitiesParams , Eip5792SendCallsParams , SUPPORTED_EIP_5792_VERSIONS } from "@toruslabs/ethereum-controllers" ;
22import { createAsyncMiddleware , createScaffoldMiddleware , JRPCMiddleware , JRPCRequest , JRPCResponse , rpcErrors } from "@web3auth/auth" ;
33import { isHex , toHex } from "viem" ;
44
@@ -16,6 +16,7 @@ export function createWalletMiddleware({
1616 processGetCapabilities,
1717 processSendCalls,
1818 processGetCallsStatus,
19+ processShowCallsStatus,
1920} : WalletMiddlewareOptions ) : JRPCMiddleware < string , unknown > {
2021 if ( ! getAccounts ) {
2122 throw new Error ( "opts.getAccounts is required" ) ;
@@ -221,6 +222,28 @@ export function createWalletMiddleware({
221222 }
222223
223224 const params = Array . isArray ( req . params ) ? req . params [ 0 ] : req . params ;
225+ if ( ! params || typeof params !== "object" ) {
226+ throw rpcErrors . invalidParams ( "Missing or invalid params for wallet_sendCalls" ) ;
227+ }
228+
229+ if ( ! params . version || ! SUPPORTED_EIP_5792_VERSIONS . includes ( params . version ) ) {
230+ throw rpcErrors . invalidParams (
231+ `Invalid version: expected one of ${ SUPPORTED_EIP_5792_VERSIONS . join ( ", " ) } , got "${ params . version || "undefined" } "`
232+ ) ;
233+ }
234+
235+ if ( ! params . chainId ) {
236+ throw rpcErrors . invalidParams ( "Missing required field: chainId" ) ;
237+ }
238+
239+ if ( ! Array . isArray ( params . calls ) || params . calls . length === 0 ) {
240+ throw rpcErrors . invalidParams ( "calls must be a non-empty array" ) ;
241+ }
242+
243+ const from = params . from as string | undefined ;
244+ if ( from ) {
245+ await validateAndNormalizeKeyholder ( from , req ) ;
246+ }
224247
225248 const walletSendCallsParams : Eip5792SendCallsParams = {
226249 ...params ,
@@ -236,10 +259,27 @@ export function createWalletMiddleware({
236259 }
237260
238261 const batchId = Array . isArray ( req . params ) ? req . params [ 0 ] : ( req . params as string ) ;
262+ if ( ! batchId || typeof batchId !== "string" ) {
263+ throw rpcErrors . invalidParams ( "Missing or invalid batchId" ) ;
264+ }
239265
240266 res . result = await processGetCallsStatus ( batchId ) ;
241267 }
242268
269+ async function walletShowCallsStatusMiddleware ( req : JRPCRequest < unknown > , res : JRPCResponse < unknown > ) : Promise < void > {
270+ if ( ! processShowCallsStatus ) {
271+ throw rpcErrors . methodNotSupported ( ) ;
272+ }
273+
274+ const batchId = Array . isArray ( req . params ) ? req . params [ 0 ] : ( req . params as string ) ;
275+ if ( ! batchId || typeof batchId !== "string" ) {
276+ throw rpcErrors . invalidParams ( "Missing or invalid batchId" ) ;
277+ }
278+
279+ await processShowCallsStatus ( batchId ) ;
280+ res . result = true ;
281+ }
282+
243283 return createScaffoldMiddleware ( {
244284 // account lookups
245285 eth_accounts : createAsyncMiddleware ( lookupAccounts ) ,
@@ -256,8 +296,9 @@ export function createWalletMiddleware({
256296 eth_signTypedData_v4 : createAsyncMiddleware ( signTypedDataV4 ) ,
257297 personal_sign : createAsyncMiddleware ( personalSign ) ,
258298 // EIP-5792
259- wallet_getCapabilities : createAsyncMiddleware ( getWalletCapabilitiesMiddleware ) , // EIP-5792: Wallet Capabilities
260- wallet_sendCalls : createAsyncMiddleware ( walletSendCallsMiddleware ) , // EIP-5792: Send Batch Calls
261- wallet_getCallsStatus : createAsyncMiddleware ( walletBatchCallStatusMiddleware ) , // EIP-5792: Batch Call Status
299+ wallet_getCapabilities : createAsyncMiddleware ( getWalletCapabilitiesMiddleware ) ,
300+ wallet_sendCalls : createAsyncMiddleware ( walletSendCallsMiddleware ) ,
301+ wallet_getCallsStatus : createAsyncMiddleware ( walletBatchCallStatusMiddleware ) ,
302+ wallet_showCallsStatus : createAsyncMiddleware ( walletShowCallsStatusMiddleware ) ,
262303 } ) ;
263304}
0 commit comments