Skip to content

Commit d191a6b

Browse files
Merge pull request #85 from LIT-Protocol/wyatt/v2
Wyatt/v2
2 parents e046e04 + cd86341 commit d191a6b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+1034
-618
lines changed

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,25 @@
11
# Lit Developer Code Guides
22

33
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.
4+
5+
## Creating a New Code Guide
6+
7+
### Generating a New Code Directory
8+
9+
```bash
10+
npx nx g templates:nodejs
11+
```
12+
13+
This will run the NX generator for the Node.js template, and create a new directory under a selected category
14+
15+
### Running All the Tests
16+
17+
```bash
18+
yarn test:all
19+
```
20+
21+
### Running the Tests for a Specific Code Guide
22+
23+
```bash
24+
yarn workspace name-of-code-guide test
25+
```

encryption/encrypt-large-file/package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99
},
1010
"dependencies": {
1111
"@dotenvx/dotenvx": "^0.44.1",
12-
"@lit-protocol/auth-helpers": "^6.11.0",
13-
"@lit-protocol/constants": "^6.11.0",
14-
"@lit-protocol/lit-node-client": "^6.11.0",
15-
"@lit-protocol/types": "^6.11.0",
12+
"@lit-protocol/auth-helpers": "^7.0.0",
13+
"@lit-protocol/constants": "^7.0.0",
14+
"@lit-protocol/lit-node-client": "^7.0.0",
15+
"@lit-protocol/types": "^7.0.0",
1616
"ethers": "v5"
1717
},
1818
"devDependencies": {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ETHEREUM_PRIVATE_KEY=
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
yarn-debug.log*
6+
yarn-error.log*
7+
pnpm-debug.log*
8+
lerna-debug.log*
9+
10+
.env
11+
node_modules
12+
dist
13+
dist-ssr
14+
*.local
15+
16+
# Editor directories and files
17+
.vscode/*
18+
!.vscode/extensions.json
19+
.idea
20+
.DS_Store
21+
*.suo
22+
*.ntvs*
23+
*.njsproj
24+
*.sln
25+
*.sw?
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"$schema": "https://json.schemastore.org/mocharc.json",
3+
"require": "tsx"
4+
}

session-signatures/generating-a-session/using-pkp/auth-with-eth-wallet/README.md

Whitespace-only changes.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"name": "generating-a-session-using-pkp-auth-with-eth-wallet",
3+
"version": "0.1.0",
4+
"main": "index.js",
5+
"license": "MIT",
6+
"type": "module",
7+
"scripts": {
8+
"test": "dotenvx run -- mocha test/**/*.spec.ts"
9+
},
10+
"dependencies": {
11+
"@dotenvx/dotenvx": "^0.44.1",
12+
"@lit-protocol/auth-helpers": "^7.0.0",
13+
"@lit-protocol/constants": "^7.0.0",
14+
"@lit-protocol/lit-auth-client": "^7.0.0",
15+
"@lit-protocol/lit-node-client": "^7.0.0",
16+
"ethers": "v5"
17+
},
18+
"devDependencies": {
19+
"@lit-protocol/contracts-sdk": "^7.0.0",
20+
"@types/chai": "^4.3.16",
21+
"@types/chai-json-schema": "^1.4.10",
22+
"@types/mocha": "^10.0.6",
23+
"chai": "^5.1.1",
24+
"chai-json-schema": "^1.5.1",
25+
"mocha": "^10.4.0",
26+
"tsc": "^2.0.4",
27+
"tsx": "^4.12.0",
28+
"typescript": "^5.4.5"
29+
}
30+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"name": "generating-a-session-using-pkp-auth-with-eth-wallet",
3+
"$schema": "../../../node_modules/nx/schemas/project-schema.json",
4+
"projectType": "library",
5+
"sourceRoot": "session-signatures/generating-a-session/using-pkp/auth-with-eth-wallet/src",
6+
"// targets": "to see all targets run: nx show project generating-a-session-using-pkp-auth-with-eth-wallet --web",
7+
"targets": {}
8+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import { LitNodeClient } from '@lit-protocol/lit-node-client';
2+
import { LIT_ABILITY } from '@lit-protocol/constants';
3+
import { LitPKPResource } from '@lit-protocol/auth-helpers';
4+
import { EthWalletProvider } from '@lit-protocol/lit-auth-client';
5+
6+
import {
7+
getEthersSigner,
8+
getLitNodeClient,
9+
type LIT_NETWORKS_KEYS,
10+
} from './utils';
11+
12+
export const getpkpSessionSigs = async (
13+
litNetwork: LIT_NETWORKS_KEYS,
14+
pkp: {
15+
tokenId: any;
16+
publicKey: string;
17+
ethAddress: string;
18+
}
19+
) => {
20+
let litNodeClient: LitNodeClient;
21+
22+
try {
23+
const ethersSigner = getEthersSigner();
24+
litNodeClient = await getLitNodeClient(litNetwork);
25+
26+
console.log('🔄 Creating AuthMethod using the ethersSigner...');
27+
const authMethod = await EthWalletProvider.authenticate({
28+
signer: ethersSigner,
29+
// @ts-expect-error There seems to be a bug with the types
30+
litNodeClient,
31+
});
32+
console.log('✅ Finished creating the AuthMethod');
33+
34+
console.log('🔄 Getting the Session Sigs for the PKP...');
35+
const sessionSignatures = await litNodeClient.getPkpSessionSigs({
36+
pkpPublicKey: pkp.publicKey!,
37+
authMethods: [authMethod],
38+
resourceAbilityRequests: [
39+
{
40+
resource: new LitPKPResource('*'),
41+
ability: LIT_ABILITY.PKPSigning,
42+
},
43+
],
44+
expiration: new Date(Date.now() + 1000 * 60 * 10).toISOString(), // 10 minutes
45+
});
46+
console.log('✅ Got PKP Session Sigs');
47+
48+
return sessionSignatures;
49+
} catch (error) {
50+
console.error(error);
51+
} finally {
52+
litNodeClient!.disconnect();
53+
}
54+
};
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { LIT_NETWORK, LIT_RPC } from '@lit-protocol/constants';
2+
import { LitContracts } from '@lit-protocol/contracts-sdk';
3+
import { ethers } from 'ethers';
4+
import { LitNodeClient } from '@lit-protocol/lit-node-client';
5+
6+
export type LIT_NETWORKS_KEYS = (typeof LIT_NETWORK)[keyof typeof LIT_NETWORK];
7+
8+
export const getEnv = (name: string): string => {
9+
const env = process.env[name];
10+
if (env === undefined || env === '')
11+
throw new Error(
12+
`${name} ENV is not defined, please define it in the .env file`
13+
);
14+
return env;
15+
};
16+
17+
export const getEthersSigner = () => {
18+
const ETHEREUM_PRIVATE_KEY = getEnv('ETHEREUM_PRIVATE_KEY');
19+
return new ethers.Wallet(
20+
ETHEREUM_PRIVATE_KEY,
21+
new ethers.providers.JsonRpcProvider(LIT_RPC.CHRONICLE_YELLOWSTONE)
22+
);
23+
};
24+
25+
export const getLitNodeClient = async (litNetwork: LIT_NETWORKS_KEYS) => {
26+
console.log('🔄 Connecting LitNodeClient to Lit network...');
27+
const litNodeClient = new LitNodeClient({
28+
litNetwork,
29+
debug: false,
30+
});
31+
await litNodeClient.connect();
32+
console.log('✅ Connected LitNodeClient to Lit network');
33+
34+
return litNodeClient;
35+
};
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import { expect, use } from 'chai';
2+
import chaiJsonSchema from 'chai-json-schema';
3+
import { LIT_NETWORK } from '@lit-protocol/constants';
4+
5+
import { getpkpSessionSigs } from '../src';
6+
import { getEthersSigner } from '../src/utils';
7+
import { mintPkp, getLitContracts } from './testUtils';
8+
9+
use(chaiJsonSchema);
10+
11+
describe('getSessionSigsPKP', () => {
12+
let pkp: {
13+
tokenId: any;
14+
publicKey: string;
15+
ethAddress: string;
16+
};
17+
18+
before(async function () {
19+
this.timeout(120_000);
20+
21+
const ethersSigner = getEthersSigner();
22+
const litContracts = await getLitContracts(ethersSigner);
23+
pkp = await mintPkp(litContracts);
24+
});
25+
26+
it('Attempting to get session signatures...', async () => {
27+
const sessionSigResponseSchema = {
28+
type: 'object',
29+
patternProperties: {
30+
'^https://\\d+\\.\\d+\\.\\d+\\.\\d+:\\d+$': {
31+
type: 'object',
32+
properties: {
33+
sig: { type: 'string' },
34+
derivedVia: { type: 'string' },
35+
signedMessage: { type: 'string' },
36+
address: { type: 'string' },
37+
algo: { type: 'string' },
38+
},
39+
required: ['sig', 'derivedVia', 'signedMessage', 'address', 'algo'],
40+
},
41+
},
42+
additionalProperties: false,
43+
};
44+
45+
const sessionSignatures = await getpkpSessionSigs(
46+
LIT_NETWORK.DatilTest,
47+
pkp
48+
);
49+
expect(sessionSignatures).to.be.jsonSchema(sessionSigResponseSchema);
50+
}).timeout(120_000);
51+
});
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { LitContracts } from '@lit-protocol/contracts-sdk';
2+
import { LIT_NETWORK } from '@lit-protocol/constants';
3+
import { ethers } from 'ethers';
4+
5+
export const getLitContracts = async (
6+
ethersSigner: ethers.Wallet,
7+
litNetwork = LIT_NETWORK.DatilTest
8+
) => {
9+
console.log('🔄 Connecting LitContracts client to network...');
10+
const litContracts = new LitContracts({
11+
signer: ethersSigner,
12+
network: litNetwork,
13+
debug: false,
14+
});
15+
await litContracts.connect();
16+
console.log('✅ Connected LitContracts client to network');
17+
18+
return litContracts;
19+
};
20+
21+
export const mintPkp = async (litContracts: LitContracts) => {
22+
console.log('🔄 Minting new PKP...');
23+
const pkp = (await litContracts.pkpNftContractUtils.write.mint()).pkp;
24+
console.log(
25+
`✅ Minted new PKP with public key: ${pkp.publicKey} and ETH address: ${pkp.ethAddress}`
26+
);
27+
28+
return pkp;
29+
};

0 commit comments

Comments
 (0)