@matter/general 0.16.0-alpha.0-20250814-484abe647 → 0.16.0-alpha.0-20250815-ac9fd6eb0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (219) hide show
  1. package/dist/cjs/codec/DerCodec.d.ts +15 -14
  2. package/dist/cjs/codec/DerCodec.d.ts.map +1 -1
  3. package/dist/cjs/codec/DerCodec.js +19 -25
  4. package/dist/cjs/codec/DerCodec.js.map +1 -1
  5. package/dist/cjs/codec/DerTypes.d.ts +9 -8
  6. package/dist/cjs/codec/DerTypes.d.ts.map +1 -1
  7. package/dist/cjs/codec/DerTypes.js.map +1 -1
  8. package/dist/cjs/codec/DnsCodec.d.ts +16 -16
  9. package/dist/cjs/codec/DnsCodec.d.ts.map +1 -1
  10. package/dist/cjs/codec/DnsCodec.js +7 -7
  11. package/dist/cjs/codec/DnsCodec.js.map +1 -1
  12. package/dist/cjs/crypto/Crypto.d.ts +11 -10
  13. package/dist/cjs/crypto/Crypto.d.ts.map +1 -1
  14. package/dist/cjs/crypto/Crypto.js +4 -4
  15. package/dist/cjs/crypto/Crypto.js.map +1 -1
  16. package/dist/cjs/crypto/Key.d.ts +22 -21
  17. package/dist/cjs/crypto/Key.d.ts.map +1 -1
  18. package/dist/cjs/crypto/Key.js +17 -16
  19. package/dist/cjs/crypto/Key.js.map +1 -1
  20. package/dist/cjs/crypto/MockCrypto.d.ts.map +1 -1
  21. package/dist/cjs/crypto/MockCrypto.js +2 -1
  22. package/dist/cjs/crypto/MockCrypto.js.map +1 -1
  23. package/dist/cjs/crypto/Spake2p.d.ts +12 -11
  24. package/dist/cjs/crypto/Spake2p.d.ts.map +1 -1
  25. package/dist/cjs/crypto/Spake2p.js +15 -11
  26. package/dist/cjs/crypto/Spake2p.js.map +1 -1
  27. package/dist/cjs/crypto/StandardCrypto.d.ts +12 -11
  28. package/dist/cjs/crypto/StandardCrypto.d.ts.map +1 -1
  29. package/dist/cjs/crypto/StandardCrypto.js +27 -17
  30. package/dist/cjs/crypto/StandardCrypto.js.map +1 -1
  31. package/dist/cjs/crypto/aes/Aes.d.ts +2 -1
  32. package/dist/cjs/crypto/aes/Aes.d.ts.map +1 -1
  33. package/dist/cjs/crypto/aes/Aes.js +3 -1
  34. package/dist/cjs/crypto/aes/Aes.js.map +1 -1
  35. package/dist/cjs/crypto/aes/Ccm.d.ts +4 -3
  36. package/dist/cjs/crypto/aes/Ccm.d.ts.map +1 -1
  37. package/dist/cjs/crypto/aes/Ccm.js.map +1 -1
  38. package/dist/cjs/log/Diagnostic.js +1 -1
  39. package/dist/cjs/log/Diagnostic.js.map +1 -1
  40. package/dist/cjs/log/LogFormat.js +1 -1
  41. package/dist/cjs/log/LogFormat.js.map +1 -1
  42. package/dist/cjs/log/Logger.js +1 -1
  43. package/dist/cjs/log/Logger.js.map +1 -1
  44. package/dist/cjs/net/NetInterface.d.ts +2 -1
  45. package/dist/cjs/net/NetInterface.d.ts.map +1 -1
  46. package/dist/cjs/net/NetInterface.js.map +1 -1
  47. package/dist/cjs/net/TransportInterface.d.ts +2 -1
  48. package/dist/cjs/net/TransportInterface.d.ts.map +1 -1
  49. package/dist/cjs/net/TransportInterface.js.map +1 -1
  50. package/dist/cjs/net/UdpChannel.d.ts +3 -3
  51. package/dist/cjs/net/UdpChannel.d.ts.map +1 -1
  52. package/dist/cjs/net/UdpInterface.d.ts +4 -3
  53. package/dist/cjs/net/UdpInterface.d.ts.map +1 -1
  54. package/dist/cjs/net/UdpInterface.js.map +1 -1
  55. package/dist/cjs/net/UdpMulticastServer.d.ts +3 -2
  56. package/dist/cjs/net/UdpMulticastServer.d.ts.map +1 -1
  57. package/dist/cjs/net/UdpMulticastServer.js.map +1 -1
  58. package/dist/cjs/net/mock/MockRouter.d.ts +1 -1
  59. package/dist/cjs/net/mock/MockRouter.d.ts.map +1 -1
  60. package/dist/cjs/net/mock/MockUdpChannel.d.ts +1 -1
  61. package/dist/cjs/net/mock/MockUdpChannel.d.ts.map +1 -1
  62. package/dist/cjs/net/mock/MockUdpChannel.js.map +1 -1
  63. package/dist/cjs/net/mock/NetworkSimulator.d.ts +1 -1
  64. package/dist/cjs/net/mock/NetworkSimulator.d.ts.map +1 -1
  65. package/dist/cjs/storage/Storage.d.ts +1 -1
  66. package/dist/cjs/storage/Storage.d.ts.map +1 -1
  67. package/dist/cjs/storage/StorageBackendMemory.d.ts +1 -1
  68. package/dist/cjs/storage/StorageBackendMemory.d.ts.map +1 -1
  69. package/dist/cjs/storage/StorageBackendMemory.js +7 -5
  70. package/dist/cjs/storage/StorageBackendMemory.js.map +1 -1
  71. package/dist/cjs/storage/StorageContext.d.ts +1 -1
  72. package/dist/cjs/storage/StorageContext.d.ts.map +1 -1
  73. package/dist/cjs/storage/StorageContext.js.map +1 -1
  74. package/dist/cjs/storage/StringifyTools.d.ts +2 -1
  75. package/dist/cjs/storage/StringifyTools.d.ts.map +1 -1
  76. package/dist/cjs/storage/StringifyTools.js +1 -1
  77. package/dist/cjs/storage/StringifyTools.js.map +1 -1
  78. package/dist/cjs/transaction/Tx.js +5 -1
  79. package/dist/cjs/transaction/Tx.js.map +1 -1
  80. package/dist/cjs/util/Bytes.d.ts +29 -11
  81. package/dist/cjs/util/Bytes.d.ts.map +1 -1
  82. package/dist/cjs/util/Bytes.js +30 -11
  83. package/dist/cjs/util/Bytes.js.map +1 -1
  84. package/dist/cjs/util/DataReader.d.ts +2 -2
  85. package/dist/cjs/util/DataReader.d.ts.map +1 -1
  86. package/dist/cjs/util/DataReader.js +1 -1
  87. package/dist/cjs/util/DataReader.js.map +1 -1
  88. package/dist/cjs/util/DataWriter.d.ts +3 -3
  89. package/dist/cjs/util/DataWriter.d.ts.map +1 -1
  90. package/dist/cjs/util/DataWriter.js +1 -1
  91. package/dist/cjs/util/DataWriter.js.map +1 -1
  92. package/dist/cjs/util/String.d.ts.map +1 -1
  93. package/dist/cjs/util/String.js +3 -8
  94. package/dist/cjs/util/String.js.map +1 -1
  95. package/dist/esm/codec/DerCodec.d.ts +15 -14
  96. package/dist/esm/codec/DerCodec.d.ts.map +1 -1
  97. package/dist/esm/codec/DerCodec.js +19 -25
  98. package/dist/esm/codec/DerCodec.js.map +1 -1
  99. package/dist/esm/codec/DerTypes.d.ts +9 -8
  100. package/dist/esm/codec/DerTypes.d.ts.map +1 -1
  101. package/dist/esm/codec/DerTypes.js.map +1 -1
  102. package/dist/esm/codec/DnsCodec.d.ts +16 -16
  103. package/dist/esm/codec/DnsCodec.d.ts.map +1 -1
  104. package/dist/esm/codec/DnsCodec.js +7 -7
  105. package/dist/esm/codec/DnsCodec.js.map +1 -1
  106. package/dist/esm/crypto/Crypto.d.ts +11 -10
  107. package/dist/esm/crypto/Crypto.d.ts.map +1 -1
  108. package/dist/esm/crypto/Crypto.js +4 -4
  109. package/dist/esm/crypto/Crypto.js.map +1 -1
  110. package/dist/esm/crypto/Key.d.ts +22 -21
  111. package/dist/esm/crypto/Key.d.ts.map +1 -1
  112. package/dist/esm/crypto/Key.js +17 -16
  113. package/dist/esm/crypto/Key.js.map +1 -1
  114. package/dist/esm/crypto/MockCrypto.d.ts.map +1 -1
  115. package/dist/esm/crypto/MockCrypto.js +2 -1
  116. package/dist/esm/crypto/MockCrypto.js.map +1 -1
  117. package/dist/esm/crypto/Spake2p.d.ts +12 -11
  118. package/dist/esm/crypto/Spake2p.d.ts.map +1 -1
  119. package/dist/esm/crypto/Spake2p.js +15 -11
  120. package/dist/esm/crypto/Spake2p.js.map +1 -1
  121. package/dist/esm/crypto/StandardCrypto.d.ts +12 -11
  122. package/dist/esm/crypto/StandardCrypto.d.ts.map +1 -1
  123. package/dist/esm/crypto/StandardCrypto.js +27 -17
  124. package/dist/esm/crypto/StandardCrypto.js.map +1 -1
  125. package/dist/esm/crypto/aes/Aes.d.ts +2 -1
  126. package/dist/esm/crypto/aes/Aes.d.ts.map +1 -1
  127. package/dist/esm/crypto/aes/Aes.js +3 -1
  128. package/dist/esm/crypto/aes/Aes.js.map +1 -1
  129. package/dist/esm/crypto/aes/Ccm.d.ts +4 -3
  130. package/dist/esm/crypto/aes/Ccm.d.ts.map +1 -1
  131. package/dist/esm/crypto/aes/Ccm.js.map +1 -1
  132. package/dist/esm/log/Diagnostic.js +1 -1
  133. package/dist/esm/log/Diagnostic.js.map +1 -1
  134. package/dist/esm/log/LogFormat.js +1 -1
  135. package/dist/esm/log/LogFormat.js.map +1 -1
  136. package/dist/esm/log/Logger.js +1 -1
  137. package/dist/esm/log/Logger.js.map +1 -1
  138. package/dist/esm/net/NetInterface.d.ts +2 -1
  139. package/dist/esm/net/NetInterface.d.ts.map +1 -1
  140. package/dist/esm/net/NetInterface.js.map +1 -1
  141. package/dist/esm/net/TransportInterface.d.ts +2 -1
  142. package/dist/esm/net/TransportInterface.d.ts.map +1 -1
  143. package/dist/esm/net/TransportInterface.js.map +1 -1
  144. package/dist/esm/net/UdpChannel.d.ts +3 -3
  145. package/dist/esm/net/UdpChannel.d.ts.map +1 -1
  146. package/dist/esm/net/UdpInterface.d.ts +4 -3
  147. package/dist/esm/net/UdpInterface.d.ts.map +1 -1
  148. package/dist/esm/net/UdpInterface.js.map +1 -1
  149. package/dist/esm/net/UdpMulticastServer.d.ts +3 -2
  150. package/dist/esm/net/UdpMulticastServer.d.ts.map +1 -1
  151. package/dist/esm/net/UdpMulticastServer.js.map +1 -1
  152. package/dist/esm/net/mock/MockRouter.d.ts +1 -1
  153. package/dist/esm/net/mock/MockRouter.d.ts.map +1 -1
  154. package/dist/esm/net/mock/MockUdpChannel.d.ts +1 -1
  155. package/dist/esm/net/mock/MockUdpChannel.d.ts.map +1 -1
  156. package/dist/esm/net/mock/MockUdpChannel.js.map +1 -1
  157. package/dist/esm/net/mock/NetworkSimulator.d.ts +1 -1
  158. package/dist/esm/net/mock/NetworkSimulator.d.ts.map +1 -1
  159. package/dist/esm/storage/Storage.d.ts +1 -1
  160. package/dist/esm/storage/Storage.d.ts.map +1 -1
  161. package/dist/esm/storage/StorageBackendMemory.d.ts +1 -1
  162. package/dist/esm/storage/StorageBackendMemory.d.ts.map +1 -1
  163. package/dist/esm/storage/StorageBackendMemory.js +7 -5
  164. package/dist/esm/storage/StorageBackendMemory.js.map +1 -1
  165. package/dist/esm/storage/StorageContext.d.ts +1 -1
  166. package/dist/esm/storage/StorageContext.d.ts.map +1 -1
  167. package/dist/esm/storage/StorageContext.js.map +1 -1
  168. package/dist/esm/storage/StringifyTools.d.ts +2 -1
  169. package/dist/esm/storage/StringifyTools.d.ts.map +1 -1
  170. package/dist/esm/storage/StringifyTools.js +1 -1
  171. package/dist/esm/storage/StringifyTools.js.map +1 -1
  172. package/dist/esm/transaction/Tx.js +5 -1
  173. package/dist/esm/transaction/Tx.js.map +1 -1
  174. package/dist/esm/util/Bytes.d.ts +29 -11
  175. package/dist/esm/util/Bytes.d.ts.map +1 -1
  176. package/dist/esm/util/Bytes.js +31 -12
  177. package/dist/esm/util/Bytes.js.map +1 -1
  178. package/dist/esm/util/DataReader.d.ts +2 -2
  179. package/dist/esm/util/DataReader.d.ts.map +1 -1
  180. package/dist/esm/util/DataReader.js +1 -1
  181. package/dist/esm/util/DataReader.js.map +1 -1
  182. package/dist/esm/util/DataWriter.d.ts +3 -3
  183. package/dist/esm/util/DataWriter.d.ts.map +1 -1
  184. package/dist/esm/util/DataWriter.js +2 -2
  185. package/dist/esm/util/DataWriter.js.map +1 -1
  186. package/dist/esm/util/String.d.ts.map +1 -1
  187. package/dist/esm/util/String.js +3 -8
  188. package/dist/esm/util/String.js.map +1 -1
  189. package/package.json +2 -2
  190. package/src/codec/DerCodec.ts +32 -39
  191. package/src/codec/DerTypes.ts +4 -3
  192. package/src/codec/DnsCodec.ts +21 -21
  193. package/src/crypto/Crypto.ts +16 -26
  194. package/src/crypto/Key.ts +46 -41
  195. package/src/crypto/MockCrypto.ts +2 -1
  196. package/src/crypto/Spake2p.ts +26 -22
  197. package/src/crypto/StandardCrypto.ts +40 -31
  198. package/src/crypto/aes/Aes.ts +4 -2
  199. package/src/crypto/aes/Ccm.ts +4 -3
  200. package/src/log/Diagnostic.ts +1 -1
  201. package/src/log/LogFormat.ts +1 -1
  202. package/src/log/Logger.ts +1 -1
  203. package/src/net/NetInterface.ts +2 -1
  204. package/src/net/TransportInterface.ts +2 -1
  205. package/src/net/UdpChannel.ts +3 -3
  206. package/src/net/UdpInterface.ts +4 -3
  207. package/src/net/UdpMulticastServer.ts +3 -2
  208. package/src/net/mock/MockRouter.ts +1 -1
  209. package/src/net/mock/MockUdpChannel.ts +1 -1
  210. package/src/net/mock/NetworkSimulator.ts +1 -1
  211. package/src/storage/Storage.ts +1 -1
  212. package/src/storage/StorageBackendMemory.ts +8 -6
  213. package/src/storage/StorageContext.ts +1 -1
  214. package/src/storage/StringifyTools.ts +2 -2
  215. package/src/transaction/Tx.ts +5 -1
  216. package/src/util/Bytes.ts +59 -21
  217. package/src/util/DataReader.ts +2 -2
  218. package/src/util/DataWriter.ts +3 -3
  219. package/src/util/String.ts +3 -8
package/src/crypto/Key.ts CHANGED
@@ -62,8 +62,8 @@ const CurveLookup = {
62
62
  };
63
63
 
64
64
  export type BinaryKeyPair = {
65
- publicKey: Uint8Array;
66
- privateKey: Uint8Array;
65
+ publicKey: Bytes;
66
+ privateKey: Bytes;
67
67
  };
68
68
 
69
69
  /**
@@ -131,40 +131,40 @@ export interface Key extends JsonWebKey {
131
131
  * Binary alias to private key field. Automatically encodes/decodes the
132
132
  * base-64 private key.
133
133
  */
134
- privateBits?: Uint8Array;
134
+ privateBits?: Bytes;
135
135
 
136
136
  /**
137
137
  * Binary alias to the x field. Automatically encodes/decodes the base-64
138
138
  * x-point on EC public keys.
139
139
  */
140
- xBits?: Uint8Array;
140
+ xBits?: Bytes;
141
141
 
142
142
  /**
143
143
  * Binary alias to the y field. Automatically encodes/decodes the base-64
144
144
  * y-point on EC public keys.
145
145
  */
146
- yBits?: Uint8Array;
146
+ yBits?: Bytes;
147
147
 
148
148
  /**
149
149
  * Import (write-only) of private keys encoded in SEC1 format.
150
150
  */
151
- sec1?: Uint8Array;
151
+ sec1?: Bytes;
152
152
 
153
153
  /**
154
154
  * Import (write-only) of private keys encoded in PKCS #8 format.
155
155
  */
156
- pkcs8?: Uint8Array;
156
+ pkcs8?: Bytes;
157
157
 
158
158
  /**
159
159
  * Import (write-only) of public keys encoded in SPKI format.
160
160
  */
161
- spki?: Uint8Array;
161
+ spki?: Bytes;
162
162
 
163
163
  /**
164
164
  * Import/export of EC public key in SEC1/SPKI format. Maps to x & y
165
165
  * fields internally.
166
166
  */
167
- publicBits?: Uint8Array;
167
+ publicBits?: Bytes;
168
168
 
169
169
  /**
170
170
  * Import/export of BinaryKeyPair structure used as an alternate
@@ -175,12 +175,12 @@ export interface Key extends JsonWebKey {
175
175
  /**
176
176
  * Alias for publicBits that throws if no public key is present.
177
177
  */
178
- publicKey: Uint8Array;
178
+ publicKey: Bytes;
179
179
 
180
180
  /**
181
181
  * Alias for privateBits that throws if no private key is present.
182
182
  */
183
- privateKey: Uint8Array;
183
+ privateKey: Bytes;
184
184
 
185
185
  /**
186
186
  * Alias for keyPairBits that throws if a complete key pair is not present.
@@ -196,9 +196,9 @@ export interface PublicKey extends Key {
196
196
  curve: CurveType;
197
197
  x: string;
198
198
  y: string;
199
- xBits: Uint8Array;
200
- yBits: Uint8Array;
201
- publicBits: Uint8Array;
199
+ xBits: Bytes;
200
+ yBits: Bytes;
201
+ publicBits: Bytes;
202
202
  }
203
203
 
204
204
  /**
@@ -207,11 +207,11 @@ export interface PublicKey extends Key {
207
207
  export interface PrivateKey extends PublicKey {
208
208
  private: string;
209
209
  d: string;
210
- privateBits: Uint8Array;
211
- privateKey: Uint8Array;
210
+ privateBits: Bytes;
211
+ privateKey: Bytes;
212
212
  keyPair: BinaryKeyPair;
213
213
  keyPairBits: BinaryKeyPair;
214
- sharedSecretFor(peerKey: PublicKey): Uint8Array;
214
+ sharedSecretFor(peerKey: PublicKey): Bytes;
215
215
  }
216
216
 
217
217
  /**
@@ -225,7 +225,11 @@ export interface SymmetricKey extends Key {
225
225
 
226
226
  function checkDerVersion(type: string, node: DerNode | undefined, version: number) {
227
227
  const derVersion =
228
- node && node._tag === DerType.Integer && node._bytes && node._bytes.length === 1 && node._bytes[0];
228
+ node &&
229
+ node._tag === DerType.Integer &&
230
+ node._bytes &&
231
+ node._bytes.byteLength === 1 &&
232
+ Bytes.of(node._bytes)[0];
229
233
 
230
234
  if (derVersion !== version) {
231
235
  throw new KeyInputError(`${type} key version mismatch`);
@@ -233,7 +237,7 @@ function checkDerVersion(type: string, node: DerNode | undefined, version: numbe
233
237
  }
234
238
 
235
239
  function getDerObjectID(type: string, node?: DerNode) {
236
- const id = node && node._tag === DerType.ObjectIdentifier && node._bytes?.length > 1 && node._bytes;
240
+ const id = node && node._tag === DerType.ObjectIdentifier && node._bytes?.byteLength > 1 && node._bytes;
237
241
 
238
242
  if (id) return id;
239
243
 
@@ -248,7 +252,7 @@ function getDerCurve(type: string, node?: DerNode) {
248
252
  }
249
253
 
250
254
  function getDerKey(type: string, node?: DerNode, derType: DerType = DerType.OctetString) {
251
- const key = node && node._tag === derType && node._bytes?.length > 1 && node._bytes;
255
+ const key = node && node._tag === derType && node._bytes?.byteLength > 1 && node._bytes;
252
256
 
253
257
  if (key) return key;
254
258
 
@@ -259,7 +263,7 @@ function getDerKey(type: string, node?: DerNode, derType: DerType = DerType.Octe
259
263
  namespace Translators {
260
264
  // Import SEC1 private key
261
265
  export const sec1 = {
262
- set: function (this: Key, input: Uint8Array) {
266
+ set: function (this: Key, input: Bytes) {
263
267
  const decoded = DerCodec.decode(input);
264
268
 
265
269
  // Version
@@ -286,7 +290,7 @@ namespace Translators {
286
290
 
287
291
  // Import PKCS8 private key
288
292
  export const pkcs8 = {
289
- set: function (this: Key, input: Uint8Array) {
293
+ set: function (this: Key, input: Bytes) {
290
294
  const outer = DerCodec.decode(input);
291
295
 
292
296
  // Version
@@ -323,7 +327,7 @@ namespace Translators {
323
327
 
324
328
  // Import SPKI public key
325
329
  export const spki = {
326
- set: function (this: Key, input: Uint8Array) {
330
+ set: function (this: Key, input: Bytes) {
327
331
  const decoded = DerCodec.decode(input);
328
332
 
329
333
  const algorithmElements = decoded?._elements?.[0]?._elements;
@@ -352,12 +356,13 @@ namespace Translators {
352
356
 
353
357
  // Import public key bytes in SEC1/SPKI format
354
358
  export const publicBits = {
355
- set: function (this: Key, input: Uint8Array) {
356
- if (!(input.length % 2)) {
359
+ set: function (this: Key, input: Bytes) {
360
+ const data = Bytes.of(input);
361
+ if (!(data.length % 2)) {
357
362
  throw new KeyInputError("Invalid public key encoding");
358
363
  }
359
364
 
360
- switch (input[0]) {
365
+ switch (data[0]) {
361
366
  case 2:
362
367
  case 3:
363
368
  throw new KeyInputError("Unsupported public key compression");
@@ -369,13 +374,13 @@ namespace Translators {
369
374
  throw new KeyInputError("Illegal public key format specifier");
370
375
  }
371
376
 
372
- const coordinateLength = (input.length - 1) / 2;
377
+ const coordinateLength = (data.length - 1) / 2;
373
378
 
374
379
  inferCurve(this, coordinateLength);
375
380
 
376
381
  this.type = KeyType.EC;
377
- this.xBits = input.slice(1, coordinateLength + 1);
378
- this.yBits = input.slice(coordinateLength + 1);
382
+ this.xBits = data.slice(1, coordinateLength + 1);
383
+ this.yBits = data.slice(coordinateLength + 1);
379
384
  },
380
385
 
381
386
  get: function (this: Key) {
@@ -383,7 +388,7 @@ namespace Translators {
383
388
  return undefined;
384
389
  }
385
390
 
386
- return new Uint8Array([0x04, ...this.xBits, ...this.yBits]);
391
+ return Bytes.concat(new Uint8Array([0x04]), this.xBits, this.yBits);
387
392
  },
388
393
  };
389
394
 
@@ -401,8 +406,8 @@ namespace Translators {
401
406
  return;
402
407
  }
403
408
  return {
404
- publicKey: publicBits,
405
- privateKey: privateBits,
409
+ publicKey: Bytes.of(publicBits),
410
+ privateKey: Bytes.of(privateBits),
406
411
  };
407
412
  },
408
413
  };
@@ -536,7 +541,7 @@ export function Key(properties: Partial<Key>) {
536
541
  }
537
542
 
538
543
  // Compute
539
- const ecKey = Point.fromPrivateKey(that.privateKey);
544
+ const ecKey = Point.fromPrivateKey(Bytes.of(that.privateKey));
540
545
 
541
546
  // Install
542
547
  that.xBits = numberToBytesBE(ecKey.x, keyLength);
@@ -545,9 +550,9 @@ export function Key(properties: Partial<Key>) {
545
550
 
546
551
  if (that.type === KeyType.EC) {
547
552
  if (that.d) {
548
- inferCurve(that, that.privateKey.length);
553
+ inferCurve(that, that.privateKey.byteLength);
549
554
  } else if (that.xBits) {
550
- inferCurve(that, that.xBits.length);
555
+ inferCurve(that, that.xBits.byteLength);
551
556
  }
552
557
 
553
558
  if (that.d && (!that.x || !that.y)) {
@@ -561,9 +566,9 @@ export function Key(properties: Partial<Key>) {
561
566
  /**
562
567
  * EC private key factory.
563
568
  */
564
- export function PrivateKey(privateKey: Uint8Array | BinaryKeyPair, options?: Partial<Key>) {
569
+ export function PrivateKey(privateKey: Bytes | BinaryKeyPair, options?: Partial<Key>) {
565
570
  let priv, pub;
566
- if (ArrayBuffer.isView(privateKey)) {
571
+ if (Bytes.isBytes(privateKey)) {
567
572
  priv = privateKey;
568
573
  } else {
569
574
  priv = privateKey.privateKey;
@@ -581,7 +586,7 @@ export function PrivateKey(privateKey: Uint8Array | BinaryKeyPair, options?: Par
581
586
  /**
582
587
  * EC public key factory.
583
588
  */
584
- export function PublicKey(publicKey: Uint8Array, options?: Partial<Key>) {
589
+ export function PublicKey(publicKey: Bytes, options?: Partial<Key>) {
585
590
  return Key({
586
591
  type: KeyType.EC,
587
592
  publicKey,
@@ -592,7 +597,7 @@ export function PublicKey(publicKey: Uint8Array, options?: Partial<Key>) {
592
597
  /**
593
598
  * Symmetric key factory.
594
599
  */
595
- export function SymmetricKey(privateKey: Uint8Array, options?: Partial<Key>) {
600
+ export function SymmetricKey(privateKey: Bytes, options?: Partial<Key>) {
596
601
  return Key({
597
602
  type: KeyType.oct,
598
603
  privateKey: privateKey,
@@ -605,6 +610,6 @@ export function SymmetricKey(privateKey: Uint8Array, options?: Partial<Key>) {
605
610
  *
606
611
  * We provide this for platforms without a native implementation.
607
612
  */
608
- export function sharedSecretFor(this: PrivateKey, peerKey: PublicKey): Uint8Array {
609
- return getSharedSecret(this.privateBits, peerKey.publicBits);
613
+ export function sharedSecretFor(this: PrivateKey, peerKey: PublicKey): Bytes {
614
+ return Bytes.of(getSharedSecret(Bytes.of(this.privateBits), Bytes.of(peerKey.publicBits)));
610
615
  }
@@ -5,6 +5,7 @@
5
5
  */
6
6
 
7
7
  import { ImplementationError } from "#MatterError.js";
8
+ import { Bytes } from "#util/Bytes.js";
8
9
  import { Crypto, ec } from "./Crypto.js";
9
10
  import { CurveType, Key, KeyType, PrivateKey } from "./Key.js";
10
11
  import { StandardCrypto } from "./StandardCrypto.js";
@@ -81,7 +82,7 @@ export function MockCrypto(index: number = 0x80, implementation: new () => Crypt
81
82
 
82
83
  // Ensure EC key generation uses our own "entropy" source rather than the platform's
83
84
  crypto.createKeyPair = function getRandomDataNONENTROPIC() {
84
- const privateBits = ec.mapHashToField(new Uint8Array(crypto.randomBytes(48)), ec.p256.CURVE.n);
85
+ const privateBits = ec.mapHashToField(Bytes.of(crypto.randomBytes(48)), ec.p256.CURVE.n);
85
86
  return Key({
86
87
  kty: KeyType.EC,
87
88
  crv: CurveType.p256,
@@ -25,19 +25,21 @@ const CRYPTO_W_SIZE_BYTES = CRYPTO_GROUP_SIZE_BYTES + 8;
25
25
 
26
26
  export interface PbkdfParameters {
27
27
  iterations: number;
28
- salt: Uint8Array;
28
+ salt: Bytes;
29
29
  }
30
30
 
31
31
  export class Spake2p {
32
32
  readonly #crypto: Crypto;
33
- readonly #context: Uint8Array;
33
+ readonly #context: Bytes;
34
34
  readonly #random: bigint;
35
35
  readonly #w0: bigint;
36
36
 
37
37
  static async computeW0W1(crypto: Crypto, { iterations, salt }: PbkdfParameters, pin: number) {
38
38
  const pinWriter = new DataWriter(Endian.Little);
39
39
  pinWriter.writeUInt32(pin);
40
- const ws = await crypto.createPbkdf2Key(pinWriter.toByteArray(), salt, iterations, CRYPTO_W_SIZE_BYTES * 2);
40
+ const ws = Bytes.of(
41
+ await crypto.createPbkdf2Key(pinWriter.toByteArray(), salt, iterations, CRYPTO_W_SIZE_BYTES * 2),
42
+ );
41
43
  const w0 = mod(bytesToNumberBE(ws.slice(0, 40)), P256_CURVE.n);
42
44
  const w1 = mod(bytesToNumberBE(ws.slice(40, 80)), P256_CURVE.n);
43
45
  return { w0, w1 };
@@ -49,30 +51,30 @@ export class Spake2p {
49
51
  return { w0, L };
50
52
  }
51
53
 
52
- static create(crypto: Crypto, context: Uint8Array, w0: bigint) {
54
+ static create(crypto: Crypto, context: Bytes, w0: bigint) {
53
55
  const random = crypto.randomBigInt(32, P256_CURVE.Fp.ORDER);
54
56
  return new Spake2p(crypto, context, random, w0);
55
57
  }
56
58
 
57
- constructor(crypto: Crypto, context: Uint8Array, random: bigint, w0: bigint) {
59
+ constructor(crypto: Crypto, context: Bytes, random: bigint, w0: bigint) {
58
60
  this.#crypto = crypto;
59
61
  this.#context = context;
60
62
  this.#random = random;
61
63
  this.#w0 = w0;
62
64
  }
63
65
 
64
- computeX(): Uint8Array {
66
+ computeX(): Bytes {
65
67
  const X = ProjectivePoint.BASE.multiply(this.#random).add(M.multiply(this.#w0));
66
68
  return X.toRawBytes(false);
67
69
  }
68
70
 
69
- computeY(): Uint8Array {
71
+ computeY(): Bytes {
70
72
  const Y = ProjectivePoint.BASE.multiply(this.#random).add(N.multiply(this.#w0));
71
73
  return Y.toRawBytes(false);
72
74
  }
73
75
 
74
- async computeSecretAndVerifiersFromY(w1: bigint, X: Uint8Array, Y: Uint8Array) {
75
- const YPoint = ProjectivePoint.fromHex(Y);
76
+ async computeSecretAndVerifiersFromY(w1: bigint, X: Bytes, Y: Bytes) {
77
+ const YPoint = ProjectivePoint.fromHex(Bytes.of(Y));
76
78
  try {
77
79
  YPoint.assertValidity();
78
80
  } catch (error) {
@@ -81,12 +83,12 @@ export class Spake2p {
81
83
  const yNwo = YPoint.add(N.multiply(this.#w0).negate());
82
84
  const Z = yNwo.multiply(this.#random);
83
85
  const V = yNwo.multiply(w1);
84
- return this.computeSecretAndVerifiers(X, Y, Z.toRawBytes(false), V.toRawBytes(false));
86
+ return this.computeSecretAndVerifiers(X, Y, Bytes.of(Z.toRawBytes(false)), Bytes.of(V.toRawBytes(false)));
85
87
  }
86
88
 
87
- async computeSecretAndVerifiersFromX(L: Uint8Array, X: Uint8Array, Y: Uint8Array) {
88
- const XPoint = ProjectivePoint.fromHex(X);
89
- const LPoint = ProjectivePoint.fromHex(L);
89
+ async computeSecretAndVerifiersFromX(L: Bytes, X: Bytes, Y: Bytes) {
90
+ const XPoint = ProjectivePoint.fromHex(Bytes.of(X));
91
+ const LPoint = ProjectivePoint.fromHex(Bytes.of(L));
90
92
  try {
91
93
  XPoint.assertValidity();
92
94
  } catch (error) {
@@ -94,15 +96,17 @@ export class Spake2p {
94
96
  }
95
97
  const Z = XPoint.add(M.multiply(this.#w0).negate()).multiply(this.#random);
96
98
  const V = LPoint.multiply(this.#random);
97
- return this.computeSecretAndVerifiers(X, Y, Z.toRawBytes(false), V.toRawBytes(false));
99
+ return this.computeSecretAndVerifiers(X, Y, Bytes.of(Z.toRawBytes(false)), Bytes.of(V.toRawBytes(false)));
98
100
  }
99
101
 
100
- private async computeSecretAndVerifiers(X: Uint8Array, Y: Uint8Array, Z: Uint8Array, V: Uint8Array) {
101
- const TT_HASH = await this.computeTranscriptHash(X, Y, Z, V);
102
+ private async computeSecretAndVerifiers(X: Bytes, Y: Bytes, Z: Bytes, V: Bytes) {
103
+ const TT_HASH = Bytes.of(await this.computeTranscriptHash(X, Y, Z, V));
102
104
  const Ka = TT_HASH.slice(0, 16);
103
105
  const Ke = TT_HASH.slice(16, 32);
104
106
 
105
- const KcAB = await this.#crypto.createHkdfKey(Ka, new Uint8Array(0), Bytes.fromString("ConfirmationKeys"), 32);
107
+ const KcAB = Bytes.of(
108
+ await this.#crypto.createHkdfKey(Ka, new Uint8Array(0), Bytes.fromString("ConfirmationKeys"), 32),
109
+ );
106
110
  const KcA = KcAB.slice(0, 16);
107
111
  const KcB = KcAB.slice(16, 32);
108
112
 
@@ -112,13 +116,13 @@ export class Spake2p {
112
116
  return { Ke, hAY, hBX };
113
117
  }
114
118
 
115
- private computeTranscriptHash(X: Uint8Array, Y: Uint8Array, Z: Uint8Array, V: Uint8Array) {
119
+ private computeTranscriptHash(X: Bytes, Y: Bytes, Z: Bytes, V: Bytes) {
116
120
  const TTwriter = new DataWriter(Endian.Little);
117
121
  this.addToContext(TTwriter, this.#context);
118
122
  this.addToContext(TTwriter, Bytes.fromString(""));
119
123
  this.addToContext(TTwriter, Bytes.fromString(""));
120
- this.addToContext(TTwriter, M.toRawBytes(false));
121
- this.addToContext(TTwriter, N.toRawBytes(false));
124
+ this.addToContext(TTwriter, Bytes.of(M.toRawBytes(false)));
125
+ this.addToContext(TTwriter, Bytes.of(N.toRawBytes(false)));
122
126
  this.addToContext(TTwriter, X);
123
127
  this.addToContext(TTwriter, Y);
124
128
  this.addToContext(TTwriter, Z);
@@ -127,8 +131,8 @@ export class Spake2p {
127
131
  return this.#crypto.computeSha256(TTwriter.toByteArray());
128
132
  }
129
133
 
130
- private addToContext(TTwriter: DataWriter<Endian.Little>, data: Uint8Array) {
131
- TTwriter.writeUInt64(data.length);
134
+ private addToContext(TTwriter: DataWriter<Endian.Little>, data: Bytes) {
135
+ TTwriter.writeUInt64(data.byteLength);
132
136
  TTwriter.writeByteArray(data);
133
137
  }
134
138
  }
@@ -71,70 +71,76 @@ export class StandardCrypto extends Crypto {
71
71
  return new StandardCrypto();
72
72
  }
73
73
 
74
- randomBytes(length: number): Uint8Array {
74
+ randomBytes(length: number): Bytes {
75
75
  const result = new Uint8Array(length);
76
76
  this.#crypto.getRandomValues(result);
77
77
  return result;
78
78
  }
79
79
 
80
- encrypt(key: Uint8Array, data: Uint8Array, nonce: Uint8Array, associatedData?: Uint8Array) {
80
+ encrypt(key: Bytes, data: Bytes, nonce: Bytes, associatedData?: Bytes) {
81
81
  const ccm = Ccm(key);
82
- return ccm.encrypt({ pt: data, nonce, adata: associatedData });
82
+ return ccm.encrypt({
83
+ pt: Bytes.of(data),
84
+ nonce: Bytes.of(nonce),
85
+ adata: associatedData !== undefined ? Bytes.of(associatedData) : undefined,
86
+ });
83
87
  }
84
88
 
85
- decrypt(key: Uint8Array, data: Uint8Array, nonce: Uint8Array, associatedData?: Uint8Array) {
89
+ decrypt(key: Bytes, data: Bytes, nonce: Bytes, associatedData?: Bytes) {
86
90
  const ccm = Ccm(key);
87
- return ccm.decrypt({ ct: data, nonce, adata: associatedData });
91
+ return ccm.decrypt({
92
+ ct: Bytes.of(data),
93
+ nonce: Bytes.of(nonce),
94
+ adata: associatedData !== undefined ? Bytes.of(associatedData) : undefined,
95
+ });
88
96
  }
89
97
 
90
- async computeSha256(buffer: Uint8Array | Uint8Array[]) {
98
+ computeSha256(buffer: Bytes | Bytes[]) {
91
99
  if (Array.isArray(buffer)) {
92
100
  buffer = Bytes.concat(...buffer);
93
101
  }
94
- return new Uint8Array(await this.#subtle.digest("SHA-256", buffer));
102
+ return this.#subtle.digest("SHA-256", Bytes.exclusive(buffer));
95
103
  }
96
104
 
97
- async createPbkdf2Key(secret: Uint8Array, salt: Uint8Array, iteration: number, keyLength: number) {
105
+ async createPbkdf2Key(secret: Bytes, salt: Bytes, iteration: number, keyLength: number) {
98
106
  const key = await this.importKey("raw", secret, "PBKDF2", false, ["deriveBits"]);
99
- const bits = await this.#subtle.deriveBits(
107
+ return this.#subtle.deriveBits(
100
108
  {
101
109
  name: "PBKDF2",
102
110
  hash: "SHA-256",
103
- salt: salt,
111
+ salt: Bytes.exclusive(salt),
104
112
  iterations: iteration,
105
113
  },
106
114
  key,
107
115
  keyLength * 8,
108
116
  );
109
- return new Uint8Array(bits);
110
117
  }
111
118
 
112
119
  async createHkdfKey(
113
- secret: Uint8Array,
114
- salt: Uint8Array,
115
- info: Uint8Array,
120
+ secret: Bytes,
121
+ salt: Bytes,
122
+ info: Bytes,
116
123
  length: number = CRYPTO_SYMMETRIC_KEY_LENGTH,
117
- ) {
124
+ ): Promise<Bytes> {
118
125
  const key = await this.importKey("raw", secret, "HKDF", false, ["deriveBits"]);
119
- const bits = await this.#subtle.deriveBits(
126
+ return this.#subtle.deriveBits(
120
127
  {
121
128
  name: "HKDF",
122
129
  hash: "SHA-256",
123
- salt: salt,
124
- info: info,
130
+ salt: Bytes.exclusive(salt),
131
+ info: Bytes.exclusive(info),
125
132
  },
126
133
  key,
127
134
  8 * length,
128
135
  );
129
- return new Uint8Array(bits);
130
136
  }
131
137
 
132
- async signHmac(secret: Uint8Array, data: Uint8Array) {
138
+ async signHmac(secret: Bytes, data: Bytes): Promise<Bytes> {
133
139
  const key = await this.importKey("raw", secret, { name: "HMAC", hash: "SHA-256" }, false, ["sign"]);
134
- return new Uint8Array(await this.#subtle.sign("HMAC", key, data));
140
+ return this.#subtle.sign("HMAC", key, Bytes.exclusive(data));
135
141
  }
136
142
 
137
- async signEcdsa(key: JsonWebKey, data: Uint8Array | Uint8Array[], dsaEncoding?: CryptoDsaEncoding) {
143
+ async signEcdsa(key: JsonWebKey, data: Bytes | Bytes[], dsaEncoding?: CryptoDsaEncoding) {
138
144
  if (Array.isArray(data)) {
139
145
  data = Bytes.concat(...data);
140
146
  }
@@ -153,9 +159,9 @@ export class StandardCrypto extends Crypto {
153
159
 
154
160
  const subtleKey = await this.importKey("jwk", key, SIGNATURE_ALGORITHM, false, ["sign"]);
155
161
 
156
- const ieeeP1363 = await this.#subtle.sign(SIGNATURE_ALGORITHM, subtleKey, data);
162
+ const ieeeP1363 = Bytes.of(await this.#subtle.sign(SIGNATURE_ALGORITHM, subtleKey, Bytes.exclusive(data)));
157
163
 
158
- if (dsaEncoding !== "der") return new Uint8Array(ieeeP1363);
164
+ if (dsaEncoding !== "der") return ieeeP1363;
159
165
 
160
166
  const bytesPerComponent = ieeeP1363.byteLength / 2;
161
167
 
@@ -165,7 +171,7 @@ export class StandardCrypto extends Crypto {
165
171
  });
166
172
  }
167
173
 
168
- async verifyEcdsa(key: JsonWebKey, data: Uint8Array, signature: Uint8Array, dsaEncoding?: CryptoDsaEncoding) {
174
+ async verifyEcdsa(key: JsonWebKey, data: Bytes, signature: Bytes, dsaEncoding?: CryptoDsaEncoding) {
169
175
  const { crv, kty, x, y } = key;
170
176
  key = { crv, kty, x, y };
171
177
  const subtleKey = await this.importKey("jwk", key, SIGNATURE_ALGORITHM, false, ["verify"]);
@@ -185,7 +191,12 @@ export class StandardCrypto extends Crypto {
185
191
  }
186
192
  }
187
193
 
188
- const verified = await this.#subtle.verify(SIGNATURE_ALGORITHM, subtleKey, signature, data);
194
+ const verified = await this.#subtle.verify(
195
+ SIGNATURE_ALGORITHM,
196
+ subtleKey,
197
+ Bytes.exclusive(signature),
198
+ Bytes.exclusive(data),
199
+ );
189
200
 
190
201
  if (!verified) {
191
202
  throw new CryptoVerifyError("Signature verification failed");
@@ -223,7 +234,7 @@ export class StandardCrypto extends Crypto {
223
234
  return await this.#subtle.exportKey("jwk", subtleKey.privateKey);
224
235
  }
225
236
 
226
- async generateDhSecret(key: PrivateKey, peerKey: PublicKey) {
237
+ async generateDhSecret(key: PrivateKey, peerKey: PublicKey): Promise<Bytes> {
227
238
  const subtleKey = await this.importKey(
228
239
  "jwk",
229
240
  key,
@@ -246,7 +257,7 @@ export class StandardCrypto extends Crypto {
246
257
  [],
247
258
  );
248
259
 
249
- const secret = await this.#subtle.deriveBits(
260
+ return this.#subtle.deriveBits(
250
261
  {
251
262
  name: "ECDH",
252
263
  public: subtlePeerKey,
@@ -254,13 +265,11 @@ export class StandardCrypto extends Crypto {
254
265
  subtleKey,
255
266
  256,
256
267
  );
257
-
258
- return new Uint8Array(secret);
259
268
  }
260
269
 
261
270
  protected async importKey(
262
271
  format: KeyFormat,
263
- keyData: JsonWebKey | BufferSource,
272
+ keyData: JsonWebKey | Bytes,
264
273
  algorithm: AlgorithmIdentifier | RsaHashedImportParams | EcKeyImportParams | HmacImportParams | AesKeyAlgorithm,
265
274
  extractable: boolean,
266
275
  keyUsages: ReadonlyArray<KeyUsage>,
@@ -10,6 +10,7 @@
10
10
  * OpenSSL: https://github.com/openssl/openssl/blob/master/crypto/aes/aes_core.c
11
11
  */
12
12
 
13
+ import { Bytes } from "#util/Bytes.js";
13
14
  import { WordArray } from "./WordArray.js";
14
15
 
15
16
  /**
@@ -17,7 +18,7 @@ import { WordArray } from "./WordArray.js";
17
18
  *
18
19
  * WARNING: Unaudited. Consider platform replacement if available.
19
20
  */
20
- export function Aes(key: Uint8Array) {
21
+ export function Aes(key: Bytes) {
21
22
  const { encryptKey, decryptKey } = expandKey(key);
22
23
 
23
24
  return {
@@ -164,7 +165,8 @@ function crypt(input: WordArray, output: WordArray, roundKeys: WordArray, tabs:
164
165
  *
165
166
  * This generates keys for each round based off of the input key.
166
167
  */
167
- function expandKey(key: Uint8Array) {
168
+ function expandKey(keyData: Bytes) {
169
+ const key = Bytes.of(keyData);
168
170
  const inputLength = key.length / 4,
169
171
  roundsNeeded = inputLength + 7,
170
172
  wordsNeeded = roundsNeeded * 4,
@@ -14,6 +14,7 @@
14
14
 
15
15
  import { CRYPTO_AEAD_MIC_LENGTH_BYTES, CRYPTO_AEAD_NONCE_LENGTH_BYTES } from "#crypto/CryptoConstants.js";
16
16
  import { CryptoInputError } from "#crypto/CryptoError.js";
17
+ import { Bytes } from "#util/Bytes.js";
17
18
  import { Aes } from "./Aes.js";
18
19
  import { WordArray } from "./WordArray.js";
19
20
 
@@ -57,11 +58,11 @@ import { WordArray } from "./WordArray.js";
57
58
  * * We use {@link DataView} to read/write words where possible. However, byte buffers may not align to word
58
59
  * boundaries. We detect this case and manually read/write the last word
59
60
  */
60
- export function Ccm(key: Uint8Array) {
61
+ export function Ccm(key: Bytes) {
61
62
  const aes = Aes(key);
62
63
 
63
64
  return {
64
- encrypt(input: Ccm.EncryptInput): Uint8Array {
65
+ encrypt(input: Ccm.EncryptInput): Bytes {
65
66
  validateNonceAndAdata(input);
66
67
 
67
68
  const ptLength = input.pt.length;
@@ -91,7 +92,7 @@ export function Ccm(key: Uint8Array) {
91
92
  return ct;
92
93
  },
93
94
 
94
- decrypt(input: Ccm.DecryptInput): Uint8Array {
95
+ decrypt(input: Ccm.DecryptInput): Bytes {
95
96
  validateNonceAndAdata(input);
96
97
 
97
98
  if (input.ct.length > MAX_CIPHERTEXT_LENGTH) {
@@ -400,7 +400,7 @@ export namespace Diagnostic {
400
400
  if (typeof value === "bigint") {
401
401
  return value.toString();
402
402
  }
403
- if (value instanceof Uint8Array) {
403
+ if (Bytes.isBytes(value)) {
404
404
  return Bytes.toHex(value);
405
405
  }
406
406
  if (value === undefined) {
@@ -465,7 +465,7 @@ function renderValue(value: unknown, formatter: Formatter, squash: boolean): str
465
465
  if (value === null) {
466
466
  return formatter.text("null");
467
467
  }
468
- if (value instanceof Uint8Array) {
468
+ if (Bytes.isBytes(value)) {
469
469
  return formatter.text(Bytes.toHex(value));
470
470
  }
471
471
  if (value instanceof Error) {
package/src/log/Logger.ts CHANGED
@@ -253,7 +253,7 @@ export class Logger {
253
253
  if (typeof value === "bigint") {
254
254
  return value.toString();
255
255
  }
256
- if (value instanceof Uint8Array) {
256
+ if (Bytes.isBytes(value)) {
257
257
  return Bytes.toHex(value);
258
258
  }
259
259
  if (value === undefined) {