@atproto/did 0.3.0 → 0.4.0

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/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # @atproto/did
2
2
 
3
+ ## 0.4.0
4
+
5
+ ### Minor Changes
6
+
7
+ - [#4929](https://github.com/bluesky-social/atproto/pull/4929) [`f01c59f`](https://github.com/bluesky-social/atproto/commit/f01c59f5bd3f75fb8b47a9eecd4858b84033fb7c) Thanks [@devinivy](https://github.com/devinivy)! - **BREAKING:** Drop support for Node.js 18 and 20. Node.js 22 is now the minimum supported version. Docker images now use Node.js 24.
8
+
9
+ - [#4943](https://github.com/bluesky-social/atproto/pull/4943) [`c459153`](https://github.com/bluesky-social/atproto/commit/c459153395a30ce89e050892c8fab7dc98e019b9) Thanks [@devinivy](https://github.com/devinivy)! - **BREAKING:** Convert to pure ESM. All packages now ship `"type": "module"` with ES module output and Node16 module resolution.
10
+
11
+ Node.js 22's `require()` compatibility layer can still load these packages in CommonJS code.
12
+
13
+ - [#4930](https://github.com/bluesky-social/atproto/pull/4930) [`908bece`](https://github.com/bluesky-social/atproto/commit/908bece169258bff5ad121e5eec157d6ded6f705) Thanks [@devinivy](https://github.com/devinivy)! - Build with TypeScript 6.0.
14
+
3
15
  ## 0.3.0
4
16
 
5
17
  ### Minor Changes
package/dist/atproto.js CHANGED
@@ -1,59 +1,46 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ATPROTO_VERIFICATION_METHOD_TYPES = exports.isAtprotoAudience = exports.atprotoDidSchema = void 0;
4
- exports.isAtprotoDid = isAtprotoDid;
5
- exports.asAtprotoDid = asAtprotoDid;
6
- exports.assertAtprotoDid = assertAtprotoDid;
7
- exports.assertAtprotoDidWeb = assertAtprotoDidWeb;
8
- exports.isAtprotoDidWeb = isAtprotoDidWeb;
9
- exports.extractAtprotoData = extractAtprotoData;
10
- exports.extractPdsUrl = extractPdsUrl;
11
- exports.isAtprotoAka = isAtprotoAka;
12
- exports.isAtprotoPersonalDataServerService = isAtprotoPersonalDataServerService;
13
- exports.isAtprotoVerificationMethod = isAtprotoVerificationMethod;
14
- const zod_1 = require("zod");
15
- const did_error_js_1 = require("./did-error.js");
16
- const uri_js_1 = require("./lib/uri.js");
17
- const methods_js_1 = require("./methods.js");
18
- const utils_js_1 = require("./utils.js");
19
- exports.atprotoDidSchema = zod_1.z
1
+ import { z } from 'zod';
2
+ import { DidError, InvalidDidError } from './did-error.js';
3
+ import { canParse, isFragment } from './lib/uri.js';
4
+ import { DID_PLC_PREFIX, DID_WEB_PREFIX, assertDidPlc, assertDidWeb, isDidPlc, isDidWeb, } from './methods.js';
5
+ import { matchesIdentifier } from './utils.js';
6
+ export const atprotoDidSchema = z
20
7
  .string()
21
8
  .refine(isAtprotoDid, `Atproto only allows "plc" and "web" DID methods`);
22
- function isAtprotoDid(input) {
23
- return (0, methods_js_1.isDidPlc)(input) || isAtprotoDidWeb(input);
9
+ export function isAtprotoDid(input) {
10
+ return isDidPlc(input) || isAtprotoDidWeb(input);
24
11
  }
25
- function asAtprotoDid(input) {
12
+ export function asAtprotoDid(input) {
26
13
  assertAtprotoDid(input);
27
14
  return input;
28
15
  }
29
- function assertAtprotoDid(input) {
16
+ export function assertAtprotoDid(input) {
30
17
  if (typeof input !== 'string') {
31
- throw new did_error_js_1.InvalidDidError(typeof input, `DID must be a string`);
18
+ throw new InvalidDidError(typeof input, `DID must be a string`);
32
19
  }
33
- else if (input.startsWith(methods_js_1.DID_PLC_PREFIX)) {
34
- (0, methods_js_1.assertDidPlc)(input);
20
+ else if (input.startsWith(DID_PLC_PREFIX)) {
21
+ assertDidPlc(input);
35
22
  }
36
- else if (input.startsWith(methods_js_1.DID_WEB_PREFIX)) {
23
+ else if (input.startsWith(DID_WEB_PREFIX)) {
37
24
  assertAtprotoDidWeb(input);
38
25
  }
39
26
  else {
40
- throw new did_error_js_1.InvalidDidError(input, `Atproto only allows "plc" and "web" DID methods`);
27
+ throw new InvalidDidError(input, `Atproto only allows "plc" and "web" DID methods`);
41
28
  }
42
29
  }
43
- function assertAtprotoDidWeb(input) {
44
- (0, methods_js_1.assertDidWeb)(input);
30
+ export function assertAtprotoDidWeb(input) {
31
+ assertDidWeb(input);
45
32
  if (isDidWebWithPath(input)) {
46
- throw new did_error_js_1.InvalidDidError(input, `Atproto does not allow path components in Web DIDs`);
33
+ throw new InvalidDidError(input, `Atproto does not allow path components in Web DIDs`);
47
34
  }
48
35
  if (isDidWebWithHttpsPort(input)) {
49
- throw new did_error_js_1.InvalidDidError(input, `Atproto does not allow port numbers in Web DIDs, except for localhost`);
36
+ throw new InvalidDidError(input, `Atproto does not allow port numbers in Web DIDs, except for localhost`);
50
37
  }
51
38
  }
52
39
  /**
53
40
  * @see {@link https://atproto.com/specs/did#blessed-did-methods}
54
41
  */
55
- function isAtprotoDidWeb(input) {
56
- if (!(0, methods_js_1.isDidWeb)(input)) {
42
+ export function isAtprotoDidWeb(input) {
43
+ if (!isDidWeb(input)) {
57
44
  return false;
58
45
  }
59
46
  if (isDidWebWithPath(input)) {
@@ -65,7 +52,7 @@ function isAtprotoDidWeb(input) {
65
52
  return true;
66
53
  }
67
54
  function isDidWebWithPath(did) {
68
- return did.includes(':', methods_js_1.DID_WEB_PREFIX.length);
55
+ return did.includes(':', DID_WEB_PREFIX.length);
69
56
  }
70
57
  function isLocalhostDid(did) {
71
58
  return (did === 'did:web:localhost' ||
@@ -75,17 +62,17 @@ function isLocalhostDid(did) {
75
62
  function isDidWebWithHttpsPort(did) {
76
63
  if (isLocalhostDid(did))
77
64
  return false;
78
- const pathIdx = did.indexOf(':', methods_js_1.DID_WEB_PREFIX.length);
65
+ const pathIdx = did.indexOf(':', DID_WEB_PREFIX.length);
79
66
  const hasPort = pathIdx === -1
80
67
  ? // No path component, check if there's a port separator anywhere after
81
68
  // the "did:web:" prefix
82
- did.includes('%3A', methods_js_1.DID_WEB_PREFIX.length)
69
+ did.includes('%3A', DID_WEB_PREFIX.length)
83
70
  : // There is a path component; if there is an encoded colon *before* it,
84
71
  // then there is a port number
85
72
  did.lastIndexOf('%3A', pathIdx) !== -1;
86
73
  return hasPort;
87
74
  }
88
- const isAtprotoAudience = (value) => {
75
+ export const isAtprotoAudience = (value) => {
89
76
  if (typeof value !== 'string')
90
77
  return false;
91
78
  const hashIndex = value.indexOf('#');
@@ -93,10 +80,9 @@ const isAtprotoAudience = (value) => {
93
80
  return false;
94
81
  if (value.indexOf('#', hashIndex + 1) !== -1)
95
82
  return false;
96
- return ((0, uri_js_1.isFragment)(value, hashIndex + 1) && isAtprotoDid(value.slice(0, hashIndex)));
83
+ return (isFragment(value, hashIndex + 1) && isAtprotoDid(value.slice(0, hashIndex)));
97
84
  };
98
- exports.isAtprotoAudience = isAtprotoAudience;
99
- function extractAtprotoData(document) {
85
+ export function extractAtprotoData(document) {
100
86
  return {
101
87
  did: document.id,
102
88
  aka: document.alsoKnownAs?.find(isAtprotoAka)?.slice(5),
@@ -104,31 +90,31 @@ function extractAtprotoData(document) {
104
90
  pds: document.service?.find((isAtprotoPersonalDataServerService), document),
105
91
  };
106
92
  }
107
- function extractPdsUrl(document) {
93
+ export function extractPdsUrl(document) {
108
94
  const service = document.service?.find(isAtprotoPersonalDataServerService, document);
109
95
  if (!service) {
110
- throw new did_error_js_1.DidError(document.id, `Document ${document.id} does not contain a (valid) #atproto_pds service URL`, 'did-service-not-found');
96
+ throw new DidError(document.id, `Document ${document.id} does not contain a (valid) #atproto_pds service URL`, 'did-service-not-found');
111
97
  }
112
98
  return new URL(service.serviceEndpoint);
113
99
  }
114
- function isAtprotoAka(value) {
100
+ export function isAtprotoAka(value) {
115
101
  return value.startsWith('at://');
116
102
  }
117
- function isAtprotoPersonalDataServerService(service) {
103
+ export function isAtprotoPersonalDataServerService(service) {
118
104
  return (service?.type === 'AtprotoPersonalDataServer' &&
119
105
  typeof service.serviceEndpoint === 'string' &&
120
- (0, uri_js_1.canParse)(service.serviceEndpoint) &&
121
- (0, utils_js_1.matchesIdentifier)(this.id, 'atproto_pds', service.id));
106
+ canParse(service.serviceEndpoint) &&
107
+ matchesIdentifier(this.id, 'atproto_pds', service.id));
122
108
  }
123
- exports.ATPROTO_VERIFICATION_METHOD_TYPES = Object.freeze([
109
+ export const ATPROTO_VERIFICATION_METHOD_TYPES = Object.freeze([
124
110
  'EcdsaSecp256r1VerificationKey2019',
125
111
  'EcdsaSecp256k1VerificationKey2019',
126
112
  'Multikey',
127
113
  ]);
128
- function isAtprotoVerificationMethod(method) {
114
+ export function isAtprotoVerificationMethod(method) {
129
115
  return (typeof method === 'object' &&
130
116
  typeof method?.publicKeyMultibase === 'string' &&
131
- exports.ATPROTO_VERIFICATION_METHOD_TYPES.includes(method.type) &&
132
- (0, utils_js_1.matchesIdentifier)(this.id, 'atproto', method.id));
117
+ ATPROTO_VERIFICATION_METHOD_TYPES.includes(method.type) &&
118
+ matchesIdentifier(this.id, 'atproto', method.id));
133
119
  }
134
120
  //# sourceMappingURL=atproto.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"atproto.js","sourceRoot":"","sources":["../src/atproto.ts"],"names":[],"mappings":";;;AAyBA,oCAEC;AAED,oCAGC;AAED,4CAaC;AAED,kDAkBC;AAKD,0CAcC;AAmDD,gDAeC;AAED,sCAeC;AAGD,oCAEC;AAUD,gFAYC;AAmBD,kEAiBC;AAxOD,6BAAuB;AAEvB,iDAA0D;AAE1D,yCAAmD;AACnD,6CAOqB;AACrB,yCAA0D;AAQ7C,QAAA,gBAAgB,GAAG,OAAC;KAC9B,MAAM,EAAE;KACR,MAAM,CAAC,YAAY,EAAE,iDAAiD,CAAC,CAAA;AAE1E,SAAgB,YAAY,CAAC,KAAc;IACzC,OAAO,IAAA,qBAAQ,EAAC,KAAK,CAAC,IAAI,eAAe,CAAC,KAAK,CAAC,CAAA;AAClD,CAAC;AAED,SAAgB,YAAY,CAAI,KAAQ;IACtC,gBAAgB,CAAC,KAAK,CAAC,C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ACtD,CAAA;AACH,CAAC;AAEY,QAAA,iCAAiC,GAAG,MAAM,CAAC,MAAM,CAAC;IAC7D,mCAAmC;IACnC,mCAAmC;IACnC,UAAU;CACF,CAAC,CAAA;AAaX,SAAgB,2BAA2B,CAIzC,MAG6D;IAE7D,OAAO,CACL,OAAO,MAAM,KAAK,QAAQ;QAC1B,OAAO,MAAM,EAAE,kBAAkB,KAAK,QAAQ;QAC7C,yCAAwD,CAAC,QAAQ,CAChE,MAAM,CAAC,IAAI,CACZ;QACD,IAAA,4BAAiB,EAAC,IAAI,CAAC,EAAE,EAAE,SAAS,EAAE,MAAM,CAAC,EAAE,CAAC,CACjD,CAAA;AACH,CAAC","sourcesContent":["import { z } from 'zod'\nimport { DidDocument, DidService } from './did-document.js'\nimport { DidError, InvalidDidError } from './did-error.js'\nimport { Did } from './did.js'\nimport { canParse, isFragment } from './lib/uri.js'\nimport {\n DID_PLC_PREFIX,\n DID_WEB_PREFIX,\n assertDidPlc,\n assertDidWeb,\n isDidPlc,\n isDidWeb,\n} from './methods.js'\nimport { Identifier, matchesIdentifier } from './utils.js'\n\n// This file contains atproto-specific DID validation utilities.\n\nexport type AtprotoIdentityDidMethods = 'plc' | 'web'\nexport type AtprotoDid = Did<AtprotoIdentityDidMethods>\nexport type AtprotoDidDocument = DidDocument<AtprotoIdentityDidMethods>\n\nexport const atprotoDidSchema = z\n .string()\n .refine(isAtprotoDid, `Atproto only allows \"plc\" and \"web\" DID methods`)\n\nexport function isAtprotoDid(input: unknown): input is AtprotoDid {\n return isDidPlc(input) || isAtprotoDidWeb(input)\n}\n\nexport function asAtprotoDid<T>(input: T) {\n assertAtprotoDid(input)\n return input\n}\n\nexport function assertAtprotoDid(input: unknown): asserts input is AtprotoDid {\n if (typeof input !== 'string') {\n throw new InvalidDidError(typeof input, `DID must be a string`)\n } else if (input.startsWith(DID_PLC_PREFIX)) {\n assertDidPlc(input)\n } else if (input.startsWith(DID_WEB_PREFIX)) {\n assertAtprotoDidWeb(input)\n } else {\n throw new InvalidDidError(\n input,\n `Atproto only allows \"plc\" and \"web\" DID methods`,\n )\n }\n}\n\nexport function assertAtprotoDidWeb(\n input: unknown,\n): asserts input is Did<'web'> {\n assertDidWeb(input)\n\n if (isDidWebWithPath(input)) {\n throw new InvalidDidError(\n input,\n `Atproto does not allow path components in Web DIDs`,\n )\n }\n\n if (isDidWebWithHttpsPort(input)) {\n throw new InvalidDidError(\n input,\n `Atproto does not allow port numbers in Web DIDs, except for localhost`,\n )\n }\n}\n\n/**\n * @see {@link https://atproto.com/specs/did#blessed-did-methods}\n */\nexport function isAtprotoDidWeb(input: unknown): input is Did<'web'> {\n if (!isDidWeb(input)) {\n return false\n }\n\n if (isDidWebWithPath(input)) {\n return false\n }\n\n if (isDidWebWithHttpsPort(input)) {\n return false\n }\n\n return true\n}\n\nfunction isDidWebWithPath(did: Did<'web'>): boolean {\n return did.includes(':', DID_WEB_PREFIX.length)\n}\n\nfunction isLocalhostDid(did: Did<'web'>): boolean {\n return (\n did === 'did:web:localhost' ||\n did.startsWith('did:web:localhost:') ||\n did.startsWith('did:web:localhost%3A')\n )\n}\n\nfunction isDidWebWithHttpsPort(did: Did<'web'>): boolean {\n if (isLocalhostDid(did)) return false\n\n const pathIdx = did.indexOf(':', DID_WEB_PREFIX.length)\n\n const hasPort =\n pathIdx === -1\n ? // No path component, check if there's a port separator anywhere after\n // the \"did:web:\" prefix\n did.includes('%3A', DID_WEB_PREFIX.length)\n : // There is a path component; if there is an encoded colon *before* it,\n // then there is a port number\n did.lastIndexOf('%3A', pathIdx) !== -1\n\n return hasPort\n}\n\nexport type AtprotoAudience = `${AtprotoDid}#${string}`\nexport const isAtprotoAudience = (value: unknown): value is AtprotoAudience => {\n if (typeof value !== 'string') return false\n const hashIndex = value.indexOf('#')\n if (hashIndex === -1) return false\n if (value.indexOf('#', hashIndex + 1) !== -1) return false\n return (\n isFragment(value, hashIndex + 1) && isAtprotoDid(value.slice(0, hashIndex))\n )\n}\n\nexport type AtprotoData<\n M extends AtprotoIdentityDidMethods = AtprotoIdentityDidMethods,\n> = {\n did: Did<M>\n aka?: string\n key?: AtprotoVerificationMethod<M>\n pds?: AtprotoPersonalDataServerService<M>\n}\n\nexport function extractAtprotoData<M extends AtprotoIdentityDidMethods>(\n document: DidDocument<M>,\n): AtprotoData<M> {\n return {\n did: document.id,\n aka: document.alsoKnownAs?.find(isAtprotoAka)?.slice(5),\n key: document.verificationMethod?.find(\n isAtprotoVerificationMethod<M>,\n document,\n ),\n pds: document.service?.find(\n isAtprotoPersonalDataServerService<M>,\n document,\n ),\n }\n}\n\nexport function extractPdsUrl(document: AtprotoDidDocument): URL {\n const service = document.service?.find(\n isAtprotoPersonalDataServerService,\n document,\n )\n\n if (!service) {\n throw new DidError(\n document.id,\n `Document ${document.id} does not contain a (valid) #atproto_pds service URL`,\n 'did-service-not-found',\n )\n }\n\n return new URL(service.serviceEndpoint)\n}\n\nexport type AtprotoAka = `at://${string}`\nexport function isAtprotoAka(value: string): value is AtprotoAka {\n return value.startsWith('at://')\n}\n\nexport type AtprotoPersonalDataServerService<\n M extends AtprotoIdentityDidMethods = AtprotoIdentityDidMethods,\n> = DidService & {\n id: Identifier<Did<M>, 'atproto_pds'>\n type: 'AtprotoPersonalDataServer'\n serviceEndpoint: string\n}\n\nexport function isAtprotoPersonalDataServerService<\n M extends AtprotoIdentityDidMethods = AtprotoIdentityDidMethods,\n>(\n this: DidDocument<M>,\n service: null | undefined | DidService,\n): service is AtprotoPersonalDataServerService<M> {\n return (\n service?.type === 'AtprotoPersonalDataServer' &&\n typeof service.serviceEndpoint === 'string' &&\n canParse(service.serviceEndpoint) &&\n matchesIdentifier(this.id, 'atproto_pds', service.id)\n )\n}\n\nexport const ATPROTO_VERIFICATION_METHOD_TYPES = Object.freeze([\n 'EcdsaSecp256r1VerificationKey2019',\n 'EcdsaSecp256k1VerificationKey2019',\n 'Multikey',\n] as const)\nexport type SupportedAtprotoVerificationMethodType =\n (typeof ATPROTO_VERIFICATION_METHOD_TYPES)[number]\n\ntype VerificationMethod = NonNullable<DidDocument['verificationMethod']>[number]\nexport type AtprotoVerificationMethod<\n M extends AtprotoIdentityDidMethods = AtprotoIdentityDidMethods,\n> = Extract<VerificationMethod, object> & {\n id: Identifier<Did<M>, 'atproto'>\n type: SupportedAtprotoVerificationMethodType\n publicKeyMultibase: string\n}\n\nexport function isAtprotoVerificationMethod<\n M extends AtprotoIdentityDidMethods = AtprotoIdentityDidMethods,\n>(\n this: DidDocument<M>,\n method:\n | null\n | undefined\n | NonNullable<DidDocument<M>['verificationMethod']>[number],\n): method is AtprotoVerificationMethod<M> {\n return (\n typeof method === 'object' &&\n typeof method?.publicKeyMultibase === 'string' &&\n (ATPROTO_VERIFICATION_METHOD_TYPES as readonly unknown[]).includes(\n method.type,\n ) &&\n matchesIdentifier(this.id, 'atproto', method.id)\n )\n}\n"]}
1
+ {"version":3,"file":"atproto.js","sourceRoot":"","sources":["../src/atproto.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAA;AAEvB,OAAO,EAAE,QAAQ,EAAE,eAAe,EAAE,MAAM,gBAAgB,CAAA;AAE1D,OAAO,EAAE,QAAQ,EAAE,UAAU,EAAE,MAAM,cAAc,CAAA;AACnD,OAAO,EACL,cAAc,EACd,cAAc,EACd,YAAY,EACZ,YAAY,EACZ,QAAQ,EACR,QAAQ,GACT,MAAM,cAAc,CAAA;AACrB,OAAO,EAAc,iBAAiB,EAAE,MAAM,YAAY,CAAA;AAQ1D,MAAM,CAAC,MAAM,gBAAgB,GAAG,CAAC;KAC9B,MAAM,EAAE;KACR,MAAM,CAAC,YAAY,EAAE,iDAAiD,CAAC,CAAA;AAE1E,MAAM,UAAU,YAAY,CAAC,KAAc;IACzC,OAAO,QAAQ,CAAC,KAAK,CAAC,IAAI,eAAe,CAAC,KAAK,CAAC,CAAA;AAClD,CAAC;AAED,MAAM,UAAU,YAAY,CAAI,KAAQ;IACtC,gBAAgB,CAAC,K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,MAAM,CAAC,MAAM,iCAAiC,GAAG,MAAM,CAAC,MAAM,CAAC;IAC7D,mCAAmC;IACnC,mCAAmC;IACnC,UAAU;CACF,CAAC,CAAA;AAaX,MAAM,UAAU,2BAA2B,CAIzC,MAG6D;IAE7D,OAAO,CACL,OAAO,MAAM,KAAK,QAAQ;QAC1B,OAAO,MAAM,EAAE,kBAAkB,KAAK,QAAQ;QAC7C,iCAAwD,CAAC,QAAQ,CAChE,MAAM,CAAC,IAAI,CACZ;QACD,iBAAiB,CAAC,IAAI,CAAC,EAAE,EAAE,SAAS,EAAE,MAAM,CAAC,EAAE,CAAC,CACjD,CAAA;AACH,CAAC","sourcesContent":["import { z } from 'zod'\nimport { DidDocument, DidService } from './did-document.js'\nimport { DidError, InvalidDidError } from './did-error.js'\nimport { Did } from './did.js'\nimport { canParse, isFragment } from './lib/uri.js'\nimport {\n DID_PLC_PREFIX,\n DID_WEB_PREFIX,\n assertDidPlc,\n assertDidWeb,\n isDidPlc,\n isDidWeb,\n} from './methods.js'\nimport { Identifier, matchesIdentifier } from './utils.js'\n\n// This file contains atproto-specific DID validation utilities.\n\nexport type AtprotoIdentityDidMethods = 'plc' | 'web'\nexport type AtprotoDid = Did<AtprotoIdentityDidMethods>\nexport type AtprotoDidDocument = DidDocument<AtprotoIdentityDidMethods>\n\nexport const atprotoDidSchema = z\n .string()\n .refine(isAtprotoDid, `Atproto only allows \"plc\" and \"web\" DID methods`)\n\nexport function isAtprotoDid(input: unknown): input is AtprotoDid {\n return isDidPlc(input) || isAtprotoDidWeb(input)\n}\n\nexport function asAtprotoDid<T>(input: T) {\n assertAtprotoDid(input)\n return input\n}\n\nexport function assertAtprotoDid(input: unknown): asserts input is AtprotoDid {\n if (typeof input !== 'string') {\n throw new InvalidDidError(typeof input, `DID must be a string`)\n } else if (input.startsWith(DID_PLC_PREFIX)) {\n assertDidPlc(input)\n } else if (input.startsWith(DID_WEB_PREFIX)) {\n assertAtprotoDidWeb(input)\n } else {\n throw new InvalidDidError(\n input,\n `Atproto only allows \"plc\" and \"web\" DID methods`,\n )\n }\n}\n\nexport function assertAtprotoDidWeb(\n input: unknown,\n): asserts input is Did<'web'> {\n assertDidWeb(input)\n\n if (isDidWebWithPath(input)) {\n throw new InvalidDidError(\n input,\n `Atproto does not allow path components in Web DIDs`,\n )\n }\n\n if (isDidWebWithHttpsPort(input)) {\n throw new InvalidDidError(\n input,\n `Atproto does not allow port numbers in Web DIDs, except for localhost`,\n )\n }\n}\n\n/**\n * @see {@link https://atproto.com/specs/did#blessed-did-methods}\n */\nexport function isAtprotoDidWeb(input: unknown): input is Did<'web'> {\n if (!isDidWeb(input)) {\n return false\n }\n\n if (isDidWebWithPath(input)) {\n return false\n }\n\n if (isDidWebWithHttpsPort(input)) {\n return false\n }\n\n return true\n}\n\nfunction isDidWebWithPath(did: Did<'web'>): boolean {\n return did.includes(':', DID_WEB_PREFIX.length)\n}\n\nfunction isLocalhostDid(did: Did<'web'>): boolean {\n return (\n did === 'did:web:localhost' ||\n did.startsWith('did:web:localhost:') ||\n did.startsWith('did:web:localhost%3A')\n )\n}\n\nfunction isDidWebWithHttpsPort(did: Did<'web'>): boolean {\n if (isLocalhostDid(did)) return false\n\n const pathIdx = did.indexOf(':', DID_WEB_PREFIX.length)\n\n const hasPort =\n pathIdx === -1\n ? // No path component, check if there's a port separator anywhere after\n // the \"did:web:\" prefix\n did.includes('%3A', DID_WEB_PREFIX.length)\n : // There is a path component; if there is an encoded colon *before* it,\n // then there is a port number\n did.lastIndexOf('%3A', pathIdx) !== -1\n\n return hasPort\n}\n\nexport type AtprotoAudience = `${AtprotoDid}#${string}`\nexport const isAtprotoAudience = (value: unknown): value is AtprotoAudience => {\n if (typeof value !== 'string') return false\n const hashIndex = value.indexOf('#')\n if (hashIndex === -1) return false\n if (value.indexOf('#', hashIndex + 1) !== -1) return false\n return (\n isFragment(value, hashIndex + 1) && isAtprotoDid(value.slice(0, hashIndex))\n )\n}\n\nexport type AtprotoData<\n M extends AtprotoIdentityDidMethods = AtprotoIdentityDidMethods,\n> = {\n did: Did<M>\n aka?: string\n key?: AtprotoVerificationMethod<M>\n pds?: AtprotoPersonalDataServerService<M>\n}\n\nexport function extractAtprotoData<M extends AtprotoIdentityDidMethods>(\n document: DidDocument<M>,\n): AtprotoData<M> {\n return {\n did: document.id,\n aka: document.alsoKnownAs?.find(isAtprotoAka)?.slice(5),\n key: document.verificationMethod?.find(\n isAtprotoVerificationMethod<M>,\n document,\n ),\n pds: document.service?.find(\n isAtprotoPersonalDataServerService<M>,\n document,\n ),\n }\n}\n\nexport function extractPdsUrl(document: AtprotoDidDocument): URL {\n const service = document.service?.find(\n isAtprotoPersonalDataServerService,\n document,\n )\n\n if (!service) {\n throw new DidError(\n document.id,\n `Document ${document.id} does not contain a (valid) #atproto_pds service URL`,\n 'did-service-not-found',\n )\n }\n\n return new URL(service.serviceEndpoint)\n}\n\nexport type AtprotoAka = `at://${string}`\nexport function isAtprotoAka(value: string): value is AtprotoAka {\n return value.startsWith('at://')\n}\n\nexport type AtprotoPersonalDataServerService<\n M extends AtprotoIdentityDidMethods = AtprotoIdentityDidMethods,\n> = DidService & {\n id: Identifier<Did<M>, 'atproto_pds'>\n type: 'AtprotoPersonalDataServer'\n serviceEndpoint: string\n}\n\nexport function isAtprotoPersonalDataServerService<\n M extends AtprotoIdentityDidMethods = AtprotoIdentityDidMethods,\n>(\n this: DidDocument<M>,\n service: null | undefined | DidService,\n): service is AtprotoPersonalDataServerService<M> {\n return (\n service?.type === 'AtprotoPersonalDataServer' &&\n typeof service.serviceEndpoint === 'string' &&\n canParse(service.serviceEndpoint) &&\n matchesIdentifier(this.id, 'atproto_pds', service.id)\n )\n}\n\nexport const ATPROTO_VERIFICATION_METHOD_TYPES = Object.freeze([\n 'EcdsaSecp256r1VerificationKey2019',\n 'EcdsaSecp256k1VerificationKey2019',\n 'Multikey',\n] as const)\nexport type SupportedAtprotoVerificationMethodType =\n (typeof ATPROTO_VERIFICATION_METHOD_TYPES)[number]\n\ntype VerificationMethod = NonNullable<DidDocument['verificationMethod']>[number]\nexport type AtprotoVerificationMethod<\n M extends AtprotoIdentityDidMethods = AtprotoIdentityDidMethods,\n> = Extract<VerificationMethod, object> & {\n id: Identifier<Did<M>, 'atproto'>\n type: SupportedAtprotoVerificationMethodType\n publicKeyMultibase: string\n}\n\nexport function isAtprotoVerificationMethod<\n M extends AtprotoIdentityDidMethods = AtprotoIdentityDidMethods,\n>(\n this: DidDocument<M>,\n method:\n | null\n | undefined\n | NonNullable<DidDocument<M>['verificationMethod']>[number],\n): method is AtprotoVerificationMethod<M> {\n return (\n typeof method === 'object' &&\n typeof method?.publicKeyMultibase === 'string' &&\n (ATPROTO_VERIFICATION_METHOD_TYPES as readonly unknown[]).includes(\n method.type,\n ) &&\n matchesIdentifier(this.id, 'atproto', method.id)\n )\n}\n"]}
@@ -1,46 +1,43 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.didDocumentValidator = exports.didDocumentSchema = void 0;
4
- const zod_1 = require("zod");
5
- const did_js_1 = require("./did.js");
6
- const uri_js_1 = require("./lib/uri.js");
1
+ import { z } from 'zod';
2
+ import { didSchema } from './did.js';
3
+ import { isFragment } from './lib/uri.js';
7
4
  /**
8
5
  * RFC3968 compliant URI
9
6
  *
10
7
  * @see {@link https://www.rfc-editor.org/rfc/rfc3986}
11
8
  */
12
- const rfc3968UriSchema = zod_1.z.string().url('RFC3968 compliant URI');
13
- const didControllerSchema = zod_1.z.union([did_js_1.didSchema, zod_1.z.array(did_js_1.didSchema)]);
9
+ const rfc3968UriSchema = z.string().url('RFC3968 compliant URI');
10
+ const didControllerSchema = z.union([didSchema, z.array(didSchema)]);
14
11
  /**
15
12
  * @note this schema is too permissive
16
13
  */
17
- const didRelativeUriSchema = zod_1.z.union([
14
+ const didRelativeUriSchema = z.union([
18
15
  rfc3968UriSchema.refine((value) => {
19
16
  const fragmentIndex = value.indexOf('#');
20
17
  if (fragmentIndex === -1)
21
18
  return false;
22
- return (0, uri_js_1.isFragment)(value, fragmentIndex + 1);
19
+ return isFragment(value, fragmentIndex + 1);
23
20
  }, {
24
21
  message: 'Missing or invalid fragment in RFC3968 URI',
25
22
  }),
26
- zod_1.z
23
+ z
27
24
  .string()
28
25
  .refine((value) => value.charCodeAt(0) === 35 /* # */, {
29
26
  message: 'Fragment must start with #',
30
27
  })
31
- .refine((value) => (0, uri_js_1.isFragment)(value, 1), {
28
+ .refine((value) => isFragment(value, 1), {
32
29
  message: 'Invalid char in URI fragment',
33
30
  }),
34
31
  ]);
35
32
  /**
36
33
  * @see {@link https://www.w3.org/TR/did-1.0/#verification-material Verification Material}
37
34
  */
38
- const didVerificationMethodSchema = zod_1.z.object({
35
+ const didVerificationMethodSchema = z.object({
39
36
  id: didRelativeUriSchema,
40
- type: zod_1.z.string().min(1),
37
+ type: z.string().min(1),
41
38
  controller: didControllerSchema,
42
- publicKeyJwk: zod_1.z.record(zod_1.z.string(), zod_1.z.unknown()).optional(),
43
- publicKeyMultibase: zod_1.z.string().optional(),
39
+ publicKeyJwk: z.record(z.string(), z.unknown()).optional(),
40
+ publicKeyMultibase: z.string().optional(),
44
41
  });
45
42
  /**
46
43
  * The value of the id property MUST be a URI conforming to [RFC3986]. A
@@ -59,7 +56,7 @@ const didServiceIdSchema = didRelativeUriSchema;
59
56
  * SHOULD be registered in the DID Specification Registries
60
57
  * [DID-SPEC-REGISTRIES].
61
58
  */
62
- const didServiceTypeSchema = zod_1.z.union([zod_1.z.string(), zod_1.z.array(zod_1.z.string())]);
59
+ const didServiceTypeSchema = z.union([z.string(), z.array(z.string())]);
63
60
  /**
64
61
  * The value of the serviceEndpoint property MUST be a string, a map, or a set
65
62
  * composed of one or more strings and/or maps. All string values MUST be valid
@@ -67,18 +64,18 @@ const didServiceTypeSchema = zod_1.z.union([zod_1.z.string(), zod_1.z.array(zod_
67
64
  * and Comparison rules in RFC3986 and to any normalization rules in its
68
65
  * applicable URI scheme specification.
69
66
  */
70
- const didServiceEndpointSchema = zod_1.z.union([
67
+ const didServiceEndpointSchema = z.union([
71
68
  rfc3968UriSchema,
72
- zod_1.z.record(zod_1.z.string(), rfc3968UriSchema),
73
- zod_1.z
74
- .array(zod_1.z.union([rfc3968UriSchema, zod_1.z.record(zod_1.z.string(), rfc3968UriSchema)]))
69
+ z.record(z.string(), rfc3968UriSchema),
70
+ z
71
+ .array(z.union([rfc3968UriSchema, z.record(z.string(), rfc3968UriSchema)]))
75
72
  .nonempty(),
76
73
  ]);
77
74
  /**
78
75
  * Each service map MUST contain id, type, and serviceEndpoint properties.
79
76
  * @see {@link https://www.w3.org/TR/did-core/#services}
80
77
  */
81
- const didServiceSchema = zod_1.z.object({
78
+ const didServiceSchema = z.object({
82
79
  id: didServiceIdSchema,
83
80
  type: didServiceTypeSchema,
84
81
  serviceEndpoint: didServiceEndpointSchema,
@@ -86,7 +83,7 @@ const didServiceSchema = zod_1.z.object({
86
83
  /**
87
84
  * @see {@link https://www.w3.org/TR/did-1.0/#referring-to-verification-methods Referring to Verification Methods}
88
85
  */
89
- const verificationMethodReference = zod_1.z.union([
86
+ const verificationMethodReference = z.union([
90
87
  //
91
88
  didRelativeUriSchema,
92
89
  didVerificationMethodSchema,
@@ -95,12 +92,12 @@ const verificationMethodReference = zod_1.z.union([
95
92
  * @note This schema is incomplete
96
93
  * @see {@link https://www.w3.org/TR/did-core/#production-0}
97
94
  */
98
- exports.didDocumentSchema = zod_1.z.object({
99
- '@context': zod_1.z
95
+ export const didDocumentSchema = z.object({
96
+ '@context': z
100
97
  .union([
101
- zod_1.z.literal('https://www.w3.org/ns/did/v1'),
102
- zod_1.z
103
- .array(zod_1.z.string().url())
98
+ z.literal('https://www.w3.org/ns/did/v1'),
99
+ z
100
+ .array(z.string().url())
104
101
  .nonempty()
105
102
  .refine((data) => data[0] === 'https://www.w3.org/ns/did/v1', {
106
103
  message: 'First @context must be https://www.w3.org/ns/did/v1',
@@ -108,15 +105,15 @@ exports.didDocumentSchema = zod_1.z.object({
108
105
  ])
109
106
  // @NOTE @context is required by producers, but optional for consumers.
110
107
  .optional(),
111
- id: did_js_1.didSchema,
108
+ id: didSchema,
112
109
  controller: didControllerSchema.optional(),
113
- alsoKnownAs: zod_1.z.array(rfc3968UriSchema).optional(),
114
- service: zod_1.z.array(didServiceSchema).optional(),
115
- authentication: zod_1.z.array(verificationMethodReference).optional(),
116
- verificationMethod: zod_1.z.array(didVerificationMethodSchema).optional(),
110
+ alsoKnownAs: z.array(rfc3968UriSchema).optional(),
111
+ service: z.array(didServiceSchema).optional(),
112
+ authentication: z.array(verificationMethodReference).optional(),
113
+ verificationMethod: z.array(didVerificationMethodSchema).optional(),
117
114
  });
118
115
  // @TODO: add other refinements ?
119
- exports.didDocumentValidator = exports.didDocumentSchema
116
+ export const didDocumentValidator = didDocumentSchema
120
117
  // Ensure that every service id is unique
121
118
  .superRefine(({ id: did, service }, ctx) => {
122
119
  if (service) {
@@ -131,7 +128,7 @@ exports.didDocumentValidator = exports.didDocumentSchema
131
128
  }
132
129
  else {
133
130
  ctx.addIssue({
134
- code: zod_1.z.ZodIssueCode.custom,
131
+ code: z.ZodIssueCode.custom,
135
132
  message: `Duplicate service id (${current.id}) found in the document`,
136
133
  path: ['service', i, 'id'],
137
134
  });
@@ -1 +1 @@
1
- {"version":3,"file":"did-document.js","sourceRoot":"","sources":["../src/did-document.ts"],"names":[],"mappings":";;;AAAA,6BAAuB;AACvB,qCAAyC;AACzC,yCAAyC;AAEzC;;;;GAIG;AACH,MAAM,gBAAgB,GAAG,OAAC,CAAC,MAAM,EAAE,CAAC,GAAG,CAAC,uBAAuB,CAAC,CAAA;AAEhE,MAAM,mBAAmB,GAAG,OAAC,CAAC,KAAK,CAAC,CAAC,kBAAS,EAAE,OAAC,CAAC,KAAK,CAAC,kBAAS,CAAC,CAAC,CAAC,CAAA;AAEpE;;GAEG;AACH,MAAM,oBAAoB,GAAG,OAAC,CAAC,KAAK,CAAC;IACnC,gBAAgB,CAAC,MAAM,CACrB,CAAC,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\nimport { Did, didSchema } from './did.js'\nimport { isFragment } from './lib/uri.js'\n\n/**\n * RFC3968 compliant URI\n *\n * @see {@link https://www.rfc-editor.org/rfc/rfc3986}\n */\nconst rfc3968UriSchema = z.string().url('RFC3968 compliant URI')\n\nconst didControllerSchema = z.union([didSchema, z.array(didSchema)])\n\n/**\n * @note this schema is too permissive\n */\nconst didRelativeUriSchema = z.union([\n rfc3968UriSchema.refine(\n (value) => {\n const fragmentIndex = value.indexOf('#')\n if (fragmentIndex === -1) return false\n return isFragment(value, fragmentIndex + 1)\n },\n {\n message: 'Missing or invalid fragment in RFC3968 URI',\n },\n ),\n z\n .string()\n .refine((value) => value.charCodeAt(0) === 35 /* # */, {\n message: 'Fragment must start with #',\n })\n .refine((value) => isFragment(value, 1), {\n message: 'Invalid char in URI fragment',\n }),\n])\n\n/**\n * @see {@link https://www.w3.org/TR/did-1.0/#verification-material Verification Material}\n */\nconst didVerificationMethodSchema = z.object({\n id: didRelativeUriSchema,\n type: z.string().min(1),\n controller: didControllerSchema,\n publicKeyJwk: z.record(z.string(), z.unknown()).optional(),\n publicKeyMultibase: z.string().optional(),\n})\n\n/**\n * The value of the id property MUST be a URI conforming to [RFC3986]. A\n * conforming producer MUST NOT produce multiple service entries with the same\n * id. A conforming consumer MUST produce an error if it detects multiple\n * service entries with the same id.\n *\n * @note Normally, only rfc3968UriSchema should be allowed here. However, the\n * did:plc uses relative URI. For this reason, we also allow relative URIs\n * here.\n */\nconst didServiceIdSchema = didRelativeUriSchema\n\n/**\n * The value of the type property MUST be a string or a set of strings. In order\n * to maximize interoperability, the service type and its associated properties\n * SHOULD be registered in the DID Specification Registries\n * [DID-SPEC-REGISTRIES].\n */\nconst didServiceTypeSchema = z.union([z.string(), z.array(z.string())])\n\n/**\n * The value of the serviceEndpoint property MUST be a string, a map, or a set\n * composed of one or more strings and/or maps. All string values MUST be valid\n * URIs conforming to [RFC3986] and normalized according to the Normalization\n * and Comparison rules in RFC3986 and to any normalization rules in its\n * applicable URI scheme specification.\n */\nconst didServiceEndpointSchema = z.union([\n rfc3968UriSchema,\n z.record(z.string(), rfc3968UriSchema),\n z\n .array(z.union([rfc3968UriSchema, z.record(z.string(), rfc3968UriSchema)]))\n .nonempty(),\n])\n\n/**\n * Each service map MUST contain id, type, and serviceEndpoint properties.\n * @see {@link https://www.w3.org/TR/did-core/#services}\n */\nconst didServiceSchema = z.object({\n id: didServiceIdSchema,\n type: didServiceTypeSchema,\n serviceEndpoint: didServiceEndpointSchema,\n})\n\nexport type DidService = z.infer<typeof didServiceSchema>\n\n/**\n * @see {@link https://www.w3.org/TR/did-1.0/#referring-to-verification-methods Referring to Verification Methods}\n */\nconst verificationMethodReference = z.union([\n //\n didRelativeUriSchema,\n didVerificationMethodSchema,\n])\n\n/**\n * @note This schema is incomplete\n * @see {@link https://www.w3.org/TR/did-core/#production-0}\n */\nexport const didDocumentSchema = z.object({\n '@context': z\n .union([\n z.literal('https://www.w3.org/ns/did/v1'),\n z\n .array(z.string().url())\n .nonempty()\n .refine((data) => data[0] === 'https://www.w3.org/ns/did/v1', {\n message: 'First @context must be https://www.w3.org/ns/did/v1',\n }),\n ])\n // @NOTE @context is required by producers, but optional for consumers.\n .optional(),\n id: didSchema,\n controller: didControllerSchema.optional(),\n alsoKnownAs: z.array(rfc3968UriSchema).optional(),\n service: z.array(didServiceSchema).optional(),\n authentication: z.array(verificationMethodReference).optional(),\n verificationMethod: z.array(didVerificationMethodSchema).optional(),\n})\n\nexport type DidDocument<Method extends string = string> = z.infer<\n typeof didDocumentSchema\n> & { id: Did<Method> }\n\n// @TODO: add other refinements ?\nexport const didDocumentValidator = didDocumentSchema\n // Ensure that every service id is unique\n .superRefine(({ id: did, service }, ctx) => {\n if (service) {\n const visited = new Set()\n\n for (let i = 0; i < service.length; i++) {\n const current = service[i]\n\n const serviceId = current.id.startsWith('#')\n ? `${did}${current.id}`\n : current.id\n\n if (!visited.has(serviceId)) {\n visited.add(serviceId)\n } else {\n ctx.addIssue({\n code: z.ZodIssueCode.custom,\n message: `Duplicate service id (${current.id}) found in the document`,\n path: ['service', i, 'id'],\n })\n }\n }\n }\n })\n"]}
1
+ {"version":3,"file":"did-document.js","sourceRoot":"","sources":["../src/did-document.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAA;AACvB,OAAO,EAAO,SAAS,EAAE,MAAM,UAAU,CAAA;AACzC,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAA;AAEzC;;;;GAIG;AACH,MAAM,gBAAgB,GAAG,CAAC,CAAC,MAAM,EAAE,CAAC,GAAG,CAAC,uBAAuB,CAAC,CAAA;AAEhE,MAAM,mBAAmB,GAAG,CAAC,CAAC,KAAK,CAAC,CAAC,SAAS,EAAE,CAAC,CAAC,KAAK,CAAC,SAAS,CAAC,CAAC,CAAC,CAAA;AAEpE;;GAEG;AACH,MAAM,oBAAoB,GAAG,CAAC,CAAC,KAAK,CAAC;IACnC,gBAAgB,CAAC,MAAM,CACrB,CAAC,KAAK,EAAE,EAAE;QACR,MAAM,aAAa,GAAG,KAAK,CAAC,OAAO,CAAC,GAAG,CAAC,CAAA;QACxC,IAAI,aAAa,KAAK,CAAC,CAAC;YAAE,OAAO,KAAK,CAAA;QACtC,OAAO,UAAU,CAAC,KAAK,EAAE,aAAa,GAAG,CAAC,CAAC,CAAA;IAC7C,CAAC,EACD;QACE,OAAO,EAAE,4CAA4C;KACtD,CACF;IACD,CAAC;SACE,MAAM,EAAE;SACR,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,UAAU,CAAC,CAAC,CAAC,KAAK,EAAE,CAAC,OAAO,EAAE;QACrD,OAAO,EAAE,4BAA4B;KACtC,CAAC;SACD,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,UAAU,CAAC,KAAK,EAAE,CAAC,CAAC,EAAE;QACvC,OAAO,EAAE,8BAA8B;KACxC,CAAC;CACL,CAAC,CAAA;AAEF;;GAEG;AAC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AC,CAAC,CAAC,CAAA;YAE1B,MAAM,SAAS,GAAG,OAAO,CAAC,EAAE,CAAC,UAAU,CAAC,GAAG,CAAC;gBAC1C,CAAC,CAAC,GAAG,GAAG,GAAG,OAAO,CAAC,EAAE,EAAE;gBACvB,CAAC,CAAC,OAAO,CAAC,EAAE,CAAA;YAEd,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,SAAS,CAAC,EAAE,CAAC;gBAC5B,OAAO,CAAC,GAAG,CAAC,SAAS,CAAC,CAAA;YACxB,CAAC;iBAAM,CAAC;gBACN,GAAG,CAAC,QAAQ,CAAC;oBACX,IAAI,EAAE,CAAC,CAAC,YAAY,CAAC,MAAM;oBAC3B,OAAO,EAAE,yBAAyB,OAAO,CAAC,EAAE,yBAAyB;oBACrE,IAAI,EAAE,CAAC,SAAS,EAAE,CAAC,EAAE,IAAI,CAAC;iBAC3B,CAAC,CAAA;YACJ,CAAC;QACH,CAAC;IACH,CAAC;AACH,CAAC,CAAC,CAAA","sourcesContent":["import { z } from 'zod'\nimport { Did, didSchema } from './did.js'\nimport { isFragment } from './lib/uri.js'\n\n/**\n * RFC3968 compliant URI\n *\n * @see {@link https://www.rfc-editor.org/rfc/rfc3986}\n */\nconst rfc3968UriSchema = z.string().url('RFC3968 compliant URI')\n\nconst didControllerSchema = z.union([didSchema, z.array(didSchema)])\n\n/**\n * @note this schema is too permissive\n */\nconst didRelativeUriSchema = z.union([\n rfc3968UriSchema.refine(\n (value) => {\n const fragmentIndex = value.indexOf('#')\n if (fragmentIndex === -1) return false\n return isFragment(value, fragmentIndex + 1)\n },\n {\n message: 'Missing or invalid fragment in RFC3968 URI',\n },\n ),\n z\n .string()\n .refine((value) => value.charCodeAt(0) === 35 /* # */, {\n message: 'Fragment must start with #',\n })\n .refine((value) => isFragment(value, 1), {\n message: 'Invalid char in URI fragment',\n }),\n])\n\n/**\n * @see {@link https://www.w3.org/TR/did-1.0/#verification-material Verification Material}\n */\nconst didVerificationMethodSchema = z.object({\n id: didRelativeUriSchema,\n type: z.string().min(1),\n controller: didControllerSchema,\n publicKeyJwk: z.record(z.string(), z.unknown()).optional(),\n publicKeyMultibase: z.string().optional(),\n})\n\n/**\n * The value of the id property MUST be a URI conforming to [RFC3986]. A\n * conforming producer MUST NOT produce multiple service entries with the same\n * id. A conforming consumer MUST produce an error if it detects multiple\n * service entries with the same id.\n *\n * @note Normally, only rfc3968UriSchema should be allowed here. However, the\n * did:plc uses relative URI. For this reason, we also allow relative URIs\n * here.\n */\nconst didServiceIdSchema = didRelativeUriSchema\n\n/**\n * The value of the type property MUST be a string or a set of strings. In order\n * to maximize interoperability, the service type and its associated properties\n * SHOULD be registered in the DID Specification Registries\n * [DID-SPEC-REGISTRIES].\n */\nconst didServiceTypeSchema = z.union([z.string(), z.array(z.string())])\n\n/**\n * The value of the serviceEndpoint property MUST be a string, a map, or a set\n * composed of one or more strings and/or maps. All string values MUST be valid\n * URIs conforming to [RFC3986] and normalized according to the Normalization\n * and Comparison rules in RFC3986 and to any normalization rules in its\n * applicable URI scheme specification.\n */\nconst didServiceEndpointSchema = z.union([\n rfc3968UriSchema,\n z.record(z.string(), rfc3968UriSchema),\n z\n .array(z.union([rfc3968UriSchema, z.record(z.string(), rfc3968UriSchema)]))\n .nonempty(),\n])\n\n/**\n * Each service map MUST contain id, type, and serviceEndpoint properties.\n * @see {@link https://www.w3.org/TR/did-core/#services}\n */\nconst didServiceSchema = z.object({\n id: didServiceIdSchema,\n type: didServiceTypeSchema,\n serviceEndpoint: didServiceEndpointSchema,\n})\n\nexport type DidService = z.infer<typeof didServiceSchema>\n\n/**\n * @see {@link https://www.w3.org/TR/did-1.0/#referring-to-verification-methods Referring to Verification Methods}\n */\nconst verificationMethodReference = z.union([\n //\n didRelativeUriSchema,\n didVerificationMethodSchema,\n])\n\n/**\n * @note This schema is incomplete\n * @see {@link https://www.w3.org/TR/did-core/#production-0}\n */\nexport const didDocumentSchema = z.object({\n '@context': z\n .union([\n z.literal('https://www.w3.org/ns/did/v1'),\n z\n .array(z.string().url())\n .nonempty()\n .refine((data) => data[0] === 'https://www.w3.org/ns/did/v1', {\n message: 'First @context must be https://www.w3.org/ns/did/v1',\n }),\n ])\n // @NOTE @context is required by producers, but optional for consumers.\n .optional(),\n id: didSchema,\n controller: didControllerSchema.optional(),\n alsoKnownAs: z.array(rfc3968UriSchema).optional(),\n service: z.array(didServiceSchema).optional(),\n authentication: z.array(verificationMethodReference).optional(),\n verificationMethod: z.array(didVerificationMethodSchema).optional(),\n})\n\nexport type DidDocument<Method extends string = string> = z.infer<\n typeof didDocumentSchema\n> & { id: Did<Method> }\n\n// @TODO: add other refinements ?\nexport const didDocumentValidator = didDocumentSchema\n // Ensure that every service id is unique\n .superRefine(({ id: did, service }, ctx) => {\n if (service) {\n const visited = new Set()\n\n for (let i = 0; i < service.length; i++) {\n const current = service[i]\n\n const serviceId = current.id.startsWith('#')\n ? `${did}${current.id}`\n : current.id\n\n if (!visited.has(serviceId)) {\n visited.add(serviceId)\n } else {\n ctx.addIssue({\n code: z.ZodIssueCode.custom,\n message: `Duplicate service id (${current.id}) found in the document`,\n path: ['service', i, 'id'],\n })\n }\n }\n }\n })\n"]}
package/dist/did-error.js CHANGED
@@ -1,27 +1,9 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.InvalidDidError = exports.DidError = void 0;
4
- class DidError extends Error {
1
+ export class DidError extends Error {
5
2
  constructor(did, message, code, status = 400, cause) {
6
3
  super(message, { cause });
7
- Object.defineProperty(this, "did", {
8
- enumerable: true,
9
- configurable: true,
10
- writable: true,
11
- value: did
12
- });
13
- Object.defineProperty(this, "code", {
14
- enumerable: true,
15
- configurable: true,
16
- writable: true,
17
- value: code
18
- });
19
- Object.defineProperty(this, "status", {
20
- enumerable: true,
21
- configurable: true,
22
- writable: true,
23
- value: status
24
- });
4
+ this.did = did;
5
+ this.code = code;
6
+ this.status = status;
25
7
  }
26
8
  /**
27
9
  * For compatibility with error handlers in common HTTP frameworks.
@@ -48,11 +30,9 @@ class DidError extends Error {
48
30
  return new DidError(did, message, 'did-unknown-error', status, cause);
49
31
  }
50
32
  }
51
- exports.DidError = DidError;
52
- class InvalidDidError extends DidError {
33
+ export class InvalidDidError extends DidError {
53
34
  constructor(did, message, cause) {
54
35
  super(did, message, 'did-invalid', 400, cause);
55
36
  }
56
37
  }
57
- exports.InvalidDidError = InvalidDidError;
58
38
  //# sourceMappingURL=did-error.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"did-error.js","sourceRoot":"","sources":["../src/did-error.ts"],"names":[],"mappings":";;;AAAA,MAAa,QAAS,SAAQ,KAAK;IACjC,YACkB,GAAW,EAC3B,OAAe,EACC,IAAY,EACZ,SAAS,GAAG,EAC5B,KAAe;QAEf,KAAK,CAAC,OAAO,EAAE,EAAE,KAAK,EAAE,CAAC,CAAA;QANzB;;;;mBAAgB,GAAG;WAAQ;QAE3B;;;;mBAAgB,IAAI;WAAQ;QAC5B;;;;mBAAgB,MAAM;WAAM;IAI9B,CAAC;IAED;;OAEG;IACH,IAAI,UAAU;QACZ,OAAO,IAAI,CAAC,MAAM,CAAA;IACpB,CAAC;IAEQ,QAAQ;QACf,OAAO,GAAG,IAAI,CAAC,WAAW,CAAC,IAAI,IAAI,IAAI,CAAC,IAAI,KAAK,IAAI,CAAC,GAAG,MAAM,IAAI,CAAC,OAAO,EAAE,CAAA;IAC/E,CAAC;IAED,MAAM,CAAC,IAAI,CAAC,KAAc,EAAE,GAAW;QACrC,IAAI,KAAK,YAAY,QAAQ,EAAE,CAAC;YAC9B,OAAO,KAAK,CAAA;QACd,CAAC;QAED,MAAM,OAAO,GACX,KAAK,YAAY,KAAK;YACpB,CAAC,CAAC,KAAK,CAAC,OAAO;YACf,CAAC,CAAC,OAAO,KAAK,KAAK,QAAQ;gBACzB,CAAC,CAAC,KAAK;gBACP,CAAC,CAAC,2BAA2B,CAAA;QAEnC,MAAM,MAAM,GACV,CAAC,OAAO,KAAK,EAAE,CAAC,YAAY,CAAC,KAAK,QAAQ;YACxC,CAAC,CAAC,KAAK,CAAC,YAAY,CAAC;YACrB,CAAC,CAAC,SAAS,CAAC;YACd,CAAC,OAAO,KAAK,EAAE,CAAC,QAAQ,CAAC,KAAK,QAAQ,CAAC,CAAC,CAAC,KAAK,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,CAAA;QAEvE,OAAO,IAAI,QAAQ,CAAC,GAAG,EAAE,OAAO,EAAE,mBAAmB,EAAE,MAAM,EAAE,KAAK,CAAC,CAAA;IACvE,CAAC;CACF;AA1CD,4BA0CC;AAED,MAAa,eAAgB,SAAQ,QAAQ;IAC3C,YAAY,GAAW,EAAE,OAAe,EAAE,KAAe;QACvD,KAAK,CAAC,GAAG,EAAE,OAAO,EAAE,aAAa,EAAE,GAAG,EAAE,KAAK,CAAC,CAAA;IAChD,CAAC;CACF;AAJD,0CAIC","sourcesContent":["export class DidError extends Error {\n constructor(\n public readonly did: string,\n message: string,\n public readonly code: string,\n public readonly status = 400,\n cause?: unknown,\n ) {\n super(message, { cause })\n }\n\n /**\n * For compatibility with error handlers in common HTTP frameworks.\n */\n get statusCode() {\n return this.status\n }\n\n override toString() {\n return `${this.constructor.name} ${this.code} (${this.did}): ${this.message}`\n }\n\n static from(cause: unknown, did: string): DidError {\n if (cause instanceof DidError) {\n return cause\n }\n\n const message =\n cause instanceof Error\n ? cause.message\n : typeof cause === 'string'\n ? cause\n : 'An unknown error occurred'\n\n const status =\n (typeof cause?.['statusCode'] === 'number'\n ? cause['statusCode']\n : undefined) ??\n (typeof cause?.['status'] === 'number' ? cause['status'] : undefined)\n\n return new DidError(did, message, 'did-unknown-error', status, cause)\n }\n}\n\nexport class InvalidDidError extends DidError {\n constructor(did: string, message: string, cause?: unknown) {\n super(did, message, 'did-invalid', 400, cause)\n }\n}\n"]}
1
+ {"version":3,"file":"did-error.js","sourceRoot":"","sources":["../src/did-error.ts"],"names":[],"mappings":"AAAA,MAAM,OAAO,QAAS,SAAQ,KAA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3C,YAAY,GAAW,EAAE,OAAe,EAAE,KAAe;QACvD,KAAK,CAAC,GAAG,EAAE,OAAO,EAAE,aAAa,EAAE,GAAG,EAAE,KAAK,CAAC,CAAA;IAChD,CAAC;CACF","sourcesContent":["export class DidError extends Error {\n constructor(\n public readonly did: string,\n message: string,\n public readonly code: string,\n public readonly status = 400,\n cause?: unknown,\n ) {\n super(message, { cause })\n }\n\n /**\n * For compatibility with error handlers in common HTTP frameworks.\n */\n get statusCode() {\n return this.status\n }\n\n override toString() {\n return `${this.constructor.name} ${this.code} (${this.did}): ${this.message}`\n }\n\n static from(cause: unknown, did: string): DidError {\n if (cause instanceof DidError) {\n return cause\n }\n\n const message =\n cause instanceof Error\n ? cause.message\n : typeof cause === 'string'\n ? cause\n : 'An unknown error occurred'\n\n const status =\n (typeof cause?.['statusCode'] === 'number'\n ? cause['statusCode']\n : undefined) ??\n (typeof cause?.['status'] === 'number' ? cause['status'] : undefined)\n\n return new DidError(did, message, 'did-unknown-error', status, cause)\n }\n}\n\nexport class InvalidDidError extends DidError {\n constructor(did: string, message: string, cause?: unknown) {\n super(did, message, 'did-invalid', 400, cause)\n }\n}\n"]}
package/dist/did.js CHANGED
@@ -1,24 +1,15 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.didSchema = exports.DID_PREFIX = void 0;
4
- exports.assertDidMethod = assertDidMethod;
5
- exports.extractDidMethod = extractDidMethod;
6
- exports.assertDidMsid = assertDidMsid;
7
- exports.assertDid = assertDid;
8
- exports.isDid = isDid;
9
- exports.asDid = asDid;
10
- const zod_1 = require("zod");
11
- const did_error_js_1 = require("./did-error.js");
1
+ import { z } from 'zod';
2
+ import { DidError, InvalidDidError } from './did-error.js';
12
3
  const DID_PREFIX = 'did:';
13
- exports.DID_PREFIX = DID_PREFIX;
14
4
  const DID_PREFIX_LENGTH = DID_PREFIX.length;
5
+ export { DID_PREFIX };
15
6
  /**
16
7
  * DID Method-name check function.
17
8
  *
18
9
  * Check if the input is a valid DID method name, at the position between
19
10
  * `start` (inclusive) and `end` (exclusive).
20
11
  */
21
- function assertDidMethod(input, start = 0, end = input.length) {
12
+ export function assertDidMethod(input, start = 0, end = input.length) {
22
13
  if (!Number.isFinite(end) ||
23
14
  !Number.isFinite(start) ||
24
15
  end < start ||
@@ -26,7 +17,7 @@ function assertDidMethod(input, start = 0, end = input.length) {
26
17
  throw new TypeError('Invalid start or end position');
27
18
  }
28
19
  if (end === start) {
29
- throw new did_error_js_1.InvalidDidError(input, `Empty method name`);
20
+ throw new InvalidDidError(input, `Empty method name`);
30
21
  }
31
22
  let c;
32
23
  for (let i = start; i < end; i++) {
@@ -34,14 +25,14 @@ function assertDidMethod(input, start = 0, end = input.length) {
34
25
  if ((c < 0x61 || c > 0x7a) && // a-z
35
26
  (c < 0x30 || c > 0x39) // 0-9
36
27
  ) {
37
- throw new did_error_js_1.InvalidDidError(input, `Invalid character at position ${i} in DID method name`);
28
+ throw new InvalidDidError(input, `Invalid character at position ${i} in DID method name`);
38
29
  }
39
30
  }
40
31
  }
41
32
  /**
42
33
  * This method assumes the input is a valid Did
43
34
  */
44
- function extractDidMethod(did) {
35
+ export function extractDidMethod(did) {
45
36
  const msidSep = did.indexOf(':', DID_PREFIX_LENGTH);
46
37
  const method = did.slice(DID_PREFIX_LENGTH, msidSep);
47
38
  return method;
@@ -52,7 +43,7 @@ function extractDidMethod(did) {
52
43
  * Check if the input is a valid DID method-specific identifier, at the position
53
44
  * between `start` (inclusive) and `end` (exclusive).
54
45
  */
55
- function assertDidMsid(input, start = 0, end = input.length) {
46
+ export function assertDidMsid(input, start = 0, end = input.length) {
56
47
  if (!Number.isFinite(end) ||
57
48
  !Number.isFinite(start) ||
58
49
  end < start ||
@@ -60,7 +51,7 @@ function assertDidMsid(input, start = 0, end = input.length) {
60
51
  throw new TypeError('Invalid start or end position');
61
52
  }
62
53
  if (end === start) {
63
- throw new did_error_js_1.InvalidDidError(input, `DID method-specific id must not be empty`);
54
+ throw new InvalidDidError(input, `DID method-specific id must not be empty`);
64
55
  }
65
56
  let c;
66
57
  for (let i = start; i < end; i++) {
@@ -77,7 +68,7 @@ function assertDidMsid(input, start = 0, end = input.length) {
77
68
  // ":"
78
69
  if (c === 0x3a) {
79
70
  if (i === end - 1) {
80
- throw new did_error_js_1.InvalidDidError(input, `DID cannot end with ":"`);
71
+ throw new InvalidDidError(input, `DID cannot end with ":"`);
81
72
  }
82
73
  continue;
83
74
  }
@@ -85,58 +76,58 @@ function assertDidMsid(input, start = 0, end = input.length) {
85
76
  if (c === 0x25) {
86
77
  c = input.charCodeAt(++i);
87
78
  if ((c < 0x30 || c > 0x39) && (c < 0x41 || c > 0x46)) {
88
- throw new did_error_js_1.InvalidDidError(input, `Invalid pct-encoded character at position ${i}`);
79
+ throw new InvalidDidError(input, `Invalid pct-encoded character at position ${i}`);
89
80
  }
90
81
  c = input.charCodeAt(++i);
91
82
  if ((c < 0x30 || c > 0x39) && (c < 0x41 || c > 0x46)) {
92
- throw new did_error_js_1.InvalidDidError(input, `Invalid pct-encoded character at position ${i}`);
83
+ throw new InvalidDidError(input, `Invalid pct-encoded character at position ${i}`);
93
84
  }
94
85
  // There must always be 2 HEXDIG after a "%"
95
86
  if (i >= end) {
96
- throw new did_error_js_1.InvalidDidError(input, `Incomplete pct-encoded character at position ${i - 2}`);
87
+ throw new InvalidDidError(input, `Incomplete pct-encoded character at position ${i - 2}`);
97
88
  }
98
89
  continue;
99
90
  }
100
- throw new did_error_js_1.InvalidDidError(input, `Disallowed character in DID at position ${i}`);
91
+ throw new InvalidDidError(input, `Disallowed character in DID at position ${i}`);
101
92
  }
102
93
  }
103
94
  }
104
- function assertDid(input) {
95
+ export function assertDid(input) {
105
96
  if (typeof input !== 'string') {
106
- throw new did_error_js_1.InvalidDidError(typeof input, `DID must be a string`);
97
+ throw new InvalidDidError(typeof input, `DID must be a string`);
107
98
  }
108
99
  const { length } = input;
109
100
  if (length > 2048) {
110
- throw new did_error_js_1.InvalidDidError(input, `DID is too long (2048 chars max)`);
101
+ throw new InvalidDidError(input, `DID is too long (2048 chars max)`);
111
102
  }
112
103
  if (!input.startsWith(DID_PREFIX)) {
113
- throw new did_error_js_1.InvalidDidError(input, `DID requires "${DID_PREFIX}" prefix`);
104
+ throw new InvalidDidError(input, `DID requires "${DID_PREFIX}" prefix`);
114
105
  }
115
106
  const idSep = input.indexOf(':', DID_PREFIX_LENGTH);
116
107
  if (idSep === -1) {
117
- throw new did_error_js_1.InvalidDidError(input, `Missing colon after method name`);
108
+ throw new InvalidDidError(input, `Missing colon after method name`);
118
109
  }
119
110
  assertDidMethod(input, DID_PREFIX_LENGTH, idSep);
120
111
  assertDidMsid(input, idSep + 1, length);
121
112
  }
122
- function isDid(input) {
113
+ export function isDid(input) {
123
114
  try {
124
115
  assertDid(input);
125
116
  return true;
126
117
  }
127
118
  catch (err) {
128
- if (err instanceof did_error_js_1.DidError) {
119
+ if (err instanceof DidError) {
129
120
  return false;
130
121
  }
131
122
  // Unexpected TypeError (should never happen)
132
123
  throw err;
133
124
  }
134
125
  }
135
- function asDid(input) {
126
+ export function asDid(input) {
136
127
  assertDid(input);
137
128
  return input;
138
129
  }
139
- exports.didSchema = zod_1.z
130
+ export const didSchema = z
140
131
  .string()
141
132
  .superRefine((value, ctx) => {
142
133
  try {
@@ -145,7 +136,7 @@ exports.didSchema = zod_1.z
145
136
  }
146
137
  catch (err) {
147
138
  ctx.addIssue({
148
- code: zod_1.z.ZodIssueCode.custom,
139
+ code: z.ZodIssueCode.custom,
149
140
  message: err instanceof Error ? err.message : 'Unexpected error',
150
141
  });
151
142
  return false;
package/dist/did.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"did.js","sourceRoot":"","sources":["../src/did.ts"],"names":[],"mappings":";;;AAqFA,0CA8BC;AAKD,4CAIC;AAQD,sCA2EC;AAED,8BAqBC;AAED,sBAYC;AAED,sBAGC;AAzPD,6BAAuB;AACvB,iDAA0D;AAE1D,MAAM,UAAU,GAAG,MAAM,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AC,GAAG,CAAC,OAAO,CAAC,CAAC,CAAC,kBAAkB;SACjE,CAAC,CAAA;QACF,OAAO,KAAK,CAAA;IACd,CAAC;AACH,CAAC,CAAC,CAAA","sourcesContent":["import { z } from 'zod'\nimport { DidError, InvalidDidError } from './did-error.js'\n\nconst DID_PREFIX = 'did:'\nconst DID_PREFIX_LENGTH = DID_PREFIX.length\nexport { DID_PREFIX }\n\n/**\n * Type representation of a Did, with method.\n *\n * ```bnf\n * did = \"did:\" method-name \":\" method-specific-id\n * method-name = 1*method-char\n * method-char = %x61-7A / DIGIT\n * method-specific-id = *( *idchar \":\" ) 1*idchar\n * idchar = ALPHA / DIGIT / \".\" / \"-\" / \"_\" / pct-encoded\n * pct-encoded = \"%\" HEXDIG HEXDIG\n * ```\n *\n * @example\n * ```ts\n * type DidWeb = Did<'web'> // `did:web:${string}`\n * type DidCustom = Did<'web' | 'plc'> // `did:${'web' | 'plc'}:${string}`\n * type DidNever = Did<' invalid 🥴 '> // never\n * type DidFoo = Did<'foo' | ' invalid 🥴 '> // `did:foo:${string}`\n * ```\n *\n * @see {@link https://www.w3.org/TR/did-core/#did-syntax}\n */\nexport type Did<M extends string = string> = `did:${AsDidMethod<M>}:${string}`\n\n/**\n * DID Method\n */\nexport type AsDidMethod<M> = string extends M\n ? string // can't know...\n : AsDidMethodInternal<M, ''>\n\ntype AlphanumericChar = DigitChar | LowerAlphaChar\ntype DigitChar = '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9'\ntype LowerAlphaChar =\n | 'a'\n | 'b'\n | 'c'\n | 'd'\n | 'e'\n | 'f'\n | 'g'\n | 'h'\n | 'i'\n | 'j'\n | 'k'\n | 'l'\n | 'm'\n | 'n'\n | 'o'\n | 'p'\n | 'q'\n | 'r'\n | 's'\n | 't'\n | 'u'\n | 'v'\n | 'w'\n | 'x'\n | 'y'\n | 'z'\n\ntype AsDidMethodInternal<\n S,\n Acc extends string,\n> = S extends `${infer H}${infer T}`\n ? H extends AlphanumericChar\n ? AsDidMethodInternal<T, `${Acc}${H}`>\n : never\n : Acc extends ''\n ? never\n : Acc\n\n/**\n * DID Method-name check function.\n *\n * Check if the input is a valid DID method name, at the position between\n * `start` (inclusive) and `end` (exclusive).\n */\nexport function assertDidMethod(\n input: string,\n start = 0,\n end = input.length,\n): void {\n if (\n !Number.isFinite(end) ||\n !Number.isFinite(start) ||\n end < start ||\n end > input.length\n ) {\n throw new TypeError('Invalid start or end position')\n }\n if (end === start) {\n throw new InvalidDidError(input, `Empty method name`)\n }\n\n let c: number\n for (let i = start; i < end; i++) {\n c = input.charCodeAt(i)\n if (\n (c < 0x61 || c > 0x7a) && // a-z\n (c < 0x30 || c > 0x39) // 0-9\n ) {\n throw new InvalidDidError(\n input,\n `Invalid character at position ${i} in DID method name`,\n )\n }\n }\n}\n\n/**\n * This method assumes the input is a valid Did\n */\nexport function extractDidMethod<D extends Did>(did: D) {\n const msidSep = did.indexOf(':', DID_PREFIX_LENGTH)\n const method = did.slice(DID_PREFIX_LENGTH, msidSep)\n return method as D extends Did<infer M> ? M : string\n}\n\n/**\n * DID Method-specific identifier check function.\n *\n * Check if the input is a valid DID method-specific identifier, at the position\n * between `start` (inclusive) and `end` (exclusive).\n */\nexport function assertDidMsid(\n input: string,\n start = 0,\n end = input.length,\n): void {\n if (\n !Number.isFinite(end) ||\n !Number.isFinite(start) ||\n end < start ||\n end > input.length\n ) {\n throw new TypeError('Invalid start or end position')\n }\n if (end === start) {\n throw new InvalidDidError(input, `DID method-specific id must not be empty`)\n }\n\n let c: number\n for (let i = start; i < end; i++) {\n c = input.charCodeAt(i)\n\n // Check for frequent chars first\n if (\n (c < 0x61 || c > 0x7a) && // a-z\n (c < 0x41 || c > 0x5a) && // A-Z\n (c < 0x30 || c > 0x39) && // 0-9\n c !== 0x2e && // .\n c !== 0x2d && // -\n c !== 0x5f // _\n ) {\n // Less frequent chars are checked here\n\n // \":\"\n if (c === 0x3a) {\n if (i === end - 1) {\n throw new InvalidDidError(input, `DID cannot end with \":\"`)\n }\n continue\n }\n\n // pct-encoded\n if (c === 0x25) {\n c = input.charCodeAt(++i)\n if ((c < 0x30 || c > 0x39) && (c < 0x41 || c > 0x46)) {\n throw new InvalidDidError(\n input,\n `Invalid pct-encoded character at position ${i}`,\n )\n }\n\n c = input.charCodeAt(++i)\n if ((c < 0x30 || c > 0x39) && (c < 0x41 || c > 0x46)) {\n throw new InvalidDidError(\n input,\n `Invalid pct-encoded character at position ${i}`,\n )\n }\n\n // There must always be 2 HEXDIG after a \"%\"\n if (i >= end) {\n throw new InvalidDidError(\n input,\n `Incomplete pct-encoded character at position ${i - 2}`,\n )\n }\n\n continue\n }\n\n throw new InvalidDidError(\n input,\n `Disallowed character in DID at position ${i}`,\n )\n }\n }\n}\n\nexport function assertDid(input: unknown): asserts input is Did {\n if (typeof input !== 'string') {\n throw new InvalidDidError(typeof input, `DID must be a string`)\n }\n\n const { length } = input\n if (length > 2048) {\n throw new InvalidDidError(input, `DID is too long (2048 chars max)`)\n }\n\n if (!input.startsWith(DID_PREFIX)) {\n throw new InvalidDidError(input, `DID requires \"${DID_PREFIX}\" prefix`)\n }\n\n const idSep = input.indexOf(':', DID_PREFIX_LENGTH)\n if (idSep === -1) {\n throw new InvalidDidError(input, `Missing colon after method name`)\n }\n\n assertDidMethod(input, DID_PREFIX_LENGTH, idSep)\n assertDidMsid(input, idSep + 1, length)\n}\n\nexport function isDid(input: unknown): input is Did {\n try {\n assertDid(input)\n return true\n } catch (err) {\n if (err instanceof DidError) {\n return false\n }\n\n // Unexpected TypeError (should never happen)\n throw err\n }\n}\n\nexport function asDid<T>(input: T) {\n assertDid(input)\n return input\n}\n\nexport const didSchema = z\n .string()\n .superRefine((value: string, ctx: z.RefinementCtx): value is Did => {\n try {\n assertDid(value)\n return true\n } catch (err) {\n ctx.addIssue({\n code: z.ZodIssueCode.custom,\n message: err instanceof Error ? err.message : 'Unexpected error',\n })\n return false\n }\n })\n"]}
1
+ {"version":3,"file":"did.js","sourceR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,SAAS,CAAC,KAAK,CAAC,CAAA;QAChB,OAAO,IAAI,CAAA;IACb,CAAC;IAAC,OAAO,GAAG,EAAE,CAAC;QACb,GAAG,CAAC,QAAQ,CAAC;YACX,IAAI,EAAE,CAAC,CAAC,YAAY,CAAC,MAAM;YAC3B,OAAO,EAAE,GAAG,YAAY,KAAK,CAAC,CAAC,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC,CAAC,kBAAkB;SACjE,CAAC,CAAA;QACF,OAAO,KAAK,CAAA;IACd,CAAC;AACH,CAAC,CAAC,CAAA","sourcesContent":["import { z } from 'zod'\nimport { DidError, InvalidDidError } from './did-error.js'\n\nconst DID_PREFIX = 'did:'\nconst DID_PREFIX_LENGTH = DID_PREFIX.length\nexport { DID_PREFIX }\n\n/**\n * Type representation of a Did, with method.\n *\n * ```bnf\n * did = \"did:\" method-name \":\" method-specific-id\n * method-name = 1*method-char\n * method-char = %x61-7A / DIGIT\n * method-specific-id = *( *idchar \":\" ) 1*idchar\n * idchar = ALPHA / DIGIT / \".\" / \"-\" / \"_\" / pct-encoded\n * pct-encoded = \"%\" HEXDIG HEXDIG\n * ```\n *\n * @example\n * ```ts\n * type DidWeb = Did<'web'> // `did:web:${string}`\n * type DidCustom = Did<'web' | 'plc'> // `did:${'web' | 'plc'}:${string}`\n * type DidNever = Did<' invalid 🥴 '> // never\n * type DidFoo = Did<'foo' | ' invalid 🥴 '> // `did:foo:${string}`\n * ```\n *\n * @see {@link https://www.w3.org/TR/did-core/#did-syntax}\n */\nexport type Did<M extends string = string> = `did:${AsDidMethod<M>}:${string}`\n\n/**\n * DID Method\n */\nexport type AsDidMethod<M> = string extends M\n ? string // can't know...\n : AsDidMethodInternal<M, ''>\n\ntype AlphanumericChar = DigitChar | LowerAlphaChar\ntype DigitChar = '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9'\ntype LowerAlphaChar =\n | 'a'\n | 'b'\n | 'c'\n | 'd'\n | 'e'\n | 'f'\n | 'g'\n | 'h'\n | 'i'\n | 'j'\n | 'k'\n | 'l'\n | 'm'\n | 'n'\n | 'o'\n | 'p'\n | 'q'\n | 'r'\n | 's'\n | 't'\n | 'u'\n | 'v'\n | 'w'\n | 'x'\n | 'y'\n | 'z'\n\ntype AsDidMethodInternal<\n S,\n Acc extends string,\n> = S extends `${infer H}${infer T}`\n ? H extends AlphanumericChar\n ? AsDidMethodInternal<T, `${Acc}${H}`>\n : never\n : Acc extends ''\n ? never\n : Acc\n\n/**\n * DID Method-name check function.\n *\n * Check if the input is a valid DID method name, at the position between\n * `start` (inclusive) and `end` (exclusive).\n */\nexport function assertDidMethod(\n input: string,\n start = 0,\n end = input.length,\n): void {\n if (\n !Number.isFinite(end) ||\n !Number.isFinite(start) ||\n end < start ||\n end > input.length\n ) {\n throw new TypeError('Invalid start or end position')\n }\n if (end === start) {\n throw new InvalidDidError(input, `Empty method name`)\n }\n\n let c: number\n for (let i = start; i < end; i++) {\n c = input.charCodeAt(i)\n if (\n (c < 0x61 || c > 0x7a) && // a-z\n (c < 0x30 || c > 0x39) // 0-9\n ) {\n throw new InvalidDidError(\n input,\n `Invalid character at position ${i} in DID method name`,\n )\n }\n }\n}\n\n/**\n * This method assumes the input is a valid Did\n */\nexport function extractDidMethod<D extends Did>(did: D) {\n const msidSep = did.indexOf(':', DID_PREFIX_LENGTH)\n const method = did.slice(DID_PREFIX_LENGTH, msidSep)\n return method as D extends Did<infer M> ? M : string\n}\n\n/**\n * DID Method-specific identifier check function.\n *\n * Check if the input is a valid DID method-specific identifier, at the position\n * between `start` (inclusive) and `end` (exclusive).\n */\nexport function assertDidMsid(\n input: string,\n start = 0,\n end = input.length,\n): void {\n if (\n !Number.isFinite(end) ||\n !Number.isFinite(start) ||\n end < start ||\n end > input.length\n ) {\n throw new TypeError('Invalid start or end position')\n }\n if (end === start) {\n throw new InvalidDidError(input, `DID method-specific id must not be empty`)\n }\n\n let c: number\n for (let i = start; i < end; i++) {\n c = input.charCodeAt(i)\n\n // Check for frequent chars first\n if (\n (c < 0x61 || c > 0x7a) && // a-z\n (c < 0x41 || c > 0x5a) && // A-Z\n (c < 0x30 || c > 0x39) && // 0-9\n c !== 0x2e && // .\n c !== 0x2d && // -\n c !== 0x5f // _\n ) {\n // Less frequent chars are checked here\n\n // \":\"\n if (c === 0x3a) {\n if (i === end - 1) {\n throw new InvalidDidError(input, `DID cannot end with \":\"`)\n }\n continue\n }\n\n // pct-encoded\n if (c === 0x25) {\n c = input.charCodeAt(++i)\n if ((c < 0x30 || c > 0x39) && (c < 0x41 || c > 0x46)) {\n throw new InvalidDidError(\n input,\n `Invalid pct-encoded character at position ${i}`,\n )\n }\n\n c = input.charCodeAt(++i)\n if ((c < 0x30 || c > 0x39) && (c < 0x41 || c > 0x46)) {\n throw new InvalidDidError(\n input,\n `Invalid pct-encoded character at position ${i}`,\n )\n }\n\n // There must always be 2 HEXDIG after a \"%\"\n if (i >= end) {\n throw new InvalidDidError(\n input,\n `Incomplete pct-encoded character at position ${i - 2}`,\n )\n }\n\n continue\n }\n\n throw new InvalidDidError(\n input,\n `Disallowed character in DID at position ${i}`,\n )\n }\n }\n}\n\nexport function assertDid(input: unknown): asserts input is Did {\n if (typeof input !== 'string') {\n throw new InvalidDidError(typeof input, `DID must be a string`)\n }\n\n const { length } = input\n if (length > 2048) {\n throw new InvalidDidError(input, `DID is too long (2048 chars max)`)\n }\n\n if (!input.startsWith(DID_PREFIX)) {\n throw new InvalidDidError(input, `DID requires \"${DID_PREFIX}\" prefix`)\n }\n\n const idSep = input.indexOf(':', DID_PREFIX_LENGTH)\n if (idSep === -1) {\n throw new InvalidDidError(input, `Missing colon after method name`)\n }\n\n assertDidMethod(input, DID_PREFIX_LENGTH, idSep)\n assertDidMsid(input, idSep + 1, length)\n}\n\nexport function isDid(input: unknown): input is Did {\n try {\n assertDid(input)\n return true\n } catch (err) {\n if (err instanceof DidError) {\n return false\n }\n\n // Unexpected TypeError (should never happen)\n throw err\n }\n}\n\nexport function asDid<T>(input: T) {\n assertDid(input)\n return input\n}\n\nexport const didSchema = z\n .string()\n .superRefine((value: string, ctx: z.RefinementCtx): value is Did => {\n try {\n assertDid(value)\n return true\n } catch (err) {\n ctx.addIssue({\n code: z.ZodIssueCode.custom,\n message: err instanceof Error ? err.message : 'Unexpected error',\n })\n return false\n }\n })\n"]}
package/dist/index.js CHANGED
@@ -1,23 +1,7 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
- };
16
- Object.defineProperty(exports, "__esModule", { value: true });
17
- __exportStar(require("./atproto.js"), exports);
18
- __exportStar(require("./did-document.js"), exports);
19
- __exportStar(require("./did-error.js"), exports);
20
- __exportStar(require("./did.js"), exports);
21
- __exportStar(require("./methods.js"), exports);
22
- __exportStar(require("./utils.js"), exports);
1
+ export * from './atproto.js';
2
+ export * from './did-document.js';
3
+ export * from './did-error.js';
4
+ export * from './did.js';
5
+ export * from './methods.js';
6
+ export * from './utils.js';
23
7
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,+CAA4B;AAC5B,oDAAiC;AACjC,iDAA8B;AAC9B,2CAAwB;AACxB,+CAA4B;AAC5B,6CAA0B","sourcesContent":["export * from './atproto.js'\nexport * from './did-document.js'\nexport * from './did-error.js'\nexport * from './did.js'\nexport * from './methods.js'\nexport * from './utils.js'\n"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,cAAc,CAAA;AAC5B,cAAc,mBAAmB,CAAA;AACjC,cAAc,gBAAgB,CAAA;AAC9B,cAAc,UAAU,CAAA;AACxB,cAAc,cAAc,CAAA;AAC5B,cAAc,YAAY,CAAA","sourcesContent":["export * from './atproto.js'\nexport * from './did-document.js'\nexport * from './did-error.js'\nexport * from './did.js'\nexport * from './methods.js'\nexport * from './utils.js'\n"]}
package/dist/lib/uri.js CHANGED
@@ -1,13 +1,8 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.canParse = void 0;
4
- exports.isFragment = isFragment;
5
- exports.isHexDigit = isHexDigit;
6
1
  /**
7
2
  * @see {@link https://www.w3.org/TR/did-1.0/#dfn-did-fragments}
8
3
  * @see {@link https://datatracker.ietf.org/doc/html/rfc3986#section-3.5}
9
4
  */
10
- function isFragment(value, startIdx = 0, endIdx = value.length) {
5
+ export function isFragment(value, startIdx = 0, endIdx = value.length) {
11
6
  let charCode;
12
7
  for (let i = startIdx; i < endIdx; i++) {
13
8
  charCode = value.charCodeAt(i);
@@ -60,13 +55,13 @@ function isFragment(value, startIdx = 0, endIdx = value.length) {
60
55
  }
61
56
  return true;
62
57
  }
63
- function isHexDigit(code) {
58
+ export function isHexDigit(code) {
64
59
  return ((code >= 48 && code <= 57) || // 0-9
65
60
  (code >= 65 && code <= 70) || // A-F
66
61
  (code >= 97 && code <= 102) // a-f
67
62
  );
68
63
  }
69
- exports.canParse = URL.canParse?.bind(URL) ??
64
+ export const canParse = URL.canParse?.bind(URL) ??
70
65
  ((url, base) => {
71
66
  try {
72
67
  new URL(url, base);
@@ -1 +1 @@
1
- {"version":3,"file":"uri.js","sourceRoot":"","sources":["../../src/lib/uri.ts"],"names":[],"mappings":";;;AAIA,gCAsDC;AAED,gCAMC;AAlED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;QACb,CAAC;QAAC,MAAM,CAAC;YACP,OAAO,KAAK,CAAA;QACd,CAAC;IACH,CAAC,CAAC,CAAA","sourcesContent":["/**\n * @see {@link https://www.w3.org/TR/did-1.0/#dfn-did-fragments}\n * @see {@link https://datatracker.ietf.org/doc/html/rfc3986#section-3.5}\n */\nexport function isFragment(\n value: string,\n startIdx = 0,\n endIdx = value.length,\n): boolean {\n let charCode: number\n for (let i = startIdx; i < endIdx; i++) {\n charCode = value.charCodeAt(i)\n\n // fragment = *( pchar / \"/\" / \"?\" )\n // pchar = unreserved / pct-encoded / sub-delims / \":\" / \"@\"\n // unreserved = ALPHA / DIGIT / \"-\" / \".\" / \"_\" / \"~\"\n // pct-encoded = \"%\" HEXDIG HEXDIG\n // sub-delims = \"!\" / \"$\" / \"&\" / \"'\" / \"(\" / \")\" / \"*\" / \"+\" / \",\" / \";\" / \"=\"\n if (\n (charCode >= 65 /* A */ && charCode <= 90) /* Z */ ||\n (charCode >= 97 /* a */ && charCode <= 122) /* z */ ||\n (charCode >= 48 /* 0 */ && charCode <= 57) /* 9 */ ||\n charCode === 45 /* \"-\" */ ||\n charCode === 46 /* \".\" */ ||\n charCode === 95 /* \"_\" */ ||\n charCode === 126 /* \"~\" */\n ) {\n // unreserved\n } else if (\n charCode === 33 /* \"!\" */ ||\n charCode === 36 /* \"$\" */ ||\n charCode === 38 /* \"&\" */ ||\n charCode === 39 /* \"'\" */ ||\n charCode === 40 /* \"(\" */ ||\n charCode === 41 /* \")\" */ ||\n charCode === 42 /* \"*\" */ ||\n charCode === 43 /* \"+\" */ ||\n charCode === 44 /* \",\" */ ||\n charCode === 59 /* \";\" */ ||\n charCode === 61 /* \"=\" */\n ) {\n // sub-delims\n } else if (charCode === 58 /* \":\" */ || charCode === 64 /* \"@\" */) {\n // pchar extra\n } else if (charCode === 47 /* \"/\" */ || charCode === 63 /* \"?\" */) {\n // fragment extra\n } else if (charCode === 37 /* \"%\" */) {\n // pct-enc\n if (i + 2 >= endIdx) return false\n if (!isHexDigit(value.charCodeAt(i + 1))) return false\n if (!isHexDigit(value.charCodeAt(i + 2))) return false\n i += 2\n } else {\n return false\n }\n }\n\n return true\n}\n\nexport function isHexDigit(code: number): boolean {\n return (\n (code >= 48 && code <= 57) || // 0-9\n (code >= 65 && code <= 70) || // A-F\n (code >= 97 && code <= 102) // a-f\n )\n}\n\nexport const canParse =\n URL.canParse?.bind(URL) ??\n ((url, base) => {\n try {\n new URL(url, base)\n return true\n } catch {\n return false\n }\n })\n"]}
1
+ {"version":3,"file":"uri.js","sourceRoot":"","sources":["../../src/lib/uri.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,MAAM,UAAU,UAAU,CACxB,KAAa,EACb,QAAQ,GAAG,CAAC,EACZ,MAAM,GAAG,KAAK,CAAC,MAAM;IAErB,IAAI,QAAgB,CAAA;IACpB,KAAK,IAAI,CAAC,GAAG,QAAQ,EAAE,CAAC,GAAG,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;QACvC,QAAQ,GAAG,KAAK,CAAC,UAAU,CAAC,CAAC,CAAC,CAAA;QAE9B,uCAAuC;QACvC,kEAAkE;QAClE,sDAAsD;QACtD,kCAAkC;QAClC,gFAAgF;QAChF,IACE,CAAC,QAAQ,IAAI,EAAE,CAAC,OAAO,IAAI,QAAQ,IAAI,EAAE,CAAC,CAAC,OAAO;YAClD,CAAC,QAAQ,IAAI,EAAE,CAAC,OAAO,IAAI,QAAQ,IAAI,GAAG,CAAC,CAAC,OAAO;YACnD,CAAC,QAAQ,IAAI,EAAE,CAAC,OAAO,IAAI,QAAQ,IAAI,EAAE,CAAC,CAAC,OAAO;YAClD,QAAQ,KAAK,EAAE,CAAC,SAAS;YACzB,QAAQ,KAAK,EAAE,CAAC,SAAS;YACzB,QAAQ,KAAK,EAAE,CAAC,SAAS;YACzB,QAAQ,KAAK,GAAG,CAAC,SAAS,EAC1B,CAAC;YACD,aAAa;QACf,CAAC;aAAM,IACL,QAAQ,KAAK,EAAE,CAAC,SA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AC,IAAI,MAAM;QACpC,CAAC,IAAI,IAAI,EAAE,IAAI,IAAI,IAAI,EAAE,CAAC,IAAI,MAAM;QACpC,CAAC,IAAI,IAAI,EAAE,IAAI,IAAI,IAAI,GAAG,CAAC,CAAC,MAAM;KACnC,CAAA;AACH,CAAC;AAED,MAAM,CAAC,MAAM,QAAQ,GACnB,GAAG,CAAC,QAAQ,EAAE,IAAI,CAAC,GAAG,CAAC;IACvB,CAAC,CAAC,GAAG,EAAE,IAAI,EAAE,EAAE;QACb,IAAI,CAAC;YACH,IAAI,GAAG,CAAC,GAAG,EAAE,IAAI,CAAC,CAAA;YAClB,OAAO,IAAI,CAAA;QACb,CAAC;QAAC,MAAM,CAAC;YACP,OAAO,KAAK,CAAA;QACd,CAAC;IACH,CAAC,CAAC,CAAA","sourcesContent":["/**\n * @see {@link https://www.w3.org/TR/did-1.0/#dfn-did-fragments}\n * @see {@link https://datatracker.ietf.org/doc/html/rfc3986#section-3.5}\n */\nexport function isFragment(\n value: string,\n startIdx = 0,\n endIdx = value.length,\n): boolean {\n let charCode: number\n for (let i = startIdx; i < endIdx; i++) {\n charCode = value.charCodeAt(i)\n\n // fragment = *( pchar / \"/\" / \"?\" )\n // pchar = unreserved / pct-encoded / sub-delims / \":\" / \"@\"\n // unreserved = ALPHA / DIGIT / \"-\" / \".\" / \"_\" / \"~\"\n // pct-encoded = \"%\" HEXDIG HEXDIG\n // sub-delims = \"!\" / \"$\" / \"&\" / \"'\" / \"(\" / \")\" / \"*\" / \"+\" / \",\" / \";\" / \"=\"\n if (\n (charCode >= 65 /* A */ && charCode <= 90) /* Z */ ||\n (charCode >= 97 /* a */ && charCode <= 122) /* z */ ||\n (charCode >= 48 /* 0 */ && charCode <= 57) /* 9 */ ||\n charCode === 45 /* \"-\" */ ||\n charCode === 46 /* \".\" */ ||\n charCode === 95 /* \"_\" */ ||\n charCode === 126 /* \"~\" */\n ) {\n // unreserved\n } else if (\n charCode === 33 /* \"!\" */ ||\n charCode === 36 /* \"$\" */ ||\n charCode === 38 /* \"&\" */ ||\n charCode === 39 /* \"'\" */ ||\n charCode === 40 /* \"(\" */ ||\n charCode === 41 /* \")\" */ ||\n charCode === 42 /* \"*\" */ ||\n charCode === 43 /* \"+\" */ ||\n charCode === 44 /* \",\" */ ||\n charCode === 59 /* \";\" */ ||\n charCode === 61 /* \"=\" */\n ) {\n // sub-delims\n } else if (charCode === 58 /* \":\" */ || charCode === 64 /* \"@\" */) {\n // pchar extra\n } else if (charCode === 47 /* \"/\" */ || charCode === 63 /* \"?\" */) {\n // fragment extra\n } else if (charCode === 37 /* \"%\" */) {\n // pct-enc\n if (i + 2 >= endIdx) return false\n if (!isHexDigit(value.charCodeAt(i + 1))) return false\n if (!isHexDigit(value.charCodeAt(i + 2))) return false\n i += 2\n } else {\n return false\n }\n }\n\n return true\n}\n\nexport function isHexDigit(code: number): boolean {\n return (\n (code >= 48 && code <= 57) || // 0-9\n (code >= 65 && code <= 70) || // A-F\n (code >= 97 && code <= 102) // a-f\n )\n}\n\nexport const canParse =\n URL.canParse?.bind(URL) ??\n ((url, base) => {\n try {\n new URL(url, base)\n return true\n } catch {\n return false\n }\n })\n"]}
@@ -1,15 +1,9 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.DID_PLC_PREFIX = void 0;
4
- exports.isDidPlc = isDidPlc;
5
- exports.asDidPlc = asDidPlc;
6
- exports.assertDidPlc = assertDidPlc;
7
- const did_error_js_1 = require("../did-error.js");
1
+ import { InvalidDidError } from '../did-error.js';
8
2
  const DID_PLC_PREFIX = `did:plc:`;
9
- exports.DID_PLC_PREFIX = DID_PLC_PREFIX;
10
3
  const DID_PLC_PREFIX_LENGTH = DID_PLC_PREFIX.length;
11
4
  const DID_PLC_LENGTH = 32;
12
- function isDidPlc(input) {
5
+ export { DID_PLC_PREFIX };
6
+ export function isDidPlc(input) {
13
7
  // Optimization: equivalent to try/catch around "assertDidPlc"
14
8
  if (typeof input !== 'string')
15
9
  return false;
@@ -23,25 +17,25 @@ function isDidPlc(input) {
23
17
  }
24
18
  return true;
25
19
  }
26
- function asDidPlc(input) {
20
+ export function asDidPlc(input) {
27
21
  assertDidPlc(input);
28
22
  return input;
29
23
  }
30
- function assertDidPlc(input) {
24
+ export function assertDidPlc(input) {
31
25
  if (typeof input !== 'string') {
32
- throw new did_error_js_1.InvalidDidError(typeof input, `DID must be a string`);
26
+ throw new InvalidDidError(typeof input, `DID must be a string`);
33
27
  }
34
28
  if (!input.startsWith(DID_PLC_PREFIX)) {
35
- throw new did_error_js_1.InvalidDidError(input, `Invalid did:plc prefix`);
29
+ throw new InvalidDidError(input, `Invalid did:plc prefix`);
36
30
  }
37
31
  if (input.length !== DID_PLC_LENGTH) {
38
- throw new did_error_js_1.InvalidDidError(input, `did:plc must be ${DID_PLC_LENGTH} characters long`);
32
+ throw new InvalidDidError(input, `did:plc must be ${DID_PLC_LENGTH} characters long`);
39
33
  }
40
34
  // The following check is not necessary, as the check below is more strict:
41
35
  // assertDidMsid(input, DID_PLC_PREFIX.length)
42
36
  for (let i = DID_PLC_PREFIX_LENGTH; i < DID_PLC_LENGTH; i++) {
43
37
  if (!isBase32Char(input.charCodeAt(i))) {
44
- throw new did_error_js_1.InvalidDidError(input, `Invalid character at position ${i}`);
38
+ throw new InvalidDidError(input, `Invalid character at position ${i}`);
45
39
  }
46
40
  }
47
41
  }
@@ -1 +1 @@
1
- {"version":3,"file":"plc.js","sourceRoot":"","sources":["../../src/methods/plc.ts"],"names":[],"mappings":";;;AASA,4BASC;AAED,4BAGC;AAED,oCAyBC;AAlDD,kDAAiD;AAGjD,MAAM,cAAc,GAAG,UAAU,CAAA;AAIxB,wCAAc;AAHvB,MAAM,qBAAqB,GAAG,cAAc,CAAC,MAAM,CAAA;AACnD,MAAM,cAAc,GAAG,EAAE,CAAA;AAIzB,SAAgB,QAAQ,CAAC,KAAc;IACrC,8DAA8D;IAC9D,IAAI,OAAO,KAAK,KAAK,QAAQ;QAAE,OAAO,KAA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C;IACH,CAAC;AACH,CAAC;AAED,MAAM,YAAY,GAAG,CAAC,CAAS,EAAW,EAAE,CAC1C,CAAC,CAAC,IAAI,IAAI,IAAI,CAAC,IAAI,IAAI,CAAC,IAAI,CAAC,CAAC,IAAI,IAAI,IAAI,CAAC,IAAI,IAAI,CAAC,CAAA,CAAC,WAAW","sourcesContent":["import { InvalidDidError } from '../did-error.js'\nimport { Did } from '../did.js'\n\nconst DID_PLC_PREFIX = `did:plc:`\nconst DID_PLC_PREFIX_LENGTH = DID_PLC_PREFIX.length\nconst DID_PLC_LENGTH = 32\n\nexport { DID_PLC_PREFIX }\n\nexport function isDidPlc(input: unknown): input is Did<'plc'> {\n // Optimization: equivalent to try/catch around \"assertDidPlc\"\n if (typeof input !== 'string') return false\n if (input.length !== DID_PLC_LENGTH) return false\n if (!input.startsWith(DID_PLC_PREFIX)) return false\n for (let i = DID_PLC_PREFIX_LENGTH; i < DID_PLC_LENGTH; i++) {\n if (!isBase32Char(input.charCodeAt(i))) return false\n }\n return true\n}\n\nexport function asDidPlc<T>(input: T) {\n assertDidPlc(input)\n return input\n}\n\nexport function assertDidPlc(input: unknown): asserts input is Did<'plc'> {\n if (typeof input !== 'string') {\n throw new InvalidDidError(typeof input, `DID must be a string`)\n }\n\n if (!input.startsWith(DID_PLC_PREFIX)) {\n throw new InvalidDidError(input, `Invalid did:plc prefix`)\n }\n\n if (input.length !== DID_PLC_LENGTH) {\n throw new InvalidDidError(\n input,\n `did:plc must be ${DID_PLC_LENGTH} characters long`,\n )\n }\n\n // The following check is not necessary, as the check below is more strict:\n\n // assertDidMsid(input, DID_PLC_PREFIX.length)\n\n for (let i = DID_PLC_PREFIX_LENGTH; i < DID_PLC_LENGTH; i++) {\n if (!isBase32Char(input.charCodeAt(i))) {\n throw new InvalidDidError(input, `Invalid character at position ${i}`)\n }\n }\n}\n\nconst isBase32Char = (c: number): boolean =>\n (c >= 0x61 && c <= 0x7a) || (c >= 0x32 && c <= 0x37) // [a-z2-7]\n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cAAc,EAAE,CAAC;QACpC,MAAM,IAAI,eAAe,CACvB,KAAK,EACL,mBAAmB,cAAc,kBAAkB,CACpD,CAAA;IACH,CAAC;IAED,2EAA2E;IAE3E,8CAA8C;IAE9C,KAAK,IAAI,CAAC,GAAG,qBAAqB,EAAE,CAAC,GAAG,cAAc,EAAE,CAAC,EAAE,EAAE,CAAC;QAC5D,IAAI,CAAC,YAAY,CAAC,KAAK,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;YACvC,MAAM,IAAI,eAAe,CAAC,KAAK,EAAE,iCAAiC,CAAC,EAAE,CAAC,CAAA;QACxE,CAAC;IACH,CAAC;AACH,CAAC;AAED,MAAM,YAAY,GAAG,CAAC,CAAS,EAAW,EAAE,CAC1C,CAAC,CAAC,IAAI,IAAI,IAAI,CAAC,IAAI,IAAI,CAAC,IAAI,CAAC,CAAC,IAAI,IAAI,IAAI,CAAC,IAAI,IAAI,CAAC,CAAA,CAAC,WAAW","sourcesContent":["import { InvalidDidError } from '../did-error.js'\nimport { Did } from '../did.js'\n\nconst DID_PLC_PREFIX = `did:plc:`\nconst DID_PLC_PREFIX_LENGTH = DID_PLC_PREFIX.length\nconst DID_PLC_LENGTH = 32\n\nexport { DID_PLC_PREFIX }\n\nexport function isDidPlc(input: unknown): input is Did<'plc'> {\n // Optimization: equivalent to try/catch around \"assertDidPlc\"\n if (typeof input !== 'string') return false\n if (input.length !== DID_PLC_LENGTH) return false\n if (!input.startsWith(DID_PLC_PREFIX)) return false\n for (let i = DID_PLC_PREFIX_LENGTH; i < DID_PLC_LENGTH; i++) {\n if (!isBase32Char(input.charCodeAt(i))) return false\n }\n return true\n}\n\nexport function asDidPlc<T>(input: T) {\n assertDidPlc(input)\n return input\n}\n\nexport function assertDidPlc(input: unknown): asserts input is Did<'plc'> {\n if (typeof input !== 'string') {\n throw new InvalidDidError(typeof input, `DID must be a string`)\n }\n\n if (!input.startsWith(DID_PLC_PREFIX)) {\n throw new InvalidDidError(input, `Invalid did:plc prefix`)\n }\n\n if (input.length !== DID_PLC_LENGTH) {\n throw new InvalidDidError(\n input,\n `did:plc must be ${DID_PLC_LENGTH} characters long`,\n )\n }\n\n // The following check is not necessary, as the check below is more strict:\n\n // assertDidMsid(input, DID_PLC_PREFIX.length)\n\n for (let i = DID_PLC_PREFIX_LENGTH; i < DID_PLC_LENGTH; i++) {\n if (!isBase32Char(input.charCodeAt(i))) {\n throw new InvalidDidError(input, `Invalid character at position ${i}`)\n }\n }\n}\n\nconst isBase32Char = (c: number): boolean =>\n (c >= 0x61 && c <= 0x7a) || (c >= 0x32 && c <= 0x37) // [a-z2-7]\n"]}
@@ -1,70 +1,61 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.DID_WEB_PREFIX = void 0;
4
- exports.isDidWeb = isDidWeb;
5
- exports.asDidWeb = asDidWeb;
6
- exports.assertDidWeb = assertDidWeb;
7
- exports.didWebToUrl = didWebToUrl;
8
- exports.urlToDidWeb = urlToDidWeb;
9
- exports.buildDidWebUrl = buildDidWebUrl;
10
- const did_error_js_1 = require("../did-error.js");
11
- const did_js_1 = require("../did.js");
12
- const uri_js_1 = require("../lib/uri.js");
13
- exports.DID_WEB_PREFIX = `did:web:`;
1
+ import { InvalidDidError } from '../did-error.js';
2
+ import { assertDidMsid } from '../did.js';
3
+ import { canParse } from '../lib/uri.js';
4
+ export const DID_WEB_PREFIX = `did:web:`;
14
5
  /**
15
6
  * This function checks if the input is a valid Web DID, as per DID spec.
16
7
  */
17
- function isDidWeb(input) {
8
+ export function isDidWeb(input) {
18
9
  // Optimization: make cheap checks first
19
10
  if (typeof input !== 'string')
20
11
  return false;
21
- if (!input.startsWith(exports.DID_WEB_PREFIX))
12
+ if (!input.startsWith(DID_WEB_PREFIX))
22
13
  return false;
23
- if (input.charAt(exports.DID_WEB_PREFIX.length) === ':')
14
+ if (input.charAt(DID_WEB_PREFIX.length) === ':')
24
15
  return false;
25
16
  try {
26
- (0, did_js_1.assertDidMsid)(input, exports.DID_WEB_PREFIX.length);
17
+ assertDidMsid(input, DID_WEB_PREFIX.length);
27
18
  }
28
19
  catch {
29
20
  return false;
30
21
  }
31
- return (0, uri_js_1.canParse)(buildDidWebUrl(input));
22
+ return canParse(buildDidWebUrl(input));
32
23
  }
33
- function asDidWeb(input) {
24
+ export function asDidWeb(input) {
34
25
  assertDidWeb(input);
35
26
  return input;
36
27
  }
37
- function assertDidWeb(input) {
28
+ export function assertDidWeb(input) {
38
29
  if (typeof input !== 'string') {
39
- throw new did_error_js_1.InvalidDidError(typeof input, `DID must be a string`);
30
+ throw new InvalidDidError(typeof input, `DID must be a string`);
40
31
  }
41
- if (!input.startsWith(exports.DID_WEB_PREFIX)) {
42
- throw new did_error_js_1.InvalidDidError(input, `Invalid did:web prefix`);
32
+ if (!input.startsWith(DID_WEB_PREFIX)) {
33
+ throw new InvalidDidError(input, `Invalid did:web prefix`);
43
34
  }
44
- if (input.charAt(exports.DID_WEB_PREFIX.length) === ':') {
45
- throw new did_error_js_1.InvalidDidError(input, 'did:web MSID must not start with a colon');
35
+ if (input.charAt(DID_WEB_PREFIX.length) === ':') {
36
+ throw new InvalidDidError(input, 'did:web MSID must not start with a colon');
46
37
  }
47
38
  // Make sure every char is valid (per DID spec)
48
- (0, did_js_1.assertDidMsid)(input, exports.DID_WEB_PREFIX.length);
49
- if (!(0, uri_js_1.canParse)(buildDidWebUrl(input))) {
50
- throw new did_error_js_1.InvalidDidError(input, 'Invalid Web DID');
39
+ assertDidMsid(input, DID_WEB_PREFIX.length);
40
+ if (!canParse(buildDidWebUrl(input))) {
41
+ throw new InvalidDidError(input, 'Invalid Web DID');
51
42
  }
52
43
  }
53
- function didWebToUrl(did) {
44
+ export function didWebToUrl(did) {
54
45
  try {
55
46
  return new URL(buildDidWebUrl(did));
56
47
  }
57
48
  catch (cause) {
58
- throw new did_error_js_1.InvalidDidError(did, 'Invalid Web DID', cause);
49
+ throw new InvalidDidError(did, 'Invalid Web DID', cause);
59
50
  }
60
51
  }
61
- function urlToDidWeb(url) {
52
+ export function urlToDidWeb(url) {
62
53
  const port = url.port ? `%3A${url.port}` : '';
63
54
  const path = url.pathname === '/' ? '' : url.pathname.replaceAll('/', ':');
64
55
  return `did:web:${url.hostname}${port}${path}`;
65
56
  }
66
- function buildDidWebUrl(did) {
67
- const hostIdx = exports.DID_WEB_PREFIX.length;
57
+ export function buildDidWebUrl(did) {
58
+ const hostIdx = DID_WEB_PREFIX.length;
68
59
  const pathIdx = did.indexOf(':', hostIdx);
69
60
  const hostEnc = pathIdx === -1 ? did.slice(hostIdx) : did.slice(hostIdx, pathIdx);
70
61
  const host = hostEnc.replaceAll('%3A', ':');
@@ -1 +1 @@
1
- {"version":3,"file":"web.js","sourceRoot":"","sources":["../../src/methods/web.ts"],"names":[],"mappings":";;;AASA,4BAaC;AAED,4BAGC;AAED,oCAmBC;AAED,kCAQC;AAED,kCAKC;AAED,wCAeC;AAlFD,kDAAiD;AACjD,sCAA8C;AAC9C,0CAAwC;AAE3B,QAAA,cAAc,GAAG,UAA+B,CAAA;AAE7D;;GAEG;AACH,SAAgB,QAAQ,CAAC,KAAc;IACrC,wCAAwC;IACxC,IAAI,OAAO,KAAK,KAAK,QAAQ;QAAE,OAAO,KAAK,CAAA;IAC3C,IAAI,CAAC,KAAK,CAAC,UAAU,CAAC,sBAAc,CAAC;QAAE,OAAO,KAAK,CAAA;IACnD,IAAI,KAAK,CAAC,MAAM,CAAC,sBAAc,CAAC,MAAM,CAAC,KAAK,GAAG;QAAE,OAAO,KAAK,CAAA;IAE7D,IAAI,CAAC;QACH,IAAA,sBAAa,EAAC,KAAK,EAAE,sBAAc,CAAC,MAAM,CAAC,CAAA;IAC7C,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,KAAK,CAAA;IACd,CAAC;IAED,OAAO,IAAA,iBAAQ,EAAC,cAAc,CAAC,KAAmB,CAAC,CAAC,CAAA;AACtD,CAAC;AAED,SAAgB,QAAQ,CAAI,KAAQ;IACl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AQ,KAAK,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,GAAG,CAAC,QAAQ,CAAC,UAAU,CAAC,GAAG,EAAE,GAAG,CAAC,CAAA;IAE1E,OAAO,WAAW,GAAG,CAAC,QAAQ,GAAG,IAAI,GAAG,IAAI,EAAE,CAAA;AAChD,CAAC;AAED,SAAgB,cAAc,CAAC,GAAe;IAC5C,MAAM,OAAO,GAAG,sBAAc,CAAC,MAAM,CAAA;IACrC,MAAM,OAAO,GAAG,GAAG,CAAC,OAAO,CAAC,GAAG,EAAE,OAAO,CAAC,CAAA;IAEzC,MAAM,OAAO,GACX,OAAO,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,KAAK,CAAC,OAAO,EAAE,OAAO,CAAC,CAAA;IACnE,MAAM,IAAI,GAAG,OAAO,CAAC,UAAU,CAAC,KAAK,EAAE,GAAG,CAAC,CAAA;IAC3C,MAAM,IAAI,GAAG,OAAO,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,GAAG,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,UAAU,CAAC,GAAG,EAAE,GAAG,CAAC,CAAA;IAC1E,MAAM,KAAK,GACT,IAAI,CAAC,UAAU,CAAC,WAAW,CAAC;QAC5B,CAAC,IAAI,CAAC,MAAM,KAAK,CAAC,IAAI,IAAI,CAAC,UAAU,CAAC,CAAC,CAAC,KAAK,EAAE,CAAC,CAAC,SAAS;QACxD,CAAC,CAAC,MAAM;QACR,CAAC,CAAC,OAAO,CAAA;IAEb,OAAO,GAAG,KAAK,MAAM,IAAI,GAAG,IAAI,EAAE,CAAA;AACpC,CAAC","sourcesContent":["import { InvalidDidError } from '../did-error.js'\nimport { Did, assertDidMsid } from '../did.js'\nimport { canParse } from '../lib/uri.js'\n\nexport const DID_WEB_PREFIX = `did:web:` satisfies Did<'web'>\n\n/**\n * This function checks if the input is a valid Web DID, as per DID spec.\n */\nexport function isDidWeb(input: unknown): input is Did<'web'> {\n // Optimization: make cheap checks first\n if (typeof input !== 'string') return false\n if (!input.startsWith(DID_WEB_PREFIX)) return false\n if (input.charAt(DID_WEB_PREFIX.length) === ':') return false\n\n try {\n assertDidMsid(input, DID_WEB_PREFIX.length)\n } catch {\n return false\n }\n\n return canParse(buildDidWebUrl(input as Did<'web'>))\n}\n\nexport function asDidWeb<T>(input: T) {\n assertDidWeb(input)\n return input\n}\n\nexport function assertDidWeb(input: unknown): asserts input is Did<'web'> {\n if (typeof input !== 'string') {\n throw new InvalidDidError(typeof input, `DID must be a string`)\n }\n\n if (!input.startsWith(DID_WEB_PREFIX)) {\n throw new InvalidDidError(input, `Invalid did:web prefix`)\n }\n\n if (input.charAt(DID_WEB_PREFIX.length) === ':') {\n throw new InvalidDidError(input, 'did:web MSID must not start with a colon')\n }\n\n // Make sure every char is valid (per DID spec)\n assertDidMsid(input, DID_WEB_PREFIX.length)\n\n if (!canParse(buildDidWebUrl(input as Did<'web'>))) {\n throw new InvalidDidError(input, 'Invalid Web DID')\n }\n}\n\nexport function didWebToUrl(did: Did<'web'>) {\n try {\n return new URL(buildDidWebUrl(did)) as URL & {\n protocol: 'http:' | 'https:'\n }\n } catch (cause) {\n throw new InvalidDidError(did, 'Invalid Web DID', cause)\n }\n}\n\nexport function urlToDidWeb(url: URL): Did<'web'> {\n const port = url.port ? `%3A${url.port}` : ''\n const path = url.pathname === '/' ? '' : url.pathname.replaceAll('/', ':')\n\n return `did:web:${url.hostname}${port}${path}`\n}\n\nexport function buildDidWebUrl(did: Did<'web'>): string {\n const hostIdx = DID_WEB_PREFIX.length\n const pathIdx = did.indexOf(':', hostIdx)\n\n const hostEnc =\n pathIdx === -1 ? did.slice(hostIdx) : did.slice(hostIdx, pathIdx)\n const host = hostEnc.replaceAll('%3A', ':')\n const path = pathIdx === -1 ? '' : did.slice(pathIdx).replaceAll(':', '/')\n const proto =\n host.startsWith('localhost') &&\n (host.length === 9 || host.charCodeAt(9) === 58) /* ':' */\n ? 'http'\n : 'https'\n\n return `${proto}://${host}${path}`\n}\n"]}
1
+ {"version":3,"file":"web.js","sourceRoot":"","sources":["../../src/methods/web.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAA;AACjD,OAAO,EAAO,aAAa,EAAE,MAAM,WAAW,CAAA;AAC9C,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAA;AAExC,MAAM,CAAC,MAAM,cAAc,GAAG,UAA+B,CAAA;AAE7D;;GAEG;AACH,MAAM,UAAU,QAAQ,CAAC,KAAc;IACrC,wCAAwC;IACxC,IAAI,OAAO,KAAK,KAAK,QAAQ;QAAE,OAAO,KAAK,CAAA;IAC3C,IAAI,CAAC,KAAK,CAAC,UAAU,CAAC,cAAc,CAAC;QAAE,OAAO,KAAK,CAAA;IACnD,IAAI,KAAK,CAAC,MAAM,CAAC,cAAc,CAAC,MAAM,CAAC,KAAK,GAAG;QAAE,OAAO,KAAK,CAAA;IAE7D,IAAI,CAAC;QACH,aAAa,CAAC,KAAK,EAAE,cAAc,CAAC,MAAM,CAAC,CAAA;IAC7C,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,KAAK,CAAA;IACd,CAAC;IAED,OAAO,QAAQ,CAAC,cAAc,CAAC,KAAmB,CAAC,CAAC,CAAA;AACtD,CAAC;AAED,MAAM,UAAU,QAAQ,CAAI,KAAQ;IAClC,YAAY,CAAC,KAAK,CAAC,CAAA;IACnB,OAAO,KAAK,CAAA;AACd,CAAC;AAED,MAAM,UAAU,YAAY,CAAC,KAAc;IACzC,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE,CAAC;QAC9B,MAAM,IAAI,eAAe,CAAC,OAAO,KAAK,EAAE,sBAAsB,CAAC,CAAA;IACjE,CAAC;IAED,IAAI,CAAC,KAAK,CAAC,UAAU,CAAC,cAAc,CAAC,EAAE,CAAC;QACt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AO,GAAG,GAAG,CAAC,OAAO,CAAC,GAAG,EAAE,OAAO,CAAC,CAAA;IAEzC,MAAM,OAAO,GACX,OAAO,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,KAAK,CAAC,OAAO,EAAE,OAAO,CAAC,CAAA;IACnE,MAAM,IAAI,GAAG,OAAO,CAAC,UAAU,CAAC,KAAK,EAAE,GAAG,CAAC,CAAA;IAC3C,MAAM,IAAI,GAAG,OAAO,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,GAAG,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,UAAU,CAAC,GAAG,EAAE,GAAG,CAAC,CAAA;IAC1E,MAAM,KAAK,GACT,IAAI,CAAC,UAAU,CAAC,WAAW,CAAC;QAC5B,CAAC,IAAI,CAAC,MAAM,KAAK,CAAC,IAAI,IAAI,CAAC,UAAU,CAAC,CAAC,CAAC,KAAK,EAAE,CAAC,CAAC,SAAS;QACxD,CAAC,CAAC,MAAM;QACR,CAAC,CAAC,OAAO,CAAA;IAEb,OAAO,GAAG,KAAK,MAAM,IAAI,GAAG,IAAI,EAAE,CAAA;AACpC,CAAC","sourcesContent":["import { InvalidDidError } from '../did-error.js'\nimport { Did, assertDidMsid } from '../did.js'\nimport { canParse } from '../lib/uri.js'\n\nexport const DID_WEB_PREFIX = `did:web:` satisfies Did<'web'>\n\n/**\n * This function checks if the input is a valid Web DID, as per DID spec.\n */\nexport function isDidWeb(input: unknown): input is Did<'web'> {\n // Optimization: make cheap checks first\n if (typeof input !== 'string') return false\n if (!input.startsWith(DID_WEB_PREFIX)) return false\n if (input.charAt(DID_WEB_PREFIX.length) === ':') return false\n\n try {\n assertDidMsid(input, DID_WEB_PREFIX.length)\n } catch {\n return false\n }\n\n return canParse(buildDidWebUrl(input as Did<'web'>))\n}\n\nexport function asDidWeb<T>(input: T) {\n assertDidWeb(input)\n return input\n}\n\nexport function assertDidWeb(input: unknown): asserts input is Did<'web'> {\n if (typeof input !== 'string') {\n throw new InvalidDidError(typeof input, `DID must be a string`)\n }\n\n if (!input.startsWith(DID_WEB_PREFIX)) {\n throw new InvalidDidError(input, `Invalid did:web prefix`)\n }\n\n if (input.charAt(DID_WEB_PREFIX.length) === ':') {\n throw new InvalidDidError(input, 'did:web MSID must not start with a colon')\n }\n\n // Make sure every char is valid (per DID spec)\n assertDidMsid(input, DID_WEB_PREFIX.length)\n\n if (!canParse(buildDidWebUrl(input as Did<'web'>))) {\n throw new InvalidDidError(input, 'Invalid Web DID')\n }\n}\n\nexport function didWebToUrl(did: Did<'web'>) {\n try {\n return new URL(buildDidWebUrl(did)) as URL & {\n protocol: 'http:' | 'https:'\n }\n } catch (cause) {\n throw new InvalidDidError(did, 'Invalid Web DID', cause)\n }\n}\n\nexport function urlToDidWeb(url: URL): Did<'web'> {\n const port = url.port ? `%3A${url.port}` : ''\n const path = url.pathname === '/' ? '' : url.pathname.replaceAll('/', ':')\n\n return `did:web:${url.hostname}${port}${path}`\n}\n\nexport function buildDidWebUrl(did: Did<'web'>): string {\n const hostIdx = DID_WEB_PREFIX.length\n const pathIdx = did.indexOf(':', hostIdx)\n\n const hostEnc =\n pathIdx === -1 ? did.slice(hostIdx) : did.slice(hostIdx, pathIdx)\n const host = hostEnc.replaceAll('%3A', ':')\n const path = pathIdx === -1 ? '' : did.slice(pathIdx).replaceAll(':', '/')\n const proto =\n host.startsWith('localhost') &&\n (host.length === 9 || host.charCodeAt(9) === 58) /* ':' */\n ? 'http'\n : 'https'\n\n return `${proto}://${host}${path}`\n}\n"]}
package/dist/methods.js CHANGED
@@ -1,19 +1,3 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
- };
16
- Object.defineProperty(exports, "__esModule", { value: true });
17
- __exportStar(require("./methods/plc.js"), exports);
18
- __exportStar(require("./methods/web.js"), exports);
1
+ export * from './methods/plc.js';
2
+ export * from './methods/web.js';
19
3
  //# sourceMappingURL=methods.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"methods.js","sourceRoot":"","sources":["../src/methods.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,mDAAgC;AAChC,mDAAgC","sourcesContent":["export * from './methods/plc.js'\nexport * from './methods/web.js'\n"]}
1
+ {"version":3,"file":"methods.js","sourceRoot":"","sources":["../src/methods.ts"],"names":[],"mappings":"AAAA,cAAc,kBAAkB,CAAA;AAChC,cAAc,kBAAkB,CAAA","sourcesContent":["export * from './methods/plc.js'\nexport * from './methods/web.js'\n"]}
package/dist/utils.js CHANGED
@@ -1,7 +1,4 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.matchesIdentifier = matchesIdentifier;
4
- function matchesIdentifier(did, id, candidate) {
1
+ export function matchesIdentifier(did, id, candidate) {
5
2
  // optimized implementation of:
6
3
  // return candidate === `#${id}` || candidate === `${did}#${id}`
7
4
  return candidate.charCodeAt(0) === 35 // '#'
package/dist/utils.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"utils.js","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":";;AAGA,8CAcC;AAdD,SAAgB,iBAAiB,CAC/B,GAAM,EACN,EAAK,EACL,SAAiB;IAEjB,+BAA+B;IAC/B,gEAAgE;IAEhE,OAAO,SAAS,CAAC,UAAU,CAAC,CAAC,CAAC,KAAK,EAAE,CAAC,MAAM;QAC1C,CAAC,CAAC,SAAS,CAAC,MAAM,KAAK,EAAE,CAAC,MAAM,GAAG,CAAC,IAAI,SAAS,CAAC,QAAQ,CAAC,EAAE,CAAC;QAC9D,CAAC,CAAC,SAAS,CAAC,MAAM,KAAK,EAAE,CAAC,MAAM,GAAG,CAAC,GAAG,GAAG,CAAC,MAAM;YAC7C,SAAS,CAAC,UAAU,CAAC,GAAG,CAAC,MAAM,CAAC,KAAK,EAAE,IAAI,MAAM;YACjD,SAAS,CAAC,UAAU,CAAC,GAAG,CAAC;YACzB,SAAS,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAA;AAC9B,CAAC","sourcesContent":["export type Identifier<D extends string, I extends string> =\n | `#${I}`\n | `${D}#${I}`\nexport function matchesIdentifier<D extends string, I extends string>(\n did: D,\n id: I,\n candidate: string,\n): candidate is Identifier<D, I> {\n // optimized implementation of:\n // return candidate === `#${id}` || candidate === `${did}#${id}`\n\n return candidate.charCodeAt(0) === 35 // '#'\n ? candidate.length === id.length + 1 && candidate.endsWith(id)\n : candidate.length === id.length + 1 + did.length &&\n candidate.charCodeAt(did.length) === 35 && // '#'\n candidate.startsWith(did) &&\n candidate.endsWith(id)\n}\n"]}
1
+ {"version":3,"file":"utils.js","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAGA,MAAM,UAAU,iBAAiB,CAC/B,GAAM,EACN,EAAK,EACL,SAAiB;IAEjB,+BAA+B;IAC/B,gEAAgE;IAEhE,OAAO,SAAS,CAAC,UAAU,CAAC,CAAC,CAAC,KAAK,EAAE,CAAC,MAAM;QAC1C,CAAC,CAAC,SAAS,CAAC,MAAM,KAAK,EAAE,CAAC,MAAM,GAAG,CAAC,IAAI,SAAS,CAAC,QAAQ,CAAC,EAAE,CAAC;QAC9D,CAAC,CAAC,SAAS,CAAC,MAAM,KAAK,EAAE,CAAC,MAAM,GAAG,CAAC,GAAG,GAAG,CAAC,MAAM;YAC7C,SAAS,CAAC,UAAU,CAAC,GAAG,CAAC,MAAM,CAAC,KAAK,EAAE,IAAI,MAAM;YACjD,SAAS,CAAC,UAAU,CAAC,GAAG,CAAC;YACzB,SAAS,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAA;AAC9B,CAAC","sourcesContent":["export type Identifier<D extends string, I extends string> =\n | `#${I}`\n | `${D}#${I}`\nexport function matchesIdentifier<D extends string, I extends string>(\n did: D,\n id: I,\n candidate: string,\n): candidate is Identifier<D, I> {\n // optimized implementation of:\n // return candidate === `#${id}` || candidate === `${did}#${id}`\n\n return candidate.charCodeAt(0) === 35 // '#'\n ? candidate.length === id.length + 1 && candidate.endsWith(id)\n : candidate.length === id.length + 1 + did.length &&\n candidate.charCodeAt(did.length) === 35 && // '#'\n candidate.startsWith(did) &&\n candidate.endsWith(id)\n}\n"]}
@@ -0,0 +1,22 @@
1
+ /** @type {import('jest').Config} */
2
+ module.exports = {
3
+ displayName: 'DID',
4
+ transform: {
5
+ '^.+\\.(t|j)s$': [
6
+ '@swc/jest',
7
+ {
8
+ jsc: {
9
+ parser: { syntax: 'typescript', importAttributes: true },
10
+ experimental: { keepImportAttributes: true },
11
+ transform: {},
12
+ },
13
+ module: { type: 'es6' },
14
+ },
15
+ ],
16
+ },
17
+ extensionsToTreatAsEsm: ['.ts'],
18
+ transformIgnorePatterns: [],
19
+ testTimeout: 60000,
20
+ setupFiles: ['<rootDir>/../../test.setup.ts'],
21
+ moduleNameMapper: { '^(\\.\\.?\\/.+)\\.js$': ['$1.ts', '$1.js'] },
22
+ }
package/package.json CHANGED
@@ -1,6 +1,9 @@
1
1
  {
2
2
  "name": "@atproto/did",
3
- "version": "0.3.0",
3
+ "version": "0.4.0",
4
+ "engines": {
5
+ "node": ">=22"
6
+ },
4
7
  "license": "MIT",
5
8
  "description": "DID resolution and verification library",
6
9
  "keywords": [
@@ -15,9 +18,7 @@
15
18
  "url": "https://github.com/bluesky-social/atproto",
16
19
  "directory": "packages/did"
17
20
  },
18
- "type": "commonjs",
19
- "main": "dist/index.js",
20
- "types": "dist/index.d.ts",
21
+ "type": "module",
21
22
  "exports": {
22
23
  ".": {
23
24
  "types": "./dist/index.d.ts",
@@ -29,11 +30,11 @@
29
30
  },
30
31
  "devDependencies": {
31
32
  "@swc/jest": "^0.2.24",
32
- "jest": "^28.1.2",
33
- "typescript": "^5.6.3"
33
+ "jest": "^30.0.0",
34
+ "typescript": "^6.0.3"
34
35
  },
35
36
  "scripts": {
36
37
  "build": "tsc --build tsconfig.build.json",
37
- "test": "jest"
38
+ "test": "NODE_OPTIONS=--experimental-vm-modules jest"
38
39
  }
39
40
  }
@@ -47,7 +47,7 @@ describe('assertDidPlc', () => {
47
47
 
48
48
  it('throws if called with non string argument', () => {
49
49
  for (const [val, message] of INVALID) {
50
- expect(() => assertDidPlc(val)).toThrowError(
50
+ expect(() => assertDidPlc(val)).toThrow(
51
51
  new InvalidDidError(
52
52
  typeof val === 'string' ? val : typeof val,
53
53
  message,
@@ -66,7 +66,7 @@ describe('asDidPlc', () => {
66
66
 
67
67
  it('throws if called with invalid dids', () => {
68
68
  for (const [val] of INVALID) {
69
- expect(() => asDidPlc(val)).toThrowError(InvalidDidError)
69
+ expect(() => asDidPlc(val)).toThrow(InvalidDidError)
70
70
  }
71
71
  })
72
72
  })
@@ -63,7 +63,7 @@ describe('assertDidWeb', () => {
63
63
 
64
64
  it('throws if called with non string argument', () => {
65
65
  for (const [val, message] of INVALID) {
66
- expect(() => assertDidWeb(val)).toThrowError(
66
+ expect(() => assertDidWeb(val)).toThrow(
67
67
  new InvalidDidError(
68
68
  typeof val === 'string' ? val : typeof val,
69
69
  message,
@@ -98,7 +98,7 @@ describe('asDidWeb', () => {
98
98
 
99
99
  it('throws if called with invalid dids', () => {
100
100
  for (const [val, message] of INVALID) {
101
- expect(() => asDidWeb(val)).toThrowError(
101
+ expect(() => asDidWeb(val)).toThrow(
102
102
  new InvalidDidError(
103
103
  typeof val === 'string' ? val : typeof val,
104
104
  message,
@@ -1 +1 @@
1
- {"root":["./src/atproto.ts","./src/did-document.ts","./src/did-error.ts","./src/did.ts","./src/index.ts","./src/methods.ts","./src/utils.ts","./src/lib/uri.ts","./src/methods/plc.ts","./src/methods/web.ts"],"version":"5.8.2"}
1
+ {"root":["./src/atproto.ts","./src/did-document.ts","./src/did-error.ts","./src/did.ts","./src/index.ts","./src/methods.ts","./src/utils.ts","./src/lib/uri.ts","./src/methods/plc.ts","./src/methods/web.ts"],"version":"6.0.3"}
package/jest.config.js DELETED
@@ -1,13 +0,0 @@
1
- /** @type {import('jest').Config} */
2
- module.exports = {
3
- displayName: 'PDS',
4
- transform: { '^.+\\.(t|j)s$': '@swc/jest' },
5
- // Jest requires all ESM dependencies to be transpiled (even if they are
6
- // dynamically import()ed).
7
- transformIgnorePatterns: [
8
- `/node_modules/.pnpm/(?!(get-port|lande|toygrad)@)`,
9
- ],
10
- testTimeout: 60000,
11
- setupFiles: ['<rootDir>/../../jest.setup.ts'],
12
- moduleNameMapper: { '^(\\.\\.?\\/.+)\\.js$': ['$1.ts', '$1.js'] },
13
- }