Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,25 @@
# Lit Developer Code Guides

This repository works in conjunction with the [Lit Build Docs](https://docs-olive-ten.vercel.app/build), and contains code examples for various Lit functionalities.

## Creating a New Code Guide

### Generating a New Code Directory

```bash
npx nx g templates:nodejs
```

This will run the NX generator for the Node.js template, and create a new directory under a selected category

### Running All the Tests

```bash
yarn test:all
```

### Running the Tests for a Specific Code Guide

```bash
yarn workspace name-of-code-guide test
```
8 changes: 4 additions & 4 deletions encryption/encrypt-large-file/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
},
"dependencies": {
"@dotenvx/dotenvx": "^0.44.1",
"@lit-protocol/auth-helpers": "^6.11.0",
"@lit-protocol/constants": "^6.11.0",
"@lit-protocol/lit-node-client": "^6.11.0",
"@lit-protocol/types": "^6.11.0",
"@lit-protocol/auth-helpers": "^7.0.0",
"@lit-protocol/constants": "^7.0.0",
"@lit-protocol/lit-node-client": "^7.0.0",
"@lit-protocol/types": "^7.0.0",
"ethers": "v5"
},
"devDependencies": {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ETHEREUM_PRIVATE_KEY=
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*

.env
node_modules
dist
dist-ssr
*.local

# Editor directories and files
.vscode/*
!.vscode/extensions.json
.idea
.DS_Store
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"$schema": "https://json.schemastore.org/mocharc.json",
"require": "tsx"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"name": "generating-a-session-using-pkp-auth-with-eth-wallet",
"version": "0.1.0",
"main": "index.js",
"license": "MIT",
"type": "module",
"scripts": {
"test": "dotenvx run -- mocha test/**/*.spec.ts"
},
"dependencies": {
"@dotenvx/dotenvx": "^0.44.1",
"@lit-protocol/auth-helpers": "^7.0.0",
"@lit-protocol/constants": "^7.0.0",
"@lit-protocol/lit-auth-client": "^7.0.0",
"@lit-protocol/lit-node-client": "^7.0.0",
"ethers": "v5"
},
"devDependencies": {
"@lit-protocol/contracts-sdk": "^7.0.0",
"@types/chai": "^4.3.16",
"@types/chai-json-schema": "^1.4.10",
"@types/mocha": "^10.0.6",
"chai": "^5.1.1",
"chai-json-schema": "^1.5.1",
"mocha": "^10.4.0",
"tsc": "^2.0.4",
"tsx": "^4.12.0",
"typescript": "^5.4.5"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "generating-a-session-using-pkp-auth-with-eth-wallet",
"$schema": "../../../node_modules/nx/schemas/project-schema.json",
"projectType": "library",
"sourceRoot": "session-signatures/generating-a-session/using-pkp/auth-with-eth-wallet/src",
"// targets": "to see all targets run: nx show project generating-a-session-using-pkp-auth-with-eth-wallet --web",
"targets": {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { LitNodeClient } from '@lit-protocol/lit-node-client';
import { LIT_ABILITY } from '@lit-protocol/constants';
import { LitPKPResource } from '@lit-protocol/auth-helpers';
import { EthWalletProvider } from '@lit-protocol/lit-auth-client';

import {
getEthersSigner,
getLitNodeClient,
type LIT_NETWORKS_KEYS,
} from './utils';

export const getpkpSessionSigs = async (
litNetwork: LIT_NETWORKS_KEYS,
pkp: {
tokenId: any;
publicKey: string;
ethAddress: string;
}
) => {
let litNodeClient: LitNodeClient;

try {
const ethersSigner = getEthersSigner();
litNodeClient = await getLitNodeClient(litNetwork);

console.log('🔄 Creating AuthMethod using the ethersSigner...');
const authMethod = await EthWalletProvider.authenticate({
signer: ethersSigner,
// @ts-expect-error There seems to be a bug with the types
litNodeClient,
});
console.log('✅ Finished creating the AuthMethod');

console.log('🔄 Getting the Session Sigs for the PKP...');
const sessionSignatures = await litNodeClient.getPkpSessionSigs({
pkpPublicKey: pkp.publicKey!,
authMethods: [authMethod],
resourceAbilityRequests: [
{
resource: new LitPKPResource('*'),
ability: LIT_ABILITY.PKPSigning,
},
],
expiration: new Date(Date.now() + 1000 * 60 * 10).toISOString(), // 10 minutes
});
console.log('✅ Got PKP Session Sigs');

return sessionSignatures;
} catch (error) {
console.error(error);
} finally {
litNodeClient!.disconnect();
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { LIT_NETWORK, LIT_RPC } from '@lit-protocol/constants';
import { LitContracts } from '@lit-protocol/contracts-sdk';
import { ethers } from 'ethers';
import { LitNodeClient } from '@lit-protocol/lit-node-client';

export type LIT_NETWORKS_KEYS = (typeof LIT_NETWORK)[keyof typeof LIT_NETWORK];

export const getEnv = (name: string): string => {
const env = process.env[name];
if (env === undefined || env === '')
throw new Error(
`${name} ENV is not defined, please define it in the .env file`
);
return env;
};

export const getEthersSigner = () => {
const ETHEREUM_PRIVATE_KEY = getEnv('ETHEREUM_PRIVATE_KEY');
return new ethers.Wallet(
ETHEREUM_PRIVATE_KEY,
new ethers.providers.JsonRpcProvider(LIT_RPC.CHRONICLE_YELLOWSTONE)
);
};

export const getLitNodeClient = async (litNetwork: LIT_NETWORKS_KEYS) => {
console.log('🔄 Connecting LitNodeClient to Lit network...');
const litNodeClient = new LitNodeClient({
litNetwork,
debug: false,
});
await litNodeClient.connect();
console.log('✅ Connected LitNodeClient to Lit network');

return litNodeClient;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { expect, use } from 'chai';
import chaiJsonSchema from 'chai-json-schema';
import { LIT_NETWORK } from '@lit-protocol/constants';

import { getpkpSessionSigs } from '../src';
import { getEthersSigner } from '../src/utils';
import { mintPkp, getLitContracts } from './testUtils';

use(chaiJsonSchema);

describe('getSessionSigsPKP', () => {
let pkp: {
tokenId: any;
publicKey: string;
ethAddress: string;
};

before(async function () {
this.timeout(120_000);

const ethersSigner = getEthersSigner();
const litContracts = await getLitContracts(ethersSigner);
pkp = await mintPkp(litContracts);
});

it('Attempting to get session signatures...', async () => {
const sessionSigResponseSchema = {
type: 'object',
patternProperties: {
'^https://\\d+\\.\\d+\\.\\d+\\.\\d+:\\d+$': {
type: 'object',
properties: {
sig: { type: 'string' },
derivedVia: { type: 'string' },
signedMessage: { type: 'string' },
address: { type: 'string' },
algo: { type: 'string' },
},
required: ['sig', 'derivedVia', 'signedMessage', 'address', 'algo'],
},
},
additionalProperties: false,
};

const sessionSignatures = await getpkpSessionSigs(
LIT_NETWORK.DatilTest,
pkp
);
expect(sessionSignatures).to.be.jsonSchema(sessionSigResponseSchema);
}).timeout(120_000);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { LitContracts } from '@lit-protocol/contracts-sdk';
import { LIT_NETWORK } from '@lit-protocol/constants';
import { ethers } from 'ethers';

export const getLitContracts = async (
ethersSigner: ethers.Wallet,
litNetwork = LIT_NETWORK.DatilTest
) => {
console.log('🔄 Connecting LitContracts client to network...');
const litContracts = new LitContracts({
signer: ethersSigner,
network: litNetwork,
debug: false,
});
await litContracts.connect();
console.log('✅ Connected LitContracts client to network');

return litContracts;
};

export const mintPkp = async (litContracts: LitContracts) => {
console.log('🔄 Minting new PKP...');
const pkp = (await litContracts.pkpNftContractUtils.write.mint()).pkp;
console.log(
`✅ Minted new PKP with public key: ${pkp.publicKey} and ETH address: ${pkp.ethAddress}`
);

return pkp;
};
Loading