diff --git a/src/App.tsx b/src/App.tsx index 8b97fe1..facfb1a 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -35,16 +35,16 @@ declare global { function App() { const [webApp, setWebApp] = useState(null); const [account, setAccount] = useState(null); - const { sdk, connected, provider } = useSDK(); + const [sessionSignatures, setSessionSignatures] = useState(null); + const [valid, setValid] = useState(null); + const [recent, setRecent] = useState(null); + const [data, setData] = useState(null); const [pkp, setPkp] = useState<{ tokenId: any; publicKey: string; ethAddress: string; } | null>(null); - const [sessionSignatures, setSessionSignatures] = useState(null); - const [valid, setValid] = useState(null); - const [recent, setRecent] = useState(null); - const [data, setData] = useState(null); + const { sdk, connected, provider } = useSDK(); useEffect(() => { const tgApp = window.Telegram?.WebApp; @@ -71,6 +71,7 @@ function App() { try { const accounts = await sdk?.connect(); setAccount(accounts?.[0]); + webApp!.showPopup({ title: "Connected", message: `Connected to MetaMask with account: ${accounts[0]}`, diff --git a/src/litConnections.ts b/src/litConnections.ts index 8acca89..2ec7604 100644 --- a/src/litConnections.ts +++ b/src/litConnections.ts @@ -1,64 +1,67 @@ -import { LitNodeClient } from '@lit-protocol/lit-node-client'; -import { LitNetwork, AuthMethodScope } from '@lit-protocol/constants'; -import { LitContracts } from '@lit-protocol/contracts-sdk'; +import { LitNodeClient } from "@lit-protocol/lit-node-client"; +import { LitNetwork, AuthMethodScope } from "@lit-protocol/constants"; +import { LitContracts } from "@lit-protocol/contracts-sdk"; import { litActionCode } from "./litAction"; -import * as ethers from 'ethers'; +import * as ethers from "ethers"; import Hash from "typestub-ipfs-only-hash"; import { - LitAbility, - LitPKPResource, - LitActionResource, - } from "@lit-protocol/auth-helpers"; + LitAbility, + LitPKPResource, + LitActionResource, +} from "@lit-protocol/auth-helpers"; export const connectToLitNodes = async () => { - const litNodeClient = new LitNodeClient({ - litNetwork: LitNetwork.DatilTest, - debug: false, - }); - await litNodeClient.connect(); - return litNodeClient; + const litNodeClient = new LitNodeClient({ + litNetwork: LitNetwork.DatilTest, + debug: false, + }); + await litNodeClient.connect(); + return litNodeClient; }; -export const mintNewPkp = async (provider: any) => { +async function setupLitContracts(provider: any) { await provider.send("eth_requestAccounts", []); const ethersProvider = new ethers.providers.Web3Provider(provider); const signer = ethersProvider.getSigner(); - const litContracts = new LitContracts({ - signer, - network: LitNetwork.DatilTest, - }); - await litContracts.connect(); + const litContracts = new LitContracts({ + signer, + network: LitNetwork.DatilTest, + }); + await litContracts.connect(); - const hash = await Hash.of(litActionCode); - const pkp = (await litContracts.pkpNftContractUtils.write.mint()).pkp; + return { litContracts, signer }; +} - const permitted = await litContracts.addPermittedAction({ - authMethodScopes: [AuthMethodScope.SignAnything], - pkpTokenId: pkp.tokenId, - ipfsId: hash - }); - console.log(permitted); - return pkp; -}; +export const mintNewPkp = async (provider: any) => { + const { litContracts } = await setupLitContracts(provider); -export const getSessionSignatures = async (litNodeClient: LitNodeClient, pkp: any, provider: any, telegramUser: string) => { - await provider.send("eth_requestAccounts", []); - const ethersProvider = new ethers.providers.Web3Provider(provider); - const signer = ethersProvider.getSigner(); + const hash = await Hash.of(litActionCode); + const pkp = (await litContracts.pkpNftContractUtils.write.mint()).pkp; - const litContracts = new LitContracts({ - signer, - network: LitNetwork.DatilTest, + const permitted = await litContracts.addPermittedAction({ + authMethodScopes: [AuthMethodScope.SignAnything], + pkpTokenId: pkp.tokenId, + ipfsId: hash, }); - await litContracts.connect(); + console.log(permitted); + return pkp; +}; + +export const getSessionSignatures = async ( + litNodeClient: LitNodeClient, + pkp: any, + provider: any, + telegramUser: string +) => { + const { litContracts, signer } = await setupLitContracts(provider); - const capacityTokenId = ( - await litContracts.mintCapacityCreditsNFT({ - requestsPerKilosecond: 10, - daysUntilUTCMidnightExpiration: 1, - }) - ).capacityTokenIdStr; + const capacityTokenId = ( + await litContracts.mintCapacityCreditsNFT({ + requestsPerKilosecond: 10, + daysUntilUTCMidnightExpiration: 1, + }) + ).capacityTokenIdStr; const { capacityDelegationAuthSig } = await litNodeClient.createCapacityDelegationAuthSig({ @@ -68,29 +71,29 @@ export const getSessionSignatures = async (litNodeClient: LitNodeClient, pkp: an uses: "1", }); - const sessionSignatures= await litNodeClient.getPkpSessionSigs({ - pkpPublicKey: pkp.publicKey, - capabilityAuthSigs: [capacityDelegationAuthSig], - litActionCode: Buffer.from(litActionCode).toString("base64"), - jsParams: { - telegramUserData: telegramUser, - telegramBotSecret: import.meta.env.VITE_TELEGRAM_BOT_TOKEN, - pkpTokenId: pkp.tokenId, - }, - resourceAbilityRequests: [ - { - resource: new LitPKPResource("*"), - ability: LitAbility.PKPSigning, - }, - { - resource: new LitActionResource("*"), - ability: LitAbility.LitActionExecution, - }, - ], - expiration: new Date(Date.now() + 1000 * 60 * 10).toISOString(), // 10 minutes - }); - console.log( - `✅ Got PKP Session Sigs: ${JSON.stringify(sessionSignatures, null, 2)}` - ); - return sessionSignatures; + const sessionSignatures = await litNodeClient.getPkpSessionSigs({ + pkpPublicKey: pkp.publicKey, + capabilityAuthSigs: [capacityDelegationAuthSig], + litActionCode: Buffer.from(litActionCode).toString("base64"), + jsParams: { + telegramUserData: telegramUser, + telegramBotSecret: import.meta.env.VITE_TELEGRAM_BOT_TOKEN, + pkpTokenId: pkp.tokenId, + }, + resourceAbilityRequests: [ + { + resource: new LitPKPResource("*"), + ability: LitAbility.PKPSigning, + }, + { + resource: new LitActionResource("*"), + ability: LitAbility.LitActionExecution, + }, + ], + expiration: new Date(Date.now() + 1000 * 60 * 10).toISOString(), // 10 minutes + }); + console.log( + `✅ Got PKP Session Sigs: ${JSON.stringify(sessionSignatures, null, 2)}` + ); + return sessionSignatures; }; \ No newline at end of file