@enbox/dids 0.0.3 → 0.0.5
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/index.js +2 -0
- package/dist/esm/index.js.map +1 -1
- 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-jwk.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/resolver/resolver-cache-memory.js +77 -0
- package/dist/esm/resolver/resolver-cache-memory.js.map +1 -0
- package/dist/esm/utils.js +25 -0
- package/dist/esm/utils.js.map +1 -1
- package/dist/types/bearer-did.d.ts +4 -4
- package/dist/types/bearer-did.d.ts.map +1 -1
- package/dist/types/index.d.ts +2 -0
- package/dist/types/index.d.ts.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 +45 -460
- 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 +4 -84
- package/dist/types/methods/did-ion.d.ts.map +1 -1
- package/dist/types/methods/did-jwk.d.ts +4 -4
- package/dist/types/methods/did-jwk.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 +6 -127
- package/dist/types/methods/did-key.d.ts.map +1 -1
- package/dist/types/methods/did-method.d.ts +3 -3
- package/dist/types/methods/did-method.d.ts.map +1 -1
- package/dist/types/resolver/resolver-cache-memory.d.ts +58 -0
- package/dist/types/resolver/resolver-cache-memory.d.ts.map +1 -0
- package/dist/types/utils.d.ts +19 -0
- 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 +11 -13
- package/src/bearer-did.ts +4 -4
- package/src/index.ts +2 -0
- 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 +125 -1131
- package/src/methods/did-ion-utils.ts +186 -0
- package/src/methods/did-ion.ts +16 -185
- package/src/methods/did-jwk.ts +4 -4
- package/src/methods/did-key-utils.ts +258 -0
- package/src/methods/did-key.ts +20 -272
- package/src/methods/did-method.ts +3 -3
- package/src/resolver/resolver-cache-memory.ts +84 -0
- package/src/utils.ts +27 -0
- package/dist/browser.js +0 -73
- package/dist/browser.js.map +0 -7
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@enbox/dids",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.5",
|
|
4
4
|
"description": "TBD DIDs library",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/esm/index.js",
|
|
@@ -9,14 +9,14 @@
|
|
|
9
9
|
"scripts": {
|
|
10
10
|
"clean": "rimraf dist",
|
|
11
11
|
"build:esm": "rimraf dist/esm dist/types && bun tsc -p tsconfig.json",
|
|
12
|
-
"build:browser": "rimraf dist/browser.mjs
|
|
13
|
-
"build:tests:browser": "rimraf tests/compiled && bun build/esbuild-tests.cjs",
|
|
12
|
+
"build:browser": "rimraf dist/browser.mjs && bun ../../build/browser-bundle.js --extra-entry src/utils.ts:dist/utils.js",
|
|
14
13
|
"build": "bun run clean && bun run build:esm && bun run build:browser",
|
|
15
14
|
"lint": "eslint . --max-warnings 0",
|
|
16
15
|
"lint:fix": "eslint . --fix",
|
|
17
16
|
"test:node": "bun test tests/",
|
|
18
17
|
"test:node:coverage": "bun test --coverage --coverage-reporter=text --coverage-reporter=lcov --coverage-dir=coverage tests/",
|
|
19
|
-
"test:browser": "bun
|
|
18
|
+
"test:browser": "bunx --bun vitest --config vitest.browser.config.ts --run",
|
|
19
|
+
"test:browser:coverage": "bunx --bun vitest --config vitest.browser.config.ts --run --coverage --coverage.provider=istanbul --coverage.reportsDirectory=./coverage-browser"
|
|
20
20
|
},
|
|
21
21
|
"homepage": "https://github.com/enboxorg/enbox/tree/main/packages/dids#readme",
|
|
22
22
|
"bugs": "https://github.com/enboxorg/enbox/issues",
|
|
@@ -50,10 +50,12 @@
|
|
|
50
50
|
],
|
|
51
51
|
"exports": {
|
|
52
52
|
".": {
|
|
53
|
+
"browser": "./dist/browser.mjs",
|
|
53
54
|
"types": "./dist/types/index.d.ts",
|
|
54
55
|
"import": "./dist/esm/index.js"
|
|
55
56
|
},
|
|
56
57
|
"./utils": {
|
|
58
|
+
"browser": "./dist/utils.js",
|
|
57
59
|
"types": "./dist/types/utils.d.ts",
|
|
58
60
|
"import": "./dist/esm/utils.js"
|
|
59
61
|
}
|
|
@@ -79,28 +81,24 @@
|
|
|
79
81
|
"@decentralized-identity/ion-sdk": "1.0.4",
|
|
80
82
|
"@dnsquery/dns-packet": "6.1.1",
|
|
81
83
|
"@enbox/common": "0.0.3",
|
|
82
|
-
"@enbox/crypto": "0.0.
|
|
84
|
+
"@enbox/crypto": "0.0.4",
|
|
83
85
|
"abstract-level": "1.0.4",
|
|
84
86
|
"bencode": "4.0.0",
|
|
85
87
|
"level": "8.0.1",
|
|
86
88
|
"ms": "2.1.3"
|
|
87
89
|
},
|
|
88
90
|
"devDependencies": {
|
|
89
|
-
"buffer": "6.0.3",
|
|
90
|
-
"@playwright/test": "1.45.3",
|
|
91
91
|
"@types/bencode": "2.0.4",
|
|
92
92
|
"@types/ms": "0.7.34",
|
|
93
93
|
"@types/node": "20.14.8",
|
|
94
94
|
"@typescript-eslint/eslint-plugin": "8.32.1",
|
|
95
95
|
"@typescript-eslint/parser": "8.32.1",
|
|
96
|
-
"@
|
|
97
|
-
"@
|
|
96
|
+
"@vitest/browser-playwright": "4.0.18",
|
|
97
|
+
"@vitest/coverage-istanbul": "4.0.18",
|
|
98
98
|
"bun-types": "latest",
|
|
99
|
-
"esbuild": "0.23.0",
|
|
100
99
|
"eslint": "9.7.0",
|
|
101
|
-
"playwright": "1.45.3",
|
|
102
100
|
"rimraf": "5.0.7",
|
|
103
|
-
"
|
|
104
|
-
"
|
|
101
|
+
"typescript": "5.5.4",
|
|
102
|
+
"vitest": "4.0.18"
|
|
105
103
|
}
|
|
106
104
|
}
|
package/src/bearer-did.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import type {
|
|
2
|
-
CryptoApi,
|
|
3
2
|
EnclosedSignParams,
|
|
4
3
|
EnclosedVerifyParams,
|
|
5
4
|
Jwk,
|
|
6
5
|
KeyIdentifier,
|
|
7
6
|
KeyImporterExporter,
|
|
7
|
+
KeyManager,
|
|
8
8
|
KmsExportKeyParams,
|
|
9
9
|
KmsImportKeyParams,
|
|
10
10
|
Signer,
|
|
@@ -77,13 +77,13 @@ export class BearerDid {
|
|
|
77
77
|
*
|
|
78
78
|
* Each DID method requires at least one key be present in the provided `keyManager`.
|
|
79
79
|
*/
|
|
80
|
-
keyManager:
|
|
80
|
+
keyManager: KeyManager;
|
|
81
81
|
|
|
82
82
|
constructor({ uri, document, metadata, keyManager }: {
|
|
83
83
|
uri: string,
|
|
84
84
|
document: DidDocument,
|
|
85
85
|
metadata: DidMetadata,
|
|
86
|
-
keyManager:
|
|
86
|
+
keyManager: KeyManager
|
|
87
87
|
}) {
|
|
88
88
|
this.uri = uri;
|
|
89
89
|
this.document = document;
|
|
@@ -238,7 +238,7 @@ export class BearerDid {
|
|
|
238
238
|
* keys for any verification method are missing in the key manager.
|
|
239
239
|
*/
|
|
240
240
|
public static async import({ portableDid, keyManager = new LocalKeyManager() }: {
|
|
241
|
-
keyManager?:
|
|
241
|
+
keyManager?: KeyManager & KeyImporterExporter<KmsImportKeyParams, KeyIdentifier, KmsExportKeyParams>;
|
|
242
242
|
portableDid: PortableDid;
|
|
243
243
|
}): Promise<BearerDid> {
|
|
244
244
|
|
package/src/index.ts
CHANGED
|
@@ -15,7 +15,9 @@ export * from './methods/did-method.js';
|
|
|
15
15
|
export * from './methods/did-web.js';
|
|
16
16
|
|
|
17
17
|
export * from './resolver/resolver-cache-level.js';
|
|
18
|
+
export * from './resolver/resolver-cache-memory.js';
|
|
18
19
|
export * from './resolver/resolver-cache-noop.js';
|
|
19
20
|
export * from './resolver/universal-resolver.js';
|
|
20
21
|
|
|
22
|
+
export { isPortableDid } from './utils.js';
|
|
21
23
|
export * as utils from './utils.js';
|
|
@@ -0,0 +1,516 @@
|
|
|
1
|
+
import type { DidMetadata } from '../types/portable-did.js';
|
|
2
|
+
import type { PreviousDidProof } from './did-dht-types.js';
|
|
3
|
+
import type { DidDocument, DidService } from '../types/did-core.js';
|
|
4
|
+
import type { Packet, StringAnswer, TxtAnswer, TxtData } from '@dnsquery/dns-packet';
|
|
5
|
+
|
|
6
|
+
import { AUTHORITATIVE_ANSWER } from '@dnsquery/dns-packet';
|
|
7
|
+
import { computeJwkThumbprint } from '@enbox/crypto';
|
|
8
|
+
import { Convert } from '@enbox/common';
|
|
9
|
+
import { DidVerificationRelationship } from '../types/did-core.js';
|
|
10
|
+
import { extractDidFragment } from '../utils.js';
|
|
11
|
+
import { DidDhtRegisteredDidType, DidDhtRegisteredKeyType, DidDhtVerificationRelationship } from './did-dht-types.js';
|
|
12
|
+
import { DidError, DidErrorCode } from '../did-error.js';
|
|
13
|
+
import { keyConverter, validatePreviousDidProof } from './did-dht-utils.js';
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* The version of the DID DHT specification that is implemented by this library.
|
|
17
|
+
*/
|
|
18
|
+
export const DID_DHT_SPECIFICATION_VERSION = 0;
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* The default TTL for DNS records published to the DHT network.
|
|
22
|
+
*
|
|
23
|
+
* The recommended TTL value is 7200 seconds (2 hours) since it matches the default TTL for
|
|
24
|
+
* Mainline DHT records.
|
|
25
|
+
*/
|
|
26
|
+
export const DNS_RECORD_TTL = 7200;
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Character used to separate distinct elements or entries in the DNS packet representation
|
|
30
|
+
* of a DID Document.
|
|
31
|
+
*/
|
|
32
|
+
export const PROPERTY_SEPARATOR = ';';
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Character used to separate distinct values within a single element or entry in the DNS packet
|
|
36
|
+
* representation of a DID Document.
|
|
37
|
+
*/
|
|
38
|
+
export const VALUE_SEPARATOR = ',';
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Private helper that maps algorithm identifiers to their corresponding DID DHT
|
|
42
|
+
* {@link DidDhtRegisteredKeyType | registered key type}.
|
|
43
|
+
*/
|
|
44
|
+
export const AlgorithmToKeyTypeMap = {
|
|
45
|
+
Ed25519 : DidDhtRegisteredKeyType.Ed25519,
|
|
46
|
+
ES256K : DidDhtRegisteredKeyType.secp256k1,
|
|
47
|
+
ES256 : DidDhtRegisteredKeyType.secp256r1,
|
|
48
|
+
'P-256' : DidDhtRegisteredKeyType.secp256r1,
|
|
49
|
+
secp256k1 : DidDhtRegisteredKeyType.secp256k1,
|
|
50
|
+
secp256r1 : DidDhtRegisteredKeyType.secp256r1,
|
|
51
|
+
X25519 : DidDhtRegisteredKeyType.X25519,
|
|
52
|
+
} as const;
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Private helper that maps DID DHT registered key types to their corresponding default algorithm identifiers.
|
|
56
|
+
*/
|
|
57
|
+
export const KeyTypeToDefaultAlgorithmMap = {
|
|
58
|
+
[DidDhtRegisteredKeyType.Ed25519] : 'EdDSA',
|
|
59
|
+
[DidDhtRegisteredKeyType.secp256k1] : 'ES256K',
|
|
60
|
+
[DidDhtRegisteredKeyType.secp256r1] : 'ES256',
|
|
61
|
+
[DidDhtRegisteredKeyType.X25519] : 'ECDH-ES+A256KW',
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Creates a TXT DNS answer record with the given name and data, using the standard DNS_RECORD_TTL.
|
|
66
|
+
*
|
|
67
|
+
* @param name - The DNS record name.
|
|
68
|
+
* @param data - The TXT record data (string or string array).
|
|
69
|
+
* @returns A TxtAnswer record.
|
|
70
|
+
*/
|
|
71
|
+
export function createTxtRecord(name: string, data: string | string[]): TxtAnswer {
|
|
72
|
+
return {
|
|
73
|
+
type : 'TXT',
|
|
74
|
+
name,
|
|
75
|
+
ttl : DNS_RECORD_TTL,
|
|
76
|
+
data,
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Decodes and parses the data value of a DNS TXT record into a string.
|
|
82
|
+
*
|
|
83
|
+
* @param txtData - The data value of a DNS TXT record.
|
|
84
|
+
* @returns A string representation of the TXT record data.
|
|
85
|
+
*/
|
|
86
|
+
export function parseTxtDataToString(txtData: TxtData): string {
|
|
87
|
+
if (typeof txtData === 'string') {
|
|
88
|
+
return txtData;
|
|
89
|
+
} else if (txtData instanceof Uint8Array) {
|
|
90
|
+
return Convert.uint8Array(txtData).toString();
|
|
91
|
+
} else if (Array.isArray(txtData)) {
|
|
92
|
+
return txtData.map((item: TxtData): string => parseTxtDataToString(item)).join('');
|
|
93
|
+
} else {
|
|
94
|
+
throw new DidError(DidErrorCode.InternalError, 'Pkarr returned DNS TXT record with invalid data type');
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Decodes and parses the data value of a DNS TXT record into a key-value object.
|
|
100
|
+
*
|
|
101
|
+
* @param txtData - The data value of a DNS TXT record.
|
|
102
|
+
* @returns An object containing the key/value pairs of the TXT record data.
|
|
103
|
+
*/
|
|
104
|
+
export function parseTxtDataToObject(txtData: TxtData): Record<string, string> {
|
|
105
|
+
return parseTxtDataToString(txtData).split(PROPERTY_SEPARATOR).reduce((acc: Record<string, string>, pair: string): Record<string, string> => {
|
|
106
|
+
const [key, value] = pair.split('=');
|
|
107
|
+
acc[key] = value;
|
|
108
|
+
return acc;
|
|
109
|
+
}, {} as Record<string, string>);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Splits a string into chunks of length 255 if the string exceeds length 255.
|
|
114
|
+
*
|
|
115
|
+
* @param data - The string to split into chunks.
|
|
116
|
+
* @returns The original string if its length is less than or equal to 255, otherwise an array of chunked strings.
|
|
117
|
+
*/
|
|
118
|
+
export function chunkDataIfNeeded(data: string): string | string[] {
|
|
119
|
+
if (data.length <= 255) {
|
|
120
|
+
return data;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// Split the data into chunks of 255 characters.
|
|
124
|
+
const chunks: string[] = [];
|
|
125
|
+
for (let i = 0; i < data.length; i += 255) {
|
|
126
|
+
chunks.push(data.slice(i, i + 255)); // end index is ignored if it exceeds the length of the string
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
return chunks;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Converts a DNS packet to a DID document according to the DID DHT specification.
|
|
134
|
+
*
|
|
135
|
+
* @see {@link https://did-dht.com/#dids-as-dns-records | DID DHT Specification, § DIDs as DNS Records}
|
|
136
|
+
*
|
|
137
|
+
* @param params - The parameters to use when converting a DNS packet to a DID document.
|
|
138
|
+
* @param params.didUri - The DID URI of the DID document.
|
|
139
|
+
* @param params.dnsPacket - The DNS packet to convert to a DID document.
|
|
140
|
+
* @returns A Promise resolving to an object containing the DID document and its metadata.
|
|
141
|
+
*/
|
|
142
|
+
export async function fromDnsPacket({ didUri, dnsPacket }: {
|
|
143
|
+
didUri: string;
|
|
144
|
+
dnsPacket: Packet;
|
|
145
|
+
}): Promise<{ didDocument: DidDocument; didDocumentMetadata: DidMetadata; didResolutionMetadata: Record<string, never> }> {
|
|
146
|
+
// Begin constructing the DID Document.
|
|
147
|
+
const didDocument: DidDocument = { id: didUri };
|
|
148
|
+
|
|
149
|
+
// Since the DID document is being retrieved from the DHT, it is considered published.
|
|
150
|
+
const didDocumentMetadata: DidMetadata = {
|
|
151
|
+
published: true
|
|
152
|
+
};
|
|
153
|
+
|
|
154
|
+
const idLookup = new Map<string, string>();
|
|
155
|
+
|
|
156
|
+
for (const answer of dnsPacket?.answers ?? []) {
|
|
157
|
+
// DID DHT properties are ONLY present in DNS TXT records.
|
|
158
|
+
if (answer.type !== 'TXT') {continue;}
|
|
159
|
+
|
|
160
|
+
// Get the DID DHT record identifier (e.g., k0, aka, did, etc.) from the DNS resource name.
|
|
161
|
+
const dnsRecordId = answer.name.split('.')[0].substring(1);
|
|
162
|
+
|
|
163
|
+
switch (true) {
|
|
164
|
+
// Process an also known as record.
|
|
165
|
+
case dnsRecordId.startsWith('aka'): {
|
|
166
|
+
// Decode the DNS TXT record data value to a string.
|
|
167
|
+
const data = parseTxtDataToString(answer.data);
|
|
168
|
+
|
|
169
|
+
// Add the 'alsoKnownAs' property to the DID document.
|
|
170
|
+
didDocument.alsoKnownAs = data.split(VALUE_SEPARATOR);
|
|
171
|
+
|
|
172
|
+
break;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
// Process a controller record.
|
|
176
|
+
case dnsRecordId.startsWith('cnt'): {
|
|
177
|
+
// Decode the DNS TXT record data value to a string.
|
|
178
|
+
const data = parseTxtDataToString(answer.data);
|
|
179
|
+
|
|
180
|
+
// Add the 'controller' property to the DID document.
|
|
181
|
+
didDocument.controller = data.includes(VALUE_SEPARATOR) ? data.split(VALUE_SEPARATOR) : data;
|
|
182
|
+
|
|
183
|
+
break;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
// Process verification methods.
|
|
187
|
+
case dnsRecordId.startsWith('k'): {
|
|
188
|
+
// Get the key type (t), Base64URL-encoded public key (k), algorithm (a), and
|
|
189
|
+
// optionally, controller (c) or Verification Method ID (id) from the decoded TXT record data.
|
|
190
|
+
const { id, t, k, c, a: parsedAlg } = parseTxtDataToObject(answer.data);
|
|
191
|
+
|
|
192
|
+
// Convert the public key from Base64URL format to a byte array.
|
|
193
|
+
const publicKeyBytes = Convert.base64Url(k).toUint8Array();
|
|
194
|
+
|
|
195
|
+
// Use the key type integer to look up the cryptographic curve name.
|
|
196
|
+
const namedCurve = DidDhtRegisteredKeyType[Number(t)];
|
|
197
|
+
|
|
198
|
+
// Convert the public key from a byte array to JWK format.
|
|
199
|
+
const publicKey = await keyConverter(namedCurve).bytesToPublicKey({ publicKeyBytes });
|
|
200
|
+
|
|
201
|
+
publicKey.alg = parsedAlg || KeyTypeToDefaultAlgorithmMap[Number(t) as DidDhtRegisteredKeyType];
|
|
202
|
+
|
|
203
|
+
// TODO: when this is complete https://github.com/enboxorg/enbox/issues/638 then we can add this back and
|
|
204
|
+
// update the test vectors kid back to '0'
|
|
205
|
+
// if(dnsRecordId === 'k0') {
|
|
206
|
+
// publicKey.kid = '0';
|
|
207
|
+
// }
|
|
208
|
+
|
|
209
|
+
// Determine the Verification Method ID: '0' for the identity key,
|
|
210
|
+
// the id from the TXT Data Object, or the JWK thumbprint if an explicity Verification Method ID not defined.
|
|
211
|
+
const vmId = dnsRecordId === 'k0' ? '0' : id !== undefined ? id : await computeJwkThumbprint({ jwk: publicKey });
|
|
212
|
+
|
|
213
|
+
// Initialize the `verificationMethod` array if it does not already exist.
|
|
214
|
+
didDocument.verificationMethod ??= [];
|
|
215
|
+
|
|
216
|
+
// Prepend the DID URI to the ID fragment to form the full verification method ID.
|
|
217
|
+
const methodId = `${didUri}#${vmId}`;
|
|
218
|
+
|
|
219
|
+
// Add the verification method to the DID document.
|
|
220
|
+
didDocument.verificationMethod.push({
|
|
221
|
+
id : methodId,
|
|
222
|
+
type : 'JsonWebKey',
|
|
223
|
+
controller : c ?? didUri,
|
|
224
|
+
publicKeyJwk : publicKey,
|
|
225
|
+
});
|
|
226
|
+
|
|
227
|
+
// Add a mapping from the DNS record ID (e.g., 'k0', 'k1', etc.) to the verification
|
|
228
|
+
// method ID (e.g., 'did:dht:...#0', etc.).
|
|
229
|
+
idLookup.set(dnsRecordId, methodId);
|
|
230
|
+
|
|
231
|
+
break;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
// Process services.
|
|
235
|
+
case dnsRecordId.startsWith('s'): {
|
|
236
|
+
// Get the service ID fragment (id), type (t), service endpoint (se), and optionally,
|
|
237
|
+
// other properties from the decoded TXT record data.
|
|
238
|
+
const { id, t, se, ...customProperties } = parseTxtDataToObject(answer.data);
|
|
239
|
+
|
|
240
|
+
// if multi-values: 'a,b,c' -> ['a', 'b', 'c'], if single-value: 'a' -> ['a']
|
|
241
|
+
// NOTE: The service endpoint technically can either be a string or an array of strings,
|
|
242
|
+
// we enforce an array for single-value to simplify verification of vector 3 in the spec: https://did-dht.com/#vector-3
|
|
243
|
+
const serviceEndpoint = se.includes(VALUE_SEPARATOR) ? se.split(VALUE_SEPARATOR) : [se];
|
|
244
|
+
|
|
245
|
+
// Convert custom property values to either a string or an array of strings.
|
|
246
|
+
const serviceProperties = Object.fromEntries(Object.entries(customProperties).map(
|
|
247
|
+
([k, v]: [string, string]): [string, string | string[]] => [k, v.includes(VALUE_SEPARATOR) ? v.split(VALUE_SEPARATOR) : v]
|
|
248
|
+
));
|
|
249
|
+
|
|
250
|
+
// Initialize the `service` array if it does not already exist.
|
|
251
|
+
didDocument.service ??= [];
|
|
252
|
+
|
|
253
|
+
didDocument.service.push({
|
|
254
|
+
...serviceProperties,
|
|
255
|
+
id : `${didUri}#${id}`,
|
|
256
|
+
type : t,
|
|
257
|
+
serviceEndpoint
|
|
258
|
+
});
|
|
259
|
+
|
|
260
|
+
break;
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
// Process DID DHT types.
|
|
264
|
+
case dnsRecordId.startsWith('typ'): {
|
|
265
|
+
// Decode the DNS TXT record data value to an object.
|
|
266
|
+
const { id: types } = parseTxtDataToObject(answer.data);
|
|
267
|
+
|
|
268
|
+
// Add the DID DHT Registered DID Types represented as numbers to DID metadata.
|
|
269
|
+
didDocumentMetadata.types = types.split(VALUE_SEPARATOR).map((typeInteger: string): number => Number(typeInteger));
|
|
270
|
+
|
|
271
|
+
break;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
// Process root record.
|
|
275
|
+
case dnsRecordId.startsWith('did'): {
|
|
276
|
+
// Helper function that maps verification relationship values to verification method IDs.
|
|
277
|
+
const recordIdsToMethodIds = (data: string): string[] => data
|
|
278
|
+
.split(VALUE_SEPARATOR)
|
|
279
|
+
.map((dnsRecordId: string): string | undefined => idLookup.get(dnsRecordId))
|
|
280
|
+
.filter((id): id is string => typeof id === 'string');
|
|
281
|
+
|
|
282
|
+
// Decode the DNS TXT record data and destructure verification relationship properties.
|
|
283
|
+
const { auth, asm, del, inv, agm } = parseTxtDataToObject(answer.data);
|
|
284
|
+
|
|
285
|
+
// Add the verification relationships, if any, to the DID document.
|
|
286
|
+
if (auth) {didDocument.authentication = recordIdsToMethodIds(auth);}
|
|
287
|
+
if (asm) {didDocument.assertionMethod = recordIdsToMethodIds(asm);}
|
|
288
|
+
if (del) {didDocument.capabilityDelegation = recordIdsToMethodIds(del);}
|
|
289
|
+
if (inv) {didDocument.capabilityInvocation = recordIdsToMethodIds(inv);}
|
|
290
|
+
if (agm) {didDocument.keyAgreement = recordIdsToMethodIds(agm);}
|
|
291
|
+
|
|
292
|
+
break;
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
return { didDocument, didDocumentMetadata, didResolutionMetadata: {} };
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
/**
|
|
301
|
+
* Converts a DID document to a DNS packet according to the DID DHT specification.
|
|
302
|
+
*
|
|
303
|
+
* @see {@link https://did-dht.com/#dids-as-dns-records | DID DHT Specification, § DIDs as DNS Records}
|
|
304
|
+
*
|
|
305
|
+
* @param params - The parameters to use when converting a DID document to a DNS packet.
|
|
306
|
+
* @param params.didDocument - The DID document to convert to a DNS packet.
|
|
307
|
+
* @param params.didMetadata - The DID metadata to include in the DNS packet.
|
|
308
|
+
* @param params.authoritativeGatewayUris - The URIs of the Authoritative Gateways to generate NS records from.
|
|
309
|
+
* @param params.previousDidProof - The signature proof that this DID is linked to the given previous DID.
|
|
310
|
+
* @returns A promise that resolves to a DNS packet.
|
|
311
|
+
*/
|
|
312
|
+
export async function toDnsPacket({ didDocument, didMetadata, authoritativeGatewayUris, previousDidProof }: {
|
|
313
|
+
didDocument: DidDocument;
|
|
314
|
+
didMetadata: DidMetadata;
|
|
315
|
+
authoritativeGatewayUris?: string[];
|
|
316
|
+
previousDidProof?: PreviousDidProof;
|
|
317
|
+
}): Promise<Packet> {
|
|
318
|
+
const txtRecords: TxtAnswer[] = [];
|
|
319
|
+
const nsRecords: StringAnswer[] = [];
|
|
320
|
+
const idLookup = new Map<string, string>();
|
|
321
|
+
const serviceIds: string[] = [];
|
|
322
|
+
const verificationMethodIds: string[] = [];
|
|
323
|
+
|
|
324
|
+
// Add `_prv._did.` TXT record if previous DID proof is provided and valid.
|
|
325
|
+
if (previousDidProof !== undefined) {
|
|
326
|
+
const { signature, previousDid } = previousDidProof;
|
|
327
|
+
|
|
328
|
+
await validatePreviousDidProof({
|
|
329
|
+
newDid: didDocument.id,
|
|
330
|
+
previousDidProof
|
|
331
|
+
});
|
|
332
|
+
|
|
333
|
+
txtRecords.push({
|
|
334
|
+
type : 'TXT',
|
|
335
|
+
name : '_prv._did.',
|
|
336
|
+
ttl : DNS_RECORD_TTL,
|
|
337
|
+
data : `id=${previousDid};s=${signature}`
|
|
338
|
+
});
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
// Add DNS TXT records if the DID document contains an `alsoKnownAs` property.
|
|
342
|
+
if (didDocument.alsoKnownAs) {
|
|
343
|
+
txtRecords.push({
|
|
344
|
+
type : 'TXT',
|
|
345
|
+
name : '_aka._did.',
|
|
346
|
+
ttl : DNS_RECORD_TTL,
|
|
347
|
+
data : didDocument.alsoKnownAs.join(VALUE_SEPARATOR)
|
|
348
|
+
});
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
// Add DNS TXT records if the DID document contains a `controller` property.
|
|
352
|
+
if (didDocument.controller) {
|
|
353
|
+
const controller = Array.isArray(didDocument.controller)
|
|
354
|
+
? didDocument.controller.join(VALUE_SEPARATOR)
|
|
355
|
+
: didDocument.controller;
|
|
356
|
+
txtRecords.push({
|
|
357
|
+
type : 'TXT',
|
|
358
|
+
name : '_cnt._did.',
|
|
359
|
+
ttl : DNS_RECORD_TTL,
|
|
360
|
+
data : controller
|
|
361
|
+
});
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
// Add DNS TXT records for each verification method.
|
|
365
|
+
for (const [index, verificationMethod] of didDocument.verificationMethod?.entries() ?? []) {
|
|
366
|
+
const dnsRecordId = `k${index}`;
|
|
367
|
+
verificationMethodIds.push(dnsRecordId);
|
|
368
|
+
const methodId = verificationMethod.id.split('#').pop()!; // Remove fragment prefix, if any.
|
|
369
|
+
idLookup.set(methodId, dnsRecordId);
|
|
370
|
+
|
|
371
|
+
const publicKey = verificationMethod.publicKeyJwk;
|
|
372
|
+
|
|
373
|
+
if (!(publicKey?.crv && publicKey.crv in AlgorithmToKeyTypeMap)) {
|
|
374
|
+
throw new DidError(DidErrorCode.InvalidPublicKeyType, `Verification method '${verificationMethod.id}' contains an unsupported key type: ${publicKey?.crv ?? 'undefined'}`);
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
// Use the public key's `crv` property to get the DID DHT key type.
|
|
378
|
+
const keyType = DidDhtRegisteredKeyType[publicKey.crv as keyof typeof DidDhtRegisteredKeyType];
|
|
379
|
+
|
|
380
|
+
// Convert the public key from JWK format to a byte array.
|
|
381
|
+
const publicKeyBytes = await keyConverter(publicKey.crv).publicKeyToBytes({ publicKey });
|
|
382
|
+
|
|
383
|
+
// Convert the public key from a byte array to Base64URL format.
|
|
384
|
+
const publicKeyBase64Url = Convert.uint8Array(publicKeyBytes).toBase64Url();
|
|
385
|
+
|
|
386
|
+
// Define the data for the DNS TXT record.
|
|
387
|
+
const txtData = [`t=${keyType}`, `k=${publicKeyBase64Url}`];
|
|
388
|
+
// if the methodId is not the identity key or a thumbprint, explicity define the id within the DNS TXT record.
|
|
389
|
+
// otherwise the id can be inferred from the thumbprint.
|
|
390
|
+
if (methodId !== '0' && await computeJwkThumbprint({ jwk: publicKey }) !== methodId) {
|
|
391
|
+
txtData.unshift(`id=${methodId}`);
|
|
392
|
+
}
|
|
393
|
+
// Only set the algorithm property (`a`) if it differs from the default algorithm for the key type.
|
|
394
|
+
if (publicKey.alg !== KeyTypeToDefaultAlgorithmMap[keyType]) {
|
|
395
|
+
txtData.push(`a=${publicKey.alg}`);
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
// Add the controller property, if set to a value other than the Identity Key (DID Subject).
|
|
399
|
+
if (verificationMethod.controller !== didDocument.id) {txtData.push(`c=${verificationMethod.controller}`);}
|
|
400
|
+
|
|
401
|
+
// Add a TXT record for the verification method.
|
|
402
|
+
txtRecords.push({
|
|
403
|
+
type : 'TXT',
|
|
404
|
+
name : `_${dnsRecordId}._did.`,
|
|
405
|
+
ttl : DNS_RECORD_TTL,
|
|
406
|
+
data : txtData.join(PROPERTY_SEPARATOR)
|
|
407
|
+
});
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
// Add DNS TXT records for each service.
|
|
411
|
+
didDocument.service?.forEach((service: DidService, index: number): void => {
|
|
412
|
+
const dnsRecordId = `s${index}`;
|
|
413
|
+
serviceIds.push(dnsRecordId);
|
|
414
|
+
|
|
415
|
+
let { id, type: t, serviceEndpoint: se, ...customProperties } = service;
|
|
416
|
+
id = extractDidFragment(id)!;
|
|
417
|
+
se = Array.isArray(se) ? se.join(',') : se;
|
|
418
|
+
|
|
419
|
+
// Define the data for the DNS TXT record.
|
|
420
|
+
const txtData = Object.entries({ id, t, se, ...customProperties }).map(
|
|
421
|
+
([key, value]: [string, unknown]): string => `${key}=${value}`
|
|
422
|
+
);
|
|
423
|
+
|
|
424
|
+
const txtDataString = txtData.join(PROPERTY_SEPARATOR);
|
|
425
|
+
const data = chunkDataIfNeeded(txtDataString);
|
|
426
|
+
|
|
427
|
+
// Add a TXT record for the verification method.
|
|
428
|
+
txtRecords.push({
|
|
429
|
+
type : 'TXT',
|
|
430
|
+
name : `_${dnsRecordId}._did.`,
|
|
431
|
+
ttl : DNS_RECORD_TTL,
|
|
432
|
+
data
|
|
433
|
+
});
|
|
434
|
+
});
|
|
435
|
+
|
|
436
|
+
// Initialize the root DNS TXT record with the DID DHT specification version.
|
|
437
|
+
const rootRecord: string[] = [`v=${DID_DHT_SPECIFICATION_VERSION}`];
|
|
438
|
+
|
|
439
|
+
// Add verification methods to the root record.
|
|
440
|
+
if (verificationMethodIds.length) {
|
|
441
|
+
rootRecord.push(`vm=${verificationMethodIds.join(VALUE_SEPARATOR)}`);
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
// Add verification relationships to the root record.
|
|
445
|
+
Object.keys(DidVerificationRelationship).forEach((relationship: string): void => {
|
|
446
|
+
// Collect the verification method IDs for the given relationship.
|
|
447
|
+
const dnsRecordIds = (didDocument[relationship as keyof DidDocument] as string[] | undefined)
|
|
448
|
+
?.map((id: string): string | undefined => idLookup.get(id.split('#').pop()!));
|
|
449
|
+
|
|
450
|
+
// If the relationship includes verification methods, add them to the root record.
|
|
451
|
+
if (dnsRecordIds) {
|
|
452
|
+
const recordName = DidDhtVerificationRelationship[relationship as keyof typeof DidDhtVerificationRelationship];
|
|
453
|
+
rootRecord.push(`${recordName}=${dnsRecordIds.join(VALUE_SEPARATOR)}`);
|
|
454
|
+
}
|
|
455
|
+
});
|
|
456
|
+
|
|
457
|
+
// Add services to the root record.
|
|
458
|
+
if (serviceIds.length) {
|
|
459
|
+
rootRecord.push(`svc=${serviceIds.join(VALUE_SEPARATOR)}`);
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
// If defined, add a DNS TXT record for each registered DID type.
|
|
463
|
+
if (didMetadata.types?.length) {
|
|
464
|
+
// DID types can be specified as either a string or a number, so we need to normalize the
|
|
465
|
+
// values to integers.
|
|
466
|
+
const types = didMetadata.types as (DidDhtRegisteredDidType | keyof typeof DidDhtRegisteredDidType)[];
|
|
467
|
+
const typeIntegers = types.map((type: DidDhtRegisteredDidType | keyof typeof DidDhtRegisteredDidType): DidDhtRegisteredDidType =>
|
|
468
|
+
typeof type === 'string' ? DidDhtRegisteredDidType[type] : type
|
|
469
|
+
);
|
|
470
|
+
|
|
471
|
+
txtRecords.push({
|
|
472
|
+
type : 'TXT',
|
|
473
|
+
name : '_typ._did.',
|
|
474
|
+
ttl : DNS_RECORD_TTL,
|
|
475
|
+
data : `id=${typeIntegers.join(VALUE_SEPARATOR)}`
|
|
476
|
+
});
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
// Add a DNS TXT record for the root record.
|
|
480
|
+
txtRecords.push({
|
|
481
|
+
type : 'TXT',
|
|
482
|
+
name : '_did.' + getUniqueDidSuffix(didDocument.id) + '.', // name of a Root Record MUST end in `<ID>.`
|
|
483
|
+
ttl : DNS_RECORD_TTL,
|
|
484
|
+
data : rootRecord.join(PROPERTY_SEPARATOR)
|
|
485
|
+
});
|
|
486
|
+
|
|
487
|
+
// Add an NS record for each authoritative gateway URI.
|
|
488
|
+
for (const gatewayUri of authoritativeGatewayUris || []) {
|
|
489
|
+
nsRecords.push({
|
|
490
|
+
type : 'NS',
|
|
491
|
+
name : '_did.' + getUniqueDidSuffix(didDocument.id) + '.', // name of an NS record a authoritative gateway MUST end in `<ID>.`
|
|
492
|
+
ttl : DNS_RECORD_TTL,
|
|
493
|
+
data : gatewayUri + '.'
|
|
494
|
+
});
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
// Create a DNS response packet with the authoritative answer flag set.
|
|
498
|
+
const dnsPacket: Packet = {
|
|
499
|
+
id : 0,
|
|
500
|
+
type : 'response',
|
|
501
|
+
flags : AUTHORITATIVE_ANSWER,
|
|
502
|
+
answers : [...txtRecords, ...nsRecords]
|
|
503
|
+
};
|
|
504
|
+
|
|
505
|
+
return dnsPacket;
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
/**
|
|
509
|
+
* Gets the unique portion of the DID identifier after the last `:` character.
|
|
510
|
+
* e.g. `did:dht:example` -> `example`
|
|
511
|
+
*
|
|
512
|
+
* @param did - The DID to extract the unique suffix from.
|
|
513
|
+
*/
|
|
514
|
+
function getUniqueDidSuffix(did: string): string {
|
|
515
|
+
return did.split(':')[2];
|
|
516
|
+
}
|