Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed DID DHT library regression where kid becomes undefined #575

Merged
merged 3 commits into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
7 changes: 4 additions & 3 deletions packages/dids/src/methods/did-dht.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1024,7 +1024,7 @@ export class DidDhtDocument {
case dnsRecordId.startsWith('k'): {
// Get the method ID fragment (id), key type (t), Base64URL-encoded public key (k), and
// optionally, controller (c) from the decoded TXT record data.
thehenrytsai marked this conversation as resolved.
Show resolved Hide resolved
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.
Copy link
Contributor

Choose a reason for hiding this comment

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

update comment

Copy link
Contributor Author

Choose a reason for hiding this comment

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

What would you like me to update the comment to? I'll do it!

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);
Copy link
Contributor

Choose a reason for hiding this comment

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

good call

});

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
Loading