npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@simplito/privmx-webendpoint-sdk

v2.0.5

Published

Set of Typescript/Javascript tools for working with Privmx Webendpoint

Downloads

96

Readme

PrivMX Web Endpoint SDK

Overview

PrivMX Web Endpoint SDK is a set of TypeScript/JavaScript tools that simplify working with PrivMX Bridge in Web environments. It consists of:

  • high-level wrappers for native WASM assets, to accelerate the development of end-to-end encrypted apps even more;
  • types for powerful autocompletion in your IDE;
  • CLI for straightforward project initialization.

Initial Requirements

To start developing end-to-end encrypted applications using PrivMX Endpoint you need:

  • A PrivMX Bridge Instance. Go to Bridge CLI repo to find scripts to create and initialize one on your local machine.

  • A server for managing users. It can be new or existing, depending on the specific requirements. For more information about how PrivMX Endpoint integrates into your stack check our getting started guide.

Installation

First, install this package using your package manager of choice.

  • npm:
npm install @simplito/privmx-webendpoint-sdk@latest 
  • pnpm:
pnpm add @simplito/privmx-webendpoint-sdk@latest 
  • yarn:
yarn add @simplito/privmx-webendpoint-sdk@latest 

The wrappers need access to the core lib in order to work properly. To do this, follow these steps:

  1. Host core WASM assets from your static assets folder (e.g. /public). Then import them using <script> tags inside your app's main file. Depending on the stack it can be for example index.html or app.js.
  2. Configure your server to properly integrate our library. You need to set up specific HTTP headers:
  • Cross-Origin-Embedder-Policy: require-corp
  • Cross-Origin-Opener-Policy: same-origin

This will ensure the library functions correctly and maintain security.

The steps above can be handled automatically, using our setup npx script:

npx @simplito/webendpoint-manager

Documentation

Full documentation about this package's functions, types and example usage can be found in our docs.

Example Apps

If you prefer a more hands-on approach to learning, start by cloning one of our example apps:

  • Endpoint Examples Minimal - a set of small Tool-specific code examples executed inside browser console. The best for a quick-start with PrivMX Endpoint.

  • Chatee - a full-fledged end-to-end encrypted chat app built using Next.js. It demonstrates how to:

    • create a secure real-time chat using Threads;
    • send attachments using Stores;
    • manage users inside PrivMX Bridge from your own server using REST API.

Creating First App

A step-by-step tutorial that will guide you through creating your first app using JavaScript Endpoint SDK with PrivMX Bridge.

Setup PrivMX Bridge Instance

To create a PrivMX Bridge Instance, you need Docker with Docker Compose installed on your machine.

  1. First, clone our Bridge CLI repo:

    git clone https://github.com/simplito/privmx-bridge-docker
  2. Run setup scripts (it may require admin permissions):

    ./setup.sh

    This will fetch the necessary Docker images, create Access Keys and your first Context. After a successful setup, CLI will display all the API keys necessary for connection.

  3. Register the first user:

    In your terminal, generate private-public key pair for your user. The keys must be in WIF format:

    ./genKeyPair.sh

    With the keys ready, register userId - public key pair in your Context. Don't forget to replace placeholder values with the ones created earlier:

    ./cli.sh context/addUserToContext '{"contextId": "CONTEXT_ID", "userId":"USER_ID", "userPubKey":"USER_PUBLIC_KEY" }'

Create Client App

To create client app, you need Node and npm installed on your machine.

  1. Create app using Vite CLI:
npm create vite

Follow all the steps displayed by Vite CLI. You can choose any framework you prefer - PrivMX Endpoint Web SDK is framework-agnostic.

  1. Install dependencies

Inside the created project folder, install this Endpoint Web SDK:

npm i @simplito/privmx-webendpoint-sdk@latest

After the installation, run setup script provided by the SDK:

npx @simplito/webendpoint-manager

Follow the steps displayed in your terminal.

  1. Connect to your Bridge Instance

Paste the following snippet to your main JavaScript file (e.g. ./src/App.ts or ./src/index.js):

 async function connectToBridge() {
     const connection = await Endpoint.connect({
         bridgeUrl:"BRIDGE_URL",
         solutionId:"SOLUTION_ID",
         privKey:"USER_PRIVATE_KEY",
     })

     const firstUser = {
         userId:"USER_ID",
         pubKey:"USER_PUBLIC_KEY",
     }

     const threadId = await connection.threads.new({
         users:[firstUser],
         managers:[firstUser]
     })

     await connection.thread(threadId).sendMessage({
         data:new TextEncoder().encode("Hello Bridge!")
     })
     const messageList = await connection.thread(threadId).getMessages(0)

     const decodedMessageList = messageList.readItems.map(message => {
         return {
             data:new TextDecoder().decode(message.data),
             info:message.info,
             authorPubKey:message.authorPubKey,
         }
     })
     console.log(decodedMessageList)
}

First, you have to connect to your Bridge Instance using Endpoint.connect method. It requires the API keys generated while initializing your local instance earlier.

const connection = await Endpoint.connect({
         bridgeUrl:"BRIDGE_URL",
         solutionId:"SOLUTION_ID",
         privKey:"USER_PRIVATE_KEY",
})

When connected, you have access to all SDK methods. This example shows how to create a Thread, send and download a message. To create a Thread inside Context, use Threads handle methods new. Note that you have to pass user ID - public key pair to make a list of users and managers.

 const firstUser = {
         userId:"USER_ID",
         pubKey:"USER_PUBLIC_KEY",
     }

     const threadId = await connection.threads.new({
         users:[firstUser],
         managers:[firstUser]
     })

With the Thread created, you can now send the first message.

Endpoint sends data in Uint8Array format. It requires encoding your data from string or object format to binary (e.g. the usage of TextEncoder for string encoding).

await connection.thread(threadId).sendMessage({
    data:new TextEncoder().encode("Hello Bridge!")
})

To get a list of messages inside a Thread, use getMessages method. Because data inside messages is in Uint8Array you have to deserialize it to human-readable string. Endpoint takes care of encrypting your data before sending it to PrivMX Bridge.

   const messageList = await connection.thread(threadId).getMessages(0)

     const decodedMessageList = messageList.readItems.map(message => {
         return {
             data:new TextDecoder().decode(message.data),
             info:message.info,
             authorPubKey:message.authorPubKey,
         }
     })
     console.log(decodedMessageList)
  1. Run your app

In your terminal, run npm run dev to start local app server.

License

MIT