diff --git a/backend/src/ipfs.ts b/backend/src/ipfs.ts index 646b3bdc..3f840e68 100644 --- a/backend/src/ipfs.ts +++ b/backend/src/ipfs.ts @@ -7,9 +7,12 @@ dotenv.config(); interface Proposal { text: string; address: string; + starknet_id?: string; } -const PINATA_JWT = process.env.PINATA_JWT || ""; +const ADDRES_REGEX: RegExp = /^0x0[0-9a-fA-F]{63}$/; +const MAX_LENGTH: number = 200; +const PINATA_JWT: string = process.env.PINATA_JWT || ""; const pinToIPFS = async (proposal: Proposal) => { const url = "https://api.pinata.cloud/pinning/pinFileToIPFS"; @@ -34,22 +37,32 @@ const pinToIPFS = async (proposal: Proposal) => { }; export const submitProposal = async (req: Request, res: Response) => { - const { text, address } = req.body; + const { text, address }: { text: string; address: string } = req.body; if (!text || !address) { return res.status(400).json({ error: "Missing text or address" }); } + if (text.length > MAX_LENGTH) { + return res.status(400).json({ + error: `Text exceeds maximum length of ${MAX_LENGTH} characters` + }); + } + + if (!ADDRES_REGEX.test(address)) { + return res.status(400).json({ error: "Invalid address" }); + } + const proposalData: Proposal = { text, address }; - // const starknetId = await getStarknetId(address); + const starknetId = await getStarknetId(address); - // if (starknetId) { - // proposalData.starknet_id = starknetId; - // } + if (starknetId) { + proposalData.starknet_id = starknetId; + } try { const pin = await pinToIPFS(proposalData); diff --git a/backend/src/starknet.ts b/backend/src/starknet.ts index fd1d46a7..62671bb9 100644 --- a/backend/src/starknet.ts +++ b/backend/src/starknet.ts @@ -1,11 +1,17 @@ import { StarknetIdNavigator } from "starknetid.js"; import { RpcProvider, constants } from "starknet"; +import * as dotenv from "dotenv"; + +dotenv.config(); + +const NODE_URL: string = process.env.NODE_URL || ""; export const getStarknetId = async ( address: string ): Promise => { const provider = new RpcProvider({ - nodeUrl: "your_mainnet_node_url", + nodeUrl: NODE_URL, + chainId: constants.StarknetChainId.SN_MAIN }); const starknetIdNavigator = new StarknetIdNavigator( @@ -13,7 +19,7 @@ export const getStarknetId = async ( constants.StarknetChainId.SN_MAIN ); - const domain = await provider.getStarkName(address); + const domain = await starknetIdNavigator.getStarkName(address); const id = await starknetIdNavigator.getStarknetId(domain); return id;