From f86d5a89158847f9fce454a68a8c217233a1dcce Mon Sep 17 00:00:00 2001 From: serafettin Date: Sun, 18 Feb 2024 20:36:51 +0100 Subject: [PATCH] Switch to habanero --- api/src/libs/lit/index.js | 94 +++++++++++++++++++++++++++++++++++-- web-app/src/config/index.ts | 2 +- 2 files changed, 91 insertions(+), 5 deletions(-) diff --git a/api/src/libs/lit/index.js b/api/src/libs/lit/index.js index 3b3e21d7..85f9260f 100644 --- a/api/src/libs/lit/index.js +++ b/api/src/libs/lit/index.js @@ -21,8 +21,11 @@ import { CID } from 'multiformats/cid'; import { SiweMessage } from "@didtools/cacao"; import { getAddress } from "@ethersproject/address"; +import { LitAbility, LitPKPResource, LitActionResource } from '@lit-protocol/auth-helpers'; + + const config = { - litNetwork: "cayenne", + litNetwork: "habanero", domain: "index.network", }; @@ -130,6 +133,65 @@ export const getPKPSessionForIndexer = async(index) => { const pkpSession = await getPKPSession(session, index); return pkpSession; } + +const provider = new ethers.JsonRpcProvider(process.env.LIT_PROTOCOL_RPC_PROVIDER); + +const dappOwnerWallet = new ethers.Wallet( + process.env.INDEXER_WALLET_PRIVATE_KEY, + provider +); + +const pkpAuthNeededCallback = async ({resources, expiration, uri}) => { + + const litResource = new LitActionResource('*'); + + const recapObject = + await litNodeClient.generateSessionCapabilityObjectWithWildcards([ + litResource, + ]); + + recapObject.addCapabilityForResource( + litResource, + LitAbility.LitActionExecution + ); + + const verified = recapObject.verifyCapabilitiesForResource( + litResource, + LitAbility.LitActionExecution + ); + + if (!verified) { + throw new Error('Failed to verify capabilities for resource'); + } + + let siweMessage = new SiweMessage({ + domain: 'index.network', // change to your domain ex: example.app.com + address: dappOwnerWallet.address, + //statement: 'Some custom statement.', // configure to what ever you would like + uri, + version: '1', + chainId: '1', + expirationTime: expiration, + resources, + }); + + siweMessage = recapObject.addToSiweMessage(siweMessage); + + const messageToSign = siweMessage.toMessage(); + const signature = await dappOwnerWallet.signMessage(messageToSign); + + const authSig = { + sig: signature.replace('0x', ''), + derivedVia: 'web3.eth.personal.sign', + signedMessage: messageToSign, + address: dappOwnerWallet.address, + }; + + return authSig; + + +} + export const getPKPSession = async (session, index) => { if(!session.did.authenticated){ @@ -166,18 +228,42 @@ export const getPKPSession = async (session, index) => { const didKey = new DID({ provider, resolver: getResolver() }); await didKey.authenticate(); - try{ + try { + const litNodeClient = new LitJsSdk.LitNodeClientNodeJs({ litNetwork: config.litNetwork, debug: false, }); await litNodeClient.connect(); + + const { capacityDelegationAuthSig } = + await litNodeClient.createCapacityDelegationAuthSig({ + uses: '2', + dAppOwnerWallet: dappOwnerWallet, + capacityTokenId: process.env.LIT_PROTOCOL_CAPACITY_TOKEN_ID, + delegateeAddresses: [authSig.address], + }); + + const pkpSessionSigs = await litNodeClient.getSessionSigs({ + pkpPublicKey: index.signerPublicKey, + expiration: new Date(Date.now() + 1000 * 60 * 60 * 24).toISOString(), // 24 hours + chain: 'ethereum', + resourceAbilityRequests: [ + { + resource: new LitPKPResource('*'), + ability: LitAbility.PKPSigning, + }, + ], + authNeededCallback: pkpAuthNeededCallback, + capacityDelegationAuthSig, + }); + const signerFunctionV0 = CID.parse(index.signerFunction).toV0().toString(); const resp = await litNodeClient.executeJs({ ipfsId: signerFunctionV0, - authSig, + sessionSigs: pkpSessionSigs, jsParams: { - authSig, + authSig, // for conditions control. session signature is not enough. chain: "ethereum", // polygon publicKey: index.signerPublicKey, didKey: didKey.id, diff --git a/web-app/src/config/index.ts b/web-app/src/config/index.ts index 5f939fc1..cbaaf6f2 100644 --- a/web-app/src/config/index.ts +++ b/web-app/src/config/index.ts @@ -4,7 +4,7 @@ export const appConfig = { ipfsProxy: "https://indexas.infura-ipfs.io/ipfs", ipfsInfura: "http://localhost:3001/avatar", defaultCID: "QmPAoTyDyaFJJdKK6FQd9pWivAhiNLYxTUpan5zVxbbMUZ", // Empty. - litNetwork: "cayenne" as "cayenne" | "custom" | "localhost" | "manzano" | "habanero", + litNetwork: "habanero" as "cayenne" | "custom" | "localhost" | "manzano" | "habanero", testNetwork: { chainId: "0x2ac49", chainName: "Chronicle - Lit Protocol Testnet",