@twin.org/crypto 0.0.1-next.38 → 0.0.1-next.39

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.
@@ -188,6 +188,27 @@ class Ed25519 {
188
188
  return false;
189
189
  }
190
190
  }
191
+ /**
192
+ * Convert a private key in PKCS8 format.
193
+ * @param privateKey The private key to convert.
194
+ * @returns The private key in PKCS8 format.
195
+ */
196
+ static async privateKeyToPKCS8(privateKey) {
197
+ core.Guards.uint8Array(Ed25519._CLASS_NAME, "privateKey", privateKey);
198
+ if (privateKey.length !== Ed25519.PRIVATE_KEY_SIZE) {
199
+ throw new core.GeneralError(Ed25519._CLASS_NAME, "privateKeyLength", {
200
+ requiredSize: Ed25519.PRIVATE_KEY_SIZE,
201
+ actualSize: privateKey.length
202
+ });
203
+ }
204
+ // crypto.subtle.importKey does not support Ed25519 keys in raw format.
205
+ // We need to convert the key to PKCS8 format before importing.
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
209
+ const fullKey = core.Uint8ArrayHelper.concat([pkcs8Prefix, privateKey]);
210
+ return crypto.subtle.importKey("pkcs8", fullKey, "Ed25519", false, ["sign"]);
211
+ }
191
212
  }
192
213
 
193
214
  // Copyright 2024 IOTA Stiftung.
@@ -1,5 +1,5 @@
1
1
  import { bech32 } from '@scure/base';
2
- import { Guards, BaseError, GeneralError, Is, Converter, GuardError, Base32, RandomHelper, Validation } from '@twin.org/core';
2
+ import { Guards, BaseError, GeneralError, Is, Uint8ArrayHelper, Converter, GuardError, Base32, RandomHelper, Validation } from '@twin.org/core';
3
3
  import { ed25519, edwardsToMontgomeryPriv, edwardsToMontgomeryPub } from '@noble/curves/ed25519';
4
4
  import { secp256k1 } from '@noble/curves/secp256k1';
5
5
  import { blake2b } from '@noble/hashes/blake2b';
@@ -166,6 +166,27 @@ class Ed25519 {
166
166
  return false;
167
167
  }
168
168
  }
169
+ /**
170
+ * Convert a private key in PKCS8 format.
171
+ * @param privateKey The private key to convert.
172
+ * @returns The private key in PKCS8 format.
173
+ */
174
+ static async privateKeyToPKCS8(privateKey) {
175
+ Guards.uint8Array(Ed25519._CLASS_NAME, "privateKey", privateKey);
176
+ if (privateKey.length !== Ed25519.PRIVATE_KEY_SIZE) {
177
+ throw new GeneralError(Ed25519._CLASS_NAME, "privateKeyLength", {
178
+ requiredSize: Ed25519.PRIVATE_KEY_SIZE,
179
+ actualSize: privateKey.length
180
+ });
181
+ }
182
+ // crypto.subtle.importKey does not support Ed25519 keys in raw format.
183
+ // We need to convert the key to PKCS8 format before importing.
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
187
+ const fullKey = Uint8ArrayHelper.concat([pkcs8Prefix, privateKey]);
188
+ return crypto.subtle.importKey("pkcs8", fullKey, "Ed25519", false, ["sign"]);
189
+ }
169
190
  }
170
191
 
171
192
  // Copyright 2024 IOTA Stiftung.
@@ -34,4 +34,10 @@ export declare class Ed25519 {
34
34
  * @throws Error if the public key is not the correct length.
35
35
  */
36
36
  static verify(publicKey: Uint8Array, block: Uint8Array, signature: Uint8Array): boolean;
37
+ /**
38
+ * Convert a private key in PKCS8 format.
39
+ * @param privateKey The private key to convert.
40
+ * @returns The private key in PKCS8 format.
41
+ */
42
+ static privateKeyToPKCS8(privateKey: Uint8Array): Promise<CryptoKey>;
37
43
  }
package/docs/changelog.md CHANGED
@@ -1,5 +1,5 @@
1
1
  # @twin.org/crypto - Changelog
2
2
 
3
- ## 0.0.1-next.38
3
+ ## 0.0.1-next.39
4
4
 
5
5
  - Added: Bip44
@@ -123,3 +123,25 @@ True if the signature matches.
123
123
  #### Throws
124
124
 
125
125
  Error if the public key is not the correct length.
126
+
127
+ ***
128
+
129
+ ### privateKeyToPKCS8()
130
+
131
+ > `static` **privateKeyToPKCS8**(`privateKey`): `Promise`\<`CryptoKey`\>
132
+
133
+ Convert a private key in PKCS8 format.
134
+
135
+ #### Parameters
136
+
137
+ ##### privateKey
138
+
139
+ `Uint8Array`
140
+
141
+ The private key to convert.
142
+
143
+ #### Returns
144
+
145
+ `Promise`\<`CryptoKey`\>
146
+
147
+ The private key in PKCS8 format.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@twin.org/crypto",
3
- "version": "0.0.1-next.38",
3
+ "version": "0.0.1-next.39",
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.38",
23
+ "@twin.org/core": "0.0.1-next.39",
24
24
  "@twin.org/nameof": "next",
25
25
  "micro-key-producer": "0.7.5"
26
26
  },