The Lit JavaScript SDK provides developers with a framework for implementing Lit functionality into their own applications. Find installation instructions in the docs to get started with the Lit SDK based on your use case:
https://developer.litprotocol.com/SDK/Explanation/installation
This new V2 SDK is written in Typescript and is a complete rewrite of the old SDK. It is much more modular and easier to use, and has a much smaller bundle size.
ChangeLog: All notable changes to this project will be documented in this file.
Removed browser-specific methods, e.g., checkAndSignAuthSig
yarn add @lit-protocol/lit-node-client-nodejs
or..
Operable in both Node.js and the browser
yarn add @lit-protocol/lit-node-client
or..
For usage directly in the browser with a script tag
<script src="https://cdn.jsdelivr.net/npm/@lit-protocol/lit-node-client-vanilla/lit-node-client.js"></script>
<script>
const authSig = LitJsSdk_litNodeClient.checkAndSignAuthMessage({chain: 'ethereum'});
</script>
📝 If you're looking to use the Lit SDK, you're probably all set with just the lit-node-client .
Get started with interacting with Lit network!
Package | Category | Version | Download |
---|---|---|---|
@lit-protocol/lit-node-client-nodejs | 2.2.26 | npm Vanilla JS (UMD) |
|
@lit-protocol/lit-node-client | 2.2.26 | npm Vanilla JS (UMD) |
If you're a tech-savvy user and wish to utilize only specific submodules that our main module relies upon, you can find individual packages listed below. This way, you can import only the necessary packages that cater to your specific use case::
App | Link |
---|---|
Simple Encrypt Decrypt | https://demo.encrypt-decrypt.react.litprotocol.com/ |
Contracts SDK | https://demo.contracts-sdk.react.litprotocol.com/ |
(Test) Html | http://test.lit-js-sdk-v2.html.litprotocol.com/ |
(Test) React | http://test.lit-js-sdk-v2.react.litprotocol.com/ |
NOTE: For (Test) apps, all packages and functions can be called inside the browser console. eg.
window.LitJsSdk_[package_name].[function_name]
- node (v18.0.0)
The following commands will help you start developing with this repository.
First, install the dependencies via yarn:
yarn
You can build the project with the following command:
yarn build
The test apps are configured to automatically import all modules and expose all module functions. For browsers, you can access these functions using window.LitJsSdk_<package_name>.<function_name>
// Running apps...
// html: http://localhost:4002
// react: http://localhost:4003
// nodejs: in the terminal
yarn apps
or runniny individually
// html
yarn nx run html:serve
// react
yarn nx run react:serve
// nodejs
yarn nx run nodejs:serve
yarn test:unit
yarn test:e2e
By default, NX provides a command to generate a library
nx generate @nrwl/js:library
. However, it doesn't have an esbuild built-in so that we've created a custom tool that modify the build commands.
yarn gen:lib <package-name> <tag>
yarn tools --create --react contracts-sdk --demo
// delete an app from ./app/<app-name>
yarn delete:app <app-name>
// delete a package from ./packages/<package-name>
yarn delete:package <package-name>
yarn build
yarn nx run <project-name>:build
During development you may wish to build your code changes in packages/
in a client application to test the correctness of the functionality.
If you would like to establish a dependency between packages within this monorepo and an external client application that consumes these packages,
- Run
npm link
at the root of the specific package you are making code changes in. - Run
yarn build:target <package>
to build that specific package. - In the client application, run
npm link <package> --force
to ensure that thepackage.json
of the client application is updated with afile:
link to the dependency. This effectively creates a symlink in thenode_modules
of the client application to the local dependency in this repository.
Having done this setup, this is what the development cycle looks like moving forward:
- Make code change
- Rebuild specific package
- Rebuild client application.
Run yarn bump
to bump the version. You must have at least nodejs v18 to do this. Next, run yarn buildAndPublish
to build and then publish.
yarn publish:packages
yarn tools --clone <project-name> <clone-project-name> <(?) --publish> <(?) --remove-after>
// eg
yarn tools --clone lit-node-client @litprotocol/dev --publish --remove-after
The following will serve the react testing app and launch the cypress e2e testing after
yarn test:e2e
There are currently three environments can be tested on, each of which can be generated from a custom command, which would automatically import all the libraries in ./packages/*
. The UI of HTML & React are visually identical but they are using different libraries.
Environment | Generate Command | Test Location |
---|---|---|
HTML | yarn gen:html |
http://localhost:4002 |
React | yarn gen:react |
http://localhost:4003 |
NodeJs | yarn gen:nodejs |
yarn nx run nodejs:serve |
yarn test:unit
Since both HTML & React UIs are identical, we can run the same test suite against two different environments of libraries. This is done by setting the PORT
number before Cypress launch.
HTML
// E2E HTML
yarn tools --test --e2e html
React
// E2E React
yarn tools --test --e2e react
yarn graph
(React) Failed to parse source map from
In your React package.json, add GENERATE_SOURCEMAP=false
to your start script
eg.
"scripts": {
"start": "GENERATE_SOURCEMAP=false react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
Web bundling using esbuild
It’s currently using a custom plugin @websaam/nx-esbuild which is a fork from @wanews/nx-esbuild
"_buildWeb": {
"executor": "@websaam/nx-esbuild:package",
"options": {
"banner": {
"js": "import { createRequire } from 'module';const require = createRequire(import.meta.url);"
},
"globalName": "LitJsSdk_CoreBrowser",
"outfile":"dist/packages/core-browser-vanilla/core-browser.js",
"entryPoints": ["./packages/core-browser/src/index.ts"],
"define": { "global": "window" },
"plugins":[
{
"package": "esbuild-node-builtins",
"function": "nodeBuiltIns"
}
]
}
}
Reference Error: crypto is not defined
import crypto, { createHash } from 'crypto';
Object.defineProperty(globalThis, 'crypto', {
value: {
getRandomValues: (arr: any) => crypto.randomBytes(arr.length),
subtle: {
digest: (algorithm: string, data: Uint8Array) => {
return new Promise((resolve, reject) =>
resolve(
createHash(algorithm.toLowerCase().replace('-', ''))
.update(data)
.digest()
)
);
},
},
},
});
error Command failed with exit code 13.
Make sure your node version is above v18.0.0