@@ -394,6 +394,15 @@ const AlgorithmToKeyTypeMap = {
394
394
secp256r1 : DidDhtRegisteredKeyType . secp256r1
395
395
} as const ;
396
396
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
+
397
406
/**
398
407
* The `DidDht` class provides an implementation of the `did:dht` DID method.
399
408
*
@@ -1015,7 +1024,7 @@ export class DidDhtDocument {
1015
1024
case dnsRecordId . startsWith ( 'k' ) : {
1016
1025
// Get the method ID fragment (id), key type (t), Base64URL-encoded public key (k), and
1017
1026
// 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 ) ;
1019
1028
1020
1029
// Convert the public key from Base64URL format to a byte array.
1021
1030
const publicKeyBytes = Convert . base64Url ( k ) . toUint8Array ( ) ;
@@ -1026,6 +1035,11 @@ export class DidDhtDocument {
1026
1035
// Convert the public key from a byte array to JWK format.
1027
1036
let publicKey = await DidDhtUtils . keyConverter ( namedCurve ) . bytesToPublicKey ( { publicKeyBytes } ) ;
1028
1037
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
+
1029
1043
// Initialize the `verificationMethod` array if it does not already exist.
1030
1044
didDocument . verificationMethod ??= [ ] ;
1031
1045
@@ -1181,6 +1195,11 @@ export class DidDhtDocument {
1181
1195
// Define the data for the DNS TXT record.
1182
1196
const txtData = [ `t=${ keyType } ` , `k=${ publicKeyBase64Url } ` ] ;
1183
1197
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
+
1184
1203
// Add the controller property, if set to a value other than the Identity Key (DID Subject).
1185
1204
if ( verificationMethod . controller !== didDocument . id ) txtData . push ( `c=${ verificationMethod . controller } ` ) ;
1186
1205
0 commit comments