Skip to content

Commit

Permalink
dht resolve test vector impl
Browse files Browse the repository at this point in the history
  • Loading branch information
nitro-neal committed Feb 13, 2024
1 parent d97cb9a commit 4757e0d
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
4 changes: 4 additions & 0 deletions packages/dids/src/methods/did-dht.ts
Original file line number Diff line number Diff line change
Expand Up @@ -889,6 +889,10 @@ export class DidDhtDocument {
// Read the Fetch Response stream into a byte array.
const messageBytes = await response.arrayBuffer();

if(!messageBytes) {
throw new DidError(DidErrorCode.NotFound, `Pkarr record not found for: ${identifier}`);
}

if (messageBytes.byteLength < 72) {
throw new DidError(DidErrorCode.InvalidDidDocumentLength, `Pkarr response must be at least 72 bytes but got: ${messageBytes.byteLength}`);
}
Expand Down
12 changes: 12 additions & 0 deletions packages/dids/tests/methods/did-dht.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import type { PortableDid } from '../../src/types/portable-did.js';

import { DidErrorCode } from '../../src/did-error.js';
import { DidDht, DidDhtRegisteredDidType } from '../../src/methods/did-dht.js';
import DidDhtResolveTestVector from '../../../../web5-spec/test-vectors/did_dht/resolve.json' assert { type: 'json' };


// Helper function to create a mocked fetch response that fails and returns a 404 Not Found.
const fetchNotFoundResponse = () => ({
Expand Down Expand Up @@ -802,4 +804,14 @@ describe('DidDht', () => {
expect(didResolutionResult.didResolutionMetadata).to.have.property('error', 'invalidDidDocumentLength');
});
});


describe('Web5TestVectorsDidDht', () => {
it('resolve', async () => {
for (const vector of DidDhtResolveTestVector.vectors) {
const didResolutionResult = await DidDht.resolve(vector.input.didUri);
expect(didResolutionResult.didResolutionMetadata.error).to.equal(vector.output.didResolutionMetadata.error);
}
});
});
});
2 changes: 1 addition & 1 deletion web5-spec

0 comments on commit 4757e0d

Please sign in to comment.