Skip to content

Commit eef9396

Browse files
authored
Always set alg and kid on expansion (#518)
* update * update to use default key type
1 parent 3a1561f commit eef9396

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

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

+20-1
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,15 @@ const AlgorithmToKeyTypeMap = {
394394
secp256r1 : DidDhtRegisteredKeyType.secp256r1
395395
} as const;
396396

397+
/**
398+
* Private helper that maps did dht registered key types to their corresponding default algorithm identifiers.
399+
*/
400+
const KeyTypeToDefaultAlgorithmMap = {
401+
[DidDhtRegisteredKeyType.Ed25519] : 'Ed25519',
402+
[DidDhtRegisteredKeyType.secp256k1] : 'ES256K',
403+
[DidDhtRegisteredKeyType.secp256r1] : 'ES256',
404+
};
405+
397406
/**
398407
* The `DidDht` class provides an implementation of the `did:dht` DID method.
399408
*
@@ -1015,7 +1024,7 @@ export class DidDhtDocument {
10151024
case dnsRecordId.startsWith('k'): {
10161025
// Get the method ID fragment (id), key type (t), Base64URL-encoded public key (k), and
10171026
// optionally, controller (c) from the decoded TXT record data.
1018-
const { id, t, k, c } = DidDhtUtils.parseTxtDataToObject(answer.data);
1027+
const { id, t, k, c, a: parsedAlg } = DidDhtUtils.parseTxtDataToObject(answer.data);
10191028

10201029
// Convert the public key from Base64URL format to a byte array.
10211030
const publicKeyBytes = Convert.base64Url(k).toUint8Array();
@@ -1026,6 +1035,11 @@ export class DidDhtDocument {
10261035
// Convert the public key from a byte array to JWK format.
10271036
let publicKey = await DidDhtUtils.keyConverter(namedCurve).bytesToPublicKey({ publicKeyBytes });
10281037

1038+
publicKey.alg = parsedAlg || KeyTypeToDefaultAlgorithmMap[Number(t) as DidDhtRegisteredKeyType];
1039+
1040+
// Determine the Key ID (kid): '0' for the identity key or JWK thumbprint for others.
1041+
publicKey.kid = dnsRecordId.endsWith('0') ? '0' : await computeJwkThumbprint({ jwk: publicKey });
1042+
10291043
// Initialize the `verificationMethod` array if it does not already exist.
10301044
didDocument.verificationMethod ??= [];
10311045

@@ -1181,6 +1195,11 @@ export class DidDhtDocument {
11811195
// Define the data for the DNS TXT record.
11821196
const txtData = [`t=${keyType}`, `k=${publicKeyBase64Url}`];
11831197

1198+
// Only set the algorithm property (`a`) if it differs from the default algorithm for the key type.
1199+
if(publicKey.alg !== KeyTypeToDefaultAlgorithmMap[keyType]) {
1200+
txtData.push(`a=${publicKey.alg}`);
1201+
}
1202+
11841203
// Add the controller property, if set to a value other than the Identity Key (DID Subject).
11851204
if (verificationMethod.controller !== didDocument.id) txtData.push(`c=${verificationMethod.controller}`);
11861205

0 commit comments

Comments
 (0)