Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

crypto-ld

digitalbazaar87.6kBSD-3-Clause7.0.0TypeScript support: definitely-typed

A Javascript library for generating and performing common operations on Linked Data cryptographic key pairs.

Decentralized, DID, Credential, Cryptography, Linked Data

readme

Cryptographic Key Pair Library for Linked Data (crypto-ld)

Node.js CI NPM Version

A Javascript library for generating and performing common operations on Linked Data cryptographic key pairs.

Table of Contents

Background

See also (related specs):

Supported Key Types (crypto-ld versions 4+)

This library provides general Linked Data cryptographic key generation functionality, but does not support any individual key type by default.

To use it, you must install individual driver libraries for each cryptographic key type. The following libraries are currently supported.

Type Crypto Suite Library Usage
Ed25519 Ed25519VerificationKey2020 (recommended), Ed25519VerificationKey2018 (legacy) ed25519-verification-key-2020 >=1.0 (recommended), ed25519-verification-key-2018 >=2.0 (legacy) Signatures, VCs, zCaps, DIDAuth
X25519/Curve25519 X25519KeyAgreementKey2019 x25519-key-agreement-key-2019 >=4.0 ECDH key agreement, JWE/CWE encryption with minimal-cipher
Secp256k1 EcdsaSecp256k1VerificationKey2019 ecdsa-secp256k1-verification-key-2019 Signatures, VCs, zCaps, DIDAuth, HD Wallets

Legacy Supported Key Types (crypto-ld versions <=3)

In the previous version (v3.x) of crypto-ld, the RSA and Ed25519 suites were bundled with crypto-ld (as opposed to residing in standalone packages). For previous usage instructions of bundled RSA, Ed25519 and standalone Curve25519/x25519-key-pair type keys, see the README for crypto-ld v3.9.

Choosing a Key Type

For digital signatures using the jsonld-signatures, signing of Verifiable Credentials using vc-js, authorization capabilities, and DIDAuth operations:

  • Prefer Ed25519VerificationKey2020 type keys, by default.
  • Use EcdsaSepc256k1 keys if your use case requires it (for example, if you're developing for a Bitcoin-based or Ethereum-based ledger), or if you require Hierarchical Deterministic (HD) wallet functionality.

For key agreement protocols for encryption operations:

Security

As with most security- and cryptography-related tools, the overall security of your system will largely depend on your design decisions.

Install

  • Node.js 14.0+ is required.

To install locally (for development):

git clone https://github.com/digitalbazaar/crypto-ld.git
cd crypto-ld
npm install

Usage

Installing Support for Key Types

In order to use this library, you will need to import and install driver libraries for key types you'll be working with via the use() method.

To use the library with one or more supported suites:

import {Ed25519VerificationKey2020} from '@digitalbazaar/ed25519-verification-key-2020';
import {X25519KeyAgreementKey2020} from '@digitalbazaar/x25519-key-agreement-key-2020';

import {CryptoLD} from 'crypto-ld';
const cryptoLd = new CryptoLD();

cryptoLd.use(Ed25519VerificationKey2020);
cryptoLd.use(X25519KeyAgreementKey2020);

const edKeyPair = await cryptoLd.generate({type: 'Ed25519VerificationKey2020'});

Generating a new public/private key pair

To generate a new public/private key pair: cryptoLd.generate(options):

  • {string} [type] Suite name, required.
  • {string} [controller] Optional controller URI or DID to initialize the generated key. (This will also init the key id.)
  • {string} [seed] Optional deterministic seed value (only supported by some key types, such as ed25519) from which to generate the key.

Importing a key pair from storage

To create an instance of a public/private key pair from data imported from storage, use cryptoLd.from():

const serializedKeyPair = { ... };

const keyPair = await cryptoLd.from(serializedKeyPair);

Note that only installed key types are supported, if you try to create a key pair via from() for an unsupported type, an error will be thrown.

Common individual key pair operations

The full range of operations will depend on key type. Here are some common operations supported by all key types.

Exporting the public key only

To export just the public key of a pair - use export():

keyPair.export({publicKey: true});
// ->
{
  type: 'Ed25519VerificationKey2020',
  id: 'did:example:1234#z6MkszZtxCmA2Ce4vUV132PCuLQmwnaDD5mw2L23fGNnsiX3',
  controller: 'did:example:1234',
  publicKeyMultibase: 'zEYJrMxWigf9boyeJMTRN4Ern8DJMoCXaLK77pzQmxVjf'
}

Exporting the full public-private key pair

To export the full key pair, including private key (warning: this should be a carefully considered operation, best left to dedicated Key Management Systems):

keyPair.export({publicKey: true, privateKey: true});
// ->
{
  type: 'Ed25519VerificationKey2020',
  id: 'did:example:1234#z6MkszZtxCmA2Ce4vUV132PCuLQmwnaDD5mw2L23fGNnsiX3',
  controller: 'did:example:1234',
  publicKeyMultibase: 'zEYJrMxWigf9boyeJMTRN4Ern8DJMoCXaLK77pzQmxVjf',
  privateKeyMultibase: 'z4E7Q4neNHwv3pXUNzUjzc6TTYspqn9Aw6vakpRKpbVrCzwKWD4hQDHnxuhfrTaMjnR8BTp9NeUvJiwJoSUM6xHAZ'
}

Generating and verifying key fingerprint

To generate a fingerprint:

keyPair.fingerprint();
// ->
'z6MkszZtxCmA2Ce4vUV132PCuLQmwnaDD5mw2L23fGNnsiX3'

To verify a fingerprint:

keyPair.verifyFingerprint({
  fingerprint: 'z6MkszZtxCmA2Ce4vUV132PCuLQmwnaDD5mw2L23fGNnsiX3'
});
// ->
{ valid: true }

For key pairs that are related to signature and verification (that extend from the LDVerifierKeyPair class), two additional operations must be supported:

Creating a signer function

In order to perform a cryptographic signature, you need to create a sign function, and then invoke it.

const keyPair = await cryptoLd.generate({type: 'Ed25519VerificationKey2020'});

const {sign} = keyPair.signer();

const data = 'test data to sign';
const signatureValue = await sign({data});

Creating a verifier function

In order to verify a cryptographic signature, you need to create a verify function, and then invoke it (passing it the data to verify, and the signature).

const keyPair = await cryptoLd.generate({type: 'Ed25519VerificationKey2020'});

const {verify} = keyPair.verifier();

const {valid} = await verify({data, signature});

Contribute

See the contribute file!

PRs accepted.

If editing the Readme, please conform to the standard-readme specification.

Commercial Support

Commercial support for this library is available upon request from Digital Bazaar: support@digitalbazaar.com

License

New BSD License (3-clause) © Digital Bazaar

changelog

crypto-ld ChangeLog

7.0.0 - 2022-06-01

Changed

  • BREAKING: Convert to module (ESM).
  • BREAKING: Require Node.js >=14.
  • Update dependencies.
  • Lint module.

6.0.0 - 2021-05-05

Changed

  • BREAKING: .from() now routes to .fromKeyDocument() if the serialized key object has a @context. This update is to make for more secure behavior when creating key pair instances from "untrusted" key objects (say, fetched from the web etc).

5.1.0 - 2021-04-01

Added

  • Implement CryptoLD.fromKeyId API.
  • Implement LDKeyPair.fromKeyDocument API.
  • Add support for revoked keys.

5.0.0 - 2021-03-16

Changed

  • BREAKING: Remove LDVerifierKeyPair subclass. Fold signer() and verifier() methods into parent LDKeyPair class.
  • BREAKING: export() is now a sync function (no reason for it to be async).
  • BREAKING: Remove keyPair.addPrivateKey() and keyPair.addPublicKey(). Subclasses will just need to override export() directly.

Upgrading from v4.x

The breaking changes in v5 do not affect any application code, they only affect key pair plugins such as https://github.com/digitalbazaar/ed25519-verification-key-2020. No changes necessary in application code upgrading from v5 from v4.

4.0.3 - 2020-11-25

Changed

  • Publish package.

4.0.2 - 2020-08-01

Changed

  • Fix use() suite usage.

4.0.1 - 2020-08-01

Changed

  • Removed unused sodium-native dependency.

4.0.0 - 2020-08-01

Changed

  • Implement chai-like .use() API for installing and specifying individual key types.
  • BREAKING: Extracted bundled Ed25519 and RSA key suites to their own libraries.
  • BREAKING: Remove deprecated .owner instance property
  • BREAKING: Remove deprecated .passphrase instance property, and the encrypt() and decrypt() methods (these are no longer used).
  • BREAKING: Remove deprecated/unused publicKey and privateKey properties.
  • BREAKING: Rename .publicNode() to .export({publicKey: true}).
  • BREAKING: .export() now requires explicitly stating whether you're exporting public or private key material.
  • BREAKING: Changed verifyFingerprint() to used named params.
  • BREAKING: Changed addPublicKey() and addPrivateKey() to used named params.

4.0.0 - Purpose

The previous design (v3.7 and earlier) bundled two key types with this library (RSA and Ed25519), which resulted in extraneous code and bundle size for projects that only used one of them (or used some other suite). The decision was made to extract those bundled suites to their own repositories, and to add a builder-style .use() API to crypto-ld so that client code could select just the suites they needed.

Since this was a comprehensive breaking change in usage, this also gave an opportunity to clean up and streamline the existing API, change function signatures to be consistent (for example, to consistently used named parameters), and to remove deprecated and unused APIs and properties.

Upgrading from v3.7.0

Since this is a comprehensive breaking change, you will need to audit and change pretty much all usage of crypto-ld and compatible key pairs. Specifically:

  • Ed25519 and RSA keys are no longer imported from crypto-ld, they'll need to be imported from their own packages.
  • Since key suites have been decoupled from crypto-ld, it means that this library should only be used when a project is using multiple key suites. If you're just using a single suite, then you can use that suite directly, without crypto-ld.
  • Most function param signatures have been changed to use {} style named params.

3.7.0 - 2019-09-06

Added

  • Add support for Node 12 Ed25519 generate, sign, and verify.
  • Make sodium-native an optional dependency.

3.6.0 - 2019-08-06

Added

  • Add LDKeyPair.fromFingerprint() to create an Ed25519KeyPair instance from a fingerprint (for use with did:key method code).

3.5.3 - 2019-07-16

Fixed

3.5.2 - 2019-04-16

Fixed

  • Fix incorrectly formatted engine tag in package file.

3.5.1 - 2019-04-09

Fixed

  • The util.base58PublicKeyFingerprint was released in error. It has been replaced by the Ed25519KeyPair.fingerprintFromPublicKey API contained in this release.

3.5.0 - 2019-04-08

Added

  • Add util.base58PublicKeyFingerprint helper for computing public key fingerprints. NOTE: this API was released in error, see release 3.5.1.

3.4.1 - 2019-03-27

Fixed

  • Fix Ed25519 fingerprint generation when running in the browser.

3.4.0 - 2019-02-26

Added

  • Enable use of a seed to generate deterministic Ed25519 keys.

3.3.0 - 2019-02-21

Changed

  • Improve error handling related to the decoding of key material in Ed25519KeyPair. This is helpful when dealing with key material that may be provided via command line or web UI.

3.2.0 - 2019-02-19

Changed

  • Remove sodium-universal dependency to reduce the size of the browser bundle.
  • Ed25519 operations in Node.js use sodium-native APIs.
  • Ed25519 operations in the browser use forge APIs.
  • Use base64url-universal which eliminates the need for a Buffer polyfill when this module is used in the browser.

3.1.0 - 2019-02-18

Changed

  • Use forge@0.8.0. The new rsa.generateKeyPair API automatically uses native implementation when available in nodejs >= 10.12.0.

3.0.0 - 2019-01-30

Changed

  • BREAKING: Make key fingerprints conform to the latest multibase/multicodec specification. The fingerprints generated by 2.x and 3.x are different due to encoding changes.
  • BREAKING: The only exports for this module are the three key classes: LDKeyPair, Ed25519KeyPair, and RSAKeyPair.

2.0.1 - 2019-01-24

Fixed

  • No need to bring in util in browser environment.

2.0.0 - 2019-01-16

Fixed

  • Specify published files.

Changed

  • BREAKING: Ed25519KeyPair now uses publicKeyBase58 and privateKeyBase58 attributes, instead of publicKeyBase and privateKeyBase
  • BREAKING: Changed signature of LDKeyPair.from() (one options param instead of a separate data and options)
  • Removed ursa support.
    • Node.js >= 10.12.0: use generateKeyPair().
    • Earlier Node.js and browsers: use forge.
    • NOTE: Newer Node.js versions are much faster at RSA key generation vs the forge fallback. It is highly recommended to use a newer Node.js.
  • Switch from chloride to sodium-universal.

Added

  • Added controller attribute (to use instead of the deprecated owner)
  • Added sign() and verify() factory functions for use with jsonld-signatures
  • Add Karma browser testing support.

1.0.0 - 2018-11-08

  • Change keyType to type (do match DID Doc usage), add key owner
  • Initial NPM release

0.1.0 - 2018-10-17

  • Moved LDKeyPair code from did-io