Skip to content

Commit e046e04

Browse files
committed
Init generating session sig with auth sig
1 parent 214e0ef commit e046e04

File tree

10 files changed

+283
-0
lines changed

10 files changed

+283
-0
lines changed
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-an-auth-sig/README.md

Whitespace-only changes.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"name": "generating-a-session-using-an-auth-sig",
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-node-client": "^7.0.0",
15+
"ethers": "v5"
16+
},
17+
"devDependencies": {
18+
"@types/chai": "^4.3.16",
19+
"@types/chai-json-schema": "^1.4.10",
20+
"@types/mocha": "^10.0.6",
21+
"chai": "^5.1.1",
22+
"chai-json-schema": "^1.5.1",
23+
"mocha": "^10.4.0",
24+
"tsc": "^2.0.4",
25+
"tsx": "^4.12.0",
26+
"typescript": "^5.4.5"
27+
}
28+
}
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-an-auth-sig",
3+
"$schema": "../../../node_modules/nx/schemas/project-schema.json",
4+
"projectType": "library",
5+
"sourceRoot": "session-signatures/generating-a-session/using-an-auth-sig/src",
6+
"// targets": "to see all targets run: nx show project generating-a-session/using-an-auth-sig --web",
7+
"targets": {}
8+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import { LitNodeClient } from '@lit-protocol/lit-node-client';
2+
import { LIT_ABILITY, LIT_NETWORK, LIT_RPC } from '@lit-protocol/constants';
3+
import {
4+
createSiweMessage,
5+
generateAuthSig,
6+
LitAccessControlConditionResource,
7+
} from '@lit-protocol/auth-helpers';
8+
import { ethers } from 'ethers';
9+
10+
import { getEnv } from './utils';
11+
12+
const ETHEREUM_PRIVATE_KEY = getEnv('ETHEREUM_PRIVATE_KEY');
13+
14+
export const runExample = async () => {
15+
let litNodeClient: LitNodeClient;
16+
17+
try {
18+
const ethersSigner = new ethers.Wallet(
19+
ETHEREUM_PRIVATE_KEY,
20+
new ethers.providers.JsonRpcProvider(LIT_RPC.CHRONICLE_YELLOWSTONE)
21+
);
22+
23+
litNodeClient = new LitNodeClient({
24+
litNetwork: LIT_NETWORK.DatilTest,
25+
debug: true,
26+
});
27+
await litNodeClient.connect();
28+
29+
const sessionSignatures = await litNodeClient.getSessionSigs({
30+
chain: 'ethereum',
31+
expiration: new Date(Date.now() + 1000 * 60 * 10).toISOString(), // 10 minutes
32+
resourceAbilityRequests: [
33+
{
34+
resource: new LitAccessControlConditionResource('*'),
35+
ability: LIT_ABILITY.AccessControlConditionDecryption,
36+
},
37+
],
38+
authNeededCallback: async ({
39+
uri,
40+
expiration,
41+
resourceAbilityRequests,
42+
}) => {
43+
const toSign = await createSiweMessage({
44+
uri,
45+
expiration,
46+
resources: resourceAbilityRequests,
47+
walletAddress: await ethersSigner.getAddress(),
48+
nonce: await litNodeClient.getLatestBlockhash(),
49+
litNodeClient,
50+
});
51+
52+
return await generateAuthSig({
53+
signer: ethersSigner,
54+
toSign,
55+
});
56+
},
57+
});
58+
59+
return sessionSignatures;
60+
} catch (error) {
61+
console.error(error);
62+
} finally {
63+
litNodeClient!.disconnect();
64+
}
65+
};
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export const getEnv = (name: string): string => {
2+
const env = process.env[name];
3+
if (env === undefined || env === '')
4+
throw new Error(
5+
`${name} ENV is not defined, please define it in the .env file`
6+
);
7+
return env;
8+
};
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { expect, use } from 'chai';
2+
import chaiJsonSchema from 'chai-json-schema';
3+
4+
import { runExample } from '../src';
5+
6+
use(chaiJsonSchema);
7+
8+
describe('getSessionSigsViaAuthSig', () => {
9+
const sessionSigResponseSchema = {
10+
type: 'object',
11+
patternProperties: {
12+
'^https://\\d+\\.\\d+\\.\\d+\\.\\d+:\\d+$': {
13+
type: 'object',
14+
properties: {
15+
sig: { type: 'string' },
16+
derivedVia: { type: 'string' },
17+
signedMessage: { type: 'string' },
18+
address: { type: 'string' },
19+
algo: { type: 'string' },
20+
},
21+
required: ['sig', 'derivedVia', 'signedMessage', 'address', 'algo'],
22+
},
23+
},
24+
additionalProperties: false,
25+
};
26+
27+
it('Attempting to get session signatures...', async () => {
28+
const sessionSignatures = await runExample();
29+
expect(sessionSignatures).to.be.jsonSchema(sessionSigResponseSchema);
30+
}).timeout(120_000);
31+
});
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
{
2+
"compilerOptions": {
3+
/* Visit https://aka.ms/tsconfig to read more about this file */
4+
5+
/* Projects */
6+
// "incremental": true, /* Save .tsbuildinfo files to allow for incremental compilation of projects. */
7+
// "composite": true, /* Enable constraints that allow a TypeScript project to be used with project references. */
8+
// "tsBuildInfoFile": "./.tsbuildinfo", /* Specify the path to .tsbuildinfo incremental compilation file. */
9+
// "disableSourceOfProjectReferenceRedirect": true, /* Disable preferring source files instead of declaration files when referencing composite projects. */
10+
// "disableSolutionSearching": true, /* Opt a project out of multi-project reference checking when editing. */
11+
// "disableReferencedProjectLoad": true, /* Reduce the number of projects loaded automatically by TypeScript. */
12+
13+
/* Language and Environment */
14+
"target": "ES2020" /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */,
15+
"lib": [
16+
"ES2020"
17+
] /* Specify a set of bundled library declaration files that describe the target runtime environment. */,
18+
// "jsx": "preserve", /* Specify what JSX code is generated. */
19+
// "experimentalDecorators": true, /* Enable experimental support for legacy experimental decorators. */
20+
// "emitDecoratorMetadata": true, /* Emit design-type metadata for decorated declarations in source files. */
21+
// "jsxFactory": "", /* Specify the JSX factory function used when targeting React JSX emit, e.g. 'React.createElement' or 'h'. */
22+
// "jsxFragmentFactory": "", /* Specify the JSX Fragment reference used for fragments when targeting React JSX emit e.g. 'React.Fragment' or 'Fragment'. */
23+
// "jsxImportSource": "", /* Specify module specifier used to import the JSX factory functions when using 'jsx: react-jsx*'. */
24+
// "reactNamespace": "", /* Specify the object invoked for 'createElement'. This only applies when targeting 'react' JSX emit. */
25+
// "noLib": true, /* Disable including any library files, including the default lib.d.ts. */
26+
// "useDefineForClassFields": true, /* Emit ECMAScript-standard-compliant class fields. */
27+
// "moduleDetection": "auto", /* Control what method is used to detect module-format JS files. */
28+
29+
/* Modules */
30+
"module": "es2022" /* Specify what module code is generated. */,
31+
"rootDir": "./src" /* Specify the root folder within your source files. */,
32+
"moduleResolution": "node" /* Specify how TypeScript looks up a file from a given module specifier. */,
33+
// "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */
34+
// "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */
35+
// "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */
36+
// "typeRoots": [], /* Specify multiple folders that act like './node_modules/@types'. */
37+
"types": [
38+
"mocha"
39+
] /* Specify type package names to be included without being referenced in a source file. */,
40+
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
41+
// "moduleSuffixes": [], /* List of file name suffixes to search when resolving a module. */
42+
// "allowImportingTsExtensions": true, /* Allow imports to include TypeScript file extensions. Requires '--moduleResolution bundler' and either '--noEmit' or '--emitDeclarationOnly' to be set. */
43+
// "resolvePackageJsonExports": true, /* Use the package.json 'exports' field when resolving package imports. */
44+
// "resolvePackageJsonImports": true, /* Use the package.json 'imports' field when resolving imports. */
45+
// "customConditions": [], /* Conditions to set in addition to the resolver-specific defaults when resolving imports. */
46+
// "resolveJsonModule": true, /* Enable importing .json files. */
47+
// "allowArbitraryExtensions": true, /* Enable importing files with any extension, provided a declaration file is present. */
48+
// "noResolve": true, /* Disallow 'import's, 'require's or '<reference>'s from expanding the number of files TypeScript should add to a project. */
49+
50+
/* JavaScript Support */
51+
// "allowJs": true, /* Allow JavaScript files to be a part of your program. Use the 'checkJS' option to get errors from these files. */
52+
// "checkJs": true, /* Enable error reporting in type-checked JavaScript files. */
53+
// "maxNodeModuleJsDepth": 1, /* Specify the maximum folder depth used for checking JavaScript files from 'node_modules'. Only applicable with 'allowJs'. */
54+
55+
/* Emit */
56+
// "declaration": true, /* Generate .d.ts files from TypeScript and JavaScript files in your project. */
57+
// "declarationMap": true, /* Create sourcemaps for d.ts files. */
58+
// "emitDeclarationOnly": true, /* Only output d.ts files and not JavaScript files. */
59+
// "sourceMap": true, /* Create source map files for emitted JavaScript files. */
60+
// "inlineSourceMap": true, /* Include sourcemap files inside the emitted JavaScript. */
61+
// "outFile": "./", /* Specify a file that bundles all outputs into one JavaScript file. If 'declaration' is true, also designates a file that bundles all .d.ts output. */
62+
// "outDir": "./", /* Specify an output folder for all emitted files. */
63+
// "removeComments": true, /* Disable emitting comments. */
64+
// "noEmit": true, /* Disable emitting files from a compilation. */
65+
// "importHelpers": true, /* Allow importing helper functions from tslib once per project, instead of including them per-file. */
66+
// "importsNotUsedAsValues": "remove", /* Specify emit/checking behavior for imports that are only used for types. */
67+
// "downlevelIteration": true, /* Emit more compliant, but verbose and less performant JavaScript for iteration. */
68+
// "sourceRoot": "", /* Specify the root path for debuggers to find the reference source code. */
69+
// "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
70+
// "inlineSources": true, /* Include source code in the sourcemaps inside the emitted JavaScript. */
71+
// "emitBOM": true, /* Emit a UTF-8 Byte Order Mark (BOM) in the beginning of output files. */
72+
// "newLine": "crlf", /* Set the newline character for emitting files. */
73+
// "stripInternal": true, /* Disable emitting declarations that have '@internal' in their JSDoc comments. */
74+
// "noEmitHelpers": true, /* Disable generating custom helper functions like '__extends' in compiled output. */
75+
// "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */
76+
// "preserveConstEnums": true, /* Disable erasing 'const enum' declarations in generated code. */
77+
// "declarationDir": "./", /* Specify the output directory for generated declaration files. */
78+
// "preserveValueImports": true, /* Preserve unused imported values in the JavaScript output that would otherwise be removed. */
79+
80+
/* Interop Constraints */
81+
// "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */
82+
// "verbatimModuleSyntax": true, /* Do not transform or elide any imports or exports not marked as type-only, ensuring they are written in the output file's format based on the 'module' setting. */
83+
// "allowSyntheticDefaultImports": true, /* Allow 'import x from y' when a module doesn't have a default export. */
84+
"esModuleInterop": true /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */,
85+
// "preserveSymlinks": true, /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */
86+
"forceConsistentCasingInFileNames": true /* Ensure that casing is correct in imports. */,
87+
88+
/* Type Checking */
89+
"strict": true /* Enable all strict type-checking options. */,
90+
// "noImplicitAny": true, /* Enable error reporting for expressions and declarations with an implied 'any' type. */
91+
// "strictNullChecks": true, /* When type checking, take into account 'null' and 'undefined'. */
92+
// "strictFunctionTypes": true, /* When assigning functions, check to ensure parameters and the return values are subtype-compatible. */
93+
// "strictBindCallApply": true, /* Check that the arguments for 'bind', 'call', and 'apply' methods match the original function. */
94+
// "strictPropertyInitialization": true, /* Check for class properties that are declared but not set in the constructor. */
95+
// "noImplicitThis": true, /* Enable error reporting when 'this' is given the type 'any'. */
96+
// "useUnknownInCatchVariables": true, /* Default catch clause variables as 'unknown' instead of 'any'. */
97+
// "alwaysStrict": true, /* Ensure 'use strict' is always emitted. */
98+
// "noUnusedLocals": true, /* Enable error reporting when local variables aren't read. */
99+
// "noUnusedParameters": true, /* Raise an error when a function parameter isn't read. */
100+
// "exactOptionalPropertyTypes": true, /* Interpret optional property types as written, rather than adding 'undefined'. */
101+
// "noImplicitReturns": true, /* Enable error reporting for codepaths that do not explicitly return in a function. */
102+
// "noFallthroughCasesInSwitch": true, /* Enable error reporting for fallthrough cases in switch statements. */
103+
// "noUncheckedIndexedAccess": true, /* Add 'undefined' to a type when accessed using an index. */
104+
// "noImplicitOverride": true, /* Ensure overriding members in derived classes are marked with an override modifier. */
105+
// "noPropertyAccessFromIndexSignature": true, /* Enforces using indexed accessors for keys declared using an indexed type. */
106+
// "allowUnusedLabels": true, /* Disable error reporting for unused labels. */
107+
// "allowUnreachableCode": true, /* Disable error reporting for unreachable code. */
108+
109+
/* Completeness */
110+
// "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */
111+
"skipLibCheck": true /* Skip type checking all .d.ts files. */
112+
}
113+
}

0 commit comments

Comments
 (0)