@did-btcr2/common 2.2.1 → 3.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.
- package/dist/cjs/canonicalization.js +66 -54
- package/dist/cjs/canonicalization.js.map +1 -1
- package/dist/cjs/constants.js +28 -24
- package/dist/cjs/constants.js.map +1 -1
- package/dist/cjs/index.js +5 -3
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/json-patch.js +98 -0
- package/dist/cjs/json-patch.js.map +1 -0
- package/dist/cjs/logger.js +46 -12
- package/dist/cjs/logger.js.map +1 -1
- package/dist/cjs/utils/date.js +32 -0
- package/dist/cjs/utils/date.js.map +1 -0
- package/dist/cjs/utils/json.js +280 -0
- package/dist/cjs/utils/json.js.map +1 -0
- package/dist/cjs/utils/set.js +23 -0
- package/dist/cjs/utils/set.js.map +1 -0
- package/dist/cjs/utils/string.js +55 -0
- package/dist/cjs/utils/string.js.map +1 -0
- package/dist/esm/canonicalization.js +66 -54
- package/dist/esm/canonicalization.js.map +1 -1
- package/dist/esm/constants.js +28 -24
- package/dist/esm/constants.js.map +1 -1
- package/dist/esm/index.js +5 -3
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/json-patch.js +98 -0
- package/dist/esm/json-patch.js.map +1 -0
- package/dist/esm/logger.js +46 -12
- package/dist/esm/logger.js.map +1 -1
- package/dist/esm/utils/date.js +32 -0
- package/dist/esm/utils/date.js.map +1 -0
- package/dist/esm/utils/json.js +280 -0
- package/dist/esm/utils/json.js.map +1 -0
- package/dist/esm/utils/set.js +23 -0
- package/dist/esm/utils/set.js.map +1 -0
- package/dist/esm/utils/string.js +55 -0
- package/dist/esm/utils/string.js.map +1 -0
- package/dist/types/canonicalization.d.ts +38 -29
- package/dist/types/canonicalization.d.ts.map +1 -1
- package/dist/types/constants.d.ts +6 -11
- package/dist/types/constants.d.ts.map +1 -1
- package/dist/types/index.d.ts +5 -3
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/interfaces.d.ts +2 -2
- package/dist/types/interfaces.d.ts.map +1 -1
- package/dist/types/json-patch.d.ts +47 -0
- package/dist/types/json-patch.d.ts.map +1 -0
- package/dist/types/logger.d.ts +31 -8
- package/dist/types/logger.d.ts.map +1 -1
- package/dist/types/types.d.ts +13 -5
- package/dist/types/types.d.ts.map +1 -1
- package/dist/types/utils/date.d.ts +19 -0
- package/dist/types/utils/date.d.ts.map +1 -0
- package/dist/types/utils/json.d.ts +89 -0
- package/dist/types/utils/json.d.ts.map +1 -0
- package/dist/types/utils/set.d.ts +14 -0
- package/dist/types/utils/set.d.ts.map +1 -0
- package/dist/types/utils/string.d.ts +39 -0
- package/dist/types/utils/string.d.ts.map +1 -0
- package/package.json +3 -4
- package/src/canonicalization.ts +75 -58
- package/src/constants.ts +30 -25
- package/src/index.ts +5 -5
- package/src/interfaces.ts +2 -2
- package/src/json-patch.ts +103 -0
- package/src/logger.ts +59 -27
- package/src/types.ts +13 -7
- package/src/utils/date.ts +32 -0
- package/src/utils/json.ts +315 -0
- package/src/utils/set.ts +23 -0
- package/src/utils/string.ts +59 -0
- package/dist/cjs/exts.js +0 -182
- package/dist/cjs/exts.js.map +0 -1
- package/dist/cjs/patch.js +0 -163
- package/dist/cjs/patch.js.map +0 -1
- package/dist/esm/exts.js +0 -182
- package/dist/esm/exts.js.map +0 -1
- package/dist/esm/patch.js +0 -163
- package/dist/esm/patch.js.map +0 -1
- package/dist/types/exts.d.ts +0 -82
- package/dist/types/exts.d.ts.map +0 -1
- package/dist/types/patch.d.ts +0 -63
- package/dist/types/patch.d.ts.map +0 -1
- package/src/exts.ts +0 -293
- package/src/patch.ts +0 -181
- package/src/rdf-canonize.d.ts +0 -6
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { PatchOperation } from './interfaces.js';
|
|
2
|
+
import { JSONObject } from './types.js';
|
|
3
|
+
/**
|
|
4
|
+
* Thin wrapper around fast-json-patch to keep a stable API within this package.
|
|
5
|
+
* @class JSONPatch
|
|
6
|
+
* @type {JSONPatch}
|
|
7
|
+
*/
|
|
8
|
+
export declare class JSONPatch {
|
|
9
|
+
/**
|
|
10
|
+
* Applies a JSON Patch to a source document and returns the patched document.
|
|
11
|
+
* Does not mutate the input document.
|
|
12
|
+
* @param {JSONObject} sourceDocument - The source JSON document to apply the patch to.
|
|
13
|
+
* @param {PatchOperation[]} operations - The JSON Patch operations to apply.
|
|
14
|
+
* @returns {JSONObject} The patched JSON document.
|
|
15
|
+
*/
|
|
16
|
+
static apply(sourceDocument: Record<any, any>, operations: PatchOperation[], options?: {
|
|
17
|
+
mutate?: boolean;
|
|
18
|
+
clone?: (value: any) => any;
|
|
19
|
+
}): Record<any, any>;
|
|
20
|
+
/**
|
|
21
|
+
* Compute a JSON Patch diff from source => target.
|
|
22
|
+
* @param {JSONObject} sourceDocument - The source JSON document.
|
|
23
|
+
* @param {JSONObject} targetDocument - The target JSON document.
|
|
24
|
+
* @param {string} [path] - An optional base path to prefix to each operation.
|
|
25
|
+
* @returns {PatchOperation[]} The computed JSON Patch operations.
|
|
26
|
+
*/
|
|
27
|
+
static diff(sourceDocument: JSONObject, targetDocument: JSONObject, path?: string): PatchOperation[];
|
|
28
|
+
/**
|
|
29
|
+
* Join a base pointer prefix with an operation path ensuring correct escaping.
|
|
30
|
+
* @param {string} prefix - The base pointer prefix.
|
|
31
|
+
* @param {string} opPath - The operation path.
|
|
32
|
+
* @returns {string} The joined pointer.
|
|
33
|
+
*/
|
|
34
|
+
static joinPointer(prefix: string, opPath: string): string;
|
|
35
|
+
/**
|
|
36
|
+
* Escape a JSON Pointer segment according to RFC 6901.
|
|
37
|
+
* @param {string} pointer - The JSON Pointer to escape.
|
|
38
|
+
* @returns {string} The escaped JSON Pointer.
|
|
39
|
+
*/
|
|
40
|
+
static escapeSegmentPath(pointer: string): string;
|
|
41
|
+
/**
|
|
42
|
+
* Validate JSON Patch operations.
|
|
43
|
+
* @param {PatchOperation[]} operations - The operations to validate.
|
|
44
|
+
* @returns {Error | null} An Error if validation fails, otherwise null.
|
|
45
|
+
*/
|
|
46
|
+
static validateOperations(operations: PatchOperation[]): Error | null;
|
|
47
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"json-patch.d.ts","sourceRoot":"","sources":["../../src/json-patch.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC;AACjD,OAAO,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAIxC;;;;GAIG;AACH,qBAAa,SAAS;IACpB;;;;;;OAMG;IACH,MAAM,CAAC,KAAK,CACV,cAAc,EAAE,MAAM,CAAC,GAAG,EAAE,GAAG,CAAC,EAChC,UAAU,EAAE,cAAc,EAAE,EAC5B,OAAO,GAAE;QAAE,MAAM,CAAC,EAAE,OAAO,CAAC;QAAC,KAAK,CAAC,EAAE,CAAC,KAAK,EAAE,GAAG,KAAK,GAAG,CAAA;KAAO,GAC9D,MAAM,CAAC,GAAG,EAAE,GAAG,CAAC;IAmBnB;;;;;;OAMG;IACH,MAAM,CAAC,IAAI,CAAC,cAAc,EAAE,UAAU,EAAE,cAAc,EAAE,UAAU,EAAE,IAAI,GAAE,MAAW,GAAG,cAAc,EAAE;IAWxG;;;;;KAKC;IACD,MAAM,CAAC,WAAW,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,MAAM;IAM1D;;;;KAIC;IACD,MAAM,CAAC,iBAAiB,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM;IAOjD;;;;KAIC;IACD,MAAM,CAAC,kBAAkB,CAAC,UAAU,EAAE,cAAc,EAAE,GAAG,KAAK,GAAG,IAAI;CAYtE"}
|
package/dist/types/logger.d.ts
CHANGED
|
@@ -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
|
-
|
|
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[]):
|
|
25
|
-
error(message?: unknown, ...args: unknown[]):
|
|
26
|
-
info(message?: unknown, ...args: unknown[]):
|
|
27
|
-
warn(message?: unknown, ...args: unknown[]):
|
|
28
|
-
security(message?: unknown, ...args: unknown[]):
|
|
29
|
-
log(message?: unknown, ...args: unknown[]):
|
|
30
|
-
newline():
|
|
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
|
|
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;;;;;;;;;GASG;AACH,qBAAa,MAAM;IACjB,OAAO,CAAC,MAAM,CAAU;IACxB,OAAO,CAAC,SAAS,CAAC,CAAS;IAC3B,OAAO,CAAC,SAAS,CAAU;IAC3B,OAAO,CAAC,MAAM,CAAC,MAAM,CAAS;IAE9B;;;;;;OAMG;gBACS,SAAS,CAAC,EAAE,MAAM,EAAE,OAAO,GAAE;QAAE,MAAM,CAAC,EAAE,KA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G,CAAC,OAAO,CAAC,EAAE,OAAO,EAAE,GAAG,IAAI,EAAE,OAAO,EAAE,GAAG,IAAI;IAIvD,MAAM,CAAC,OAAO;IAId,OAAO,CAAC,MAAM,CAAC,QAAQ;CASxB"}
|
package/dist/types/types.d.ts
CHANGED
|
@@ -61,17 +61,24 @@ export declare enum BitcoinNetworkNames {
|
|
|
61
61
|
testnet4 = 4,
|
|
62
62
|
mutinynet = 5
|
|
63
63
|
}
|
|
64
|
-
export type
|
|
64
|
+
export type DecentralizedIdentifier = string;
|
|
65
|
+
export type Did = DecentralizedIdentifier;
|
|
65
66
|
export type BeaconUri = string;
|
|
66
67
|
export type DidPlaceholder = 'did:btcr2:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
|
|
67
68
|
export type CanonicalizedProofConfig = string;
|
|
68
69
|
export type CryptosuiteName = 'bip340-jcs-2025' | 'bip340-rdfc-2025';
|
|
69
|
-
export type
|
|
70
|
-
export type
|
|
71
|
-
export type
|
|
72
|
-
export type
|
|
70
|
+
export type JsonPrimitive = string | number | boolean | null;
|
|
71
|
+
export type JsonArray = JsonValue[];
|
|
72
|
+
export type JsonValue = JsonPrimitive | JsonArray | JsonObject;
|
|
73
|
+
export type JsonObject = {
|
|
74
|
+
[key: string]: JsonValue;
|
|
75
|
+
};
|
|
76
|
+
export type JSONObject = JsonObject;
|
|
73
77
|
export type Prototyped = JSONObject;
|
|
74
78
|
export type Unprototyped = JSONObject;
|
|
79
|
+
export type ContextObject = Record<string, JsonValue>;
|
|
80
|
+
export type Context = string | string[] | ContextObject | ContextObject[];
|
|
81
|
+
export type Maybe<T> = T | unknown;
|
|
75
82
|
export type TwoDigits = `${number}${number}`;
|
|
76
83
|
export type ThreeDigits = `${number}${number}${number}`;
|
|
77
84
|
export type Year = `${1 | 2}${ThreeDigits}`;
|
|
@@ -85,4 +92,5 @@ export type TzOffset = `${Hours}:${Minutes}`;
|
|
|
85
92
|
export type DateTimestamp = `${UtcTimestamp}Z` | `${UtcTimestamp}-${TzOffset}`;
|
|
86
93
|
export type CanonicalizableObject = Record<string, any>;
|
|
87
94
|
export type CanonicalizationAlgorithm = 'jcs' | 'rdfc';
|
|
95
|
+
export type CanonicalizationEncoding = 'hex' | 'base58';
|
|
88
96
|
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,GAA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
+
/**
|
|
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 getUTCDateTime(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
|
+
}
|
|
@@ -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,cAAc,CAAC,IAAI,GAAE,IAAiB,GAAG,MAAM;IAQtD;;;;OAIG;IACH,MAAM,CAAC,aAAa,CAAC,IAAI,GAAE,IAAiB,GAAG,MAAM;CAOtD"}
|
|
@@ -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,eAAe,CAAC,EAAE,OAAO,CAAC;IAC1B,SAAS,CAAC,EAAE,CAAC,KAAK,EAAE,GAAG,KAAK,GAAG,CAAC;CACjC,CA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AAC,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": "
|
|
3
|
+
"version": "3.0.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",
|
package/src/canonicalization.ts
CHANGED
|
@@ -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
|
|
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'
|
|
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.
|
|
23
|
+
this._defaultAlgorithm = Canonicalization.normalizeAlgorithm(algorithm);
|
|
26
24
|
}
|
|
27
25
|
|
|
28
26
|
/**
|
|
29
|
-
*
|
|
30
|
-
* @
|
|
27
|
+
* Gets the canonicalization algorithm.
|
|
28
|
+
* @returns {CanonicalizationAlgorithm} The current canonicalization algorithm.
|
|
31
29
|
*/
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
30
|
+
get algorithm(): CanonicalizationAlgorithm {
|
|
31
|
+
return this._defaultAlgorithm;
|
|
32
|
+
}
|
|
35
33
|
|
|
36
|
-
|
|
37
|
-
|
|
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
|
-
|
|
41
|
-
this._algorithm = algorithm;
|
|
45
|
+
return normalized;
|
|
42
46
|
}
|
|
43
47
|
|
|
44
48
|
/**
|
|
45
|
-
*
|
|
46
|
-
* @
|
|
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
|
-
|
|
49
|
-
|
|
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,50 +66,56 @@ 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
|
|
69
|
+
* Step 1 Canonicalize (JCS) → Step 2 Hash (SHA256) → Step 3 Encode (Hex/Base58).
|
|
60
70
|
*
|
|
61
|
-
* @param {
|
|
62
|
-
* @param {
|
|
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.
|
|
63
75
|
* @returns {Promise<string>} The final SHA-256 hash bytes as a hex string.
|
|
64
76
|
*/
|
|
65
|
-
|
|
77
|
+
async process(object: Record<any, any>, options: {
|
|
78
|
+
encoding?: CanonicalizationEncoding;
|
|
79
|
+
algorithm?: CanonicalizationAlgorithm;
|
|
80
|
+
multibase?: boolean;
|
|
81
|
+
} = {}): Promise<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 = await 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
|
|
78
|
-
* @param {
|
|
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.
|
|
79
99
|
* @returns {Promise<string>} The canonicalized object.
|
|
80
100
|
*/
|
|
81
|
-
|
|
82
|
-
|
|
101
|
+
async canonicalize(object: Record<any, any>, algorithm: CanonicalizationAlgorithm = this._defaultAlgorithm): Promise<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 {
|
|
112
|
+
* @param {Record<any, any>} object The object to canonicalize.
|
|
88
113
|
* @returns {string} The canonicalized object.
|
|
89
114
|
*/
|
|
90
|
-
|
|
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.
|
|
@@ -112,19 +128,18 @@ export class Canonicalization {
|
|
|
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 {
|
|
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:
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
default:
|
|
126
|
-
throw new CanonicalizationError(`Unsupported encoding: ${encoding}`, 'ENCODING_ERROR');
|
|
135
|
+
public 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
|
/**
|
|
@@ -142,17 +157,21 @@ export class Canonicalization {
|
|
|
142
157
|
* @returns {string} The hash as a hex string.
|
|
143
158
|
*/
|
|
144
159
|
public base58(hashBytes: HashBytes): string {
|
|
145
|
-
|
|
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 {
|
|
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(
|
|
155
|
-
|
|
170
|
+
public async canonicalhash(
|
|
171
|
+
object: Record<any, any>,
|
|
172
|
+
algorithm: CanonicalizationAlgorithm = this._defaultAlgorithm
|
|
173
|
+
): Promise<HashBytes> {
|
|
174
|
+
const canonicalized = await this.canonicalize(object, algorithm);
|
|
156
175
|
return this.hash(canonicalized);
|
|
157
176
|
}
|
|
158
177
|
|
|
@@ -163,7 +182,7 @@ export class Canonicalization {
|
|
|
163
182
|
* @returns {string} The SHA-256 hash as a hex string.
|
|
164
183
|
*/
|
|
165
184
|
public hashhex(canonicalized: string): string {
|
|
166
|
-
return this.encode(this.hash(canonicalized));
|
|
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
|
|
176
|
-
return this.encode(this.hash(canonicalized), 'base58');
|
|
194
|
+
public hashbase58(canonicalized: string): string {
|
|
195
|
+
return this.encode(this.hash(canonicalized), 'base58', false);
|
|
177
196
|
}
|
|
178
197
|
}
|
|
179
|
-
|
|
180
|
-
export const canonicalization = new Canonicalization();
|