@enbox/dids 0.0.4 → 0.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/browser.mjs +1 -1
- package/dist/browser.mjs.map +4 -4
- package/dist/esm/methods/did-dht-dns.js +455 -0
- package/dist/esm/methods/did-dht-dns.js.map +1 -0
- package/dist/esm/methods/did-dht-pkarr.js +168 -0
- package/dist/esm/methods/did-dht-pkarr.js.map +1 -0
- package/dist/esm/methods/did-dht-types.js +116 -0
- package/dist/esm/methods/did-dht-types.js.map +1 -0
- package/dist/esm/methods/did-dht-utils.js +143 -0
- package/dist/esm/methods/did-dht-utils.js.map +1 -0
- package/dist/esm/methods/did-dht.js +65 -842
- package/dist/esm/methods/did-dht.js.map +1 -1
- package/dist/esm/methods/did-ion-utils.js +161 -0
- package/dist/esm/methods/did-ion-utils.js.map +1 -0
- package/dist/esm/methods/did-ion.js +4 -151
- package/dist/esm/methods/did-ion.js.map +1 -1
- package/dist/esm/methods/did-key-utils.js +235 -0
- package/dist/esm/methods/did-key-utils.js.map +1 -0
- package/dist/esm/methods/did-key.js +6 -222
- package/dist/esm/methods/did-key.js.map +1 -1
- package/dist/esm/utils.js +15 -19
- package/dist/esm/utils.js.map +1 -1
- package/dist/types/methods/did-dht-dns.d.ts +114 -0
- package/dist/types/methods/did-dht-dns.d.ts.map +1 -0
- package/dist/types/methods/did-dht-pkarr.d.ts +56 -0
- package/dist/types/methods/did-dht-pkarr.d.ts.map +1 -0
- package/dist/types/methods/did-dht-types.d.ts +286 -0
- package/dist/types/methods/did-dht-types.d.ts.map +1 -0
- package/dist/types/methods/did-dht-utils.d.ts +54 -0
- package/dist/types/methods/did-dht-utils.d.ts.map +1 -0
- package/dist/types/methods/did-dht.d.ts +42 -457
- package/dist/types/methods/did-dht.d.ts.map +1 -1
- package/dist/types/methods/did-ion-utils.d.ts +86 -0
- package/dist/types/methods/did-ion-utils.d.ts.map +1 -0
- package/dist/types/methods/did-ion.d.ts +2 -82
- package/dist/types/methods/did-ion.d.ts.map +1 -1
- package/dist/types/methods/did-key-utils.d.ts +138 -0
- package/dist/types/methods/did-key-utils.d.ts.map +1 -0
- package/dist/types/methods/did-key.d.ts +3 -124
- package/dist/types/methods/did-key.d.ts.map +1 -1
- package/dist/types/utils.d.ts +25 -25
- package/dist/types/utils.d.ts.map +1 -1
- package/dist/utils.js +1 -1
- package/dist/utils.js.map +4 -4
- package/package.json +4 -4
- package/src/methods/did-dht-dns.ts +516 -0
- package/src/methods/did-dht-pkarr.ts +192 -0
- package/src/methods/did-dht-types.ts +316 -0
- package/src/methods/did-dht-utils.ts +157 -0
- package/src/methods/did-dht.ts +122 -1128
- package/src/methods/did-ion-utils.ts +186 -0
- package/src/methods/did-ion.ts +14 -190
- package/src/methods/did-key-utils.ts +258 -0
- package/src/methods/did-key.ts +14 -266
- package/src/utils.ts +31 -30
|
@@ -7,211 +7,27 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
7
7
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
8
|
});
|
|
9
9
|
};
|
|
10
|
-
|
|
11
|
-
var t = {};
|
|
12
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
13
|
-
t[p] = s[p];
|
|
14
|
-
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
15
|
-
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
16
|
-
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
17
|
-
t[p[i]] = s[p[i]];
|
|
18
|
-
}
|
|
19
|
-
return t;
|
|
20
|
-
};
|
|
21
|
-
import bencode from 'bencode';
|
|
22
|
-
import { Convert } from '@enbox/common';
|
|
23
|
-
import { AUTHORITATIVE_ANSWER, decode as dnsPacketDecode, encode as dnsPacketEncode } from '@dnsquery/dns-packet';
|
|
24
|
-
import { computeJwkThumbprint, Ed25519, LocalKeyManager, Secp256k1, Secp256r1, X25519 } from '@enbox/crypto';
|
|
10
|
+
import { computeJwkThumbprint, LocalKeyManager } from '@enbox/crypto';
|
|
25
11
|
import { BearerDid } from '../bearer-did.js';
|
|
26
12
|
import { Did } from '../did.js';
|
|
27
13
|
import { DidMethod } from './did-method.js';
|
|
28
|
-
import { DidVerificationRelationship } from '../types/did-core.js';
|
|
29
14
|
import { EMPTY_DID_RESOLUTION_RESULT } from '../types/did-resolution.js';
|
|
30
15
|
import { extractDidFragment } from '../utils.js';
|
|
31
16
|
import { DidError, DidErrorCode } from '../did-error.js';
|
|
17
|
+
// Re-export types from the new modules for backward compatibility.
|
|
18
|
+
export * from './did-dht-types.js';
|
|
19
|
+
// Re-export standalone functions from extracted modules.
|
|
20
|
+
export { pkarrGet, pkarrPut, createBep44PutMessage, parseBep44GetMessage, } from './did-dht-pkarr.js';
|
|
21
|
+
export { AlgorithmToKeyTypeMap, chunkDataIfNeeded, createTxtRecord, DID_DHT_SPECIFICATION_VERSION, DNS_RECORD_TTL, fromDnsPacket, KeyTypeToDefaultAlgorithmMap, parseTxtDataToObject, parseTxtDataToString, PROPERTY_SEPARATOR, toDnsPacket, VALUE_SEPARATOR, } from './did-dht-dns.js';
|
|
22
|
+
export { identifierToIdentityKey, identifierToIdentityKeyBytes, identityKeyToIdentifier, keyConverter, validatePreviousDidProof, } from './did-dht-utils.js';
|
|
23
|
+
import { createBep44PutMessage, parseBep44GetMessage, pkarrGet, pkarrPut, } from './did-dht-pkarr.js';
|
|
24
|
+
import { AlgorithmToKeyTypeMap, chunkDataIfNeeded, fromDnsPacket, parseTxtDataToObject, parseTxtDataToString, toDnsPacket, } from './did-dht-dns.js';
|
|
25
|
+
import { identifierToIdentityKey, identifierToIdentityKeyBytes, identityKeyToIdentifier, keyConverter, validatePreviousDidProof, } from './did-dht-utils.js';
|
|
32
26
|
/**
|
|
33
27
|
* The default DID DHT Gateway or Pkarr Relay server to use when publishing and resolving DID
|
|
34
28
|
* documents.
|
|
35
29
|
*/
|
|
36
30
|
const DEFAULT_GATEWAY_URI = process.env.DID_DHT_GATEWAY_URI || 'https://enbox-did-dht.fly.dev';
|
|
37
|
-
/**
|
|
38
|
-
* The version of the DID DHT specification that is implemented by this library.
|
|
39
|
-
*
|
|
40
|
-
* When a DID DHT document is published to the DHT network, the version of the specification that
|
|
41
|
-
* was used to create the document is included in the DNS TXT record for the root record. This
|
|
42
|
-
* allows clients to determine whether the DID DHT document is compatible with the client's
|
|
43
|
-
* implementation of the DID DHT specification. The version number is not present in the
|
|
44
|
-
* corresponding DID document.
|
|
45
|
-
*
|
|
46
|
-
* @see {@link https://did-dht.com | DID DHT Method Specification}
|
|
47
|
-
*/
|
|
48
|
-
const DID_DHT_SPECIFICATION_VERSION = 0;
|
|
49
|
-
/**
|
|
50
|
-
* The default TTL for DNS records published to the DHT network.
|
|
51
|
-
*
|
|
52
|
-
* The recommended TTL value is 7200 seconds (2 hours) since it matches the default TTL for
|
|
53
|
-
* Mainline DHT records.
|
|
54
|
-
*/
|
|
55
|
-
const DNS_RECORD_TTL = 7200;
|
|
56
|
-
/**
|
|
57
|
-
* Character used to separate distinct elements or entries in the DNS packet representation
|
|
58
|
-
* of a DID Document.
|
|
59
|
-
*
|
|
60
|
-
* For example, verification methods, verification relationships, and services are separated by
|
|
61
|
-
* semicolons (`;`) in the root record:
|
|
62
|
-
* ```
|
|
63
|
-
* vm=k1;auth=k1;asm=k2;inv=k3;del=k3;srv=s1
|
|
64
|
-
* ```
|
|
65
|
-
*/
|
|
66
|
-
const PROPERTY_SEPARATOR = ';';
|
|
67
|
-
/**
|
|
68
|
-
* Character used to separate distinct values within a single element or entry in the DNS packet
|
|
69
|
-
* representation of a DID Document.
|
|
70
|
-
*
|
|
71
|
-
* For example, multiple key references for the `authentication` verification relationships are
|
|
72
|
-
* separated by commas (`,`):
|
|
73
|
-
* ```
|
|
74
|
-
* auth=0,1,2
|
|
75
|
-
* ```
|
|
76
|
-
*/
|
|
77
|
-
const VALUE_SEPARATOR = ',';
|
|
78
|
-
/**
|
|
79
|
-
* Represents an optional extension to a DID Document’s DNS packet representation exposed as a
|
|
80
|
-
* type index.
|
|
81
|
-
*
|
|
82
|
-
* Type indexing is an OPTIONAL feature that enables DIDs to become discoverable. DIDs that wish to
|
|
83
|
-
* be discoverable and resolveable by type can include one or more types when publishing their DID
|
|
84
|
-
* document to a DID DHT Gateway.
|
|
85
|
-
*
|
|
86
|
-
* The registered DID types are published in the {@link https://did-dht.com/registry/index.html#indexed-types | DID DHT Registry}.
|
|
87
|
-
*/
|
|
88
|
-
export var DidDhtRegisteredDidType;
|
|
89
|
-
(function (DidDhtRegisteredDidType) {
|
|
90
|
-
/**
|
|
91
|
-
* Type 0 is reserved for DIDs that do not wish to associate themselves with a specific type but
|
|
92
|
-
* wish to make themselves discoverable.
|
|
93
|
-
*/
|
|
94
|
-
DidDhtRegisteredDidType[DidDhtRegisteredDidType["Discoverable"] = 0] = "Discoverable";
|
|
95
|
-
/**
|
|
96
|
-
* Organization
|
|
97
|
-
* @see {@link https://schema.org/Organization | schema definition}
|
|
98
|
-
*/
|
|
99
|
-
DidDhtRegisteredDidType[DidDhtRegisteredDidType["Organization"] = 1] = "Organization";
|
|
100
|
-
/**
|
|
101
|
-
* Government Organization
|
|
102
|
-
* @see {@link https://schema.org/GovernmentOrganization | schema definition}
|
|
103
|
-
*/
|
|
104
|
-
DidDhtRegisteredDidType[DidDhtRegisteredDidType["Government"] = 2] = "Government";
|
|
105
|
-
/**
|
|
106
|
-
* Corporation
|
|
107
|
-
* @see {@link https://schema.org/Corporation | schema definition}
|
|
108
|
-
*/
|
|
109
|
-
DidDhtRegisteredDidType[DidDhtRegisteredDidType["Corporation"] = 3] = "Corporation";
|
|
110
|
-
/**
|
|
111
|
-
* Corporation
|
|
112
|
-
* @see {@link https://schema.org/Corporation | schema definition}
|
|
113
|
-
*/
|
|
114
|
-
DidDhtRegisteredDidType[DidDhtRegisteredDidType["LocalBusiness"] = 4] = "LocalBusiness";
|
|
115
|
-
/**
|
|
116
|
-
* Software Package
|
|
117
|
-
* @see {@link https://schema.org/SoftwareSourceCode | schema definition}
|
|
118
|
-
*/
|
|
119
|
-
DidDhtRegisteredDidType[DidDhtRegisteredDidType["SoftwarePackage"] = 5] = "SoftwarePackage";
|
|
120
|
-
/**
|
|
121
|
-
* Web App
|
|
122
|
-
* @see {@link https://schema.org/WebApplication | schema definition}
|
|
123
|
-
*/
|
|
124
|
-
DidDhtRegisteredDidType[DidDhtRegisteredDidType["WebApp"] = 6] = "WebApp";
|
|
125
|
-
/**
|
|
126
|
-
* Financial Institution
|
|
127
|
-
* @see {@link https://schema.org/FinancialService | schema definition}
|
|
128
|
-
*/
|
|
129
|
-
DidDhtRegisteredDidType[DidDhtRegisteredDidType["FinancialInstitution"] = 7] = "FinancialInstitution";
|
|
130
|
-
})(DidDhtRegisteredDidType || (DidDhtRegisteredDidType = {}));
|
|
131
|
-
/**
|
|
132
|
-
* Enumerates the types of keys that can be used in a DID DHT document.
|
|
133
|
-
*
|
|
134
|
-
* The DID DHT method supports various cryptographic key types. These key types are essential for
|
|
135
|
-
* the creation and management of DIDs and their associated cryptographic operations like signing
|
|
136
|
-
* and encryption. The registered key types are published in the DID DHT Registry and each is
|
|
137
|
-
* assigned a unique numerical value for use by client and gateway implementations.
|
|
138
|
-
*
|
|
139
|
-
* The registered key types are published in the {@link https://did-dht.com/registry/index.html#key-type-index | DID DHT Registry}.
|
|
140
|
-
*/
|
|
141
|
-
export var DidDhtRegisteredKeyType;
|
|
142
|
-
(function (DidDhtRegisteredKeyType) {
|
|
143
|
-
/**
|
|
144
|
-
* Ed25519: A public-key signature system using the EdDSA (Edwards-curve Digital Signature
|
|
145
|
-
* Algorithm) and Curve25519.
|
|
146
|
-
*/
|
|
147
|
-
DidDhtRegisteredKeyType[DidDhtRegisteredKeyType["Ed25519"] = 0] = "Ed25519";
|
|
148
|
-
/**
|
|
149
|
-
* secp256k1: A cryptographic curve used for digital signatures in a range of decentralized
|
|
150
|
-
* systems.
|
|
151
|
-
*/
|
|
152
|
-
DidDhtRegisteredKeyType[DidDhtRegisteredKeyType["secp256k1"] = 1] = "secp256k1";
|
|
153
|
-
/**
|
|
154
|
-
* secp256r1: Also known as P-256 or prime256v1, this curve is used for cryptographic operations
|
|
155
|
-
* and is widely supported in various cryptographic libraries and standards.
|
|
156
|
-
*/
|
|
157
|
-
DidDhtRegisteredKeyType[DidDhtRegisteredKeyType["secp256r1"] = 2] = "secp256r1";
|
|
158
|
-
/**
|
|
159
|
-
* X25519: A public key used for Diffie-Hellman key exchange using Curve25519.
|
|
160
|
-
*/
|
|
161
|
-
DidDhtRegisteredKeyType[DidDhtRegisteredKeyType["X25519"] = 3] = "X25519";
|
|
162
|
-
})(DidDhtRegisteredKeyType || (DidDhtRegisteredKeyType = {}));
|
|
163
|
-
/**
|
|
164
|
-
* Maps {@link https://www.w3.org/TR/did-core/#verification-relationships | DID Core Verification Relationship}
|
|
165
|
-
* values to the corresponding record name in the DNS packet representation of a DHT DID document.
|
|
166
|
-
*/
|
|
167
|
-
export var DidDhtVerificationRelationship;
|
|
168
|
-
(function (DidDhtVerificationRelationship) {
|
|
169
|
-
/**
|
|
170
|
-
* Specifies how the DID subject is expected to be authenticated.
|
|
171
|
-
*/
|
|
172
|
-
DidDhtVerificationRelationship["authentication"] = "auth";
|
|
173
|
-
/**
|
|
174
|
-
* Specifies how the DID subject is expected to express claims, such as for issuing Verifiable
|
|
175
|
-
* Credentials.
|
|
176
|
-
*/
|
|
177
|
-
DidDhtVerificationRelationship["assertionMethod"] = "asm";
|
|
178
|
-
/**
|
|
179
|
-
* Specifies a mechanism used by the DID subject to delegate a cryptographic capability to another
|
|
180
|
-
* party
|
|
181
|
-
*/
|
|
182
|
-
DidDhtVerificationRelationship["capabilityDelegation"] = "del";
|
|
183
|
-
/**
|
|
184
|
-
* Specifies a verification method used by the DID subject to invoke a cryptographic capability.
|
|
185
|
-
*/
|
|
186
|
-
DidDhtVerificationRelationship["capabilityInvocation"] = "inv";
|
|
187
|
-
/**
|
|
188
|
-
* Specifies how an entity can generate encryption material to communicate confidentially with the
|
|
189
|
-
* DID subject.
|
|
190
|
-
*/
|
|
191
|
-
DidDhtVerificationRelationship["keyAgreement"] = "agm";
|
|
192
|
-
})(DidDhtVerificationRelationship || (DidDhtVerificationRelationship = {}));
|
|
193
|
-
/**
|
|
194
|
-
* Private helper that maps algorithm identifiers to their corresponding DID DHT
|
|
195
|
-
* {@link DidDhtRegisteredKeyType | registered key type}.
|
|
196
|
-
*/
|
|
197
|
-
const AlgorithmToKeyTypeMap = {
|
|
198
|
-
Ed25519: DidDhtRegisteredKeyType.Ed25519,
|
|
199
|
-
ES256K: DidDhtRegisteredKeyType.secp256k1,
|
|
200
|
-
ES256: DidDhtRegisteredKeyType.secp256r1,
|
|
201
|
-
'P-256': DidDhtRegisteredKeyType.secp256r1,
|
|
202
|
-
secp256k1: DidDhtRegisteredKeyType.secp256k1,
|
|
203
|
-
secp256r1: DidDhtRegisteredKeyType.secp256r1,
|
|
204
|
-
X25519: DidDhtRegisteredKeyType.X25519,
|
|
205
|
-
};
|
|
206
|
-
/**
|
|
207
|
-
* Private helper that maps did dht registered key types to their corresponding default algorithm identifiers.
|
|
208
|
-
*/
|
|
209
|
-
const KeyTypeToDefaultAlgorithmMap = {
|
|
210
|
-
[DidDhtRegisteredKeyType.Ed25519]: 'EdDSA',
|
|
211
|
-
[DidDhtRegisteredKeyType.secp256k1]: 'ES256K',
|
|
212
|
-
[DidDhtRegisteredKeyType.secp256r1]: 'ES256',
|
|
213
|
-
[DidDhtRegisteredKeyType.X25519]: 'ECDH-ES+A256KW',
|
|
214
|
-
};
|
|
215
31
|
/**
|
|
216
32
|
* The `DidDht` class provides an implementation of the `did:dht` DID method.
|
|
217
33
|
*
|
|
@@ -452,33 +268,10 @@ export class DidDht extends DidMethod {
|
|
|
452
268
|
/**
|
|
453
269
|
* Publishes a DID to the DHT, making it publicly discoverable and resolvable.
|
|
454
270
|
*
|
|
455
|
-
* This method handles the publication of a DID Document associated with a `did:dht` DID to the
|
|
456
|
-
* Mainline DHT network. The publication process involves storing the DID Document in Mainline DHT
|
|
457
|
-
* via a Pkarr relay server.
|
|
458
|
-
*
|
|
459
|
-
* @remarks
|
|
460
|
-
* - This method is typically invoked automatically during the creation of a new DID unless the
|
|
461
|
-
* `publish` option is set to `false`.
|
|
462
|
-
* - For existing, unpublished DIDs, it can be used to publish the DID Document to Mainline DHT.
|
|
463
|
-
* - The method relies on the specified Pkarr relay server to interface with the DHT network.
|
|
464
|
-
*
|
|
465
|
-
* @example
|
|
466
|
-
* ```ts
|
|
467
|
-
* // Generate a new DID and keys but explicitly disable publishing.
|
|
468
|
-
* const did = await DidDht.create({ options: { publish: false } });
|
|
469
|
-
* // Publish the DID to the DHT.
|
|
470
|
-
* const registrationResult = await DidDht.publish({ did });
|
|
471
|
-
* // `registrationResult.didDocumentMetadata.published` is true if the DID was successfully published.
|
|
472
|
-
* ```
|
|
473
|
-
*
|
|
474
271
|
* @param params - The parameters for the `publish` operation.
|
|
475
272
|
* @param params.did - The `BearerDid` object representing the DID to be published.
|
|
476
|
-
* @param params.gatewayUri - Optional. The URI of a
|
|
477
|
-
*
|
|
478
|
-
* to be a DID DHT Gateway or Pkarr Relay. If not specified, a default
|
|
479
|
-
* gateway node is used.
|
|
480
|
-
* @returns A promise that resolves to a {@link DidRegistrationResult} object that contains
|
|
481
|
-
* the result of registering the DID with a DID DHT Gateway or Pkarr relay.
|
|
273
|
+
* @param params.gatewayUri - Optional. The URI of a DID DHT Gateway or Pkarr Relay.
|
|
274
|
+
* @returns A promise that resolves to a {@link DidRegistrationResult} object.
|
|
482
275
|
*/
|
|
483
276
|
static publish(_a) {
|
|
484
277
|
return __awaiter(this, arguments, void 0, function* ({ did, gatewayUri = DEFAULT_GATEWAY_URI }) {
|
|
@@ -489,25 +282,9 @@ export class DidDht extends DidMethod {
|
|
|
489
282
|
/**
|
|
490
283
|
* Resolves a `did:dht` identifier to its corresponding DID document.
|
|
491
284
|
*
|
|
492
|
-
* This method performs the resolution of a `did:dht` DID, retrieving its DID Document from the
|
|
493
|
-
* Mainline DHT network. The process involves querying the DHT network via a Pkarr relay server to
|
|
494
|
-
* retrieve the DID Document that corresponds to the given DID identifier.
|
|
495
|
-
*
|
|
496
|
-
* @remarks
|
|
497
|
-
* - If a `gatewayUri` option is not specified, a default Pkarr relay is used to access the DHT
|
|
498
|
-
* network.
|
|
499
|
-
* - It decodes the DID identifier and retrieves the associated DID Document and metadata.
|
|
500
|
-
* - In case of resolution failure, appropriate error information is returned.
|
|
501
|
-
*
|
|
502
|
-
* @example
|
|
503
|
-
* ```ts
|
|
504
|
-
* const resolutionResult = await DidDht.resolve('did:dht:example');
|
|
505
|
-
* ```
|
|
506
|
-
*
|
|
507
285
|
* @param didUri - The DID to be resolved.
|
|
508
286
|
* @param options - Optional parameters for resolving the DID. Unused by this DID method.
|
|
509
|
-
* @returns A Promise resolving to a {@link DidResolutionResult} object
|
|
510
|
-
* the resolution.
|
|
287
|
+
* @returns A Promise resolving to a {@link DidResolutionResult} object.
|
|
511
288
|
*/
|
|
512
289
|
static resolve(didUri_1) {
|
|
513
290
|
return __awaiter(this, arguments, void 0, function* (didUri, options = {}) {
|
|
@@ -543,7 +320,8 @@ DidDht.methodName = 'dht';
|
|
|
543
320
|
* Mainline DHT in support of DID DHT method create, resolve, update, and deactivate operations.
|
|
544
321
|
*
|
|
545
322
|
* This class includes methods for retrieving and publishing DID documents to and from the DHT,
|
|
546
|
-
* using DNS packet encoding and DID DHT Gateway or Pkarr Relay servers.
|
|
323
|
+
* using DNS packet encoding and DID DHT Gateway or Pkarr Relay servers. Methods delegate to
|
|
324
|
+
* standalone functions extracted into focused modules.
|
|
547
325
|
*/
|
|
548
326
|
export class DidDhtDocument {
|
|
549
327
|
/**
|
|
@@ -552,8 +330,7 @@ export class DidDhtDocument {
|
|
|
552
330
|
* @param params - The parameters for the get operation.
|
|
553
331
|
* @param params.didUri - The DID URI containing the Identity Key.
|
|
554
332
|
* @param params.gatewayUri - The DID DHT Gateway or Pkarr Relay URI.
|
|
555
|
-
* @returns A Promise resolving to a {@link DidResolutionResult} object
|
|
556
|
-
* document and its metadata.
|
|
333
|
+
* @returns A Promise resolving to a {@link DidResolutionResult} object.
|
|
557
334
|
*/
|
|
558
335
|
static get(_a) {
|
|
559
336
|
return __awaiter(this, arguments, void 0, function* ({ didUri, gatewayUri }) {
|
|
@@ -576,8 +353,7 @@ export class DidDhtDocument {
|
|
|
576
353
|
* @param params - The parameters to use when publishing the DID document to the DHT network.
|
|
577
354
|
* @param params.did - The DID object whose DID document will be published.
|
|
578
355
|
* @param params.gatewayUri - The DID DHT Gateway or Pkarr Relay URI.
|
|
579
|
-
* @returns A promise that resolves to a {@link DidRegistrationResult} object
|
|
580
|
-
* the result of registering the DID with a DID DHT Gateway or Pkarr relay.
|
|
356
|
+
* @returns A promise that resolves to a {@link DidRegistrationResult} object.
|
|
581
357
|
*/
|
|
582
358
|
static put(_a) {
|
|
583
359
|
return __awaiter(this, arguments, void 0, function* ({ did, gatewayUri }) {
|
|
@@ -604,402 +380,28 @@ export class DidDhtDocument {
|
|
|
604
380
|
};
|
|
605
381
|
});
|
|
606
382
|
}
|
|
607
|
-
/**
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
*
|
|
612
|
-
* @param params
|
|
613
|
-
* @param params.gatewayUri - The DID DHT Gateway or Pkarr Relay URI.
|
|
614
|
-
* @param params.publicKeyBytes - The public key bytes of the Identity Key, z-base-32 encoded.
|
|
615
|
-
* @returns A promise resolving to a BEP44 message containing the signed DNS packet.
|
|
616
|
-
*/
|
|
617
|
-
static pkarrGet(_a) {
|
|
618
|
-
return __awaiter(this, arguments, void 0, function* ({ gatewayUri, publicKeyBytes }) {
|
|
619
|
-
// The identifier (key in the DHT) is the z-base-32 encoding of the Identity Key.
|
|
620
|
-
const identifier = Convert.uint8Array(publicKeyBytes).toBase32Z();
|
|
621
|
-
// Concatenate the gateway URI with the identifier to form the full URL.
|
|
622
|
-
const url = new URL(identifier, gatewayUri).href;
|
|
623
|
-
// Transmit the Get request to the DID DHT Gateway or Pkarr Relay and get the response.
|
|
624
|
-
let response;
|
|
625
|
-
try {
|
|
626
|
-
response = yield fetch(url, { method: 'GET' });
|
|
627
|
-
if (!response.ok) {
|
|
628
|
-
throw new DidError(DidErrorCode.NotFound, `Pkarr record not found for: ${identifier}`);
|
|
629
|
-
}
|
|
630
|
-
}
|
|
631
|
-
catch (error) {
|
|
632
|
-
if (error instanceof DidError) {
|
|
633
|
-
throw error;
|
|
634
|
-
}
|
|
635
|
-
throw new DidError(DidErrorCode.InternalError, `Failed to fetch Pkarr record: ${error.message}`);
|
|
636
|
-
}
|
|
637
|
-
// Read the Fetch Response stream into a byte array.
|
|
638
|
-
const messageBytes = yield response.arrayBuffer();
|
|
639
|
-
if (!messageBytes) {
|
|
640
|
-
throw new DidError(DidErrorCode.NotFound, `Pkarr record not found for: ${identifier}`);
|
|
641
|
-
}
|
|
642
|
-
if (messageBytes.byteLength < 72) {
|
|
643
|
-
throw new DidError(DidErrorCode.InvalidDidDocumentLength, `Pkarr response must be at least 72 bytes but got: ${messageBytes.byteLength}`);
|
|
644
|
-
}
|
|
645
|
-
if (messageBytes.byteLength > 1072) {
|
|
646
|
-
throw new DidError(DidErrorCode.InvalidDidDocumentLength, `Pkarr response exceeds 1000 byte limit: ${messageBytes.byteLength}`);
|
|
647
|
-
}
|
|
648
|
-
// Decode the BEP44 message from the byte array.
|
|
649
|
-
const bep44Message = {
|
|
650
|
-
k: publicKeyBytes,
|
|
651
|
-
seq: Number(new DataView(messageBytes).getBigUint64(64)),
|
|
652
|
-
sig: new Uint8Array(messageBytes, 0, 64),
|
|
653
|
-
v: new Uint8Array(messageBytes, 72)
|
|
654
|
-
};
|
|
655
|
-
return bep44Message;
|
|
383
|
+
/** Delegates to {@link pkarrGet} from `did-dht-pkarr.js`. */
|
|
384
|
+
static pkarrGet(params) {
|
|
385
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
386
|
+
return pkarrGet(params);
|
|
656
387
|
});
|
|
657
388
|
}
|
|
658
|
-
/**
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
*
|
|
663
|
-
* @param params - The parameters to use when publishing a signed BEP44 message to a Pkarr relay server.
|
|
664
|
-
* @param params.gatewayUri - The DID DHT Gateway or Pkarr Relay URI.
|
|
665
|
-
* @param params.bep44Message - The BEP44 message to be published, containing the signed DNS packet.
|
|
666
|
-
* @returns A promise resolving to `true` if the message was successfully published, otherwise `false`.
|
|
667
|
-
*/
|
|
668
|
-
static pkarrPut(_a) {
|
|
669
|
-
return __awaiter(this, arguments, void 0, function* ({ gatewayUri, bep44Message }) {
|
|
670
|
-
// The identifier (key in the DHT) is the z-base-32 encoding of the Identity Key.
|
|
671
|
-
const identifier = Convert.uint8Array(bep44Message.k).toBase32Z();
|
|
672
|
-
// Concatenate the gateway URI with the identifier to form the full URL.
|
|
673
|
-
const url = new URL(identifier, gatewayUri).href;
|
|
674
|
-
// Construct the body of the request according to the Pkarr relay specification.
|
|
675
|
-
const body = new Uint8Array(bep44Message.v.length + 72);
|
|
676
|
-
body.set(bep44Message.sig, 0);
|
|
677
|
-
new DataView(body.buffer).setBigUint64(bep44Message.sig.length, BigInt(bep44Message.seq));
|
|
678
|
-
body.set(bep44Message.v, bep44Message.sig.length + 8);
|
|
679
|
-
// Transmit the Put request to the Pkarr relay and get the response.
|
|
680
|
-
let response;
|
|
681
|
-
try {
|
|
682
|
-
response = yield fetch(url, {
|
|
683
|
-
method: 'PUT',
|
|
684
|
-
headers: { 'Content-Type': 'application/octet-stream' },
|
|
685
|
-
body
|
|
686
|
-
});
|
|
687
|
-
}
|
|
688
|
-
catch (error) {
|
|
689
|
-
throw new DidError(DidErrorCode.InternalError, `Failed to put Pkarr record for identifier ${identifier}: ${error.message}`);
|
|
690
|
-
}
|
|
691
|
-
// Return `true` if the DHT request was successful, otherwise return `false`.
|
|
692
|
-
return response.ok;
|
|
389
|
+
/** Delegates to {@link pkarrPut} from `did-dht-pkarr.js`. */
|
|
390
|
+
static pkarrPut(params) {
|
|
391
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
392
|
+
return pkarrPut(params);
|
|
693
393
|
});
|
|
694
394
|
}
|
|
695
|
-
/**
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
*
|
|
700
|
-
* @param params - The parameters to use when converting a DNS packet to a DID document.
|
|
701
|
-
* @param params.didUri - The DID URI of the DID document.
|
|
702
|
-
* @param params.dnsPacket - The DNS packet to convert to a DID document.
|
|
703
|
-
* @returns A Promise resolving to a {@link DidResolutionResult} object containing the DID
|
|
704
|
-
* document and its metadata.
|
|
705
|
-
*/
|
|
706
|
-
static fromDnsPacket(_a) {
|
|
707
|
-
return __awaiter(this, arguments, void 0, function* ({ didUri, dnsPacket }) {
|
|
708
|
-
var _b, _c, _d;
|
|
709
|
-
// Begin constructing the DID Document.
|
|
710
|
-
const didDocument = { id: didUri };
|
|
711
|
-
// Since the DID document is being retrieved from the DHT, it is considered published.
|
|
712
|
-
const didDocumentMetadata = {
|
|
713
|
-
published: true
|
|
714
|
-
};
|
|
715
|
-
const idLookup = new Map();
|
|
716
|
-
for (const answer of (_b = dnsPacket === null || dnsPacket === void 0 ? void 0 : dnsPacket.answers) !== null && _b !== void 0 ? _b : []) {
|
|
717
|
-
// DID DHT properties are ONLY present in DNS TXT records.
|
|
718
|
-
if (answer.type !== 'TXT') {
|
|
719
|
-
continue;
|
|
720
|
-
}
|
|
721
|
-
// Get the DID DHT record identifier (e.g., k0, aka, did, etc.) from the DNS resource name.
|
|
722
|
-
const dnsRecordId = answer.name.split('.')[0].substring(1);
|
|
723
|
-
switch (true) {
|
|
724
|
-
// Process an also known as record.
|
|
725
|
-
case dnsRecordId.startsWith('aka'): {
|
|
726
|
-
// Decode the DNS TXT record data value to a string.
|
|
727
|
-
const data = DidDhtUtils.parseTxtDataToString(answer.data);
|
|
728
|
-
// Add the 'alsoKnownAs' property to the DID document.
|
|
729
|
-
didDocument.alsoKnownAs = data.split(VALUE_SEPARATOR);
|
|
730
|
-
break;
|
|
731
|
-
}
|
|
732
|
-
// Process a controller record.
|
|
733
|
-
case dnsRecordId.startsWith('cnt'): {
|
|
734
|
-
// Decode the DNS TXT record data value to a string.
|
|
735
|
-
const data = DidDhtUtils.parseTxtDataToString(answer.data);
|
|
736
|
-
// Add the 'controller' property to the DID document.
|
|
737
|
-
didDocument.controller = data.includes(VALUE_SEPARATOR) ? data.split(VALUE_SEPARATOR) : data;
|
|
738
|
-
break;
|
|
739
|
-
}
|
|
740
|
-
// Process verification methods.
|
|
741
|
-
case dnsRecordId.startsWith('k'): {
|
|
742
|
-
// Get the key type (t), Base64URL-encoded public key (k), algorithm (a), and
|
|
743
|
-
// optionally, controller (c) or Verification Method ID (id) from the decoded TXT record data.
|
|
744
|
-
const { id, t, k, c, a: parsedAlg } = DidDhtUtils.parseTxtDataToObject(answer.data);
|
|
745
|
-
// Convert the public key from Base64URL format to a byte array.
|
|
746
|
-
const publicKeyBytes = Convert.base64Url(k).toUint8Array();
|
|
747
|
-
// Use the key type integer to look up the cryptographic curve name.
|
|
748
|
-
const namedCurve = DidDhtRegisteredKeyType[Number(t)];
|
|
749
|
-
// Convert the public key from a byte array to JWK format.
|
|
750
|
-
const publicKey = yield DidDhtUtils.keyConverter(namedCurve).bytesToPublicKey({ publicKeyBytes });
|
|
751
|
-
publicKey.alg = parsedAlg || KeyTypeToDefaultAlgorithmMap[Number(t)];
|
|
752
|
-
// TODO: when this is complete https://github.com/enboxorg/enbox/issues/638 then we can add this back and
|
|
753
|
-
// update the test vectors kid back to '0'
|
|
754
|
-
// if(dnsRecordId === 'k0') {
|
|
755
|
-
// publicKey.kid = '0';
|
|
756
|
-
// }
|
|
757
|
-
// Determine the Verification Method ID: '0' for the identity key,
|
|
758
|
-
// the id from the TXT Data Object, or the JWK thumbprint if an explicity Verification Method ID not defined.
|
|
759
|
-
const vmId = dnsRecordId === 'k0' ? '0' : id !== undefined ? id : yield computeJwkThumbprint({ jwk: publicKey });
|
|
760
|
-
// Initialize the `verificationMethod` array if it does not already exist.
|
|
761
|
-
(_c = didDocument.verificationMethod) !== null && _c !== void 0 ? _c : (didDocument.verificationMethod = []);
|
|
762
|
-
// Prepend the DID URI to the ID fragment to form the full verification method ID.
|
|
763
|
-
const methodId = `${didUri}#${vmId}`;
|
|
764
|
-
// Add the verification method to the DID document.
|
|
765
|
-
didDocument.verificationMethod.push({
|
|
766
|
-
id: methodId,
|
|
767
|
-
type: 'JsonWebKey',
|
|
768
|
-
controller: c !== null && c !== void 0 ? c : didUri,
|
|
769
|
-
publicKeyJwk: publicKey,
|
|
770
|
-
});
|
|
771
|
-
// Add a mapping from the DNS record ID (e.g., 'k0', 'k1', etc.) to the verification
|
|
772
|
-
// method ID (e.g., 'did:dht:...#0', etc.).
|
|
773
|
-
idLookup.set(dnsRecordId, methodId);
|
|
774
|
-
break;
|
|
775
|
-
}
|
|
776
|
-
// Process services.
|
|
777
|
-
case dnsRecordId.startsWith('s'): {
|
|
778
|
-
// Get the service ID fragment (id), type (t), service endpoint (se), and optionally,
|
|
779
|
-
// other properties from the decoded TXT record data.
|
|
780
|
-
const _e = DidDhtUtils.parseTxtDataToObject(answer.data), { id, t, se } = _e, customProperties = __rest(_e, ["id", "t", "se"]);
|
|
781
|
-
// if multi-values: 'a,b,c' -> ['a', 'b', 'c'], if single-value: 'a' -> ['a']
|
|
782
|
-
// NOTE: The service endpoint technically can either be a string or an array of strings,
|
|
783
|
-
// we enforce an array for single-value to simplify verification of vector 3 in the spec: https://did-dht.com/#vector-3
|
|
784
|
-
const serviceEndpoint = se.includes(VALUE_SEPARATOR) ? se.split(VALUE_SEPARATOR) : [se];
|
|
785
|
-
// Convert custom property values to either a string or an array of strings.
|
|
786
|
-
const serviceProperties = Object.fromEntries(Object.entries(customProperties).map(([k, v]) => [k, v.includes(VALUE_SEPARATOR) ? v.split(VALUE_SEPARATOR) : v]));
|
|
787
|
-
// Initialize the `service` array if it does not already exist.
|
|
788
|
-
(_d = didDocument.service) !== null && _d !== void 0 ? _d : (didDocument.service = []);
|
|
789
|
-
didDocument.service.push(Object.assign(Object.assign({}, serviceProperties), { id: `${didUri}#${id}`, type: t, serviceEndpoint }));
|
|
790
|
-
break;
|
|
791
|
-
}
|
|
792
|
-
// Process DID DHT types.
|
|
793
|
-
case dnsRecordId.startsWith('typ'): {
|
|
794
|
-
// Decode the DNS TXT record data value to an object.
|
|
795
|
-
const { id: types } = DidDhtUtils.parseTxtDataToObject(answer.data);
|
|
796
|
-
// Add the DID DHT Registered DID Types represented as numbers to DID metadata.
|
|
797
|
-
didDocumentMetadata.types = types.split(VALUE_SEPARATOR).map(typeInteger => Number(typeInteger));
|
|
798
|
-
break;
|
|
799
|
-
}
|
|
800
|
-
// Process root record.
|
|
801
|
-
case dnsRecordId.startsWith('did'): {
|
|
802
|
-
// Helper function that maps verification relationship values to verification method IDs.
|
|
803
|
-
const recordIdsToMethodIds = (data) => data
|
|
804
|
-
.split(VALUE_SEPARATOR)
|
|
805
|
-
.map(dnsRecordId => idLookup.get(dnsRecordId))
|
|
806
|
-
.filter((id) => typeof id === 'string');
|
|
807
|
-
// Decode the DNS TXT record data and destructure verification relationship properties.
|
|
808
|
-
const { auth, asm, del, inv, agm } = DidDhtUtils.parseTxtDataToObject(answer.data);
|
|
809
|
-
// Add the verification relationships, if any, to the DID document.
|
|
810
|
-
if (auth) {
|
|
811
|
-
didDocument.authentication = recordIdsToMethodIds(auth);
|
|
812
|
-
}
|
|
813
|
-
if (asm) {
|
|
814
|
-
didDocument.assertionMethod = recordIdsToMethodIds(asm);
|
|
815
|
-
}
|
|
816
|
-
if (del) {
|
|
817
|
-
didDocument.capabilityDelegation = recordIdsToMethodIds(del);
|
|
818
|
-
}
|
|
819
|
-
if (inv) {
|
|
820
|
-
didDocument.capabilityInvocation = recordIdsToMethodIds(inv);
|
|
821
|
-
}
|
|
822
|
-
if (agm) {
|
|
823
|
-
didDocument.keyAgreement = recordIdsToMethodIds(agm);
|
|
824
|
-
}
|
|
825
|
-
break;
|
|
826
|
-
}
|
|
827
|
-
}
|
|
828
|
-
}
|
|
829
|
-
return { didDocument, didDocumentMetadata, didResolutionMetadata: {} };
|
|
395
|
+
/** Delegates to {@link fromDnsPacket} from `did-dht-dns.js`. */
|
|
396
|
+
static fromDnsPacket(params) {
|
|
397
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
398
|
+
return fromDnsPacket(params);
|
|
830
399
|
});
|
|
831
400
|
}
|
|
832
|
-
/**
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
*
|
|
837
|
-
* @param params - The parameters to use when converting a DID document to a DNS packet.
|
|
838
|
-
* @param params.didDocument - The DID document to convert to a DNS packet.
|
|
839
|
-
* @param params.didMetadata - The DID metadata to include in the DNS packet.
|
|
840
|
-
* @param params.authoritativeGatewayUris - The URIs of the Authoritative Gateways to generate NS records from.
|
|
841
|
-
* @param params.previousDidProof - The signature proof that this DID is linked to the given previous DID.
|
|
842
|
-
* @returns A promise that resolves to a DNS packet.
|
|
843
|
-
*/
|
|
844
|
-
static toDnsPacket(_a) {
|
|
845
|
-
return __awaiter(this, arguments, void 0, function* ({ didDocument, didMetadata, authoritativeGatewayUris, previousDidProof }) {
|
|
846
|
-
var _b, _c, _d, _e, _f;
|
|
847
|
-
const txtRecords = [];
|
|
848
|
-
const nsRecords = [];
|
|
849
|
-
const idLookup = new Map();
|
|
850
|
-
const serviceIds = [];
|
|
851
|
-
const verificationMethodIds = [];
|
|
852
|
-
// Add `_prv._did.` TXT record if previous DID proof is provided and valid.
|
|
853
|
-
if (previousDidProof !== undefined) {
|
|
854
|
-
const { signature, previousDid } = previousDidProof;
|
|
855
|
-
yield DidDhtUtils.validatePreviousDidProof({
|
|
856
|
-
newDid: didDocument.id,
|
|
857
|
-
previousDidProof
|
|
858
|
-
});
|
|
859
|
-
txtRecords.push({
|
|
860
|
-
type: 'TXT',
|
|
861
|
-
name: '_prv._did.',
|
|
862
|
-
ttl: DNS_RECORD_TTL,
|
|
863
|
-
data: `id=${previousDid};s=${signature}`
|
|
864
|
-
});
|
|
865
|
-
}
|
|
866
|
-
// Add DNS TXT records if the DID document contains an `alsoKnownAs` property.
|
|
867
|
-
if (didDocument.alsoKnownAs) {
|
|
868
|
-
txtRecords.push({
|
|
869
|
-
type: 'TXT',
|
|
870
|
-
name: '_aka._did.',
|
|
871
|
-
ttl: DNS_RECORD_TTL,
|
|
872
|
-
data: didDocument.alsoKnownAs.join(VALUE_SEPARATOR)
|
|
873
|
-
});
|
|
874
|
-
}
|
|
875
|
-
// Add DNS TXT records if the DID document contains a `controller` property.
|
|
876
|
-
if (didDocument.controller) {
|
|
877
|
-
const controller = Array.isArray(didDocument.controller)
|
|
878
|
-
? didDocument.controller.join(VALUE_SEPARATOR)
|
|
879
|
-
: didDocument.controller;
|
|
880
|
-
txtRecords.push({
|
|
881
|
-
type: 'TXT',
|
|
882
|
-
name: '_cnt._did.',
|
|
883
|
-
ttl: DNS_RECORD_TTL,
|
|
884
|
-
data: controller
|
|
885
|
-
});
|
|
886
|
-
}
|
|
887
|
-
// Add DNS TXT records for each verification method.
|
|
888
|
-
for (const [index, verificationMethod] of (_c = (_b = didDocument.verificationMethod) === null || _b === void 0 ? void 0 : _b.entries()) !== null && _c !== void 0 ? _c : []) {
|
|
889
|
-
const dnsRecordId = `k${index}`;
|
|
890
|
-
verificationMethodIds.push(dnsRecordId);
|
|
891
|
-
const methodId = verificationMethod.id.split('#').pop(); // Remove fragment prefix, if any.
|
|
892
|
-
idLookup.set(methodId, dnsRecordId);
|
|
893
|
-
const publicKey = verificationMethod.publicKeyJwk;
|
|
894
|
-
if (!((publicKey === null || publicKey === void 0 ? void 0 : publicKey.crv) && publicKey.crv in AlgorithmToKeyTypeMap)) {
|
|
895
|
-
throw new DidError(DidErrorCode.InvalidPublicKeyType, `Verification method '${verificationMethod.id}' contains an unsupported key type: ${(_d = publicKey === null || publicKey === void 0 ? void 0 : publicKey.crv) !== null && _d !== void 0 ? _d : 'undefined'}`);
|
|
896
|
-
}
|
|
897
|
-
// Use the public key's `crv` property to get the DID DHT key type.
|
|
898
|
-
const keyType = DidDhtRegisteredKeyType[publicKey.crv];
|
|
899
|
-
// Convert the public key from JWK format to a byte array.
|
|
900
|
-
const publicKeyBytes = yield DidDhtUtils.keyConverter(publicKey.crv).publicKeyToBytes({ publicKey });
|
|
901
|
-
// Convert the public key from a byte array to Base64URL format.
|
|
902
|
-
const publicKeyBase64Url = Convert.uint8Array(publicKeyBytes).toBase64Url();
|
|
903
|
-
// Define the data for the DNS TXT record.
|
|
904
|
-
const txtData = [`t=${keyType}`, `k=${publicKeyBase64Url}`];
|
|
905
|
-
// if the methodId is not the identity key or a thumbprint, explicity define the id within the DNS TXT record.
|
|
906
|
-
// otherwise the id can be inferred from the thumbprint.
|
|
907
|
-
if (methodId !== '0' && (yield computeJwkThumbprint({ jwk: publicKey })) !== methodId) {
|
|
908
|
-
txtData.unshift(`id=${methodId}`);
|
|
909
|
-
}
|
|
910
|
-
// Only set the algorithm property (`a`) if it differs from the default algorithm for the key type.
|
|
911
|
-
if (publicKey.alg !== KeyTypeToDefaultAlgorithmMap[keyType]) {
|
|
912
|
-
txtData.push(`a=${publicKey.alg}`);
|
|
913
|
-
}
|
|
914
|
-
// Add the controller property, if set to a value other than the Identity Key (DID Subject).
|
|
915
|
-
if (verificationMethod.controller !== didDocument.id) {
|
|
916
|
-
txtData.push(`c=${verificationMethod.controller}`);
|
|
917
|
-
}
|
|
918
|
-
// Add a TXT record for the verification method.
|
|
919
|
-
txtRecords.push({
|
|
920
|
-
type: 'TXT',
|
|
921
|
-
name: `_${dnsRecordId}._did.`,
|
|
922
|
-
ttl: DNS_RECORD_TTL,
|
|
923
|
-
data: txtData.join(PROPERTY_SEPARATOR)
|
|
924
|
-
});
|
|
925
|
-
}
|
|
926
|
-
// Add DNS TXT records for each service.
|
|
927
|
-
(_e = didDocument.service) === null || _e === void 0 ? void 0 : _e.forEach((service, index) => {
|
|
928
|
-
const dnsRecordId = `s${index}`;
|
|
929
|
-
serviceIds.push(dnsRecordId);
|
|
930
|
-
let { id, type: t, serviceEndpoint: se } = service, customProperties = __rest(service, ["id", "type", "serviceEndpoint"]);
|
|
931
|
-
id = extractDidFragment(id);
|
|
932
|
-
se = Array.isArray(se) ? se.join(',') : se;
|
|
933
|
-
// Define the data for the DNS TXT record.
|
|
934
|
-
const txtData = Object.entries(Object.assign({ id, t, se }, customProperties)).map(([key, value]) => `${key}=${value}`);
|
|
935
|
-
const txtDataString = txtData.join(PROPERTY_SEPARATOR);
|
|
936
|
-
const data = DidDhtUtils.chunkDataIfNeeded(txtDataString);
|
|
937
|
-
// Add a TXT record for the verification method.
|
|
938
|
-
txtRecords.push({
|
|
939
|
-
type: 'TXT',
|
|
940
|
-
name: `_${dnsRecordId}._did.`,
|
|
941
|
-
ttl: DNS_RECORD_TTL,
|
|
942
|
-
data
|
|
943
|
-
});
|
|
944
|
-
});
|
|
945
|
-
// Initialize the root DNS TXT record with the DID DHT specification version.
|
|
946
|
-
const rootRecord = [`v=${DID_DHT_SPECIFICATION_VERSION}`];
|
|
947
|
-
// Add verification methods to the root record.
|
|
948
|
-
if (verificationMethodIds.length) {
|
|
949
|
-
rootRecord.push(`vm=${verificationMethodIds.join(VALUE_SEPARATOR)}`);
|
|
950
|
-
}
|
|
951
|
-
// Add verification relationships to the root record.
|
|
952
|
-
Object.keys(DidVerificationRelationship).forEach(relationship => {
|
|
953
|
-
var _a;
|
|
954
|
-
// Collect the verification method IDs for the given relationship.
|
|
955
|
-
const dnsRecordIds = (_a = didDocument[relationship]) === null || _a === void 0 ? void 0 : _a.map((id) => idLookup.get(id.split('#').pop()));
|
|
956
|
-
// If the relationship includes verification methods, add them to the root record.
|
|
957
|
-
if (dnsRecordIds) {
|
|
958
|
-
const recordName = DidDhtVerificationRelationship[relationship];
|
|
959
|
-
rootRecord.push(`${recordName}=${dnsRecordIds.join(VALUE_SEPARATOR)}`);
|
|
960
|
-
}
|
|
961
|
-
});
|
|
962
|
-
// Add services to the root record.
|
|
963
|
-
if (serviceIds.length) {
|
|
964
|
-
rootRecord.push(`svc=${serviceIds.join(VALUE_SEPARATOR)}`);
|
|
965
|
-
}
|
|
966
|
-
// If defined, add a DNS TXT record for each registered DID type.
|
|
967
|
-
if ((_f = didMetadata.types) === null || _f === void 0 ? void 0 : _f.length) {
|
|
968
|
-
// DID types can be specified as either a string or a number, so we need to normalize the
|
|
969
|
-
// values to integers.
|
|
970
|
-
const types = didMetadata.types;
|
|
971
|
-
const typeIntegers = types.map(type => typeof type === 'string' ? DidDhtRegisteredDidType[type] : type);
|
|
972
|
-
txtRecords.push({
|
|
973
|
-
type: 'TXT',
|
|
974
|
-
name: '_typ._did.',
|
|
975
|
-
ttl: DNS_RECORD_TTL,
|
|
976
|
-
data: `id=${typeIntegers.join(VALUE_SEPARATOR)}`
|
|
977
|
-
});
|
|
978
|
-
}
|
|
979
|
-
// Add a DNS TXT record for the root record.
|
|
980
|
-
txtRecords.push({
|
|
981
|
-
type: 'TXT',
|
|
982
|
-
name: '_did.' + DidDhtDocument.getUniqueDidSuffix(didDocument.id) + '.', // name of a Root Record MUST end in `<ID>.`
|
|
983
|
-
ttl: DNS_RECORD_TTL,
|
|
984
|
-
data: rootRecord.join(PROPERTY_SEPARATOR)
|
|
985
|
-
});
|
|
986
|
-
// Add an NS record for each authoritative gateway URI.
|
|
987
|
-
for (const gatewayUri of authoritativeGatewayUris || []) {
|
|
988
|
-
nsRecords.push({
|
|
989
|
-
type: 'NS',
|
|
990
|
-
name: '_did.' + DidDhtDocument.getUniqueDidSuffix(didDocument.id) + '.', // name of an NS record a authoritative gateway MUST end in `<ID>.`
|
|
991
|
-
ttl: DNS_RECORD_TTL,
|
|
992
|
-
data: gatewayUri + '.'
|
|
993
|
-
});
|
|
994
|
-
}
|
|
995
|
-
// Create a DNS response packet with the authoritative answer flag set.
|
|
996
|
-
const dnsPacket = {
|
|
997
|
-
id: 0,
|
|
998
|
-
type: 'response',
|
|
999
|
-
flags: AUTHORITATIVE_ANSWER,
|
|
1000
|
-
answers: [...txtRecords, ...nsRecords]
|
|
1001
|
-
};
|
|
1002
|
-
return dnsPacket;
|
|
401
|
+
/** Delegates to {@link toDnsPacket} from `did-dht-dns.js`. */
|
|
402
|
+
static toDnsPacket(params) {
|
|
403
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
404
|
+
return toDnsPacket(params);
|
|
1003
405
|
});
|
|
1004
406
|
}
|
|
1005
407
|
/**
|
|
@@ -1014,237 +416,58 @@ export class DidDhtDocument {
|
|
|
1014
416
|
}
|
|
1015
417
|
/**
|
|
1016
418
|
* The `DidDhtUtils` class provides utility functions to support operations in the DID DHT method.
|
|
1017
|
-
*
|
|
1018
|
-
* converting between different formats and representations.
|
|
419
|
+
* Methods delegate to standalone functions extracted into focused modules for backward compatibility.
|
|
1019
420
|
*/
|
|
1020
421
|
export class DidDhtUtils {
|
|
1021
|
-
/**
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
* @param params.dnsPacket - The DNS packet to encode in the BEP44 message.
|
|
1026
|
-
* @param params.publicKeyBytes - The public key bytes of the Identity Key.
|
|
1027
|
-
* @param params.signer - Signer that can sign and verify data using the Identity Key.
|
|
1028
|
-
* @returns A promise that resolves to a BEP44 put message.
|
|
1029
|
-
*/
|
|
1030
|
-
static createBep44PutMessage(_a) {
|
|
1031
|
-
return __awaiter(this, arguments, void 0, function* ({ dnsPacket, publicKeyBytes, signer }) {
|
|
1032
|
-
// BEP44 requires that the sequence number be a monotoically increasing integer, so we use the
|
|
1033
|
-
// current time in seconds since Unix epoch as a simple solution. Higher precision is not
|
|
1034
|
-
// recommended since DID DHT documents are not expected to change frequently and there are
|
|
1035
|
-
// small differences in system clocks that can cause issues if multiple clients are publishing
|
|
1036
|
-
// updates to the same DID document.
|
|
1037
|
-
const sequenceNumber = Math.ceil(Date.now() / 1000);
|
|
1038
|
-
// Encode the DNS packet into a byte array containing a UDP payload.
|
|
1039
|
-
const encodedDnsPacket = dnsPacketEncode(dnsPacket);
|
|
1040
|
-
// Encode the sequence and DNS byte array to bencode format.
|
|
1041
|
-
const bencodedData = bencode.encode({ seq: sequenceNumber, v: encodedDnsPacket }).subarray(1, -1);
|
|
1042
|
-
if (bencodedData.length > 1000) {
|
|
1043
|
-
throw new DidError(DidErrorCode.InvalidDidDocumentLength, `DNS packet exceeds the 1000 byte maximum size: ${bencodedData.length} bytes`);
|
|
1044
|
-
}
|
|
1045
|
-
// Sign the BEP44 message.
|
|
1046
|
-
const signature = yield signer.sign({ data: bencodedData });
|
|
1047
|
-
return { k: publicKeyBytes, seq: sequenceNumber, sig: signature, v: encodedDnsPacket };
|
|
422
|
+
/** Delegates to {@link createBep44PutMessage} from `did-dht-pkarr.js`. */
|
|
423
|
+
static createBep44PutMessage(params) {
|
|
424
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
425
|
+
return createBep44PutMessage(params);
|
|
1048
426
|
});
|
|
1049
427
|
}
|
|
1050
|
-
/**
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
* @param params.didUri - The DID URI containing the Identity Key.
|
|
1055
|
-
* @returns A promise that resolves to a JWK representing the Identity Key.
|
|
1056
|
-
*/
|
|
1057
|
-
static identifierToIdentityKey(_a) {
|
|
1058
|
-
return __awaiter(this, arguments, void 0, function* ({ didUri }) {
|
|
1059
|
-
// Decode the method-specific identifier from z-base-32 to a byte array.
|
|
1060
|
-
const identityKeyBytes = DidDhtUtils.identifierToIdentityKeyBytes({ didUri });
|
|
1061
|
-
// Convert the byte array to a JWK.
|
|
1062
|
-
const identityKey = yield Ed25519.bytesToPublicKey({ publicKeyBytes: identityKeyBytes });
|
|
1063
|
-
return identityKey;
|
|
428
|
+
/** Delegates to {@link identifierToIdentityKey} from `did-dht-utils.js`. */
|
|
429
|
+
static identifierToIdentityKey(params) {
|
|
430
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
431
|
+
return identifierToIdentityKey(params);
|
|
1064
432
|
});
|
|
1065
433
|
}
|
|
1066
|
-
/**
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
* @param params - The parameters to use for the conversion.
|
|
1070
|
-
* @param params.didUri - The DID URI containing the Identity Key.
|
|
1071
|
-
* @returns A byte array representation of the Identity Key.
|
|
1072
|
-
*/
|
|
1073
|
-
static identifierToIdentityKeyBytes({ didUri }) {
|
|
1074
|
-
// Parse the DID URI.
|
|
1075
|
-
const parsedDid = Did.parse(didUri);
|
|
1076
|
-
// Verify that the DID URI is valid.
|
|
1077
|
-
if (!parsedDid) {
|
|
1078
|
-
throw new DidError(DidErrorCode.InvalidDid, `Invalid DID URI: ${didUri}`);
|
|
1079
|
-
}
|
|
1080
|
-
// Verify the DID method is supported.
|
|
1081
|
-
if (parsedDid.method !== DidDht.methodName) {
|
|
1082
|
-
throw new DidError(DidErrorCode.MethodNotSupported, `Method not supported: ${parsedDid.method}`);
|
|
1083
|
-
}
|
|
1084
|
-
// Decode the method-specific identifier from z-base-32 to a byte array.
|
|
1085
|
-
let identityKeyBytes;
|
|
1086
|
-
try {
|
|
1087
|
-
identityKeyBytes = Convert.base32Z(parsedDid.id).toUint8Array();
|
|
1088
|
-
}
|
|
1089
|
-
catch (_a) {
|
|
1090
|
-
throw new DidError(DidErrorCode.InvalidPublicKey, `Failed to decode method-specific identifier`);
|
|
1091
|
-
}
|
|
1092
|
-
if (identityKeyBytes.length !== 32) {
|
|
1093
|
-
throw new DidError(DidErrorCode.InvalidPublicKeyLength, `Invalid public key length: ${identityKeyBytes.length}`);
|
|
1094
|
-
}
|
|
1095
|
-
return identityKeyBytes;
|
|
434
|
+
/** Delegates to {@link identifierToIdentityKeyBytes} from `did-dht-utils.js`. */
|
|
435
|
+
static identifierToIdentityKeyBytes(params) {
|
|
436
|
+
return identifierToIdentityKeyBytes(params);
|
|
1096
437
|
}
|
|
1097
|
-
/**
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
* `did:dht:` to form the DID identifier.
|
|
1102
|
-
*
|
|
1103
|
-
* @param params - The parameters to use for the conversion.
|
|
1104
|
-
* @param params.identityKey The Identity Key from which the DID identifier is computed.
|
|
1105
|
-
* @returns A promise that resolves to a string containing the DID identifier.
|
|
1106
|
-
*/
|
|
1107
|
-
static identityKeyToIdentifier(_a) {
|
|
1108
|
-
return __awaiter(this, arguments, void 0, function* ({ identityKey }) {
|
|
1109
|
-
// Convert the key from JWK format to a byte array.
|
|
1110
|
-
const publicKeyBytes = yield Ed25519.publicKeyToBytes({ publicKey: identityKey });
|
|
1111
|
-
// Encode the byte array as a z-base-32 string.
|
|
1112
|
-
const identifier = Convert.uint8Array(publicKeyBytes).toBase32Z();
|
|
1113
|
-
return `did:${DidDht.methodName}:${identifier}`;
|
|
438
|
+
/** Delegates to {@link identityKeyToIdentifier} from `did-dht-utils.js`. */
|
|
439
|
+
static identityKeyToIdentifier(params) {
|
|
440
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
441
|
+
return identityKeyToIdentifier(params);
|
|
1114
442
|
});
|
|
1115
443
|
}
|
|
1116
|
-
/**
|
|
1117
|
-
* Returns the appropriate key converter for the specified cryptographic curve.
|
|
1118
|
-
*
|
|
1119
|
-
* @param curve - The cryptographic curve to use for the key conversion.
|
|
1120
|
-
* @returns An `AsymmetricKeyConverter` for the specified curve.
|
|
1121
|
-
*/
|
|
444
|
+
/** Delegates to {@link keyConverter} from `did-dht-utils.js`. */
|
|
1122
445
|
static keyConverter(curve) {
|
|
1123
|
-
|
|
1124
|
-
'Ed25519': Ed25519,
|
|
1125
|
-
'P-256': {
|
|
1126
|
-
// Wrap the key converter which produces uncompressed public key bytes to produce compressed key bytes as required by the DID DHT spec.
|
|
1127
|
-
// See https://did-dht.com/#representing-keys for more info.
|
|
1128
|
-
publicKeyToBytes: (_a) => __awaiter(this, [_a], void 0, function* ({ publicKey }) {
|
|
1129
|
-
const publicKeyBytes = yield Secp256r1.publicKeyToBytes({ publicKey });
|
|
1130
|
-
const compressedPublicKey = yield Secp256r1.compressPublicKey({ publicKeyBytes });
|
|
1131
|
-
return compressedPublicKey;
|
|
1132
|
-
}),
|
|
1133
|
-
bytesToPublicKey: Secp256r1.bytesToPublicKey,
|
|
1134
|
-
privateKeyToBytes: Secp256r1.privateKeyToBytes,
|
|
1135
|
-
bytesToPrivateKey: Secp256r1.bytesToPrivateKey,
|
|
1136
|
-
},
|
|
1137
|
-
'secp256k1': {
|
|
1138
|
-
// Wrap the key converter which produces uncompressed public key bytes to produce compressed key bytes as required by the DID DHT spec.
|
|
1139
|
-
// See https://did-dht.com/#representing-keys for more info.
|
|
1140
|
-
publicKeyToBytes: (_a) => __awaiter(this, [_a], void 0, function* ({ publicKey }) {
|
|
1141
|
-
const publicKeyBytes = yield Secp256k1.publicKeyToBytes({ publicKey });
|
|
1142
|
-
const compressedPublicKey = yield Secp256k1.compressPublicKey({ publicKeyBytes });
|
|
1143
|
-
return compressedPublicKey;
|
|
1144
|
-
}),
|
|
1145
|
-
bytesToPublicKey: Secp256k1.bytesToPublicKey,
|
|
1146
|
-
privateKeyToBytes: Secp256k1.privateKeyToBytes,
|
|
1147
|
-
bytesToPrivateKey: Secp256k1.bytesToPrivateKey,
|
|
1148
|
-
},
|
|
1149
|
-
X25519: X25519,
|
|
1150
|
-
};
|
|
1151
|
-
const converter = converters[curve];
|
|
1152
|
-
if (!converter) {
|
|
1153
|
-
throw new DidError(DidErrorCode.InvalidPublicKeyType, `Unsupported curve: ${curve}`);
|
|
1154
|
-
}
|
|
1155
|
-
return converter;
|
|
446
|
+
return keyConverter(curve);
|
|
1156
447
|
}
|
|
1157
|
-
/**
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
* @param params.bep44Message - The BEP44 message to verify and parse.
|
|
1162
|
-
* @returns A promise that resolves to a DNS packet.
|
|
1163
|
-
*/
|
|
1164
|
-
static parseBep44GetMessage(_a) {
|
|
1165
|
-
return __awaiter(this, arguments, void 0, function* ({ bep44Message }) {
|
|
1166
|
-
// Convert the public key byte array to JWK format.
|
|
1167
|
-
const publicKey = yield Ed25519.bytesToPublicKey({ publicKeyBytes: bep44Message.k });
|
|
1168
|
-
// Encode the sequence and DNS byte array to bencode format.
|
|
1169
|
-
const bencodedData = bencode.encode({ seq: bep44Message.seq, v: bep44Message.v }).subarray(1, -1);
|
|
1170
|
-
// Verify the signature of the BEP44 message.
|
|
1171
|
-
const isValid = yield Ed25519.verify({
|
|
1172
|
-
key: publicKey,
|
|
1173
|
-
signature: bep44Message.sig,
|
|
1174
|
-
data: bencodedData
|
|
1175
|
-
});
|
|
1176
|
-
if (!isValid) {
|
|
1177
|
-
throw new DidError(DidErrorCode.InvalidSignature, `Invalid signature for DHT BEP44 message`);
|
|
1178
|
-
}
|
|
1179
|
-
return dnsPacketDecode(bep44Message.v);
|
|
448
|
+
/** Delegates to {@link parseBep44GetMessage} from `did-dht-pkarr.js`. */
|
|
449
|
+
static parseBep44GetMessage(params) {
|
|
450
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
451
|
+
return parseBep44GetMessage(params);
|
|
1180
452
|
});
|
|
1181
453
|
}
|
|
1182
|
-
/**
|
|
1183
|
-
* Decodes and parses the data value of a DNS TXT record into a key-value object.
|
|
1184
|
-
*
|
|
1185
|
-
* @param txtData - The data value of a DNS TXT record.
|
|
1186
|
-
* @returns An object containing the key/value pairs of the TXT record data.
|
|
1187
|
-
*/
|
|
454
|
+
/** Delegates to {@link parseTxtDataToObject} from `did-dht-dns.js`. */
|
|
1188
455
|
static parseTxtDataToObject(txtData) {
|
|
1189
|
-
return
|
|
1190
|
-
const [key, value] = pair.split('=');
|
|
1191
|
-
acc[key] = value;
|
|
1192
|
-
return acc;
|
|
1193
|
-
}, {});
|
|
456
|
+
return parseTxtDataToObject(txtData);
|
|
1194
457
|
}
|
|
1195
|
-
/**
|
|
1196
|
-
* Decodes and parses the data value of a DNS TXT record into a string.
|
|
1197
|
-
*
|
|
1198
|
-
* @param txtData - The data value of a DNS TXT record.
|
|
1199
|
-
* @returns A string representation of the TXT record data.
|
|
1200
|
-
*/
|
|
458
|
+
/** Delegates to {@link parseTxtDataToString} from `did-dht-dns.js`. */
|
|
1201
459
|
static parseTxtDataToString(txtData) {
|
|
1202
|
-
|
|
1203
|
-
return txtData;
|
|
1204
|
-
}
|
|
1205
|
-
else if (txtData instanceof Uint8Array) {
|
|
1206
|
-
return Convert.uint8Array(txtData).toString();
|
|
1207
|
-
}
|
|
1208
|
-
else if (Array.isArray(txtData)) {
|
|
1209
|
-
return txtData.map(item => this.parseTxtDataToString(item)).join('');
|
|
1210
|
-
}
|
|
1211
|
-
else {
|
|
1212
|
-
throw new DidError(DidErrorCode.InternalError, 'Pkarr returned DNS TXT record with invalid data type');
|
|
1213
|
-
}
|
|
460
|
+
return parseTxtDataToString(txtData);
|
|
1214
461
|
}
|
|
1215
|
-
/**
|
|
1216
|
-
|
|
1217
|
-
|
|
1218
|
-
|
|
1219
|
-
* @param params.newDid - The new DID that the previous DID is linking to.
|
|
1220
|
-
* @param params.previousDidProof - The proof of the previous DID, containing the previous DID and signature signed by the previous DID.
|
|
1221
|
-
*/
|
|
1222
|
-
static validatePreviousDidProof(_a) {
|
|
1223
|
-
return __awaiter(this, arguments, void 0, function* ({ newDid, previousDidProof }) {
|
|
1224
|
-
const key = yield DidDhtUtils.identifierToIdentityKey({ didUri: previousDidProof.previousDid });
|
|
1225
|
-
const data = DidDhtUtils.identifierToIdentityKeyBytes({ didUri: newDid });
|
|
1226
|
-
const signature = Convert.base64Url(previousDidProof.signature).toUint8Array();
|
|
1227
|
-
const isValid = yield Ed25519.verify({ key, data, signature });
|
|
1228
|
-
if (!isValid) {
|
|
1229
|
-
throw new DidError(DidErrorCode.InvalidPreviousDidProof, 'The previous DID proof is invalid.');
|
|
1230
|
-
}
|
|
462
|
+
/** Delegates to {@link validatePreviousDidProof} from `did-dht-utils.js`. */
|
|
463
|
+
static validatePreviousDidProof(params) {
|
|
464
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
465
|
+
return validatePreviousDidProof(params);
|
|
1231
466
|
});
|
|
1232
467
|
}
|
|
1233
|
-
/**
|
|
1234
|
-
* Splits a string into chunks of length 255 if the string exceeds length 255.
|
|
1235
|
-
* @param data - The string to split into chunks.
|
|
1236
|
-
* @returns The original string if its length is less than or equal to 255, otherwise an array of chunked strings.
|
|
1237
|
-
*/
|
|
468
|
+
/** Delegates to {@link chunkDataIfNeeded} from `did-dht-dns.js`. */
|
|
1238
469
|
static chunkDataIfNeeded(data) {
|
|
1239
|
-
|
|
1240
|
-
return data;
|
|
1241
|
-
}
|
|
1242
|
-
// Split the data into chunks of 255 characters.
|
|
1243
|
-
const chunks = [];
|
|
1244
|
-
for (let i = 0; i < data.length; i += 255) {
|
|
1245
|
-
chunks.push(data.slice(i, i + 255)); // end index is ignored if it exceeds the length of the string
|
|
1246
|
-
}
|
|
1247
|
-
return chunks;
|
|
470
|
+
return chunkDataIfNeeded(data);
|
|
1248
471
|
}
|
|
1249
472
|
}
|
|
1250
473
|
//# sourceMappingURL=did-dht.js.map
|