Skip to content

Commit 161acd1

Browse files
committed
Init Connecting to Lit example
1 parent e67f6ba commit 161acd1

File tree

7 files changed

+3808
-0
lines changed

7 files changed

+3808
-0
lines changed
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+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# Connecting to a Lit Network
2+
3+
This directory contains a code example that demonstrates how to connect to a Lit Network using the Lit SDK's `LitNodeClient`.
4+
5+
The [code example](./src/index.ts) is written in TypeScript and uses the `LitNodeClient` to connect to the `DatilDev` network.
6+
7+
## Running the Code Example
8+
9+
### Requirements
10+
11+
- [Node.js](https://nodejs.org/en)
12+
- [Yarn](https://yarnpkg.com/getting-started)
13+
- `@lit-protocol/constants`
14+
- `@lit-protocol/lit-node-client`
15+
16+
### Steps
17+
18+
1. `yarn` to install the dependencies
19+
2. `yarn test` to run the code example
20+
21+
After running the code example, you should see output in your terminal indicating that the `LitNodeClient` was successfully connected to the `DatilDev` network:
22+
23+
```bash
24+
[Lit-JS-SDK v6.11.0] [2024-11-15T04:22:17.234Z] [DEBUG] [core] listening for state change on staking contract: 0xD4507CD392Af2c80919219d7896508728f6A623F
25+
[Lit-JS-SDK v6.11.0] [2024-11-15T04:22:17.235Z] [DEBUG] [core] 🔥 lit is ready. "litNodeClient" variable is ready to use globally.
26+
[Lit-JS-SDK v6.11.0] [2024-11-15T04:22:17.235Z] [DEBUG] [core] current network config {
27+
networkPubkey: '82b7ec4aac62a87aba55f6920862e021df37c445a075e72d87a64c3573aea67cfbb59a7be671e785fb3cb05d242cf2e6',
28+
networkPubKeySet: '82b7ec4aac62a87aba55f6920862e021df37c445a075e72d87a64c3573aea67cfbb59a7be671e785fb3cb05d242cf2e6',
29+
hdRootPubkeys: [
30+
'03ab0ccc2dc8a3d9c2de9a7421b171475a09e60cb546fe446a9d0c3aadb8f639ae',
31+
'03b6300abf338b504b7b49eb83b59554d12232b9f390f6bb5bf06150b812678b53',
32+
'039911489c48624554fef51f37323053953def50ed3b40e69620d0a6bcb4ffdc2f',
33+
'034feeda3a7912131c980ae8b6376611dd5052d574979247d64da8bb1446e240d7',
34+
'023dd81dba4b89da5e4c3dec5da93ba0e1addc0eafbe03487450c42e28628570d1',
35+
'0346ac7da954f1b8ee891b79eb8cf45bafef62c85dffc1dc41e4b755bc0428689f',
36+
'0222be55763b2339daf0c149e5e6e927894e0667671de47a591fc24f38eef48cd3',
37+
'0313cce430e8c79053e02460e880ec36f1d64763617b47e0a2f83d06caae86c27a',
38+
'023e4f4020ea03b0631b02cbb595631e6bbbd563f1fe9f1cf953b29ea8a2d26a85',
39+
'0332914fa7b8ca6083139bb969650c26b092fe9990ef188daada9e4049bef58899'
40+
],
41+
subnetPubkey: '82b7ec4aac62a87aba55f6920862e021df37c445a075e72d87a64c3573aea67cfbb59a7be671e785fb3cb05d242cf2e6',
42+
latestBlockhash: '0x1e1a3f530ed757b6a61b0667396f7d71207b59b53893b1583e37e4476ed47ecb'
43+
}
44+
[Lit-JS-SDK v6.11.0] [2024-11-15T04:22:17.236Z] [DEBUG] [core] running cleanup for global modules
45+
```
46+
47+
## Understanding the Code
48+
49+
The following code from [./src/index.ts](./src/index.ts) is what's responsible for connecting to the `DatilDev` network:
50+
51+
```typescript
52+
litNodeClient = new LitNodeClient({
53+
litNetwork: LitNetwork.DatilDev,
54+
debug: true,
55+
});
56+
await litNodeClient.connect();
57+
```
58+
59+
The `litNetwork` option specifies which Lit Network to connect to. In this example, we're connecting to the `DatilDev` network, and is the network you should be using for the hackathon.
60+
61+
Setting the `debug` option to `true` will print additional information to the terminal about the connection process, and any errors that occur when processing your Lit network request.
62+
63+
To disconnect from the Lit Network, you can call the `disconnect()` method on the `LitNodeClient` instance:
64+
65+
```typescript
66+
litNodeClient!.disconnect();
67+
```
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"name": "nodejs",
3+
"version": "1.0.0",
4+
"main": "index.js",
5+
"license": "MIT",
6+
"type": "module",
7+
"scripts": {
8+
"test": "npx mocha test/**/*.spec.ts"
9+
},
10+
"dependencies": {
11+
"@lit-protocol/constants": "^6.11.0",
12+
"@lit-protocol/lit-node-client": "^6.11.0"
13+
},
14+
"devDependencies": {
15+
"@types/chai": "^4.3.16",
16+
"@types/chai-json-schema": "^1.4.10",
17+
"@types/mocha": "^10.0.6",
18+
"chai": "^5.1.1",
19+
"chai-json-schema": "^1.5.1",
20+
"mocha": "^10.4.0",
21+
"tsc": "^2.0.4",
22+
"tsx": "^4.12.0",
23+
"typescript": "^5.4.5"
24+
}
25+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { LitNodeClient } from "@lit-protocol/lit-node-client";
2+
import { LitNetwork } from "@lit-protocol/constants";
3+
4+
export const runExample = async () => {
5+
let litNodeClient: LitNodeClient;
6+
7+
try {
8+
litNodeClient = new LitNodeClient({
9+
litNetwork: LitNetwork.DatilDev,
10+
debug: true,
11+
});
12+
await litNodeClient.connect();
13+
} catch (error) {
14+
console.error(error);
15+
} finally {
16+
litNodeClient!.disconnect();
17+
}
18+
};
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { runExample } from "../src";
2+
3+
describe("Testing connecting to a Lit Network", () => {
4+
it("should connect to a Lit Network", async () => {
5+
await runExample();
6+
}).timeout(120_000);
7+
});
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)