@twin.org/web 0.0.1-next.42 → 0.0.1-next.44

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.
@@ -727,21 +727,16 @@ class Jwk {
727
727
  */
728
728
  static async fromEd25519Private(privateKey) {
729
729
  core.Guards.uint8Array(Jwk._CLASS_NAME, "privateKey", privateKey);
730
- try {
731
- const publicKey = crypto.Ed25519.publicKeyFromPrivateKey(privateKey);
732
- const jwk = {
733
- kty: "OKP",
734
- use: "sig",
735
- alg: "EdDSA",
736
- crv: "Ed25519",
737
- x: core.Converter.bytesToBase64Url(publicKey),
738
- d: core.Converter.bytesToBase64Url(privateKey)
739
- };
740
- return (await jose.importJWK(jwk));
741
- }
742
- catch (err) {
743
- throw new core.GeneralError(Jwk._CLASS_NAME, "jwkImportFailed", undefined, err);
744
- }
730
+ const publicKey = crypto.Ed25519.publicKeyFromPrivateKey(privateKey);
731
+ const jwk = {
732
+ kty: "OKP",
733
+ use: "enc",
734
+ alg: "EdDSA",
735
+ crv: "Ed25519",
736
+ x: core.Converter.bytesToBase64Url(publicKey),
737
+ d: core.Converter.bytesToBase64Url(privateKey)
738
+ };
739
+ return jwk;
745
740
  }
746
741
  /**
747
742
  * Convert the Ed25519 public key to a crypto key.
@@ -750,19 +745,34 @@ class Jwk {
750
745
  */
751
746
  static async fromEd25519Public(publicKey) {
752
747
  core.Guards.uint8Array(Jwk._CLASS_NAME, "publicKey", publicKey);
753
- try {
754
- const jwk = {
755
- kty: "OKP",
756
- use: "sig",
757
- alg: "EdDSA",
758
- crv: "Ed25519",
759
- x: core.Converter.bytesToBase64Url(publicKey)
760
- };
761
- return (await jose.importJWK(jwk));
748
+ const jwk = {
749
+ kty: "OKP",
750
+ use: "sig",
751
+ alg: "EdDSA",
752
+ crv: "Ed25519",
753
+ x: core.Converter.bytesToBase64Url(publicKey)
754
+ };
755
+ return jwk;
756
+ }
757
+ /**
758
+ * Convert the JWK to raw keys.
759
+ * @param jwk The JWK to convert to raw.
760
+ * @returns The crypto key.
761
+ */
762
+ static async toRaw(jwk) {
763
+ core.Guards.object(Jwk._CLASS_NAME, "jwk", jwk);
764
+ let publicKey;
765
+ let privateKey;
766
+ if (core.Is.stringBase64Url(jwk.x)) {
767
+ publicKey = core.Converter.base64UrlToBytes(jwk.x);
762
768
  }
763
- catch (err) {
764
- throw new core.GeneralError(Jwk._CLASS_NAME, "jwkImportFailed", undefined, err);
769
+ if (core.Is.stringBase64Url(jwk.d)) {
770
+ privateKey = core.Converter.base64UrlToBytes(jwk.d);
765
771
  }
772
+ return {
773
+ publicKey,
774
+ privateKey
775
+ };
766
776
  }
767
777
  }
768
778
 
@@ -781,15 +791,16 @@ class Jws {
781
791
  * Create a signature.
782
792
  * @param privateKey The private key to use.
783
793
  * @param hash The hash to sign.
794
+ * @param algOverride An optional algorithm override.
784
795
  * @returns The signature.
785
796
  */
786
- static async create(privateKey, hash) {
797
+ static async create(privateKey, hash, algOverride) {
787
798
  core.Guards.defined(Jws._CLASS_NAME, "privateKey", privateKey);
788
799
  core.Guards.uint8Array(Jws._CLASS_NAME, "hash", hash);
789
800
  try {
790
801
  const jws = await new jose.CompactSign(hash)
791
802
  .setProtectedHeader({
792
- alg: privateKey.algorithm.name,
803
+ alg: core.Is.uint8Array(privateKey) ? (algOverride ?? "EdDSA") : privateKey.algorithm.name,
793
804
  b64: false,
794
805
  crit: ["b64"]
795
806
  })
@@ -949,7 +960,7 @@ class Jwt {
949
960
  let finalKey = key;
950
961
  if (header.alg === "EdDSA" && core.Is.uint8Array(key)) {
951
962
  // Jose does not support Ed25519 keys in raw format, so we need to convert it to PKCS8.
952
- finalKey = await crypto.Ed25519.privateKeyToPKCS8(key);
963
+ finalKey = await crypto.Ed25519.privateKeyToPkcs8(key);
953
964
  }
954
965
  return signer.sign(finalKey);
955
966
  }
@@ -725,21 +725,16 @@ class Jwk {
725
725
  */
726
726
  static async fromEd25519Private(privateKey) {
727
727
  Guards.uint8Array(Jwk._CLASS_NAME, "privateKey", privateKey);
728
- try {
729
- const publicKey = Ed25519.publicKeyFromPrivateKey(privateKey);
730
- const jwk = {
731
- kty: "OKP",
732
- use: "sig",
733
- alg: "EdDSA",
734
- crv: "Ed25519",
735
- x: Converter.bytesToBase64Url(publicKey),
736
- d: Converter.bytesToBase64Url(privateKey)
737
- };
738
- return (await importJWK(jwk));
739
- }
740
- catch (err) {
741
- throw new GeneralError(Jwk._CLASS_NAME, "jwkImportFailed", undefined, err);
742
- }
728
+ const publicKey = Ed25519.publicKeyFromPrivateKey(privateKey);
729
+ const jwk = {
730
+ kty: "OKP",
731
+ use: "enc",
732
+ alg: "EdDSA",
733
+ crv: "Ed25519",
734
+ x: Converter.bytesToBase64Url(publicKey),
735
+ d: Converter.bytesToBase64Url(privateKey)
736
+ };
737
+ return jwk;
743
738
  }
744
739
  /**
745
740
  * Convert the Ed25519 public key to a crypto key.
@@ -748,19 +743,34 @@ class Jwk {
748
743
  */
749
744
  static async fromEd25519Public(publicKey) {
750
745
  Guards.uint8Array(Jwk._CLASS_NAME, "publicKey", publicKey);
751
- try {
752
- const jwk = {
753
- kty: "OKP",
754
- use: "sig",
755
- alg: "EdDSA",
756
- crv: "Ed25519",
757
- x: Converter.bytesToBase64Url(publicKey)
758
- };
759
- return (await importJWK(jwk));
746
+ const jwk = {
747
+ kty: "OKP",
748
+ use: "sig",
749
+ alg: "EdDSA",
750
+ crv: "Ed25519",
751
+ x: Converter.bytesToBase64Url(publicKey)
752
+ };
753
+ return jwk;
754
+ }
755
+ /**
756
+ * Convert the JWK to raw keys.
757
+ * @param jwk The JWK to convert to raw.
758
+ * @returns The crypto key.
759
+ */
760
+ static async toRaw(jwk) {
761
+ Guards.object(Jwk._CLASS_NAME, "jwk", jwk);
762
+ let publicKey;
763
+ let privateKey;
764
+ if (Is.stringBase64Url(jwk.x)) {
765
+ publicKey = Converter.base64UrlToBytes(jwk.x);
760
766
  }
761
- catch (err) {
762
- throw new GeneralError(Jwk._CLASS_NAME, "jwkImportFailed", undefined, err);
767
+ if (Is.stringBase64Url(jwk.d)) {
768
+ privateKey = Converter.base64UrlToBytes(jwk.d);
763
769
  }
770
+ return {
771
+ publicKey,
772
+ privateKey
773
+ };
764
774
  }
765
775
  }
766
776
 
@@ -779,15 +789,16 @@ class Jws {
779
789
  * Create a signature.
780
790
  * @param privateKey The private key to use.
781
791
  * @param hash The hash to sign.
792
+ * @param algOverride An optional algorithm override.
782
793
  * @returns The signature.
783
794
  */
784
- static async create(privateKey, hash) {
795
+ static async create(privateKey, hash, algOverride) {
785
796
  Guards.defined(Jws._CLASS_NAME, "privateKey", privateKey);
786
797
  Guards.uint8Array(Jws._CLASS_NAME, "hash", hash);
787
798
  try {
788
799
  const jws = await new CompactSign(hash)
789
800
  .setProtectedHeader({
790
- alg: privateKey.algorithm.name,
801
+ alg: Is.uint8Array(privateKey) ? (algOverride ?? "EdDSA") : privateKey.algorithm.name,
791
802
  b64: false,
792
803
  crit: ["b64"]
793
804
  })
@@ -947,7 +958,7 @@ class Jwt {
947
958
  let finalKey = key;
948
959
  if (header.alg === "EdDSA" && Is.uint8Array(key)) {
949
960
  // Jose does not support Ed25519 keys in raw format, so we need to convert it to PKCS8.
950
- finalKey = await Ed25519.privateKeyToPKCS8(key);
961
+ finalKey = await Ed25519.privateKeyToPkcs8(key);
951
962
  }
952
963
  return signer.sign(finalKey);
953
964
  }
@@ -15,11 +15,20 @@ export declare class Jwk {
15
15
  * @param privateKey The private key to use.
16
16
  * @returns The crypto key.
17
17
  */
18
- static fromEd25519Private(privateKey: Uint8Array): Promise<CryptoKey>;
18
+ static fromEd25519Private(privateKey: Uint8Array): Promise<IJwk>;
19
19
  /**
20
20
  * Convert the Ed25519 public key to a crypto key.
21
21
  * @param publicKey The private key to use.
22
22
  * @returns The crypto key.
23
23
  */
24
- static fromEd25519Public(publicKey: Uint8Array): Promise<CryptoKey>;
24
+ static fromEd25519Public(publicKey: Uint8Array): Promise<IJwk>;
25
+ /**
26
+ * Convert the JWK to raw keys.
27
+ * @param jwk The JWK to convert to raw.
28
+ * @returns The crypto key.
29
+ */
30
+ static toRaw(jwk: IJwk): Promise<{
31
+ publicKey?: Uint8Array;
32
+ privateKey?: Uint8Array;
33
+ }>;
25
34
  }
@@ -1,3 +1,4 @@
1
+ import type { JwkCryptoKey } from "../models/jwkCryptoKey";
1
2
  /**
2
3
  * Class to handle JSON Web Signatures.
3
4
  */
@@ -6,9 +7,10 @@ export declare class Jws {
6
7
  * Create a signature.
7
8
  * @param privateKey The private key to use.
8
9
  * @param hash The hash to sign.
10
+ * @param algOverride An optional algorithm override.
9
11
  * @returns The signature.
10
12
  */
11
- static create(privateKey: CryptoKey, hash: Uint8Array): Promise<string>;
13
+ static create(privateKey: JwkCryptoKey, hash: Uint8Array, algOverride?: string): Promise<string>;
12
14
  /**
13
15
  * Verify a signature.
14
16
  * @param jws The signature to verify.
@@ -16,5 +18,5 @@ export declare class Jws {
16
18
  * @param hash The hash to verify.
17
19
  * @returns True if the signature was verified.
18
20
  */
19
- static verify(jws: string, publicKey: CryptoKey, hash: Uint8Array): Promise<boolean>;
21
+ static verify(jws: string, publicKey: JwkCryptoKey, hash: Uint8Array): Promise<boolean>;
20
22
  }
package/docs/changelog.md CHANGED
@@ -1,5 +1,5 @@
1
1
  # @twin.org/web - Changelog
2
2
 
3
- ## 0.0.1-next.42
3
+ ## 0.0.1-next.44
4
4
 
5
5
  - Initial Release
@@ -38,7 +38,7 @@ The crypto key.
38
38
 
39
39
  ### fromEd25519Private()
40
40
 
41
- > `static` **fromEd25519Private**(`privateKey`): `Promise`\<`CryptoKey`\>
41
+ > `static` **fromEd25519Private**(`privateKey`): `Promise`\<[`IJwk`](../interfaces/IJwk.md)\>
42
42
 
43
43
  Convert the Ed25519 private key to a crypto key.
44
44
 
@@ -52,7 +52,7 @@ The private key to use.
52
52
 
53
53
  #### Returns
54
54
 
55
- `Promise`\<`CryptoKey`\>
55
+ `Promise`\<[`IJwk`](../interfaces/IJwk.md)\>
56
56
 
57
57
  The crypto key.
58
58
 
@@ -60,7 +60,7 @@ The crypto key.
60
60
 
61
61
  ### fromEd25519Public()
62
62
 
63
- > `static` **fromEd25519Public**(`publicKey`): `Promise`\<`CryptoKey`\>
63
+ > `static` **fromEd25519Public**(`publicKey`): `Promise`\<[`IJwk`](../interfaces/IJwk.md)\>
64
64
 
65
65
  Convert the Ed25519 public key to a crypto key.
66
66
 
@@ -74,6 +74,28 @@ The private key to use.
74
74
 
75
75
  #### Returns
76
76
 
77
- `Promise`\<`CryptoKey`\>
77
+ `Promise`\<[`IJwk`](../interfaces/IJwk.md)\>
78
+
79
+ The crypto key.
80
+
81
+ ***
82
+
83
+ ### toRaw()
84
+
85
+ > `static` **toRaw**(`jwk`): `Promise`\<\{ `publicKey`: `Uint8Array`; `privateKey`: `Uint8Array`; \}\>
86
+
87
+ Convert the JWK to raw keys.
88
+
89
+ #### Parameters
90
+
91
+ ##### jwk
92
+
93
+ [`IJwk`](../interfaces/IJwk.md)
94
+
95
+ The JWK to convert to raw.
96
+
97
+ #### Returns
98
+
99
+ `Promise`\<\{ `publicKey`: `Uint8Array`; `privateKey`: `Uint8Array`; \}\>
78
100
 
79
101
  The crypto key.
@@ -16,7 +16,7 @@ Class to handle JSON Web Signatures.
16
16
 
17
17
  ### create()
18
18
 
19
- > `static` **create**(`privateKey`, `hash`): `Promise`\<`string`\>
19
+ > `static` **create**(`privateKey`, `hash`, `algOverride`?): `Promise`\<`string`\>
20
20
 
21
21
  Create a signature.
22
22
 
@@ -24,7 +24,7 @@ Create a signature.
24
24
 
25
25
  ##### privateKey
26
26
 
27
- `CryptoKey`
27
+ [`JwkCryptoKey`](../type-aliases/JwkCryptoKey.md)
28
28
 
29
29
  The private key to use.
30
30
 
@@ -34,6 +34,12 @@ The private key to use.
34
34
 
35
35
  The hash to sign.
36
36
 
37
+ ##### algOverride?
38
+
39
+ `string`
40
+
41
+ An optional algorithm override.
42
+
37
43
  #### Returns
38
44
 
39
45
  `Promise`\<`string`\>
@@ -58,7 +64,7 @@ The signature to verify.
58
64
 
59
65
  ##### publicKey
60
66
 
61
- `CryptoKey`
67
+ [`JwkCryptoKey`](../type-aliases/JwkCryptoKey.md)
62
68
 
63
69
  The public key to verify the signature with.
64
70
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@twin.org/web",
3
- "version": "0.0.1-next.42",
3
+ "version": "0.0.1-next.44",
4
4
  "description": "Contains classes for use with web operations",
5
5
  "repository": {
6
6
  "type": "git",
@@ -14,8 +14,8 @@
14
14
  "node": ">=20.0.0"
15
15
  },
16
16
  "dependencies": {
17
- "@twin.org/core": "0.0.1-next.42",
18
- "@twin.org/crypto": "0.0.1-next.42",
17
+ "@twin.org/core": "0.0.1-next.44",
18
+ "@twin.org/crypto": "0.0.1-next.44",
19
19
  "@twin.org/nameof": "next",
20
20
  "jose": "6.0.8"
21
21
  },