@dxos/keys 0.7.5-labs.35b4b42 → 0.7.5-labs.a279d8c

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/src/dxn.ts CHANGED
@@ -41,8 +41,7 @@ export class DXN {
41
41
  */
42
42
  ECHO: 'echo',
43
43
  /**
44
- * The subspace tag enables us to partition queues by usage within the context of a space.
45
- * dxn:queue:<subspace_tag>:<space_id>:<queue_id>[:object_id]
44
+ * dxn:queue:<subspace tag>:<space id>:<queue id>[:object id]
46
45
  * dxn:queue:data:BA25QRC2FEWCSAMRP4RZL65LWJ7352CKE:01J00J9B45YHYSGZQTQMSKMGJ6
47
46
  * dxn:queue:trace:BA25QRC2FEWCSAMRP4RZL65LWJ7352CKE:01J00J9B45YHYSGZQTQMSKMGJ6
48
47
  */
@@ -57,32 +56,38 @@ export class DXN {
57
56
  return dxn.startsWith('dxn:');
58
57
  }
59
58
 
60
- static parse(dxn: string): DXN {
59
+ static parse(dxn: string): DXN;
60
+ static parse(dxn: string, noThrow: boolean): DXN | undefined;
61
+ static parse(dxn: string, noThrow?: boolean): DXN | undefined {
61
62
  if (typeof dxn !== 'string') {
63
+ if (noThrow) {
64
+ return undefined;
65
+ }
62
66
  throw new Error(`Invalid DXN: ${dxn}`);
63
67
  }
64
68
  const [prefix, kind, ...parts] = dxn.split(':');
65
69
  if (!(prefix === 'dxn')) {
70
+ if (noThrow) {
71
+ return undefined;
72
+ }
66
73
  throw new Error(`Invalid DXN: ${dxn}`);
67
74
  }
68
75
  if (!(typeof kind === 'string' && kind.length > 0)) {
76
+ if (noThrow) {
77
+ return undefined;
78
+ }
69
79
  throw new Error(`Invalid DXN: ${dxn}`);
70
80
  }
71
81
  if (!(parts.length > 0)) {
82
+ if (noThrow) {
83
+ return undefined;
84
+ }
72
85
  throw new Error(`Invalid DXN: ${dxn}`);
73
86
  }
74
87
 
75
88
  return new DXN(kind, parts);
76
89
  }
77
90
 
78
- static tryParse(dxn: string) {
79
- try {
80
- return DXN.parse(dxn);
81
- } catch (error) {
82
- return undefined;
83
- }
84
- }
85
-
86
91
  /**
87
92
  * @example `dxn:type:example.com/type/Contact`
88
93
  */
@@ -180,7 +185,6 @@ export class DXN {
180
185
  if (typeof queueId !== 'string') {
181
186
  return undefined;
182
187
  }
183
-
184
188
  return {
185
189
  subspaceTag,
186
190
  spaceId: spaceId as SpaceId,
package/src/index.ts CHANGED
@@ -2,7 +2,6 @@
2
2
  // Copyright 2020 DXOS.org
3
3
  //
4
4
 
5
- export * from './identity-did';
6
5
  export * from './public-key';
7
6
  export * from './types';
8
7
  export * from './space-id';
@@ -1,16 +0,0 @@
1
- /**
2
- * A unique identifier for a space.
3
- * Space keys are generated by creating a keypair, and then taking the first 20 bytes of the SHA-256 hash of the public key and encoding them to multibase RFC4648 base-32 format (prefixed with B, see Multibase Table).
4
- * @example did:halo:BA25QRC2FEWCSAMRP4RZL65LWJ7352CKE
5
- */
6
- export type IdentityDid = string & {
7
- __IdentityDid: true;
8
- };
9
- export declare const IdentityDid: Readonly<{
10
- byteLength: 20;
11
- encode: (value: Uint8Array) => IdentityDid;
12
- decode: (value: IdentityDid) => Uint8Array;
13
- isValid: (value: string) => value is IdentityDid;
14
- random: () => IdentityDid;
15
- }>;
16
- //# sourceMappingURL=identity-did.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"identity-did.d.ts","sourceRoot":"","sources":["../../../src/identity-did.ts"],"names":[],"mappings":"AAWA;;;;GAIG;AACH,MAAM,MAAM,WAAW,GAAG,MAAM,GAAG;IAAE,aAAa,EAAE,IAAI,CAAA;CAAE,CAAC;AAE3D,eAAO,MAAM,WAAW;;oBAEN,UAAU,KAAG,WAAW;oBAMxB,WAAW,KAAG,UAAU;qBAKvB,MAAM,KAAG,KAAK,IAAI,WAAW;kBAKlC,WAAW;EAGvB,CAAC"}
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=identity-did.test.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"identity-did.test.d.ts","sourceRoot":"","sources":["../../../src/identity-did.test.ts"],"names":[],"mappings":""}
@@ -1,17 +0,0 @@
1
- //
2
- // Copyright 2024 DXOS.org
3
- //
4
-
5
- import { expect, test } from 'vitest';
6
-
7
- import { IdentityDid } from './identity-did';
8
-
9
- test('identity-did', () => {
10
- const id = IdentityDid.random();
11
-
12
- expect(id.length).toBe(42);
13
- expect(IdentityDid.isValid(id)).toBe(true);
14
- const decoded = IdentityDid.decode(id);
15
- expect(decoded.length).toBe(IdentityDid.byteLength);
16
- expect(IdentityDid.encode(decoded)).toBe(id);
17
- });
@@ -1,49 +0,0 @@
1
- //
2
- // Copyright 2024 DXOS.org
3
- //
4
-
5
- import base32Decode from 'base32-decode';
6
- import base32Encode from 'base32-encode';
7
-
8
- import { invariant } from '@dxos/invariant';
9
-
10
- import { randomBytes } from './random-bytes';
11
-
12
- /**
13
- * A unique identifier for a space.
14
- * Space keys are generated by creating a keypair, and then taking the first 20 bytes of the SHA-256 hash of the public key and encoding them to multibase RFC4648 base-32 format (prefixed with B, see Multibase Table).
15
- * @example did:halo:BA25QRC2FEWCSAMRP4RZL65LWJ7352CKE
16
- */
17
- export type IdentityDid = string & { __IdentityDid: true };
18
-
19
- export const IdentityDid = Object.freeze({
20
- byteLength: 20,
21
- encode: (value: Uint8Array): IdentityDid => {
22
- invariant(value instanceof Uint8Array, 'Invalid type');
23
- invariant(value.length === IdentityDid.byteLength, 'Invalid length');
24
-
25
- return (DID_PREFIX + MULTIBASE_PREFIX + base32Encode(value, 'RFC4648')) as IdentityDid;
26
- },
27
- decode: (value: IdentityDid): Uint8Array => {
28
- invariant(value.startsWith(DID_PREFIX + MULTIBASE_PREFIX), 'Invalid multibase32 encoding');
29
-
30
- return new Uint8Array(base32Decode(value.slice(10), 'RFC4648'));
31
- },
32
- isValid: (value: string): value is IdentityDid => {
33
- return (
34
- typeof value === 'string' && value.startsWith(DID_PREFIX + MULTIBASE_PREFIX) && value.length === ENCODED_LENGTH
35
- );
36
- },
37
- random: (): IdentityDid => {
38
- return IdentityDid.encode(randomBytes(IdentityDid.byteLength));
39
- },
40
- });
41
-
42
- /**
43
- * Denotes RFC4648 base-32 format.
44
- */
45
- const MULTIBASE_PREFIX = 'B';
46
-
47
- const DID_PREFIX = 'did:halo:';
48
-
49
- const ENCODED_LENGTH = 42;