@digitalcredentials/crypto-ld

中文文档
7.0.6发布于 2022/10/20BSD-3-ClauseTypeScriptESM + CJS零依赖近 30 天下载 4924

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

原始使用文档
文档目录15 节
  1. Table of Contents
  2. Background
  3. Supported Key Types
  4. Choosing a Key Type
  5. Security
  6. Install
  7. Usage
  8. Installing Support for Key Types
  9. Generating a new public/private key pair
  10. Importing a key pair from storage
  11. Common individual key pair operations
  12. Operations on signature-related key pairs
  13. Contribute
  14. Commercial Support
  15. License

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

Build status 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

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.

TypeCrypto SuiteLibraryUsage
Ed25519Ed25519VerificationKey2020 (recommended), Ed25519VerificationKey2018 (legacy)ed25519-verification-key-2020 >=1.0 (recommended), ed25519-verification-key-2018 >=2.0 (legacy)Signatures, VCs, zCaps, DIDAuth
X25519/Curve25519X25519KeyAgreementKey2019x25519-key-agreement-key-2019 >=4.0ECDH key agreement, JWE/CWE encryption with minimal-cipher
Secp256k1EcdsaSecp256k1VerificationKey2019ecdsa-secp256k1-verification-key-2019Signatures, VCs, zCaps, DIDAuth, HD Wallets

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/digitalcredentials/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 '@digitalcredentials/ed25519-verification-key-2020';
import {X25519KeyAgreementKey2020} from '@digitalcredentials/x25519-key-agreement-key-2020';

import {CryptoLD} from '@digitalcredentials/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: [email protected]

License

New BSD License (3-clause) © Digital Bazaar

版本与发布记录

@digitalcredentials/crypto-ld 共发布 5 个版本,首次发布于 2022/10/18;过去 12 个月发布了 0 个版本。

版本发布日期距今
7.0.6latest2022/10/204 年前
7.0.52022/10/204 年前
7.0.42022/10/204 年前
7.0.32022/10/184 年前
7.0.22022/10/184 年前

关于 @digitalcredentials/crypto-ld 的常见问题

@digitalcredentials/crypto-ld 是什么?

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

如何安装 @digitalcredentials/crypto-ld?

在项目目录运行 npm install @digitalcredentials/crypto-ld,或使用 pnpm add / yarn add / bun add。

@digitalcredentials/crypto-ld 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@digitalcredentials/crypto-ld 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@digitalcredentials/crypto-ld 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=16。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@digitalcredentials/crypto-ld 可以商用吗?

@digitalcredentials/crypto-ld 使用 BSD-3-Clause 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。另外不得用原作者名义为衍生产品背书。以上为概括,不构成法律意见,请以许可原文为准。

@digitalcredentials/crypto-ld 的下载量有多少?

根据 npm 官方下载统计,@digitalcredentials/crypto-ld 在 2026/10/12 – 2026/11/10 这 30 天内被下载 4,924 次,日均约 164 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@digitalcredentials/crypto-ld 还在维护吗?

最新版本 7.0.6 发布于 2022/10/20(4 年前);至今共发布 5 个版本,其中过去 12 个月发布了 0 个;首次发布于 2022/10/18。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。