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
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
đź“ť 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 | Download |
---|---|---|
@lit-protocol/lit-node-client-nodejs | ||
@lit-protocol/lit-node-client |
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::
Version | Link |
---|---|
V7 (Current) | 7.x.x docs |
V6 | 6.x.x docs |
V5 | 5.x.x docs |
V2 | 2.x.x docs |
Before you begin, ensure you have the following installed:
- Node.js v19.0.0 or later
- Rust v1.70.0 or later
- wasm-pack for WebAssembly compilation
Recommended for better development experience:
- NX Console - Visual Studio Code extension for NX workspace management
To start developing with this repository:
- Install dependencies:
yarn
- Build the packages:
yarn build:dev
Build the project using one of these commands:
// For local development (optimized, excludes production-only operations)
yarn build:dev
// For testing and publishing (full build with all operations)
yarn build
yarn test:unit
yarn test:local
nx generate @nx/js:library
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.
cd ./packages/*/<package-name>
npm link
- Build the packages with or without dependencies
yarn build
# or
yarn nx run lit-node-client-nodejs:build --with-deps=false
- In the client application, run
npm link <package> --save
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.
cd path/to/client-application
npm link <package> --save
Having done this setup, this is what the development cycle looks like moving forward:
- Make code change
- Rebuild specific package
- Rebuild client application.
For changes to WebAssembly components in packages/wasm
, refer to the WebAssembly build guide.
Prerequisites:
- Node.js v18.0.0 or later
Publishing steps:
-
Create a release PR:
- Create a new branch from master with format
vX.X.X-Publish
- Add changelog as PR description
- Add "Release" label to the PR
- Reference example: #753
- Create a new branch from master with format
-
After PR approval, proceed with publishing:
- Update dependencies:
yarn install
- Increment version:
yarn bump
- Build packages:
yarn build
- Run tests:
- Unit tests:
yarn test:unit
- E2E tests:
yarn test:local
- Unit tests:
- Generate documentation:
yarn gen:docs --push
- Publish packages:
yarn publish:packages
- Commit with message: "Published version X.X.X"
- Update dependencies:
Command | Description |
---|---|
yarn test:unit |
Run unit tests for all packages |
yarn test:local |
Run E2E tests in Node.js environment |
-
Unit Tests:
yarn test:unit
-
End-to-End Tests:
yarn test:local
Optional Environment Variables:
- NETWORK=<network_name> (datil, datil-test, datil-dev, etc.)
- DEBUG=true/false
Optional Flags:
- --filter=
See more in
local-tests/README.md
-
Deploy Lit Node Contracts (addresses will be read from
../lit-assets/blockchain/contracts/deployed-lit-node-contracts-temp.json
) -
Configure environment variables:
# Enable local node development
export LIT_JS_SDK_LOCAL_NODE_DEV="true"
# Set funded wallet for Chronicle testnet
export LIT_JS_SDK_FUNDED_WALLET_PRIVATE_KEY="your-funded-private-key"
Variable | Description | Usage |
---|---|---|
LIT_JS_SDK_GITHUB_ACCESS_TOKEN |
GitHub access token | Required for accessing contract ABIs from private repository |
LIT_JS_SDK_LOCAL_NODE_DEV |
Local node development flag | Set to true to use a local Lit node |
LIT_JS_SDK_FUNDED_WALLET_PRIVATE_KEY |
Funded wallet private key | Required for Chronicle Testnet transactions |
The SDK implements a robust error handling system using @openagenda/verror. This system provides:
- Detailed error information with cause tracking
- Error composition and chaining
- Additional context through metadata
- Compatibility with native JavaScript Error handling
import { VError } from '@openagenda/verror';
import { LitNodeClientBadConfigError } from '@lit-protocol/constants';
try {
// Simulate an error condition
const someNativeError = new Error('some native error');
// Throw a Lit-specific error with context
throw new LitNodeClientBadConfigError(
{
cause: someNativeError,
info: { foo: 'bar' },
meta: { baz: 'qux' },
},
'some useful message'
);
} catch (e) {
// Access error details
console.log(e.name); // LitNodeClientBadConfigError
console.log(e.message); // some useful message: some native error
console.log(e.info); // { foo: 'bar' }
console.log(e.baz); // qux
// Additional error information
// - VError.cause(e): Original error (someNativeError)
// - VError.info(e): Additional context ({ foo: 'bar' })
// - VError.meta(e): Metadata ({ baz: 'qux', code: 'lit_node_client_bad_config_error', kind: 'Config' })
// - VError.fullStack(e): Complete error chain stack trace
}
To add new error types:
- Locate
packages/constants/src/lib/errors.ts
- Add your error definition to the
LIT_ERROR
object - Export the new error class
- Import and use in your code with relevant context:
throw new YourCustomError( { cause: originalError, info: { /* context */ }, meta: { /* metadata */ }, }, 'Error message' );
...coming soon
The Lit Protocol SDK provides the following core systems:
- Cryptographic key management (PKP - Programmable Key Pair)
- Blockchain wallet interactions (Ethereum, Solana, Cosmos)
- Decentralized authentication and authorization
- Distributed computing and signing
- Smart contract management
- Access control and encryption services
Key components available across packages:
PKPEthersWallet
: Ethereum wallet management for PKPLitNodeClient
: Network interaction clientexecuteJs()
: Decentralized JavaScript executionsignMessageWithEncryptedKey()
: Cryptographic signinggeneratePrivateKey()
: Key generation utilitiesTinnyEnvironment
: Testing environment setup
Problem: "Reference Error: crypto is not defined"
Solution: Add the following polyfill for environments without native crypto:
import crypto, { createHash } from 'crypto';
// Add crypto to global scope
Object.defineProperty(globalThis, 'crypto', {
value: {
// Implement getRandomValues
getRandomValues: (arr: any) => crypto.randomBytes(arr.length),
// Implement subtle crypto
subtle: {
digest: (algorithm: string, data: Uint8Array) => {
return new Promise((resolve) =>
resolve(
createHash(algorithm.toLowerCase().replace('-', ''))
.update(data)
.digest()
)
);
},
},
},
});
Problem: Exit code 13
Solution: Make sure your node version is above v18.0.0