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

cloudless-cart

v0.1.5

Published

simple module to create distributed secure shopping carts

Downloads

330

Readme

Cloudless Cart

Generic class to store a list of items, sign them, and verify the signature.

Uses the cryptography library to sign and verify the items.

Intent

many only shopping experiences rely on a centralized "cart" databse to store and retrieve data. This library allows for passing a cart around to different services without having to worry about the backend state of the cart. This is useful to decouple systems in a microservices ecosystem.

The main "cart" service can interact with the cart and sign it and other systems can verify integrity without calling back to main database.

JsonSignature can be used by itself to sign and verify any arbitrary json object.

Usage


import { CloudlessCart, JsonSignature } from 'cloudless-cart';
//create a new cart
const cart = new CloudlessCart();
//create a new signer
const signer = new JsonSignature();
//generate a new keypair 
// (note: this is unique to this instance see 
//below for how to export/import a keypair)
await signer.generateKeyPair();
//set the signer
cart.setSigner(signer);
//add some stuff to the cart
cart.addItem({ name: 'item', price: 10 });
//sign the cart, this "signed" cart can be passed around
// freely and verified by anyone 
// (note: the private key is stored in the keypair object)
// you don't want anyone to have this as they can then
// sign anything as you
const signed = await cart.signedCart();
//verify the cart is authentic and unchanged
const verifiedCart = await cart.verifyCart(
  signed.signed,
  signed.keypair.key
);

Signed cart will be json that looks like this:

      {
        signature: 'Bb28VIN1E5HdTHrtqOocy9ZZxLQjpuBq8KGFfDlqRfsJ5TOmNmmkQlgEmV5YZ1BGUvy1U2fJUVN2dTat02koE99BVJOQfNCfa2XDyyz0wWT5h66izA8jfnUrV5sywXy5t2OUKgo-ub4fXMo9GQDC7OUhQeiyy2-DGQrquwyCuTu4WOS538KayuOoBTYoB5JlRj6IZBgf12ae1BFvYXYcQTwT3U3JR6q-swi02AIU2t2H5DjZgUetCedZC0ObcPl7v6VGP2RPHMCmPK17puz8hEnQU1TP1hCFP6aiSPeZ7uqkwjDpRw1mQIW3a-bowMkthUl8610Yb18Z0f70IQ2Bgg',
        protected: 'eyJhbGciOiJQUzI1NiIsImtpZCI6ImYxZmM2YzZlLWU0NGUtNDUzMS1hNGY1LWM2N2ZiNWQzYTRjYyIsImI2NCI6ZmFsc2UsImNyaXQiOlsiYjY0Il19',
        payload: { id: 1, message: 'hello' }
      }

public key will be JWK that looks like this:

      {
        signature: 'Bb28VIN1E5HdTHrtqOocy9ZZxLQjpuBq8KGFfDlqRfsJ5TOmNmmkQlgEmV5YZ1BGUvy1U2fJUVN2dTat02koE99BVJOQfNCfa2XDyyz0wWT5h66izA8jfnUrV5sywXy5t2OUKgo-ub4fXMo9GQDC7OUhQeiyy2-DGQrquwyCuTu4WOS538KayuOoBTYoB5JlRj6IZBgf12ae1BFvYXYcQTwT3U3JR6q-swi02AIU2t2H5DjZgUetCedZC0ObcPl7v6VGP2RPHMCmPK17puz8hEnQU1TP1hCFP6aiSPeZ7uqkwjDpRw1mQIW3a-bowMkthUl8610Yb18Z0f70IQ2Bgg',
        protected: 'eyJhbGciOiJQUzI1NiIsImtpZCI6ImYxZmM2YzZlLWU0NGUtNDUzMS1hNGY1LWM2N2ZiNWQzYTRjYyIsImI2NCI6ZmFsc2UsImNyaXQiOlsiYjY0Il19',
        payload: { id: 1, message: 'hello' }
      }

In the example, we overrode the default generated kid with 'testtest', alternatively you can not set any kid and import the public key with the kid in the JWS header and it will work fine.

Exporting and Importing a keypair

//create new keypair, export, and store somewhere secure
const composer = new JsonSignature();
const keypair = await composer.generateKeyPair();
//export the public key so folks verifying can use it
const pubKey = await composer.getPublicKey(keypair.key);
const message = { message: 'hello', id: 1 };
const signed = await composer.sign(keypair.key, message);

//import the public key and verify the message (in browser, another system, whatever)
const composer2 = new JsonSignature();
await composer2.setPublicKey('testtest', pubKey);
const verified = await composer2.verify(signed, 'testtest');
expect(verified).toEqual(message);