Skip to content

Commit

Permalink
Example works on mobile, refactoring some code
Browse files Browse the repository at this point in the history
  • Loading branch information
awisniew207 committed Sep 10, 2024
1 parent a8e265f commit f122407
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 74 deletions.
11 changes: 6 additions & 5 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,16 @@ declare global {
function App() {
const [webApp, setWebApp] = useState<TelegramWebApp | null>(null);
const [account, setAccount] = useState<string | null>(null);
const { sdk, connected, provider } = useSDK();
const [sessionSignatures, setSessionSignatures] = useState<any | null>(null);
const [valid, setValid] = useState<boolean | null>(null);
const [recent, setRecent] = useState<boolean | null>(null);
const [data, setData] = useState<any | null>(null);
const [pkp, setPkp] = useState<{
tokenId: any;
publicKey: string;
ethAddress: string;
} | null>(null);
const [sessionSignatures, setSessionSignatures] = useState<any | null>(null);
const [valid, setValid] = useState<boolean | null>(null);
const [recent, setRecent] = useState<boolean | null>(null);
const [data, setData] = useState<any | null>(null);
const { sdk, connected, provider } = useSDK();

useEffect(() => {
const tgApp = window.Telegram?.WebApp;
Expand All @@ -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]}`,
Expand Down
141 changes: 72 additions & 69 deletions src/litConnections.ts
Original file line number Diff line number Diff line change
@@ -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({
Expand All @@ -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;
};

0 comments on commit f122407

Please sign in to comment.