Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@
"uuid": "^9.0.1"
},
"devDependencies": {
"@digitalbazaar/did-method-key": "^3.0.0",
"@digitalbazaar/did-method-key": "^5.2.0",
"@digitalbazaar/ed25519-multikey": "^1.1.0",
"@digitalbazaar/ed25519-signature-2020": "^5.0.0",
"@digitalbazaar/ed25519-verification-key-2020": "^4.1.0",
"c8": "^8.0.1",
"chai": "^4.3.6",
"cross-env": "^7.0.3",
Expand Down
113 changes: 89 additions & 24 deletions tests/unit/EzcapClient.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@
* Copyright (c) 2020-2023 Digital Bazaar, Inc. All rights reserved.
*/
import * as didKey from '@digitalbazaar/did-method-key';
import {getCapabilitySigners, ZcapClient} from '../../lib/index.js';
import * as Ed25519Multikey from '@digitalbazaar/ed25519-multikey';
import chai from 'chai';
import {Ed25519Signature2020} from '@digitalbazaar/ed25519-signature-2020';
import {Ed25519VerificationKey2020} from
'@digitalbazaar/ed25519-verification-key-2020';
import {ZcapClient} from '../../lib/index.js';

chai.should();
const {expect} = chai;
Expand All @@ -13,59 +16,114 @@ const didKeyDriver = didKey.driver();
describe('ZcapClient', () => {
describe('constructor', () => {
it('should create an ZcapClient using a didDocument', async () => {
const {didDocument, keyPairs} = await didKeyDriver.generate();
didKeyDriver.use({
multibaseMultikeyHeader: 'z6Mk',
fromMultibase: Ed25519VerificationKey2020.from
});
const verificationKeyPair = await Ed25519VerificationKey2020.generate();
const {didDocument, keyPairs} =
await didKeyDriver.fromKeyPair({verificationKeyPair});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, we might want to just remove this test. keyPairs would need to be updated for this to work properly. Someone might also copy this example and it isn't correct now. The keyPairs would need to be updated in some way to enable the use of signers.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, that's what this issue is about: #26

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In preparation for a fix somehow somewhere here, I think the appropriate thing is to demonstrate how to properly initialize the client. And therefore proper keys should be passed in etc. We now recognize that successfully calling the constructor does not produce a fully operational client instance.

const zcapClient = new ZcapClient({
SuiteClass: Ed25519Signature2020,
didDocument, keyPairs
});

expect(zcapClient).to.exist;
});
it('should create an ZcapClient using signers', async () => {
const {didDocument, keyPairs} = await didKeyDriver.generate();
const {invocationSigner, delegationSigner} = getCapabilitySigners({
didDocument, keyPairs});
const verificationKeyPair = await Ed25519VerificationKey2020.generate();
didKeyDriver.use({
multibaseMultikeyHeader: 'z6Mk',
fromMultibase: Ed25519VerificationKey2020.from
});
const {didDocument} =
await didKeyDriver.fromKeyPair({verificationKeyPair});
// this `id` value manifest as the `verificationMethod` on the proof
verificationKeyPair.id = didDocument.verificationMethod[0].id;
const zcapClient = new ZcapClient({
SuiteClass: Ed25519Signature2020,
invocationSigner, delegationSigner
invocationSigner: verificationKeyPair.signer(),
delegationSigner: verificationKeyPair.signer(),
});

expect(zcapClient).to.exist;
});
it('should delegate a root zcap', async () => {
const {didDocument, keyPairs} = await didKeyDriver.generate();
const {invocationSigner, delegationSigner} = getCapabilitySigners({
didDocument, keyPairs});
const verificationKeyPair = await Ed25519VerificationKey2020.generate();
didKeyDriver.use({
multibaseMultikeyHeader: 'z6Mk',
fromMultibase: Ed25519VerificationKey2020.from
});
const {didDocument} =
await didKeyDriver.fromKeyPair({verificationKeyPair});
// this `id` value manifest as the `verificationMethod` on the proof
// see the assertion below
verificationKeyPair.id = didDocument.verificationMethod[0].id;
const zcapClient = new ZcapClient({
SuiteClass: Ed25519Signature2020,
invocationSigner, delegationSigner
invocationSigner: verificationKeyPair.signer(),
delegationSigner: verificationKeyPair.signer(),
});
expect(zcapClient).to.exist;

const url = 'https://zcap.example/items';
const controller =
'did:key:z6MkogR2ZPr4ZGvLV2wZ7cWUamNMhpg3bkVeXARDBrKQVn2c';
'did:key:z6MkogR2ZPr4ZGvLV2wZ7cWUamNMhpg3bkVeXARDBrKQVn2c';
const delegatedZcap = await zcapClient.delegate({
invocationTarget: url, controller
});
delegatedZcap.parentCapability.should.equal(
'urn:zcap:root:' + encodeURIComponent(url));
delegatedZcap.controller.should.equal(controller);
delegatedZcap.proof.proofPurpose.should.equal('capabilityDelegation');
delegatedZcap.proof.capabilityChain.should.have.length(1);
delegatedZcap.proof.verificationMethod.should.equal(
verificationKeyPair.id);
});
it('should delegate a root zcap using Ed25519Multikey', async () => {
const verificationKeyPair = await Ed25519Multikey.generate();
didKeyDriver.use({
multibaseMultikeyHeader: 'z6Mk',
fromMultibase: Ed25519Multikey.from
});
const {didDocument} =
await didKeyDriver.fromKeyPair({verificationKeyPair});
// this `id` value manifest as the `verificationMethod` on the proof
verificationKeyPair.id = didDocument.verificationMethod[0].id;
const zcapClient = new ZcapClient({
SuiteClass: Ed25519Signature2020,
invocationSigner: verificationKeyPair.signer(),
delegationSigner: verificationKeyPair.signer(),
});
expect(zcapClient).to.exist;
const url = 'https://zcap.example/items';
const controller =
'did:key:z6MkogR2ZPr4ZGvLV2wZ7cWUamNMhpg3bkVeXARDBrKQVn2c';
const delegatedZcap = await zcapClient.delegate({
invocationTarget: url, controller
});

delegatedZcap.parentCapability.should.equal(
'urn:zcap:root:' + encodeURIComponent(url));
delegatedZcap.controller.should.equal(controller);
delegatedZcap.proof.proofPurpose.should.equal('capabilityDelegation');
delegatedZcap.proof.capabilityChain.should.have.length(1);
delegatedZcap.proof.verificationMethod.should.equal(
verificationKeyPair.id);
});
it('should throw error if controller is not provided when delegating zcap',
async () => {
const {didDocument, keyPairs} = await didKeyDriver.generate();
const {invocationSigner, delegationSigner} = getCapabilitySigners({
didDocument, keyPairs});
const verificationKeyPair = await Ed25519VerificationKey2020.generate();
didKeyDriver.use({
multibaseMultikeyHeader: 'z6Mk',
fromMultibase: Ed25519VerificationKey2020.from
});
const {didDocument} =
await didKeyDriver.fromKeyPair({verificationKeyPair});
// this `id` value manifest as the `verificationMethod` on the proof
verificationKeyPair.id = didDocument.verificationMethod[0].id;
const zcapClient = new ZcapClient({
SuiteClass: Ed25519Signature2020,
invocationSigner, delegationSigner
invocationSigner: verificationKeyPair.signer(),
delegationSigner: verificationKeyPair.signer(),
});
expect(zcapClient).to.exist;

const url = 'https://zcap.example/items';
let err;
let delegatedZcap;
Expand All @@ -82,12 +140,19 @@ describe('ZcapClient', () => {
'"controller" must be a string expressing an absolute URI.');
});
it('should delegate a deeper zcap chain', async () => {
const {didDocument, keyPairs} = await didKeyDriver.generate();
const {invocationSigner, delegationSigner} = getCapabilitySigners({
didDocument, keyPairs});
const verificationKeyPair = await Ed25519VerificationKey2020.generate();
didKeyDriver.use({
multibaseMultikeyHeader: 'z6Mk',
fromMultibase: Ed25519VerificationKey2020.from
});
const {didDocument} =
await didKeyDriver.fromKeyPair({verificationKeyPair});
// this `id` value manifest as the `verificationMethod` on the proof
verificationKeyPair.id = didDocument.verificationMethod[0].id;
const zcapClient = new ZcapClient({
SuiteClass: Ed25519Signature2020,
invocationSigner, delegationSigner
invocationSigner: verificationKeyPair.signer(),
delegationSigner: verificationKeyPair.signer(),
});
expect(zcapClient).to.exist;

Expand Down