Skip to content

Commit

Permalink
Switch to habanero
Browse files Browse the repository at this point in the history
  • Loading branch information
serefyarar committed Feb 18, 2024
1 parent ea16db8 commit f86d5a8
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 5 deletions.
94 changes: 90 additions & 4 deletions api/src/libs/lit/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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",
};

Expand Down Expand Up @@ -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){
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion web-app/src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit f86d5a8

Please sign in to comment.