@did-btcr2/common 4.0.1 → 7.0.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.
Files changed (77) hide show
  1. package/dist/cjs/canonicalization.js +95 -197
  2. package/dist/cjs/canonicalization.js.map +1 -1
  3. package/dist/cjs/errors.js +5 -8
  4. package/dist/cjs/errors.js.map +1 -1
  5. package/dist/cjs/index.js +0 -4
  6. package/dist/cjs/index.js.map +1 -1
  7. package/dist/cjs/json-patch.js +1 -1
  8. package/dist/cjs/json-patch.js.map +1 -1
  9. package/dist/cjs/types.js.map +1 -1
  10. package/dist/cjs/utils/date.js +3 -4
  11. package/dist/cjs/utils/date.js.map +1 -1
  12. package/dist/cjs/utils/json.js +2 -9
  13. package/dist/cjs/utils/json.js.map +1 -1
  14. package/dist/esm/canonicalization.js +95 -197
  15. package/dist/esm/canonicalization.js.map +1 -1
  16. package/dist/esm/errors.js +5 -8
  17. package/dist/esm/errors.js.map +1 -1
  18. package/dist/esm/index.js +0 -4
  19. package/dist/esm/index.js.map +1 -1
  20. package/dist/esm/json-patch.js +1 -1
  21. package/dist/esm/json-patch.js.map +1 -1
  22. package/dist/esm/types.js.map +1 -1
  23. package/dist/esm/utils/date.js +3 -4
  24. package/dist/esm/utils/date.js.map +1 -1
  25. package/dist/esm/utils/json.js +2 -9
  26. package/dist/esm/utils/json.js.map +1 -1
  27. package/dist/types/canonicalization.d.ts +46 -118
  28. package/dist/types/canonicalization.d.ts.map +1 -1
  29. package/dist/types/errors.d.ts +8 -7
  30. package/dist/types/errors.d.ts.map +1 -1
  31. package/dist/types/index.d.ts +1 -4
  32. package/dist/types/index.d.ts.map +1 -1
  33. package/dist/types/json-patch.d.ts +11 -1
  34. package/dist/types/json-patch.d.ts.map +1 -1
  35. package/dist/types/types.d.ts +8 -28
  36. package/dist/types/types.d.ts.map +1 -1
  37. package/dist/types/utils/date.d.ts +4 -4
  38. package/dist/types/utils/date.d.ts.map +1 -1
  39. package/dist/types/utils/json.d.ts +2 -16
  40. package/dist/types/utils/json.d.ts.map +1 -1
  41. package/dist/types/utils/string.d.ts +1 -0
  42. package/package.json +11 -5
  43. package/src/canonicalization.ts +100 -216
  44. package/src/errors.ts +8 -16
  45. package/src/index.ts +0 -4
  46. package/src/json-patch.ts +12 -2
  47. package/src/types.ts +7 -30
  48. package/src/utils/date.ts +3 -4
  49. package/src/utils/json.ts +3 -10
  50. package/dist/cjs/constants.js +0 -119
  51. package/dist/cjs/constants.js.map +0 -1
  52. package/dist/cjs/interfaces.js +0 -2
  53. package/dist/cjs/interfaces.js.map +0 -1
  54. package/dist/cjs/logger.js +0 -155
  55. package/dist/cjs/logger.js.map +0 -1
  56. package/dist/cjs/utils/set.js +0 -23
  57. package/dist/cjs/utils/set.js.map +0 -1
  58. package/dist/esm/constants.js +0 -119
  59. package/dist/esm/constants.js.map +0 -1
  60. package/dist/esm/interfaces.js +0 -2
  61. package/dist/esm/interfaces.js.map +0 -1
  62. package/dist/esm/logger.js +0 -155
  63. package/dist/esm/logger.js.map +0 -1
  64. package/dist/esm/utils/set.js +0 -23
  65. package/dist/esm/utils/set.js.map +0 -1
  66. package/dist/types/constants.d.ts +0 -97
  67. package/dist/types/constants.d.ts.map +0 -1
  68. package/dist/types/interfaces.d.ts +0 -11
  69. package/dist/types/interfaces.d.ts.map +0 -1
  70. package/dist/types/logger.d.ts +0 -64
  71. package/dist/types/logger.d.ts.map +0 -1
  72. package/dist/types/utils/set.d.ts +0 -14
  73. package/dist/types/utils/set.d.ts.map +0 -1
  74. package/src/constants.ts +0 -127
  75. package/src/interfaces.ts +0 -11
  76. package/src/logger.ts +0 -173
  77. package/src/utils/set.ts +0 -23
@@ -1,241 +1,125 @@
1
1
  import { sha256 } from '@noble/hashes/sha2';
2
- import { bytesToHex, hexToBytes } from '@noble/hashes/utils';
2
+ import { base58, base64urlnopad, hex } from '@scure/base';
3
3
  import { canonicalize as jcsa } from 'json-canonicalize';
4
- import { base58btc } from 'multiformats/bases/base58';
5
4
  import { CanonicalizationError } from './errors.js';
6
- import { CanonicalizationAlgorithm, CanonicalizationEncoding, HashBytes, HexString } from './types.js';
5
+ import { HashBytes } from './types.js';
6
+
7
+ export type CanonicalizationAlgorithm = 'jcs' | 'rdfc';
8
+ export type CanonicalizationEncoding = 'hex' | 'base58' | 'base64url';
7
9
 
8
10
  export interface CanonicalizationOptions {
9
11
  algorithm?: CanonicalizationAlgorithm;
10
12
  encoding?: CanonicalizationEncoding;
11
13
  }
12
14
 
15
+ const SUPPORTED_ALGORITHMS: ReadonlySet<CanonicalizationAlgorithm> = new Set(['jcs']);
16
+ const SUPPORTED_ENCODINGS: ReadonlySet<CanonicalizationEncoding> = new Set(['hex', 'base58', 'base64url']);
17
+
13
18
  /**
14
- * Canonicalization class provides methods for canonicalizing JSON objects
15
- * and hashing them using SHA-256. It supports different canonicalization
16
- * algorithms and encoding formats (hex and base58).
17
- * @class Canonicalization
18
- * @type {Canonicalization}
19
+ * Normalizes and validates the canonicalization algorithm.
20
+ * @param {CanonicalizationAlgorithm} algorithm - The algorithm to normalize.
21
+ * @returns {CanonicalizationAlgorithm} The normalized algorithm.
22
+ * @throws {CanonicalizationError} If the algorithm is not supported.
19
23
  */
20
- export class Canonicalization {
21
- /**
22
- * Normalizes the canonicalization algorithm.
23
- * @param {CanonicalizationAlgorithm} algorithm
24
- * @returns {CanonicalizationAlgorithm} The normalized algorithm.
25
- * @throws {CanonicalizationError} If the algorithm is not supported.
26
- */
27
- static normalizeAlgorithm(algorithm: CanonicalizationAlgorithm): CanonicalizationAlgorithm {
28
- const normalized = algorithm.toLowerCase() as CanonicalizationAlgorithm;
29
- if (normalized !== 'jcs') {
30
- throw new CanonicalizationError(`Unsupported algorithm: ${algorithm}`, 'ALGORITHM_ERROR');
31
- }
32
- return normalized;
33
- }
34
-
35
- /**
36
- * Normalizes the canonicalization encoding.
37
- * @param {CanonicalizationEncoding} encoding - The encoding to normalize.
38
- * @returns {CanonicalizationEncoding} The normalized encoding.
39
- * @throws {CanonicalizationError} If the encoding is not supported.
40
- */
41
- static normalizeEncoding(encoding: CanonicalizationEncoding): CanonicalizationEncoding {
42
- const normalized = encoding.toLowerCase() as CanonicalizationEncoding;
43
- if (normalized !== 'hex' && normalized !== 'base58') {
44
- throw new CanonicalizationError(`Unsupported encoding: ${encoding}`, 'ENCODING_ERROR');
45
- }
46
- return normalized;
24
+ function normalizeAlgorithm(algorithm: CanonicalizationAlgorithm): CanonicalizationAlgorithm {
25
+ const lower = algorithm.toLowerCase();
26
+ if (!SUPPORTED_ALGORITHMS.has(lower as CanonicalizationAlgorithm)) {
27
+ throw new CanonicalizationError(`Unsupported algorithm: ${algorithm}`, 'ALGORITHM_ERROR');
47
28
  }
29
+ return lower as CanonicalizationAlgorithm;
30
+ }
48
31
 
49
- /**
50
- * Implements {@link http://dcdpr.github.io/did-btcr2/#json-canonicalization-and-hash | 9.2 JSON Canonicalization and Hash}.
51
- *
52
- * A macro function that takes in a JSON document, document, and canonicalizes it following the JSON Canonicalization
53
- * Scheme. The function returns the canonicalizedBytes.
54
- *
55
- * Optionally encodes a sha256 hashed canonicalized JSON object.
56
- * Step 1 Canonicalize (JCS) → Step 2 Hash (SHA256) → Step 3 Encode (Hex/Base58).
57
- *
58
- * @param {Record<any, any>} object The object to process.
59
- * @param {Object} [options] Options for processing.
60
- * @param {CanonicalizationEncoding} [options.encoding='hex'] The encoding format ('hex' or 'base58').
61
- * @param {CanonicalizationAlgorithm} [options.algorithm] The canonicalization algorithm to use.
62
- * @returns {string} The final SHA-256 hash bytes as a hex string.
63
- */
64
- static process(object: Record<any, any>, options?: CanonicalizationOptions): string {
65
- // Normalize the algorithm
66
- const algorithm = Canonicalization.normalizeAlgorithm(options?.algorithm ?? 'jcs');
67
- // Normalize the encoding
68
- const encoding = Canonicalization.normalizeEncoding(options?.encoding ?? 'hex');
69
-
70
- // Step 1: Canonicalize
71
- const canonicalized = this.canonicalize(object, algorithm);
72
- // Step 2: Hash
73
- const hashed = this.toHash(canonicalized);
74
- // Step 3: Encode
75
- const encoded = this.encode(hashed, encoding);
76
- // Return the encoded string
77
- return encoded;
78
- }
79
-
80
- /**
81
- * Step 1: Uses this.algorithm to determine the method (JCS).
82
- * @param {Record<any, any>} object The object to canonicalize.
83
- * @param {CanonicalizationAlgorithm} [algorithm] The algorithm to use.
84
- * @returns {string} The canonicalized object.
85
- */
86
- static canonicalize(object: Record<any, any>, algorithm: CanonicalizationAlgorithm = 'jcs'): string {
87
- switch (Canonicalization.normalizeAlgorithm(algorithm)) {
88
- case 'jcs':
89
- return this.jcs(object);
90
- default:
91
- throw new CanonicalizationError(`Unsupported algorithm: ${algorithm}`, 'ALGORITHM_ERROR');
92
- }
93
- }
94
-
95
- /**
96
- * Step 1: Canonicalizes an object using JCS (JSON Canonicalization Scheme).
97
- * @param {Record<any, any>} object The object to canonicalize.
98
- * @returns {string} The canonicalized object.
99
- */
100
- static jcs(object: Record<any, any>): string {
101
- return jcsa(object);
102
- }
103
-
104
- /**
105
- * Step 2: SHA-256 hashes a canonicalized object.
106
- * @param {string} canonicalized The canonicalized object.
107
- * @returns {HashBytes} The SHA-256 HashBytes (Uint8Array).
108
- */
109
- static toHash(canonicalized: string): HashBytes {
110
- return sha256(canonicalized);
111
- }
112
-
113
- /**
114
- * Step 3: Encodes SHA-256 hashed, canonicalized object as a hex or base58 string.
115
- * @param {string} canonicalizedhash The canonicalized object to encode.
116
- * @param {CanonicalizationEncoding} encoding The encoding format ('hex' or 'base58').
117
- * @throws {CanonicalizationError} If the encoding format is not supported.
118
- * @returns {string} The encoded string.
119
- */
120
- static encode(canonicalizedhash: HashBytes, encoding: CanonicalizationEncoding = 'hex'): string {
121
- // Normalize encoding
122
- const normalized = Canonicalization.normalizeEncoding(encoding);
123
-
124
- // If encoding is hex, encode to hex
125
- if (normalized === 'hex') {
126
- return this.toHex(canonicalizedhash);
127
- }
128
-
129
- // If encoding is base58, encode to base58
130
- if (normalized === 'base58') {
131
- return this.toBase58(canonicalizedhash);
132
- }
133
-
134
- // Throw error if encoding is unsupported
32
+ /**
33
+ * Normalizes and validates the canonicalization encoding.
34
+ * @param {CanonicalizationEncoding} encoding - The encoding to normalize.
35
+ * @returns {CanonicalizationEncoding} The normalized encoding.
36
+ * @throws {CanonicalizationError} If the encoding is not supported.
37
+ */
38
+ function normalizeEncoding(encoding: CanonicalizationEncoding): CanonicalizationEncoding {
39
+ const lower = encoding.toLowerCase();
40
+ if (!SUPPORTED_ENCODINGS.has(lower as CanonicalizationEncoding)) {
135
41
  throw new CanonicalizationError(`Unsupported encoding: ${encoding}`, 'ENCODING_ERROR');
136
42
  }
43
+ return lower as CanonicalizationEncoding;
44
+ }
137
45
 
138
- /**
139
- * Decodes SHA-256 hashed, canonicalized object as a hex or base58 string.
140
- * @param {string} canonicalizedhash The canonicalized object to encode.
141
- * @param {CanonicalizationEncoding} encoding The encoding format ('hex' or 'base58').
142
- * @throws {CanonicalizationError} If the encoding format is not supported.
143
- * @returns {string} The encoded string.
144
- */
145
- static decode(canonicalizedhash: string, encoding: CanonicalizationEncoding = 'hex'): HashBytes {
146
- // Normalize encoding
147
- const normalized = Canonicalization.normalizeEncoding(encoding);
148
-
149
- // If encoding is hex, decode from hex
150
- if (normalized === 'hex') {
151
- return this.fromHex(canonicalizedhash);
152
- }
153
-
154
- // If encoding is base58, decode from base58
155
- if (normalized === 'base58') {
156
- return this.fromBase58(canonicalizedhash);
46
+ /**
47
+ * Canonicalizes a JSON object using the specified algorithm.
48
+ *
49
+ * @param {Record<any, any>} object - The object to canonicalize.
50
+ * @param {CanonicalizationAlgorithm} [algorithm='jcs'] - The algorithm to use.
51
+ * @returns {string} The canonicalized string.
52
+ * @throws {CanonicalizationError} If the algorithm is not supported.
53
+ */
54
+ export function canonicalize(object: Record<any, any>, algorithm: CanonicalizationAlgorithm = 'jcs'): string {
55
+ const normalized = normalizeAlgorithm(algorithm);
56
+ switch (normalized) {
57
+ case 'jcs': {
58
+ // Round-trip to a plain object so JCS always sees the same key set
59
+ // regardless of whether the input is a class instance or a POJO.
60
+ const plain = JSON.parse(JSON.stringify(object));
61
+ return jcsa(plain);
157
62
  }
158
-
159
- // Throw error if encoding is unsupported
160
- throw new CanonicalizationError(`Unsupported encoding: ${encoding}`, 'DECODING_ERROR');
161
- }
162
-
163
- /**
164
- * Step 3.1: Encodes HashBytes (Uint8Array) to a hex string.
165
- * @param {HashBytes} hashBytes The hash as a Uint8Array.
166
- * @returns {string} The hash as a hex string.
167
- */
168
- static toHex(hashBytes: HashBytes): string {
169
- return bytesToHex(hashBytes);
170
- }
171
-
172
- /**
173
- * Decodes a hex string to HashBytes (Uint8Array).
174
- * @param {HexString} hexString The hash as a hex string.
175
- * @returns {HashBytes} The hash bytes.
176
- */
177
- static fromHex(hexString: HexString): HashBytes {
178
- return hexToBytes(hexString);
179
- }
180
-
181
- /**
182
- * Step 3.2: Encodes HashBytes (Uint8Array) to a base58btc string.
183
- * @param {HashBytes} hashBytes The hash as a Uint8Array.
184
- * @returns {string} The hash as a hex string.
185
- */
186
- static toBase58(hashBytes: HashBytes): string {
187
- return base58btc.encode(hashBytes);
63
+ default:
64
+ throw new CanonicalizationError(`Unsupported algorithm: ${algorithm}`, 'ALGORITHM_ERROR');
188
65
  }
66
+ }
189
67
 
190
- /**
191
- * Decodes a base58 string to HashBytes (Uint8Array).
192
- * @param {string} b58str The hash as a base58 string.
193
- * @returns {HashBytes} The hash bytes.
194
- */
195
- static fromBase58(b58str: string): HashBytes {
196
- return base58btc.decode(b58str);
197
- }
68
+ /**
69
+ * SHA-256 hashes a canonicalized string.
70
+ *
71
+ * @param {string} canonicalized - The canonicalized string to hash.
72
+ * @returns {HashBytes} The SHA-256 hash bytes (Uint8Array).
73
+ */
74
+ export function hash(canonicalized: string): HashBytes {
75
+ return sha256(canonicalized);
76
+ }
198
77
 
199
- /**
200
- * Canonicalizes an object, hashes it and returns it as hash bytes.
201
- * Step 1-2: Canonicalize → Hash.
202
- * @param {Record<any, any>} object The object to process.
203
- * @returns {Promise<HashBytes>} The final SHA-256 hash bytes.
204
- */
205
- static andHash(
206
- object: Record<any, any>,
207
- algorithm: CanonicalizationAlgorithm = 'jcs'
208
- ): HashBytes {
209
- // Step 1: Canonicalize
210
- const canonicalized = this.canonicalize(object, algorithm);
211
- // Step 2: Hash
212
- const hashed = this.toHash(canonicalized);
213
- // Return canonicalized hash bytes
214
- return hashed;
78
+ /**
79
+ * Encodes hash bytes using the specified encoding.
80
+ *
81
+ * @param {HashBytes} hashBytes - The hash bytes to encode.
82
+ * @param {CanonicalizationEncoding} [encoding='hex'] - The encoding format.
83
+ * @returns {string} The encoded string.
84
+ * @throws {CanonicalizationError} If the encoding is not supported.
85
+ */
86
+ export function encodeHash(hashBytes: HashBytes, encoding: CanonicalizationEncoding = 'hex'): string {
87
+ const normalized = normalizeEncoding(encoding);
88
+ switch (normalized) {
89
+ case 'hex': return hex.encode(hashBytes);
90
+ case 'base58': return base58.encode(hashBytes);
91
+ case 'base64url': return base64urlnopad.encode(hashBytes);
215
92
  }
93
+ }
216
94
 
217
- /**
218
- * Computes the SHA-256 hash of a canonicalized object and encodes it as a hex string.
219
- * Step 2-3: Hash → Encode(Hex).
220
- * @param {string} canonicalized The canonicalized object to hash.
221
- * @returns {string} The SHA-256 hash as a hex string.
222
- */
223
- static andHashToHex(canonicalized: string): string {
224
- // Step 2: Hash
225
- const hashed = this.toHash(canonicalized);
226
- // Step 3: Encode (Hex)
227
- const hexed = this.toHex(hashed);
228
- // Return the hashed encoded string
229
- return hexed;
95
+ /**
96
+ * Decodes an encoded hash string back to bytes.
97
+ *
98
+ * @param {string} encoded - The encoded hash string.
99
+ * @param {CanonicalizationEncoding} [encoding='hex'] - The encoding format.
100
+ * @returns {HashBytes} The decoded hash bytes.
101
+ * @throws {CanonicalizationError} If the encoding is not supported.
102
+ */
103
+ export function decodeHash(encoded: string, encoding: CanonicalizationEncoding = 'hex'): HashBytes {
104
+ const normalized = normalizeEncoding(encoding);
105
+ switch (normalized) {
106
+ case 'hex': return hex.decode(encoded);
107
+ case 'base58': return base58.decode(encoded);
108
+ case 'base64url': return base64urlnopad.decode(encoded);
230
109
  }
110
+ }
231
111
 
232
- /**
233
- * Computes the SHA-256 hashes of canonicalized object and encodes it as a base58 string.
234
- * Step 2-3: Hash → Encode(base58).
235
- * @param {string} canonicalized The canonicalized object to hash.
236
- * @returns {string} The SHA-256 hash as a base58 string.
237
- */
238
- static andHashToBase58(canonicalized: string): string {
239
- return this.encode(this.toHash(canonicalized), 'base58');
240
- }
112
+ /**
113
+ * Implements {@link http://dcdpr.github.io/did-btcr2/#json-canonicalization-and-hash | 9.2 JSON Canonicalization and Hash}.
114
+ *
115
+ * Full pipeline: Canonicalize (JCS) -> Hash (SHA-256) -> Encode.
116
+ *
117
+ * @param {Record<any, any>} object - The object to process.
118
+ * @param {CanonicalizationOptions} [options] - Options for algorithm and encoding.
119
+ * @returns {string} The encoded hash string.
120
+ */
121
+ export function canonicalHash(object: Record<any, any>, options?: CanonicalizationOptions): string {
122
+ const algorithm = normalizeAlgorithm(options?.algorithm ?? 'jcs');
123
+ const encoding = normalizeEncoding(options?.encoding ?? 'hex');
124
+ return encodeHash(hash(canonicalize(object, algorithm)), encoding);
241
125
  }
package/src/errors.ts CHANGED
@@ -82,7 +82,10 @@ export enum MethodErrorCode {
82
82
  INVALID_UPDATE = 'INVALID_UPDATE',
83
83
 
84
84
  /** The proof is missing or has a malformed domain field. */
85
- INVALID_DOMAIN_ERROR = 'INVALID_DOMAIN_ERROR'
85
+ INVALID_DOMAIN_ERROR = 'INVALID_DOMAIN_ERROR',
86
+
87
+ /** Options required for resolution are missing. */
88
+ MISSING_RESOLUTION_OPTIONS = 'MISSING_RESOLUTION_OPTIONS'
86
89
  }
87
90
 
88
91
  export const {
@@ -91,7 +94,6 @@ export const {
91
94
  INTERNAL_ERROR,
92
95
  INVALID_DID_DOCUMENT,
93
96
  INVALID_DID_UPDATE,
94
- INVALID_DID_DOCUMENT_LENGTH,
95
97
  INVALID_DID_URL,
96
98
  INVALID_PREVIOUS_DID_PROOF,
97
99
  INVALID_PUBLIC_KEY,
@@ -99,9 +101,6 @@ export const {
99
101
  INVALID_PUBLIC_KEY_LENGTH,
100
102
  INVALID_PUBLIC_KEY_TYPE,
101
103
  INVALID_SIGNATURE,
102
- NOT_FOUND,
103
- REPRESENTATION_NOT_SUPPORTED,
104
- UNSUPPORTED_PUBLIC_KEY_TYPE,
105
104
  PROOF_VERIFICATION_ERROR,
106
105
  PROOF_GENERATION_ERROR,
107
106
  PROOF_SERIALIZATION_ERROR,
@@ -110,11 +109,10 @@ export const {
110
109
  LATE_PUBLISHING_ERROR,
111
110
  INVALID_SIDECAR_DATA,
112
111
  MISSING_UPDATE_DATA,
113
- INVALID_UPDATE,
114
- INVALID_DOMAIN_ERROR
112
+ MISSING_RESOLUTION_OPTIONS
115
113
  } = MethodErrorCode;
116
114
 
117
- export type ErrorOptions = {
115
+ type ErrorOptions = {
118
116
  type?: string;
119
117
  name?: string;
120
118
  data?: any;
@@ -165,7 +163,7 @@ export class DidMethodError extends Error {
165
163
  }
166
164
 
167
165
  export class MethodError extends DidMethodError {
168
- constructor(message: string, type: string, data?: Record<string, any>) {
166
+ constructor(message: string, type: string = 'MethodError', data?: Record<string, any>) {
169
167
  super(message, { type, name: type, data });
170
168
  }
171
169
  }
@@ -184,7 +182,7 @@ export class UpdateError extends DidMethodError {
184
182
 
185
183
  export class ResolveError extends DidMethodError {
186
184
  constructor(message: string, type: string = 'ResolveError', data?: Record<string, any>) {
187
- super(message, { type, name: 'ResolveError', data });
185
+ super(message, { type, name: type, data });
188
186
  }
189
187
  }
190
188
 
@@ -254,12 +252,6 @@ export class CIDAggregateBeaconError extends DidMethodError {
254
252
  }
255
253
  }
256
254
 
257
- export class SMTAggregateBeaconError extends DidMethodError {
258
- constructor(message: string, type: string = 'SMTAggregateBeaconError', data?: Record<string, any>) {
259
- super(message, { type, name: type, data });
260
- }
261
- }
262
-
263
255
  export class CanonicalizationError extends DidMethodError {
264
256
  constructor(message: string, type: string = 'CanonicalizationError', data?: Record<string, any>) {
265
257
  super(message, { type, name: type, data });
package/src/index.ts CHANGED
@@ -1,11 +1,7 @@
1
1
  export * from './canonicalization.js';
2
- export * from './constants.js';
3
2
  export * from './errors.js';
4
- export * from './interfaces.js';
5
3
  export * from './json-patch.js';
6
- export * from './logger.js';
7
4
  export * from './types.js';
8
5
  export * from './utils/date.js';
9
6
  export * from './utils/json.js';
10
- export * from './utils/set.js';
11
7
  export * from './utils/string.js';
package/src/json-patch.ts CHANGED
@@ -1,10 +1,20 @@
1
1
  import jsonPatch, { Operation } from 'fast-json-patch';
2
2
  import { MethodError } from './errors.js';
3
- import { PatchOperation } from './interfaces.js';
4
3
  import { JSONObject } from './types.js';
5
4
 
6
5
  const { applyPatch, compare, deepClone } = jsonPatch;
7
6
 
7
+ export type PatchOpCode = 'add' | 'remove' | 'replace' | 'move' | 'copy' | 'test';
8
+ /**
9
+ * A JSON Patch operation, as defined in {@link https://datatracker.ietf.org/doc/html/rfc6902 | RFC 6902}.
10
+ */
11
+ export interface PatchOperation {
12
+ op: PatchOpCode;
13
+ path: string;
14
+ value?: unknown; // Required for add, replace, test
15
+ from?: string; // Required for move, copy
16
+ }
17
+
8
18
  /**
9
19
  * Thin wrapper around fast-json-patch to keep a stable API within this package.
10
20
  * @class JSONPatch
@@ -31,7 +41,7 @@ export class JSONPatch {
31
41
  throw new MethodError('Invalid JSON Patch operations', 'JSON_PATCH_APPLY_ERROR', { error: validationError });
32
42
  }
33
43
  try {
34
- const result = applyPatch(docClone, operations as Operation[], true, mutate);
44
+ const result = applyPatch(docClone, operations as Operation[], false, mutate);
35
45
  if (result.newDocument === undefined) {
36
46
  throw new MethodError('JSON Patch application failed', 'JSON_PATCH_APPLY_ERROR', { result });
37
47
  }
package/src/types.ts CHANGED
@@ -1,11 +1,9 @@
1
- import { HDKey } from '@scure/bip32';
1
+ import type { HDKey } from '@scure/bip32';
2
2
 
3
3
  /* Crypto Types */
4
4
  export type Bytes = Uint8Array;
5
5
  export type Hex = Bytes | string;
6
6
  export type HexString = string;
7
- export type SignatureHex = Hex;
8
- export type HashHex = Hex;
9
7
 
10
8
  export type DocumentBytes = Bytes;
11
9
  export type SignatureBytes = Bytes;
@@ -14,10 +12,6 @@ export type HashBytes = Bytes;
14
12
  export type MessageBytes = Bytes;
15
13
  export type Entropy = Bytes | bigint;
16
14
 
17
- export type CompressedPublicKeyParityByte = 0x02 | 0x03;
18
- export type Bip340Encoding = string;
19
- export type Base58BtcPrefix = 'z';
20
-
21
15
  export type KeyBytes = Bytes;
22
16
  export type Point = {
23
17
  x: Array<number>;
@@ -69,33 +63,16 @@ export enum BitcoinNetworkNames {
69
63
  }
70
64
  export type DecentralizedIdentifier = string;
71
65
  export type Did = DecentralizedIdentifier;
72
- export type BeaconUri = string;
73
66
  export type CanonicalizedProofConfig = string;
74
67
  export type CryptosuiteName = 'bip340-jcs-2025' | 'bip340-rdfc-2025';
75
- export type JsonPrimitive = string | number | boolean | null;
76
- export type JsonArray = JsonValue[];
77
- export type JsonValue = JsonPrimitive | JsonArray | JsonObject;
78
- export type JsonObject = { [key: string]: JsonValue };
79
- export type JSONObject = JsonObject; // JSON object: prototyped or unprototyped
68
+ type JsonPrimitive = string | number | boolean | null;
69
+ type JsonArray = JsonValue[];
70
+ type JsonValue = JsonPrimitive | JsonArray | JsonObject;
71
+ type JsonObject = { [key: string]: JsonValue };
72
+ export type JSONObject = JsonObject;
80
73
  export type Prototyped = JSONObject;
81
74
  export type Unprototyped = JSONObject;
82
- export type ContextObject = Record<string, JsonValue>;
83
- export type Context = string | string[] | ContextObject | ContextObject[]
84
75
 
85
76
  /* General Types */
86
- export type Maybe<T> = T | unknown;
87
- export type TwoDigits = `${number}${number}`;
88
- export type ThreeDigits = `${number}${number}${number}`;
89
- export type Year = `${1 | 2}${ThreeDigits}`;
90
- export type Month = TwoDigits;
91
- export type Day = TwoDigits;
92
- export type Hours = TwoDigits;
93
- export type Minutes = TwoDigits;
94
- export type Seconds = TwoDigits;
95
- export type UtcTimestamp = `${Year}-${Month}-${Day}T${Hours}:${Minutes}:${Seconds}`;
96
- export type TzOffset = `${Hours}:${Minutes}`;
97
- export type DateTimestamp = `${UtcTimestamp}Z` | `${UtcTimestamp}-${TzOffset}`;
98
- export type CanonicalizableObject = Record<string, any>;
99
- export type CanonicalizationAlgorithm = 'jcs' | 'rdfc';
100
- export type CanonicalizationEncoding = 'hex' | 'base58';
77
+ export type Maybe<T> = T | undefined;
101
78
  export type UnixTimestamp = number;
package/src/utils/date.ts CHANGED
@@ -31,10 +31,9 @@ export class DateUtils {
31
31
  }
32
32
 
33
33
  /**
34
- * Validate if a string is a valid UTC date string.
35
- * @param {string} dateString - The date string to validate.
36
- * @returns {boolean} True if valid, otherwise false.
37
- * @throws {Error} If the date string is invalid.
34
+ * Parse a date string into a Date object.
35
+ * @param {string} dateString - The date string to parse.
36
+ * @returns {Date} The parsed Date, or Date(0) (Unix epoch) if the string is invalid.
38
37
  */
39
38
  static dateStringToTimestamp(dateString: string): Date {
40
39
  const date = new Date(dateString);
package/src/utils/json.ts CHANGED
@@ -207,7 +207,7 @@ export class JSONUtils {
207
207
  }
208
208
 
209
209
  if (candidate && typeof candidate === 'object') {
210
- const result: any = Array.isArray(candidate) ? [] : {};
210
+ const result: any = {};
211
211
  for (const key of Object.keys(candidate)) {
212
212
  if (keySet.has(key)) continue;
213
213
  result[key] = walk(candidate[key]);
@@ -229,7 +229,7 @@ export class JSONUtils {
229
229
  static sanitize<T>(value: T): T {
230
230
  const walk = (candidate: any): any => {
231
231
  if (Array.isArray(candidate)) {
232
- return candidate.map(item => walk(item));
232
+ return candidate.filter(item => item !== undefined).map(item => walk(item));
233
233
  }
234
234
 
235
235
  if (candidate && typeof candidate === 'object') {
@@ -249,14 +249,7 @@ export class JSONUtils {
249
249
  return walk(value);
250
250
  }
251
251
 
252
- /**
253
- * Internal function to clone JSON values with options.
254
- * @param {T} value - The value to clone.
255
- * @param {CloneOptions} options - The cloning options.
256
- * @param {WeakMap<object, any>} seen - A WeakMap to track seen objects for circular reference detection.
257
- * @returns {any} The cloned value.
258
- */
259
- static cloneInternal<T>(
252
+ private static cloneInternal<T>(
260
253
  value: T,
261
254
  options: CloneOptions = {},
262
255
  seen: WeakMap<object, any> = new WeakMap<object, any>(),