@atproto/did 0.2.2 → 0.2.3

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,13 @@
1
1
  # @atproto/did
2
2
 
3
+ ## 0.2.3
4
+
5
+ ### Patch Changes
6
+
7
+ - [#4383](https://github.com/bluesky-social/atproto/pull/4383) [`8012627`](https://github.com/bluesky-social/atproto/commit/8012627a1226cb2f1c753385ad2497b6b43ffd2e) Thanks [@matthieusieben](https://github.com/matthieusieben)! - Export `AtprotoData` type
8
+
9
+ - [#4384](https://github.com/bluesky-social/atproto/pull/4384) [`d396de0`](https://github.com/bluesky-social/atproto/commit/d396de016d1d55d08cfad1dabd3ffd9eaeea76ea) Thanks [@matthieusieben](https://github.com/matthieusieben)! - Expose `matchesIdentifier` and `extractAtprotoData` utilities.
10
+
3
11
  ## 0.2.2
4
12
 
5
13
  ### Patch Changes
package/dist/atproto.d.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  import { z } from 'zod';
2
- import { DidDocument } from './did-document.js';
2
+ import { DidDocument, DidService } from './did-document.js';
3
3
  import { Did } from './did.js';
4
+ import { Identifier } from './utils.js';
4
5
  export type AtprotoIdentityDidMethods = 'plc' | 'web';
5
6
  export type AtprotoDid = Did<AtprotoIdentityDidMethods>;
6
7
  export declare const atprotoDidSchema: z.ZodEffects<z.ZodString, `did:plc:${string}` | `did:web:${string}`, string>;
@@ -14,5 +15,30 @@ export declare function assertAtprotoDidWeb(input: unknown): asserts input is Di
14
15
  export declare function isAtprotoDidWeb(input: unknown): input is Did<'web'>;
15
16
  export type AtprotoAudience = `${AtprotoDid}#${string}`;
16
17
  export declare const isAtprotoAudience: (value: unknown) => value is AtprotoAudience;
18
+ export type AtprotoData<M extends AtprotoIdentityDidMethods = AtprotoIdentityDidMethods> = {
19
+ did: Did<M>;
20
+ aka?: string;
21
+ key?: AtprotoVerificationMethod<M>;
22
+ pds?: AtprotoPersonalDataServerService<M>;
23
+ };
24
+ export declare function extractAtprotoData<M extends AtprotoIdentityDidMethods>(document: DidDocument<M>): AtprotoData<M>;
17
25
  export declare function extractPdsUrl(document: DidDocument<AtprotoIdentityDidMethods>): URL;
26
+ export type AtprotoAka = `at://${string}`;
27
+ export declare function isAtprotoAka(value: string): value is AtprotoAka;
28
+ export type AtprotoPersonalDataServerService<M extends AtprotoIdentityDidMethods = AtprotoIdentityDidMethods> = DidService & {
29
+ id: Identifier<Did<M>, 'atproto_pds'>;
30
+ type: 'AtprotoPersonalDataServer';
31
+ serviceEndpoint: string;
32
+ };
33
+ export declare function isAtprotoPersonalDataServerService<M extends AtprotoIdentityDidMethods = AtprotoIdentityDidMethods>(this: DidDocument<M>, service: null | undefined | DidService): service is AtprotoPersonalDataServerService<M>;
34
+ export declare const ATPROTO_VERIFICATION_METHOD_TYPES: readonly ["EcdsaSecp256r1VerificationKey2019", "EcdsaSecp256k1VerificationKey2019", "Multikey"];
35
+ export type SupportedAtprotoVerificationMethodType = (typeof ATPROTO_VERIFICATION_METHOD_TYPES)[number];
36
+ type VerificationMethod = NonNullable<DidDocument['verificationMethod']>[number];
37
+ export type AtprotoVerificationMethod<M extends AtprotoIdentityDidMethods = AtprotoIdentityDidMethods> = Extract<VerificationMethod, object> & {
38
+ id: Identifier<Did<M>, 'atproto'>;
39
+ type: SupportedAtprotoVerificationMethodType;
40
+ publicKeyMultibase: string;
41
+ };
42
+ export declare function isAtprotoVerificationMethod<M extends AtprotoIdentityDidMethods = AtprotoIdentityDidMethods>(this: DidDocument<M>, method: null | undefined | NonNullable<DidDocument<M>['verificationMethod']>[number]): method is AtprotoVerificationMethod<M>;
43
+ export {};
18
44
  //# sourceMappingURL=atproto.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"atproto.d.ts","sourceRoot":"","sources":["../src/atproto.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAA;AACvB,OAAO,EAAE,WAAW,E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CAAC,CAAC,CAAC,MAAM,CAAC,GAC5D,MAAM,IAAI,yBAAyB,CAAC,CAAC,CAAC,CASxC"}
package/dist/atproto.js CHANGED
@@ -1,16 +1,21 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.isAtprotoAudience = exports.atprotoDidSchema = void 0;
3
+ exports.ATPROTO_VERIFICATION_METHOD_TYPES = exports.isAtprotoAudience = exports.atprotoDidSchema = void 0;
4
4
  exports.isAtprotoDid = isAtprotoDid;
5
5
  exports.asAtprotoDid = asAtprotoDid;
6
6
  exports.assertAtprotoDid = assertAtprotoDid;
7
7
  exports.assertAtprotoDidWeb = assertAtprotoDidWeb;
8
8
  exports.isAtprotoDidWeb = isAtprotoDidWeb;
9
+ exports.extractAtprotoData = extractAtprotoData;
9
10
  exports.extractPdsUrl = extractPdsUrl;
11
+ exports.isAtprotoAka = isAtprotoAka;
12
+ exports.isAtprotoPersonalDataServerService = isAtprotoPersonalDataServerService;
13
+ exports.isAtprotoVerificationMethod = isAtprotoVerificationMethod;
10
14
  const zod_1 = require("zod");
11
15
  const did_error_js_1 = require("./did-error.js");
12
16
  const uri_js_1 = require("./lib/uri.js");
13
17
  const methods_js_1 = require("./methods.js");
18
+ const utils_js_1 = require("./utils.js");
14
19
  exports.atprotoDidSchema = zod_1.z
15
20
  .string()
16
21
  .refine(isAtprotoDid, `Atproto only allows "plc" and "web" DID methods`);
@@ -91,23 +96,39 @@ const isAtprotoAudience = (value) => {
91
96
  return ((0, uri_js_1.isFragment)(value, hashIndex + 1) && isAtprotoDid(value.slice(0, hashIndex)));
92
97
  };
93
98
  exports.isAtprotoAudience = isAtprotoAudience;
99
+ function extractAtprotoData(document) {
100
+ return {
101
+ did: document.id,
102
+ aka: document.alsoKnownAs?.find(isAtprotoAka)?.slice(5),
103
+ key: document.verificationMethod?.find((isAtprotoVerificationMethod), document),
104
+ pds: document.service?.find((isAtprotoPersonalDataServerService), document),
105
+ };
106
+ }
94
107
  function extractPdsUrl(document) {
95
- const service = document.service?.find((isAtprotoPersonalDataServerService), document);
108
+ const service = document.service?.find(isAtprotoPersonalDataServerService, document);
96
109
  if (!service) {
97
- throw new did_error_js_1.DidError(document.id, `Identity "${document.id}" does not have a PDS URL`, 'did-service-not-found');
98
- }
99
- try {
100
- return new URL(service.serviceEndpoint);
101
- }
102
- catch (cause) {
103
- throw new did_error_js_1.DidError(document.id, `Invalid PDS URL in DID document: ${service.serviceEndpoint}`, 'did-document-format-error', undefined, cause);
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');
104
111
  }
112
+ return new URL(service.serviceEndpoint);
113
+ }
114
+ function isAtprotoAka(value) {
115
+ return value.startsWith('at://');
116
+ }
117
+ function isAtprotoPersonalDataServerService(service) {
118
+ return (service?.type === 'AtprotoPersonalDataServer' &&
119
+ typeof service.serviceEndpoint === 'string' &&
120
+ (0, uri_js_1.canParse)(service.serviceEndpoint) &&
121
+ (0, utils_js_1.matchesIdentifier)(this.id, 'atproto_pds', service.id));
105
122
  }
106
- function isAtprotoPersonalDataServerService(s) {
107
- return (typeof s.serviceEndpoint === 'string' &&
108
- s.type === 'AtprotoPersonalDataServer' &&
109
- (s.id.startsWith('#')
110
- ? s.id === '#atproto_pds'
111
- : s.id === `${this.id}#atproto_pds`));
123
+ exports.ATPROTO_VERIFICATION_METHOD_TYPES = Object.freeze([
124
+ 'EcdsaSecp256r1VerificationKey2019',
125
+ 'EcdsaSecp256k1VerificationKey2019',
126
+ 'Multikey',
127
+ ]);
128
+ function isAtprotoVerificationMethod(method) {
129
+ return (typeof method === 'object' &&
130
+ typeof method?.publicKeyMultibase === 'string' &&
131
+ exports.ATPROTO_VERIFICATION_METHOD_TYPES.includes(method.type) &&
132
+ (0, utils_js_1.matchesIdentifier)(this.id, 'atproto', method.id));
112
133
  }
113
134
  //# sourceMappingURL=atproto.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"atproto.js","sourceR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,eAAe,KAAK,QAAQ;QACrC,CAAC,CAAC,IAAI,KAAK,2BAA2B;QACtC,CAAC,CAAC,CAAC,EAAE,CAAC,UAAU,CAAC,GAAG,CAAC;YACnB,CAAC,CAAC,CAAC,CAAC,EAAE,KAAK,cAAc;YACzB,CAAC,CAAC,CAAC,CAAC,EAAE,KAAK,GAAG,IAAI,CAAC,EAAE,cAAc,CAAC,CACvC,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 { 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'\n\n// This file contains atproto-specific DID validation utilities.\n\nexport type AtprotoIdentityDidMethods = 'plc' | 'web'\nexport type AtprotoDid = Did<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 function extractPdsUrl(\n document: DidDocument<AtprotoIdentityDidMethods>,\n): URL {\n const service = document.service?.find(\n isAtprotoPersonalDataServerService<AtprotoIdentityDidMethods>,\n document,\n )\n\n if (!service) {\n throw new DidError(\n document.id,\n `Identity \"${document.id}\" does not have a PDS URL`,\n 'did-service-not-found',\n )\n }\n\n try {\n return new URL(service.serviceEndpoint)\n } catch (cause) {\n throw new DidError(\n document.id,\n `Invalid PDS URL in DID document: ${service.serviceEndpoint}`,\n 'did-document-format-error',\n undefined,\n cause,\n )\n }\n}\n\nfunction isAtprotoPersonalDataServerService<M extends string>(\n this: DidDocument<M>,\n s: DidService,\n): s is {\n id: '#atproto_pds' | `${Did<M>}#atproto_pds`\n type: 'AtprotoPersonalDataServer'\n serviceEndpoint: string\n} {\n return (\n typeof s.serviceEndpoint === 'string' &&\n s.type === 'AtprotoPersonalDataServer' &&\n (s.id.startsWith('#')\n ? s.id === '#atproto_pds'\n : s.id === `${this.id}#atproto_pds`)\n )\n}\n"]}
1
+ {"version":3,"file":"atproto.js","sourceRoot":"","sources":["../src/atproto.ts"],"names":[],"mappings":";;;AAwBA,oCAEC;AAED,oCAGC;AAED,4CAaC;AAED,kDAkBC;AAKD,0CAcC;AAmDD,gDAeC;AAED,sCAiBC;AAGD,oCAEC;AAUD,gFAYC;AAmBD,kEAiBC;AAzOD,6BAAuB;AAEvB,iDAA0D;AAE1D,yCAAmD;AACnD,6CAOqB;AACrB,yCAA0D;AAO7C,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,CAAA;IACvB,OAAO,KAAK,CAAA;AACd,CAAC;AAED,SAAgB,gBAAgB,CAAC,KAAc;IAC7C,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE,CAAC;QAC9B,MAAM,IAAI,8BAAe,CAAC,OAAO,KAAK,EAAE,sBAAsB,CAAC,CAAA;IACjE,CAAC;SAAM,IAAI,KAAK,CAAC,UAAU,CAAC,2BAAc,CAAC,EAAE,CAAC;QAC5C,IAAA,yBAAY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,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>\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(\n document: DidDocument<AtprotoIdentityDidMethods>,\n): 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"]}
package/dist/index.d.ts CHANGED
@@ -3,4 +3,5 @@ export * from './did-document.js';
3
3
  export * from './did-error.js';
4
4
  export * from './did.js';
5
5
  export * from './methods.js';
6
+ export * from './utils.js';
6
7
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","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"}
1
+ {"version":3,"file":"index.d.ts","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"}
package/dist/index.js CHANGED
@@ -19,4 +19,5 @@ __exportStar(require("./did-document.js"), exports);
19
19
  __exportStar(require("./did-error.js"), exports);
20
20
  __exportStar(require("./did.js"), exports);
21
21
  __exportStar(require("./methods.js"), exports);
22
+ __exportStar(require("./utils.js"), exports);
22
23
  //# 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","sourcesContent":["export * from './atproto.js'\nexport * from './did-document.js'\nexport * from './did-error.js'\nexport * from './did.js'\nexport * from './methods.js'\n"]}
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"]}
package/dist/lib/uri.d.ts CHANGED
@@ -4,4 +4,5 @@
4
4
  */
5
5
  export declare function isFragment(value: string, startIdx?: number, endIdx?: number): boolean;
6
6
  export declare function isHexDigit(code: number): boolean;
7
+ export declare const canParse: (url: string | URL, base?: string | URL) => boolean;
7
8
  //# sourceMappingURL=uri.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"uri.d.ts","sourceRoot":"","sources":["../../src/lib/uri.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,wBAAgB,UAAU,CACxB,KAAK,EAAE,MAAM,EACb,QAAQ,SAAI,EACZ,MAAM,SAAe,GACpB,OAAO,CAkDT;AAED,wBAAgB,UAAU,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAMhD"}
1
+ {"version":3,"file":"uri.d.ts","sourceRoot":"","sources":["../../src/lib/uri.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,wBAAgB,UAAU,CACxB,KAAK,EAAE,MAAM,EACb,QAAQ,SAAI,EACZ,MAAM,SAAe,GACpB,OAAO,CAkDT;AAED,wBAAgB,UAAU,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAMhD;AAED,eAAO,MAAM,QAAQ,qDASjB,CAAA"}
package/dist/lib/uri.js CHANGED
@@ -1,5 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.canParse = void 0;
3
4
  exports.isFragment = isFragment;
4
5
  exports.isHexDigit = isHexDigit;
5
6
  /**
@@ -65,4 +66,14 @@ function isHexDigit(code) {
65
66
  (code >= 97 && code <= 102) // a-f
66
67
  );
67
68
  }
69
+ exports.canParse = URL.canParse?.bind(URL) ??
70
+ ((url, base) => {
71
+ try {
72
+ new URL(url, base);
73
+ return true;
74
+ }
75
+ catch {
76
+ return false;
77
+ }
78
+ });
68
79
  //# sourceMappingURL=uri.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"uri.js","sourceRoot":"","sources":["../../src/lib/uri.ts"],"names":[],"mappings":";;AAIA,gCAsDC;AAED,gCAMC;AAlED;;;GAGG;AACH,SAAgB,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,SAAS;YACzB,QAAQ,KAAK,EAAE,CA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AI,EAAE,IAAI,IAAI,IAAI,EAAE,CAAC,IAAI,MAAM;QACpC,CAAC,IAAI,IAAI,EAAE,IAAI,IAAI,IAAI,GAAG,CAAC,CAAC,MAAM;KACnC,CAAA;AACH,CAAC","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"]}
1
+ {"version":3,"file":"uri.js","sourceRoot":"","sources":["../../src/lib/uri.ts"],"names":[],"mappings":";;;AAIA,gCAsDC;AAED,gCAMC;AAlED;;;GAGG;AACH,SAAgB,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,SAAS;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,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;AAEY,QAAA,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 +1 @@
1
- {"version":3,"file":"web.d.ts","sourceRoot":"","sources":["../../src/methods/web.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,GAAG,EAAiB,MAAM,WAAW,CAAA;AAE9C,eAAO,MAAM,cAAc,aAAkC,CAAA;AAa7D;;GAEG;AACH,wBAAgB,QAAQ,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,GAAG,CAAC,KAAK,CAAC,CAa5D;AAED,wBAAgB,QAAQ,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,2BAGnC;AAED,wBAAgB,YAAY,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,CAAC,KAAK,IAAI,GAAG,CAAC,KAAK,CAAC,CAmBxE;AAED,wBAAgB,WAAW,CAAC,GAAG,EAAE,GAAG,CAAC,KAAK,CAAC;cAG3B,OAAO,GAAG,QAAQ;EAKjC;AAED,wBAAgB,WAAW,CAAC,GAAG,EAAE,GAAG,GAAG,GAAG,CAAC,KAAK,CAAC,CAKhD;AAED,wBAAgB,cAAc,CAAC,GAAG,EAAE,GAAG,CAAC,KAAK,CAAC,GAAG,MAAM,CAetD"}
1
+ {"version":3,"file":"web.d.ts","sourceRoot":"","sources":["../../src/methods/web.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,GAAG,EAAiB,MAAM,WAAW,CAAA;AAG9C,eAAO,MAAM,cAAc,aAAkC,CAAA;AAE7D;;GAEG;AACH,wBAAgB,QAAQ,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,GAAG,CAAC,KAAK,CAAC,CAa5D;AAED,wBAAgB,QAAQ,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,2BAGnC;AAED,wBAAgB,YAAY,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,CAAC,KAAK,IAAI,GAAG,CAAC,KAAK,CAAC,CAmBxE;AAED,wBAAgB,WAAW,CAAC,GAAG,EAAE,GAAG,CAAC,KAAK,CAAC;cAG3B,OAAO,GAAG,QAAQ;EAKjC;AAED,wBAAgB,WAAW,CAAC,GAAG,EAAE,GAAG,GAAG,GAAG,CAAC,KAAK,CAAC,CAKhD;AAED,wBAAgB,cAAc,CAAC,GAAG,EAAE,GAAG,CAAC,KAAK,CAAC,GAAG,MAAM,CAetD"}
@@ -9,17 +9,8 @@ exports.urlToDidWeb = urlToDidWeb;
9
9
  exports.buildDidWebUrl = buildDidWebUrl;
10
10
  const did_error_js_1 = require("../did-error.js");
11
11
  const did_js_1 = require("../did.js");
12
+ const uri_js_1 = require("../lib/uri.js");
12
13
  exports.DID_WEB_PREFIX = `did:web:`;
13
- const canParse = URL.canParse?.bind(URL) ??
14
- ((url, base) => {
15
- try {
16
- new URL(url, base);
17
- return true;
18
- }
19
- catch {
20
- return false;
21
- }
22
- });
23
14
  /**
24
15
  * This function checks if the input is a valid Web DID, as per DID spec.
25
16
  */
@@ -37,7 +28,7 @@ function isDidWeb(input) {
37
28
  catch {
38
29
  return false;
39
30
  }
40
- return canParse(buildDidWebUrl(input));
31
+ return (0, uri_js_1.canParse)(buildDidWebUrl(input));
41
32
  }
42
33
  function asDidWeb(input) {
43
34
  assertDidWeb(input);
@@ -55,7 +46,7 @@ function assertDidWeb(input) {
55
46
  }
56
47
  // Make sure every char is valid (per DID spec)
57
48
  (0, did_js_1.assertDidMsid)(input, exports.DID_WEB_PREFIX.length);
58
- if (!canParse(buildDidWebUrl(input))) {
49
+ if (!(0, uri_js_1.canParse)(buildDidWebUrl(input))) {
59
50
  throw new did_error_js_1.InvalidDidError(input, 'Invalid Web DID');
60
51
  }
61
52
  }
@@ -1 +1 @@
1
- {"version":3,"file":"web.js","sourceRoot":"","sources":["../../src/methods/web.ts"],"names":[],"mappings":";;;AAmBA,4BAaC;AAED,4BAGC;AAED,oCAmBC;AAED,kCAQC;AAED,kCAKC;AAED,wCAeC;AA5FD,kDAAiD;AACjD,sCAA8C;AAEjC,QAAA,cAAc,GAAG,UAA+B,CAAA;AAE7D,MAAM,QAAQ,GACZ,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;AAEJ;;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,CA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,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'\n\nexport const DID_WEB_PREFIX = `did:web:` satisfies Did<'web'>\n\nconst 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\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":";;;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,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AI,GAAG,GAAG,CAAC,QAAQ,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"]}
@@ -0,0 +1,3 @@
1
+ export type Identifier<D extends string, I extends string> = `#${I}` | `${D}#${I}`;
2
+ export declare function matchesIdentifier<D extends string, I extends string>(did: D, id: I, candidate: string): candidate is Identifier<D, I>;
3
+ //# sourceMappingURL=utils.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,UAAU,CAAC,CAAC,SAAS,MAAM,EAAE,CAAC,SAAS,MAAM,IACrD,IAAI,CAAC,EAAE,GACP,GAAG,CAAC,IAAI,CAAC,EAAE,CAAA;AACf,wBAAgB,iBAAiB,CAAC,CAAC,SAAS,MAAM,EAAE,CAAC,SAAS,MAAM,EAClE,GAAG,EAAE,CAAC,EACN,EAAE,EAAE,CAAC,EACL,SAAS,EAAE,MAAM,GAChB,SAAS,IAAI,UAAU,CAAC,CAAC,EAAE,CAAC,CAAC,CAU/B"}
package/dist/utils.js ADDED
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.matchesIdentifier = matchesIdentifier;
4
+ function matchesIdentifier(did, id, candidate) {
5
+ // optimized implementation of:
6
+ // return candidate === `#${id}` || candidate === `${did}#${id}`
7
+ return candidate.charCodeAt(0) === 35 // '#'
8
+ ? candidate.length === id.length + 1 && candidate.endsWith(id)
9
+ : candidate.length === id.length + 1 + did.length &&
10
+ candidate.charCodeAt(did.length) === 35 && // '#'
11
+ candidate.startsWith(did) &&
12
+ candidate.endsWith(id);
13
+ }
14
+ //# sourceMappingURL=utils.js.map
@@ -0,0 +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"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atproto/did",
3
- "version": "0.2.2",
3
+ "version": "0.2.3",
4
4
  "license": "MIT",
5
5
  "description": "DID resolution and verification library",
6
6
  "keywords": [
package/src/atproto.ts CHANGED
@@ -2,7 +2,7 @@ import { z } from 'zod'
2
2
  import { DidDocument, DidService } from './did-document.js'
3
3
  import { DidError, InvalidDidError } from './did-error.js'
4
4
  import { Did } from './did.js'
5
- import { isFragment } from './lib/uri.js'
5
+ import { canParse, isFragment } from './lib/uri.js'
6
6
  import {
7
7
  DID_PLC_PREFIX,
8
8
  DID_WEB_PREFIX,
@@ -11,6 +11,7 @@ import {
11
11
  isDidPlc,
12
12
  isDidWeb,
13
13
  } from './methods.js'
14
+ import { Identifier, matchesIdentifier } from './utils.js'
14
15
 
15
16
  // This file contains atproto-specific DID validation utilities.
16
17
 
@@ -124,48 +125,110 @@ export const isAtprotoAudience = (value: unknown): value is AtprotoAudience => {
124
125
  )
125
126
  }
126
127
 
128
+ export type AtprotoData<
129
+ M extends AtprotoIdentityDidMethods = AtprotoIdentityDidMethods,
130
+ > = {
131
+ did: Did<M>
132
+ aka?: string
133
+ key?: AtprotoVerificationMethod<M>
134
+ pds?: AtprotoPersonalDataServerService<M>
135
+ }
136
+
137
+ export function extractAtprotoData<M extends AtprotoIdentityDidMethods>(
138
+ document: DidDocument<M>,
139
+ ): AtprotoData<M> {
140
+ return {
141
+ did: document.id,
142
+ aka: document.alsoKnownAs?.find(isAtprotoAka)?.slice(5),
143
+ key: document.verificationMethod?.find(
144
+ isAtprotoVerificationMethod<M>,
145
+ document,
146
+ ),
147
+ pds: document.service?.find(
148
+ isAtprotoPersonalDataServerService<M>,
149
+ document,
150
+ ),
151
+ }
152
+ }
153
+
127
154
  export function extractPdsUrl(
128
155
  document: DidDocument<AtprotoIdentityDidMethods>,
129
156
  ): URL {
130
157
  const service = document.service?.find(
131
- isAtprotoPersonalDataServerService<AtprotoIdentityDidMethods>,
158
+ isAtprotoPersonalDataServerService,
132
159
  document,
133
160
  )
134
161
 
135
162
  if (!service) {
136
163
  throw new DidError(
137
164
  document.id,
138
- `Identity "${document.id}" does not have a PDS URL`,
165
+ `Document ${document.id} does not contain a (valid) #atproto_pds service URL`,
139
166
  'did-service-not-found',
140
167
  )
141
168
  }
142
169
 
143
- try {
144
- return new URL(service.serviceEndpoint)
145
- } catch (cause) {
146
- throw new DidError(
147
- document.id,
148
- `Invalid PDS URL in DID document: ${service.serviceEndpoint}`,
149
- 'did-document-format-error',
150
- undefined,
151
- cause,
152
- )
153
- }
170
+ return new URL(service.serviceEndpoint)
154
171
  }
155
172
 
156
- function isAtprotoPersonalDataServerService<M extends string>(
157
- this: DidDocument<M>,
158
- s: DidService,
159
- ): s is {
160
- id: '#atproto_pds' | `${Did<M>}#atproto_pds`
173
+ export type AtprotoAka = `at://${string}`
174
+ export function isAtprotoAka(value: string): value is AtprotoAka {
175
+ return value.startsWith('at://')
176
+ }
177
+
178
+ export type AtprotoPersonalDataServerService<
179
+ M extends AtprotoIdentityDidMethods = AtprotoIdentityDidMethods,
180
+ > = DidService & {
181
+ id: Identifier<Did<M>, 'atproto_pds'>
161
182
  type: 'AtprotoPersonalDataServer'
162
183
  serviceEndpoint: string
163
- } {
184
+ }
185
+
186
+ export function isAtprotoPersonalDataServerService<
187
+ M extends AtprotoIdentityDidMethods = AtprotoIdentityDidMethods,
188
+ >(
189
+ this: DidDocument<M>,
190
+ service: null | undefined | DidService,
191
+ ): service is AtprotoPersonalDataServerService<M> {
192
+ return (
193
+ service?.type === 'AtprotoPersonalDataServer' &&
194
+ typeof service.serviceEndpoint === 'string' &&
195
+ canParse(service.serviceEndpoint) &&
196
+ matchesIdentifier(this.id, 'atproto_pds', service.id)
197
+ )
198
+ }
199
+
200
+ export const ATPROTO_VERIFICATION_METHOD_TYPES = Object.freeze([
201
+ 'EcdsaSecp256r1VerificationKey2019',
202
+ 'EcdsaSecp256k1VerificationKey2019',
203
+ 'Multikey',
204
+ ] as const)
205
+ export type SupportedAtprotoVerificationMethodType =
206
+ (typeof ATPROTO_VERIFICATION_METHOD_TYPES)[number]
207
+
208
+ type VerificationMethod = NonNullable<DidDocument['verificationMethod']>[number]
209
+ export type AtprotoVerificationMethod<
210
+ M extends AtprotoIdentityDidMethods = AtprotoIdentityDidMethods,
211
+ > = Extract<VerificationMethod, object> & {
212
+ id: Identifier<Did<M>, 'atproto'>
213
+ type: SupportedAtprotoVerificationMethodType
214
+ publicKeyMultibase: string
215
+ }
216
+
217
+ export function isAtprotoVerificationMethod<
218
+ M extends AtprotoIdentityDidMethods = AtprotoIdentityDidMethods,
219
+ >(
220
+ this: DidDocument<M>,
221
+ method:
222
+ | null
223
+ | undefined
224
+ | NonNullable<DidDocument<M>['verificationMethod']>[number],
225
+ ): method is AtprotoVerificationMethod<M> {
164
226
  return (
165
- typeof s.serviceEndpoint === 'string' &&
166
- s.type === 'AtprotoPersonalDataServer' &&
167
- (s.id.startsWith('#')
168
- ? s.id === '#atproto_pds'
169
- : s.id === `${this.id}#atproto_pds`)
227
+ typeof method === 'object' &&
228
+ typeof method?.publicKeyMultibase === 'string' &&
229
+ (ATPROTO_VERIFICATION_METHOD_TYPES as readonly unknown[]).includes(
230
+ method.type,
231
+ ) &&
232
+ matchesIdentifier(this.id, 'atproto', method.id)
170
233
  )
171
234
  }
package/src/index.ts CHANGED
@@ -3,3 +3,4 @@ export * from './did-document.js'
3
3
  export * from './did-error.js'
4
4
  export * from './did.js'
5
5
  export * from './methods.js'
6
+ export * from './utils.js'
package/src/lib/uri.ts CHANGED
@@ -65,3 +65,14 @@ export function isHexDigit(code: number): boolean {
65
65
  (code >= 97 && code <= 102) // a-f
66
66
  )
67
67
  }
68
+
69
+ export const canParse =
70
+ URL.canParse?.bind(URL) ??
71
+ ((url, base) => {
72
+ try {
73
+ new URL(url, base)
74
+ return true
75
+ } catch {
76
+ return false
77
+ }
78
+ })
@@ -1,19 +1,9 @@
1
1
  import { InvalidDidError } from '../did-error.js'
2
2
  import { Did, assertDidMsid } from '../did.js'
3
+ import { canParse } from '../lib/uri.js'
3
4
 
4
5
  export const DID_WEB_PREFIX = `did:web:` satisfies Did<'web'>
5
6
 
6
- const canParse =
7
- URL.canParse?.bind(URL) ??
8
- ((url, base) => {
9
- try {
10
- new URL(url, base)
11
- return true
12
- } catch {
13
- return false
14
- }
15
- })
16
-
17
7
  /**
18
8
  * This function checks if the input is a valid Web DID, as per DID spec.
19
9
  */
package/src/utils.ts ADDED
@@ -0,0 +1,18 @@
1
+ export type Identifier<D extends string, I extends string> =
2
+ | `#${I}`
3
+ | `${D}#${I}`
4
+ export function matchesIdentifier<D extends string, I extends string>(
5
+ did: D,
6
+ id: I,
7
+ candidate: string,
8
+ ): candidate is Identifier<D, I> {
9
+ // optimized implementation of:
10
+ // return candidate === `#${id}` || candidate === `${did}#${id}`
11
+
12
+ return candidate.charCodeAt(0) === 35 // '#'
13
+ ? candidate.length === id.length + 1 && candidate.endsWith(id)
14
+ : candidate.length === id.length + 1 + did.length &&
15
+ candidate.charCodeAt(did.length) === 35 && // '#'
16
+ candidate.startsWith(did) &&
17
+ candidate.endsWith(id)
18
+ }
@@ -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/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":"5.8.2"}