0.0.1 • Published 7 months ago

js-sdk-v3-test v0.0.1

Weekly downloads
-
License
MIT
Repository
-
Last release
7 months ago

This new V3 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.

Quick Start

NodeJS Exclusive

Removed browser-specific methods, e.g., checkAndSignAuthSig

yarn add @lit-protocol/lit-node-client-nodejs

or..

Isomorphic Implementation

Operable in both Node.js and the browser

yarn add @lit-protocol/lit-node-client

or..

Vanilla JavaScript (UMD)

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>

Packages

📝 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!

PackageCategoryVersionDownload
@lit-protocol/lit-node-client-nodejslit-node-client-nodejs3.0.5npmVanilla JS (UMD)
@lit-protocol/lit-node-clientlit-node-client3.0.5npmVanilla 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::

PackageCategoryVersionDownload
@lit-protocol/access-control-conditionsaccess-control-conditions3.0.5npmVanilla JS (UMD)
@lit-protocol/auth-helpersauth-helpers3.0.5npmVanilla JS (UMD)
@lit-protocol/bls-sdkbls-sdk3.0.5npmVanilla JS (UMD)
@lit-protocol/constantsconstants3.0.5npmVanilla JS (UMD)
@lit-protocol/contracts-sdkcontracts-sdk3.0.5npmVanilla JS (UMD)
@lit-protocol/corecore3.0.5npmVanilla JS (UMD)
@lit-protocol/cryptocrypto3.0.5npmVanilla JS (UMD)
@lit-protocol/ecdsa-sdkecdsa-sdk3.0.5npmVanilla JS (UMD)
@lit-protocol/encryptionencryption3.0.5npmVanilla JS (UMD)
@lit-protocol/lit-third-party-libslit-third-party-libs3.0.5npmVanilla JS (UMD)
@lit-protocol/miscmisc3.0.5npmVanilla JS (UMD)
@lit-protocol/naclnacl3.0.5npmVanilla JS (UMD)
@lit-protocol/pkp-basepkp-base3.0.5npmVanilla JS (UMD)
@lit-protocol/pkp-clientpkp-client3.0.5npmVanilla JS (UMD)
@lit-protocol/pkp-cosmospkp-cosmos3.0.5npmVanilla JS (UMD)
@lit-protocol/pkp-etherspkp-ethers3.0.5npmVanilla JS (UMD)
@lit-protocol/pkp-suipkp-sui3.0.5npmVanilla JS (UMD)
@lit-protocol/pkp-walletconnectpkp-walletconnect3.0.5npmVanilla JS (UMD)
@lit-protocol/typestypes3.0.5npmVanilla JS (UMD)
@lit-protocol/uint8arraysuint8arrays3.0.5npmVanilla JS (UMD)
@lit-protocol/auth-browserauth-browser3.0.5npmVanilla JS (UMD)
@lit-protocol/misc-browsermisc-browser3.0.5npmVanilla JS (UMD)

API Doc

http://docs.lit-js-sdk-v2.litprotocol.com/

NOTE: For (Test) apps, all packages and functions can be called inside the browser console. eg. window.LitJsSdk_[package_name].[function_name]

Contributing and developing to this SDK

Prerequisite

  • node (v18.0.0)

Recommended

Quick Start

The following commands will help you start developing with this repository.

First, install the dependencies via yarn:

yarn

Building

You can build the project with the following command:

yarn build

Running Test Apps

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 running individually

// html
yarn nx run html:serve

// react
yarn nx run react:serve

// nodejs
yarn nx run nodejs:serve

Run unit tests

yarn test:unit

Run E2E tests

// -- web
yarn test:e2e:web

// -- node
yarn test:e2e:node

Advanced

Creating a new library

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>

Create a new react demo app using the Lit JS SDK

yarn tools --create --react contracts-sdk --demo

Deleting a package or app

// delete an app from ./app/<app-name>
yarn delete:app <app-name>

// delete a package from ./packages/<package-name>
yarn delete:package <package-name>

Building

yarn build

Building target package

yarn nx run <project-name>:build

Building Local Changes

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:

  1. Run npm link at the root of the specific package you are making code changes in.
cd ./packages/*/<package-name>
npm link
  1. Build the packages with or without dependencies
yarn build
# or
yarn nx run lit-node-client-nodejs:build --with-deps=false
  1. In the client application, run npm link <package> --save to ensure that the package.json of the client application is updated with a file: link to the dependency. This effectively creates a symlink in the node_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:

  1. Make code change
  2. Rebuild specific package
  3. Rebuild client application.

Publishing

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.

to npm

yarn publish:packages

clone & publish to npm

yarn tools --clone <project-name> <clone-project-name> <(?) --publish> <(?) --remove-after>

// eg
yarn tools --clone lit-node-client @litprotocol/dev --publish --remove-after

The Publish Pipeline - bump, build, test, gen docs, publish, git push

yarn bump
yarn build
yarn test:unit
yarn test:e2e:nodejs
yarn gen:docs --push
yarn publish:packages
git add *
git commit -m "Published version X.X.X"
git push

Testing

Quick Start on E2E Testing

The following will serve the react testing app and launch the cypress e2e testing after

yarn test:e2e:web
or
yarn test:e2e:node

Environments

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.

EnvironmentGenerate CommandTest Location
HTMLyarn gen:htmlhttp://localhost:4002
Reactyarn gen:reacthttp://localhost:4003
NodeJsyarn gen:nodejsyarn nx run nodejs:serve

Unit Tests

yarn test:unit

E2E Testing with Metamask using Cypress (for Browser)

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

Testing with a Local Lit Node

First, deploy your Lit Node Contracts, since the correct addresses will be pulled from the ../lit-assets/blockchain/contracts/deployed-lit-node-contracts-temp.json file.

Set these two env vars:

export LIT_JS_SDK_LOCAL_NODE_DEV="true"
export LIT_JS_SDK_FUNDED_WALLET_PRIVATE_KEY="putAFundedPrivateKeyOnChronicleHere"

Run:

yarn update:contracts-sdk --fetch
yarn update:contracts-sdk --gen
yarn build:packages

To run manual tests:

 yarn nx run nodejs:serve

ENV Vars

  • LIT_JS_SDK_GITHUB_ACCESS_TOKEN - a github access token to get the contract ABIs from a private repo
  • LIT_JS_SDK_LOCAL_NODE_DEV - set to true to use a local node
  • LIT_JS_SDK_FUNDED_WALLET_PRIVATE_KEY - set to a funded wallet on Chronicle Testnet

Dockerfile

...coming soon

Other Commands

Interactive graph dependencies using NX

yarn graph

npm.io

FAQs & Common Errors

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"
  },

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"
        }
      ]
    }
  }
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()
          )
        );
      },
    },
  },
});

Make sure your node version is above v18.0.0