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:
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():
To export the full key pair, including private key (warning: this should be a
carefully considered operation, best left to dedicated Key Management Systems):
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).