@did-btcr2/common 3.1.0 → 4.0.1

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.
@@ -1,5 +1,5 @@
1
1
  import { sha256 } from '@noble/hashes/sha2';
2
- import { bytesToHex } from '@noble/hashes/utils';
2
+ import { bytesToHex, hexToBytes } from '@noble/hashes/utils';
3
3
  import { canonicalize as jcsa } from 'json-canonicalize';
4
4
  import { base58btc } from 'multiformats/bases/base58';
5
5
  import { CanonicalizationError } from './errors.js';
@@ -11,21 +11,6 @@ import { CanonicalizationError } from './errors.js';
11
11
  * @type {Canonicalization}
12
12
  */
13
13
  export class Canonicalization {
14
- _defaultAlgorithm;
15
- /**
16
- * Initializes the Canonicalization class with the specified algorithm.
17
- * @param {CanonicalizationAlgorithm} algorithm The canonicalization algorithm to use ('jcs').
18
- */
19
- constructor(algorithm = 'jcs') {
20
- this._defaultAlgorithm = Canonicalization.normalizeAlgorithm(algorithm);
21
- }
22
- /**
23
- * Gets the canonicalization algorithm.
24
- * @returns {CanonicalizationAlgorithm} The current canonicalization algorithm.
25
- */
26
- get algorithm() {
27
- return this._defaultAlgorithm;
28
- }
29
14
  /**
30
15
  * Normalizes the canonicalization algorithm.
31
16
  * @param {CanonicalizationAlgorithm} algorithm
@@ -67,15 +52,17 @@ export class Canonicalization {
67
52
  * @param {CanonicalizationAlgorithm} [options.algorithm] The canonicalization algorithm to use.
68
53
  * @returns {string} The final SHA-256 hash bytes as a hex string.
69
54
  */
70
- process(object, options = {}) {
71
- const algorithm = Canonicalization.normalizeAlgorithm(options.algorithm ?? this._defaultAlgorithm);
72
- const encoding = Canonicalization.normalizeEncoding(options.encoding ?? 'hex');
55
+ static process(object, options) {
56
+ // Normalize the algorithm
57
+ const algorithm = Canonicalization.normalizeAlgorithm(options?.algorithm ?? 'jcs');
58
+ // Normalize the encoding
59
+ const encoding = Canonicalization.normalizeEncoding(options?.encoding ?? 'hex');
73
60
  // Step 1: Canonicalize
74
61
  const canonicalized = this.canonicalize(object, algorithm);
75
62
  // Step 2: Hash
76
- const hashed = this.hash(canonicalized);
63
+ const hashed = this.toHash(canonicalized);
77
64
  // Step 3: Encode
78
- const encoded = this.encode(hashed, encoding, options.multibase ?? false);
65
+ const encoded = this.encode(hashed, encoding);
79
66
  // Return the encoded string
80
67
  return encoded;
81
68
  }
@@ -85,7 +72,7 @@ export class Canonicalization {
85
72
  * @param {CanonicalizationAlgorithm} [algorithm] The algorithm to use.
86
73
  * @returns {string} The canonicalized object.
87
74
  */
88
- canonicalize(object, algorithm = this._defaultAlgorithm) {
75
+ static canonicalize(object, algorithm = 'jcs') {
89
76
  switch (Canonicalization.normalizeAlgorithm(algorithm)) {
90
77
  case 'jcs':
91
78
  return this.jcs(object);
@@ -98,7 +85,7 @@ export class Canonicalization {
98
85
  * @param {Record<any, any>} object The object to canonicalize.
99
86
  * @returns {string} The canonicalized object.
100
87
  */
101
- jcs(object) {
88
+ static jcs(object) {
102
89
  return jcsa(object);
103
90
  }
104
91
  /**
@@ -106,7 +93,7 @@ export class Canonicalization {
106
93
  * @param {string} canonicalized The canonicalized object.
107
94
  * @returns {HashBytes} The SHA-256 HashBytes (Uint8Array).
108
95
  */
109
- hash(canonicalized) {
96
+ static toHash(canonicalized) {
110
97
  return sha256(canonicalized);
111
98
  }
112
99
  /**
@@ -116,32 +103,72 @@ export class Canonicalization {
116
103
  * @throws {CanonicalizationError} If the encoding format is not supported.
117
104
  * @returns {string} The encoded string.
118
105
  */
119
- encode(canonicalizedhash, encoding = 'hex', multibase = false) {
106
+ static encode(canonicalizedhash, encoding = 'hex') {
107
+ // Normalize encoding
120
108
  const normalized = Canonicalization.normalizeEncoding(encoding);
121
- if (normalized === 'hex')
122
- return this.hex(canonicalizedhash);
109
+ // If encoding is hex, encode to hex
110
+ if (normalized === 'hex') {
111
+ return this.toHex(canonicalizedhash);
112
+ }
113
+ // If encoding is base58, encode to base58
123
114
  if (normalized === 'base58') {
124
- const encoded = this.base58(canonicalizedhash);
125
- return multibase ? `z${encoded}` : encoded;
115
+ return this.toBase58(canonicalizedhash);
126
116
  }
117
+ // Throw error if encoding is unsupported
127
118
  throw new CanonicalizationError(`Unsupported encoding: ${encoding}`, 'ENCODING_ERROR');
128
119
  }
120
+ /**
121
+ * Decodes SHA-256 hashed, canonicalized object as a hex or base58 string.
122
+ * @param {string} canonicalizedhash The canonicalized object to encode.
123
+ * @param {CanonicalizationEncoding} encoding The encoding format ('hex' or 'base58').
124
+ * @throws {CanonicalizationError} If the encoding format is not supported.
125
+ * @returns {string} The encoded string.
126
+ */
127
+ static decode(canonicalizedhash, encoding = 'hex') {
128
+ // Normalize encoding
129
+ const normalized = Canonicalization.normalizeEncoding(encoding);
130
+ // If encoding is hex, decode from hex
131
+ if (normalized === 'hex') {
132
+ return this.fromHex(canonicalizedhash);
133
+ }
134
+ // If encoding is base58, decode from base58
135
+ if (normalized === 'base58') {
136
+ return this.fromBase58(canonicalizedhash);
137
+ }
138
+ // Throw error if encoding is unsupported
139
+ throw new CanonicalizationError(`Unsupported encoding: ${encoding}`, 'DECODING_ERROR');
140
+ }
129
141
  /**
130
142
  * Step 3.1: Encodes HashBytes (Uint8Array) to a hex string.
131
143
  * @param {HashBytes} hashBytes The hash as a Uint8Array.
132
144
  * @returns {string} The hash as a hex string.
133
145
  */
134
- hex(hashBytes) {
146
+ static toHex(hashBytes) {
135
147
  return bytesToHex(hashBytes);
136
148
  }
149
+ /**
150
+ * Decodes a hex string to HashBytes (Uint8Array).
151
+ * @param {HexString} hexString The hash as a hex string.
152
+ * @returns {HashBytes} The hash bytes.
153
+ */
154
+ static fromHex(hexString) {
155
+ return hexToBytes(hexString);
156
+ }
137
157
  /**
138
158
  * Step 3.2: Encodes HashBytes (Uint8Array) to a base58btc string.
139
159
  * @param {HashBytes} hashBytes The hash as a Uint8Array.
140
160
  * @returns {string} The hash as a hex string.
141
161
  */
142
- base58(hashBytes) {
143
- const encoded = base58btc.encode(hashBytes);
144
- return encoded.startsWith('z') ? encoded.slice(1) : encoded;
162
+ static toBase58(hashBytes) {
163
+ return base58btc.encode(hashBytes);
164
+ }
165
+ /**
166
+ * Decodes a base58 string to HashBytes (Uint8Array).
167
+ * @param {string} b58str The hash as a base58 string.
168
+ * @returns {HashBytes} The hash bytes.
169
+ */
170
+ static fromBase58(b58str) {
171
+ return base58btc.decode(b58str);
145
172
  }
146
173
  /**
147
174
  * Canonicalizes an object, hashes it and returns it as hash bytes.
@@ -149,9 +176,13 @@ export class Canonicalization {
149
176
  * @param {Record<any, any>} object The object to process.
150
177
  * @returns {Promise<HashBytes>} The final SHA-256 hash bytes.
151
178
  */
152
- canonicalhash(object, algorithm = this._defaultAlgorithm) {
179
+ static andHash(object, algorithm = 'jcs') {
180
+ // Step 1: Canonicalize
153
181
  const canonicalized = this.canonicalize(object, algorithm);
154
- return this.hash(canonicalized);
182
+ // Step 2: Hash
183
+ const hashed = this.toHash(canonicalized);
184
+ // Return canonicalized hash bytes
185
+ return hashed;
155
186
  }
156
187
  /**
157
188
  * Computes the SHA-256 hash of a canonicalized object and encodes it as a hex string.
@@ -159,8 +190,13 @@ export class Canonicalization {
159
190
  * @param {string} canonicalized The canonicalized object to hash.
160
191
  * @returns {string} The SHA-256 hash as a hex string.
161
192
  */
162
- hashhex(canonicalized) {
163
- return this.encode(this.hash(canonicalized), 'hex');
193
+ static andHashToHex(canonicalized) {
194
+ // Step 2: Hash
195
+ const hashed = this.toHash(canonicalized);
196
+ // Step 3: Encode (Hex)
197
+ const hexed = this.toHex(hashed);
198
+ // Return the hashed encoded string
199
+ return hexed;
164
200
  }
165
201
  /**
166
202
  * Computes the SHA-256 hashes of canonicalized object and encodes it as a base58 string.
@@ -168,8 +204,8 @@ export class Canonicalization {
168
204
  * @param {string} canonicalized The canonicalized object to hash.
169
205
  * @returns {string} The SHA-256 hash as a base58 string.
170
206
  */
171
- hashbase58(canonicalized) {
172
- return this.encode(this.hash(canonicalized), 'base58', false);
207
+ static andHashToBase58(canonicalized) {
208
+ return this.encode(this.toHash(canonicalized), 'base58');
173
209
  }
174
210
  }
175
211
  //# sourceMappingURL=canonicalization.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"canonicalization.js","sourceRoot":"","sources":["../../src/canonicalization.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAC;AAC5C,OAAO,EAAE,UAAU,EAAE,MAAM,qBAAqB,CAAC;AACjD,OAAO,EAAE,YAAY,IAAI,IAAI,EAAE,MAAM,mBAAmB,CAAC;AACzD,OAAO,EAAE,SAAS,EAAE,MAAM,2BAA2B,CAAC;AACtD,OAAO,EAAE,qBAAqB,EAAE,MAAM,aAAa,CAAC;AAGpD;;;;;;GAMG;AACH,MAAM,OAAO,gBAAgB;IACV,iBAAiB,CAA4B;IAE9D;;;OAGG;IACH,YAAY,YAAuC,KAAK;QACtD,IAAI,CAAC,iBAAiB,GAAG,gBAAgB,CAAC,kBAAkB,CAAC,SAAS,CAAC,CAAC;IAC1E,CAAC;IAED;;;OAGG;IACH,IAAI,SAAS;QACX,OAAO,IAAI,CAAC,iBAAiB,CAAC;IAChC,CAAC;IAED;;;;;OAKG;IACH,MAAM,CAAC,kBAAkB,CAAC,SAAoC;QAC5D,MAAM,UAAU,GAAG,SAAS,CAAC,WAAW,EAA+B,CAAC;QACxE,IAAI,UAAU,KAAK,KAAK,EAAE,CAAC;YACzB,MAAM,IAAI,qBAAqB,CAAC,0BAA0B,SAAS,EAAE,EAAE,iBAAiB,CAAC,CAAC;QAC5F,CAAC;QACD,OAAO,UAAU,CAAC;IACpB,CAAC;IAED;;;;;OAKG;IACH,MAAM,CAAC,iBAAiB,CAAC,QAAkC;QACzD,MAAM,UAAU,GAAG,QAAQ,CAAC,WAAW,EAA8B,CAAC;QACtE,IAAI,UAAU,KAAK,KAAK,IAAI,UAAU,KAAK,QAAQ,EAAE,CAAC;YACpD,MAAM,IAAI,qBAAqB,CAAC,yBAAyB,QAAQ,EAAE,EAAE,g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,MAAM,CAAC,UAAU,CAAC,MAAc;QAC9B,OAAO,SAAS,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;IAClC,CAAC;IAED;;;;;OAKG;IACH,MAAM,CAAC,OAAO,CACZ,MAAwB,EACxB,YAAuC,KAAK;QAE5C,uBAAuB;QACvB,MAAM,aAAa,GAAG,IAAI,CAAC,YAAY,CAAC,MAAM,EAAE,SAAS,CAAC,CAAC;QAC3D,eAAe;QACf,MAAM,MAAM,GAAG,IAAI,CAAC,MAAM,CAAC,aAAa,CAAC,CAAC;QAC1C,kCAAkC;QAClC,OAAO,MAAM,CAAC;IAChB,CAAC;IAED;;;;;OAKG;IACH,MAAM,CAAC,YAAY,CAAC,aAAqB;QACvC,eAAe;QACf,MAAM,MAAM,GAAG,IAAI,CAAC,MAAM,CAAC,aAAa,CAAC,CAAC;QAC1C,uBAAuB;QACvB,MAAM,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC;QACjC,mCAAmC;QACnC,OAAO,KAAK,CAAC;IACf,CAAC;IAED;;;;;OAKG;IACH,MAAM,CAAC,eAAe,CAAC,aAAqB;QAC1C,OAAO,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,aAAa,CAAC,EAAE,QAAQ,CAAC,CAAC;IAC3D,CAAC;CACF"}
@@ -1,5 +1,5 @@
1
1
  import { sha256 } from '@noble/hashes/sha2';
2
- import { bytesToHex } from '@noble/hashes/utils';
2
+ import { bytesToHex, hexToBytes } from '@noble/hashes/utils';
3
3
  import { canonicalize as jcsa } from 'json-canonicalize';
4
4
  import { base58btc } from 'multiformats/bases/base58';
5
5
  import { CanonicalizationError } from './errors.js';
@@ -11,21 +11,6 @@ import { CanonicalizationError } from './errors.js';
11
11
  * @type {Canonicalization}
12
12
  */
13
13
  export class Canonicalization {
14
- _defaultAlgorithm;
15
- /**
16
- * Initializes the Canonicalization class with the specified algorithm.
17
- * @param {CanonicalizationAlgorithm} algorithm The canonicalization algorithm to use ('jcs').
18
- */
19
- constructor(algorithm = 'jcs') {
20
- this._defaultAlgorithm = Canonicalization.normalizeAlgorithm(algorithm);
21
- }
22
- /**
23
- * Gets the canonicalization algorithm.
24
- * @returns {CanonicalizationAlgorithm} The current canonicalization algorithm.
25
- */
26
- get algorithm() {
27
- return this._defaultAlgorithm;
28
- }
29
14
  /**
30
15
  * Normalizes the canonicalization algorithm.
31
16
  * @param {CanonicalizationAlgorithm} algorithm
@@ -67,15 +52,17 @@ export class Canonicalization {
67
52
  * @param {CanonicalizationAlgorithm} [options.algorithm] The canonicalization algorithm to use.
68
53
  * @returns {string} The final SHA-256 hash bytes as a hex string.
69
54
  */
70
- process(object, options = {}) {
71
- const algorithm = Canonicalization.normalizeAlgorithm(options.algorithm ?? this._defaultAlgorithm);
72
- const encoding = Canonicalization.normalizeEncoding(options.encoding ?? 'hex');
55
+ static process(object, options) {
56
+ // Normalize the algorithm
57
+ const algorithm = Canonicalization.normalizeAlgorithm(options?.algorithm ?? 'jcs');
58
+ // Normalize the encoding
59
+ const encoding = Canonicalization.normalizeEncoding(options?.encoding ?? 'hex');
73
60
  // Step 1: Canonicalize
74
61
  const canonicalized = this.canonicalize(object, algorithm);
75
62
  // Step 2: Hash
76
- const hashed = this.hash(canonicalized);
63
+ const hashed = this.toHash(canonicalized);
77
64
  // Step 3: Encode
78
- const encoded = this.encode(hashed, encoding, options.multibase ?? false);
65
+ const encoded = this.encode(hashed, encoding);
79
66
  // Return the encoded string
80
67
  return encoded;
81
68
  }
@@ -85,7 +72,7 @@ export class Canonicalization {
85
72
  * @param {CanonicalizationAlgorithm} [algorithm] The algorithm to use.
86
73
  * @returns {string} The canonicalized object.
87
74
  */
88
- canonicalize(object, algorithm = this._defaultAlgorithm) {
75
+ static canonicalize(object, algorithm = 'jcs') {
89
76
  switch (Canonicalization.normalizeAlgorithm(algorithm)) {
90
77
  case 'jcs':
91
78
  return this.jcs(object);
@@ -98,7 +85,7 @@ export class Canonicalization {
98
85
  * @param {Record<any, any>} object The object to canonicalize.
99
86
  * @returns {string} The canonicalized object.
100
87
  */
101
- jcs(object) {
88
+ static jcs(object) {
102
89
  return jcsa(object);
103
90
  }
104
91
  /**
@@ -106,7 +93,7 @@ export class Canonicalization {
106
93
  * @param {string} canonicalized The canonicalized object.
107
94
  * @returns {HashBytes} The SHA-256 HashBytes (Uint8Array).
108
95
  */
109
- hash(canonicalized) {
96
+ static toHash(canonicalized) {
110
97
  return sha256(canonicalized);
111
98
  }
112
99
  /**
@@ -116,32 +103,72 @@ export class Canonicalization {
116
103
  * @throws {CanonicalizationError} If the encoding format is not supported.
117
104
  * @returns {string} The encoded string.
118
105
  */
119
- encode(canonicalizedhash, encoding = 'hex', multibase = false) {
106
+ static encode(canonicalizedhash, encoding = 'hex') {
107
+ // Normalize encoding
120
108
  const normalized = Canonicalization.normalizeEncoding(encoding);
121
- if (normalized === 'hex')
122
- return this.hex(canonicalizedhash);
109
+ // If encoding is hex, encode to hex
110
+ if (normalized === 'hex') {
111
+ return this.toHex(canonicalizedhash);
112
+ }
113
+ // If encoding is base58, encode to base58
123
114
  if (normalized === 'base58') {
124
- const encoded = this.base58(canonicalizedhash);
125
- return multibase ? `z${encoded}` : encoded;
115
+ return this.toBase58(canonicalizedhash);
126
116
  }
117
+ // Throw error if encoding is unsupported
127
118
  throw new CanonicalizationError(`Unsupported encoding: ${encoding}`, 'ENCODING_ERROR');
128
119
  }
120
+ /**
121
+ * Decodes SHA-256 hashed, canonicalized object as a hex or base58 string.
122
+ * @param {string} canonicalizedhash The canonicalized object to encode.
123
+ * @param {CanonicalizationEncoding} encoding The encoding format ('hex' or 'base58').
124
+ * @throws {CanonicalizationError} If the encoding format is not supported.
125
+ * @returns {string} The encoded string.
126
+ */
127
+ static decode(canonicalizedhash, encoding = 'hex') {
128
+ // Normalize encoding
129
+ const normalized = Canonicalization.normalizeEncoding(encoding);
130
+ // If encoding is hex, decode from hex
131
+ if (normalized === 'hex') {
132
+ return this.fromHex(canonicalizedhash);
133
+ }
134
+ // If encoding is base58, decode from base58
135
+ if (normalized === 'base58') {
136
+ return this.fromBase58(canonicalizedhash);
137
+ }
138
+ // Throw error if encoding is unsupported
139
+ throw new CanonicalizationError(`Unsupported encoding: ${encoding}`, 'DECODING_ERROR');
140
+ }
129
141
  /**
130
142
  * Step 3.1: Encodes HashBytes (Uint8Array) to a hex string.
131
143
  * @param {HashBytes} hashBytes The hash as a Uint8Array.
132
144
  * @returns {string} The hash as a hex string.
133
145
  */
134
- hex(hashBytes) {
146
+ static toHex(hashBytes) {
135
147
  return bytesToHex(hashBytes);
136
148
  }
149
+ /**
150
+ * Decodes a hex string to HashBytes (Uint8Array).
151
+ * @param {HexString} hexString The hash as a hex string.
152
+ * @returns {HashBytes} The hash bytes.
153
+ */
154
+ static fromHex(hexString) {
155
+ return hexToBytes(hexString);
156
+ }
137
157
  /**
138
158
  * Step 3.2: Encodes HashBytes (Uint8Array) to a base58btc string.
139
159
  * @param {HashBytes} hashBytes The hash as a Uint8Array.
140
160
  * @returns {string} The hash as a hex string.
141
161
  */
142
- base58(hashBytes) {
143
- const encoded = base58btc.encode(hashBytes);
144
- return encoded.startsWith('z') ? encoded.slice(1) : encoded;
162
+ static toBase58(hashBytes) {
163
+ return base58btc.encode(hashBytes);
164
+ }
165
+ /**
166
+ * Decodes a base58 string to HashBytes (Uint8Array).
167
+ * @param {string} b58str The hash as a base58 string.
168
+ * @returns {HashBytes} The hash bytes.
169
+ */
170
+ static fromBase58(b58str) {
171
+ return base58btc.decode(b58str);
145
172
  }
146
173
  /**
147
174
  * Canonicalizes an object, hashes it and returns it as hash bytes.
@@ -149,9 +176,13 @@ export class Canonicalization {
149
176
  * @param {Record<any, any>} object The object to process.
150
177
  * @returns {Promise<HashBytes>} The final SHA-256 hash bytes.
151
178
  */
152
- canonicalhash(object, algorithm = this._defaultAlgorithm) {
179
+ static andHash(object, algorithm = 'jcs') {
180
+ // Step 1: Canonicalize
153
181
  const canonicalized = this.canonicalize(object, algorithm);
154
- return this.hash(canonicalized);
182
+ // Step 2: Hash
183
+ const hashed = this.toHash(canonicalized);
184
+ // Return canonicalized hash bytes
185
+ return hashed;
155
186
  }
156
187
  /**
157
188
  * Computes the SHA-256 hash of a canonicalized object and encodes it as a hex string.
@@ -159,8 +190,13 @@ export class Canonicalization {
159
190
  * @param {string} canonicalized The canonicalized object to hash.
160
191
  * @returns {string} The SHA-256 hash as a hex string.
161
192
  */
162
- hashhex(canonicalized) {
163
- return this.encode(this.hash(canonicalized), 'hex');
193
+ static andHashToHex(canonicalized) {
194
+ // Step 2: Hash
195
+ const hashed = this.toHash(canonicalized);
196
+ // Step 3: Encode (Hex)
197
+ const hexed = this.toHex(hashed);
198
+ // Return the hashed encoded string
199
+ return hexed;
164
200
  }
165
201
  /**
166
202
  * Computes the SHA-256 hashes of canonicalized object and encodes it as a base58 string.
@@ -168,8 +204,8 @@ export class Canonicalization {
168
204
  * @param {string} canonicalized The canonicalized object to hash.
169
205
  * @returns {string} The SHA-256 hash as a base58 string.
170
206
  */
171
- hashbase58(canonicalized) {
172
- return this.encode(this.hash(canonicalized), 'base58', false);
207
+ static andHashToBase58(canonicalized) {
208
+ return this.encode(this.toHash(canonicalized), 'base58');
173
209
  }
174
210
  }
175
211
  //# sourceMappingURL=canonicalization.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"canonicalization.js","sourceRoot":"","sources":["../../src/canonicalization.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAC;AAC5C,OAAO,EAAE,UAAU,EAAE,MAAM,qBAAqB,CAAC;AACjD,OAAO,EAAE,YAAY,IAAI,IAAI,EAAE,MAAM,mBAAmB,CAAC;AACzD,OAAO,EAAE,SAAS,EAAE,MAAM,2BAA2B,CAAC;AACtD,OAAO,EAAE,qBAAqB,EAAE,MAAM,aAAa,CAAC;AAGpD;;;;;;GAMG;AACH,MAAM,OAAO,gBAAgB;IACV,iBAAiB,CAA4B;IAE9D;;;OAGG;IACH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C;QAC1C,uBAAuB;QACvB,MAAM,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC;QACjC,mCAAmC;QACnC,OAAO,KAAK,CAAC;IACf,CAAC;IAED;;;;;OAKG;IACH,MAAM,CAAC,eAAe,CAAC,aAAqB;QAC1C,OAAO,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,aAAa,CAAC,EAAE,QAAQ,CAAC,CAAC;IAC3D,CAAC;CACF"}
@@ -1,4 +1,8 @@
1
- import { CanonicalizationAlgorithm, CanonicalizationEncoding, HashBytes } from './types.js';
1
+ import { CanonicalizationAlgorithm, CanonicalizationEncoding, HashBytes, HexString } from './types.js';
2
+ export interface CanonicalizationOptions {
3
+ algorithm?: CanonicalizationAlgorithm;
4
+ encoding?: CanonicalizationEncoding;
5
+ }
2
6
  /**
3
7
  * Canonicalization class provides methods for canonicalizing JSON objects
4
8
  * and hashing them using SHA-256. It supports different canonicalization
@@ -7,17 +11,6 @@ import { CanonicalizationAlgorithm, CanonicalizationEncoding, HashBytes } from '
7
11
  * @type {Canonicalization}
8
12
  */
9
13
  export declare class Canonicalization {
10
- private readonly _defaultAlgorithm;
11
- /**
12
- * Initializes the Canonicalization class with the specified algorithm.
13
- * @param {CanonicalizationAlgorithm} algorithm The canonicalization algorithm to use ('jcs').
14
- */
15
- constructor(algorithm?: CanonicalizationAlgorithm);
16
- /**
17
- * Gets the canonicalization algorithm.
18
- * @returns {CanonicalizationAlgorithm} The current canonicalization algorithm.
19
- */
20
- get algorithm(): CanonicalizationAlgorithm;
21
14
  /**
22
15
  * Normalizes the canonicalization algorithm.
23
16
  * @param {CanonicalizationAlgorithm} algorithm
@@ -47,30 +40,26 @@ export declare class Canonicalization {
47
40
  * @param {CanonicalizationAlgorithm} [options.algorithm] The canonicalization algorithm to use.
48
41
  * @returns {string} The final SHA-256 hash bytes as a hex string.
49
42
  */
50
- process(object: Record<any, any>, options?: {
51
- encoding?: CanonicalizationEncoding;
52
- algorithm?: CanonicalizationAlgorithm;
53
- multibase?: boolean;
54
- }): string;
43
+ static process(object: Record<any, any>, options?: CanonicalizationOptions): string;
55
44
  /**
56
45
  * Step 1: Uses this.algorithm to determine the method (JCS).
57
46
  * @param {Record<any, any>} object The object to canonicalize.
58
47
  * @param {CanonicalizationAlgorithm} [algorithm] The algorithm to use.
59
48
  * @returns {string} The canonicalized object.
60
49
  */
61
- canonicalize(object: Record<any, any>, algorithm?: CanonicalizationAlgorithm): string;
50
+ static canonicalize(object: Record<any, any>, algorithm?: CanonicalizationAlgorithm): string;
62
51
  /**
63
52
  * Step 1: Canonicalizes an object using JCS (JSON Canonicalization Scheme).
64
53
  * @param {Record<any, any>} object The object to canonicalize.
65
54
  * @returns {string} The canonicalized object.
66
55
  */
67
- jcs(object: Record<any, any>): string;
56
+ static jcs(object: Record<any, any>): string;
68
57
  /**
69
58
  * Step 2: SHA-256 hashes a canonicalized object.
70
59
  * @param {string} canonicalized The canonicalized object.
71
60
  * @returns {HashBytes} The SHA-256 HashBytes (Uint8Array).
72
61
  */
73
- hash(canonicalized: string): HashBytes;
62
+ static toHash(canonicalized: string): HashBytes;
74
63
  /**
75
64
  * Step 3: Encodes SHA-256 hashed, canonicalized object as a hex or base58 string.
76
65
  * @param {string} canonicalizedhash The canonicalized object to encode.
@@ -78,38 +67,58 @@ export declare class Canonicalization {
78
67
  * @throws {CanonicalizationError} If the encoding format is not supported.
79
68
  * @returns {string} The encoded string.
80
69
  */
81
- encode(canonicalizedhash: HashBytes, encoding?: CanonicalizationEncoding, multibase?: boolean): string;
70
+ static encode(canonicalizedhash: HashBytes, encoding?: CanonicalizationEncoding): string;
71
+ /**
72
+ * Decodes SHA-256 hashed, canonicalized object as a hex or base58 string.
73
+ * @param {string} canonicalizedhash The canonicalized object to encode.
74
+ * @param {CanonicalizationEncoding} encoding The encoding format ('hex' or 'base58').
75
+ * @throws {CanonicalizationError} If the encoding format is not supported.
76
+ * @returns {string} The encoded string.
77
+ */
78
+ static decode(canonicalizedhash: string, encoding?: CanonicalizationEncoding): HashBytes;
82
79
  /**
83
80
  * Step 3.1: Encodes HashBytes (Uint8Array) to a hex string.
84
81
  * @param {HashBytes} hashBytes The hash as a Uint8Array.
85
82
  * @returns {string} The hash as a hex string.
86
83
  */
87
- hex(hashBytes: HashBytes): string;
84
+ static toHex(hashBytes: HashBytes): string;
85
+ /**
86
+ * Decodes a hex string to HashBytes (Uint8Array).
87
+ * @param {HexString} hexString The hash as a hex string.
88
+ * @returns {HashBytes} The hash bytes.
89
+ */
90
+ static fromHex(hexString: HexString): HashBytes;
88
91
  /**
89
92
  * Step 3.2: Encodes HashBytes (Uint8Array) to a base58btc string.
90
93
  * @param {HashBytes} hashBytes The hash as a Uint8Array.
91
94
  * @returns {string} The hash as a hex string.
92
95
  */
93
- base58(hashBytes: HashBytes): string;
96
+ static toBase58(hashBytes: HashBytes): string;
97
+ /**
98
+ * Decodes a base58 string to HashBytes (Uint8Array).
99
+ * @param {string} b58str The hash as a base58 string.
100
+ * @returns {HashBytes} The hash bytes.
101
+ */
102
+ static fromBase58(b58str: string): HashBytes;
94
103
  /**
95
104
  * Canonicalizes an object, hashes it and returns it as hash bytes.
96
105
  * Step 1-2: Canonicalize → Hash.
97
106
  * @param {Record<any, any>} object The object to process.
98
107
  * @returns {Promise<HashBytes>} The final SHA-256 hash bytes.
99
108
  */
100
- canonicalhash(object: Record<any, any>, algorithm?: CanonicalizationAlgorithm): HashBytes;
109
+ static andHash(object: Record<any, any>, algorithm?: CanonicalizationAlgorithm): HashBytes;
101
110
  /**
102
111
  * Computes the SHA-256 hash of a canonicalized object and encodes it as a hex string.
103
112
  * Step 2-3: Hash → Encode(Hex).
104
113
  * @param {string} canonicalized The canonicalized object to hash.
105
114
  * @returns {string} The SHA-256 hash as a hex string.
106
115
  */
107
- hashhex(canonicalized: string): string;
116
+ static andHashToHex(canonicalized: string): string;
108
117
  /**
109
118
  * Computes the SHA-256 hashes of canonicalized object and encodes it as a base58 string.
110
119
  * Step 2-3: Hash → Encode(base58).
111
120
  * @param {string} canonicalized The canonicalized object to hash.
112
121
  * @returns {string} The SHA-256 hash as a base58 string.
113
122
  */
114
- hashbase58(canonicalized: string): string;
123
+ static andHashToBase58(canonicalized: string): string;
115
124
  }
@@ -1 +1 @@
1
- {"version":3,"file":"canonicalization.d.ts","sourceRoot":"","sources":["../../src/canonicalization.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,yBAAyB,EAAE,wBAAwB,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AAE5F;;;;;;GAMG;AACH,qBAAa,gBAAgB;IAC3B,OAAO,CAAC,QAAQ,CAAC,iBAAiB,CAA4B;IAE9D;;;OAGG;gBACS,SAAS,GAAE,yBAAiC;IAIxD;;;OAGG;IACH,IAAI,SAAS,IAAI,yBAAyB,CAEzC;IAED;;;;;OAKG;IACH,MAAM,CAAC,kBAAkB,CAAC,SAAS,EAAE,yBAAyB,GAAG,yBAAyB;IAQ1F;;;;;OAKG;IACH,MAAM,CAAC,iBAAiB,CAAC,QAAQ,EAAE,wBAAwB,GAAG,wBAAwB;IAQtF;;;;;;;;;;;;;;OAcG;IACH,OAAO,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,EAAE,GAAG,CAAC,EAAE,OAAO,GAAE;QACzC,QAAQ,CAAC,EAAE,wBAAwB,CAAC;QACpC,SAAS,CAAC,EAAE,yBAAyB,CAAC;QACtC,SAAS,CAAC,EAAE,OAAO,CAAC;KAChB,GAAG,MAAM;IAcf;;;;;OAKG;IACH,YAAY,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,EAAE,GAAG,CAAC,EAAE,SAAS,GAAE,yBAAkD,GAAG,MAAM;IAS7G;;;;OAIG;IACH,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,EAAE,GAAG,CAAC,GAAG,MAAM;IAIrC;;;;OAIG;IACH,IAAI,CAAC,aAAa,EAAE,MAAM,GAAG,SAAS;IAItC;;;;;;OAMG;IACH,M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yBAAiC,GAAG,MAAM;IASnG;;;;OAIG;IACH,MAAM,CAAC,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,EAAE,GAAG,CAAC,GAAG,MAAM;IAI5C;;;;OAIG;IACH,MAAM,CAAC,MAAM,CAAC,aAAa,EAAE,MAAM,GAAG,SAAS;IAI/C;;;;;;OAMG;IACH,MAAM,CAAC,MAAM,CAAC,iBAAiB,EAAE,SAAS,EAAE,QAAQ,GAAE,wBAAgC,GAAG,MAAM;IAkB/F;;;;;;OAMG;IACH,MAAM,CAAC,MAAM,CAAC,iBAAiB,EAAE,MAAM,EAAE,QAAQ,GAAE,wBAAgC,GAAG,SAAS;IAkB/F;;;;OAIG;IACH,MAAM,CAAC,KAAK,CAAC,SAAS,EAAE,SAAS,GAAG,MAAM;IAI1C;;;;OAIG;IACH,MAAM,CAAC,OAAO,CAAC,SAAS,EAAE,SAAS,GAAG,SAAS;IAI/C;;;;OAIG;IACH,MAAM,CAAC,QAAQ,CAAC,SAAS,EAAE,SAAS,GAAG,MAAM;IAI7C;;;;OAIG;IACH,MAAM,CAAC,UAAU,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS;IAI5C;;;;;OAKG;IACH,MAAM,CAAC,OAAO,CACZ,MAAM,EAAE,MAAM,CAAC,GAAG,EAAE,GAAG,CAAC,EACxB,SAAS,GAAE,yBAAiC,GAC3C,SAAS;IASZ;;;;;OAKG;IACH,MAAM,CAAC,YAAY,CAAC,aAAa,EAAE,MAAM,GAAG,MAAM;IASlD;;;;;OAKG;IACH,MAAM,CAAC,eAAe,CAAC,aAAa,EAAE,MAAM,GAAG,MAAM;CAGtD"}
@@ -21,13 +21,13 @@ export type Point = {
21
21
  };
22
22
  export type PublicKeyObject = {
23
23
  point: Point;
24
- hex: Hex;
24
+ hex: HexString;
25
25
  multibase: MultibaseObject;
26
26
  };
27
27
  export type SecretKeyObject = {
28
28
  bytes: Array<number>;
29
29
  seed?: string;
30
- hex?: Hex;
30
+ hex?: HexString;
31
31
  };
32
32
  export type SchnorrKeyPair = {
33
33
  secretKey: KeyBytes;
@@ -65,7 +65,6 @@ export declare enum BitcoinNetworkNames {
65
65
  export type DecentralizedIdentifier = string;
66
66
  export type Did = DecentralizedIdentifier;
67
67
  export type BeaconUri = string;
68
- export type DidPlaceholder = 'did:btcr2:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
69
68
  export type CanonicalizedProofConfig = string;
70
69
  export type CryptosuiteName = 'bip340-jcs-2025' | 'bip340-rdfc-2025';
71
70
  export type JsonPrimitive = string | number | boolean | null;
@@ -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,SAAS,GAAG,MAAM,CAAC;AAC/B,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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G,SAAS,CAAC;AAC5B,MAAM,MAAM,KAAK,GAAG,SAAS,CAAC;AAC9B,MAAM,MAAM,OAAO,GAAG,SAAS,CAAC;AAChC,MAAM,MAAM,OAAO,GAAG,SAAS,CAAC;AAChC,MAAM,MAAM,YAAY,GAAG,GAAG,IAAI,IAAI,KAAK,IAAI,GAAG,IAAI,KAAK,IAAI,OAAO,IAAI,OAAO,EAAE,CAAC;AACpF,MAAM,MAAM,QAAQ,GAAG,GAAG,KAAK,IAAI,OAAO,EAAE,CAAC;AAC7C,MAAM,MAAM,aAAa,GAAG,GAAG,YAAY,GAAG,GAAG,GAAG,YAAY,IAAI,QAAQ,EAAE,CAAC;AAC/E,MAAM,MAAM,qBAAqB,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;AACxD,MAAM,MAAM,yBAAyB,GAAG,KAAK,GAAG,MAAM,CAAC;AACvD,MAAM,MAAM,wBAAwB,GAAG,KAAK,GAAG,QAAQ,CAAC;AACxD,MAAM,MAAM,aAAa,GAAG,MAAM,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@did-btcr2/common",
3
- "version": "3.1.0",
3
+ "version": "4.0.1",
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",
@@ -1,9 +1,14 @@
1
1
  import { sha256 } from '@noble/hashes/sha2';
2
- import { bytesToHex } from '@noble/hashes/utils';
2
+ import { bytesToHex, hexToBytes } from '@noble/hashes/utils';
3
3
  import { canonicalize as jcsa } from 'json-canonicalize';
4
4
  import { base58btc } from 'multiformats/bases/base58';
5
5
  import { CanonicalizationError } from './errors.js';
6
- import { CanonicalizationAlgorithm, CanonicalizationEncoding, HashBytes } from './types.js';
6
+ import { CanonicalizationAlgorithm, CanonicalizationEncoding, HashBytes, HexString } from './types.js';
7
+
8
+ export interface CanonicalizationOptions {
9
+ algorithm?: CanonicalizationAlgorithm;
10
+ encoding?: CanonicalizationEncoding;
11
+ }
7
12
 
8
13
  /**
9
14
  * Canonicalization class provides methods for canonicalizing JSON objects
@@ -13,24 +18,6 @@ import { CanonicalizationAlgorithm, CanonicalizationEncoding, HashBytes } from '
13
18
  * @type {Canonicalization}
14
19
  */
15
20
  export class Canonicalization {
16
- private readonly _defaultAlgorithm: CanonicalizationAlgorithm;
17
-
18
- /**
19
- * Initializes the Canonicalization class with the specified algorithm.
20
- * @param {CanonicalizationAlgorithm} algorithm The canonicalization algorithm to use ('jcs').
21
- */
22
- constructor(algorithm: CanonicalizationAlgorithm = 'jcs') {
23
- this._defaultAlgorithm = Canonicalization.normalizeAlgorithm(algorithm);
24
- }
25
-
26
- /**
27
- * Gets the canonicalization algorithm.
28
- * @returns {CanonicalizationAlgorithm} The current canonicalization algorithm.
29
- */
30
- get algorithm(): CanonicalizationAlgorithm {
31
- return this._defaultAlgorithm;
32
- }
33
-
34
21
  /**
35
22
  * Normalizes the canonicalization algorithm.
36
23
  * @param {CanonicalizationAlgorithm} algorithm
@@ -74,20 +61,18 @@ export class Canonicalization {
74
61
  * @param {CanonicalizationAlgorithm} [options.algorithm] The canonicalization algorithm to use.
75
62
  * @returns {string} The final SHA-256 hash bytes as a hex string.
76
63
  */
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');
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');
84
69
 
85
70
  // Step 1: Canonicalize
86
71
  const canonicalized = this.canonicalize(object, algorithm);
87
72
  // Step 2: Hash
88
- const hashed = this.hash(canonicalized);
73
+ const hashed = this.toHash(canonicalized);
89
74
  // Step 3: Encode
90
- const encoded = this.encode(hashed, encoding, options.multibase ?? false);
75
+ const encoded = this.encode(hashed, encoding);
91
76
  // Return the encoded string
92
77
  return encoded;
93
78
  }
@@ -98,7 +83,7 @@ export class Canonicalization {
98
83
  * @param {CanonicalizationAlgorithm} [algorithm] The algorithm to use.
99
84
  * @returns {string} The canonicalized object.
100
85
  */
101
- canonicalize(object: Record<any, any>, algorithm: CanonicalizationAlgorithm = this._defaultAlgorithm): string {
86
+ static canonicalize(object: Record<any, any>, algorithm: CanonicalizationAlgorithm = 'jcs'): string {
102
87
  switch (Canonicalization.normalizeAlgorithm(algorithm)) {
103
88
  case 'jcs':
104
89
  return this.jcs(object);
@@ -112,7 +97,7 @@ export class Canonicalization {
112
97
  * @param {Record<any, any>} object The object to canonicalize.
113
98
  * @returns {string} The canonicalized object.
114
99
  */
115
- jcs(object: Record<any, any>): string {
100
+ static jcs(object: Record<any, any>): string {
116
101
  return jcsa(object);
117
102
  }
118
103
 
@@ -121,7 +106,7 @@ export class Canonicalization {
121
106
  * @param {string} canonicalized The canonicalized object.
122
107
  * @returns {HashBytes} The SHA-256 HashBytes (Uint8Array).
123
108
  */
124
- hash(canonicalized: string): HashBytes {
109
+ static toHash(canonicalized: string): HashBytes {
125
110
  return sha256(canonicalized);
126
111
  }
127
112
 
@@ -132,33 +117,83 @@ export class Canonicalization {
132
117
  * @throws {CanonicalizationError} If the encoding format is not supported.
133
118
  * @returns {string} The encoded string.
134
119
  */
135
- encode(canonicalizedhash: HashBytes, encoding: CanonicalizationEncoding = 'hex', multibase: boolean = false): string {
120
+ static encode(canonicalizedhash: HashBytes, encoding: CanonicalizationEncoding = 'hex'): string {
121
+ // Normalize encoding
136
122
  const normalized = Canonicalization.normalizeEncoding(encoding);
137
- if (normalized === 'hex') return this.hex(canonicalizedhash);
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
138
130
  if (normalized === 'base58') {
139
- const encoded = this.base58(canonicalizedhash);
140
- return multibase ? `z${encoded}` : encoded;
131
+ return this.toBase58(canonicalizedhash);
141
132
  }
133
+
134
+ // Throw error if encoding is unsupported
142
135
  throw new CanonicalizationError(`Unsupported encoding: ${encoding}`, 'ENCODING_ERROR');
143
136
  }
144
137
 
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);
157
+ }
158
+
159
+ // Throw error if encoding is unsupported
160
+ throw new CanonicalizationError(`Unsupported encoding: ${encoding}`, 'DECODING_ERROR');
161
+ }
162
+
145
163
  /**
146
164
  * Step 3.1: Encodes HashBytes (Uint8Array) to a hex string.
147
165
  * @param {HashBytes} hashBytes The hash as a Uint8Array.
148
166
  * @returns {string} The hash as a hex string.
149
167
  */
150
- hex(hashBytes: HashBytes): string {
168
+ static toHex(hashBytes: HashBytes): string {
151
169
  return bytesToHex(hashBytes);
152
170
  }
153
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
+
154
181
  /**
155
182
  * Step 3.2: Encodes HashBytes (Uint8Array) to a base58btc string.
156
183
  * @param {HashBytes} hashBytes The hash as a Uint8Array.
157
184
  * @returns {string} The hash as a hex string.
158
185
  */
159
- base58(hashBytes: HashBytes): string {
160
- const encoded = base58btc.encode(hashBytes);
161
- return encoded.startsWith('z') ? encoded.slice(1) : encoded;
186
+ static toBase58(hashBytes: HashBytes): string {
187
+ return base58btc.encode(hashBytes);
188
+ }
189
+
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);
162
197
  }
163
198
 
164
199
  /**
@@ -167,12 +202,16 @@ export class Canonicalization {
167
202
  * @param {Record<any, any>} object The object to process.
168
203
  * @returns {Promise<HashBytes>} The final SHA-256 hash bytes.
169
204
  */
170
- canonicalhash(
205
+ static andHash(
171
206
  object: Record<any, any>,
172
- algorithm: CanonicalizationAlgorithm = this._defaultAlgorithm
207
+ algorithm: CanonicalizationAlgorithm = 'jcs'
173
208
  ): HashBytes {
209
+ // Step 1: Canonicalize
174
210
  const canonicalized = this.canonicalize(object, algorithm);
175
- return this.hash(canonicalized);
211
+ // Step 2: Hash
212
+ const hashed = this.toHash(canonicalized);
213
+ // Return canonicalized hash bytes
214
+ return hashed;
176
215
  }
177
216
 
178
217
  /**
@@ -181,8 +220,13 @@ export class Canonicalization {
181
220
  * @param {string} canonicalized The canonicalized object to hash.
182
221
  * @returns {string} The SHA-256 hash as a hex string.
183
222
  */
184
- hashhex(canonicalized: string): string {
185
- return this.encode(this.hash(canonicalized), 'hex');
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;
186
230
  }
187
231
 
188
232
  /**
@@ -191,7 +235,7 @@ export class Canonicalization {
191
235
  * @param {string} canonicalized The canonicalized object to hash.
192
236
  * @returns {string} The SHA-256 hash as a base58 string.
193
237
  */
194
- hashbase58(canonicalized: string): string {
195
- return this.encode(this.hash(canonicalized), 'base58', false);
238
+ static andHashToBase58(canonicalized: string): string {
239
+ return this.encode(this.toHash(canonicalized), 'base58');
196
240
  }
197
241
  }
package/src/types.ts CHANGED
@@ -26,13 +26,13 @@ export type Point = {
26
26
  }
27
27
  export type PublicKeyObject = {
28
28
  point: Point;
29
- hex: Hex;
29
+ hex: HexString;
30
30
  multibase: MultibaseObject;
31
31
  };
32
32
  export type SecretKeyObject = {
33
33
  bytes: Array<number>;
34
34
  seed?: string;
35
- hex?: Hex;
35
+ hex?: HexString;
36
36
  };
37
37
  export type SchnorrKeyPair = {
38
38
  secretKey: KeyBytes;
@@ -70,7 +70,6 @@ export enum BitcoinNetworkNames {
70
70
  export type DecentralizedIdentifier = string;
71
71
  export type Did = DecentralizedIdentifier;
72
72
  export type BeaconUri = string;
73
- export type DidPlaceholder = 'did:btcr2:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
74
73
  export type CanonicalizedProofConfig = string;
75
74
  export type CryptosuiteName = 'bip340-jcs-2025' | 'bip340-rdfc-2025';
76
75
  export type JsonPrimitive = string | number | boolean | null;