Skip to content

Commit

Permalink
Add documentLoader and update zcap context.
Browse files Browse the repository at this point in the history
  • Loading branch information
JSAssassin authored and mattcollier committed Apr 26, 2021
1 parent 4b18be3 commit f16ad79
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 6 deletions.
3 changes: 3 additions & 0 deletions lib/kms.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
51 changes: 47 additions & 4 deletions lib/zcaps.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,55 @@
'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',
'Ed25519VerificationKey2020',
'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} = {}) => {
Expand All @@ -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()
};
Expand Down Expand Up @@ -206,6 +248,7 @@ exports.delegate = async ({zcap, signer, capabilityChain}) => {
purpose: new CapabilityDelegation({
capabilityChain
}),
compactProof: false
compactProof: false,
documentLoader: createDocumentLoader({signer})
});
};
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down

0 comments on commit f16ad79

Please sign in to comment.