@digitalbazaar/ed25519-verification-key-2020
Advanced tools
Comparing version 1.0.0 to 2.0.0
# @digitalbazaar/ed25519-verification-key-2020 ChangeLog | ||
## 2.0.0 - 2021-03-17 | ||
## Changed | ||
- Update to use `crypto-ld v5.0`. | ||
- **BREAKING**: Removed helper methods `addPublicKey` and `addPrivateKey`. | ||
## Added | ||
- Add `Ed25519VerificationKey2020.fromEd25519VerificationKey2018()` method, | ||
for backwards compatibility with the `Ed25519VerificationKey2018` key type. | ||
See "Converting from previous Ed25519VerificationKey2018 key type" section | ||
of the README for details. | ||
## 1.0.0 - 2021-02-27 | ||
Initial version. |
/*! | ||
* Copyright (c) 2020 Digital Bazaar, Inc. All rights reserved. | ||
* Copyright (c) 2021 Digital Bazaar, Inc. All rights reserved. | ||
*/ | ||
import * as base58btc from 'base58-universal'; | ||
import ed25519 from './ed25519.js'; | ||
import {LDVerifierKeyPair} from 'crypto-ld'; | ||
import {LDKeyPair} from 'crypto-ld'; | ||
const SUITE_ID = 'Ed25519VerificationKey2020'; | ||
export class Ed25519VerificationKey2020 extends LDVerifierKeyPair { | ||
export class Ed25519VerificationKey2020 extends LDKeyPair { | ||
/** | ||
@@ -57,2 +57,26 @@ * An implementation of the Ed25519VerificationKey2020 spec, for use with | ||
/** | ||
* Instance creation method for backwards compatibility with the | ||
* `Ed25519VerificationKey2018` key suite. | ||
* | ||
* @see https://github.com/digitalbazaar/ed25519-verification-key-2018 | ||
* @typedef {object} Ed25519VerificationKey2018 | ||
* @param {Ed25519VerificationKey2018} keyPair - Ed25519 2018 suite key pair. | ||
* | ||
* @returns {Ed25519VerificationKey2020} - 2020 suite instance. | ||
*/ | ||
static fromEd25519VerificationKey2018({keyPair} = {}) { | ||
const keyPair2020 = new Ed25519VerificationKey2020({ | ||
id: keyPair.id, | ||
controller: keyPair.controller, | ||
publicKeyMultibase: `z${keyPair.publicKeyBase58}` | ||
}); | ||
if(keyPair.privateKeyBase58) { | ||
keyPair2020.privateKeyMultibase = `z${keyPair.privateKeyBase58}`; | ||
} | ||
return keyPair2020; | ||
} | ||
/** | ||
* Generates a KeyPair with an optional deterministic seed. | ||
@@ -141,28 +165,32 @@ * | ||
/** | ||
* Adds a public key base to the key pair object. | ||
* Exports the serialized representation of the KeyPair | ||
* and other information that json-ld Signatures can use to form a proof. | ||
* | ||
* @param {object} options - Options hashmap. | ||
* @param {object} options.key - Key pair object being exported. | ||
* @param {object} [options={}] - Options hashmap. | ||
* @param {boolean} [options.publicKey] - Export public key material? | ||
* @param {boolean} [options.privateKey] - Export private key material? | ||
* | ||
* @returns {object} A PublicKeyNode, with key material. | ||
* @returns {object} A plain js object that's ready for serialization | ||
* (to JSON, etc), for use in DIDs, Linked Data Proofs, etc. | ||
*/ | ||
addPublicKey({key}) { | ||
key.publicKeyMultibase = this.publicKeyMultibase; | ||
return key; | ||
export({publicKey = false, privateKey = false} = {}) { | ||
if(!(publicKey || privateKey)) { | ||
throw new TypeError( | ||
'Export requires specifying either "publicKey" or "privateKey".'); | ||
} | ||
const exportedKey = { | ||
id: this.id, | ||
type: this.type, | ||
controller: this.controller | ||
}; | ||
if(publicKey) { | ||
exportedKey.publicKeyMultibase = this.publicKeyMultibase; | ||
} | ||
if(privateKey) { | ||
exportedKey.privateKeyMultibase = this.privateKeyMultibase; | ||
} | ||
return exportedKey; | ||
} | ||
/** | ||
* Adds the private key material to the key pair object. | ||
* | ||
* @param {object} options - Options hashmap. | ||
* @param {object} options.key - Key pair object being exported. | ||
* | ||
* @returns {object} The keyNode with encoded private key material. | ||
*/ | ||
addPrivateKey({key}) { | ||
key.privateKeyMultibase = this.privateKeyMultibase; | ||
return key; | ||
} | ||
/** | ||
* Tests whether the fingerprint was generated from a given key pair. | ||
@@ -169,0 +197,0 @@ * |
{ | ||
"name": "@digitalbazaar/ed25519-verification-key-2020", | ||
"version": "1.0.0", | ||
"version": "2.0.0", | ||
"description": "Javascript library for generating and working with Ed25519VerificationKey2020 key pairs, for use with crypto-ld.", | ||
@@ -19,6 +19,7 @@ "homepage": "https://github.com/digitalbazaar/ed25519-verification-key-2020", | ||
"base58-universal": "^1.0.0", | ||
"crypto-ld": "^4.0.2", | ||
"crypto-ld": "^5.0.0", | ||
"esm": "^3.2.25" | ||
}, | ||
"devDependencies": { | ||
"@digitalbazaar/ed25519-verification-key-2018": "^3.0.0", | ||
"chai": "^4.2.0", | ||
@@ -59,3 +60,3 @@ "cross-env": "^7.0.2", | ||
"test-node": "cross-env NODE_ENV=test mocha -r esm --preserve-symlinks -t 10000 test/**/*.spec.js", | ||
"lint": "eslint . --fix" | ||
"lint": "eslint ." | ||
}, | ||
@@ -62,0 +63,0 @@ "nyc": { |
@@ -136,4 +136,6 @@ # Ed25519VerificationKey2020 Key Pair Library for Linked Data _(@digitalbazaar/ed25519-verification-key-2020)_ | ||
const data = Buffer.from('test data to sign', 'utf8'); | ||
const signatureValue = await sign({data}); | ||
// 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}); | ||
``` | ||
@@ -155,3 +157,49 @@ | ||
### Converting from previous Ed25519VerificationKey2018 key type | ||
If you have serialized and stored keys of the previous | ||
`Ed25519VerificationKey2018` key type (for example, generated using | ||
the [`ed25519-verification-key-2018`](https://github.com/digitalbazaar/ed25519-verification-key-2018)) | ||
library, or using the `Ed25519KeyPair` keys bundled with `crypto-ld v3.x`), | ||
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 the 2020 suite is the way the public | ||
and private key material is serialized when exporting. The 2018 suite key | ||
types serialize using the `publicKeyBase58` and `privateKeyBase58` properties, | ||
and the 2020 suite key (this repo) serializes using corresponding | ||
`publicKeyMultibase` and `privateKeyMultibase` property. | ||
* You can convert from the 2018 key type to the 2020 key type using the provided | ||
`Ed25519VerificationKey2020.fromEd25519VerificationKey2018()` method (see below). | ||
* They `generate()` the same key material, given the same `seed` parameter. | ||
* Both the 2018 and 2020 keys produce and verify the same signatures. | ||
Example of converting: | ||
```js | ||
import {Ed25519VerificationKey2018} | ||
from '@digitalbazaar/ed25519-verification-key-2018'; | ||
import {Ed25519VerificationKey2020} | ||
from '@digitalbazaar/ed25519-verification-key-2020'; | ||
const keyPair2018 = await Ed25519VerificationKey2018.generate({ | ||
controller: 'did:example:1234' | ||
}); | ||
const keyPair2020 = await Ed25519VerificationKey2020 | ||
.fromEd25519VerificationKey2018({keyPair: keyPair2018}); | ||
// The resulting keyPair2020 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 keyPair2020.verifier().verify({data, signature: signatureBytes2018}) | ||
// true | ||
``` | ||
## Contribute | ||
@@ -158,0 +206,0 @@ |
25184
417
220
22
+ Addedcrypto-ld@5.1.0(transitive)
- Removedcrypto-ld@4.0.3(transitive)
Updatedcrypto-ld@^5.0.0