-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add send raw transaction & user operation endpoints (#350)
* Add send raw transaction & user operation endpoints * Reset * Update * Update * Update
- Loading branch information
Showing
7 changed files
with
211 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { Static, Type } from "@sinclair/typebox"; | ||
import { FastifyInstance } from "fastify"; | ||
import { StatusCodes } from "http-status-codes"; | ||
import { getSdk } from "../../../utils/cache/getSdk"; | ||
import { standardResponseSchema } from "../../schemas/sharedApiSchemas"; | ||
import { getChainIdFromChain } from "../../utils/chain"; | ||
|
||
const ParamsSchema = Type.Object({ | ||
chain: Type.String(), | ||
}); | ||
|
||
const BodySchema = Type.Object({ | ||
signedTransaction: Type.String(), | ||
}); | ||
|
||
const ReplySchema = Type.Object({ | ||
result: Type.Object({ | ||
transactionHash: Type.String(), | ||
}), | ||
}); | ||
|
||
export async function sendSignedTransaction(fastify: FastifyInstance) { | ||
fastify.route<{ | ||
Params: Static<typeof ParamsSchema>; | ||
Body: Static<typeof BodySchema>; | ||
Reply: Static<typeof ReplySchema>; | ||
}>({ | ||
method: "POST", | ||
url: "/transaction/:chain/send-signed-transaction", | ||
schema: { | ||
summary: "Send a signed transaction", | ||
description: "Send a signed transaction", | ||
tags: ["Transaction"], | ||
operationId: "sendRawTransaction", | ||
params: ParamsSchema, | ||
body: BodySchema, | ||
response: { | ||
...standardResponseSchema, | ||
[StatusCodes.OK]: ReplySchema, | ||
}, | ||
}, | ||
handler: async (req, res) => { | ||
const { chain } = req.params; | ||
const { signedTransaction } = req.body; | ||
const chainId = await getChainIdFromChain(chain); | ||
const sdk = await getSdk({ chainId }); | ||
|
||
const txRes = await sdk.getProvider().sendTransaction(signedTransaction); | ||
|
||
res.status(200).send({ | ||
result: { | ||
transactionHash: txRes.hash, | ||
}, | ||
}); | ||
}, | ||
}); | ||
} |
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 { Static, Type } from "@sinclair/typebox"; | ||
import { Value } from "@sinclair/typebox/value"; | ||
import { FastifyInstance } from "fastify"; | ||
import { StatusCodes } from "http-status-codes"; | ||
import { deriveClientId } from "../../../utils/api-keys"; | ||
import { env } from "../../../utils/env"; | ||
import { standardResponseSchema } from "../../schemas/sharedApiSchemas"; | ||
import { getChainIdFromChain } from "../../utils/chain"; | ||
|
||
const UserOp = Type.Object({ | ||
sender: Type.String(), | ||
nonce: Type.String(), | ||
initCode: Type.String(), | ||
callData: Type.String(), | ||
callGasLimit: Type.String(), | ||
verificationGasLimit: Type.String(), | ||
preVerificationGas: Type.String(), | ||
maxFeePerGas: Type.String(), | ||
maxPriorityFeePerGas: Type.String(), | ||
paymasterAndData: Type.String(), | ||
signature: Type.String(), | ||
}); | ||
|
||
const UserOpString = Type.Transform(Type.String()) | ||
.Decode((signedUserOp) => JSON.parse(signedUserOp) as Static<typeof UserOp>) | ||
.Encode((userOp) => JSON.stringify(userOp)); | ||
|
||
const ParamsSchema = Type.Object({ | ||
chain: Type.String(), | ||
}); | ||
|
||
const BodySchema = Type.Object({ | ||
// signedUserOp: Type.Union([UserOpString, UserOp]), | ||
signedUserOp: Type.Any(), | ||
}); | ||
|
||
const ReplySchema = Type.Union([ | ||
Type.Object({ | ||
result: Type.Object({ | ||
userOpHash: Type.String(), | ||
}), | ||
}), | ||
Type.Object({ | ||
error: Type.Object({ | ||
message: Type.String(), | ||
}), | ||
}), | ||
]); | ||
|
||
type RpcResponse = | ||
| { | ||
result: string; | ||
error: undefined; | ||
} | ||
| { | ||
result: undefined; | ||
error: { | ||
message: string; | ||
}; | ||
}; | ||
|
||
export async function sendSignedUserOp(fastify: FastifyInstance) { | ||
fastify.route<{ | ||
Params: Static<typeof ParamsSchema>; | ||
Body: Static<typeof BodySchema>; | ||
Reply: Static<typeof ReplySchema>; | ||
}>({ | ||
method: "POST", | ||
url: "/transaction/:chain/send-signed-user-op", | ||
schema: { | ||
summary: "Send a signed user operation", | ||
description: "Send a signed user operation", | ||
tags: ["Transaction"], | ||
operationId: "sendSignedUserOp", | ||
params: ParamsSchema, | ||
body: BodySchema, | ||
response: { | ||
...standardResponseSchema, | ||
[StatusCodes.OK]: ReplySchema, | ||
}, | ||
}, | ||
handler: async (req, res) => { | ||
const { chain } = req.params; | ||
const { signedUserOp } = req.body; | ||
const chainId = await getChainIdFromChain(chain); | ||
|
||
let userOp: Static<typeof UserOp>; | ||
if (typeof signedUserOp === "string") { | ||
try { | ||
userOp = Value.Decode(UserOpString, signedUserOp); | ||
} catch (err: any) { | ||
return res.status(400).send({ | ||
error: { | ||
message: `Invalid signed user operation. - ${err.message || err}`, | ||
}, | ||
}); | ||
} | ||
} else { | ||
userOp = signedUserOp; | ||
} | ||
|
||
const entryPointAddress = "0x5FF137D4b0FDCD49DcA30c7CF57E578a026d2789"; | ||
const userOpRes = await fetch(`https://${chainId}.bundler.thirdweb.com`, { | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
"x-client-id": deriveClientId(env.THIRDWEB_API_SECRET_KEY), | ||
"x-secret-key": env.THIRDWEB_API_SECRET_KEY, | ||
}, | ||
body: JSON.stringify({ | ||
id: 1, | ||
jsonrpc: "2.0", | ||
method: "eth_sendUserOperation", | ||
params: [userOp, entryPointAddress], | ||
}), | ||
}); | ||
|
||
const { result: userOpHash, error } = | ||
(await userOpRes.json()) as RpcResponse; | ||
|
||
if (error) { | ||
return res.status(400).send({ | ||
error: { | ||
message: `Failed to send - ${error.message || error}`, | ||
}, | ||
}); | ||
} | ||
|
||
return res.status(200).send({ | ||
result: { | ||
userOpHash, | ||
}, | ||
}); | ||
}, | ||
}); | ||
} |
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,7 @@ | ||
import { sha256HexSync } from "@thirdweb-dev/crypto"; | ||
|
||
export const deriveClientId = (secretKey: string): string => { | ||
const hashedSecretKey = sha256HexSync(secretKey); | ||
const derivedClientId = hashedSecretKey.slice(0, 32); | ||
return derivedClientId; | ||
}; |
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