@atproto/identity 0.1.0 → 0.2.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.
@@ -4,5 +4,6 @@ export declare const getKey: (doc: DidDocument) => string | undefined;
4
4
  export declare const getHandle: (doc: DidDocument) => string | undefined;
5
5
  export declare const getPds: (doc: DidDocument) => string | undefined;
6
6
  export declare const getFeedGen: (doc: DidDocument) => string | undefined;
7
+ export declare const getNotif: (doc: DidDocument) => string | undefined;
7
8
  export declare const parseToAtprotoDocument: (doc: DidDocument) => Partial<AtprotoData>;
8
9
  export declare const ensureAtpDocument: (doc: DidDocument) => AtprotoData;
package/dist/index.js CHANGED
@@ -21279,6 +21279,7 @@ __export(src_exports3, {
21279
21279
  getFeedGen: () => getFeedGen,
21280
21280
  getHandle: () => getHandle,
21281
21281
  getKey: () => getKey,
21282
+ getNotif: () => getNotif,
21282
21283
  getPds: () => getPds,
21283
21284
  parseToAtprotoDocument: () => parseToAtprotoDocument,
21284
21285
  service: () => service,
@@ -21292,7 +21293,8 @@ var import_axios = __toESM(require_axios2());
21292
21293
  // ../crypto/src/const.ts
21293
21294
  var P256_DID_PREFIX = new Uint8Array([128, 36]);
21294
21295
  var SECP256K1_DID_PREFIX = new Uint8Array([231, 1]);
21295
- var BASE58_DID_PREFIX = "did:key:z";
21296
+ var BASE58_MULTIBASE_PREFIX = "z";
21297
+ var DID_KEY_PREFIX = "did:key:";
21296
21298
  var P256_JWT_ALG = "ES256";
21297
21299
  var SECP256K1_JWT_ALG = "ES256K";
21298
21300
 
@@ -24326,12 +24328,12 @@ var plugins = [plugin_default, plugin_default2];
24326
24328
  var plugins_default = plugins;
24327
24329
 
24328
24330
  // ../crypto/src/did.ts
24329
- var parseDidKey = (did) => {
24330
- if (!did.startsWith(BASE58_DID_PREFIX)) {
24331
- throw new Error(`Incorrect prefix for did:key: ${did}`);
24331
+ var parseMultikey = (multikey) => {
24332
+ if (!multikey.startsWith(BASE58_MULTIBASE_PREFIX)) {
24333
+ throw new Error(`Incorrect prefix for multikey: ${multikey}`);
24332
24334
  }
24333
24335
  const prefixedBytes = fromString2(
24334
- did.slice(BASE58_DID_PREFIX.length),
24336
+ multikey.slice(BASE58_MULTIBASE_PREFIX.length),
24335
24337
  "base58btc"
24336
24338
  );
24337
24339
  const plugin = plugins_default.find((p) => hasPrefix(prefixedBytes, p.prefix));
@@ -24349,7 +24351,7 @@ var parseDidKey = (did) => {
24349
24351
  keyBytes
24350
24352
  };
24351
24353
  };
24352
- var formatDidKey = (jwtAlg, keyBytes) => {
24354
+ var formatMultikey = (jwtAlg, keyBytes) => {
24353
24355
  const plugin = plugins_default.find((p) => p.jwtAlg === jwtAlg);
24354
24356
  if (!plugin) {
24355
24357
  throw new Error("Unsupported key type");
@@ -24360,7 +24362,16 @@ var formatDidKey = (jwtAlg, keyBytes) => {
24360
24362
  keyBytes = compressPubkey2(keyBytes);
24361
24363
  }
24362
24364
  const prefixedBytes = concat([plugin.prefix, keyBytes]);
24363
- return BASE58_DID_PREFIX + toString2(prefixedBytes, "base58btc");
24365
+ return BASE58_MULTIBASE_PREFIX + toString2(prefixedBytes, "base58btc");
24366
+ };
24367
+ var parseDidKey = (did) => {
24368
+ if (!did.startsWith(DID_KEY_PREFIX)) {
24369
+ throw new Error(`Incorrect prefix for did:key: ${did}`);
24370
+ }
24371
+ return parseMultikey(did.slice(DID_KEY_PREFIX.length));
24372
+ };
24373
+ var formatDidKey = (jwtAlg, keyBytes) => {
24374
+ return DID_KEY_PREFIX + formatMultikey(jwtAlg, keyBytes);
24364
24375
  };
24365
24376
  var hasPrefix = (bytes2, prefix) => {
24366
24377
  return equals(prefix, bytes2.subarray(0, prefix.byteLength));
@@ -28080,6 +28091,7 @@ var getDid = (doc) => {
28080
28091
  return id;
28081
28092
  };
28082
28093
  var getKey = (doc) => {
28094
+ const did = getDid(doc);
28083
28095
  let keys = doc.verificationMethod;
28084
28096
  if (!keys)
28085
28097
  return void 0;
@@ -28088,7 +28100,9 @@ var getKey = (doc) => {
28088
28100
  if (!Array.isArray(keys)) {
28089
28101
  keys = [keys];
28090
28102
  }
28091
- const found = keys.find((key) => key.id === "#atproto");
28103
+ const found = keys.find(
28104
+ (key) => key.id === "#atproto" || key.id === `${did}#atproto`
28105
+ );
28092
28106
  if (!found)
28093
28107
  return void 0;
28094
28108
  if (!found.publicKeyMultibase)
@@ -28099,6 +28113,9 @@ var getKey = (doc) => {
28099
28113
  didKey = formatDidKey(P256_JWT_ALG, keyBytes);
28100
28114
  } else if (found.type === "EcdsaSecp256k1VerificationKey2019") {
28101
28115
  didKey = formatDidKey(SECP256K1_JWT_ALG, keyBytes);
28116
+ } else if (found.type === "Multikey") {
28117
+ const parsed = parseMultikey(found.publicKeyMultibase);
28118
+ didKey = formatDidKey(parsed.jwtAlg, parsed.keyBytes);
28102
28119
  }
28103
28120
  return didKey;
28104
28121
  };
@@ -28112,46 +28129,22 @@ var getHandle = (doc) => {
28112
28129
  return found.slice(5);
28113
28130
  };
28114
28131
  var getPds = (doc) => {
28115
- let services = doc.service;
28116
- if (!services)
28117
- return void 0;
28118
- if (typeof services !== "object")
28119
- return void 0;
28120
- if (!Array.isArray(services)) {
28121
- services = [services];
28122
- }
28123
- const found = services.find((service2) => service2.id === "#atproto_pds");
28124
- if (!found)
28125
- return void 0;
28126
- if (found.type !== "AtprotoPersonalDataServer") {
28127
- return void 0;
28128
- }
28129
- if (typeof found.serviceEndpoint !== "string") {
28130
- return void 0;
28131
- }
28132
- validateUrl(found.serviceEndpoint);
28133
- return found.serviceEndpoint;
28132
+ return getServiceEndpoint(doc, {
28133
+ id: "#atproto_pds",
28134
+ type: "AtprotoPersonalDataServer"
28135
+ });
28134
28136
  };
28135
28137
  var getFeedGen = (doc) => {
28136
- let services = doc.service;
28137
- if (!services)
28138
- return void 0;
28139
- if (typeof services !== "object")
28140
- return void 0;
28141
- if (!Array.isArray(services)) {
28142
- services = [services];
28143
- }
28144
- const found = services.find((service2) => service2.id === "#bsky_fg");
28145
- if (!found)
28146
- return void 0;
28147
- if (found.type !== "BskyFeedGenerator") {
28148
- return void 0;
28149
- }
28150
- if (typeof found.serviceEndpoint !== "string") {
28151
- return void 0;
28152
- }
28153
- validateUrl(found.serviceEndpoint);
28154
- return found.serviceEndpoint;
28138
+ return getServiceEndpoint(doc, {
28139
+ id: "#bsky_fg",
28140
+ type: "BskyFeedGenerator"
28141
+ });
28142
+ };
28143
+ var getNotif = (doc) => {
28144
+ return getServiceEndpoint(doc, {
28145
+ id: "#bsky_notif",
28146
+ type: "BskyNotificationService"
28147
+ });
28155
28148
  };
28156
28149
  var parseToAtprotoDocument = (doc) => {
28157
28150
  const did = getDid(doc);
@@ -28187,6 +28180,30 @@ var validateUrl = (url) => {
28187
28180
  throw new Error("Invalid pds hostname");
28188
28181
  }
28189
28182
  };
28183
+ var getServiceEndpoint = (doc, opts) => {
28184
+ const did = getDid(doc);
28185
+ let services = doc.service;
28186
+ if (!services)
28187
+ return void 0;
28188
+ if (typeof services !== "object")
28189
+ return void 0;
28190
+ if (!Array.isArray(services)) {
28191
+ services = [services];
28192
+ }
28193
+ const found = services.find(
28194
+ (service2) => service2.id === opts.id || service2.id === `${did}${opts.id}`
28195
+ );
28196
+ if (!found)
28197
+ return void 0;
28198
+ if (found.type !== opts.type) {
28199
+ return void 0;
28200
+ }
28201
+ if (typeof found.serviceEndpoint !== "string") {
28202
+ return void 0;
28203
+ }
28204
+ validateUrl(found.serviceEndpoint);
28205
+ return found.serviceEndpoint;
28206
+ };
28190
28207
 
28191
28208
  // src/errors.ts
28192
28209
  var DidNotFoundError = class extends Error {
@@ -28532,6 +28549,7 @@ var IdResolver = class {
28532
28549
  getFeedGen,
28533
28550
  getHandle,
28534
28551
  getKey,
28552
+ getNotif,
28535
28553
  getPds,
28536
28554
  parseToAtprotoDocument,
28537
28555
  service,