@oma3/omatrust 0.1.0-alpha.10 → 0.1.0-alpha.12
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/README.md +3 -0
- package/dist/app-registry/index.cjs +53 -0
- package/dist/app-registry/index.cjs.map +1 -1
- package/dist/app-registry/index.js +53 -0
- package/dist/app-registry/index.js.map +1 -1
- package/dist/identity/index.cjs +1028 -6
- package/dist/identity/index.cjs.map +1 -1
- package/dist/identity/index.d.cts +2 -1
- package/dist/identity/index.d.ts +2 -1
- package/dist/identity/index.js +984 -7
- package/dist/identity/index.js.map +1 -1
- package/dist/index-B5OC9_8B.d.cts +480 -0
- package/dist/index-Bu-xxcv9.d.ts +407 -0
- package/dist/index-C6WNgPRx.d.ts +480 -0
- package/dist/index-C7odEbp6.d.cts +407 -0
- package/dist/index.cjs +2498 -373
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +4 -3
- package/dist/index.d.ts +4 -3
- package/dist/index.js +2342 -236
- package/dist/index.js.map +1 -1
- package/dist/reputation/index.browser.cjs +949 -67
- package/dist/reputation/index.browser.cjs.map +1 -1
- package/dist/reputation/index.browser.d.cts +2 -2
- package/dist/reputation/index.browser.d.ts +2 -2
- package/dist/reputation/index.browser.js +947 -67
- package/dist/reputation/index.browser.js.map +1 -1
- package/dist/reputation/index.cjs +1745 -232
- package/dist/reputation/index.cjs.map +1 -1
- package/dist/reputation/index.d.cts +3 -2
- package/dist/reputation/index.d.ts +3 -2
- package/dist/reputation/index.js +1727 -232
- package/dist/reputation/index.js.map +1 -1
- package/dist/{subject-ownership-CXvzEjpH.d.cts → subject-ownership-B7cFlm8c.d.cts} +256 -26
- package/dist/{subject-ownership-CXvzEjpH.d.ts → subject-ownership-B7cFlm8c.d.ts} +256 -26
- package/dist/types-Dn0OwaNj.d.cts +307 -0
- package/dist/types-Dn0OwaNj.d.ts +307 -0
- package/dist/widgets/index.cjs +70 -27
- package/dist/widgets/index.cjs.map +1 -1
- package/dist/widgets/index.d.cts +45 -18
- package/dist/widgets/index.d.ts +45 -18
- package/dist/widgets/index.js +67 -26
- package/dist/widgets/index.js.map +1 -1
- package/package.json +5 -2
- package/dist/index-B9KW02US.d.cts +0 -119
- package/dist/index-DXrwBex9.d.ts +0 -119
- package/dist/index-QZDExA4I.d.cts +0 -90
- package/dist/index-QZDExA4I.d.ts +0 -90
package/dist/identity/index.js
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
import { keccak256, toUtf8Bytes, isAddress, getAddress, sha256 } from 'ethers';
|
|
2
|
+
import { base64url, calculateJwkThumbprint } from 'jose';
|
|
2
3
|
import canonicalize from 'canonicalize';
|
|
4
|
+
import { CID } from 'multiformats/cid';
|
|
5
|
+
import { sha256 as sha256$1 } from 'multiformats/hashes/sha2';
|
|
6
|
+
import * as raw from 'multiformats/codecs/raw';
|
|
7
|
+
import { base32 } from 'multiformats/bases/base32';
|
|
8
|
+
import { base58btc } from 'multiformats/bases/base58';
|
|
3
9
|
|
|
4
10
|
// src/identity/did.ts
|
|
5
11
|
|
|
@@ -7,18 +13,23 @@ import canonicalize from 'canonicalize';
|
|
|
7
13
|
var OmaTrustError = class extends Error {
|
|
8
14
|
code;
|
|
9
15
|
details;
|
|
10
|
-
constructor(
|
|
16
|
+
constructor(code2, message, details) {
|
|
11
17
|
super(message);
|
|
12
18
|
this.name = "OmaTrustError";
|
|
13
|
-
this.code =
|
|
19
|
+
this.code = code2;
|
|
14
20
|
this.details = details;
|
|
15
21
|
}
|
|
16
22
|
};
|
|
17
23
|
|
|
18
24
|
// src/shared/assert.ts
|
|
19
|
-
function assertString(value, name,
|
|
25
|
+
function assertString(value, name, code2 = "INVALID_INPUT") {
|
|
20
26
|
if (typeof value !== "string" || value.trim().length === 0) {
|
|
21
|
-
throw new OmaTrustError(
|
|
27
|
+
throw new OmaTrustError(code2, `${name} must be a non-empty string`, { value });
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
function assertObject(value, name, code2 = "INVALID_INPUT") {
|
|
31
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) {
|
|
32
|
+
throw new OmaTrustError(code2, `${name} must be an object`, { value });
|
|
22
33
|
}
|
|
23
34
|
}
|
|
24
35
|
|
|
@@ -75,6 +86,158 @@ function parseCaip2(caip2) {
|
|
|
75
86
|
}
|
|
76
87
|
return { namespace, reference };
|
|
77
88
|
}
|
|
89
|
+
var VALID_KTY = /* @__PURE__ */ new Set(["EC", "OKP", "RSA"]);
|
|
90
|
+
var PRIVATE_KEY_FIELDS = /* @__PURE__ */ new Set(["d", "p", "q", "dp", "dq", "qi", "oth"]);
|
|
91
|
+
var REQUIRED_PUBLIC_FIELDS = {
|
|
92
|
+
EC: ["crv", "x", "y"],
|
|
93
|
+
OKP: ["crv", "x"],
|
|
94
|
+
RSA: ["n", "e"]
|
|
95
|
+
};
|
|
96
|
+
function validatePublicJwk(jwk) {
|
|
97
|
+
if (!jwk || typeof jwk !== "object" || Array.isArray(jwk)) {
|
|
98
|
+
return { valid: false, error: "JWK must be a non-null object" };
|
|
99
|
+
}
|
|
100
|
+
const obj = jwk;
|
|
101
|
+
const kty = obj.kty;
|
|
102
|
+
if (typeof kty !== "string" || !VALID_KTY.has(kty)) {
|
|
103
|
+
return {
|
|
104
|
+
valid: false,
|
|
105
|
+
error: `Invalid or missing kty (must be one of: ${[...VALID_KTY].join(", ")})`
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
for (const field of PRIVATE_KEY_FIELDS) {
|
|
109
|
+
if (field in obj) {
|
|
110
|
+
return {
|
|
111
|
+
valid: false,
|
|
112
|
+
error: `JWK contains private key field "${field}" \u2014 only public keys are allowed`
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
const required = REQUIRED_PUBLIC_FIELDS[kty];
|
|
117
|
+
if (required) {
|
|
118
|
+
for (const field of required) {
|
|
119
|
+
if (!(field in obj) || obj[field] === void 0 || obj[field] === null || obj[field] === "") {
|
|
120
|
+
return {
|
|
121
|
+
valid: false,
|
|
122
|
+
error: `Missing required public key field "${field}" for kty="${kty}"`
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
return { valid: true };
|
|
128
|
+
}
|
|
129
|
+
function canonicalizeJwkJson(jwk) {
|
|
130
|
+
const sorted = Object.keys(jwk).sort();
|
|
131
|
+
const obj = {};
|
|
132
|
+
for (const key of sorted) {
|
|
133
|
+
obj[key] = jwk[key];
|
|
134
|
+
}
|
|
135
|
+
return JSON.stringify(obj);
|
|
136
|
+
}
|
|
137
|
+
function jwkToDidJwk(jwk) {
|
|
138
|
+
assertObject(jwk, "jwk", "INVALID_JWK");
|
|
139
|
+
const validation = validatePublicJwk(jwk);
|
|
140
|
+
if (!validation.valid) {
|
|
141
|
+
throw new OmaTrustError("INVALID_JWK", validation.error ?? "Invalid public JWK", { jwk });
|
|
142
|
+
}
|
|
143
|
+
const canonical = canonicalizeJwkJson(jwk);
|
|
144
|
+
const encoded = base64url.encode(new TextEncoder().encode(canonical));
|
|
145
|
+
return `did:jwk:${encoded}`;
|
|
146
|
+
}
|
|
147
|
+
function didJwkToJwk(didJwk) {
|
|
148
|
+
if (typeof didJwk !== "string" || !didJwk.startsWith("did:jwk:")) {
|
|
149
|
+
throw new OmaTrustError("INVALID_DID", "Expected a did:jwk DID", { input: didJwk });
|
|
150
|
+
}
|
|
151
|
+
const parts = didJwk.split(":");
|
|
152
|
+
if (parts.length !== 3) {
|
|
153
|
+
throw new OmaTrustError("INVALID_DID", "did:jwk must have exactly 3 colon-separated parts", {
|
|
154
|
+
input: didJwk
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
const encoded = parts[2];
|
|
158
|
+
if (!encoded || encoded.length === 0) {
|
|
159
|
+
throw new OmaTrustError("INVALID_DID", "Missing base64url-encoded JWK identifier", {
|
|
160
|
+
input: didJwk
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
let decoded;
|
|
164
|
+
try {
|
|
165
|
+
const bytes = base64url.decode(encoded);
|
|
166
|
+
decoded = new TextDecoder().decode(bytes);
|
|
167
|
+
} catch {
|
|
168
|
+
throw new OmaTrustError("INVALID_DID", "Failed to base64url-decode did:jwk identifier", {
|
|
169
|
+
input: didJwk
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
let jwk;
|
|
173
|
+
try {
|
|
174
|
+
jwk = JSON.parse(decoded);
|
|
175
|
+
} catch {
|
|
176
|
+
throw new OmaTrustError("INVALID_DID", "Decoded did:jwk identifier is not valid JSON", {
|
|
177
|
+
input: didJwk
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
if (!jwk || typeof jwk !== "object" || Array.isArray(jwk)) {
|
|
181
|
+
throw new OmaTrustError("INVALID_DID", "Decoded did:jwk must be a JSON object", {
|
|
182
|
+
input: didJwk
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
const validation = validatePublicJwk(jwk);
|
|
186
|
+
if (!validation.valid) {
|
|
187
|
+
throw new OmaTrustError("INVALID_DID", validation.error ?? "Invalid public JWK in did:jwk", {
|
|
188
|
+
input: didJwk
|
|
189
|
+
});
|
|
190
|
+
}
|
|
191
|
+
return jwk;
|
|
192
|
+
}
|
|
193
|
+
function extractPublicKeyFields(jwk) {
|
|
194
|
+
const result = {};
|
|
195
|
+
const metadataFields = /* @__PURE__ */ new Set(["kid", "use", "key_ops", "alg", "ext"]);
|
|
196
|
+
for (const [key, value] of Object.entries(jwk)) {
|
|
197
|
+
if (PRIVATE_KEY_FIELDS.has(key)) continue;
|
|
198
|
+
if (metadataFields.has(key)) continue;
|
|
199
|
+
result[key] = value;
|
|
200
|
+
}
|
|
201
|
+
return result;
|
|
202
|
+
}
|
|
203
|
+
function publicJwkEquals(a, b) {
|
|
204
|
+
assertObject(a, "a", "INVALID_JWK");
|
|
205
|
+
assertObject(b, "b", "INVALID_JWK");
|
|
206
|
+
const aObj = a;
|
|
207
|
+
const bObj = b;
|
|
208
|
+
for (const field of PRIVATE_KEY_FIELDS) {
|
|
209
|
+
if (field in aObj) {
|
|
210
|
+
throw new OmaTrustError(
|
|
211
|
+
"INVALID_JWK",
|
|
212
|
+
`First JWK contains private key field "${field}"`,
|
|
213
|
+
{ field }
|
|
214
|
+
);
|
|
215
|
+
}
|
|
216
|
+
if (field in bObj) {
|
|
217
|
+
throw new OmaTrustError(
|
|
218
|
+
"INVALID_JWK",
|
|
219
|
+
`Second JWK contains private key field "${field}"`,
|
|
220
|
+
{ field }
|
|
221
|
+
);
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
const aPublic = extractPublicKeyFields(aObj);
|
|
225
|
+
const bPublic = extractPublicKeyFields(bObj);
|
|
226
|
+
return canonicalizeJwkJson(aPublic) === canonicalizeJwkJson(bPublic);
|
|
227
|
+
}
|
|
228
|
+
async function computeJwkThumbprint(jwk, digestAlgorithm = "sha256") {
|
|
229
|
+
assertObject(jwk, "jwk", "INVALID_JWK");
|
|
230
|
+
const validation = validatePublicJwk(jwk);
|
|
231
|
+
if (!validation.valid) {
|
|
232
|
+
throw new OmaTrustError("INVALID_JWK", validation.error ?? "Invalid public JWK", { jwk });
|
|
233
|
+
}
|
|
234
|
+
const alg = digestAlgorithm === "sha256" ? "sha256" : digestAlgorithm === "sha384" ? "sha384" : "sha512";
|
|
235
|
+
return calculateJwkThumbprint(jwk, alg);
|
|
236
|
+
}
|
|
237
|
+
async function formatJktValue(jwk) {
|
|
238
|
+
const thumbprint = await computeJwkThumbprint(jwk, "sha256");
|
|
239
|
+
return `jkt=S256:${thumbprint}`;
|
|
240
|
+
}
|
|
78
241
|
|
|
79
242
|
// src/identity/did.ts
|
|
80
243
|
var DID_REGEX = /^did:[a-z0-9]+:.+$/i;
|
|
@@ -150,7 +313,7 @@ function normalizeDidKey(input) {
|
|
|
150
313
|
}
|
|
151
314
|
function normalizeDid(input) {
|
|
152
315
|
assertString(input, "input", "INVALID_DID");
|
|
153
|
-
const trimmed = input.trim();
|
|
316
|
+
const trimmed = input.trim().split("#")[0];
|
|
154
317
|
if (!trimmed.startsWith("did:")) {
|
|
155
318
|
return normalizeDidWeb(trimmed);
|
|
156
319
|
}
|
|
@@ -167,6 +330,8 @@ function normalizeDid(input) {
|
|
|
167
330
|
return normalizeDidHandle(trimmed);
|
|
168
331
|
case "key":
|
|
169
332
|
return normalizeDidKey(trimmed);
|
|
333
|
+
case "jwk":
|
|
334
|
+
return normalizeDidJwk(trimmed);
|
|
170
335
|
default:
|
|
171
336
|
return trimmed;
|
|
172
337
|
}
|
|
@@ -270,26 +435,838 @@ function extractAddressFromDid(identifier) {
|
|
|
270
435
|
}
|
|
271
436
|
throw new OmaTrustError("INVALID_DID", "Unsupported identifier format", { identifier });
|
|
272
437
|
}
|
|
438
|
+
var CAIP2_NAMESPACE_REGEX = /^[a-z0-9-]{3,8}$/;
|
|
439
|
+
var BASE64URL_REGEX = /^[A-Za-z0-9_-]+$/;
|
|
440
|
+
var VALID_JWK_KTY = /* @__PURE__ */ new Set(["EC", "OKP", "RSA"]);
|
|
441
|
+
function isPrivateKeyDid(did) {
|
|
442
|
+
return validatePrivateKeyDid(did).valid;
|
|
443
|
+
}
|
|
444
|
+
function validatePrivateKeyDid(did) {
|
|
445
|
+
if (typeof did !== "string" || did.trim().length === 0) {
|
|
446
|
+
return { valid: false, method: null, error: "DID must be a non-empty string" };
|
|
447
|
+
}
|
|
448
|
+
const trimmed = did.trim();
|
|
449
|
+
const method = extractDidMethod(trimmed);
|
|
450
|
+
switch (method) {
|
|
451
|
+
case "pkh":
|
|
452
|
+
return validateDidPkh(trimmed);
|
|
453
|
+
case "jwk":
|
|
454
|
+
return validateDidJwk(trimmed);
|
|
455
|
+
default:
|
|
456
|
+
return {
|
|
457
|
+
valid: false,
|
|
458
|
+
method: null,
|
|
459
|
+
error: method ? `DID method "${method}" is not a recognized private-key method` : "Invalid DID format"
|
|
460
|
+
};
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
function validateDidPkh(did) {
|
|
464
|
+
const parts = did.split(":");
|
|
465
|
+
if (parts.length !== 5) {
|
|
466
|
+
return {
|
|
467
|
+
valid: false,
|
|
468
|
+
method: "pkh",
|
|
469
|
+
error: `did:pkh must have exactly 5 colon-separated parts, got ${parts.length}`
|
|
470
|
+
};
|
|
471
|
+
}
|
|
472
|
+
const [, , namespace, chainId, address] = parts;
|
|
473
|
+
if (!namespace) {
|
|
474
|
+
return { valid: false, method: "pkh", error: "Missing namespace" };
|
|
475
|
+
}
|
|
476
|
+
if (!CAIP2_NAMESPACE_REGEX.test(namespace)) {
|
|
477
|
+
return {
|
|
478
|
+
valid: false,
|
|
479
|
+
method: "pkh",
|
|
480
|
+
error: `Invalid CAIP-2 namespace "${namespace}" (must be 3-8 lowercase alphanumeric/hyphen chars)`
|
|
481
|
+
};
|
|
482
|
+
}
|
|
483
|
+
if (!chainId) {
|
|
484
|
+
return { valid: false, method: "pkh", error: "Missing chain ID (reference)" };
|
|
485
|
+
}
|
|
486
|
+
if (!address) {
|
|
487
|
+
return { valid: false, method: "pkh", error: "Missing address" };
|
|
488
|
+
}
|
|
489
|
+
if (namespace === "eip155") {
|
|
490
|
+
if (!/^\d+$/.test(chainId)) {
|
|
491
|
+
return {
|
|
492
|
+
valid: false,
|
|
493
|
+
method: "pkh",
|
|
494
|
+
error: `Invalid eip155 chain ID "${chainId}" (must be numeric)`
|
|
495
|
+
};
|
|
496
|
+
}
|
|
497
|
+
if (!isAddress(address)) {
|
|
498
|
+
return {
|
|
499
|
+
valid: false,
|
|
500
|
+
method: "pkh",
|
|
501
|
+
error: `Invalid EVM address "${address}" (must be 0x + 40 hex chars)`
|
|
502
|
+
};
|
|
503
|
+
}
|
|
504
|
+
}
|
|
505
|
+
if (namespace === "solana") {
|
|
506
|
+
if (!/^[1-9A-HJ-NP-Za-km-z]{32,44}$/.test(address)) {
|
|
507
|
+
return {
|
|
508
|
+
valid: false,
|
|
509
|
+
method: "pkh",
|
|
510
|
+
error: `Invalid Solana address "${address}" (must be 32-44 base58 characters)`
|
|
511
|
+
};
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
return { valid: true, method: "pkh" };
|
|
515
|
+
}
|
|
516
|
+
function validateDidJwk(did) {
|
|
517
|
+
const parts = did.split(":");
|
|
518
|
+
if (parts.length !== 3) {
|
|
519
|
+
return {
|
|
520
|
+
valid: false,
|
|
521
|
+
method: "jwk",
|
|
522
|
+
error: `did:jwk must have exactly 3 colon-separated parts, got ${parts.length}`
|
|
523
|
+
};
|
|
524
|
+
}
|
|
525
|
+
const [, , encoded] = parts;
|
|
526
|
+
if (!encoded || encoded.length === 0) {
|
|
527
|
+
return { valid: false, method: "jwk", error: "Missing base64url-encoded JWK identifier" };
|
|
528
|
+
}
|
|
529
|
+
if (!BASE64URL_REGEX.test(encoded)) {
|
|
530
|
+
return {
|
|
531
|
+
valid: false,
|
|
532
|
+
method: "jwk",
|
|
533
|
+
error: "Identifier contains invalid base64url characters"
|
|
534
|
+
};
|
|
535
|
+
}
|
|
536
|
+
let decoded;
|
|
537
|
+
try {
|
|
538
|
+
const bytes = base64url.decode(encoded);
|
|
539
|
+
decoded = new TextDecoder().decode(bytes);
|
|
540
|
+
} catch {
|
|
541
|
+
return { valid: false, method: "jwk", error: "Failed to base64url-decode identifier" };
|
|
542
|
+
}
|
|
543
|
+
let jwk;
|
|
544
|
+
try {
|
|
545
|
+
jwk = JSON.parse(decoded);
|
|
546
|
+
} catch {
|
|
547
|
+
return { valid: false, method: "jwk", error: "Decoded identifier is not valid JSON" };
|
|
548
|
+
}
|
|
549
|
+
if (!jwk || typeof jwk !== "object" || Array.isArray(jwk)) {
|
|
550
|
+
return { valid: false, method: "jwk", error: "Decoded JWK must be a JSON object" };
|
|
551
|
+
}
|
|
552
|
+
const kty = jwk.kty;
|
|
553
|
+
if (typeof kty !== "string" || !VALID_JWK_KTY.has(kty)) {
|
|
554
|
+
return {
|
|
555
|
+
valid: false,
|
|
556
|
+
method: "jwk",
|
|
557
|
+
error: `Invalid or missing kty field (must be one of: EC, OKP, RSA)`
|
|
558
|
+
};
|
|
559
|
+
}
|
|
560
|
+
if ("d" in jwk) {
|
|
561
|
+
return {
|
|
562
|
+
valid: false,
|
|
563
|
+
method: "jwk",
|
|
564
|
+
error: "DID must reference a public key \u2014 private key component (d) is not allowed"
|
|
565
|
+
};
|
|
566
|
+
}
|
|
567
|
+
const jwkValidation = validatePublicJwk(jwk);
|
|
568
|
+
if (!jwkValidation.valid) {
|
|
569
|
+
return { valid: false, method: "jwk", error: jwkValidation.error };
|
|
570
|
+
}
|
|
571
|
+
return { valid: true, method: "jwk" };
|
|
572
|
+
}
|
|
573
|
+
function normalizeDidJwk(input) {
|
|
574
|
+
assertString(input, "input", "INVALID_DID");
|
|
575
|
+
const trimmed = input.trim();
|
|
576
|
+
if (!trimmed.startsWith("did:jwk:")) {
|
|
577
|
+
throw new OmaTrustError("INVALID_DID", "Expected did:jwk DID", { input });
|
|
578
|
+
}
|
|
579
|
+
const result = validateDidJwk(trimmed);
|
|
580
|
+
if (!result.valid) {
|
|
581
|
+
throw new OmaTrustError("INVALID_DID", result.error ?? "Invalid did:jwk", { input });
|
|
582
|
+
}
|
|
583
|
+
return trimmed;
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
// src/identity/did-url.ts
|
|
587
|
+
var DID_URL_REGEX = /^did:[a-z0-9]+:.+$/i;
|
|
588
|
+
function parseDidUrl(input) {
|
|
589
|
+
assertString(input, "input", "INVALID_DID_URL");
|
|
590
|
+
const trimmed = input.trim();
|
|
591
|
+
const hashIndex = trimmed.indexOf("#");
|
|
592
|
+
let did;
|
|
593
|
+
let fragment;
|
|
594
|
+
if (hashIndex === -1) {
|
|
595
|
+
did = trimmed;
|
|
596
|
+
fragment = null;
|
|
597
|
+
} else {
|
|
598
|
+
did = trimmed.slice(0, hashIndex);
|
|
599
|
+
const rawFragment = trimmed.slice(hashIndex + 1);
|
|
600
|
+
if (rawFragment.length === 0) {
|
|
601
|
+
throw new OmaTrustError(
|
|
602
|
+
"INVALID_DID_URL",
|
|
603
|
+
"DID URL has empty fragment (trailing '#' with no identifier)",
|
|
604
|
+
{ input }
|
|
605
|
+
);
|
|
606
|
+
}
|
|
607
|
+
fragment = rawFragment;
|
|
608
|
+
}
|
|
609
|
+
if (!DID_URL_REGEX.test(did)) {
|
|
610
|
+
throw new OmaTrustError(
|
|
611
|
+
"INVALID_DID_URL",
|
|
612
|
+
"Invalid DID URL: base DID portion is malformed",
|
|
613
|
+
{ input, did }
|
|
614
|
+
);
|
|
615
|
+
}
|
|
616
|
+
return {
|
|
617
|
+
didUrl: trimmed,
|
|
618
|
+
did,
|
|
619
|
+
fragment
|
|
620
|
+
};
|
|
621
|
+
}
|
|
622
|
+
function isDidUrl(input) {
|
|
623
|
+
if (typeof input !== "string") return false;
|
|
624
|
+
const trimmed = input.trim();
|
|
625
|
+
return trimmed.includes("#") && DID_URL_REGEX.test(trimmed.split("#")[0]);
|
|
626
|
+
}
|
|
627
|
+
function assertBareDid(input, paramName = "did") {
|
|
628
|
+
assertString(input, paramName, "INVALID_DID");
|
|
629
|
+
if (isDidUrl(input)) {
|
|
630
|
+
throw new OmaTrustError(
|
|
631
|
+
"INVALID_DID",
|
|
632
|
+
`Expected a bare DID but received a DID URL with a fragment. Use parseDidUrl() to handle DID URLs.`,
|
|
633
|
+
{ input }
|
|
634
|
+
);
|
|
635
|
+
}
|
|
636
|
+
}
|
|
637
|
+
|
|
638
|
+
// src/shared/did-document.ts
|
|
639
|
+
async function fetchDidDocument(domain) {
|
|
640
|
+
const normalized = domain.toLowerCase().replace(/\.$/, "");
|
|
641
|
+
const url = `https://${normalized}/.well-known/did.json`;
|
|
642
|
+
let response;
|
|
643
|
+
try {
|
|
644
|
+
response = await fetch(url, { headers: { Accept: "application/json" } });
|
|
645
|
+
} catch (err) {
|
|
646
|
+
throw new OmaTrustError("NETWORK_ERROR", "Failed to fetch DID document", { domain, err });
|
|
647
|
+
}
|
|
648
|
+
if (!response.ok) {
|
|
649
|
+
throw new OmaTrustError("NETWORK_ERROR", `DID document fetch failed: ${response.status}`, {
|
|
650
|
+
domain,
|
|
651
|
+
status: response.status
|
|
652
|
+
});
|
|
653
|
+
}
|
|
654
|
+
return await response.json();
|
|
655
|
+
}
|
|
656
|
+
|
|
657
|
+
// src/identity/resolve-key.ts
|
|
658
|
+
function findVerificationMethod(didDocument, didUrl, fragment) {
|
|
659
|
+
const methods = didDocument.verificationMethod;
|
|
660
|
+
if (!Array.isArray(methods)) {
|
|
661
|
+
return null;
|
|
662
|
+
}
|
|
663
|
+
for (const method of methods) {
|
|
664
|
+
if (!method || typeof method !== "object") continue;
|
|
665
|
+
const id = method.id;
|
|
666
|
+
if (typeof id !== "string") continue;
|
|
667
|
+
if (id === didUrl) return method;
|
|
668
|
+
if (fragment && (id === `#${fragment}` || id.endsWith(`#${fragment}`))) {
|
|
669
|
+
return method;
|
|
670
|
+
}
|
|
671
|
+
}
|
|
672
|
+
return null;
|
|
673
|
+
}
|
|
674
|
+
function extractMethodIdsFromDidDocument(didDocument) {
|
|
675
|
+
const methods = didDocument.verificationMethod;
|
|
676
|
+
if (!Array.isArray(methods)) return [];
|
|
677
|
+
return methods.filter((m) => m && typeof m.id === "string").map((m) => m.id);
|
|
678
|
+
}
|
|
679
|
+
async function resolveDidUrlToPublicKey(didUrl, options) {
|
|
680
|
+
assertString(didUrl, "didUrl", "INVALID_DID_URL");
|
|
681
|
+
const parsed = parseDidUrl(didUrl);
|
|
682
|
+
const method = extractDidMethod(parsed.did);
|
|
683
|
+
if (!method) {
|
|
684
|
+
throw new OmaTrustError("INVALID_DID_URL", "Cannot extract DID method from DID URL", {
|
|
685
|
+
didUrl,
|
|
686
|
+
did: parsed.did
|
|
687
|
+
});
|
|
688
|
+
}
|
|
689
|
+
switch (method) {
|
|
690
|
+
case "web":
|
|
691
|
+
return resolveDidUrlKey(parsed.didUrl, parsed.did, parsed.fragment, options);
|
|
692
|
+
default:
|
|
693
|
+
throw new OmaTrustError(
|
|
694
|
+
"UNSUPPORTED_DID_METHOD",
|
|
695
|
+
`DID URL key resolution is not supported for method "${method}"`,
|
|
696
|
+
{ didUrl, method }
|
|
697
|
+
);
|
|
698
|
+
}
|
|
699
|
+
}
|
|
700
|
+
async function resolveDidUrlToControllerDid(didUrl, options) {
|
|
701
|
+
const resolved = await resolveDidUrlToPublicKey(didUrl, options);
|
|
702
|
+
const controllerDid = jwkToDidJwk(resolved.publicKeyJwk);
|
|
703
|
+
return {
|
|
704
|
+
...resolved,
|
|
705
|
+
controllerDid
|
|
706
|
+
};
|
|
707
|
+
}
|
|
708
|
+
async function resolveDidUrlKey(didUrl, did, fragment, options) {
|
|
709
|
+
const domain = getDomainFromDidWeb(did);
|
|
710
|
+
if (!domain) {
|
|
711
|
+
throw new OmaTrustError("INVALID_DID_URL", "Cannot extract domain from did:web DID", {
|
|
712
|
+
didUrl,
|
|
713
|
+
did
|
|
714
|
+
});
|
|
715
|
+
}
|
|
716
|
+
const fetchFn = options?.fetchDidDocument ?? fetchDidDocument;
|
|
717
|
+
const didDocument = await fetchFn(domain);
|
|
718
|
+
const method = findVerificationMethod(didDocument, didUrl, fragment);
|
|
719
|
+
if (!method) {
|
|
720
|
+
throw new OmaTrustError(
|
|
721
|
+
"KEY_NOT_FOUND",
|
|
722
|
+
`No verification method found matching "${fragment ? `#${fragment}` : didUrl}"`,
|
|
723
|
+
{ didUrl, fragment, availableMethods: extractMethodIdsFromDidDocument(didDocument) }
|
|
724
|
+
);
|
|
725
|
+
}
|
|
726
|
+
const publicKeyJwk = method.publicKeyJwk;
|
|
727
|
+
if (!publicKeyJwk || typeof publicKeyJwk !== "object") {
|
|
728
|
+
throw new OmaTrustError(
|
|
729
|
+
"KEY_NOT_FOUND",
|
|
730
|
+
`Verification method "${method.id}" does not contain publicKeyJwk`,
|
|
731
|
+
{ didUrl, verificationMethodId: method.id }
|
|
732
|
+
);
|
|
733
|
+
}
|
|
734
|
+
const validation = validatePublicJwk(publicKeyJwk);
|
|
735
|
+
if (!validation.valid) {
|
|
736
|
+
throw new OmaTrustError(
|
|
737
|
+
"INVALID_JWK",
|
|
738
|
+
`publicKeyJwk in verification method "${method.id}" is invalid: ${validation.error}`,
|
|
739
|
+
{ didUrl, verificationMethodId: method.id }
|
|
740
|
+
);
|
|
741
|
+
}
|
|
742
|
+
return {
|
|
743
|
+
didUrl,
|
|
744
|
+
did,
|
|
745
|
+
fragment,
|
|
746
|
+
publicKeyJwk,
|
|
747
|
+
verificationMethodId: method.id
|
|
748
|
+
};
|
|
749
|
+
}
|
|
750
|
+
|
|
751
|
+
// src/identity/controller-id.ts
|
|
752
|
+
function isSameControllerId(a, b) {
|
|
753
|
+
let normalizedA = null;
|
|
754
|
+
let normalizedB = null;
|
|
755
|
+
try {
|
|
756
|
+
normalizedA = normalizeDid(a);
|
|
757
|
+
} catch {
|
|
758
|
+
}
|
|
759
|
+
try {
|
|
760
|
+
normalizedB = normalizeDid(b);
|
|
761
|
+
} catch {
|
|
762
|
+
}
|
|
763
|
+
if (normalizedA && normalizedB && normalizedA === normalizedB) {
|
|
764
|
+
return true;
|
|
765
|
+
}
|
|
766
|
+
const evmA = extractControllerEvmAddress(a);
|
|
767
|
+
const evmB = extractControllerEvmAddress(b);
|
|
768
|
+
if (evmA && evmB && evmA.toLowerCase() === evmB.toLowerCase()) {
|
|
769
|
+
return true;
|
|
770
|
+
}
|
|
771
|
+
const methodA = extractDidMethod(a);
|
|
772
|
+
const methodB = extractDidMethod(b);
|
|
773
|
+
if (methodA === "jwk" && methodB === "jwk") {
|
|
774
|
+
try {
|
|
775
|
+
const jwkA = didJwkToJwk(a);
|
|
776
|
+
const jwkB = didJwkToJwk(b);
|
|
777
|
+
return publicJwkEquals(jwkA, jwkB);
|
|
778
|
+
} catch {
|
|
779
|
+
return false;
|
|
780
|
+
}
|
|
781
|
+
}
|
|
782
|
+
return false;
|
|
783
|
+
}
|
|
784
|
+
function extractControllerEvmAddress(controllerDid) {
|
|
785
|
+
try {
|
|
786
|
+
const method = extractDidMethod(controllerDid);
|
|
787
|
+
if (method === "pkh" && controllerDid.includes("eip155")) {
|
|
788
|
+
return extractAddressFromDid(controllerDid);
|
|
789
|
+
}
|
|
790
|
+
} catch {
|
|
791
|
+
}
|
|
792
|
+
return null;
|
|
793
|
+
}
|
|
794
|
+
|
|
795
|
+
// src/identity/types.ts
|
|
796
|
+
function extractAuthorizationMetadata(result) {
|
|
797
|
+
const payload = result.payload;
|
|
798
|
+
const resourceUrl = typeof payload.resourceUrl === "string" ? payload.resourceUrl : null;
|
|
799
|
+
const issuedAt = typeof payload.issuedAt === "string" ? payload.issuedAt : typeof payload.issuedAt === "number" ? payload.issuedAt : null;
|
|
800
|
+
let subjectDid = null;
|
|
801
|
+
if (resourceUrl) {
|
|
802
|
+
try {
|
|
803
|
+
const url = new URL(resourceUrl);
|
|
804
|
+
subjectDid = `did:web:${url.hostname}`;
|
|
805
|
+
} catch {
|
|
806
|
+
}
|
|
807
|
+
}
|
|
808
|
+
if ("publicKeyDid" in result) {
|
|
809
|
+
const jwsResult = result;
|
|
810
|
+
return {
|
|
811
|
+
controllerDid: jwsResult.publicKeyDid,
|
|
812
|
+
subjectDid,
|
|
813
|
+
resourceUrl,
|
|
814
|
+
issuedAt,
|
|
815
|
+
kid: jwsResult.kid,
|
|
816
|
+
publicKeyJwk: jwsResult.publicKeyJwk,
|
|
817
|
+
signer: null
|
|
818
|
+
};
|
|
819
|
+
}
|
|
820
|
+
const eip712Result = result;
|
|
821
|
+
const signerAddress = eip712Result.signer.toLowerCase();
|
|
822
|
+
return {
|
|
823
|
+
controllerDid: `did:pkh:eip155:1:${signerAddress}`,
|
|
824
|
+
subjectDid,
|
|
825
|
+
resourceUrl,
|
|
826
|
+
issuedAt,
|
|
827
|
+
kid: null,
|
|
828
|
+
publicKeyJwk: null,
|
|
829
|
+
signer: eip712Result.signer
|
|
830
|
+
};
|
|
831
|
+
}
|
|
832
|
+
var VALID_ALGORITHMS = /* @__PURE__ */ new Set(["keccak256", "sha256"]);
|
|
833
|
+
var MAX_JSON_DEPTH = 32;
|
|
834
|
+
function assertDepthLimit(depth, context) {
|
|
835
|
+
if (depth > MAX_JSON_DEPTH) {
|
|
836
|
+
const msg = context ? `JSON nesting depth exceeds maximum of ${MAX_JSON_DEPTH} at ${context}` : `JSON nesting depth exceeds maximum of ${MAX_JSON_DEPTH}`;
|
|
837
|
+
throw new OmaTrustError("INVALID_INPUT", msg);
|
|
838
|
+
}
|
|
839
|
+
}
|
|
840
|
+
function parseJsonStrict(input) {
|
|
841
|
+
if (typeof input !== "string") {
|
|
842
|
+
throw new OmaTrustError("INVALID_INPUT", "Input must be a string");
|
|
843
|
+
}
|
|
844
|
+
const duplicateKeys = detectDuplicateKeys(input);
|
|
845
|
+
if (duplicateKeys.length > 0) {
|
|
846
|
+
throw new OmaTrustError(
|
|
847
|
+
"INVALID_INPUT",
|
|
848
|
+
`Duplicate JSON key${duplicateKeys.length > 1 ? "s" : ""} detected: ${duplicateKeys.map((k) => `"${k}"`).join(", ")}`,
|
|
849
|
+
{ duplicateKeys }
|
|
850
|
+
);
|
|
851
|
+
}
|
|
852
|
+
try {
|
|
853
|
+
return JSON.parse(input);
|
|
854
|
+
} catch {
|
|
855
|
+
throw new OmaTrustError("INVALID_INPUT", "Input is not valid JSON");
|
|
856
|
+
}
|
|
857
|
+
}
|
|
858
|
+
function detectDuplicateKeys(input) {
|
|
859
|
+
const duplicates = [];
|
|
860
|
+
const objectKeyStack = [];
|
|
861
|
+
let inString = false;
|
|
862
|
+
let escaped = false;
|
|
863
|
+
let currentKey = "";
|
|
864
|
+
let collectingKey = false;
|
|
865
|
+
let afterColon = false;
|
|
866
|
+
let i = 0;
|
|
867
|
+
while (i < input.length) {
|
|
868
|
+
const ch = input[i];
|
|
869
|
+
if (escaped) {
|
|
870
|
+
if (collectingKey) currentKey += ch;
|
|
871
|
+
escaped = false;
|
|
872
|
+
i++;
|
|
873
|
+
continue;
|
|
874
|
+
}
|
|
875
|
+
if (ch === "\\") {
|
|
876
|
+
escaped = true;
|
|
877
|
+
if (collectingKey) currentKey += ch;
|
|
878
|
+
i++;
|
|
879
|
+
continue;
|
|
880
|
+
}
|
|
881
|
+
if (ch === '"') {
|
|
882
|
+
if (!inString) {
|
|
883
|
+
inString = true;
|
|
884
|
+
if (objectKeyStack.length > 0 && !afterColon) {
|
|
885
|
+
collectingKey = true;
|
|
886
|
+
currentKey = "";
|
|
887
|
+
}
|
|
888
|
+
} else {
|
|
889
|
+
inString = false;
|
|
890
|
+
if (collectingKey) {
|
|
891
|
+
collectingKey = false;
|
|
892
|
+
const keySet = objectKeyStack[objectKeyStack.length - 1];
|
|
893
|
+
if (keySet.has(currentKey)) {
|
|
894
|
+
if (!duplicates.includes(currentKey)) {
|
|
895
|
+
duplicates.push(currentKey);
|
|
896
|
+
}
|
|
897
|
+
} else {
|
|
898
|
+
keySet.add(currentKey);
|
|
899
|
+
}
|
|
900
|
+
}
|
|
901
|
+
}
|
|
902
|
+
i++;
|
|
903
|
+
continue;
|
|
904
|
+
}
|
|
905
|
+
if (inString) {
|
|
906
|
+
if (collectingKey) currentKey += ch;
|
|
907
|
+
i++;
|
|
908
|
+
continue;
|
|
909
|
+
}
|
|
910
|
+
if (ch === "{") {
|
|
911
|
+
objectKeyStack.push(/* @__PURE__ */ new Set());
|
|
912
|
+
assertDepthLimit(objectKeyStack.length);
|
|
913
|
+
afterColon = false;
|
|
914
|
+
} else if (ch === "}") {
|
|
915
|
+
objectKeyStack.pop();
|
|
916
|
+
afterColon = objectKeyStack.length > 0;
|
|
917
|
+
} else if (ch === "[") {
|
|
918
|
+
objectKeyStack.push(/* @__PURE__ */ new Set(["__array__"]));
|
|
919
|
+
assertDepthLimit(objectKeyStack.length);
|
|
920
|
+
afterColon = false;
|
|
921
|
+
} else if (ch === "]") {
|
|
922
|
+
objectKeyStack.pop();
|
|
923
|
+
afterColon = objectKeyStack.length > 0;
|
|
924
|
+
} else if (ch === ":") {
|
|
925
|
+
afterColon = true;
|
|
926
|
+
} else if (ch === ",") {
|
|
927
|
+
afterColon = false;
|
|
928
|
+
}
|
|
929
|
+
i++;
|
|
930
|
+
}
|
|
931
|
+
return duplicates;
|
|
932
|
+
}
|
|
933
|
+
function assertJsonSafe(value, path = "$", depth = 0) {
|
|
934
|
+
assertDepthLimit(depth, path);
|
|
935
|
+
if (value === void 0) {
|
|
936
|
+
throw new OmaTrustError("INVALID_INPUT", `Non-JSON-safe value at ${path}: undefined`);
|
|
937
|
+
}
|
|
938
|
+
if (value === null) return;
|
|
939
|
+
switch (typeof value) {
|
|
940
|
+
case "string":
|
|
941
|
+
case "boolean":
|
|
942
|
+
return;
|
|
943
|
+
case "number":
|
|
944
|
+
if (!Number.isFinite(value)) {
|
|
945
|
+
throw new OmaTrustError("INVALID_INPUT", `Non-JSON-safe value at ${path}: ${value} (must be finite number)`);
|
|
946
|
+
}
|
|
947
|
+
return;
|
|
948
|
+
case "bigint":
|
|
949
|
+
throw new OmaTrustError("INVALID_INPUT", `Non-JSON-safe value at ${path}: BigInt (use number or string)`);
|
|
950
|
+
case "function":
|
|
951
|
+
throw new OmaTrustError("INVALID_INPUT", `Non-JSON-safe value at ${path}: function`);
|
|
952
|
+
case "symbol":
|
|
953
|
+
throw new OmaTrustError("INVALID_INPUT", `Non-JSON-safe value at ${path}: Symbol`);
|
|
954
|
+
case "object":
|
|
955
|
+
if (value instanceof Date) {
|
|
956
|
+
throw new OmaTrustError("INVALID_INPUT", `Non-JSON-safe value at ${path}: Date (use ISO string or timestamp)`);
|
|
957
|
+
}
|
|
958
|
+
if (value instanceof RegExp) {
|
|
959
|
+
throw new OmaTrustError("INVALID_INPUT", `Non-JSON-safe value at ${path}: RegExp`);
|
|
960
|
+
}
|
|
961
|
+
if (Array.isArray(value)) {
|
|
962
|
+
for (let i = 0; i < value.length; i++) {
|
|
963
|
+
assertJsonSafe(value[i], `${path}[${i}]`, depth + 1);
|
|
964
|
+
}
|
|
965
|
+
return;
|
|
966
|
+
}
|
|
967
|
+
for (const [key, val] of Object.entries(value)) {
|
|
968
|
+
assertJsonSafe(val, `${path}.${key}`, depth + 1);
|
|
969
|
+
}
|
|
970
|
+
return;
|
|
971
|
+
default:
|
|
972
|
+
throw new OmaTrustError("INVALID_INPUT", `Non-JSON-safe value at ${path}: unknown type "${typeof value}"`);
|
|
973
|
+
}
|
|
974
|
+
}
|
|
273
975
|
function canonicalizeJson(obj) {
|
|
976
|
+
assertJsonSafe(obj);
|
|
274
977
|
const jcs = canonicalize(obj);
|
|
275
978
|
if (!jcs) {
|
|
276
979
|
throw new OmaTrustError("INVALID_INPUT", "Object cannot be canonicalized", { obj });
|
|
277
980
|
}
|
|
278
981
|
return jcs;
|
|
279
982
|
}
|
|
280
|
-
function
|
|
983
|
+
function canonicalizeAndKeccak256(obj) {
|
|
281
984
|
const jcsJson = canonicalizeJson(obj);
|
|
282
985
|
return {
|
|
283
986
|
jcsJson,
|
|
284
987
|
hash: keccak256(toUtf8Bytes(jcsJson))
|
|
285
988
|
};
|
|
286
989
|
}
|
|
990
|
+
function canonicalizeForHash(obj) {
|
|
991
|
+
return canonicalizeAndKeccak256(obj);
|
|
992
|
+
}
|
|
287
993
|
function hashCanonicalizedJson(obj, algorithm) {
|
|
994
|
+
if (!VALID_ALGORITHMS.has(algorithm)) {
|
|
995
|
+
throw new OmaTrustError("INVALID_INPUT", `Unsupported hash algorithm: "${algorithm}". Must be "keccak256" or "sha256".`, { algorithm });
|
|
996
|
+
}
|
|
288
997
|
const jcs = canonicalizeJson(obj);
|
|
289
998
|
const bytes = toUtf8Bytes(jcs);
|
|
290
999
|
return algorithm === "keccak256" ? keccak256(bytes) : sha256(bytes);
|
|
291
1000
|
}
|
|
1001
|
+
var DID_ARTIFACT_PREFIX = "did:artifact:";
|
|
1002
|
+
var CID_VERSION = 1;
|
|
1003
|
+
var RAW_CODEC = raw.code;
|
|
1004
|
+
var SHA2_256_CODE = 18;
|
|
1005
|
+
var SHA2_256_DIGEST_LENGTH = 32;
|
|
1006
|
+
async function artifactDidFromBytes(bytes) {
|
|
1007
|
+
if (!(bytes instanceof Uint8Array) || bytes.length === 0) {
|
|
1008
|
+
throw new OmaTrustError(
|
|
1009
|
+
"INVALID_INPUT",
|
|
1010
|
+
"bytes must be a non-empty Uint8Array"
|
|
1011
|
+
);
|
|
1012
|
+
}
|
|
1013
|
+
const digest = await sha256$1.digest(bytes);
|
|
1014
|
+
const cid = CID.createV1(RAW_CODEC, digest);
|
|
1015
|
+
return `${DID_ARTIFACT_PREFIX}${cid.toString(base32)}`;
|
|
1016
|
+
}
|
|
1017
|
+
async function artifactDidFromJson(input) {
|
|
1018
|
+
const value = typeof input === "string" ? parseJsonStrict(input) : input;
|
|
1019
|
+
const jcs = canonicalizeJson(value);
|
|
1020
|
+
const bytes = new TextEncoder().encode(jcs);
|
|
1021
|
+
const digest = await sha256$1.digest(bytes);
|
|
1022
|
+
const cid = CID.createV1(RAW_CODEC, digest);
|
|
1023
|
+
return `${DID_ARTIFACT_PREFIX}${cid.toString(base32)}`;
|
|
1024
|
+
}
|
|
1025
|
+
function parseArtifactDid(did) {
|
|
1026
|
+
if (typeof did !== "string" || !did.startsWith(DID_ARTIFACT_PREFIX)) {
|
|
1027
|
+
throw new OmaTrustError("INVALID_DID", "Expected a did:artifact DID", { did });
|
|
1028
|
+
}
|
|
1029
|
+
const identifier = did.slice(DID_ARTIFACT_PREFIX.length);
|
|
1030
|
+
if (!identifier || identifier.length === 0) {
|
|
1031
|
+
throw new OmaTrustError("INVALID_DID", "Missing method-specific identifier", { did });
|
|
1032
|
+
}
|
|
1033
|
+
if (identifier[0] !== "b") {
|
|
1034
|
+
throw new OmaTrustError(
|
|
1035
|
+
"INVALID_DID",
|
|
1036
|
+
`Invalid multibase prefix "${identifier[0]}", expected "b" (base32lower)`,
|
|
1037
|
+
{ did }
|
|
1038
|
+
);
|
|
1039
|
+
}
|
|
1040
|
+
let cid;
|
|
1041
|
+
try {
|
|
1042
|
+
cid = CID.parse(identifier, base32);
|
|
1043
|
+
} catch (err) {
|
|
1044
|
+
throw new OmaTrustError(
|
|
1045
|
+
"INVALID_DID",
|
|
1046
|
+
"Failed to decode base32 CID from did:artifact identifier",
|
|
1047
|
+
{ did, cause: err }
|
|
1048
|
+
);
|
|
1049
|
+
}
|
|
1050
|
+
if (cid.version !== CID_VERSION) {
|
|
1051
|
+
throw new OmaTrustError(
|
|
1052
|
+
"INVALID_DID",
|
|
1053
|
+
`CID version must be 1, got ${cid.version}`,
|
|
1054
|
+
{ did }
|
|
1055
|
+
);
|
|
1056
|
+
}
|
|
1057
|
+
if (cid.code !== RAW_CODEC) {
|
|
1058
|
+
throw new OmaTrustError(
|
|
1059
|
+
"INVALID_DID",
|
|
1060
|
+
`Multicodec must be raw (0x55), got 0x${cid.code.toString(16)}`,
|
|
1061
|
+
{ did }
|
|
1062
|
+
);
|
|
1063
|
+
}
|
|
1064
|
+
if (cid.multihash.code !== SHA2_256_CODE) {
|
|
1065
|
+
throw new OmaTrustError(
|
|
1066
|
+
"INVALID_DID",
|
|
1067
|
+
`Multihash function must be sha2-256 (0x12), got 0x${cid.multihash.code.toString(16)}`,
|
|
1068
|
+
{ did }
|
|
1069
|
+
);
|
|
1070
|
+
}
|
|
1071
|
+
if (cid.multihash.digest.length !== SHA2_256_DIGEST_LENGTH) {
|
|
1072
|
+
throw new OmaTrustError(
|
|
1073
|
+
"INVALID_DID",
|
|
1074
|
+
`SHA-256 digest must be 32 bytes, got ${cid.multihash.digest.length}`,
|
|
1075
|
+
{ did }
|
|
1076
|
+
);
|
|
1077
|
+
}
|
|
1078
|
+
const digest = cid.multihash.digest;
|
|
1079
|
+
const digestHex = Array.from(digest).map((b) => b.toString(16).padStart(2, "0")).join("");
|
|
1080
|
+
return { did, identifier, digest, digestHex };
|
|
1081
|
+
}
|
|
1082
|
+
async function verifyDidArtifact(did, content) {
|
|
1083
|
+
let parsed;
|
|
1084
|
+
try {
|
|
1085
|
+
parsed = parseArtifactDid(did);
|
|
1086
|
+
} catch (err) {
|
|
1087
|
+
return {
|
|
1088
|
+
valid: false,
|
|
1089
|
+
reason: err instanceof OmaTrustError ? err.message : "Invalid did:artifact DID"
|
|
1090
|
+
};
|
|
1091
|
+
}
|
|
1092
|
+
const expectedHex = parsed.digestHex;
|
|
1093
|
+
if (typeof content === "string" || typeof content === "object" && content !== null && !(content instanceof Uint8Array)) {
|
|
1094
|
+
try {
|
|
1095
|
+
const value = typeof content === "string" ? parseJsonStrict(content) : content;
|
|
1096
|
+
const jcs = canonicalizeJson(value);
|
|
1097
|
+
const jcsBytes = new TextEncoder().encode(jcs);
|
|
1098
|
+
const digest2 = await sha256$1.digest(jcsBytes);
|
|
1099
|
+
const digestHex2 = Array.from(digest2.digest).map((b) => b.toString(16).padStart(2, "0")).join("");
|
|
1100
|
+
if (digestHex2 === expectedHex) {
|
|
1101
|
+
return { valid: true, matchedAs: "json" };
|
|
1102
|
+
}
|
|
1103
|
+
} catch {
|
|
1104
|
+
}
|
|
1105
|
+
}
|
|
1106
|
+
let rawBytes;
|
|
1107
|
+
if (content instanceof Uint8Array) {
|
|
1108
|
+
rawBytes = content;
|
|
1109
|
+
} else if (typeof content === "string") {
|
|
1110
|
+
rawBytes = new TextEncoder().encode(content);
|
|
1111
|
+
} else {
|
|
1112
|
+
return {
|
|
1113
|
+
valid: false,
|
|
1114
|
+
reason: "Content did not match as canonical JSON and is not raw bytes"
|
|
1115
|
+
};
|
|
1116
|
+
}
|
|
1117
|
+
const digest = await sha256$1.digest(rawBytes);
|
|
1118
|
+
const digestHex = Array.from(digest.digest).map((b) => b.toString(16).padStart(2, "0")).join("");
|
|
1119
|
+
if (digestHex === expectedHex) {
|
|
1120
|
+
return { valid: true, matchedAs: "binary" };
|
|
1121
|
+
}
|
|
1122
|
+
return {
|
|
1123
|
+
valid: false,
|
|
1124
|
+
reason: "Content does not match the did:artifact identifier (neither as canonical JSON nor as raw bytes)"
|
|
1125
|
+
};
|
|
1126
|
+
}
|
|
1127
|
+
function didEthrToDidPkh(did) {
|
|
1128
|
+
if (typeof did !== "string" || !did.startsWith("did:ethr:")) {
|
|
1129
|
+
throw new OmaTrustError("INVALID_DID", "Expected a did:ethr DID", { did });
|
|
1130
|
+
}
|
|
1131
|
+
const remainder = did.slice("did:ethr:".length);
|
|
1132
|
+
if (!remainder) {
|
|
1133
|
+
throw new OmaTrustError("INVALID_DID", "Missing identifier in did:ethr DID", { did });
|
|
1134
|
+
}
|
|
1135
|
+
let chainId;
|
|
1136
|
+
let address;
|
|
1137
|
+
const parts = remainder.split(":");
|
|
1138
|
+
if (parts.length === 1) {
|
|
1139
|
+
chainId = "1";
|
|
1140
|
+
address = parts[0];
|
|
1141
|
+
} else if (parts.length === 2) {
|
|
1142
|
+
const rawChainId = parts[0];
|
|
1143
|
+
address = parts[1];
|
|
1144
|
+
if (rawChainId.startsWith("0x")) {
|
|
1145
|
+
chainId = String(parseInt(rawChainId, 16));
|
|
1146
|
+
} else if (/^\d+$/.test(rawChainId)) {
|
|
1147
|
+
chainId = rawChainId;
|
|
1148
|
+
} else {
|
|
1149
|
+
const networkMap = {
|
|
1150
|
+
mainnet: "1",
|
|
1151
|
+
goerli: "5",
|
|
1152
|
+
sepolia: "11155111",
|
|
1153
|
+
polygon: "137",
|
|
1154
|
+
arbitrum: "42161",
|
|
1155
|
+
optimism: "10",
|
|
1156
|
+
base: "8453"
|
|
1157
|
+
};
|
|
1158
|
+
const mapped = networkMap[rawChainId.toLowerCase()];
|
|
1159
|
+
if (!mapped) {
|
|
1160
|
+
throw new OmaTrustError(
|
|
1161
|
+
"INVALID_DID",
|
|
1162
|
+
`Unknown did:ethr network "${rawChainId}"`,
|
|
1163
|
+
{ did, network: rawChainId }
|
|
1164
|
+
);
|
|
1165
|
+
}
|
|
1166
|
+
chainId = mapped;
|
|
1167
|
+
}
|
|
1168
|
+
} else {
|
|
1169
|
+
throw new OmaTrustError("INVALID_DID", "Invalid did:ethr format", { did });
|
|
1170
|
+
}
|
|
1171
|
+
if (!isAddress(address)) {
|
|
1172
|
+
throw new OmaTrustError("INVALID_DID", "Invalid Ethereum address in did:ethr DID", {
|
|
1173
|
+
did,
|
|
1174
|
+
address
|
|
1175
|
+
});
|
|
1176
|
+
}
|
|
1177
|
+
const checksummed = getAddress(address);
|
|
1178
|
+
return `did:pkh:eip155:${chainId}:${checksummed.toLowerCase()}`;
|
|
1179
|
+
}
|
|
1180
|
+
var MULTICODEC_KEY_TYPES = {
|
|
1181
|
+
// Ed25519 public key: 0xed (varint encoded as [0xed, 0x01])
|
|
1182
|
+
237: { kty: "OKP", crv: "Ed25519" },
|
|
1183
|
+
// X25519 public key: 0xec (varint encoded as [0xec, 0x01])
|
|
1184
|
+
236: { kty: "OKP", crv: "X25519" },
|
|
1185
|
+
// secp256k1 public key (compressed): 0xe7 (varint encoded as [0xe7, 0x01])
|
|
1186
|
+
231: { kty: "EC", crv: "secp256k1", coordSize: 33 },
|
|
1187
|
+
// P-256 public key (compressed): 0x80 0x24 (varint 0x1200)
|
|
1188
|
+
4608: { kty: "EC", crv: "P-256", coordSize: 33 },
|
|
1189
|
+
// P-384 public key (compressed): 0x81 0x24 (varint 0x1201)
|
|
1190
|
+
4609: { kty: "EC", crv: "P-384", coordSize: 49 }
|
|
1191
|
+
};
|
|
1192
|
+
function readVarint(bytes, offset) {
|
|
1193
|
+
let value = 0;
|
|
1194
|
+
let shift = 0;
|
|
1195
|
+
let bytesRead = 0;
|
|
1196
|
+
while (offset + bytesRead < bytes.length) {
|
|
1197
|
+
const byte = bytes[offset + bytesRead];
|
|
1198
|
+
value |= (byte & 127) << shift;
|
|
1199
|
+
bytesRead++;
|
|
1200
|
+
if ((byte & 128) === 0) {
|
|
1201
|
+
return { value, bytesRead };
|
|
1202
|
+
}
|
|
1203
|
+
shift += 7;
|
|
1204
|
+
if (shift > 28) {
|
|
1205
|
+
throw new OmaTrustError("INVALID_DID", "Varint too long in did:key multicodec prefix");
|
|
1206
|
+
}
|
|
1207
|
+
}
|
|
1208
|
+
throw new OmaTrustError("INVALID_DID", "Truncated varint in did:key multicodec prefix");
|
|
1209
|
+
}
|
|
1210
|
+
function decompressEcKey(compressedKey, crv) {
|
|
1211
|
+
throw new OmaTrustError(
|
|
1212
|
+
"UNSUPPORTED_KEY_TYPE",
|
|
1213
|
+
`Cannot convert compressed EC key (${crv}) from did:key to did:jwk. Decompressing EC points requires elliptic curve arithmetic. For EVM keys, use didEthrToDidPkh() instead.`,
|
|
1214
|
+
{ crv, keyLength: compressedKey.length }
|
|
1215
|
+
);
|
|
1216
|
+
}
|
|
1217
|
+
function didKeyToDidJwk(did) {
|
|
1218
|
+
if (typeof did !== "string" || !did.startsWith("did:key:")) {
|
|
1219
|
+
throw new OmaTrustError("INVALID_DID", "Expected a did:key DID", { did });
|
|
1220
|
+
}
|
|
1221
|
+
const identifier = did.slice("did:key:".length);
|
|
1222
|
+
if (!identifier || !identifier.startsWith("z")) {
|
|
1223
|
+
throw new OmaTrustError(
|
|
1224
|
+
"INVALID_DID",
|
|
1225
|
+
"did:key identifier must start with 'z' (base58btc multibase prefix)",
|
|
1226
|
+
{ did }
|
|
1227
|
+
);
|
|
1228
|
+
}
|
|
1229
|
+
let decoded;
|
|
1230
|
+
try {
|
|
1231
|
+
decoded = base58btc.decode(identifier);
|
|
1232
|
+
} catch (err) {
|
|
1233
|
+
throw new OmaTrustError(
|
|
1234
|
+
"INVALID_DID",
|
|
1235
|
+
"Failed to decode base58btc from did:key identifier",
|
|
1236
|
+
{ did, cause: err }
|
|
1237
|
+
);
|
|
1238
|
+
}
|
|
1239
|
+
if (decoded.length < 3) {
|
|
1240
|
+
throw new OmaTrustError("INVALID_DID", "did:key decoded bytes too short", { did });
|
|
1241
|
+
}
|
|
1242
|
+
const { value: codecValue, bytesRead } = readVarint(decoded, 0);
|
|
1243
|
+
const keyType = MULTICODEC_KEY_TYPES[codecValue];
|
|
1244
|
+
if (!keyType) {
|
|
1245
|
+
throw new OmaTrustError(
|
|
1246
|
+
"UNSUPPORTED_KEY_TYPE",
|
|
1247
|
+
`Unsupported multicodec key type 0x${codecValue.toString(16)} in did:key`,
|
|
1248
|
+
{ did, codec: `0x${codecValue.toString(16)}` }
|
|
1249
|
+
);
|
|
1250
|
+
}
|
|
1251
|
+
const keyBytes = decoded.slice(bytesRead);
|
|
1252
|
+
if (keyType.kty === "OKP") {
|
|
1253
|
+
if (keyBytes.length !== 32) {
|
|
1254
|
+
throw new OmaTrustError(
|
|
1255
|
+
"INVALID_DID",
|
|
1256
|
+
`Expected 32-byte ${keyType.crv} key, got ${keyBytes.length} bytes`,
|
|
1257
|
+
{ did, crv: keyType.crv }
|
|
1258
|
+
);
|
|
1259
|
+
}
|
|
1260
|
+
const jwk = {
|
|
1261
|
+
kty: keyType.kty,
|
|
1262
|
+
crv: keyType.crv,
|
|
1263
|
+
x: base64url.encode(keyBytes)
|
|
1264
|
+
};
|
|
1265
|
+
return jwkToDidJwk(jwk);
|
|
1266
|
+
}
|
|
1267
|
+
return decompressEcKey(keyBytes, keyType.crv).x.toString();
|
|
1268
|
+
}
|
|
292
1269
|
|
|
293
|
-
export { buildCaip10, buildCaip2, buildDidPkh, buildDidPkhFromCaip10, buildDidWeb, buildEvmDidPkh, canonicalizeForHash, canonicalizeJson, computeDidAddress, computeDidHash, didToAddress, extractAddressFromDid, extractDidIdentifier, extractDidMethod, getAddressFromDidPkh, getChainIdFromDidPkh, getDomainFromDidWeb, getNamespaceFromDidPkh, hashCanonicalizedJson, isEvmDidPkh, isValidDid, normalizeCaip10, normalizeDid, normalizeDidHandle, normalizeDidKey, normalizeDidPkh, normalizeDidWeb, normalizeDomain, parseCaip10, parseCaip2, validateDidAddress };
|
|
1270
|
+
export { artifactDidFromBytes, artifactDidFromJson, assertBareDid, assertJsonSafe, buildCaip10, buildCaip2, buildDidPkh, buildDidPkhFromCaip10, buildDidWeb, buildEvmDidPkh, canonicalizeAndKeccak256, canonicalizeForHash, canonicalizeJson, computeDidAddress, computeDidHash, computeJwkThumbprint, didEthrToDidPkh, didJwkToJwk, didKeyToDidJwk, didToAddress, extractAddressFromDid, extractAuthorizationMetadata, extractControllerEvmAddress, extractDidIdentifier, extractDidMethod, formatJktValue, getAddressFromDidPkh, getChainIdFromDidPkh, getDomainFromDidWeb, getNamespaceFromDidPkh, hashCanonicalizedJson, isDidUrl, isEvmDidPkh, isPrivateKeyDid, isSameControllerId, isValidDid, jwkToDidJwk, normalizeCaip10, normalizeDid, normalizeDidHandle, normalizeDidJwk, normalizeDidKey, normalizeDidPkh, normalizeDidWeb, normalizeDomain, parseArtifactDid, parseCaip10, parseCaip2, parseDidUrl, parseJsonStrict, publicJwkEquals, resolveDidUrlToControllerDid, resolveDidUrlToPublicKey, validateDidAddress, validatePrivateKeyDid, validatePublicJwk, verifyDidArtifact };
|
|
294
1271
|
//# sourceMappingURL=index.js.map
|
|
295
1272
|
//# sourceMappingURL=index.js.map
|