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

@nichoth/ratchet

v0.0.8

Published

Key ratcheting

Downloads

20

Readme

ratchet

tests types module semantic versioning Common Changelog install size license

Key ratcheting in typescript, implemented with noble crypto.

The way this works

When you create a message, we create a new keypair, and embed the public side in the message. Each message is encrypted with the new private key, + the public key embedded in the previous message.

It can be decrypted by the recipient because the recipient knows the previous secret key, and can combine this with the public key embedded in the message.

Read some API docs

install

npm i -S @nichoth/ratchet

Example

See the tests for more examples.

Create a new keypair

Create two keypairs -- ed25519, for signing, and x25519, for encryption.

import { create } from '@nichoth/ratchet'
const alice = create()

// => {
//    ed25519: {
//        privateKey:Uint8Array
//        publicKey:Uint8Array
//    };
//    x25519: {
//        privateKey:Uint8Array
//        publicKey:Uint8Array
//     }
//  }

Encrypt a new message

This returns an array of [Message, { keys }], where keys is the new keypair that was created for this message. A string version of the public key is embedded in the message.

You must pass in a DID string that is used as author field. This is passed in separately because we might want to use a different keypair for identity vs the keypair passed in, which is used for encryption.

function message (
    text:string|Uint8Array,
    theirPublicKey:string|X25519Keys['publicKey'],
    author:DID,
    newKeypair?:{
        privateKey:string|X25519Keys['publicKey'],
        publicKey: string|X25519Keys['privateKey']
    },
    info?:string
):[Message, { keys:X25519Keys }]
import { message } from '@nichoth/ratchet'

// a message from Alice to Bob
const [msg, { keys }] = message(
    'hello world',
    bob.x25519.publicKey,
    alicesDid
)

Pass in the public key from the previous message to ratchet the keys

const [newMsg, { keys }] = message(
    'hello again',
    prevMsg.keys.publicKey,
    alicesDid
)

Decrypt a message

Decrypt the given message with the matching private key.

import { decryptMsg } from '@nichoth/ratchet'

// pass in the message and the keypair containing the relevant secret key
const decrypted =  decryptMsg(msg, bob.x25519)

Decrypt a message with keys and the previous message

You can pass in the previous message and keys to decrypt.

import { decryptMsg } from '@nichoth/ratchet'

const decrypted = decryptMsg(newMessage, keys, previousMessage)

Types

All the key types are just aliases to Uint8Array.

Ed25519Keys

interface Ed25519Keys {
    privateKey:Uint8Array;
    publicKey:Uint8Array;
}

X25519Keys

type X25519Keys = Ed25519Keys;

DID

type DID = `did:key:z${string}`;

Keys

These are aliases to Uint8Array.

interface Keys {
    ed25519: {
        privateKey:Ed25519Keys['privateKey'];
        publicKey:Ed25519Keys['publicKey'];
    };
    x25519: {
        privateKey:X25519Keys['privateKey'];
        publicKey:X25519Keys['publicKey'];
    }
}

Message

interface Message {
    keys:{  // <-- base64pad encoded
        publicKey:string;
    };
    author:DID;
    body:{
        text:string;
    };
}

An encrypted message looks like this:

{
    keys: { publicKey: 'W+V510cXyL6LT8+MIT7KmE9+PccQtTOZwWNCYG+EVxY=' },
    author: 'did:key:z6Mker5GURbWxk3YxW8vet9dt1Mk55D97hzLDGBtSpMBm21S',
    body: {
        text: 'HnlVO3QvJJQhdqmM8EGnsJgmgYpu/GOXl2OR/EFPptk8RdGvLxxmG4vQQ2pNpm2JxEvlfoZC'
    }
}

Decrypted, it looks like this:

{
    keys: { publicKey: 'W+V510cXyL6LT8+MIT7KmE9+PccQtTOZwWNCYG+EVxY=' },
    author: 'did:key:z6Mker5GURbWxk3YxW8vet9dt1Mk55D97hzLDGBtSpMBm21S',
    body: { text: 'hello messages' }
}

Exports

This exposes ESM and common JS via package.json exports field.

ESM

import { create, encrypt } from '@nichoth/ratchet'

Common JS

const { create, encrypt } = require('@nichoth/ratchet')