diff --git a/functions/AppendWhitelist.js b/functions/AppendWhitelist.js index fdb6c5c..06f9908 100644 --- a/functions/AppendWhitelist.js +++ b/functions/AppendWhitelist.js @@ -1,95 +1,111 @@ -import {MerkleTree} from "merkletreejs"; +import { MerkleTree } from "merkletreejs"; import keccak256 from "keccak256"; import { deploy_nft_abi } from "../abi.js"; import { initializeApp } from "firebase/app"; import { ethers } from "ethers"; -import { getFirestore, collection, query, getDocs, where, setDoc, doc } from 'firebase/firestore/lite'; +import { + getFirestore, + collection, + query, + getDocs, + where, + setDoc, + doc, +} from "firebase/firestore/lite"; import { GetProvider } from "./GetProvider.js"; const firebaseConfig = { - apiKey: process.env.fb_key, - authDomain: process.env.authDomain, - projectId: process.env.projectId, - storageBucket: process.env.storageBucket, - messagingSenderId: process.env.messagingSenderId, - appId: process.env.appId, - measurementId: process.env.measurementId + apiKey: process.env.fb_key, + authDomain: process.env.authDomain, + projectId: process.env.projectId, + storageBucket: process.env.storageBucket, + messagingSenderId: process.env.messagingSenderId, + appId: process.env.appId, + measurementId: process.env.measurementId, }; const fb = initializeApp(firebaseConfig); const db = getFirestore(fb); export const AppendWhitelist = async (req) => { - const contract = req.query.contract; - const addresses = req.query.wallets.split(','); - const network = req.query.network; - const sender = req.query.wallet; + const contract = req.query.contract; + const addresses = req.query.wallets.split(","); + const network = req.query.network; + const sender = req.query.wallet; - const provider = GetProvider(network); + const provider = GetProvider(network); - const code = network === 'goerli' ? await provider.getCode(contract) : network === 'mainnet' ? await provider.getCode(contract) : 'invalid'; + const code = + network === "goerli" + ? await provider.getCode(contract) + : network === "mainnet" + ? await provider.getCode(contract) + : "invalid"; - if(code === 'invalid') { - throw('Network was not specified'); - } - if(code === '0x') { - throw('Address entered is not a contract address'); - } + if (code === "invalid") { + throw "Network was not specified"; + } + if (code === "0x") { + throw "Address entered is not a contract address"; + } - const NFTContract = new ethers.Contract(contract, deploy_nft_abi, provider); - const isAdmin = await NFTContract.isAdmin(process.env.wallet_address); - const owner = await NFTContract.owner(); - console.log(owner); - console.log(sender); - console.log(isAdmin); + const NFTContract = new ethers.Contract(contract, deploy_nft_abi, provider); + const isAdmin = await NFTContract.isAdmin(process.env.wallet_address); + const owner = await NFTContract.owner(); + console.log(owner); + console.log(sender); + console.log(isAdmin); - if(!isAdmin) { - throw('We are not authorized to update this contract'); - } - if(owner.toLowerCase() !== sender.toLowerCase()) { - throw('Sender is not the contract owner'); - } + if (!isAdmin) { + throw "We are not authorized to update this contract"; + } + if (owner.toLowerCase() !== sender.toLowerCase()) { + throw "Sender is not the contract owner"; + } - const wlRef = collection(db, 'whitelists'); - const q = query(collection(db, "whitelists"), where("contract", "==", contract)); - const wlSnapshot = await getDocs(q); - - let whitelist; - if(wlSnapshot.docs.length === 0) { - //wlRef = doc(db, 'whitelists', contract) - await setDoc(doc(wlRef, contract), { - addresses: addresses, - contract: contract - }); - - whitelist = addresses; - } else { - whitelist = wlSnapshot.docs[0].data().addresses; - for(let i = 0; i < addresses.length; i++) { - if(!whitelist.includes(addresses[i])){ - whitelist.push(addresses[i]); - } - } - - await setDoc(doc(wlRef, contract), { - addresses: whitelist, - contract: contract - }); - } + const wlRef = collection(db, "whitelists"); + const q = query( + collection(db, "whitelists"), + where("contract", "==", contract) + ); + const wlSnapshot = await getDocs(q); - const leafNodes = whitelist.map(addr => keccak256(addr)); - const merkleTree = new MerkleTree(leafNodes, keccak256, {sortPairs: true}); - const root = merkleTree.getHexRoot(); + let whitelist; + if (wlSnapshot.docs.length === 0) { + //wlRef = doc(db, 'whitelists', contract) + await setDoc(doc(wlRef, contract), { + addresses: addresses, + contract: contract, + }); - const result = { - inputs: {contract: contract}, - output: {data: root}, - success: true + whitelist = addresses; + } else { + whitelist = wlSnapshot.docs[0].data().addresses; + for (let i = 0; i < addresses.length; i++) { + if (!whitelist.includes(addresses[i])) { + whitelist.push(addresses[i]); + } } - - const signer = new ethers.Wallet(process.env.PRIVATE_KEY, provider); - const tx = await NFTContract.connect(signer).setALRoot(result.output.data); - result.output.tx = tx.hash; - return result; -} \ No newline at end of file + await setDoc(doc(wlRef, contract), { + addresses: whitelist, + contract: contract, + }); + } + + const leafNodes = whitelist.map((addr) => keccak256(addr)); + const merkleTree = new MerkleTree(leafNodes, keccak256, { sortPairs: true }); + const root = merkleTree.getHexRoot(); + + const result = { + inputs: { contract: contract }, + output: { data: root }, + success: true, + }; + + const signer = new ethers.Wallet(process.env.PRIVATE_KEY, provider); + + const tx = await NFTContract.connect(signer).setALRoot(result.output.data); + result.output.tx = tx.hash; + return result; +}; diff --git a/functions/ContractOwners.js b/functions/ContractOwners.js index 6276de4..65d6de9 100644 --- a/functions/ContractOwners.js +++ b/functions/ContractOwners.js @@ -1,21 +1,21 @@ import { Network, Alchemy } from "alchemy-sdk"; const settings = { - apiKey: process.env.ALCHEMY_KEY, - network: Network.ETH_MAINNET + apiKey: process.env.ALCHEMY_KEY, + network: Network.ETH_MAINNET, }; - + const alchemy = new Alchemy(settings); export const ContractOwners = async (req) => { - const contract = req.query.contract; + const contract = req.query.contract; - const owners = await alchemy.nft.getOwnersForContract(contract); + const owners = await alchemy.nft.getOwnersForContract(contract); - const result = { - inputs: {contract: contract}, - output: {data: owners}, - success: true - } + const result = { + inputs: { contract: contract }, + output: { data: owners }, + success: true, + }; - return result; -} \ No newline at end of file + return result; +}; diff --git a/functions/DeployNFT.js b/functions/DeployNFT.js index 8ee939d..a6bf052 100644 --- a/functions/DeployNFT.js +++ b/functions/DeployNFT.js @@ -1,88 +1,111 @@ -import { ethers } from 'ethers'; -import {deploy_nft_abi, nft_bytecode} from '../abi.js'; -import dotenv from 'dotenv' -import { GetProvider } from './GetProvider.js'; +import { ethers } from "ethers"; +import { deploy_nft_abi, nft_bytecode } from "../abi.js"; +import dotenv from "dotenv"; +import { GetProvider } from "./GetProvider.js"; dotenv.config(); -import { getFirestore, collection, query, getDocs, where, setDoc, doc } from 'firebase/firestore/lite'; +import { + getFirestore, + collection, + query, + getDocs, + where, + setDoc, + doc, +} from "firebase/firestore/lite"; import { initializeApp } from "firebase/app"; const firebaseConfig = { - apiKey: process.env.fb_key, - authDomain: process.env.authDomain, - projectId: process.env.projectId, - storageBucket: process.env.storageBucket, - messagingSenderId: process.env.messagingSenderId, - appId: process.env.appId, - measurementId: process.env.measurementId + apiKey: process.env.fb_key, + authDomain: process.env.authDomain, + projectId: process.env.projectId, + storageBucket: process.env.storageBucket, + messagingSenderId: process.env.messagingSenderId, + appId: process.env.appId, + measurementId: process.env.measurementId, }; const fb = initializeApp(firebaseConfig); const db = getFirestore(fb); export const DeployNFT = async (req) => { - let wallet = req.query.wallet; - const network = req.query.network - - const provider = GetProvider(network); - - if(wallet === '' || wallet === undefined) { - throw('No wallet address sent'); - } - - wallet = wallet.toLowerCase(); - - const SourceNFT = new ethers.Contract('0x933F6088681F5DCEB1636c839Ff75F4071D52132', deploy_nft_abi, provider); - const bal = await SourceNFT.balanceOf(wallet); - - if(parseInt(bal) === 0) { - throw ('Must own an AGLD NFT first'); - } - - const name = req.query.name; - const symbol = req.query.symbol; - const maxSupply = req.query.maxSupply; - const price = req.query.price; - const whitelist_price = req.query.whitelist_price; - - if(!name || !symbol || !maxSupply || !price || !whitelist_price) { - throw ('Please send values for: name, symbol, maxSupply, price, whitelist_price'); - } - - const signer = new ethers.Wallet(process.env.PRIVATE_KEY, provider); - - const NFT_Factory = new ethers.ContractFactory(deploy_nft_abi, nft_bytecode, signer); - const deployed_nft = await NFT_Factory.connect(signer).deploy(name, symbol, maxSupply, price, whitelist_price); - - await deployed_nft.deployed(); - - const transfer_ownership = await deployed_nft.connect(signer).transferOwnership(wallet); - await transfer_ownership.wait(1); - - const result = { - inputs: {wallet: wallet}, - output: {data: deployed_nft.address}, - success: true - } - - const userRef = collection(db, 'users'); - const q = query(userRef, where('wallet', '==', wallet)); - const userSnapshot = await getDocs(q); - - if(userSnapshot.docs.length === 0) { - await setDoc(doc(userRef, wallet), { - owned_contracts: [deployed_nft.address], - wallet: wallet - }); - - } else { - let contracts = userSnapshot.docs[0].data().owned_contracts; - contracts.push(deployed_nft.address); - - await setDoc(doc(userRef, wallet), { - owned_contracts: contracts, - wallet: wallet - }); - } - - return result; -} \ No newline at end of file + let wallet = req.query.wallet; + const network = req.query.network; + + const provider = GetProvider(network); + + if (wallet === "" || wallet === undefined) { + throw "No wallet address sent"; + } + + wallet = wallet.toLowerCase(); + + const SourceNFT = new ethers.Contract( + "0x933F6088681F5DCEB1636c839Ff75F4071D52132", + deploy_nft_abi, + provider + ); + const bal = await SourceNFT.balanceOf(wallet); + + if (parseInt(bal) === 0) { + throw "Must own an AGLD NFT first"; + } + + const name = req.query.name; + const symbol = req.query.symbol; + const maxSupply = req.query.maxSupply; + const price = req.query.price; + const whitelist_price = req.query.whitelist_price; + + if (!name || !symbol || !maxSupply || !price || !whitelist_price) { + throw "Please send values for: name, symbol, maxSupply, price, whitelist_price"; + } + + const signer = new ethers.Wallet(process.env.PRIVATE_KEY, provider); + + const NFT_Factory = new ethers.ContractFactory( + deploy_nft_abi, + nft_bytecode, + signer + ); + const deployed_nft = await NFT_Factory.connect(signer).deploy( + name, + symbol, + maxSupply, + price, + whitelist_price + ); + + await deployed_nft.deployed(); + + const transfer_ownership = await deployed_nft + .connect(signer) + .transferOwnership(wallet); + await transfer_ownership.wait(1); + + const result = { + inputs: { wallet: wallet }, + output: { data: deployed_nft.address }, + success: true, + }; + + const userRef = collection(db, "users"); + const q = query(userRef, where("wallet", "==", wallet)); + const userSnapshot = await getDocs(q); + + if (userSnapshot.docs.length === 0) { + await setDoc(doc(userRef, wallet), { + owned_contracts: [deployed_nft.address], + wallet: wallet, + }); + } else { + let contracts = userSnapshot.docs[0].data().owned_contracts; + contracts.push(deployed_nft.address); + + await setDoc(doc(userRef, wallet), { + owned_contracts: contracts, + wallet: wallet, + }); + } + + return result; +}; diff --git a/functions/ETHBalance.js b/functions/ETHBalance.js index 1ac6310..9859a3a 100644 --- a/functions/ETHBalance.js +++ b/functions/ETHBalance.js @@ -1,27 +1,26 @@ -import { ethers } from "ethers"; import { GetProvider } from "./GetProvider.js"; export const ETHBalance = async (req) => { - const network = req.query.network; - const wallet = req.query.wallet; - - const provider = GetProvider(network); + const network = req.query.network; + const wallet = req.query.wallet; - if(provider === 'invalid') { - throw('Network was not specified') - } + const provider = GetProvider(network); - if(wallet === "" || wallet === undefined) { - throw("Invalid or empty wallet address"); - } + if (provider === "invalid") { + throw "Network was not specified"; + } - const balance = await provider.getBalance(wallet) + if (wallet === "" || wallet === undefined) { + throw "Invalid or empty wallet address"; + } - const result = { - inputs: {wallet: wallet}, - output: {data: (parseInt(balance) / 10**18).toFixed(6)}, - success: true - } + const balance = await provider.getBalance(wallet); - return result; -} \ No newline at end of file + const result = { + inputs: { wallet: wallet }, + output: { data: (parseInt(balance) / 10 ** 18).toFixed(6) }, + success: true, + }; + + return result; +}; diff --git a/functions/GetEncodedParams.js b/functions/GetEncodedParams.js index e096f49..8417e07 100644 --- a/functions/GetEncodedParams.js +++ b/functions/GetEncodedParams.js @@ -1,21 +1,21 @@ -import { ethers } from 'ethers'; +import { ethers } from "ethers"; export const GetEncodedParams = async (req) => { - const abiCoder = new ethers.utils.AbiCoder(); + const abiCoder = new ethers.utils.AbiCoder(); - const types = req.query.types.split(','); - const values = req.query.values.split(','); + const types = req.query.types.split(","); + const values = req.query.values.split(","); - console.log(types, values); + console.log(types, values); - let data = abiCoder.encode(types, values); - data = data.slice(2,data.length); + let data = abiCoder.encode(types, values); + data = data.slice(2, data.length); - const result = { - inputs: {types: types, values: values}, - output: {data: data}, - success: true - } + const result = { + inputs: { types: types, values: values }, + output: { data: data }, + success: true, + }; - return result; -} \ No newline at end of file + return result; +}; diff --git a/functions/GetOwnedContracts.js b/functions/GetOwnedContracts.js index 091e360..e25d75e 100644 --- a/functions/GetOwnedContracts.js +++ b/functions/GetOwnedContracts.js @@ -1,44 +1,56 @@ -import dotenv from 'dotenv' +import dotenv from "dotenv"; dotenv.config(); -import { getFirestore, collection, query, getDocs, where, setDoc, doc } from 'firebase/firestore/lite'; +import { + getFirestore, + collection, + query, + getDocs, + where, + setDoc, + doc, +} from "firebase/firestore/lite"; import { initializeApp } from "firebase/app"; const firebaseConfig = { - apiKey: process.env.fb_key, - authDomain: process.env.authDomain, - projectId: process.env.projectId, - storageBucket: process.env.storageBucket, - messagingSenderId: process.env.messagingSenderId, - appId: process.env.appId, - measurementId: process.env.measurementId + apiKey: process.env.fb_key, + authDomain: process.env.authDomain, + projectId: process.env.projectId, + storageBucket: process.env.storageBucket, + messagingSenderId: process.env.messagingSenderId, + appId: process.env.appId, + measurementId: process.env.measurementId, }; const fb = initializeApp(firebaseConfig); const db = getFirestore(fb); export const GetOwnedContracts = async (req) => { - const wallet = req.query.wallet; + const wallet = req.query.wallet; - if(wallet === '' || wallet === undefined) { - throw('No wallet address sent'); - } - - let owned_contracts = []; + if (wallet === "" || wallet === undefined) { + throw "No wallet address sent"; + } - const userRef = collection(db, 'users'); - const q = query(userRef, where('wallet', '==', wallet.toLowerCase())); - const userSnapshot = await getDocs(q); + let owned_contracts = []; - if(userSnapshot.docs.length !== 0) { - for(let i = 0; i < userSnapshot.docs[0].data().owned_contracts.length; i++) { - owned_contracts.push(userSnapshot.docs[0].data().owned_contracts[i]); - } - } + const userRef = collection(db, "users"); + const q = query(userRef, where("wallet", "==", wallet.toLowerCase())); + const userSnapshot = await getDocs(q); - const result = { - inputs: {wallet: wallet}, - output: {data: owned_contracts}, - success: true + if (userSnapshot.docs.length !== 0) { + for ( + let i = 0; + i < userSnapshot.docs[0].data().owned_contracts.length; + i++ + ) { + owned_contracts.push(userSnapshot.docs[0].data().owned_contracts[i]); } - return result; -} \ No newline at end of file + } + + const result = { + inputs: { wallet: wallet }, + output: { data: owned_contracts }, + success: true, + }; + return result; +}; diff --git a/functions/GetProvider.js b/functions/GetProvider.js index 30469b3..d8bab2e 100644 --- a/functions/GetProvider.js +++ b/functions/GetProvider.js @@ -1,5 +1,9 @@ -import { ethers } from "ethers" +import { ethers } from "ethers"; export const GetProvider = (network) => { - return network === 'goerli' ? new ethers.providers.JsonRpcProvider(process.env.RPC_GOERLI) : network === 'mainnet' ? new ethers.providers.JsonRpcProvider(process.env.RPC_URL) : 'invalid'; -} \ No newline at end of file + return network === "goerli" + ? new ethers.providers.JsonRpcProvider(process.env.RPC_GOERLI) + : network === "mainnet" + ? new ethers.providers.JsonRpcProvider(process.env.RPC_URL) + : "invalid"; +}; diff --git a/functions/GetRelayNonce.js b/functions/GetRelayNonce.js index 313da6b..a4a5e9c 100644 --- a/functions/GetRelayNonce.js +++ b/functions/GetRelayNonce.js @@ -1,22 +1,22 @@ -import { ethers } from 'ethers'; -import { relayer_abi } from '../abi.js'; -import dotenv from 'dotenv' -import { GetProvider } from './GetProvider.js'; +import { ethers } from "ethers"; +import { relayer_abi } from "../abi.js"; +import dotenv from "dotenv"; +import { GetProvider } from "./GetProvider.js"; dotenv.config(); export const GetRelayNonce = async (req) => { - const wallet = req.query.wallet; - const relayer = '0x2A0d1f0EE9c5584b1694BCa16879423432770A52'; - const network = req.query.network - const provider = GetProvider(network); - const relay = new ethers.Contract(relayer, relayer_abi, provider); + const wallet = req.query.wallet; + const relayer = "0x2A0d1f0EE9c5584b1694BCa16879423432770A52"; + const network = req.query.network; + const provider = GetProvider(network); + const relay = new ethers.Contract(relayer, relayer_abi, provider); - const nonce = await relay.getNonce(wallet); - const result = { - inputs: {wallet: wallet}, - output: {data: parseInt(nonce)}, - success: true - } + const nonce = await relay.getNonce(wallet); + const result = { + inputs: { wallet: wallet }, + output: { data: parseInt(nonce) }, + success: true, + }; - return result; -} \ No newline at end of file + return result; +}; diff --git a/functions/GetSelector.js b/functions/GetSelector.js index 38b4412..e2ed1ea 100644 --- a/functions/GetSelector.js +++ b/functions/GetSelector.js @@ -1,23 +1,27 @@ -import { ethers } from 'ethers'; -import { selector_abi } from '../abi.js'; -import dotenv from 'dotenv' -import { GetProvider } from './GetProvider.js'; +import { ethers } from "ethers"; +import { selector_abi } from "../abi.js"; +import dotenv from "dotenv"; +import { GetProvider } from "./GetProvider.js"; dotenv.config(); export const GetSelector = async (req) => { - const network = req.query.network; - const provider = GetProvider(network); - - const selector = new ethers.Contract('0xD7dA7285f732262B3Cc80639d27c5Ee87f2e3a70', selector_abi, goerli_provider); + const network = req.query.network; + const provider = GetProvider(network); - let func = req.query.func; - const funcHash = await selector.getSelector(func); + const selector = new ethers.Contract( + "0xD7dA7285f732262B3Cc80639d27c5Ee87f2e3a70", + selector_abi, + goerli_provider + ); - const result = { - inputs: {func: func}, - output: {data: funcHash}, - success: true - } + const func = req.query.func; + const funcHash = await selector.getSelector(func); - return result; -} \ No newline at end of file + const result = { + inputs: { func: func }, + output: { data: funcHash }, + success: true, + }; + + return result; +}; diff --git a/functions/MerkleProof.js b/functions/MerkleProof.js index 99fc61c..839d5ae 100644 --- a/functions/MerkleProof.js +++ b/functions/MerkleProof.js @@ -1,15 +1,23 @@ -import {MerkleTree} from "merkletreejs"; +import { MerkleTree } from "merkletreejs"; import keccak256 from "keccak256"; import { initializeApp } from "firebase/app"; -import { getFirestore, collection, query, getDocs, where, setDoc, doc } from 'firebase/firestore/lite'; +import { + getFirestore, + collection, + query, + getDocs, + where, + setDoc, + doc, +} from "firebase/firestore/lite"; const firebaseConfig = { - apiKey: process.env.fb_key, - authDomain: process.env.authDomain, - projectId: process.env.projectId, - storageBucket: process.env.storageBucket, - messagingSenderId: process.env.messagingSenderId, - appId: process.env.appId, - measurementId: process.env.measurementId + apiKey: process.env.fb_key, + authDomain: process.env.authDomain, + projectId: process.env.projectId, + storageBucket: process.env.storageBucket, + messagingSenderId: process.env.messagingSenderId, + appId: process.env.appId, + measurementId: process.env.measurementId, }; const fb = initializeApp(firebaseConfig); @@ -17,33 +25,36 @@ const fb = initializeApp(firebaseConfig); const db = getFirestore(fb); export const MerkleProof = async (req) => { - const contract = req.query.contract; - const address = req.query.wallet; + const contract = req.query.contract; + const address = req.query.wallet; - const q = query(collection(db, "whitelists"), where("contract", "==", contract)); - const wlSnapshot = await getDocs(q); + const q = query( + collection(db, "whitelists"), + where("contract", "==", contract) + ); + const wlSnapshot = await getDocs(q); - if(wlSnapshot.docs.length === 0) { - throw('No contract found with that address'); - } + if (wlSnapshot.docs.length === 0) { + throw "No contract found with that address"; + } - let whitelist = wlSnapshot.docs[0].data().addresses; - if(!whitelist.includes(address)) { - throw('Wallet provided is not on whitelist') - } + let whitelist = wlSnapshot.docs[0].data().addresses; + if (!whitelist.includes(address)) { + throw "Wallet provided is not on whitelist"; + } - const leafNodes = whitelist.map(addr => keccak256(addr)); - const merkleTree = new MerkleTree(leafNodes, keccak256, {sortPairs: true}); - - const leaf = keccak256(address); + const leafNodes = whitelist.map((addr) => keccak256(addr)); + const merkleTree = new MerkleTree(leafNodes, keccak256, { sortPairs: true }); - const proof = merkleTree.getHexProof(leaf); + const leaf = keccak256(address); - const result = { - inputs: {contract: contract, wallet: address}, - output: {data: proof}, - success: true - } + const proof = merkleTree.getHexProof(leaf); - return result; -} \ No newline at end of file + const result = { + inputs: { contract: contract, wallet: address }, + output: { data: proof }, + success: true, + }; + + return result; +}; diff --git a/functions/MerkleRoot.js b/functions/MerkleRoot.js index 3c71b4f..d495fa0 100644 --- a/functions/MerkleRoot.js +++ b/functions/MerkleRoot.js @@ -1,42 +1,53 @@ -import {MerkleTree} from "merkletreejs"; +import { MerkleTree } from "merkletreejs"; import keccak256 from "keccak256"; import { initializeApp } from "firebase/app"; -import { getFirestore, collection, query, getDocs, where, setDoc, doc } from 'firebase/firestore/lite'; +import { + getFirestore, + collection, + query, + getDocs, + where, + setDoc, + doc, +} from "firebase/firestore/lite"; const firebaseConfig = { - apiKey: process.env.fb_key, - authDomain: process.env.authDomain, - projectId: process.env.projectId, - storageBucket: process.env.storageBucket, - messagingSenderId: process.env.messagingSenderId, - appId: process.env.appId, - measurementId: process.env.measurementId + apiKey: process.env.fb_key, + authDomain: process.env.authDomain, + projectId: process.env.projectId, + storageBucket: process.env.storageBucket, + messagingSenderId: process.env.messagingSenderId, + appId: process.env.appId, + measurementId: process.env.measurementId, }; - + const fb = initializeApp(firebaseConfig); const db = getFirestore(fb); export const MerkleRoot = async (req) => { - const contract = req.query.contract; + const contract = req.query.contract; - const q = query(collection(db, "whitelists"), where("contract", "==", contract)); - const wlSnapshot = await getDocs(q); + const q = query( + collection(db, "whitelists"), + where("contract", "==", contract) + ); + const wlSnapshot = await getDocs(q); - if(wlSnapshot.docs.length === 0) { - throw('No contract found with that address'); - } + if (wlSnapshot.docs.length === 0) { + throw "No contract found with that address"; + } - let whitelist = wlSnapshot.docs[0].data().addresses; + let whitelist = wlSnapshot.docs[0].data().addresses; - const leafNodes = whitelist.map(addr => keccak256(addr)); - const merkleTree = new MerkleTree(leafNodes, keccak256, {sortPairs: true}); - const root = merkleTree.getHexRoot(); + const leafNodes = whitelist.map((addr) => keccak256(addr)); + const merkleTree = new MerkleTree(leafNodes, keccak256, { sortPairs: true }); + const root = merkleTree.getHexRoot(); - const result = { - inputs: {contract: contract}, - output: {data: root}, - success: true - } + const result = { + inputs: { contract: contract }, + output: { data: root }, + success: true, + }; - return result; -} \ No newline at end of file + return result; +}; diff --git a/functions/NFTBalance.js b/functions/NFTBalance.js index 92f7b1c..3e778d8 100644 --- a/functions/NFTBalance.js +++ b/functions/NFTBalance.js @@ -1,40 +1,39 @@ -import { ethers } from 'ethers'; -import { nft_abi } from '../abi.js'; -import dotenv from 'dotenv' -import { GetProvider } from './GetProvider.js'; +import { ethers } from "ethers"; +import { nft_abi } from "../abi.js"; +import dotenv from "dotenv"; +import { GetProvider } from "./GetProvider.js"; dotenv.config(); -export const NFTBalance = async(req) => { - const contract = req.query.contract; - const wallet = req.query.wallet; - const network = req.query.network; - - const provider = GetProvider(network); - - let error = {errors: []}; - - if(contract === "" || contract === undefined) { - error.errors.push("Invalid or empty contract address"); - } - if(wallet === "" || wallet === undefined) { - error.errors.push("Invalid or empty wallet address"); - } - if(error.errors.length > 0) { - return { - errors: error, - success: false - }; - } - const nft = new ethers.Contract(contract, nft_abi, provider); - - const bal = await nft.balanceOf(wallet); - - const result = { - inputs: {wallet: wallet, contract: contract}, - output: {balance: parseInt(bal)}, - success: true - } - - return result; -} - +export const NFTBalance = async (req) => { + const contract = req.query.contract; + const wallet = req.query.wallet; + const network = req.query.network; + + const provider = GetProvider(network); + + const error = { errors: [] }; + + if (contract === "" || contract === undefined) { + error.errors.push("Invalid or empty contract address"); + } + if (wallet === "" || wallet === undefined) { + error.errors.push("Invalid or empty wallet address"); + } + if (error.errors.length > 0) { + return { + errors: error, + success: false, + }; + } + const nft = new ethers.Contract(contract, nft_abi, provider); + + const bal = await nft.balanceOf(wallet); + + const result = { + inputs: { wallet: wallet, contract: contract }, + output: { balance: parseInt(bal) }, + success: true, + }; + + return result; +}; diff --git a/functions/OwnedNFTs.js b/functions/OwnedNFTs.js index e773d48..71f7a0e 100644 --- a/functions/OwnedNFTs.js +++ b/functions/OwnedNFTs.js @@ -1,34 +1,34 @@ import { Network, Alchemy } from "alchemy-sdk"; const settings = { - apiKey: process.env.ALCHEMY_KEY, - network: Network.ETH_MAINNET + apiKey: process.env.ALCHEMY_KEY, + network: Network.ETH_MAINNET, }; - + const alchemy = new Alchemy(settings); export const OwnedNFTs = async (req) => { - const wallet = req.query.wallet; + const wallet = req.query.wallet; - const nfts = await alchemy.nft.getNftsForOwner(wallet); + const nfts = await alchemy.nft.getNftsForOwner(wallet); - const total = nfts.totalCount; - let data = {total: 0, nfts: []}; + const total = nfts.totalCount; + let data = { total: 0, nfts: [] }; - for (const n of nfts.ownedNfts) { - data.nfts.push({ - name: n.contract.name, - symbol: n.contract.symbol, - address: n.contract.address, - token_id: n.tokenId - }); - } - data.total = total; + for (const n of nfts.ownedNfts) { + data.nfts.push({ + name: n.contract.name, + symbol: n.contract.symbol, + address: n.contract.address, + token_id: n.tokenId, + }); + } + data.total = total; - const result = { - inputs: {wallet: wallet}, - output: {data: data}, - success: true - } + const result = { + inputs: { wallet: wallet }, + output: { data: data }, + success: true, + }; - return result; -} \ No newline at end of file + return result; +}; diff --git a/functions/Relay.js b/functions/Relay.js index 65b950b..14e45bd 100644 --- a/functions/Relay.js +++ b/functions/Relay.js @@ -1,24 +1,24 @@ -import { ethers } from 'ethers'; -import { relayer_abi } from '../abi.js'; -import dotenv from 'dotenv' -import { GetProvider } from './GetProvider.js'; +import { ethers } from "ethers"; +import { relayer_abi } from "../abi.js"; +import dotenv from "dotenv"; +import { GetProvider } from "./GetProvider.js"; dotenv.config(); export const Relay = async (req) => { - const network = req.query.network; - const provider = GetProvider(network); - const privateKey = process.env.PRIVATE_KEY; - const signer = new ethers.Wallet(privateKey, provider); - const relayer = '0x2A0d1f0EE9c5584b1694BCa16879423432770A52'; - const signature = req.query.signature; - const reqStruct = JSON.parse(req.query.reqStruct); - const contract = new ethers.Contract(relayer, relayer_abi, provider); - let result = await contract.connect(signer).execute(reqStruct, signature); - await result.wait(1) - const response = { - inputs: {signature: signature, reqStruct: reqStruct}, - output: {data: result.hash}, - success: true - } - return response; -} \ No newline at end of file + const network = req.query.network; + const provider = GetProvider(network); + const privateKey = process.env.PRIVATE_KEY; + const signer = new ethers.Wallet(privateKey, provider); + const relayer = "0x2A0d1f0EE9c5584b1694BCa16879423432770A52"; + const signature = req.query.signature; + const reqStruct = JSON.parse(req.query.reqStruct); + const contract = new ethers.Contract(relayer, relayer_abi, provider); + const result = await contract.connect(signer).execute(reqStruct, signature); + await result.wait(1); + const response = { + inputs: { signature: signature, reqStruct: reqStruct }, + output: { data: result.hash }, + success: true, + }; + return response; +}; diff --git a/functions/SignatureAuth.js b/functions/SignatureAuth.js index eeba1d7..52a9b55 100644 --- a/functions/SignatureAuth.js +++ b/functions/SignatureAuth.js @@ -1,43 +1,50 @@ import { initializeApp } from "firebase/app"; import { ethers } from "ethers"; -import { getFirestore, collection, query, getDocs, where, setDoc, doc } from 'firebase/firestore/lite'; -0 +import { + getFirestore, + collection, + query, + getDocs, + where, + setDoc, + doc, +} from "firebase/firestore/lite"; +0; const firebaseConfig = { - apiKey: process.env.fb_key, - authDomain: process.env.authDomain, - projectId: process.env.projectId, - storageBucket: process.env.storageBucket, - messagingSenderId: process.env.messagingSenderId, - appId: process.env.appId, - measurementId: process.env.measurementId + apiKey: process.env.fb_key, + authDomain: process.env.authDomain, + projectId: process.env.projectId, + storageBucket: process.env.storageBucket, + messagingSenderId: process.env.messagingSenderId, + appId: process.env.appId, + measurementId: process.env.measurementId, }; const fb = initializeApp(firebaseConfig); const db = getFirestore(fb); -export const SignatureAuth = async(req) => { - const signature = req.query.signature; - const address = req.query.wallet; - const message = req.query.message; +export const SignatureAuth = async (req) => { + const signature = req.query.signature; + const address = req.query.wallet; + const message = req.query.message; - const sigRef = collection(db, 'Signatures'); - const q = query(sigRef); - const sigSnapshot = await getDocs(q); + const sigRef = collection(db, "Signatures"); + const q = query(sigRef); + const sigSnapshot = await getDocs(q); - let usedSignatures = sigSnapshot.docs[0].data().usedSignatures; + let usedSignatures = sigSnapshot.docs[0].data().usedSignatures; - if(usedSignatures.includes(signature)) return false; + if (usedSignatures.includes(signature)) return false; - // Make sure you arrayify the message if you want the bytes to be used as the message - const recover = ethers.utils.verifyMessage(message, signature) + // Make sure you arrayify the message if you want the bytes to be used as the message + const recover = ethers.utils.verifyMessage(message, signature); - if(recover !== address) return false; + if (recover !== address) return false; - usedSignatures.push(signature); - await setDoc(doc(sigRef, 'UsedSignatures'), { - usedSignatures: usedSignatures, - }); + usedSignatures.push(signature); + await setDoc(doc(sigRef, "UsedSignatures"), { + usedSignatures: usedSignatures, + }); - return true; - -} \ No newline at end of file + return true; +}; diff --git a/functions/TokenBalance.js b/functions/TokenBalance.js index 70cdcc6..8dbcb1e 100644 --- a/functions/TokenBalance.js +++ b/functions/TokenBalance.js @@ -1,39 +1,38 @@ -import { ethers } from 'ethers'; -import { token_abi } from '../abi.js'; -import dotenv from 'dotenv' -import { GetProvider } from './GetProvider.js'; +import { ethers } from "ethers"; +import { token_abi } from "../abi.js"; +import dotenv from "dotenv"; +import { GetProvider } from "./GetProvider.js"; dotenv.config(); export const TokenBalance = async (req) => { - const contract = req.query.contract; - const wallet = req.query.wallet; - const network = req.query.network; + const contract = req.query.contract; + const wallet = req.query.wallet; + const network = req.query.network; - const provider = GetProvider(network); + const provider = GetProvider(network); - let error = {errors: []}; + const error = { errors: [] }; - if(contract === "" || contract === undefined) { - error.errors.push("Invalid or empty contract address"); - } - if(wallet === "" || wallet === undefined) { - error.errors.push("Invalid or empty wallet address"); - } - if(error.errors.length > 0) { - return { - errors: error, - success: false - }; - } - const token = new ethers.Contract(contract, token_abi, provider); - const bal = await token.balanceOf(wallet); - + if (contract === "" || contract === undefined) { + error.errors.push("Invalid or empty contract address"); + } + if (wallet === "" || wallet === undefined) { + error.errors.push("Invalid or empty wallet address"); + } + if (error.errors.length > 0) { + return { + errors: error, + success: false, + }; + } + const token = new ethers.Contract(contract, token_abi, provider); + const bal = await token.balanceOf(wallet); - const result = { - inputs: {wallet: wallet, contract: contract}, - output: {data: parseFloat(bal) / 10**18}, - success: true - } + const result = { + inputs: { wallet: wallet, contract: contract }, + output: { data: parseFloat(bal) / 10 ** 18 }, + success: true, + }; - return result; -} \ No newline at end of file + return result; +}; diff --git a/index.js b/index.js index aafe538..8af71a9 100644 --- a/index.js +++ b/index.js @@ -1,190 +1,192 @@ -import express from 'express' -export const router = express.Router() -const app = express() -import bodyParser from 'body-parser' -import dotenv from 'dotenv' +import express from "express"; +export const router = express.Router(); +const app = express(); +import bodyParser from "body-parser"; +import dotenv from "dotenv"; dotenv.config(); -import { NFTBalance } from './functions/NFTBalance.js' -import { TokenBalance } from './functions/TokenBalance.js' -import { Relay } from './functions/Relay.js' -import { GetSelector } from './functions/GetSelector.js' -import { GetEncodedParams } from './functions/GetEncodedParams.js' -import { GetRelayNonce } from './functions/GetRelayNonce.js' -import { OwnedNFTs } from './functions/OwnedNFTs.js' -import { ContractOwners } from './functions/ContractOwners.js' -import { MerkleProof } from './functions/MerkleProof.js' -import { MerkleRoot } from './functions/MerkleRoot.js' -import { DeployNFT } from './functions/DeployNFT.js' -import { AppendWhitelist } from './functions/AppendWhitelist.js' -import { ETHBalance } from './functions/ETHBalance.js' -import { SignatureAuth } from './functions/SignatureAuth.js' -import { GetOwnedContracts } from './functions/GetOwnedContracts.js' - -router.use(bodyParser.json()) +import { NFTBalance } from "./functions/NFTBalance.js"; +import { TokenBalance } from "./functions/TokenBalance.js"; +import { Relay } from "./functions/Relay.js"; +import { GetSelector } from "./functions/GetSelector.js"; +import { GetEncodedParams } from "./functions/GetEncodedParams.js"; +import { GetRelayNonce } from "./functions/GetRelayNonce.js"; +import { OwnedNFTs } from "./functions/OwnedNFTs.js"; +import { ContractOwners } from "./functions/ContractOwners.js"; +import { MerkleProof } from "./functions/MerkleProof.js"; +import { MerkleRoot } from "./functions/MerkleRoot.js"; +import { DeployNFT } from "./functions/DeployNFT.js"; +import { AppendWhitelist } from "./functions/AppendWhitelist.js"; +import { ETHBalance } from "./functions/ETHBalance.js"; +import { SignatureAuth } from "./functions/SignatureAuth.js"; +import { GetOwnedContracts } from "./functions/GetOwnedContracts.js"; + +router.use(bodyParser.json()); function auth(req) { - if(!process.env.API_KEYS.split(',').includes(req.query.api_key)) return false; - return true; + if (!process.env.API_KEYS.split(",").includes(req.query.api_key)) + return false; + return true; } -router.get('/nft_balance', async (req, res) => { - try{ - const result = await NFTBalance(req); +router.get("/nft_balance", async (req, res) => { + try { + const result = await NFTBalance(req); - res.json(result); - } catch (error) { - console.log(error); - } + res.json(result); + } catch (error) { + console.log(error); + } }); -router.get('/token_balance', async (req, res) => { - try { - const result = await TokenBalance(req); +router.get("/token_balance", async (req, res) => { + try { + const result = await TokenBalance(req); - res.json(result); - } catch(err) { - res.json({success: false, error: err}); - } + res.json(result); + } catch (err) { + res.json({ success: false, error: err }); + } +}); + +router.get("/eth_balance", async (req, res) => { + try { + const result = await ETHBalance(req); + res.json(result); + } catch (error) { + res.json({ success: false, errors: error }); + } }); -router.get('/eth_balance', async (req, res) => { - try{ - const result = await ETHBalance(req); - - res.json(result); - } catch (error) { - res.json({success: false, errors: error}); - } -}) - -router.get('/relay', async (req, res) => { - !auth(req) ? res.status(401).send('Access Denied') : console.log('authorized'); - - try{ - const result = await Relay(req); - - res.json(result); - } catch(error) { - res.json({success: false, errors: error}); - } -}) - -router.get('/get_selector', async (req, res) => { - try{ - const result = await GetSelector(req); - - res.json(result); - } catch(error) { - res.send(error); - } -}) - -router.get('/get_encoded_params', async (req, res) => { - try{ - const result = await GetEncodedParams(req); - - res.json(result); - } catch(error) { - res.send(error); - } -}) - -router.get('/get_relay_nonce', async (req, res) => { - try{ - const result = await GetRelayNonce(req); - - res.json(result); - } catch (error) { - res.send(error); - } -}) - -router.get('/owned_nfts', async(req, res) => { - try{ - const result = await OwnedNFTs(req); - - res.json(result); - } catch (error) { - res.json({success: false, error: error}) - } +router.get("/relay", async (req, res) => { + !auth(req) + ? res.status(401).send("Access Denied") + : console.log("authorized"); + + try { + const result = await Relay(req); + + res.json(result); + } catch (error) { + res.json({ success: false, errors: error }); + } }); -router.get('/contract_owners', async (req, res) => { - try{ - const result = await ContractOwners(req); - - res.json(result); - } catch (error) { - res.json({success: false, error: error}) - } -}) - -router.get('/merkle_proof', async (req, res) => { - try{ - const result = await MerkleProof(req); - - res.json(result); - } catch (error) { - res.json({error: error, success: false}); - } -}) - -router.get('/merkle_root', async (req, res) => { - try{ - const result = await MerkleRoot(req); - - res.json(result); - } catch (error) { - res.json({error: error, success: false}); - } -}) - -router.get('/deploy_nft', async (req, res) => { - const auth = await SignatureAuth(req); - !auth ? res.status(401).send('Access Denied') : console.log('authorized'); - try{ - const result = await DeployNFT(req); +router.get("/get_selector", async (req, res) => { + try { + const result = await GetSelector(req); + + res.json(result); + } catch (error) { + res.send(error); + } +}); + +router.get("/get_encoded_params", async (req, res) => { + try { + const result = await GetEncodedParams(req); + + res.json(result); + } catch (error) { + res.send(error); + } +}); + +router.get("/get_relay_nonce", async (req, res) => { + try { + const result = await GetRelayNonce(req); + + res.json(result); + } catch (error) { + res.send(error); + } +}); + +router.get("/owned_nfts", async (req, res) => { + try { + const result = await OwnedNFTs(req); + + res.json(result); + } catch (error) { + res.json({ success: false, error: error }); + } +}); + +router.get("/contract_owners", async (req, res) => { + try { + const result = await ContractOwners(req); + + res.json(result); + } catch (error) { + res.json({ success: false, error: error }); + } +}); + +router.get("/merkle_proof", async (req, res) => { + try { + const result = await MerkleProof(req); + + res.json(result); + } catch (error) { + res.json({ error: error, success: false }); + } +}); + +router.get("/merkle_root", async (req, res) => { + try { + const result = await MerkleRoot(req); + + res.json(result); + } catch (error) { + res.json({ error: error, success: false }); + } +}); + +router.get("/deploy_nft", async (req, res) => { + const auth = await SignatureAuth(req); + !auth ? res.status(401).send("Access Denied") : console.log("authorized"); + try { + const result = await DeployNFT(req); + + res.json(result); + } catch (error) { + res.json({ error: error, success: false }); + } +}); + +router.get("/append_whitelist", async (req, res) => { + const auth = await SignatureAuth(req); + !auth ? res.status(401).send("Access Denied") : console.log("authorized"); + + try { + const result = await AppendWhitelist(req); + + res.json(result); + } catch (error) { + console.log(error); + res.json({ error: error, success: false }); + } +}); - res.json(result); - } catch (error) { - res.json({error: error, success: false}); - } -}) +router.get("/get_owned_contracts", async (req, res) => { + try { + const result = await GetOwnedContracts(req); -router.get('/append_whitelist', async (req, res) => { + res.json(result); + } catch (error) { + console.log(error); + res.json({ error: error, success: false }); + } +}); + +router.get("/signature_auth", async (req, res) => { + try { const auth = await SignatureAuth(req); - !auth ? res.status(401).send('Access Denied') : console.log('authorized'); - - try{ - const result = await AppendWhitelist(req); - - res.json(result); - } catch(error) { - console.log(error); - res.json({error: error, success: false}); - } -}) - -router.get('/get_owned_contracts', async (req, res) => { - try { - const result = await GetOwnedContracts(req); - - res.json(result); - } catch (error) { - console.log(error) - res.json({error: error, success: false}); - } -}) - -router.get('/signature_auth', async (req, res) => { - try { - const auth = await SignatureAuth(req); - - res.json(auth); - } catch (error) { - console.log(error); - res.json({success: false, error: error}); - } -}) \ No newline at end of file + + res.json(auth); + } catch (error) { + console.log(error); + res.json({ success: false, error: error }); + } +}); diff --git a/server.js b/server.js index 627f4f4..65c9de3 100644 --- a/server.js +++ b/server.js @@ -1,13 +1,13 @@ -import express from 'express'; -import cors from 'cors'; -import bodyParser from 'body-parser'; +import express from "express"; +import cors from "cors"; +import bodyParser from "body-parser"; // const passport = require('passport'); -import path from 'path'; -import fs from 'fs'; +import path from "path"; +import fs from "fs"; const router = express.Router(); const app = express(); -import dotenv from 'dotenv' -dotenv.config() +import dotenv from "dotenv"; +dotenv.config(); const port = process.env.PORT || 5050; @@ -15,10 +15,10 @@ const port = process.env.PORT || 5050; * Middleware */ app.use(cors()); -app.use(bodyParser.json({ limit: '50mb' })); +app.use(bodyParser.json({ limit: "50mb" })); app.use( bodyParser.urlencoded({ - limit: '50mb', + limit: "50mb", extended: true, parameterLimit: 50000, }) @@ -34,15 +34,15 @@ app.use(bodyParser.json()); /** * Routes */ -import {router as functions} from './index.js'; -app.use('/', functions); +import { router as functions } from "./index.js"; +app.use("/", functions); // Online check app.use( - '/', - router.get('/', (req, res) => { + "/", + router.get("/", (req, res) => { try { - res.json({ res: 'success' }); + res.json({ res: "success" }); } catch (err) { res.json({ res: err }); } @@ -50,19 +50,19 @@ app.use( ); //Change to server.listen(...) for https -app.listen(port, '0.0.0.0', err => { +app.listen(port, "0.0.0.0", (err) => { if (err) throw err; console.log(`Server is running on port: ${port}`); }); const exitHandler = (exitCode, options) => { - console.log('Shutting down'); + console.log("Shutting down"); process.exit(); }; -process.on('exit', exitHandler.bind(null, { exit: true })); -process.on('SIGINT', exitHandler.bind(null, { exit: true })); -process.on('SIGUSR1', exitHandler.bind(null, { exit: true })); -process.on('SIGUSR2', exitHandler.bind(null, { exit: true })); -process.on('uncaughtException', exitHandler.bind(null, { exit: true })); \ No newline at end of file +process.on("exit", exitHandler.bind(null, { exit: true })); +process.on("SIGINT", exitHandler.bind(null, { exit: true })); +process.on("SIGUSR1", exitHandler.bind(null, { exit: true })); +process.on("SIGUSR2", exitHandler.bind(null, { exit: true })); +process.on("uncaughtException", exitHandler.bind(null, { exit: true }));