Javascript library for generating and working with Ed25519Multikey key pairs.
npm install @digitalbazaar/ed25519-multikey

> Javascript library for generating and working with Ed25519Multikey key pairs.
- Background
- Security
- Install
- Usage
- Contribute
- Commercial Support
- License
For use with:
* @digitalbazaar/eddsa-2022-cryptosuite ^1.0.0
crypto suite (with jsonld-signatures ^11.0.0)
* @digitalbazaar/data-integrity ^1.0.0
See also (related specs):
* Verifiable Credential Data Integrity
As with most security- and cryptography-related tools, the overall security of
your system will largely depend on your design decisions.
- Node.js 16+ is required.
To install locally (for development):
```
git clone https://github.com/digitalbazaar/ed25519-multikey.git
cd ed25519-multikey
npm install
To generate a new public/secret key pair:
* {string} [id] Optional id for the generated key.{string} [controller]
* Optional controller URI or DID to initialize the{string} [seed]
generated key. (This will also init the key id.)
* Optional deterministic seed value from which to generate the
key.
`js
import * as Ed25519Multikey from '@digitalbazaar/ed25519-multikey';
const edKeyPair = await Ed25519Multikey.generate();
`
To create an instance of a public/secret key pair from data imported from
storage, use .from():
`js
const serializedKeyPair = { ... };
const keyPair = await Ed25519Multikey.from(serializedKeyPair);
``
To export just the public key of a pair:
`js`
await keyPair.export({publicKey: true});
// ->
{
type: 'Multikey',
id: 'did:example:1234#z6Mkon3Necd6NkkyfoGoHxid2znGc59LU3K7mubaRcFbLfLX',
controller: 'did:example:1234',
publicKeyMultibase: 'z6Mkon3Necd6NkkyfoGoHxid2znGc59LU3K7mubaRcFbLfLX'
}
To export the full key pair, including secret key (warning: this should be a
carefully considered operation, best left to dedicated Key Management Systems):
`js`
await keyPair.export({publicKey: true, secretKey: true});
// ->
{
type: 'Multikey',
id: 'did:example:1234#z6Mkon3Necd6NkkyfoGoHxid2znGc59LU3K7mubaRcFbLfLX',
controller: 'did:example:1234',
publicKeyMultibase: 'z6Mkon3Necd6NkkyfoGoHxid2znGc59LU3K7mubaRcFbLfLX',
secretKeyMultibase: 'zruzf4Y29hDp7vLoV3NWzuymGMTtJcQfttAWzESod4wV2fbPvEp4XtzGp2VWwQSQAXMxDyqrnVurYg2sBiqiu1FHDDM'
}
In order to perform a cryptographic signature, you need to create a sign
function, and then invoke it.
`js
const keyPair = Ed25519Multikey.generate();
const {sign} = keyPair.signer();
// data is a Uint8Array of bytes
const data = (new TextEncoder()).encode('test data goes here');
// Signing also outputs a Uint8Array, which you can serialize to text etc.
const signatureValueBytes = await sign({data});
`
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).
`js
const keyPair = Ed25519Multikey.generate();
const {verify} = keyPair.verifier();
const valid = await verify({data, signature});
// true
`
If you have serialized and stored keys of the previous
Ed25519VerificationKey2020 key type (for example, generated usinged25519-verification-key-2020
the )
library, things to keep in mind:
* Instances of those key types still contain the same key material, the only
thing that has changed from the 2020 suite to Multikey is the property name
change for storing the secret key and replacing the type with Multikey.Ed25519VerificationKey2020
The 2020 suite key types serialize using the type privateKeyMultibase
and stored secret key material in , and the Ed25519Multikey
Multikey (this repo) serializes using corresponding the type ansecretKeyMultibase
stores the secret key material in property.Ed25519Multikey.from()
* You can convert from the 2020 key type to Multikey using the provided
method (see below).generate()
* They the same key material, given the same seed parameter.
* Both the 2020 and Multikey keys produce and verify the same signatures.
Example of converting:
`js
import * as Ed25519Multikey from '@digitalbazaar/ed25519-multikey';
import {Ed25519VerificationKey2020}
from '@digitalbazaar/ed25519-verification-key-2020';
const keyPair2020 = await Ed25519VerificationKey2020.generate({
controller: 'did:example:1234'
});
const ed25519Multikey = await Ed25519Multikey.from(keyPair2020);
// The resulting ed25519Multikey will have the same id and controller properties
// as its 2020 source. They will also produce and verify the same signatures.
// data is a Uint8Array of bytes
const data = (new TextEncoder()).encode('test data goes here');
const signatureBytes2020 = await keyPair2020.signer().sign({data});
// this is the same signature as that produced by the 2020 key. And will verify
// the same.
await ed25519Multikey.verifier().verify({data, signature: signatureBytes2020})
// true
`
If you have serialized and stored keys of the previous
Ed25519VerificationKey2018 key type (for example, generated usinged25519-verification-key-2018
the )
library, things to keep in mind:
* Instances of those key types still contain the same key material, the only
thing that has changed from the 2018 suite to Multikey is the way the public
and secret key material is serialized when exporting. The 2018 suite key
types serialize using the publicKeyBase58 and secretKeyBase58 properties,publicKeyMultibase
and the Ed25519 Multikey (this repo) serializes using corresponding
and secretKeyMultibase property.Ed25519Multikey.from()
* You can convert from the 2018 key type to Multikey using the provided
method (see below).generate()
* They the same key material, given the same seed parameter.
* Both the 2018 and Multikey keys produce and verify the same signatures.
Example of converting:
`js
import * as Ed25519Multikey from '@digitalbazaar/ed25519-multikey';
import {Ed25519VerificationKey2018}
from '@digitalbazaar/ed25519-verification-key-2018';
const keyPair2018 = await Ed25519VerificationKey2018.generate({
controller: 'did:example:1234'
});
const ed25519Multikey = await Ed25519Multikey.from(keyPair2018);
// The resulting ed25519Multikey will have the same id and controller properties
// as its 2018 source. They will also produce and verify the same signatures.
// data is a Uint8Array of bytes
const data = (new TextEncoder()).encode('test data goes here');
const signatureBytes2018 = await keyPair2018.signer().sign({data});
// this is the same signature as that produced by the 2020 key. And will verify
// the same.
await ed25519Multikey.verifier().verify({data, signature: signatureBytes2018})
// true
``
See the contribute file!
PRs accepted.
If editing the Readme, please conform to the
standard-readme specification.
Commercial support for this library is available upon request from
Digital Bazaar: support@digitalbazaar.com
New BSD License (3-clause) © 2020 Digital Bazaar