@digitalbazaar/ed25519-verification-key-2020
Advanced tools
Comparing version 3.1.0 to 3.2.0
# @digitalbazaar/ed25519-verification-key-2020 ChangeLog | ||
## 3.2.0 - 2021-10-15 | ||
### Added | ||
- Add support for `JsonWebKey2020` and JWK import/export, as well as | ||
JWK thumbprint function. | ||
## 3.1.0 - 2021-06-24 | ||
@@ -4,0 +10,0 @@ |
@@ -18,3 +18,6 @@ /*! | ||
return ed25519.generateKeyPairFromSeed(seed); | ||
}, | ||
async sha256digest({data}) { | ||
return crypto.subtle.digest('SHA-256', data); | ||
} | ||
}; |
@@ -7,2 +7,3 @@ /*! | ||
verify, | ||
createHash, | ||
createPrivateKey, | ||
@@ -69,2 +70,5 @@ createPublicKey, | ||
return verify(null, data, publicKey, signature); | ||
}, | ||
async sha256digest({data}) { | ||
return createHash('sha256').update(data).digest(); | ||
} | ||
@@ -71,0 +75,0 @@ }; |
@@ -5,2 +5,3 @@ /*! | ||
import * as base58btc from 'base58-universal'; | ||
import * as base64url from 'base64url-universal'; | ||
import ed25519 from './ed25519.js'; | ||
@@ -85,2 +86,8 @@ import {LDKeyPair} from 'crypto-ld'; | ||
static async from(options) { | ||
if(options.type === 'Ed25519VerificationKey2018') { | ||
return Ed25519VerificationKey2020.fromEd25519VerificationKey2018(options); | ||
} | ||
if(options.type === 'JsonWebKey2020') { | ||
return Ed25519VerificationKey2020.fromJsonWebKey2020(options); | ||
} | ||
return new Ed25519VerificationKey2020(options); | ||
@@ -118,2 +125,40 @@ } | ||
/** | ||
* Creates a key pair instance (public key only) from a JsonWebKey2020 | ||
* object. | ||
* | ||
* @see https://w3c-ccg.github.io/lds-jws2020/#json-web-key-2020 | ||
* | ||
* @param {object} options - Options hashmap. | ||
* @param {string} options.id - Key id. | ||
* @param {string} options.type - Key suite type. | ||
* @param {string} options.controller - Key controller. | ||
* @param {object} options.publicKeyJwk - JWK object. | ||
* | ||
* @returns {Promise<Ed25519VerificationKey2020>} Resolves with key pair. | ||
*/ | ||
static fromJsonWebKey2020({id, type, controller, publicKeyJwk} = {}) { | ||
if(type !== 'JsonWebKey2020') { | ||
throw new TypeError(`Invalid key type: "${type}".`); | ||
} | ||
if(!publicKeyJwk) { | ||
throw new TypeError('"publicKeyJwk" property is required.'); | ||
} | ||
const {kty, crv} = publicKeyJwk; | ||
if(kty !== 'OKP') { | ||
throw new TypeError('"kty" is required to be "OKP".'); | ||
} | ||
if(crv !== 'Ed25519') { | ||
throw new TypeError('"crv" is required to be "Ed25519".'); | ||
} | ||
const {x: publicKeyBase64Url} = publicKeyJwk; | ||
const publicKeyMultibase = _encodeMbKey( | ||
MULTICODEC_ED25519_PUB_HEADER, | ||
base64url.decode(publicKeyBase64Url)); | ||
return Ed25519VerificationKey2020.from({ | ||
id, controller, publicKeyMultibase | ||
}); | ||
} | ||
/** | ||
* Generates a KeyPair with an optional deterministic seed. | ||
@@ -161,2 +206,5 @@ * | ||
/** | ||
* @returns {Uint8Array} Public key bytes. | ||
*/ | ||
get _publicKeyBuffer() { | ||
@@ -176,2 +224,5 @@ if(!this.publicKeyMultibase) { | ||
/** | ||
* @returns {Uint8Array} Private key bytes. | ||
*/ | ||
get _privateKeyBuffer() { | ||
@@ -243,2 +294,58 @@ if(!this.privateKeyMultibase) { | ||
/** | ||
* Returns the JWK representation of this key pair. | ||
* | ||
* @see https://datatracker.ietf.org/doc/html/rfc8037 | ||
* | ||
* @param {object} [options={}] - Options hashmap. | ||
* @param {boolean} [options.publicKey] - Include public key? | ||
* @param {boolean} [options.privateKey] - Include private key? | ||
* | ||
* @returns {{kty: string, crv: string, x: string, d: string}} JWK | ||
* representation. | ||
*/ | ||
toJwk({publicKey = true, privateKey = false} = {}) { | ||
if(!(publicKey || privateKey)) { | ||
throw TypeError('Either a "publicKey" or a "privateKey" is required.'); | ||
} | ||
const jwk = {crv: 'Ed25519', kty: 'OKP'}; | ||
if(publicKey) { | ||
jwk.x = base64url.encode(this._publicKeyBuffer); | ||
} | ||
if(privateKey) { | ||
jwk.d = base64url.encode(this._privateKeyBuffer); | ||
} | ||
return jwk; | ||
} | ||
/** | ||
* @see https://datatracker.ietf.org/doc/html/rfc8037#appendix-A.3 | ||
* | ||
* @returns {Promise<string>} JWK Thumbprint. | ||
*/ | ||
async jwkThumbprint() { | ||
const publicKey = base64url.encode(this._publicKeyBuffer); | ||
const serialized = `{"crv":"Ed25519","kty":"OKP","x":"${publicKey}"}`; | ||
const data = new TextEncoder().encode(serialized); | ||
return base64url.encode( | ||
new Uint8Array(await ed25519.sha256digest({data}))); | ||
} | ||
/** | ||
* Returns the JsonWebKey2020 representation of this key pair. | ||
* | ||
* @see https://w3c-ccg.github.io/lds-jws2020/#json-web-key-2020 | ||
* | ||
* @returns {Promise<object>} JsonWebKey2020 representation. | ||
*/ | ||
async toJsonWebKey2020() { | ||
return { | ||
'@context': 'https://w3id.org/security/jws/v1', | ||
id: this.controller + '#' + await this.jwkThumbprint(), | ||
type: 'JsonWebKey2020', | ||
controller: this.controller, | ||
publicKeyJwk: this.toJwk({publicKey: true}) | ||
}; | ||
} | ||
/** | ||
* Tests whether the fingerprint was generated from a given key pair. | ||
@@ -245,0 +352,0 @@ * |
{ | ||
"name": "@digitalbazaar/ed25519-verification-key-2020", | ||
"version": "3.1.0", | ||
"version": "3.2.0", | ||
"description": "Javascript library for generating and working with Ed25519VerificationKey2020 key pairs, for use with crypto-ld.", | ||
@@ -19,2 +19,3 @@ "homepage": "https://github.com/digitalbazaar/ed25519-verification-key-2020", | ||
"base58-universal": "^1.0.0", | ||
"base64url-universal": "^1.1.0", | ||
"crypto-ld": "^5.1.0", | ||
@@ -21,0 +22,0 @@ "esm": "^3.2.25" |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
32645
594
5
+ Addedbase64url-universal@^1.1.0
+ Addedbase64url@3.0.1(transitive)
+ Addedbase64url-universal@1.1.0(transitive)