diff --git a/bot/modules/starknet.js b/bot/modules/starknet.js index 8961a98..19501ce 100644 --- a/bot/modules/starknet.js +++ b/bot/modules/starknet.js @@ -1,6 +1,16 @@ import "dotenv/config"; import { Provider, Contract, Account, cairo, CallData } from "starknet"; +import { num } from "starknet"; + +// Function to encode domain parts +const useEncoded = (str) => { + if (!str) return num.toBigInt(0); + const encoded = Buffer.from(str.toLowerCase()) + .reduce((acc, byte) => acc + byte.toString(16).padStart(2, "0"), "0x"); + return num.toBigInt(encoded); +}; + //initialize Provider const provider = new Provider({ nodeUrl: process.env.RPC_URL, @@ -59,10 +69,27 @@ const mintToUser = async (calldata) => { const getAddressFromDomain = async (domainName) => { try { - const res = await provider.getAddressFromStarkName(domainName); - return res; + if (domainName.endsWith('.stark')) { + const res = await provider.getAddressFromStarkName(domainName); + return res; + } else if (domainName.endsWith('.brother')) { + const encodedDomain = domainName + .replace('.brother', '') + .split('.') + .map((part) => useEncoded(part).toString(10)); + + const domainDetails = await provider.callContract({ + contractAddress: process.env.BROTHER_CONTRACT, + entrypoint: 'get_details_by_domain', + calldata: CallData.compile({ domain: encodedDomain }), + }); + + // The contract returns a DomainDetails struct where resolver is the second field + return domainDetails[1]; + } + return null; } catch (err) { - console.log(`Error fetching address for domain ${domainName}`); + console.log(`Error fetching address for domain ${domainName}:`, err); return null; } }; diff --git a/bot/utils/index.js b/bot/utils/index.js index c558a26..1af7a2b 100644 --- a/bot/utils/index.js +++ b/bot/utils/index.js @@ -22,7 +22,7 @@ export const checkForAddressFromTweet = (tweet) => { }; export const checkDomainFromTweet = async (tweet) => { - const regex = /\.stark$/; + const regex = /\.(stark|brother)$/; // split words and check let wordIndex = 0; @@ -32,7 +32,7 @@ export const checkDomainFromTweet = async (tweet) => { let numOfWords = formattedText.split(" ").length; while (wordIndex < numOfWords) { const word = formattedText.split(" ")[wordIndex]; - if (regex.test(word) && word !== ".stark") { + if (regex.test(word) && word !== ".stark" && word !== ".brother") { currentAddress = await getAddressFromDomain(word?.toLowerCase()); } wordIndex++;