Skip to content

Commit

Permalink
did dht mock publishing
Browse files Browse the repository at this point in the history
  • Loading branch information
gabe authored and frankhinek committed Nov 6, 2023
1 parent 80c8d71 commit 29860a4
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 23 deletions.
16 changes: 10 additions & 6 deletions packages/dids/tests/dht.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { expect } from 'chai';
import { Jose } from '@web5/crypto';
import sinon from 'sinon';

import type { DidDhtKeySet } from '../src/did-dht.js';
import type { DidKeySetVerificationMethodKey, DidService } from '../src/types.js';
Expand All @@ -15,6 +16,9 @@ describe('DidDht', () => {
const publicCryptoKey = await Jose.jwkToCryptoKey({ key: ks.identityKey.publicKeyJwk });
const privateCryptoKey = await Jose.jwkToCryptoKey({ key: ks.identityKey.privateKeyJwk });

const dhtPublishSpy = sinon.stub(DidDht, 'publishDidDocument').resolves(true);
const dhtGetSpy = sinon.stub(DidDht, 'getDidDocument').resolves(document);

const published = await DidDht.publishDidDocument({
keyPair: {
publicKey : publicCryptoKey,
Expand All @@ -25,10 +29,6 @@ describe('DidDht', () => {

expect(published).to.be.true;

// wait for propagation
const wait = ms => new Promise(resolve => setTimeout(resolve, ms));
await wait(1000*10);

const gotDid = await DidDht.getDidDocument({ did: document.id });
expect(gotDid.id).to.deep.equal(document.id);
expect(gotDid.capabilityDelegation).to.deep.equal(document.capabilityDelegation);
Expand All @@ -41,7 +41,11 @@ describe('DidDht', () => {
expect(gotDid.verificationMethod[0].controller).to.deep.equal(document.verificationMethod[0].controller);
expect(gotDid.verificationMethod[0].publicKeyJwk.kid).to.deep.equal(document.verificationMethod[0].publicKeyJwk.kid);
expect(gotDid.verificationMethod[0].publicKeyJwk.kty).to.deep.equal(document.verificationMethod[0].publicKeyJwk.kty);
}).timeout(15000); // 15 seconds

sinon.assert.calledOnce(dhtPublishSpy);
sinon.assert.calledOnce(dhtGetSpy);
sinon.restore();
});

describe('Codec', async () => {
it('encodes and decodes a DID Document as a DNS Packet', async () => {
Expand Down Expand Up @@ -81,4 +85,4 @@ describe('DidDht', () => {
expect(document.verificationMethod[1].publicKeyJwk.kty).to.deep.equal(decoded.verificationMethod[1].publicKeyJwk.kty);
});
});
});
});
75 changes: 58 additions & 17 deletions packages/dids/tests/did-dht.spec.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import chai, { expect } from 'chai';
import chaiAsPromised from 'chai-as-promised';
import sinon from 'sinon';

import type { DidDhtKeySet } from '../src/did-dht.js';
import type { DidKeySetVerificationMethodKey, DidService } from '../src/types.js';
import type {DidKeySetVerificationMethodKey, DidService, PortableDid} from '../src/types.js';

import { DidDhtMethod } from '../src/did-dht.js';

chai.use(chaiAsPromised);

const wait = ms => new Promise(resolve => setTimeout(resolve, ms));

describe('DidDhtMethod', () => {
describe('keypairs', () => {
it('should generate a key pair', async () => {
Expand Down Expand Up @@ -201,33 +200,71 @@ describe('DidDhtMethod', () => {
});

describe('did publishing and resolving', function () {
this.timeout(20000); // 20 seconds

it('should publish and get a did document', async () => {
const { document, keySet } = await DidDhtMethod.create();
const isPublished = await DidDhtMethod.publish({ keySet, didDocument: document });

expect(isPublished).to.be.true;
const dhtDidPublishSpy = sinon.stub(DidDhtMethod, 'publish').resolves(true);
const dhtDidResolutionSpy = sinon.stub(DidDhtMethod, 'resolve').resolves({
'@context' : 'https://w3id.org/did-resolution/v1',
didDocument : document,
didDocumentMetadata : {},
didResolutionMetadata : {
contentType : 'application/did+ld+json',
error : 'invalidDid',
errorMessage : `Cannot parse DID: ${document.id}`
}
});

// wait for propagation
await wait(1000*10);
const isPublished = await DidDhtMethod.publish({ keySet, didDocument: document });
expect(isPublished).to.be.true;

const didResolutionResult = await DidDhtMethod.resolve({ didUrl: document.id });
const didDocument = didResolutionResult.didDocument;
expect(didDocument.id).to.deep.equal(document.id);
expect(didDocument.service).to.deep.equal(document.service);
expect(didDocument.verificationMethod[0].id).to.deep.equal(document.verificationMethod[0].id);
expect(didDocument.verificationMethod[0].type).to.deep.equal(document.verificationMethod[0].type);
expect(didDocument.verificationMethod[0].controller).to.deep.equal(document.verificationMethod[0].controller);
expect(didDocument.verificationMethod[0].publicKeyJwk.kid).to.deep.equal(document.verificationMethod[0].publicKeyJwk.kid);

sinon.assert.calledOnce(dhtDidPublishSpy);
sinon.assert.calledOnce(dhtDidResolutionSpy);
sinon.restore();
});

it('should create with publish and get a did document', async () => {
const mockDocument: PortableDid = {
keySet : 'any' as any,
did : 'did:dht:123456789abcdefghi',
document : {
id : 'did:dht:123456789abcdefghi',
verificationMethod : [{
id : 'did:dht:123456789abcdefghi#0',
type : 'JsonWebKey2020',
controller : 'did:dht:123456789abcdefghi',
publicKeyJwk : {
kty : 'OKP',
crv : 'Ed25519',
kid : '0',
x : 'O2onvM62pC1io6jQKm8Nc2UyFXcd4kOmOsBIoYtZ2ik'
}
}],
assertionMethod : ['did:dht:123456789abcdefghi#0'],
authentication : ['did:dht:123456789abcdefghi#0'],
capabilityDelegation : ['did:dht:123456789abcdefghi#0'],
capabilityInvocation : ['did:dht:123456789abcdefghi#0']
}
};
const didDhtCreateSpy = sinon.stub(DidDhtMethod, 'create').resolves(mockDocument);

const { document } = await DidDhtMethod.create({ publish: true });
const did = document.id;

// wait for propagation
await wait(1000*10);
const dhtDidResolutionSpy = sinon.stub(DidDhtMethod, 'resolve').resolves({
'@context' : 'https://w3id.org/did-resolution/v1',
didDocument : document,
didDocumentMetadata : {},
didResolutionMetadata : {
contentType : 'application/did+ld+json',
error : 'invalidDid',
errorMessage : `Cannot parse DID: ${document.id}`
}
});

const didResolutionResult = await DidDhtMethod.resolve({ didUrl: did });
const resolvedDocument = didResolutionResult.didDocument;
Expand All @@ -237,6 +274,10 @@ describe('DidDhtMethod', () => {
expect(resolvedDocument.verificationMethod[0].type).to.deep.equal(document.verificationMethod[0].type);
expect(resolvedDocument.verificationMethod[0].controller).to.deep.equal(document.verificationMethod[0].controller);
expect(resolvedDocument.verificationMethod[0].publicKeyJwk.kid).to.deep.equal(document.verificationMethod[0].publicKeyJwk.kid);

sinon.assert.calledOnce(didDhtCreateSpy);
sinon.assert.calledOnce(dhtDidResolutionSpy);
sinon.restore();
});
});
});
});

0 comments on commit 29860a4

Please sign in to comment.