Skip to content

Commit

Permalink
Fixed DID DHT library regression where kid becomes undefined (#575)
Browse files Browse the repository at this point in the history
- Fixed DID DHT library regression where kid becomes undefined.
- Added tests to prevent this from happening again in vector tests.

Co-authored-by: Liran Cohen <c.liran.c@gmail.com>

---------

Co-authored-by: Liran Cohen <c.liran.c@gmail.com>
  • Loading branch information
thehenrytsai and LiranCohen committed May 16, 2024
1 parent eef9396 commit 98eeb4c
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/few-glasses-brake.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@web5/dids": patch
---

Fixed DID DHT library regression where `kid` becomes `undefined`
9 changes: 5 additions & 4 deletions packages/dids/src/methods/did-dht.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1022,9 +1022,9 @@ export class DidDhtDocument {

// Process verification methods.
case dnsRecordId.startsWith('k'): {
// Get the method ID fragment (id), key type (t), Base64URL-encoded public key (k), and
// Get the key type (t), Base64URL-encoded public key (k), and
// optionally, controller (c) from the decoded TXT record data.
const { id, t, k, c, a: parsedAlg } = DidDhtUtils.parseTxtDataToObject(answer.data);
const { t, k, c, a: parsedAlg } = DidDhtUtils.parseTxtDataToObject(answer.data);

// Convert the public key from Base64URL format to a byte array.
const publicKeyBytes = Convert.base64Url(k).toUint8Array();
Expand All @@ -1038,13 +1038,14 @@ export class DidDhtDocument {
publicKey.alg = parsedAlg || KeyTypeToDefaultAlgorithmMap[Number(t) as DidDhtRegisteredKeyType];

// Determine the Key ID (kid): '0' for the identity key or JWK thumbprint for others.
publicKey.kid = dnsRecordId.endsWith('0') ? '0' : await computeJwkThumbprint({ jwk: publicKey });
const kid = dnsRecordId.endsWith('0') ? '0' : await computeJwkThumbprint({ jwk: publicKey });
publicKey.kid = kid;

// Initialize the `verificationMethod` array if it does not already exist.
didDocument.verificationMethod ??= [];

// Prepend the DID URI to the ID fragment to form the full verification method ID.
const methodId = `${didUri}#${id}`;
const methodId = `${didUri}#${kid}`;

// Add the verification method to the DID document.
didDocument.verificationMethod.push({
Expand Down
20 changes: 18 additions & 2 deletions packages/dids/tests/methods/did-dht.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1172,20 +1172,29 @@ describe('DidDhtDocument', () => {
// vectors come from https://did-dht.com/#test-vectors
describe('Official DID:DHT Vector tests', () => {
it('vector 1', async () => {
const inputDidDocument = officialTestVector1.didDocument as DidDocument;
const dnsPacket = await DidDhtDocument.toDnsPacket({
didDocument : officialTestVector1.didDocument as DidDocument,
didDocument : inputDidDocument,
didMetadata : { published: false }
});

expect(dnsPacket.answers).to.have.length(officialTestVector1.dnsRecords.length);

const normalizedConstructedRecords = normalizeDnsRecords(dnsPacket.answers!);
expect(normalizedConstructedRecords).to.deep.include.members(officialTestVector1.dnsRecords);

const didResolutionResult = await DidDhtDocument.fromDnsPacket({
didUri : inputDidDocument.id,
dnsPacket : dnsPacket
});

expect(didResolutionResult.didDocument).to.deep.equal(inputDidDocument);
});

it('vector 2', async () => {
const inputDidDocument = officialTestVector2.didDocument as DidDocument;
const dnsPacket = await DidDhtDocument.toDnsPacket({
didDocument : officialTestVector2.didDocument as DidDocument,
didDocument : inputDidDocument,
didMetadata : {
published : false,
types : [DidDhtRegisteredDidType.Organization, DidDhtRegisteredDidType.Government, DidDhtRegisteredDidType.Corporation]
Expand All @@ -1197,6 +1206,13 @@ describe('Official DID:DHT Vector tests', () => {

const normalizedConstructedRecords = normalizeDnsRecords(dnsPacket.answers!);
expect(normalizedConstructedRecords).to.deep.include.members(officialTestVector2.dnsRecords);

const didResolutionResult = await DidDhtDocument.fromDnsPacket({
didUri : inputDidDocument.id,
dnsPacket : dnsPacket
});

expect(didResolutionResult.didDocument).to.deep.equal(inputDidDocument);
});
});

Expand Down

0 comments on commit 98eeb4c

Please sign in to comment.