@enbox/agent 0.1.3 → 0.1.4

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 (224) hide show
  1. package/dist/browser.mjs +45 -52
  2. package/dist/browser.mjs.map +4 -4
  3. package/dist/esm/crypto-api.js +6 -13
  4. package/dist/esm/crypto-api.js.map +1 -1
  5. package/dist/esm/dwn-api.js +80 -78
  6. package/dist/esm/dwn-api.js.map +1 -1
  7. package/dist/esm/hd-identity-vault.js +4 -5
  8. package/dist/esm/hd-identity-vault.js.map +1 -1
  9. package/dist/esm/identity-api.js +1 -1
  10. package/dist/esm/identity-api.js.map +1 -1
  11. package/dist/esm/index.js +0 -2
  12. package/dist/esm/index.js.map +1 -1
  13. package/dist/esm/local-key-manager.js +31 -32
  14. package/dist/esm/local-key-manager.js.map +1 -1
  15. package/dist/esm/prototyping/crypto/jose/jwe-compact.js +1 -2
  16. package/dist/esm/prototyping/crypto/jose/jwe-compact.js.map +1 -1
  17. package/dist/esm/prototyping/crypto/jose/jwe-flattened.js +15 -4
  18. package/dist/esm/prototyping/crypto/jose/jwe-flattened.js.map +1 -1
  19. package/dist/esm/prototyping/crypto/jose/jwe.js +1 -1
  20. package/dist/esm/prototyping/crypto/jose/jwe.js.map +1 -1
  21. package/dist/esm/store-data.js +3 -3
  22. package/dist/esm/store-data.js.map +1 -1
  23. package/dist/esm/store-did.js +1 -1
  24. package/dist/esm/store-did.js.map +1 -1
  25. package/dist/esm/sync-engine-level.js +49 -11
  26. package/dist/esm/sync-engine-level.js.map +1 -1
  27. package/dist/esm/test-harness.js +24 -8
  28. package/dist/esm/test-harness.js.map +1 -1
  29. package/dist/esm/types/dwn.js +1 -1
  30. package/dist/esm/types/dwn.js.map +1 -1
  31. package/dist/esm/web5-user-agent.js +1 -1
  32. package/dist/esm/web5-user-agent.js.map +1 -1
  33. package/dist/types/crypto-api.d.ts +8 -12
  34. package/dist/types/crypto-api.d.ts.map +1 -1
  35. package/dist/types/dwn-api.d.ts +10 -10
  36. package/dist/types/dwn-api.d.ts.map +1 -1
  37. package/dist/types/hd-identity-vault.d.ts.map +1 -1
  38. package/dist/types/identity-api.d.ts.map +1 -1
  39. package/dist/types/index.d.ts +0 -2
  40. package/dist/types/index.d.ts.map +1 -1
  41. package/dist/types/local-key-manager.d.ts +15 -18
  42. package/dist/types/local-key-manager.d.ts.map +1 -1
  43. package/dist/types/prototyping/crypto/jose/jwe-compact.d.ts +1 -3
  44. package/dist/types/prototyping/crypto/jose/jwe-compact.d.ts.map +1 -1
  45. package/dist/types/prototyping/crypto/jose/jwe-flattened.d.ts +1 -3
  46. package/dist/types/prototyping/crypto/jose/jwe-flattened.d.ts.map +1 -1
  47. package/dist/types/prototyping/crypto/jose/jwe.d.ts +1 -3
  48. package/dist/types/prototyping/crypto/jose/jwe.d.ts.map +1 -1
  49. package/dist/types/store-data.d.ts +2 -2
  50. package/dist/types/store-did.d.ts.map +1 -1
  51. package/dist/types/sync-engine-level.d.ts +18 -6
  52. package/dist/types/sync-engine-level.d.ts.map +1 -1
  53. package/dist/types/test-harness.d.ts.map +1 -1
  54. package/dist/types/types/agent.d.ts +1 -1
  55. package/dist/types/types/agent.d.ts.map +1 -1
  56. package/dist/types/types/dwn.d.ts +2 -2
  57. package/dist/types/types/dwn.d.ts.map +1 -1
  58. package/dist/types/types/key-manager.d.ts +15 -22
  59. package/dist/types/types/key-manager.d.ts.map +1 -1
  60. package/dist/types/utils-internal.d.ts +1 -1
  61. package/dist/types/utils-internal.d.ts.map +1 -1
  62. package/dist/types/web5-user-agent.d.ts +1 -1
  63. package/dist/types/web5-user-agent.d.ts.map +1 -1
  64. package/package.json +10 -12
  65. package/src/crypto-api.ts +24 -20
  66. package/src/dwn-api.ts +109 -102
  67. package/src/hd-identity-vault.ts +4 -5
  68. package/src/identity-api.ts +2 -1
  69. package/src/index.ts +0 -2
  70. package/src/local-key-manager.ts +43 -44
  71. package/src/prototyping/crypto/jose/jwe-compact.ts +3 -7
  72. package/src/prototyping/crypto/jose/jwe-flattened.ts +20 -9
  73. package/src/prototyping/crypto/jose/jwe.ts +2 -6
  74. package/src/store-data.ts +3 -3
  75. package/src/store-did.ts +1 -1
  76. package/src/sync-engine-level.ts +59 -16
  77. package/src/test-harness.ts +23 -8
  78. package/src/types/agent.ts +1 -1
  79. package/src/types/dwn.ts +2 -2
  80. package/src/types/key-manager.ts +31 -22
  81. package/src/utils-internal.ts +1 -1
  82. package/src/web5-user-agent.ts +2 -2
  83. package/dist/browser.js +0 -2213
  84. package/dist/browser.js.map +0 -7
  85. package/dist/esm/dwn-registrar.js +0 -120
  86. package/dist/esm/dwn-registrar.js.map +0 -1
  87. package/dist/esm/prototyping/clients/dwn-rpc-types.js +0 -2
  88. package/dist/esm/prototyping/clients/dwn-rpc-types.js.map +0 -1
  89. package/dist/esm/prototyping/clients/dwn-server-info-cache-memory.js +0 -74
  90. package/dist/esm/prototyping/clients/dwn-server-info-cache-memory.js.map +0 -1
  91. package/dist/esm/prototyping/clients/http-dwn-rpc-client.js +0 -112
  92. package/dist/esm/prototyping/clients/http-dwn-rpc-client.js.map +0 -1
  93. package/dist/esm/prototyping/clients/json-rpc-socket.js +0 -167
  94. package/dist/esm/prototyping/clients/json-rpc-socket.js.map +0 -1
  95. package/dist/esm/prototyping/clients/json-rpc.js +0 -58
  96. package/dist/esm/prototyping/clients/json-rpc.js.map +0 -1
  97. package/dist/esm/prototyping/clients/server-info-types.js +0 -2
  98. package/dist/esm/prototyping/clients/server-info-types.js.map +0 -1
  99. package/dist/esm/prototyping/clients/web-socket-clients.js +0 -90
  100. package/dist/esm/prototyping/clients/web-socket-clients.js.map +0 -1
  101. package/dist/esm/prototyping/common/object.js +0 -14
  102. package/dist/esm/prototyping/common/object.js.map +0 -1
  103. package/dist/esm/prototyping/common/type-utils.js +0 -2
  104. package/dist/esm/prototyping/common/type-utils.js.map +0 -1
  105. package/dist/esm/prototyping/crypto/algorithms/aes-gcm.js +0 -147
  106. package/dist/esm/prototyping/crypto/algorithms/aes-gcm.js.map +0 -1
  107. package/dist/esm/prototyping/crypto/algorithms/aes-kw.js +0 -136
  108. package/dist/esm/prototyping/crypto/algorithms/aes-kw.js.map +0 -1
  109. package/dist/esm/prototyping/crypto/algorithms/ecdsa.js +0 -311
  110. package/dist/esm/prototyping/crypto/algorithms/ecdsa.js.map +0 -1
  111. package/dist/esm/prototyping/crypto/algorithms/eddsa.js +0 -268
  112. package/dist/esm/prototyping/crypto/algorithms/eddsa.js.map +0 -1
  113. package/dist/esm/prototyping/crypto/algorithms/hkdf.js +0 -38
  114. package/dist/esm/prototyping/crypto/algorithms/hkdf.js.map +0 -1
  115. package/dist/esm/prototyping/crypto/algorithms/pbkdf2.js +0 -40
  116. package/dist/esm/prototyping/crypto/algorithms/pbkdf2.js.map +0 -1
  117. package/dist/esm/prototyping/crypto/crypto-error.js +0 -41
  118. package/dist/esm/prototyping/crypto/crypto-error.js.map +0 -1
  119. package/dist/esm/prototyping/crypto/types/crypto-api.js +0 -2
  120. package/dist/esm/prototyping/crypto/types/crypto-api.js.map +0 -1
  121. package/dist/esm/prototyping/crypto/types/key-converter.js +0 -2
  122. package/dist/esm/prototyping/crypto/types/key-converter.js.map +0 -1
  123. package/dist/esm/prototyping/crypto/types/key-deriver.js +0 -2
  124. package/dist/esm/prototyping/crypto/types/key-deriver.js.map +0 -1
  125. package/dist/esm/prototyping/crypto/types/key-io.js +0 -2
  126. package/dist/esm/prototyping/crypto/types/key-io.js.map +0 -1
  127. package/dist/esm/prototyping/crypto/types/key-manager.js +0 -2
  128. package/dist/esm/prototyping/crypto/types/key-manager.js.map +0 -1
  129. package/dist/esm/prototyping/crypto/types/params-direct.js +0 -2
  130. package/dist/esm/prototyping/crypto/types/params-direct.js.map +0 -1
  131. package/dist/esm/prototyping/crypto/types/params-kms.js +0 -2
  132. package/dist/esm/prototyping/crypto/types/params-kms.js.map +0 -1
  133. package/dist/esm/prototyping/crypto/utils.js +0 -19
  134. package/dist/esm/prototyping/crypto/utils.js.map +0 -1
  135. package/dist/esm/prototyping/dids/resolver-cache-memory.js +0 -77
  136. package/dist/esm/prototyping/dids/resolver-cache-memory.js.map +0 -1
  137. package/dist/esm/prototyping/dids/utils.js +0 -9
  138. package/dist/esm/prototyping/dids/utils.js.map +0 -1
  139. package/dist/esm/rpc-client.js +0 -123
  140. package/dist/esm/rpc-client.js.map +0 -1
  141. package/dist/types/dwn-registrar.d.ts +0 -29
  142. package/dist/types/dwn-registrar.d.ts.map +0 -1
  143. package/dist/types/prototyping/clients/dwn-rpc-types.d.ts +0 -45
  144. package/dist/types/prototyping/clients/dwn-rpc-types.d.ts.map +0 -1
  145. package/dist/types/prototyping/clients/dwn-server-info-cache-memory.d.ts +0 -57
  146. package/dist/types/prototyping/clients/dwn-server-info-cache-memory.d.ts.map +0 -1
  147. package/dist/types/prototyping/clients/http-dwn-rpc-client.d.ts +0 -13
  148. package/dist/types/prototyping/clients/http-dwn-rpc-client.d.ts.map +0 -1
  149. package/dist/types/prototyping/clients/json-rpc-socket.d.ts +0 -43
  150. package/dist/types/prototyping/clients/json-rpc-socket.d.ts.map +0 -1
  151. package/dist/types/prototyping/clients/json-rpc.d.ts +0 -49
  152. package/dist/types/prototyping/clients/json-rpc.d.ts.map +0 -1
  153. package/dist/types/prototyping/clients/server-info-types.d.ts +0 -20
  154. package/dist/types/prototyping/clients/server-info-types.d.ts.map +0 -1
  155. package/dist/types/prototyping/clients/web-socket-clients.d.ts +0 -10
  156. package/dist/types/prototyping/clients/web-socket-clients.d.ts.map +0 -1
  157. package/dist/types/prototyping/common/object.d.ts +0 -2
  158. package/dist/types/prototyping/common/object.d.ts.map +0 -1
  159. package/dist/types/prototyping/common/type-utils.d.ts +0 -7
  160. package/dist/types/prototyping/common/type-utils.d.ts.map +0 -1
  161. package/dist/types/prototyping/crypto/algorithms/aes-gcm.d.ts +0 -151
  162. package/dist/types/prototyping/crypto/algorithms/aes-gcm.d.ts.map +0 -1
  163. package/dist/types/prototyping/crypto/algorithms/aes-kw.d.ts +0 -108
  164. package/dist/types/prototyping/crypto/algorithms/aes-kw.d.ts.map +0 -1
  165. package/dist/types/prototyping/crypto/algorithms/ecdsa.d.ts +0 -160
  166. package/dist/types/prototyping/crypto/algorithms/ecdsa.d.ts.map +0 -1
  167. package/dist/types/prototyping/crypto/algorithms/eddsa.d.ts +0 -157
  168. package/dist/types/prototyping/crypto/algorithms/eddsa.d.ts.map +0 -1
  169. package/dist/types/prototyping/crypto/algorithms/hkdf.d.ts +0 -20
  170. package/dist/types/prototyping/crypto/algorithms/hkdf.d.ts.map +0 -1
  171. package/dist/types/prototyping/crypto/algorithms/pbkdf2.d.ts +0 -20
  172. package/dist/types/prototyping/crypto/algorithms/pbkdf2.d.ts.map +0 -1
  173. package/dist/types/prototyping/crypto/crypto-error.d.ts +0 -29
  174. package/dist/types/prototyping/crypto/crypto-error.d.ts.map +0 -1
  175. package/dist/types/prototyping/crypto/types/crypto-api.d.ts +0 -34
  176. package/dist/types/prototyping/crypto/types/crypto-api.d.ts.map +0 -1
  177. package/dist/types/prototyping/crypto/types/key-converter.d.ts +0 -49
  178. package/dist/types/prototyping/crypto/types/key-converter.d.ts.map +0 -1
  179. package/dist/types/prototyping/crypto/types/key-deriver.d.ts +0 -50
  180. package/dist/types/prototyping/crypto/types/key-deriver.d.ts.map +0 -1
  181. package/dist/types/prototyping/crypto/types/key-io.d.ts +0 -49
  182. package/dist/types/prototyping/crypto/types/key-io.d.ts.map +0 -1
  183. package/dist/types/prototyping/crypto/types/key-manager.d.ts +0 -69
  184. package/dist/types/prototyping/crypto/types/key-manager.d.ts.map +0 -1
  185. package/dist/types/prototyping/crypto/types/params-direct.d.ts +0 -75
  186. package/dist/types/prototyping/crypto/types/params-direct.d.ts.map +0 -1
  187. package/dist/types/prototyping/crypto/types/params-kms.d.ts +0 -63
  188. package/dist/types/prototyping/crypto/types/params-kms.d.ts.map +0 -1
  189. package/dist/types/prototyping/crypto/utils.d.ts +0 -7
  190. package/dist/types/prototyping/crypto/utils.d.ts.map +0 -1
  191. package/dist/types/prototyping/dids/resolver-cache-memory.d.ts +0 -57
  192. package/dist/types/prototyping/dids/resolver-cache-memory.d.ts.map +0 -1
  193. package/dist/types/prototyping/dids/utils.d.ts +0 -3
  194. package/dist/types/prototyping/dids/utils.d.ts.map +0 -1
  195. package/dist/types/rpc-client.d.ts +0 -51
  196. package/dist/types/rpc-client.d.ts.map +0 -1
  197. package/src/dwn-registrar.ts +0 -127
  198. package/src/prototyping/clients/dwn-rpc-types.ts +0 -55
  199. package/src/prototyping/clients/dwn-server-info-cache-memory.ts +0 -79
  200. package/src/prototyping/clients/http-dwn-rpc-client.ts +0 -119
  201. package/src/prototyping/clients/json-rpc-socket.ts +0 -189
  202. package/src/prototyping/clients/json-rpc.ts +0 -113
  203. package/src/prototyping/clients/server-info-types.ts +0 -21
  204. package/src/prototyping/clients/web-socket-clients.ts +0 -103
  205. package/src/prototyping/common/object.ts +0 -15
  206. package/src/prototyping/common/type-utils.ts +0 -6
  207. package/src/prototyping/crypto/algorithms/aes-gcm.ts +0 -211
  208. package/src/prototyping/crypto/algorithms/aes-kw.ts +0 -160
  209. package/src/prototyping/crypto/algorithms/ecdsa.ts +0 -366
  210. package/src/prototyping/crypto/algorithms/eddsa.ts +0 -311
  211. package/src/prototyping/crypto/algorithms/hkdf.ts +0 -38
  212. package/src/prototyping/crypto/algorithms/pbkdf2.ts +0 -41
  213. package/src/prototyping/crypto/crypto-error.ts +0 -45
  214. package/src/prototyping/crypto/types/crypto-api.ts +0 -77
  215. package/src/prototyping/crypto/types/key-converter.ts +0 -53
  216. package/src/prototyping/crypto/types/key-deriver.ts +0 -56
  217. package/src/prototyping/crypto/types/key-io.ts +0 -51
  218. package/src/prototyping/crypto/types/key-manager.ts +0 -83
  219. package/src/prototyping/crypto/types/params-direct.ts +0 -95
  220. package/src/prototyping/crypto/types/params-kms.ts +0 -76
  221. package/src/prototyping/crypto/utils.ts +0 -41
  222. package/src/prototyping/dids/resolver-cache-memory.ts +0 -83
  223. package/src/prototyping/dids/utils.ts +0 -10
  224. package/src/rpc-client.ts +0 -160
@@ -1,160 +0,0 @@
1
- import type { RequireOnly } from '@enbox/common';
2
- import type { BytesToPrivateKeyParams, GenerateKeyParams, Jwk, KeyConverter, KeyGenerator, KeyWrapper, PrivateKeyToBytesParams, UnwrapKeyParams, WrapKeyParams } from '@enbox/crypto';
3
-
4
- import { AesKw, CryptoAlgorithm } from '@enbox/crypto';
5
-
6
- /**
7
- * The `AesKwGenerateKeyParams` interface defines the algorithm-specific parameters that should be
8
- * passed into the `generateKey()` method when using the AES-KW algorithm.
9
- */
10
- export interface AesKwGenerateKeyParams extends GenerateKeyParams {
11
- /** Specifies the algorithm variant for key generation in AES-KW mode.
12
- * The value determines the length of the key to be generated and must be one of the following:
13
- * - `"A128KW"`: AES Key Wrap using a 128-bit key.
14
- * - `"A192KW"`: AES Key Wrap using a 192-bit key.
15
- * - `"A256KW"`: AES Key Wrap using a 256-bit key.
16
- */
17
- algorithm: 'A128KW' | 'A192KW' | 'A256KW';
18
- }
19
-
20
- /**
21
- * The `AesKwAlgorithm` class provides a concrete implementation for cryptographic operations using
22
- * the AES algorithm for key wrapping. This class implements both
23
- * {@link KeyGenerator | `KeyGenerator`} and {@link KeyWrapper | `KeyWrapper`} interfaces, providing
24
- * key generation, key wrapping, and key unwrapping features.
25
- *
26
- * This class is typically accessed through implementations that extend the
27
- * {@link CryptoApi | `CryptoApi`} interface.
28
- */
29
- export class AesKwAlgorithm extends CryptoAlgorithm
30
- implements KeyConverter,
31
- KeyGenerator<AesKwGenerateKeyParams, Jwk>,
32
- KeyWrapper<WrapKeyParams, UnwrapKeyParams> {
33
-
34
- public async bytesToPrivateKey({ privateKeyBytes }:
35
- RequireOnly<BytesToPrivateKeyParams, 'privateKeyBytes'>
36
- ): Promise<Jwk> {
37
- // Convert the byte array to a JWK.
38
- const privateKey = await AesKw.bytesToPrivateKey({ privateKeyBytes });
39
-
40
- // Set the `alg` property based on the key length.
41
- privateKey.alg = { 16: 'A128KW', 24: 'A192KW', 32: 'A256KW' }[privateKeyBytes.length];
42
-
43
- return privateKey;
44
- }
45
-
46
- /**
47
- * Generates a symmetric key for AES for key wrapping in JSON Web Key (JWK) format.
48
- *
49
- * @remarks
50
- * This method generates a symmetric AES key for use in key wrapping mode, based on the specified
51
- * `algorithm` parameter which determines the key length. It uses cryptographically secure random
52
- * number generation to ensure the uniqueness and security of the key. The key is returned in JWK
53
- * format.
54
- *
55
- * The generated key includes the following components:
56
- * - `kty`: Key Type, set to 'oct' for Octet Sequence.
57
- * - `k`: The symmetric key component, base64url-encoded.
58
- * - `kid`: Key ID, generated based on the JWK thumbprint.
59
- * - `alg`: Algorithm, set to 'A128KW', 'A192KW', or 'A256KW' for AES Key Wrap with the
60
- * specified key length.
61
- *
62
- * @example
63
- * ```ts
64
- * const aesKw = new AesKwAlgorithm();
65
- * const privateKey = await aesKw.generateKey({ algorithm: 'A256KW' });
66
- * ```
67
- *
68
- * @param params - The parameters for the key generation.
69
- *
70
- * @returns A Promise that resolves to the generated symmetric key in JWK format.
71
- */
72
- public async generateKey({ algorithm }:
73
- AesKwGenerateKeyParams
74
- ): Promise<Jwk> {
75
- // Map algorithm name to key length.
76
- const length = { A128KW: 128, A192KW: 192, A256KW: 256 }[algorithm] as 128 | 192 | 256;
77
-
78
- // Generate a random private key.
79
- const privateKey = await AesKw.generateKey({ length });
80
-
81
- // Set the `alg` property based on the specified algorithm.
82
- privateKey.alg = algorithm;
83
-
84
- return privateKey;
85
- }
86
-
87
- public async privateKeyToBytes({ privateKey }:
88
- PrivateKeyToBytesParams
89
- ): Promise<Uint8Array> {
90
- // Convert the JWK to a byte array.
91
- const privateKeyBytes = await AesKw.privateKeyToBytes({ privateKey });
92
-
93
- return privateKeyBytes;
94
- }
95
-
96
- /**
97
- * Decrypts a wrapped key using the AES Key Wrap algorithm.
98
- *
99
- * @remarks
100
- * This method unwraps a previously wrapped cryptographic key using the AES Key Wrap algorithm.
101
- * The wrapped key, provided as a byte array, is unwrapped using the decryption key specified in
102
- * the parameters.
103
- *
104
- * This operation is useful for securely receiving keys transmitted over untrusted mediums. The
105
- * method returns the unwrapped key as a JSON Web Key (JWK).
106
- *
107
- * @example
108
- * ```ts
109
- * const aesKw = new AesKwAlgorithm();
110
- * const wrappedKeyBytes = new Uint8Array([...]); // Byte array of a wrapped AES-256 GCM key
111
- * const decryptionKey = { ... }; // A Jwk object representing the AES unwrapping key
112
- * const unwrappedKey = await aesKw.unwrapKey({
113
- * wrappedKeyBytes,
114
- * wrappedKeyAlgorithm: 'A256GCM',
115
- * decryptionKey
116
- * });
117
- * ```
118
- *
119
- * @param params - The parameters for the key unwrapping operation.
120
- *
121
- * @returns A Promise that resolves to the unwrapped key in JWK format.
122
- */
123
- public async unwrapKey(params:
124
- UnwrapKeyParams
125
- ): Promise<Jwk> {
126
- const unwrappedKey = await AesKw.unwrapKey(params);
127
-
128
- return unwrappedKey;
129
- }
130
-
131
- /**
132
- * Encrypts a given key using the AES Key Wrap algorithm.
133
- *
134
- * @remarks
135
- * This method wraps a given cryptographic key using the AES Key Wrap algorithm. The private key
136
- * to be wrapped is provided in the form of a JSON Web Key (JWK).
137
- *
138
- * This operation is useful for securely transmitting keys over untrusted mediums. The method
139
- * returns the wrapped key as a byte array.
140
- *
141
- * @example
142
- * ```ts
143
- * const aesKw = new AesKwAlgorithm();
144
- * const unwrappedKey = { ... }; // A Jwk object representing the key to be wrapped
145
- * const encryptionKey = { ... }; // A Jwk object representing the AES wrapping key
146
- * const wrappedKeyBytes = await aesKw.wrapKey({ unwrappedKey, encryptionKey });
147
- * ```
148
- *
149
- * @param params - The parameters for the key wrapping operation.
150
- *
151
- * @returns A Promise that resolves to the wrapped key as a Uint8Array.
152
- */
153
- public async wrapKey(params:
154
- WrapKeyParams
155
- ): Promise<Uint8Array> {
156
- const wrappedKeyBytes = AesKw.wrapKey(params);
157
-
158
- return wrappedKeyBytes;
159
- }
160
- }
@@ -1,366 +0,0 @@
1
- import type {
2
- AsymmetricKeyConverter,
3
- AsymmetricKeyGenerator,
4
- ComputePublicKeyParams,
5
- GenerateKeyParams,
6
- GetPublicKeyParams,
7
- Jwk,
8
- KeyConverter,
9
- Signer,
10
- SignParams,
11
- VerifyParams,
12
- } from '@enbox/crypto';
13
-
14
- import { CryptoAlgorithm, isEcPrivateJwk, isEcPublicJwk, Secp256k1, Secp256r1 } from '@enbox/crypto';
15
-
16
- import type { BytesToPrivateKeyParams, BytesToPublicKeyParams, PrivateKeyToBytesParams, PublicKeyToBytesParams } from '../types/params-direct.js';
17
- import { CryptoError, CryptoErrorCode } from '../crypto-error.js';
18
-
19
- /**
20
- * The `EcdsaGenerateKeyParams` interface defines the algorithm-specific parameters that should be
21
- * passed into the `generateKey()` method when using the ECDSA algorithm.
22
- */
23
- export interface EcdsaGenerateKeyParams extends GenerateKeyParams {
24
- /**
25
- * A string defining the type of key to generate. The value must be one of the following:
26
- * - `"ES256"`: ECDSA using the secp256r1 (P-256) curve and SHA-256.
27
- * - `"ES256K"`: ECDSA using the secp256k1 curve and SHA-256.
28
- * - `"secp256k1"`: ECDSA using the secp256k1 curve and SHA-256.
29
- * - `"secp256r1"`: ECDSA using the secp256r1 (P-256) curve and SHA-256.
30
- */
31
- algorithm: 'ES256' | 'ES256K' | 'secp256k1' | 'secp256r1';
32
- }
33
-
34
- /**
35
- * The `EcdsaAlgorithm` class provides a concrete implementation for cryptographic operations using
36
- * the Elliptic Curve Digital Signature Algorithm (ECDSA). This class implements both
37
- * {@link Signer | `Signer`} and { @link AsymmetricKeyGenerator | `AsymmetricKeyGenerator`}
38
- * interfaces, providing private key generation, public key derivation, and creation/verification
39
- * of signatures.
40
- *
41
- * This class is typically accessed through implementations that extend the
42
- * {@link CryptoApi | `CryptoApi`} interface.
43
- */
44
- export class EcdsaAlgorithm extends CryptoAlgorithm
45
- implements AsymmetricKeyGenerator<EcdsaGenerateKeyParams, Jwk, GetPublicKeyParams>,
46
- KeyConverter, AsymmetricKeyConverter,
47
- Signer<SignParams, VerifyParams> {
48
-
49
- public async bytesToPrivateKey({ algorithm, privateKeyBytes }:
50
- BytesToPrivateKeyParams & { algorithm: 'ES256' | 'ES256K' | 'secp256k1' | 'secp256r1' }
51
- ): Promise<Jwk> {
52
- switch (algorithm) {
53
-
54
- case 'ES256K':
55
- case 'secp256k1': {
56
- const privateKey = await Secp256k1.bytesToPrivateKey({ privateKeyBytes });
57
- privateKey.alg = 'EdDSA';
58
- return privateKey;
59
- }
60
-
61
- case 'ES256':
62
- case 'secp256r1': {
63
- const privateKey = await Secp256r1.bytesToPrivateKey({ privateKeyBytes });
64
- privateKey.alg = 'EdDSA';
65
- return privateKey;
66
- }
67
-
68
- default: {
69
- throw new CryptoError(CryptoErrorCode.AlgorithmNotSupported, `Algorithm not supported: ${algorithm}`);
70
- }
71
- }
72
- }
73
-
74
- public async bytesToPublicKey({ algorithm, publicKeyBytes }:
75
- BytesToPublicKeyParams & { algorithm: 'ES256' | 'ES256K' | 'secp256k1' | 'secp256r1' }
76
- ): Promise<Jwk> {
77
- switch (algorithm) {
78
-
79
- case 'ES256K':
80
- case 'secp256k1': {
81
- const publicKey = await Secp256k1.bytesToPublicKey({ publicKeyBytes });
82
- publicKey.alg = 'EdDSA';
83
- return publicKey;
84
- }
85
-
86
- case 'ES256':
87
- case 'secp256r1': {
88
- const publicKey = await Secp256r1.bytesToPublicKey({ publicKeyBytes });
89
- publicKey.alg = 'EdDSA';
90
- return publicKey;
91
- }
92
-
93
- default: {
94
- throw new CryptoError(CryptoErrorCode.AlgorithmNotSupported, `Algorithm not supported: ${algorithm}`);
95
- }
96
- }
97
- }
98
-
99
- /**
100
- * Derives the public key in JWK format from a given private key.
101
- *
102
- * @remarks
103
- * This method takes a private key in JWK format and derives its corresponding public key,
104
- * also in JWK format. The process ensures that the derived public key correctly corresponds to
105
- * the given private key.
106
- *
107
- * @example
108
- * ```ts
109
- * const ecdsa = new EcdsaAlgorithm();
110
- * const privateKey = { ... }; // A Jwk object representing a private key
111
- * const publicKey = await ecdsa.computePublicKey({ key: privateKey });
112
- * ```
113
- *
114
- * @param params - The parameters for the public key derivation.
115
- * @param params.key - The private key in JWK format from which to derive the public key.
116
- *
117
- * @returns A Promise that resolves to the derived public key in JWK format.
118
- */
119
- public async computePublicKey({ key }:
120
- ComputePublicKeyParams
121
- ): Promise<Jwk> {
122
- if (!isEcPrivateJwk(key)) {throw new TypeError('Invalid key provided. Must be an elliptic curve (EC) private key.');}
123
-
124
- switch (key.crv) {
125
-
126
- case 'secp256k1': {
127
- const publicKey = await Secp256k1.computePublicKey({ key });
128
- publicKey.alg = 'ES256K';
129
- return publicKey;
130
- }
131
-
132
- case 'P-256': {
133
- const publicKey = await Secp256r1.computePublicKey({ key });
134
- publicKey.alg = 'ES256';
135
- return publicKey;
136
- }
137
-
138
- default: {
139
- throw new Error(`Unsupported curve: ${key.crv}`);
140
- }
141
- }
142
- }
143
-
144
- /**
145
- * Generates a new private key with the specified algorithm in JSON Web Key (JWK) format.
146
- *
147
- * @example
148
- * ```ts
149
- * const ecdsa = new EcdsaAlgorithm();
150
- * const privateKey = await ecdsa.generateKey({ algorithm: 'ES256K' });
151
- * ```
152
- *
153
- * @param params - The parameters for key generation.
154
- * @param params.algorithm - The algorithm to use for key generation.
155
- *
156
- * @returns A Promise that resolves to the generated private key in JWK format.
157
- */
158
- public async generateKey({ algorithm }:
159
- EcdsaGenerateKeyParams
160
- ): Promise<Jwk> {
161
- switch (algorithm) {
162
-
163
- case 'ES256K':
164
- case 'secp256k1': {
165
- const privateKey = await Secp256k1.generateKey();
166
- privateKey.alg = 'ES256K';
167
- return privateKey;
168
- }
169
-
170
- case 'ES256':
171
- case 'secp256r1': {
172
- const privateKey = await Secp256r1.generateKey();
173
- privateKey.alg = 'ES256';
174
- return privateKey;
175
- }
176
- }
177
- }
178
-
179
- /**
180
- * Retrieves the public key properties from a given private key in JWK format.
181
- *
182
- * @remarks
183
- * This method extracts the public key portion from an ECDSA private key in JWK format. It does
184
- * so by removing the private key property 'd' and making a shallow copy, effectively yielding the
185
- * public key.
186
- *
187
- * Note: This method offers a significant performance advantage, being about 200 times faster
188
- * than `computePublicKey()`. However, it does not mathematically validate the private key, nor
189
- * does it derive the public key from the private key. It simply extracts existing public key
190
- * properties from the private key object. This makes it suitable for scenarios where speed is
191
- * critical and the private key's integrity is already assured.
192
- *
193
- * @example
194
- * ```ts
195
- * const ecdsa = new EcdsaAlgorithm();
196
- * const privateKey = { ... }; // A Jwk object representing a private key
197
- * const publicKey = await ecdsa.getPublicKey({ key: privateKey });
198
- * ```
199
- *
200
- * @param params - The parameters for retrieving the public key properties.
201
- * @param params.key - The private key in JWK format.
202
- *
203
- * @returns A Promise that resolves to the public key in JWK format.
204
- */
205
- public async getPublicKey({ key }:
206
- GetPublicKeyParams
207
- ): Promise<Jwk> {
208
- if (!isEcPrivateJwk(key)) {throw new TypeError('Invalid key provided. Must be an elliptic curve (EC) private key.');}
209
-
210
- switch (key.crv) {
211
-
212
- case 'secp256k1': {
213
- const publicKey = await Secp256k1.getPublicKey({ key });
214
- publicKey.alg = 'ES256K';
215
- return publicKey;
216
- }
217
-
218
- case 'P-256': {
219
- const publicKey = await Secp256r1.getPublicKey({ key });
220
- publicKey.alg = 'ES256';
221
- return publicKey;
222
- }
223
-
224
- default: {
225
- throw new Error(`Unsupported curve: ${key.crv}`);
226
- }
227
- }
228
- }
229
-
230
- public async privateKeyToBytes({ privateKey }:
231
- PrivateKeyToBytesParams
232
- ): Promise<Uint8Array> {
233
- switch (privateKey.crv) {
234
-
235
- case 'secp256k1': {
236
- return await Secp256k1.privateKeyToBytes({ privateKey });
237
- }
238
-
239
- case 'P-256': {
240
- return await Secp256r1.privateKeyToBytes({ privateKey });
241
- }
242
-
243
- default: {
244
- throw new CryptoError(CryptoErrorCode.AlgorithmNotSupported, `Curve not supported: ${privateKey.crv}`);
245
- }
246
- }
247
- }
248
-
249
- public async publicKeyToBytes({ publicKey }:
250
- PublicKeyToBytesParams
251
- ): Promise<Uint8Array> {
252
- switch (publicKey.crv) {
253
-
254
- case 'secp256k1': {
255
- return await Secp256k1.publicKeyToBytes({ publicKey });
256
- }
257
-
258
- case 'P-256': {
259
- return await Secp256r1.publicKeyToBytes({ publicKey });
260
- }
261
-
262
- default: {
263
- throw new CryptoError(CryptoErrorCode.AlgorithmNotSupported, `Curve not supported: ${publicKey.crv}`);
264
- }
265
- }
266
- }
267
-
268
- /**
269
- * Generates an ECDSA signature of given data using a private key.
270
- *
271
- * @remarks
272
- * This method uses the signature algorithm determined by the given `algorithm` to sign the
273
- * provided data.
274
- *
275
- * The signature can later be verified by parties with access to the corresponding
276
- * public key, ensuring that the data has not been tampered with and was indeed signed by the
277
- * holder of the private key.
278
- *
279
- * @example
280
- * ```ts
281
- * const ecdsa = new EcdsaAlgorithm();
282
- * const data = new TextEncoder().encode('Message');
283
- * const privateKey = { ... }; // A Jwk object representing a private key
284
- * const signature = await ecdsa.sign({
285
- * key: privateKey,
286
- * data
287
- * });
288
- * ```
289
- *
290
- * @param params - The parameters for the signing operation.
291
- * @param params.key - The private key to use for signing, represented in JWK format.
292
- * @param params.data - The data to sign.
293
- *
294
- * @returns A Promise resolving to the digital signature as a `Uint8Array`.
295
- */
296
- public async sign({ key, data }:
297
- SignParams
298
- ): Promise<Uint8Array> {
299
- if (!isEcPrivateJwk(key)) {throw new TypeError('Invalid key provided. Must be an elliptic curve (EC) private key.');}
300
-
301
- switch (key.crv) {
302
-
303
- case 'secp256k1': {
304
- return await Secp256k1.sign({ key, data });
305
- }
306
-
307
- case 'P-256': {
308
- return await Secp256r1.sign({ key, data });
309
- }
310
-
311
- default: {
312
- throw new Error(`Unsupported curve: ${key.crv}`);
313
- }
314
- }
315
- }
316
-
317
- /**
318
- * Verifies an ECDSA signature associated with the provided data using the provided key.
319
- *
320
- * @remarks
321
- * This method uses the signature algorithm determined by the `crv` property of the provided key
322
- * to check the validity of a digital signature against the original data. It confirms whether the
323
- * signature was created by the holder of the corresponding private key and that the data has not
324
- * been tampered with.
325
- *s
326
- * @example
327
- * ```ts
328
- * const ecdsa = new EcdsaAlgorithm();
329
- * const publicKey = { ... }; // Public key in JWK format corresponding to the private key that signed the data
330
- * const signature = new Uint8Array([...]); // Signature to verify
331
- * const data = new TextEncoder().encode('Message');
332
- * const isValid = await ecdsa.verify({
333
- * key: publicKey,
334
- * signature,
335
- * data
336
- * });
337
- * ```
338
- *
339
- * @param params - The parameters for the verification operation.
340
- * @param params.key - The key to use for verification.
341
- * @param params.signature - The signature to verify.
342
- * @param params.data - The data to verify.
343
- *
344
- * @returns A Promise resolving to a boolean indicating whether the signature is valid.
345
- */
346
- public async verify({ key, signature, data }:
347
- VerifyParams
348
- ): Promise<boolean> {
349
- if (!isEcPublicJwk(key)) {throw new TypeError('Invalid key provided. Must be an elliptic curve (EC) public key.');}
350
-
351
- switch (key.crv) {
352
-
353
- case 'secp256k1': {
354
- return await Secp256k1.verify({ key, signature, data });
355
- }
356
-
357
- case 'P-256': {
358
- return await Secp256r1.verify({ key, signature, data });
359
- }
360
-
361
- default: {
362
- throw new Error(`Unsupported curve: ${key.crv}`);
363
- }
364
- }
365
- }
366
- }