|
| 1 | +import { Type } from "@fastify/type-provider-typebox"; |
| 2 | +import { verifyTypedData, zeroAddress } from "viem"; |
| 3 | + |
| 4 | +import { |
| 5 | + Endpoint, |
| 6 | + ErrorResponse, |
| 7 | + FastifyReplyTypeBox, |
| 8 | + FastifyRequestTypeBox, |
| 9 | +} from "../../utils"; |
| 10 | +import { ChainMetadataEthereumVm, getChain } from "../../../common/chains"; |
| 11 | +import { externalError } from "../../../common/error"; |
| 12 | +import { saveNonceMapping } from "../../../models/nonce-mappings"; |
| 13 | + |
| 14 | +const Schema = { |
| 15 | + body: Type.Object({ |
| 16 | + walletChainId: Type.String({ |
| 17 | + description: "The chain id of the wallet", |
| 18 | + }), |
| 19 | + wallet: Type.String({ |
| 20 | + description: "The wallet address", |
| 21 | + }), |
| 22 | + nonce: Type.String({ |
| 23 | + description: "The nonce to associate the id to", |
| 24 | + }), |
| 25 | + id: Type.String({ |
| 26 | + description: "The id to associate the nonce to", |
| 27 | + }), |
| 28 | + signatureChainId: Type.String({ |
| 29 | + description: "The chain id of the signature", |
| 30 | + }), |
| 31 | + signature: Type.String({ |
| 32 | + description: "The signature for the mapping", |
| 33 | + }), |
| 34 | + }), |
| 35 | + response: { |
| 36 | + 200: Type.Object({ |
| 37 | + message: Type.String({ description: "Success message" }), |
| 38 | + }), |
| 39 | + ...ErrorResponse, |
| 40 | + }, |
| 41 | +}; |
| 42 | + |
| 43 | +export default { |
| 44 | + method: "POST", |
| 45 | + url: "/actions/nonce-mappings/v1", |
| 46 | + schema: Schema, |
| 47 | + handler: async ( |
| 48 | + req: FastifyRequestTypeBox<typeof Schema>, |
| 49 | + reply: FastifyReplyTypeBox<typeof Schema> |
| 50 | + ) => { |
| 51 | + const { walletChainId, wallet, nonce, id, signatureChainId, signature } = |
| 52 | + req.body; |
| 53 | + |
| 54 | + const NONCE_MAPPING_DOMAIN = (chainId: number) => ({ |
| 55 | + name: "RelayNonceMapping", |
| 56 | + version: "1", |
| 57 | + chainId, |
| 58 | + verifyingContract: zeroAddress, |
| 59 | + }); |
| 60 | + |
| 61 | + const NONCE_MAPPING_TYPES = { |
| 62 | + NonceMapping: [ |
| 63 | + { name: "wallet", type: "address" }, |
| 64 | + { name: "id", type: "bytes32" }, |
| 65 | + { name: "nonce", type: "uint256" }, |
| 66 | + ], |
| 67 | + }; |
| 68 | + |
| 69 | + const message = { |
| 70 | + wallet: wallet as `0x${string}`, |
| 71 | + id: id as `0x${string}`, |
| 72 | + nonce: BigInt(nonce), |
| 73 | + }; |
| 74 | + |
| 75 | + const signatureChain = await getChain(signatureChainId); |
| 76 | + if (signatureChain.vmType !== "ethereum-vm") { |
| 77 | + throw externalError("Unsupported signature chain", "INVALID_SIGNATURE"); |
| 78 | + } |
| 79 | + |
| 80 | + const isValidSignature = await verifyTypedData({ |
| 81 | + address: wallet as `0x${string}`, |
| 82 | + domain: NONCE_MAPPING_DOMAIN( |
| 83 | + (signatureChain.metadata as ChainMetadataEthereumVm).chainId |
| 84 | + ), |
| 85 | + types: NONCE_MAPPING_TYPES, |
| 86 | + primaryType: "NonceMapping", |
| 87 | + message, |
| 88 | + signature: signature as `0x${string}`, |
| 89 | + }); |
| 90 | + if (!isValidSignature) { |
| 91 | + throw externalError("Invalid signature", "INVALID_SIGNATURE"); |
| 92 | + } |
| 93 | + |
| 94 | + const saveResult = await saveNonceMapping({ |
| 95 | + walletChainId, |
| 96 | + wallet, |
| 97 | + nonce, |
| 98 | + id, |
| 99 | + signatureChainId, |
| 100 | + signature, |
| 101 | + }); |
| 102 | + if (!saveResult) { |
| 103 | + throw externalError( |
| 104 | + "Nonce mapping already exists", |
| 105 | + "NONCE_MAPPING_ALREADY_EXISTS" |
| 106 | + ); |
| 107 | + } |
| 108 | + |
| 109 | + return reply.status(200).send({ message: "Success" }); |
| 110 | + }, |
| 111 | +} as Endpoint; |
0 commit comments