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

@keeex/js-keys

v4.5.3

Published

Manage keys for digital signatures

Downloads

148

Readme

KeeeX Key handling library

This library provide a Keys class allowing easy generation, exportation, importation and usage of cryptographic keys for the purpose of digital signature.

Currently only support the "Bitcoin" type of keys, which exposes their public key as a Bitcoin address.

Quick how-to

The @keeex/js-keys library uses the @keeex/crypto library to generate cryptographically secure random keys.

Initialize crypto provider

To make sure the correct cryptographic is imported before using this library. See the package @keeex/crypto for more details about provider.

import "@keeex/crypto-provider-node";

Manipulating keys

To generate a new key:

import {
  generateKey,
  KeyType,
} from "@keeex/js-keys/lib/keys";

generateKey(KeyType.bitcoin).then(key => console.log(key));

To export a key as JSON:

const key = /* get your key object */
key.exportKey(false, {type: "json"}).then(
  exportedData => console.log(Buffer.from(exportedData).toString())
);

exportedData is an ArrayBuffer. In the case of a JSON export the ArrayBuffer will contain text data. The first argument of exportKey() is "publicOnly", to only export public data.

To import a key from a previous JSON export:

import {
  importKey,
  KeyType,
} from "@keeex/js-keys/lib/keys";

importKey(KeyType.bitcoin, {type:"json", value: keyData})
  .then(key => console.log(key));

To export a key protected by a password:

const key = /* get your key object */
key.exportKey(
    false,
    {
      type: "json",
      password: "somepass",
    }
  )
).then(secureKey => ...);

To import a key protected by a password:

import {
  importKey,
  KeyType,
} from "@keeex/js-keys/lib/keys";

importKey(KeyType.bitcoin, {
  type: "json",
  password: "somepass",
  value: previouslySealedKey,
});

Signature/verification

To sign:

key.sign(data).then(signature => ...);

Both data and signature are ArrayBuffer

To verify:

key.verify(data, signature).then(signOk => ...);

Both data and signature are ArrayBuffer; signOk is a boolean.

In the case of bitcoin-message signature, it is possible to convert the signature to a string.

Data encryption/decryption

It is possible to encrypt data only knowing the recipient's public key.

There are many functions and methods to tweak the process, but the main usage is:

import {Bundle} from "@keeex/js-keys/lib/bundle";
import "@keeex/crypto-provider-node";
import {generateKey, KeyType} from "@keeex/js-keys/lib/keys";
import assert from "assert";

const main = async(): Promise<void> => {
  // Recipients only need publicKey part
  const recipientKey1 = await generateKey(KeyType.bitcoin);
  const recipientKey2 = await generateKey(KeyType.bitcoin);

  // The data and metadata to encrypt
  const inputData = new Uint8Array([1, 2, 3, 4]).buffer;
  const inputMetadata = new Uint8Array([5, 6, 7, 8]).buffer;

  const bigBundle = await Bundle.createBundle(
    [
      recipientKey1,
      recipientKey2,
    ],
    {
      metadata: inputMetadata,
      data: inputData,
    },
  );
  const encryptedMetadataBundle = await bigBundle.saveBundle(["metadata"]);
  const encryptedDataBundle = await bigBundle.saveBundle(["data"]);

  // Decrypt data
  // For this, the recipient key must have the private part
  const readBundleMetadata = await Bundle.loadBundle(encryptedMetadataBundle);
  const readBundleData = await Bundle.loadBundle(encryptedDataBundle);
  const decryptedMetadata = await readBundleMetadata.getData(
    recipientKey1,
    "metadata",
  );
  const decryptedData = await readBundleData.getData(recipientKey1, "data");
  assert.deepStrictEqual(decryptedMetadata, inputMetadata);
  assert.deepStrictEqual(decryptedData, inputData);
  console.log("Ok");
};
main().catch(e => console.error(e));