@twin.org/crypto 0.0.1-next.41 → 0.0.1-next.43

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.
@@ -193,7 +193,7 @@ class Ed25519 {
193
193
  * @param privateKey The private key to convert.
194
194
  * @returns The private key in PKCS8 format.
195
195
  */
196
- static async privateKeyToPKCS8(privateKey) {
196
+ static async privateKeyToPkcs8(privateKey) {
197
197
  core.Guards.uint8Array(Ed25519._CLASS_NAME, "privateKey", privateKey);
198
198
  if (privateKey.length !== Ed25519.PRIVATE_KEY_SIZE) {
199
199
  throw new core.GeneralError(Ed25519._CLASS_NAME, "privateKeyLength", {
@@ -204,10 +204,22 @@ class Ed25519 {
204
204
  // crypto.subtle.importKey does not support Ed25519 keys in raw format.
205
205
  // We need to convert the key to PKCS8 format before importing.
206
206
  // The PKCS8 format is the raw key prefixed with the ASN.1 sequence for an Ed25519 private key.
207
- // The ASN.1 sequence is 48 46 02 01 00 30 05 06 03 2b 65 70 04 20 04 20
208
- const pkcs8Prefix = new Uint8Array([48, 46, 2, 1, 0, 48, 5, 6, 3, 43, 101, 112, 4, 34, 4, 32]); // 0x302e020100300506032b657004220420
207
+ // The ASN.1 sequence is 48 46 02 01 00 30 05 06 03 2b 65 70 04 20 04 20 (0x302e020100300506032b657004220420)
208
+ const pkcs8Prefix = new Uint8Array([48, 46, 2, 1, 0, 48, 5, 6, 3, 43, 101, 112, 4, 34, 4, 32]);
209
209
  const fullKey = core.Uint8ArrayHelper.concat([pkcs8Prefix, privateKey]);
210
- return crypto.subtle.importKey("pkcs8", fullKey, "Ed25519", false, ["sign"]);
210
+ return crypto.subtle.importKey("pkcs8", fullKey, "Ed25519", true, ["sign"]);
211
+ }
212
+ /**
213
+ * Convert a crypto key to raw private key.
214
+ * @param cryptoKey The crypto key to convert.
215
+ * @returns The raw private key.
216
+ */
217
+ static async pkcs8ToPrivateKey(cryptoKey) {
218
+ core.Guards.defined(Ed25519._CLASS_NAME, "cryptoKey", cryptoKey);
219
+ // crypto.subtle.exportKey does not support Ed25519 keys in raw format.
220
+ // so we export as PKCS8 and remove the ASN.1 sequence prefix.
221
+ const pkcs8Bytes = await crypto.subtle.exportKey("pkcs8", cryptoKey);
222
+ return new Uint8Array(pkcs8Bytes.slice(16));
211
223
  }
212
224
  }
213
225
 
@@ -171,7 +171,7 @@ class Ed25519 {
171
171
  * @param privateKey The private key to convert.
172
172
  * @returns The private key in PKCS8 format.
173
173
  */
174
- static async privateKeyToPKCS8(privateKey) {
174
+ static async privateKeyToPkcs8(privateKey) {
175
175
  Guards.uint8Array(Ed25519._CLASS_NAME, "privateKey", privateKey);
176
176
  if (privateKey.length !== Ed25519.PRIVATE_KEY_SIZE) {
177
177
  throw new GeneralError(Ed25519._CLASS_NAME, "privateKeyLength", {
@@ -182,10 +182,22 @@ class Ed25519 {
182
182
  // crypto.subtle.importKey does not support Ed25519 keys in raw format.
183
183
  // We need to convert the key to PKCS8 format before importing.
184
184
  // The PKCS8 format is the raw key prefixed with the ASN.1 sequence for an Ed25519 private key.
185
- // The ASN.1 sequence is 48 46 02 01 00 30 05 06 03 2b 65 70 04 20 04 20
186
- const pkcs8Prefix = new Uint8Array([48, 46, 2, 1, 0, 48, 5, 6, 3, 43, 101, 112, 4, 34, 4, 32]); // 0x302e020100300506032b657004220420
185
+ // The ASN.1 sequence is 48 46 02 01 00 30 05 06 03 2b 65 70 04 20 04 20 (0x302e020100300506032b657004220420)
186
+ const pkcs8Prefix = new Uint8Array([48, 46, 2, 1, 0, 48, 5, 6, 3, 43, 101, 112, 4, 34, 4, 32]);
187
187
  const fullKey = Uint8ArrayHelper.concat([pkcs8Prefix, privateKey]);
188
- return crypto.subtle.importKey("pkcs8", fullKey, "Ed25519", false, ["sign"]);
188
+ return crypto.subtle.importKey("pkcs8", fullKey, "Ed25519", true, ["sign"]);
189
+ }
190
+ /**
191
+ * Convert a crypto key to raw private key.
192
+ * @param cryptoKey The crypto key to convert.
193
+ * @returns The raw private key.
194
+ */
195
+ static async pkcs8ToPrivateKey(cryptoKey) {
196
+ Guards.defined(Ed25519._CLASS_NAME, "cryptoKey", cryptoKey);
197
+ // crypto.subtle.exportKey does not support Ed25519 keys in raw format.
198
+ // so we export as PKCS8 and remove the ASN.1 sequence prefix.
199
+ const pkcs8Bytes = await crypto.subtle.exportKey("pkcs8", cryptoKey);
200
+ return new Uint8Array(pkcs8Bytes.slice(16));
189
201
  }
190
202
  }
191
203
 
@@ -39,5 +39,11 @@ export declare class Ed25519 {
39
39
  * @param privateKey The private key to convert.
40
40
  * @returns The private key in PKCS8 format.
41
41
  */
42
- static privateKeyToPKCS8(privateKey: Uint8Array): Promise<CryptoKey>;
42
+ static privateKeyToPkcs8(privateKey: Uint8Array): Promise<CryptoKey>;
43
+ /**
44
+ * Convert a crypto key to raw private key.
45
+ * @param cryptoKey The crypto key to convert.
46
+ * @returns The raw private key.
47
+ */
48
+ static pkcs8ToPrivateKey(cryptoKey: CryptoKey): Promise<Uint8Array>;
43
49
  }
package/docs/changelog.md CHANGED
@@ -1,5 +1,5 @@
1
1
  # @twin.org/crypto - Changelog
2
2
 
3
- ## 0.0.1-next.41
3
+ ## 0.0.1-next.43
4
4
 
5
5
  - Added: Bip44
@@ -126,9 +126,9 @@ Error if the public key is not the correct length.
126
126
 
127
127
  ***
128
128
 
129
- ### privateKeyToPKCS8()
129
+ ### privateKeyToPkcs8()
130
130
 
131
- > `static` **privateKeyToPKCS8**(`privateKey`): `Promise`\<`CryptoKey`\>
131
+ > `static` **privateKeyToPkcs8**(`privateKey`): `Promise`\<`CryptoKey`\>
132
132
 
133
133
  Convert a private key in PKCS8 format.
134
134
 
@@ -145,3 +145,25 @@ The private key to convert.
145
145
  `Promise`\<`CryptoKey`\>
146
146
 
147
147
  The private key in PKCS8 format.
148
+
149
+ ***
150
+
151
+ ### pkcs8ToPrivateKey()
152
+
153
+ > `static` **pkcs8ToPrivateKey**(`cryptoKey`): `Promise`\<`Uint8Array`\>
154
+
155
+ Convert a crypto key to raw private key.
156
+
157
+ #### Parameters
158
+
159
+ ##### cryptoKey
160
+
161
+ `CryptoKey`
162
+
163
+ The crypto key to convert.
164
+
165
+ #### Returns
166
+
167
+ `Promise`\<`Uint8Array`\>
168
+
169
+ The raw private key.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@twin.org/crypto",
3
- "version": "0.0.1-next.41",
3
+ "version": "0.0.1-next.43",
4
4
  "description": "Contains helper methods and classes which implement cryptographic functions",
5
5
  "repository": {
6
6
  "type": "git",
@@ -20,7 +20,7 @@
20
20
  "@scure/base": "1.2.4",
21
21
  "@scure/bip32": "1.6.2",
22
22
  "@scure/bip39": "1.5.4",
23
- "@twin.org/core": "0.0.1-next.41",
23
+ "@twin.org/core": "0.0.1-next.43",
24
24
  "@twin.org/nameof": "next",
25
25
  "micro-key-producer": "0.7.5"
26
26
  },