diff --git a/lib/kms.js b/lib/kms.js index 6130209..2ca9362 100644 --- a/lib/kms.js +++ b/lib/kms.js @@ -77,6 +77,9 @@ exports.generateKey = async ( } else if(type === 'Ed25519VerificationKey2018') { type = 'Ed25519VerificationKey2018'; Class = AsymmetricKey; + } else if(type === 'Ed25519VerificationKey2020') { + type = 'Ed25519VerificationKey2020'; + Class = AsymmetricKey; } else if(type === 'keyAgreement' || type === 'X25519KeyAgreementKey2019') { type = 'X25519KeyAgreementKey2019'; Class = KeyAgreementKey; diff --git a/lib/zcaps.js b/lib/zcaps.js index 785eda8..269a5fb 100644 --- a/lib/zcaps.js +++ b/lib/zcaps.js @@ -4,12 +4,23 @@ 'use strict'; const {CapabilityDelegation} = require('ocapld'); +const context2018 = require('ed25519-signature-2018-context'); +const context2020 = require('ed25519-signature-2020-context'); const {EdvClient} = require('edv-client'); -const jsigs = require('jsonld-signatures'); const {Ed25519Signature2018} = require('@digitalbazaar/ed25519-signature-2018'); const {Ed25519Signature2020} = require('@digitalbazaar/ed25519-signature-2020'); +const { + documentLoaderFactory, + contexts, +} = require('@transmute/jsonld-document-loader'); +const {sign, SECURITY_CONTEXT_V2_URL} = require('jsonld-signatures'); -const {SECURITY_CONTEXT_V2_URL, sign} = jsigs; +const {constants: {CONTEXT_URL: CONTEXT_URL_2018}} = context2018; +const {constants: {CONTEXT_URL: CONTEXT_URL_2020}} = context2020; + +const v1 = require('did-veres-one'); +const veresDriver = v1.driver(); +const didKeyDriver = require('did-method-key').driver(); const SUPPORTED_KEY_TYPES = [ 'Ed25519VerificationKey2018', @@ -17,6 +28,31 @@ const SUPPORTED_KEY_TYPES = [ 'Sha256HmacKey2019', 'X25519KeyAgreementKey2019' ]; +const createDocumentLoader = ({signer}) => { + let contextsValue; + if(signer.type === 'Ed25519VerificationKey2018') { + contextsValue = { + ...contexts.W3ID_Security_Vocabulary, + SECURITY_CONTEXT_V2_URL, + 'https://w3id.org/security/ed25519-signature-2018/v1': context2018 + .contexts.get('https://w3id.org/security/ed25519-signature-2018/v1') + }; + } else if(signer.type === 'Ed25519VerificationKey2020') { + contextsValue = { + 'https://w3id.org/security/ed25519-signature-2020/v1': context2020 + .contexts.get('https://w3id.org/security/ed25519-signature-2020/v1') + }; + } + return documentLoaderFactory.pluginFactory.build({contexts: contextsValue}) + .addResolver({ + ['did:key']: { + resolve: did => didKeyDriver.get({did}) + }, + ['did:v1']: { + resolve: did => veresDriver.get({did}) + } + }).buildDocumentLoader(); +}; exports.delegateCapability = async ( {signer, keystoreAgent, edvClient, request} = {}) => { @@ -30,8 +66,14 @@ exports.delegateCapability = async ( '"invocationTarget" must be an object that includes a "type".'); } // TODO: Look into requiring an invoker or controller on a zcap + let contextUrl; + if(signer.type === 'Ed25519VerificationKey2018') { + contextUrl = [SECURITY_CONTEXT_V2_URL, CONTEXT_URL_2018]; + } else if(signer.type === 'Ed25519VerificationKey2020') { + contextUrl = CONTEXT_URL_2020; + } let zcap = { - '@context': SECURITY_CONTEXT_V2_URL, + '@context': contextUrl, // use 128-bit random multibase encoded value id: await exports.id() }; @@ -206,6 +248,7 @@ exports.delegate = async ({zcap, signer, capabilityChain}) => { purpose: new CapabilityDelegation({ capabilityChain }), - compactProof: false + compactProof: false, + documentLoader: createDocumentLoader({signer}) }); }; diff --git a/package.json b/package.json index cdb9cd1..7180343 100644 --- a/package.json +++ b/package.json @@ -23,15 +23,18 @@ }, "homepage": "https://github.com/digitalbazaar/bedrock-profile", "dependencies": { + "@digitalbazaar/ed25519-signature-2018": "github:digitalbazaar/@digitalbazaar/ed25519-signature-2018#initial", + "@digitalbazaar/ed25519-signature-2020": "github:digitalbazaar/@digitalbazaar/ed25519-signature-2020#initial", + "@transmute/jsonld-document-loader": "^0.2.0", "assert-plus": "^1.0.0", "base64url-universal": "^1.1.0", "crypto-ld": "^3.7.0", "did-method-key": "^0.7.0", "did-veres-one": "^12.1.1", + "ed25519-signature-2018-context": "github:digitalbazaar/@digitalbazaar/ed25519-signature-2018-context#initial", + "ed25519-signature-2020-context": "github:digitalbazaar/@digitalbazaar/ed25519-signature-2020-context#initial", "edv-client": "^6.0.1", "jsonld-signatures": "^5.1.0", - "@digitalbazaar/ed25519-signature-2018": "github:digitalbazaar/@digitalbazaar/ed25519-signature-2018#initial", - "@digitalbazaar/ed25519-signature-2020": "github:digitalbazaar/@digitalbazaar/ed25519-signature-2020#initial", "ocapld": "^2.0.0", "webkms-client": "^3.0.0" },