Skip to content

Commit

Permalink
feat: implement starknet.id retrieval
Browse files Browse the repository at this point in the history
- add type assertions and validations to `text` and `address`
  • Loading branch information
Mystic-Nayy committed Jun 2, 2024
1 parent 1b36c3e commit a8b4db9
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
25 changes: 19 additions & 6 deletions backend/src/ipfs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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);
Expand Down
10 changes: 8 additions & 2 deletions backend/src/starknet.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
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<string | null> => {
const provider = new RpcProvider({
nodeUrl: "your_mainnet_node_url",
nodeUrl: NODE_URL,
chainId: constants.StarknetChainId.SN_MAIN
});

const starknetIdNavigator = new StarknetIdNavigator(
provider,
constants.StarknetChainId.SN_MAIN
);

const domain = await provider.getStarkName(address);
const domain = await starknetIdNavigator.getStarkName(address);
const id = await starknetIdNavigator.getStarknetId(domain);

return id;
Expand Down

0 comments on commit a8b4db9

Please sign in to comment.