@did-btcr2/common 2.2.2 → 3.1.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 (94) hide show
  1. package/dist/cjs/canonicalization.js +68 -56
  2. package/dist/cjs/canonicalization.js.map +1 -1
  3. package/dist/cjs/constants.js +17 -12
  4. package/dist/cjs/constants.js.map +1 -1
  5. package/dist/cjs/errors.js +20 -3
  6. package/dist/cjs/errors.js.map +1 -1
  7. package/dist/cjs/index.js +5 -3
  8. package/dist/cjs/index.js.map +1 -1
  9. package/dist/cjs/json-patch.js +98 -0
  10. package/dist/cjs/json-patch.js.map +1 -0
  11. package/dist/cjs/logger.js +46 -12
  12. package/dist/cjs/logger.js.map +1 -1
  13. package/dist/cjs/types.js.map +1 -1
  14. package/dist/cjs/utils/date.js +123 -0
  15. package/dist/cjs/utils/date.js.map +1 -0
  16. package/dist/cjs/utils/json.js +280 -0
  17. package/dist/cjs/utils/json.js.map +1 -0
  18. package/dist/cjs/utils/set.js +23 -0
  19. package/dist/cjs/utils/set.js.map +1 -0
  20. package/dist/cjs/utils/string.js +55 -0
  21. package/dist/cjs/utils/string.js.map +1 -0
  22. package/dist/esm/canonicalization.js +68 -56
  23. package/dist/esm/canonicalization.js.map +1 -1
  24. package/dist/esm/constants.js +17 -12
  25. package/dist/esm/constants.js.map +1 -1
  26. package/dist/esm/errors.js +20 -3
  27. package/dist/esm/errors.js.map +1 -1
  28. package/dist/esm/index.js +5 -3
  29. package/dist/esm/index.js.map +1 -1
  30. package/dist/esm/json-patch.js +98 -0
  31. package/dist/esm/json-patch.js.map +1 -0
  32. package/dist/esm/logger.js +46 -12
  33. package/dist/esm/logger.js.map +1 -1
  34. package/dist/esm/types.js.map +1 -1
  35. package/dist/esm/utils/date.js +123 -0
  36. package/dist/esm/utils/date.js.map +1 -0
  37. package/dist/esm/utils/json.js +280 -0
  38. package/dist/esm/utils/json.js.map +1 -0
  39. package/dist/esm/utils/set.js +23 -0
  40. package/dist/esm/utils/set.js.map +1 -0
  41. package/dist/esm/utils/string.js +55 -0
  42. package/dist/esm/utils/string.js.map +1 -0
  43. package/dist/types/canonicalization.d.ts +40 -31
  44. package/dist/types/canonicalization.d.ts.map +1 -1
  45. package/dist/types/constants.d.ts +6 -9
  46. package/dist/types/constants.d.ts.map +1 -1
  47. package/dist/types/errors.d.ts +14 -3
  48. package/dist/types/errors.d.ts.map +1 -1
  49. package/dist/types/index.d.ts +5 -3
  50. package/dist/types/index.d.ts.map +1 -1
  51. package/dist/types/interfaces.d.ts +2 -273
  52. package/dist/types/interfaces.d.ts.map +1 -1
  53. package/dist/types/json-patch.d.ts +47 -0
  54. package/dist/types/json-patch.d.ts.map +1 -0
  55. package/dist/types/logger.d.ts +31 -8
  56. package/dist/types/logger.d.ts.map +1 -1
  57. package/dist/types/types.d.ts +12 -4
  58. package/dist/types/types.d.ts.map +1 -1
  59. package/dist/types/utils/date.d.ts +39 -0
  60. package/dist/types/utils/date.d.ts.map +1 -0
  61. package/dist/types/utils/json.d.ts +89 -0
  62. package/dist/types/utils/json.d.ts.map +1 -0
  63. package/dist/types/utils/set.d.ts +14 -0
  64. package/dist/types/utils/set.d.ts.map +1 -0
  65. package/dist/types/utils/string.d.ts +39 -0
  66. package/dist/types/utils/string.d.ts.map +1 -0
  67. package/package.json +3 -4
  68. package/src/canonicalization.ts +81 -64
  69. package/src/constants.ts +19 -13
  70. package/src/errors.ts +25 -3
  71. package/src/index.ts +5 -5
  72. package/src/interfaces.ts +2 -302
  73. package/src/json-patch.ts +103 -0
  74. package/src/logger.ts +59 -27
  75. package/src/types.ts +12 -6
  76. package/src/utils/date.ts +130 -0
  77. package/src/utils/json.ts +315 -0
  78. package/src/utils/set.ts +23 -0
  79. package/src/utils/string.ts +59 -0
  80. package/dist/cjs/exts.js +0 -189
  81. package/dist/cjs/exts.js.map +0 -1
  82. package/dist/cjs/patch.js +0 -163
  83. package/dist/cjs/patch.js.map +0 -1
  84. package/dist/esm/exts.js +0 -189
  85. package/dist/esm/exts.js.map +0 -1
  86. package/dist/esm/patch.js +0 -163
  87. package/dist/esm/patch.js.map +0 -1
  88. package/dist/types/exts.d.ts +0 -90
  89. package/dist/types/exts.d.ts.map +0 -1
  90. package/dist/types/patch.d.ts +0 -63
  91. package/dist/types/patch.d.ts.map +0 -1
  92. package/src/exts.ts +0 -310
  93. package/src/patch.ts +0 -181
  94. package/src/rdf-canonize.d.ts +0 -6
@@ -12,24 +12,46 @@ export declare const NODE_ENV: Env;
12
12
  * - File/line tracing
13
13
  * - Timestamps
14
14
  * - Colorized output
15
+ * @class Logger
16
+ * @type {Logger}
15
17
  */
16
18
  export declare class Logger {
17
19
  private levels;
18
20
  private namespace?;
19
- constructor(namespace?: string);
21
+ private useColors;
22
+ private static shared;
23
+ /**
24
+ * Creates a new Logger instance.
25
+ * @param {string} namespace - Optional namespace for log messages.
26
+ * @param {Object} options - Configuration options.
27
+ * @param {Level[]} options.levels - Log levels to enable.
28
+ * @param {boolean} options.useColors - Whether to use colored output.
29
+ */
30
+ constructor(namespace?: string, options?: {
31
+ levels?: Level[];
32
+ useColors?: boolean;
33
+ });
20
34
  /**
21
35
  * Logs a message with the specified level.
36
+ * @param {Level} level - The log level.
37
+ * @param {unknown} message - The message to log.
38
+ * @param {...unknown[]} args - Additional arguments to log.
39
+ * @returns {void}
22
40
  */
23
41
  private _log;
24
- debug(message?: unknown, ...args: unknown[]): this;
25
- error(message?: unknown, ...args: unknown[]): this;
26
- info(message?: unknown, ...args: unknown[]): this;
27
- warn(message?: unknown, ...args: unknown[]): this;
28
- security(message?: unknown, ...args: unknown[]): this;
29
- log(message?: unknown, ...args: unknown[]): this;
30
- newline(): this;
42
+ debug(message?: unknown, ...args: unknown[]): Logger;
43
+ error(message?: unknown, ...args: unknown[]): Logger;
44
+ info(message?: unknown, ...args: unknown[]): Logger;
45
+ warn(message?: unknown, ...args: unknown[]): Logger;
46
+ security(message?: unknown, ...args: unknown[]): Logger;
47
+ log(message?: unknown, ...args: unknown[]): Logger;
48
+ newline(): Logger;
31
49
  /**
32
50
  * Static methods for convenience (auto-instantiate).
51
+ * These use a shared singleton instance.
52
+ * @param {unknown} message - The message to log.
53
+ * @param {...unknown[]} args - Additional arguments to log.
54
+ * @returns {void}
33
55
  */
34
56
  static debug(message?: unknown, ...args: unknown[]): void;
35
57
  static error(message?: unknown, ...args: unknown[]): void;
@@ -38,4 +60,5 @@ export declare class Logger {
38
60
  static security(message?: unknown, ...args: unknown[]): void;
39
61
  static log(message?: unknown, ...args: unknown[]): void;
40
62
  static newline(): void;
63
+ private static instance;
41
64
  }
@@ -1 +1 @@
1
- {"version":3,"file":"logger.d.ts","sourceRoot":"","sources":["../../src/logger.ts"],"names":[],"mappings":"AAEA,oBAAY,GAAG;IACb,WAAW,gBAAgB;IAC3B,UAAU,eAAe;IACzB,IAAI,SAAS;CACd;AAED,MAAM,MAAM,KAAK,GAAG,OAAO,GAAG,OAAO,GAAG,MAAM,GAAG,KAAK,GAAG,MAAM,GAAG,UAAU,CAAC;AAE7E,eAAO,MAAM,QAAQ,KAAmD,CAAC;AAoCzE;;;;;;;GAOG;AACH,qBAAa,MAAM;IACjB,OAAO,CAAC,MAAM,CAAU;IACxB,OAAO,CAAC,SAAS,CAAC,CAAS;gBAEf,SAAS,CAAC,EAAE,MAAM;IAK9B;;OAEG;IACH,OAAO,CAAC,IAAI;IAgBL,KAAK,CAAC,OAAO,CAAC,EAAE,OAAO,EAAE,GAAG,IAAI,EAAE,OAAO,EAAE;IAI3C,KAAK,CAAC,OAAO,CAAC,EAAE,OAAO,EAAE,GAAG,IAAI,EAAE,OAAO,EAAE;IAI3C,IAAI,CAAC,OAAO,CAAC,EAAE,OAAO,EAAE,GAAG,IAAI,EAAE,OAAO,EAAE;IAI1C,IAAI,CAAC,OAAO,CAAC,EAAE,OAAO,EAAE,GAAG,IAAI,EAAE,OAAO,EAAE;IAI1C,QAAQ,CAAC,OAAO,CAAC,EAAE,OAAO,EAAE,GAAG,IAA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,IAAI,EAAE,OAAO,EAAE,GAAG,IAAI;IAIvD,MAAM,CAAC,OAAO;IAId,OAAO,CAAC,MAAM,CAAC,QAAQ;CASxB"}
@@ -1,6 +1,7 @@
1
1
  import { HDKey } from '@scure/bip32';
2
2
  export type Bytes = Uint8Array;
3
3
  export type Hex = Bytes | string;
4
+ export type HexString = string;
4
5
  export type SignatureHex = Hex;
5
6
  export type HashHex = Hex;
6
7
  export type DocumentBytes = Bytes;
@@ -67,12 +68,18 @@ export type BeaconUri = string;
67
68
  export type DidPlaceholder = 'did:btcr2:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
68
69
  export type CanonicalizedProofConfig = string;
69
70
  export type CryptosuiteName = 'bip340-jcs-2025' | 'bip340-rdfc-2025';
70
- export type ContextObject = Record<string | number | symbol, any>;
71
- export type Context = string | string[] | ContextObject | ContextObject[];
72
- export type Maybe<T> = T | any;
73
- export type JSONObject = Record<string | number | symbol, any>;
71
+ export type JsonPrimitive = string | number | boolean | null;
72
+ export type JsonArray = JsonValue[];
73
+ export type JsonValue = JsonPrimitive | JsonArray | JsonObject;
74
+ export type JsonObject = {
75
+ [key: string]: JsonValue;
76
+ };
77
+ export type JSONObject = JsonObject;
74
78
  export type Prototyped = JSONObject;
75
79
  export type Unprototyped = JSONObject;
80
+ export type ContextObject = Record<string, JsonValue>;
81
+ export type Context = string | string[] | ContextObject | ContextObject[];
82
+ export type Maybe<T> = T | unknown;
76
83
  export type TwoDigits = `${number}${number}`;
77
84
  export type ThreeDigits = `${number}${number}${number}`;
78
85
  export type Year = `${1 | 2}${ThreeDigits}`;
@@ -86,4 +93,5 @@ export type TzOffset = `${Hours}:${Minutes}`;
86
93
  export type DateTimestamp = `${UtcTimestamp}Z` | `${UtcTimestamp}-${TzOffset}`;
87
94
  export type CanonicalizableObject = Record<string, any>;
88
95
  export type CanonicalizationAlgorithm = 'jcs' | 'rdfc';
96
+ export type CanonicalizationEncoding = 'hex' | 'base58';
89
97
  export type UnixTimestamp = number;
@@ -1 +1 @@
1
- {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,cAAc,CAAC;AAGrC,MAAM,MAAM,KAAK,GAAG,UAAU,CAAC;AAC/B,MAAM,MAAM,GAAG,GAAG,KAAK,GAAG,MAAM,CAAC;AACjC,MAAM,MAAM,YAAY,GAAG,GAAG,CAAC;AAC/B,MAAM,MAAM,OAAO,GAAG,GAAG,CAAC;AAE1B,MAAM,MAAM,aAAa,GAAG,KAAK,CAAC;AAClC,MAAM,MAAM,cAAc,GAAG,KAAK,CAAC;AACnC,MAAM,MAAM,UAAU,GAAG,KAAK,CAAC;AAC/B,MAAM,MAAM,SAAS,GAAG,KAAK,CAAC;AAC9B,MAAM,MAAM,YAAY,GAAG,KAAK,CAAC;AACjC,MAAM,MAAM,OAAO,GAAG,KAAK,GAAG,MAAM,CAAC;AAErC,MAAM,MAAM,6BAA6B,GAAG,IAAI,GAAG,IAAI,CAAC;AACxD,MAAM,MAAM,cAAc,GAAG,MAAM,CAAC;AACpC,MAAM,MAAM,eAAe,GAAG,GAAG,CAAC;AAElC,MAAM,MAAM,QAAQ,GAAG,KAAK,CAAC;AAC7B,MAAM,MAAM,KAAK,GAAG;IAClB,CAAC,EAAE,KAAK,CAAC,MAAM,CAAC,CAAC;IACjB,CAAC,EAAE,KAAK,CAAC,MAAM,CAAC,CAAC;IACjB,MAAM,EAAE,MAAM,CAAC;CAChB,CAAA;AACD,MAAM,MAAM,eAAe,GAAG;IAC5B,KAAK,EAAE,KAAK,CAAC;IACb,GAAG,EAAE,GAAG,CAAC;IACT,SAAS,EAAE,eAAe,CAAC;CAC5B,CAAC;AACF,MAAM,MAAM,eAAe,GAAG;IAC5B,KAAK,EAAE,KAAK,CAAC,MAAM,CAAC,CAAC;IACrB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,GAAG,CAAC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MAAM,MAAM,aAAa,GAAG,MAAM,CAAC"}
@@ -0,0 +1,39 @@
1
+ /**
2
+ * Utility class for date-related operations.
3
+ * @name DateUtils
4
+ * @class DateUtils
5
+ */
6
+ export declare class DateUtils {
7
+ /**
8
+ * Render an ISO 8601 UTC timestamp without fractional seconds.
9
+ * @param {Date} [date=new Date()] - The date to format.
10
+ * @returns {string} The formatted date string.
11
+ */
12
+ static toISOStringNonFractional(date?: Date): string;
13
+ /**
14
+ * Unix timestamp in seconds (integer).
15
+ * @param {Date} [date=new Date()] - The date to convert.
16
+ * @returns {number} The Unix timestamp in seconds.
17
+ */
18
+ static toUnixSeconds(date?: Date): number;
19
+ /**
20
+ * Validate if a string is a valid UTC date string.
21
+ * @param {string} dateString - The date string to validate.
22
+ * @returns {boolean} True if valid, otherwise false.
23
+ * @throws {Error} If the date string is invalid.
24
+ */
25
+ static dateStringToTimestamp(dateString: string): Date;
26
+ /**
27
+ * Convert a blocktime (Unix timestamp in seconds) to a Date object.
28
+ * @param {number} blocktime - The blocktime in seconds.
29
+ * @returns {Date} The corresponding Date object.
30
+ */
31
+ static blocktimeToTimestamp(blocktime: number): Date;
32
+ /**
33
+ * Validates an XMLSCHEMA11-2 dateTime string.
34
+ * Format: [-]YYYY-MM-DDThh:mm:ss[.fractional][Z|(+|-)hh:mm]
35
+ *
36
+ * @see https://www.w3.org/TR/xmlschema11-2/#dateTime
37
+ */
38
+ static isValidXsdDateTime(value?: string): boolean;
39
+ }
@@ -0,0 +1 @@
1
+ {"version":3,"file":"date.d.ts","sourceRoot":"","sources":["../../../src/utils/date.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,qBAAa,SAAS;IACpB;;;;OAIG;IACH,MAAM,CAAC,wBAAwB,CAAC,IAAI,GAAE,IAAiB,GAAG,MAAM;IAQhE;;;;OAIG;IACH,MAAM,CAAC,aAAa,CAAC,IAAI,GAAE,IAAiB,GAAG,MAAM;IAQrD;;;;;OAKG;IACH,MAAM,CAAC,qBAAqB,CAAC,UAAU,EAAE,MAAM,GAAG,IAAI;IAQtD;;;;OAIG;IACH,MAAM,CAAC,oBAAoB,CAAC,SAAS,EAAE,MAAM,GAAG,IAAI;IAIpD;;;;;OAKG;IACH,MAAM,CAAC,kBAAkB,CAAC,KAAK,CAAC,EAAE,MAAM,GAAG,OAAO;CAoEnD"}
@@ -0,0 +1,89 @@
1
+ import { JSONObject, Prototyped, Unprototyped } from '../types.js';
2
+ /**
3
+ * Options for cloning JSON values.
4
+ */
5
+ type CloneOptions = {
6
+ stripPrototypes?: boolean;
7
+ transform?: (value: any) => any;
8
+ };
9
+ /**
10
+ * Utilities for working with JSON data.
11
+ * @name JSONUtils
12
+ * @class JSONUtils
13
+ */
14
+ export declare class JSONUtils {
15
+ /**
16
+ * Check if a value is a JSON object (not an array, not null, and has Object prototype).
17
+ * @param {unknown} value - The value to check.
18
+ * @returns {boolean} True if the value is a JSON object.
19
+ */
20
+ static isObject(value: unknown): value is JSONObject;
21
+ /**
22
+ * Check if a value is a parsable JSON string.
23
+ * @param {unknown} value - The value to check.
24
+ * @returns {boolean} True if the value is a parsable JSON string.
25
+ */
26
+ static isParsable(value: unknown): value is string;
27
+ /**
28
+ * Check if a value is an unprototyped object (i.e., Object.create(null)).
29
+ * @param {unknown} value - The value to check.
30
+ * @returns {boolean} True if the value is an unprototyped object.
31
+ */
32
+ static isUnprototyped(value: unknown): value is Unprototyped;
33
+ /**
34
+ * Normalize a JSON value by stripping prototypes from all objects within it.
35
+ * @param {T} value - The JSON value to normalize.
36
+ * @returns {Prototyped} The normalized JSON value.
37
+ */
38
+ static normalize<T extends JSONObject | Array<any>>(value: T): Prototyped;
39
+ /**
40
+ * Shallow copy of a JSON object.
41
+ * @param {T extends JSONObject} value - The JSON object to copy.
42
+ * @returns {T} The copied JSON object.
43
+ */
44
+ static copy<T extends JSONObject>(value: T): T;
45
+ /**
46
+ * Deep clone a JSON value.
47
+ * @param {T} value - The JSON value to clone.
48
+ * @returns {T} The cloned JSON value.
49
+ */
50
+ static clone<T>(value: T): T;
51
+ /**
52
+ * Deep clone a JSON value, replacing strings that match a pattern.
53
+ * @param {T} value - The JSON value to clone.
54
+ * @param {RegExp} pattern - The regex pattern to match strings.
55
+ * @param {string} replacement - The replacement string.
56
+ * @returns {T} The cloned JSON value with replacements.
57
+ */
58
+ static cloneReplace<T>(value: T, pattern: RegExp, replacement: string): T;
59
+ /**
60
+ * Deep equality check between two values.
61
+ * @param {unknown} a - The first value to compare.
62
+ * @param {unknown} b - The second value to compare.
63
+ * @param {WeakMap<object, object>} seen - A WeakMap to track seen object pairs for circular reference detection.
64
+ * @returns {boolean} True if the values are deeply equal.
65
+ */
66
+ static deepEqual(a: unknown, b: unknown, seen?: WeakMap<object, object>, depth?: number): boolean;
67
+ /**
68
+ * Delete specified keys from a JSON value.
69
+ * @param {T} value - The JSON value to process.
70
+ * @param {Array<string | number | symbol>} keys - The keys to delete.
71
+ * @returns {T} The JSON value with specified keys deleted.
72
+ */
73
+ static deleteKeys<T>(value: T, keys: Array<string | number | symbol>): T;
74
+ /**
75
+ * Sanitize a JSON value by removing undefined values from objects and arrays.
76
+ * @param {T} value - The JSON value to sanitize.
77
+ * @returns {T} The sanitized JSON value.
78
+ */
79
+ static sanitize<T>(value: T): T;
80
+ /**
81
+ * Internal function to clone JSON values with options.
82
+ * @param {T} value - The value to clone.
83
+ * @param {CloneOptions} options - The cloning options.
84
+ * @param {WeakMap<object, any>} seen - A WeakMap to track seen objects for circular reference detection.
85
+ * @returns {any} The cloned value.
86
+ */
87
+ static cloneInternal<T>(value: T, options?: CloneOptions, seen?: WeakMap<object, any>, depth?: number): any;
88
+ }
89
+ export {};
@@ -0,0 +1 @@
1
+ {"version":3,"file":"json.d.ts","sourceRoot":"","sources":["../../../src/utils/json.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,UAAU,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAEnE;;GAEG;AACH,KAAK,YAAY,GAAG;IAClB,eA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AQ,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,GAAG,CAAC;IAuB/B;;;;;;OAMG;IACH,MAAM,CAAC,aAAa,CAAC,CAAC,EACpB,KAAK,EAAE,CAAC,EACR,OAAO,GAAE,YAAiB,EAC1B,IAAI,GAAE,OAAO,CAAC,MAAM,EAAE,GAAG,CAA8B,EACvD,KAAK,GAAE,MAAU,GAChB,GAAG;CAmDP"}
@@ -0,0 +1,14 @@
1
+ /**
2
+ * Utility class for set operations.
3
+ * @name SetUtils
4
+ * @class SetUtils
5
+ */
6
+ export declare class SetUtils {
7
+ /**
8
+ * Compute the set difference without mutating the inputs.
9
+ * @param {Set<T>} left - The left set.
10
+ * @param {Set<T>} right - The right set.
11
+ * @returns {Set<T>} A new set containing elements in `left` that are not in `right`.
12
+ */
13
+ static difference<T>(left: Set<T>, right: Set<T>): Set<T>;
14
+ }
@@ -0,0 +1 @@
1
+ {"version":3,"file":"set.d.ts","sourceRoot":"","sources":["../../../src/utils/set.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,qBAAa,QAAQ;IACnB;;;;;OAKG;IACH,MAAM,CAAC,UAAU,CAAC,CAAC,EAAE,IAAI,EAAE,GAAG,CAAC,CAAC,CAAC,EAAE,KAAK,EAAE,GAAG,CAAC,CAAC,CAAC,GAAG,GAAG,CAAC,CAAC,CAAC;CAU1D"}
@@ -0,0 +1,39 @@
1
+ /**
2
+ * Utility class string-related operations.
3
+ * @name StringUtils
4
+ * @class StringUtils
5
+ */
6
+ export declare class StringUtils {
7
+ /**
8
+ * Escape special characters in a string for use in a regular expression.
9
+ * @param {string} value - The string to escape.
10
+ * @returns {string} The escaped string.
11
+ */
12
+ static escapeRegExp(value: string): string;
13
+ /**
14
+ * Convert a camelCase string to snake_case.
15
+ * @param {string} value - The camelCase string to convert.
16
+ * @returns {string} The converted snake_case string.
17
+ */
18
+ static toSnake(value: string): string;
19
+ /**
20
+ * Convert a string to SNAKE_SCREAMING_CASE.
21
+ * @param {string} value - The string to convert.
22
+ * @returns {string} The converted SNAKE_SCREAMING_CASE string.
23
+ */
24
+ static toSnakeScream(value: string): string;
25
+ /**
26
+ * Remove the last character from a string.
27
+ * @param {string} value - The string to chop.
28
+ * @returns {string} The chopped string.
29
+ */
30
+ static chop(value: string): string;
31
+ /**
32
+ * Replace the end of a string if it matches a given pattern.
33
+ * @param {string} value - The string to modify.
34
+ * @param {string | RegExp} pattern - The pattern to match at the end of the string.
35
+ * @param {string} [replacement=''] - The replacement string.
36
+ * @returns {string} The modified string.
37
+ */
38
+ static replaceEnd(value: string, pattern: string | RegExp, replacement?: string): string;
39
+ }
@@ -0,0 +1 @@
1
+ {"version":3,"file":"string.d.ts","sourceRoot":"","sources":["../../../src/utils/string.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,qBAAa,WAAW;IACtB;;;;OAIG;IACH,MAAM,CAAC,YAAY,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM;IAI1C;;;;OAIG;IACH,MAAM,CAAC,OAAO,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM;IAMrC;;;;OAIG;IACH,MAAM,CAAC,aAAa,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM;IAI3C;;;;OAIG;IACH,MAAM,CAAC,IAAI,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM;IAIlC;;;;;;OAMG;IACH,MAAM,CAAC,UAAU,CAAC,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG,MAAM,EAAE,WAAW,GAAE,MAAW,GAAG,MAAM;CAO7F"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@did-btcr2/common",
3
- "version": "2.2.2",
3
+ "version": "3.1.0",
4
4
  "type": "module",
5
5
  "description": "Common utilities, types, interfaces, etc. shared across the did-btcr2-js monorepo packages.",
6
6
  "main": "./dist/cjs/index.js",
@@ -46,11 +46,10 @@
46
46
  "dependencies": {
47
47
  "@noble/hashes": "^1.7.1",
48
48
  "@scure/bip32": "^1.5.0",
49
- "canonicalize": "^2.1.0",
50
49
  "chalk": "^5.4.1",
50
+ "fast-json-patch": "^3.1.1",
51
51
  "json-canonicalize": "^1.0.6",
52
- "multiformats": "^13.3.2",
53
- "rdf-canonize": "^4.0.1"
52
+ "multiformats": "^13.3.2"
54
53
  },
55
54
  "devDependencies": {
56
55
  "@eslint/js": "^9.21.0",
@@ -2,9 +2,8 @@ import { sha256 } from '@noble/hashes/sha2';
2
2
  import { bytesToHex } from '@noble/hashes/utils';
3
3
  import { canonicalize as jcsa } from 'json-canonicalize';
4
4
  import { base58btc } from 'multiformats/bases/base58';
5
- import rdf from 'rdf-canonize';
6
- import { CanonicalizationAlgorithm, HashBytes, JSONObject } from './types.js';
7
5
  import { CanonicalizationError } from './errors.js';
6
+ import { CanonicalizationAlgorithm, CanonicalizationEncoding, HashBytes } from './types.js';
8
7
 
9
8
  /**
10
9
  * Canonicalization class provides methods for canonicalizing JSON objects
@@ -14,39 +13,50 @@ import { CanonicalizationError } from './errors.js';
14
13
  * @type {Canonicalization}
15
14
  */
16
15
  export class Canonicalization {
17
- private _algorithm: CanonicalizationAlgorithm;
16
+ private readonly _defaultAlgorithm: CanonicalizationAlgorithm;
18
17
 
19
18
  /**
20
19
  * Initializes the Canonicalization class with the specified algorithm.
21
- * @param {CanonicalizationAlgorithm} algorithm The canonicalization algorithm to use ('jcs' or 'rdfc').
20
+ * @param {CanonicalizationAlgorithm} algorithm The canonicalization algorithm to use ('jcs').
22
21
  */
23
- // TODO: Need to move to using RDFC by default
24
22
  constructor(algorithm: CanonicalizationAlgorithm = 'jcs') {
25
- this._algorithm = algorithm;
23
+ this._defaultAlgorithm = Canonicalization.normalizeAlgorithm(algorithm);
26
24
  }
27
25
 
28
26
  /**
29
- * Sets the canonicalization algorithm.
30
- * @param {'jcs' | 'rdfc'} algorithm Either 'jcs' or 'rdfc'.
27
+ * Gets the canonicalization algorithm.
28
+ * @returns {CanonicalizationAlgorithm} The current canonicalization algorithm.
31
29
  */
32
- set algorithm(algorithm: 'jcs' | 'rdfc') {
33
- // Normalize the passed algorithm to lowercase
34
- algorithm = algorithm.toLowerCase() as CanonicalizationAlgorithm;
30
+ get algorithm(): CanonicalizationAlgorithm {
31
+ return this._defaultAlgorithm;
32
+ }
35
33
 
36
- // Validate the algorithm is either 'jcs' or 'rdfc'
37
- if(!['jcs', 'rdfc'].includes(algorithm)){
34
+ /**
35
+ * Normalizes the canonicalization algorithm.
36
+ * @param {CanonicalizationAlgorithm} algorithm
37
+ * @returns {CanonicalizationAlgorithm} The normalized algorithm.
38
+ * @throws {CanonicalizationError} If the algorithm is not supported.
39
+ */
40
+ static normalizeAlgorithm(algorithm: CanonicalizationAlgorithm): CanonicalizationAlgorithm {
41
+ const normalized = algorithm.toLowerCase() as CanonicalizationAlgorithm;
42
+ if (normalized !== 'jcs') {
38
43
  throw new CanonicalizationError(`Unsupported algorithm: ${algorithm}`, 'ALGORITHM_ERROR');
39
44
  }
40
- // Set the algorithm
41
- this._algorithm = algorithm;
45
+ return normalized;
42
46
  }
43
47
 
44
48
  /**
45
- * Gets the canonicalization algorithm.
46
- * @returns {CanonicalizationAlgorithm} The current canonicalization algorithm.
49
+ * Normalizes the canonicalization encoding.
50
+ * @param {CanonicalizationEncoding} encoding - The encoding to normalize.
51
+ * @returns {CanonicalizationEncoding} The normalized encoding.
52
+ * @throws {CanonicalizationError} If the encoding is not supported.
47
53
  */
48
- get algorithm(): CanonicalizationAlgorithm {
49
- return this._algorithm;
54
+ static normalizeEncoding(encoding: CanonicalizationEncoding): CanonicalizationEncoding {
55
+ const normalized = encoding.toLowerCase() as CanonicalizationEncoding;
56
+ if (normalized !== 'hex' && normalized !== 'base58') {
57
+ throw new CanonicalizationError(`Unsupported encoding: ${encoding}`, 'ENCODING_ERROR');
58
+ }
59
+ return normalized;
50
60
  }
51
61
 
52
62
  /**
@@ -56,75 +66,80 @@ export class Canonicalization {
56
66
  * Scheme. The function returns the canonicalizedBytes.
57
67
  *
58
68
  * Optionally encodes a sha256 hashed canonicalized JSON object.
59
- * Step 1 Canonicalize (JCS/RDFC) → Step 2 Hash (SHA256) → Step 3 Encode (Hex/Base58).
69
+ * Step 1 Canonicalize (JCS) → Step 2 Hash (SHA256) → Step 3 Encode (Hex/Base58).
60
70
  *
61
- * @param {JSONObject} object The object to process.
62
- * @param {string} encoding The encoding format ('hex' or 'base58').
63
- * @returns {Promise<string>} The final SHA-256 hash bytes as a hex string.
71
+ * @param {Record<any, any>} object The object to process.
72
+ * @param {Object} [options] Options for processing.
73
+ * @param {CanonicalizationEncoding} [options.encoding='hex'] The encoding format ('hex' or 'base58').
74
+ * @param {CanonicalizationAlgorithm} [options.algorithm] The canonicalization algorithm to use.
75
+ * @returns {string} The final SHA-256 hash bytes as a hex string.
64
76
  */
65
- public async process(object: JSONObject, encoding: string = 'hex'): Promise<string> {
77
+ process(object: Record<any, any>, options: {
78
+ encoding?: CanonicalizationEncoding;
79
+ algorithm?: CanonicalizationAlgorithm;
80
+ multibase?: boolean;
81
+ } = {}): string {
82
+ const algorithm = Canonicalization.normalizeAlgorithm(options.algorithm ?? this._defaultAlgorithm);
83
+ const encoding = Canonicalization.normalizeEncoding(options.encoding ?? 'hex');
84
+
66
85
  // Step 1: Canonicalize
67
- const canonicalized = await this.canonicalize(object);
86
+ const canonicalized = this.canonicalize(object, algorithm);
68
87
  // Step 2: Hash
69
88
  const hashed = this.hash(canonicalized);
70
89
  // Step 3: Encode
71
- const encoded = this.encode(hashed, encoding);
90
+ const encoded = this.encode(hashed, encoding, options.multibase ?? false);
72
91
  // Return the encoded string
73
92
  return encoded;
74
93
  }
75
94
 
76
95
  /**
77
- * Step 1: Uses this.algorithm to determine the method (JCS/RDFC).
78
- * @param {JSONObject} object The object to canonicalize.
79
- * @returns {Promise<string>} The canonicalized object.
96
+ * Step 1: Uses this.algorithm to determine the method (JCS).
97
+ * @param {Record<any, any>} object The object to canonicalize.
98
+ * @param {CanonicalizationAlgorithm} [algorithm] The algorithm to use.
99
+ * @returns {string} The canonicalized object.
80
100
  */
81
- public async canonicalize(object: JSONObject): Promise<string> {
82
- return await (this[this.algorithm] as (object: JSONObject) => any)(object);
101
+ canonicalize(object: Record<any, any>, algorithm: CanonicalizationAlgorithm = this._defaultAlgorithm): string {
102
+ switch (Canonicalization.normalizeAlgorithm(algorithm)) {
103
+ case 'jcs':
104
+ return this.jcs(object);
105
+ default:
106
+ throw new CanonicalizationError(`Unsupported algorithm: ${algorithm}`, 'ALGORITHM_ERROR');
107
+ }
83
108
  }
84
109
 
85
110
  /**
86
111
  * Step 1: Canonicalizes an object using JCS (JSON Canonicalization Scheme).
87
- * @param {JSONObject} object The object to canonicalize.
112
+ * @param {Record<any, any>} object The object to canonicalize.
88
113
  * @returns {string} The canonicalized object.
89
114
  */
90
- public jcs(object: JSONObject): any {
115
+ jcs(object: Record<any, any>): string {
91
116
  return jcsa(object);
92
117
  }
93
118
 
94
- /**
95
- * Step 1: Canonicalizes an object using RDF Canonicalization (RDFC).
96
- * @param {JSONObject} object The object to canonicalize.
97
- * @returns {Promise<string>} The canonicalized object.
98
- */
99
- public rdfc(object: JSONObject): Promise<string> {
100
- return rdf.canonize([object], { algorithm: 'RDFC-1.0' });
101
- }
102
-
103
119
  /**
104
120
  * Step 2: SHA-256 hashes a canonicalized object.
105
121
  * @param {string} canonicalized The canonicalized object.
106
122
  * @returns {HashBytes} The SHA-256 HashBytes (Uint8Array).
107
123
  */
108
- public hash(canonicalized: string): HashBytes {
124
+ hash(canonicalized: string): HashBytes {
109
125
  return sha256(canonicalized);
110
126
  }
111
127
 
112
128
  /**
113
129
  * Step 3: Encodes SHA-256 hashed, canonicalized object as a hex or base58 string.
114
130
  * @param {string} canonicalizedhash The canonicalized object to encode.
115
- * @param {string} encoding The encoding format ('hex' or 'base58').
131
+ * @param {CanonicalizationEncoding} encoding The encoding format ('hex' or 'base58').
116
132
  * @throws {CanonicalizationError} If the encoding format is not supported.
117
133
  * @returns {string} The encoded string.
118
134
  */
119
- public encode(canonicalizedhash: HashBytes, encoding: string = 'hex'): string {
120
- switch(encoding) {
121
- case 'hex':
122
- return this.hex(canonicalizedhash);
123
- case 'base58':
124
- return this.base58(canonicalizedhash);
125
- default:
126
- throw new CanonicalizationError(`Unsupported encoding: ${encoding}`, 'ENCODING_ERROR');
135
+ encode(canonicalizedhash: HashBytes, encoding: CanonicalizationEncoding = 'hex', multibase: boolean = false): string {
136
+ const normalized = Canonicalization.normalizeEncoding(encoding);
137
+ if (normalized === 'hex') return this.hex(canonicalizedhash);
138
+ if (normalized === 'base58') {
139
+ const encoded = this.base58(canonicalizedhash);
140
+ return multibase ? `z${encoded}` : encoded;
127
141
  }
142
+ throw new CanonicalizationError(`Unsupported encoding: ${encoding}`, 'ENCODING_ERROR');
128
143
  }
129
144
 
130
145
  /**
@@ -132,7 +147,7 @@ export class Canonicalization {
132
147
  * @param {HashBytes} hashBytes The hash as a Uint8Array.
133
148
  * @returns {string} The hash as a hex string.
134
149
  */
135
- public hex(hashBytes: HashBytes): string {
150
+ hex(hashBytes: HashBytes): string {
136
151
  return bytesToHex(hashBytes);
137
152
  }
138
153
 
@@ -141,18 +156,22 @@ export class Canonicalization {
141
156
  * @param {HashBytes} hashBytes The hash as a Uint8Array.
142
157
  * @returns {string} The hash as a hex string.
143
158
  */
144
- public base58(hashBytes: HashBytes): string {
145
- return base58btc.encode(hashBytes);
159
+ base58(hashBytes: HashBytes): string {
160
+ const encoded = base58btc.encode(hashBytes);
161
+ return encoded.startsWith('z') ? encoded.slice(1) : encoded;
146
162
  }
147
163
 
148
164
  /**
149
165
  * Canonicalizes an object, hashes it and returns it as hash bytes.
150
166
  * Step 1-2: Canonicalize → Hash.
151
- * @param {JSONObject} object The object to process.
167
+ * @param {Record<any, any>} object The object to process.
152
168
  * @returns {Promise<HashBytes>} The final SHA-256 hash bytes.
153
169
  */
154
- public async canonicalhash(object: JSONObject): Promise<HashBytes> {
155
- const canonicalized = await this.canonicalize(object);
170
+ canonicalhash(
171
+ object: Record<any, any>,
172
+ algorithm: CanonicalizationAlgorithm = this._defaultAlgorithm
173
+ ): HashBytes {
174
+ const canonicalized = this.canonicalize(object, algorithm);
156
175
  return this.hash(canonicalized);
157
176
  }
158
177
 
@@ -162,8 +181,8 @@ export class Canonicalization {
162
181
  * @param {string} canonicalized The canonicalized object to hash.
163
182
  * @returns {string} The SHA-256 hash as a hex string.
164
183
  */
165
- public hashhex(canonicalized: string): string {
166
- return this.encode(this.hash(canonicalized));
184
+ hashhex(canonicalized: string): string {
185
+ return this.encode(this.hash(canonicalized), 'hex');
167
186
  }
168
187
 
169
188
  /**
@@ -172,9 +191,7 @@ export class Canonicalization {
172
191
  * @param {string} canonicalized The canonicalized object to hash.
173
192
  * @returns {string} The SHA-256 hash as a base58 string.
174
193
  */
175
- public hashb58(canonicalized: string): string {
176
- return this.encode(this.hash(canonicalized), 'base58');
194
+ hashbase58(canonicalized: string): string {
195
+ return this.encode(this.hash(canonicalized), 'base58', false);
177
196
  }
178
197
  }
179
-
180
- export const canonicalization = new Canonicalization();