Skip to content

Commit 269384b

Browse files
authored
Reintroduce ability to specify custom Verification Method IDs in did:dht (#637)
* allow verification method Id to be defined by the user * make previous toDNSPacket and fromDNSPacket tests more e2e * add DNS Packet e2e test for custom VM controller * explicit equality for `k0` DNS Record ID
1 parent 0de58f3 commit 269384b

File tree

3 files changed

+231
-105
lines changed

3 files changed

+231
-105
lines changed

.changeset/nervous-dingos-appear.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@web5/dids": patch
3+
---
4+
5+
Reintroduce ability to specify custom Verification Method IDs for `did:dht`

packages/dids/src/methods/did-dht.ts

+12-7
Original file line numberDiff line numberDiff line change
@@ -1022,9 +1022,9 @@ export class DidDhtDocument {
10221022

10231023
// Process verification methods.
10241024
case dnsRecordId.startsWith('k'): {
1025-
// Get the key type (t), Base64URL-encoded public key (k), and
1026-
// optionally, controller (c) from the decoded TXT record data.
1027-
const { t, k, c, a: parsedAlg } = DidDhtUtils.parseTxtDataToObject(answer.data);
1025+
// Get the key type (t), Base64URL-encoded public key (k), algorithm (a), and
1026+
// optionally, controller (c) or Verification Method ID (id) from the decoded TXT record data.
1027+
const { id, t, k, c, a: parsedAlg } = DidDhtUtils.parseTxtDataToObject(answer.data);
10281028

10291029
// Convert the public key from Base64URL format to a byte array.
10301030
const publicKeyBytes = Convert.base64Url(k).toUint8Array();
@@ -1037,15 +1037,15 @@ export class DidDhtDocument {
10371037

10381038
publicKey.alg = parsedAlg || KeyTypeToDefaultAlgorithmMap[Number(t) as DidDhtRegisteredKeyType];
10391039

1040-
// Determine the Key ID (kid): '0' for the identity key or JWK thumbprint for others.
1041-
const kid = dnsRecordId.endsWith('0') ? '0' : await computeJwkThumbprint({ jwk: publicKey });
1042-
publicKey.kid = kid;
1040+
// Determine the Verification Method ID: '0' for the identity key,
1041+
// the id from the TXT Data Object, or the JWK thumbprint if an explicity Verification Method ID not defined.
1042+
const vmId = dnsRecordId === 'k0' ? '0' : id !== undefined ? id : await computeJwkThumbprint({ jwk: publicKey });
10431043

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

10471047
// Prepend the DID URI to the ID fragment to form the full verification method ID.
1048-
const methodId = `${didUri}#${kid}`;
1048+
const methodId = `${didUri}#${vmId}`;
10491049

10501050
// Add the verification method to the DID document.
10511051
didDocument.verificationMethod.push({
@@ -1195,6 +1195,11 @@ export class DidDhtDocument {
11951195

11961196
// Define the data for the DNS TXT record.
11971197
const txtData = [`t=${keyType}`, `k=${publicKeyBase64Url}`];
1198+
// if the methodId is not the identity key or a thumbprint, explicity define the id within the DNS TXT record.
1199+
// otherwise the id can be inferred from the thumbprint.
1200+
if (methodId !== '0' && await computeJwkThumbprint({ jwk: publicKey }) !== methodId) {
1201+
txtData.unshift(`id=${methodId}`);
1202+
}
11981203

11991204
// Only set the algorithm property (`a`) if it differs from the default algorithm for the key type.
12001205
if(publicKey.alg !== KeyTypeToDefaultAlgorithmMap[keyType]) {

0 commit comments

Comments
 (0)