@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
package/src/constants.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { sha256 } from '@noble/hashes/sha2';
|
|
2
|
+
import { bytesToHex } from '@noble/hashes/utils';
|
|
2
3
|
import { Bytes, HashHex } from './types.js';
|
|
3
4
|
|
|
4
5
|
export const ID_PLACEHOLDER_VALUE = 'did:btcr2:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
|
|
@@ -9,32 +10,38 @@ export const MULTIBASE_URI_PREFIX = 'urn:mb:';
|
|
|
9
10
|
export const INITIAL_BLOCK_REWARD = 50;
|
|
10
11
|
export const HALVING_INTERVAL = 150;
|
|
11
12
|
export const COINBASE_MATURITY_DELAY = 100;
|
|
12
|
-
export const
|
|
13
|
+
export const DEFAULT_POLAR_CONFIG = {
|
|
13
14
|
username : 'polaruser',
|
|
14
15
|
password : 'polarpass',
|
|
15
16
|
host : 'http://127.0.0.1:18443',
|
|
16
17
|
allowDefaultWallet : true,
|
|
17
18
|
version : '28.1.0'
|
|
18
19
|
};
|
|
19
|
-
export const POLAR_ALICE_CLIENT_CONFIG = {
|
|
20
|
-
username : 'polaruser',
|
|
21
|
-
password : 'polarpass',
|
|
22
|
-
host : 'http://127.0.0.1:18444',
|
|
23
|
-
allowDefaultWallet : true,
|
|
24
|
-
version : '28.1.0'
|
|
25
|
-
};
|
|
26
20
|
export const DEFAULT_REST_CONFIG = { host: 'http://localhost:3000' };
|
|
27
|
-
export const DEFAULT_RPC_CONFIG =
|
|
21
|
+
export const DEFAULT_RPC_CONFIG = DEFAULT_POLAR_CONFIG;
|
|
28
22
|
export const DEFAULT_BLOCK_CONFIRMATIONS = 7;
|
|
29
23
|
|
|
24
|
+
/**
|
|
25
|
+
* Load a default RPC config, allowing environment overrides to avoid hard-coding credentials/hosts in bundles.
|
|
26
|
+
* @returns {typeof DEFAULT_POLAR_CONFIG} The RPC config.
|
|
27
|
+
*/
|
|
28
|
+
export function getDefaultRpcConfig(): typeof DEFAULT_POLAR_CONFIG {
|
|
29
|
+
return {
|
|
30
|
+
...DEFAULT_POLAR_CONFIG,
|
|
31
|
+
host : process.env.BTCR2_RPC_HOST ?? DEFAULT_POLAR_CONFIG.host,
|
|
32
|
+
username : process.env.BTCR2_RPC_USER ?? DEFAULT_POLAR_CONFIG.username,
|
|
33
|
+
password : process.env.BTCR2_RPC_PASS ?? DEFAULT_POLAR_CONFIG.password,
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
|
|
30
37
|
// Fixed public key header bytes per the Data Integrity BIP340 Cryptosuite spec: [0xe7, 0x01] / [231, 1]
|
|
31
38
|
export const BIP340_PUBLIC_KEY_MULTIBASE_PREFIX: Bytes = new Uint8Array([0xe7, 0x01]);
|
|
32
39
|
// Hash of the BIP-340 Multikey prefix
|
|
33
|
-
export const BIP340_PUBLIC_KEY_MULTIBASE_PREFIX_HASH: HashHex =
|
|
40
|
+
export const BIP340_PUBLIC_KEY_MULTIBASE_PREFIX_HASH: HashHex = bytesToHex(sha256(BIP340_PUBLIC_KEY_MULTIBASE_PREFIX));
|
|
34
41
|
// Fixed secret key header bytes per the Data Integrity BIP340 Cryptosuite spec: [0x81, 0x26] / [129, 38]
|
|
35
42
|
export const BIP340_SECRET_KEY_MULTIBASE_PREFIX: Bytes = new Uint8Array([0x81, 0x26]);
|
|
36
43
|
// Hash of the BIP-340 Multikey prefix
|
|
37
|
-
export const BIP340_SECRET_KEY_MULTIBASE_PREFIX_HASH: HashHex =
|
|
44
|
+
export const BIP340_SECRET_KEY_MULTIBASE_PREFIX_HASH: HashHex = bytesToHex(sha256(BIP340_SECRET_KEY_MULTIBASE_PREFIX));
|
|
38
45
|
// curve's field size
|
|
39
46
|
export const B256 = 2n ** 256n;
|
|
40
47
|
// curve's field prime
|
|
@@ -72,35 +79,32 @@ export const W3C_DID_RESOLUTION_V1 = 'https://w3id.org/did-resolution/v1';
|
|
|
72
79
|
export const CONTEXT_URL_MAP = {
|
|
73
80
|
w3c : {
|
|
74
81
|
did : {
|
|
75
|
-
v1 :
|
|
76
|
-
v1_1 :
|
|
82
|
+
v1 : W3C_DID_V1,
|
|
83
|
+
v1_1 : W3C_DID_V1_1,
|
|
77
84
|
},
|
|
78
85
|
didresolution : {
|
|
79
|
-
v1 :
|
|
86
|
+
v1 : W3C_DID_RESOLUTION_V1,
|
|
80
87
|
},
|
|
81
88
|
security : {
|
|
82
|
-
v2 :
|
|
89
|
+
v2 : W3C_SECURITY_V2,
|
|
83
90
|
},
|
|
84
91
|
dataintegrity : {
|
|
85
|
-
v1 :
|
|
86
|
-
v2 :
|
|
92
|
+
v1 : W3C_DATA_INTEGRITY_V1,
|
|
93
|
+
v2 : W3C_DATA_INTEGRITY_V2,
|
|
87
94
|
},
|
|
88
95
|
zcap : {
|
|
89
|
-
v1 :
|
|
96
|
+
v1 : W3C_ZCAP_V1,
|
|
90
97
|
},
|
|
91
98
|
jsonldpatch : {
|
|
92
|
-
v1 :
|
|
99
|
+
v1 : W3C_JSONLD_PATCH_V1,
|
|
93
100
|
},
|
|
94
101
|
multikey : {
|
|
95
|
-
v1 :
|
|
102
|
+
v1 : W3C_MULTIKEY_V1,
|
|
96
103
|
},
|
|
97
104
|
},
|
|
98
105
|
btcr2 : {
|
|
99
|
-
diddocument : {
|
|
100
|
-
v1 : 'https://dcdpr.github.io/did-btcr2-js/ns/did-document/v1',
|
|
101
|
-
},
|
|
102
106
|
method : {
|
|
103
|
-
v1 :
|
|
107
|
+
v1 : BTCR2_METHOD_CONTEXT
|
|
104
108
|
},
|
|
105
109
|
},
|
|
106
110
|
|
|
@@ -117,7 +121,8 @@ export const BTCR2_MULTIKEY_CONTEXT = [
|
|
|
117
121
|
];
|
|
118
122
|
|
|
119
123
|
export const BTCR2_DID_UPDATE_PAYLOAD_CONTEXT = [
|
|
124
|
+
CONTEXT_URL_MAP.btcr2.method.v1,
|
|
120
125
|
CONTEXT_URL_MAP.w3c.security.v2,
|
|
121
126
|
CONTEXT_URL_MAP.w3c.zcap.v1,
|
|
122
127
|
CONTEXT_URL_MAP.w3c.jsonldpatch.v1,
|
|
123
|
-
];
|
|
128
|
+
];
|
package/src/index.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import './exts.js';
|
|
2
|
-
|
|
3
1
|
export * from './canonicalization.js';
|
|
4
2
|
export * from './constants.js';
|
|
5
3
|
export * from './errors.js';
|
|
6
4
|
export * from './interfaces.js';
|
|
5
|
+
export * from './json-patch.js';
|
|
7
6
|
export * from './logger.js';
|
|
8
|
-
export * from './patch.js';
|
|
9
7
|
export * from './types.js';
|
|
10
|
-
|
|
11
|
-
export * from './
|
|
8
|
+
export * from './utils/date.js';
|
|
9
|
+
export * from './utils/json.js';
|
|
10
|
+
export * from './utils/set.js';
|
|
11
|
+
export * from './utils/string.js';
|
package/src/interfaces.ts
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
export type JsonPatch = Array<PatchOperation>;
|
|
2
|
-
export type PatchOpCode = 'add' | 'remove' | 'replace' | 'move' | 'copy' | 'test' | string;
|
|
2
|
+
export type PatchOpCode = 'add' | 'remove' | 'replace' | 'move' | 'copy' | 'test' | (string & {});
|
|
3
3
|
/**
|
|
4
4
|
* A JSON Patch operation, as defined in {@link https://datatracker.ietf.org/doc/html/rfc6902 | RFC 6902}.
|
|
5
5
|
*/
|
|
6
6
|
export interface PatchOperation {
|
|
7
7
|
op: PatchOpCode;
|
|
8
8
|
path: string;
|
|
9
|
-
value?:
|
|
9
|
+
value?: unknown; // Required for add, replace, test
|
|
10
10
|
from?: string; // Required for move, copy
|
|
11
11
|
}
|
|
12
12
|
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import jsonPatch, { Operation } from 'fast-json-patch';
|
|
2
|
+
import { MethodError } from './errors.js';
|
|
3
|
+
import { PatchOperation } from './interfaces.js';
|
|
4
|
+
import { JSONObject } from './types.js';
|
|
5
|
+
|
|
6
|
+
const { applyPatch, compare, deepClone } = jsonPatch;
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Thin wrapper around fast-json-patch to keep a stable API within this package.
|
|
10
|
+
* @class JSONPatch
|
|
11
|
+
* @type {JSONPatch}
|
|
12
|
+
*/
|
|
13
|
+
export class JSONPatch {
|
|
14
|
+
/**
|
|
15
|
+
* Applies a JSON Patch to a source document and returns the patched document.
|
|
16
|
+
* Does not mutate the input document.
|
|
17
|
+
* @param {JSONObject} sourceDocument - The source JSON document to apply the patch to.
|
|
18
|
+
* @param {PatchOperation[]} operations - The JSON Patch operations to apply.
|
|
19
|
+
* @returns {JSONObject} The patched JSON document.
|
|
20
|
+
*/
|
|
21
|
+
static apply(
|
|
22
|
+
sourceDocument: Record<any, any>,
|
|
23
|
+
operations: PatchOperation[],
|
|
24
|
+
options: { mutate?: boolean; clone?: (value: any) => any } = {}
|
|
25
|
+
): Record<any, any> {
|
|
26
|
+
const mutate = options.mutate ?? false;
|
|
27
|
+
const cloneFn = options.clone ?? deepClone;
|
|
28
|
+
const docClone = mutate ? sourceDocument : cloneFn(sourceDocument);
|
|
29
|
+
const validationError = this.validateOperations(operations);
|
|
30
|
+
if (validationError) {
|
|
31
|
+
throw new MethodError('Invalid JSON Patch operations', 'JSON_PATCH_APPLY_ERROR', { error: validationError });
|
|
32
|
+
}
|
|
33
|
+
try {
|
|
34
|
+
const result = applyPatch(docClone, operations as Operation[], true, mutate);
|
|
35
|
+
if (result.newDocument === undefined) {
|
|
36
|
+
throw new MethodError('JSON Patch application failed', 'JSON_PATCH_APPLY_ERROR', { result });
|
|
37
|
+
}
|
|
38
|
+
return result.newDocument as JSONObject;
|
|
39
|
+
} catch (error) {
|
|
40
|
+
throw new MethodError('JSON Patch application failed', 'JSON_PATCH_APPLY_ERROR', { error });
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Compute a JSON Patch diff from source => target.
|
|
46
|
+
* @param {JSONObject} sourceDocument - The source JSON document.
|
|
47
|
+
* @param {JSONObject} targetDocument - The target JSON document.
|
|
48
|
+
* @param {string} [path] - An optional base path to prefix to each operation.
|
|
49
|
+
* @returns {PatchOperation[]} The computed JSON Patch operations.
|
|
50
|
+
*/
|
|
51
|
+
static diff(sourceDocument: JSONObject, targetDocument: JSONObject, path: string = ''): PatchOperation[] {
|
|
52
|
+
const ops = compare(sourceDocument ?? {}, targetDocument ?? {}) as PatchOperation[];
|
|
53
|
+
if (!path) return ops;
|
|
54
|
+
|
|
55
|
+
const prefix = path.endsWith('/') ? path.slice(0, -1) : path;
|
|
56
|
+
return ops.map(op => ({
|
|
57
|
+
...op,
|
|
58
|
+
path : this.joinPointer(prefix, op.path)
|
|
59
|
+
}));
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Join a base pointer prefix with an operation path ensuring correct escaping.
|
|
64
|
+
* @param {string} prefix - The base pointer prefix.
|
|
65
|
+
* @param {string} opPath - The operation path.
|
|
66
|
+
* @returns {string} The joined pointer.
|
|
67
|
+
*/
|
|
68
|
+
static joinPointer(prefix: string, opPath: string): string {
|
|
69
|
+
if (!prefix) return opPath;
|
|
70
|
+
const normalizedPrefix = prefix.startsWith('/') ? prefix : `/${prefix}`;
|
|
71
|
+
return `${this.escapeSegmentPath(normalizedPrefix)}${opPath}`;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Escape a JSON Pointer segment according to RFC 6901.
|
|
76
|
+
* @param {string} pointer - The JSON Pointer to escape.
|
|
77
|
+
* @returns {string} The escaped JSON Pointer.
|
|
78
|
+
*/
|
|
79
|
+
static escapeSegmentPath(pointer: string): string {
|
|
80
|
+
return pointer
|
|
81
|
+
.split('/')
|
|
82
|
+
.map((segment, idx) => idx === 0 ? segment : segment.replace(/~/g, '~0').replace(/\//g, '~1'))
|
|
83
|
+
.join('/');
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Validate JSON Patch operations.
|
|
88
|
+
* @param {PatchOperation[]} operations - The operations to validate.
|
|
89
|
+
* @returns {Error | null} An Error if validation fails, otherwise null.
|
|
90
|
+
*/
|
|
91
|
+
static validateOperations(operations: PatchOperation[]): Error | null {
|
|
92
|
+
if (!Array.isArray(operations)) return new Error('Operations must be an array');
|
|
93
|
+
for (const op of operations) {
|
|
94
|
+
if (!op || typeof op !== 'object') return new Error('Operation must be an object');
|
|
95
|
+
if (typeof op.op !== 'string') return new Error('Operation.op must be a string');
|
|
96
|
+
if (typeof op.path !== 'string') return new Error('Operation.path must be a string');
|
|
97
|
+
if ((op.op === 'move' || op.op === 'copy') && typeof op.from !== 'string') {
|
|
98
|
+
return new Error(`Operation.from must be a string for op=${op.op}`);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
return null;
|
|
102
|
+
}
|
|
103
|
+
}
|
package/src/logger.ts
CHANGED
|
@@ -51,18 +51,35 @@ const LEVEL_METHODS: Record<Level, keyof Console> = {
|
|
|
51
51
|
* - File/line tracing
|
|
52
52
|
* - Timestamps
|
|
53
53
|
* - Colorized output
|
|
54
|
+
* @class Logger
|
|
55
|
+
* @type {Logger}
|
|
54
56
|
*/
|
|
55
57
|
export class Logger {
|
|
56
58
|
private levels: Level[];
|
|
57
59
|
private namespace?: string;
|
|
60
|
+
private useColors: boolean;
|
|
61
|
+
private static shared: Logger;
|
|
58
62
|
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
63
|
+
/**
|
|
64
|
+
* Creates a new Logger instance.
|
|
65
|
+
* @param {string} namespace - Optional namespace for log messages.
|
|
66
|
+
* @param {Object} options - Configuration options.
|
|
67
|
+
* @param {Level[]} options.levels - Log levels to enable.
|
|
68
|
+
* @param {boolean} options.useColors - Whether to use colored output.
|
|
69
|
+
*/
|
|
70
|
+
constructor(namespace?: string, options: { levels?: Level[]; useColors?: boolean } = {}) {
|
|
71
|
+
this.levels = options.levels || LOG_LEVELS[NODE_ENV] || [];
|
|
72
|
+
this.namespace = namespace || 'did-btcr2-js';
|
|
73
|
+
const envForce = process.env.LOG_COLORS;
|
|
74
|
+
this.useColors = options.useColors || (envForce ? envForce !== '0' && envForce.toLowerCase() !== 'false' : Boolean(process.stdout.isTTY));
|
|
62
75
|
}
|
|
63
76
|
|
|
64
77
|
/**
|
|
65
78
|
* Logs a message with the specified level.
|
|
79
|
+
* @param {Level} level - The log level.
|
|
80
|
+
* @param {unknown} message - The message to log.
|
|
81
|
+
* @param {...unknown[]} args - Additional arguments to log.
|
|
82
|
+
* @returns {void}
|
|
66
83
|
*/
|
|
67
84
|
private _log(level: Level, message?: unknown, ...args: unknown[]): void {
|
|
68
85
|
if (!this.levels.includes(level)) return;
|
|
@@ -72,70 +89,85 @@ export class Logger {
|
|
|
72
89
|
|
|
73
90
|
const timestamp = new Date().toISOString();
|
|
74
91
|
const namespace = this.namespace ? `[${this.namespace}]` : '';
|
|
92
|
+
const render = this.useColors ? color : (v: string) => v;
|
|
93
|
+
const renderGray = this.useColors ? chalk.gray : (v: string) => v;
|
|
75
94
|
|
|
76
95
|
(console[method] as (...args: any[]) => void)(
|
|
77
|
-
`${
|
|
96
|
+
`${renderGray(timestamp)} ${namespace} ${render(level)}: ${message}`,
|
|
78
97
|
...args
|
|
79
98
|
);
|
|
80
99
|
}
|
|
81
100
|
|
|
82
|
-
|
|
83
|
-
public debug(message?: unknown, ...args: unknown[]) {
|
|
101
|
+
debug(message?: unknown, ...args: unknown[]): Logger {
|
|
84
102
|
this._log('debug', message, ...args); return this;
|
|
85
103
|
}
|
|
86
104
|
|
|
87
|
-
|
|
105
|
+
error(message?: unknown, ...args: unknown[]): Logger {
|
|
88
106
|
this._log('error', message, ...args); return this;
|
|
89
107
|
}
|
|
90
108
|
|
|
91
|
-
|
|
109
|
+
info(message?: unknown, ...args: unknown[]): Logger {
|
|
92
110
|
this._log('info', message, ...args); return this;
|
|
93
111
|
}
|
|
94
112
|
|
|
95
|
-
|
|
113
|
+
warn(message?: unknown, ...args: unknown[]): Logger {
|
|
96
114
|
this._log('warn', message, ...args); return this;
|
|
97
115
|
}
|
|
98
116
|
|
|
99
|
-
|
|
117
|
+
security(message?: unknown, ...args: unknown[]): Logger {
|
|
100
118
|
this._log('security', message, ...args); return this;
|
|
101
119
|
}
|
|
102
120
|
|
|
103
|
-
|
|
121
|
+
log(message?: unknown, ...args: unknown[]): Logger {
|
|
104
122
|
this._log('log', message, ...args); return this;
|
|
105
123
|
}
|
|
106
124
|
|
|
107
|
-
|
|
125
|
+
newline(): Logger {
|
|
108
126
|
console.log(); return this;
|
|
109
127
|
}
|
|
110
128
|
|
|
111
129
|
/**
|
|
112
130
|
* Static methods for convenience (auto-instantiate).
|
|
131
|
+
* These use a shared singleton instance.
|
|
132
|
+
* @param {unknown} message - The message to log.
|
|
133
|
+
* @param {...unknown[]} args - Additional arguments to log.
|
|
134
|
+
* @returns {void}
|
|
113
135
|
*/
|
|
114
|
-
|
|
115
|
-
|
|
136
|
+
static debug(message?: unknown, ...args: unknown[]): void {
|
|
137
|
+
Logger.instance().debug(message, ...args);
|
|
116
138
|
}
|
|
117
139
|
|
|
118
|
-
|
|
119
|
-
|
|
140
|
+
static error(message?: unknown, ...args: unknown[]): void {
|
|
141
|
+
Logger.instance().error(message, ...args);
|
|
120
142
|
}
|
|
121
143
|
|
|
122
|
-
|
|
123
|
-
|
|
144
|
+
static info(message?: unknown, ...args: unknown[]): void {
|
|
145
|
+
Logger.instance().info(message, ...args);
|
|
124
146
|
}
|
|
125
147
|
|
|
126
|
-
|
|
127
|
-
|
|
148
|
+
static warn(message?: unknown, ...args: unknown[]): void {
|
|
149
|
+
Logger.instance().warn(message, ...args);
|
|
128
150
|
}
|
|
129
151
|
|
|
130
|
-
|
|
131
|
-
|
|
152
|
+
static security(message?: unknown, ...args: unknown[]): void {
|
|
153
|
+
Logger.instance().security(message, ...args);
|
|
132
154
|
}
|
|
133
155
|
|
|
134
|
-
|
|
135
|
-
|
|
156
|
+
static log(message?: unknown, ...args: unknown[]): void {
|
|
157
|
+
Logger.instance().log(message, ...args);
|
|
136
158
|
}
|
|
137
159
|
|
|
138
|
-
|
|
139
|
-
|
|
160
|
+
static newline() {
|
|
161
|
+
Logger.instance().newline();
|
|
140
162
|
}
|
|
141
|
-
|
|
163
|
+
|
|
164
|
+
private static instance(levels?: Level[], useColors?: boolean): Logger {
|
|
165
|
+
if (!Logger.shared) {
|
|
166
|
+
Logger.shared = new Logger(undefined, { levels, useColors });
|
|
167
|
+
} else {
|
|
168
|
+
if (levels) Logger.shared.levels = levels;
|
|
169
|
+
if (useColors !== undefined) Logger.shared.useColors = useColors;
|
|
170
|
+
}
|
|
171
|
+
return Logger.shared;
|
|
172
|
+
}
|
|
173
|
+
}
|
package/src/types.ts
CHANGED
|
@@ -66,19 +66,24 @@ export enum BitcoinNetworkNames {
|
|
|
66
66
|
testnet4 = 4,
|
|
67
67
|
mutinynet = 5
|
|
68
68
|
}
|
|
69
|
-
export type
|
|
69
|
+
export type DecentralizedIdentifier = string;
|
|
70
|
+
export type Did = DecentralizedIdentifier;
|
|
70
71
|
export type BeaconUri = string;
|
|
71
72
|
export type DidPlaceholder = 'did:btcr2:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
|
|
72
73
|
export type CanonicalizedProofConfig = string;
|
|
73
74
|
export type CryptosuiteName = 'bip340-jcs-2025' | 'bip340-rdfc-2025';
|
|
74
|
-
export type
|
|
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
|
|
80
|
+
export type Prototyped = JSONObject;
|
|
81
|
+
export type Unprototyped = JSONObject;
|
|
82
|
+
export type ContextObject = Record<string, JsonValue>;
|
|
75
83
|
export type Context = string | string[] | ContextObject | ContextObject[]
|
|
76
84
|
|
|
77
85
|
/* General Types */
|
|
78
|
-
export type Maybe<T> = T |
|
|
79
|
-
export type JSONObject = Record<string | number | symbol, any>; // JSON object: prototyped or unprototyped
|
|
80
|
-
export type Prototyped = JSONObject;
|
|
81
|
-
export type Unprototyped = JSONObject;
|
|
86
|
+
export type Maybe<T> = T | unknown;
|
|
82
87
|
export type TwoDigits = `${number}${number}`;
|
|
83
88
|
export type ThreeDigits = `${number}${number}${number}`;
|
|
84
89
|
export type Year = `${1 | 2}${ThreeDigits}`;
|
|
@@ -92,4 +97,5 @@ export type TzOffset = `${Hours}:${Minutes}`;
|
|
|
92
97
|
export type DateTimestamp = `${UtcTimestamp}Z` | `${UtcTimestamp}-${TzOffset}`;
|
|
93
98
|
export type CanonicalizableObject = Record<string, any>;
|
|
94
99
|
export type CanonicalizationAlgorithm = 'jcs' | 'rdfc';
|
|
95
|
-
export type
|
|
100
|
+
export type CanonicalizationEncoding = 'hex' | 'base58';
|
|
101
|
+
export type UnixTimestamp = number;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Utility class for date-related operations.
|
|
3
|
+
* @name DateUtils
|
|
4
|
+
* @class DateUtils
|
|
5
|
+
*/
|
|
6
|
+
export 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 getUTCDateTime(date: Date = new Date()): string {
|
|
13
|
+
const time = date.getTime();
|
|
14
|
+
if (Number.isNaN(time)) {
|
|
15
|
+
throw new Error(`Invalid date: ${date}`);
|
|
16
|
+
}
|
|
17
|
+
return date.toISOString().replace(/\.\d{3}Z$/, 'Z');
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Unix timestamp in seconds (integer).
|
|
22
|
+
* @param {Date} [date=new Date()] - The date to convert.
|
|
23
|
+
* @returns {number} The Unix timestamp in seconds.
|
|
24
|
+
*/
|
|
25
|
+
static toUnixSeconds(date: Date = new Date()): number {
|
|
26
|
+
const time = date.getTime();
|
|
27
|
+
if (Number.isNaN(time)) {
|
|
28
|
+
throw new Error(`Invalid date: ${date}`);
|
|
29
|
+
}
|
|
30
|
+
return Math.floor(date.getTime() / 1000);
|
|
31
|
+
}
|
|
32
|
+
}
|