@coinbase/cdp-core 0.0.24 → 0.0.25

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 (108) hide show
  1. package/README.md +12 -12
  2. package/dist/esm/index.js +17 -15
  3. package/dist/esm/index10.js +20 -14
  4. package/dist/esm/index100.js +6 -9
  5. package/dist/esm/index101.js +11 -13
  6. package/dist/esm/index102.js +11 -8
  7. package/dist/esm/index103.js +7 -14
  8. package/dist/esm/index104.js +16 -80
  9. package/dist/esm/index105.js +72 -70
  10. package/dist/esm/index106.js +81 -0
  11. package/dist/esm/index11.js +13 -117
  12. package/dist/esm/index12.js +116 -11
  13. package/dist/esm/index13.js +14 -42
  14. package/dist/esm/index14.js +42 -20
  15. package/dist/esm/index15.js +20 -172
  16. package/dist/esm/index16.js +170 -24
  17. package/dist/esm/index17.js +25 -40
  18. package/dist/esm/index18.js +158 -15
  19. package/dist/esm/index19.js +47 -4
  20. package/dist/esm/index2.js +10 -10
  21. package/dist/esm/index20.js +250 -5
  22. package/dist/esm/index21.js +50 -154
  23. package/dist/esm/index22.js +39 -46
  24. package/dist/esm/index23.js +14 -247
  25. package/dist/esm/index24.js +5 -56
  26. package/dist/esm/index25.js +5 -9
  27. package/dist/esm/index26.js +8 -17
  28. package/dist/esm/index27.js +16 -44
  29. package/dist/esm/index28.js +45 -13
  30. package/dist/esm/index29.js +13 -55
  31. package/dist/esm/index3.js +4 -4
  32. package/dist/esm/index30.js +55 -6
  33. package/dist/esm/index31.js +6 -21
  34. package/dist/esm/index32.js +21 -11
  35. package/dist/esm/index33.js +11 -3
  36. package/dist/esm/index34.js +3 -3
  37. package/dist/esm/index35.js +3 -34
  38. package/dist/esm/index36.js +32 -10
  39. package/dist/esm/index37.js +12 -4
  40. package/dist/esm/index38.js +4 -28
  41. package/dist/esm/index39.js +28 -6
  42. package/dist/esm/index40.js +6 -24
  43. package/dist/esm/index41.js +24 -16
  44. package/dist/esm/index42.js +16 -54
  45. package/dist/esm/index43.js +53 -22
  46. package/dist/esm/index44.js +23 -11
  47. package/dist/esm/index45.js +10 -26
  48. package/dist/esm/index46.js +26 -41
  49. package/dist/esm/index47.js +41 -54
  50. package/dist/esm/index48.js +54 -126
  51. package/dist/esm/index49.js +126 -11
  52. package/dist/esm/index5.js +1 -1
  53. package/dist/esm/index50.js +9 -101
  54. package/dist/esm/index51.js +104 -6
  55. package/dist/esm/index52.js +7 -3
  56. package/dist/esm/index53.js +3 -15
  57. package/dist/esm/index54.js +14 -42
  58. package/dist/esm/index55.js +38 -74
  59. package/dist/esm/index56.js +79 -2
  60. package/dist/esm/index57.js +2 -2
  61. package/dist/esm/index58.js +2 -13
  62. package/dist/esm/index59.js +8 -33
  63. package/dist/esm/index6.js +13 -183
  64. package/dist/esm/index60.js +36 -5
  65. package/dist/esm/index61.js +5 -33
  66. package/dist/esm/index62.js +34 -8
  67. package/dist/esm/index63.js +8 -10
  68. package/dist/esm/index64.js +10 -11
  69. package/dist/esm/index65.js +11 -13
  70. package/dist/esm/index66.js +13 -18
  71. package/dist/esm/index67.js +18 -5
  72. package/dist/esm/index68.js +6 -5
  73. package/dist/esm/index69.js +5 -11
  74. package/dist/esm/index7.js +192 -15
  75. package/dist/esm/index70.js +10 -16
  76. package/dist/esm/index71.js +15 -21
  77. package/dist/esm/index72.js +21 -49
  78. package/dist/esm/index73.js +51 -2
  79. package/dist/esm/index74.js +1 -1
  80. package/dist/esm/index75.js +2 -20
  81. package/dist/esm/index76.js +20 -80
  82. package/dist/esm/index77.js +79 -103
  83. package/dist/esm/index78.js +101 -30
  84. package/dist/esm/index79.js +32 -6
  85. package/dist/esm/index8.js +14 -15
  86. package/dist/esm/index80.js +6 -54
  87. package/dist/esm/index81.js +78 -10
  88. package/dist/esm/index82.js +10 -2
  89. package/dist/esm/index83.js +54 -44
  90. package/dist/esm/index84.js +10 -13
  91. package/dist/esm/index85.js +2 -10
  92. package/dist/esm/index86.js +44 -78
  93. package/dist/esm/index87.js +14 -5
  94. package/dist/esm/index88.js +5 -21
  95. package/dist/esm/index89.js +12 -9
  96. package/dist/esm/index9.js +15 -20
  97. package/dist/esm/index90.js +33 -17
  98. package/dist/esm/index91.js +41 -93
  99. package/dist/esm/index92.js +20 -20
  100. package/dist/esm/index93.js +9 -117
  101. package/dist/esm/index94.js +18 -11
  102. package/dist/esm/index95.js +90 -32
  103. package/dist/esm/index96.js +20 -41
  104. package/dist/esm/index97.js +118 -18
  105. package/dist/esm/index98.js +18 -28
  106. package/dist/esm/index99.js +26 -6
  107. package/dist/types/index.d.ts +6 -0
  108. package/package.json +2 -2
package/README.md CHANGED
@@ -37,7 +37,7 @@ in CDP Portal, and click Add origin to include your local app
37
37
 
38
38
  Before calling any methods in the SDK, you must first initialize it:
39
39
 
40
- ```ts
40
+ ```ts lines
41
41
  import { Config, initialize } from "@coinbase/cdp-core";
42
42
 
43
43
  const config: Config = {
@@ -52,7 +52,7 @@ await initialize(config);
52
52
 
53
53
  You can configure the SDK to automatically create Smart Accounts for new users by setting `createAccountOnLogin` to `"evm-smart"`:
54
54
 
55
- ```ts
55
+ ```ts lines
56
56
  const config: Config = {
57
57
  projectId: "your-project-id",
58
58
  createAccountOnLogin: "evm-smart", // Creates Smart Accounts instead of EOAs
@@ -71,7 +71,7 @@ When `createAccountOnLogin` is set to `"evm-smart"`, the SDK will:
71
71
  You're now ready to start calling the APIs provided by the package!
72
72
  The following code signs in an end user:
73
73
 
74
- ```ts
74
+ ```ts lines
75
75
  import { signInWithEmail, verifyEmailOTP } from "@coinbase/cdp-core";
76
76
 
77
77
  // Send an email to user@example.com with a One Time Password (OTP).
@@ -92,7 +92,7 @@ const user = verifyResult.user;
92
92
  ### View User Information
93
93
  Once the end user has signed in, you can display their information in your application:
94
94
 
95
- ```typescript
95
+ ```typescript lines
96
96
  import { getCurrentUser, isSignedIn } from "@coinbase/cdp-core";
97
97
 
98
98
  // Check if user is signed in
@@ -122,7 +122,7 @@ We support signing and sending a Blockchain transaction in a single call on the
122
122
  - Optimism
123
123
  - Polygon
124
124
 
125
- ```typescript
125
+ ```typescript lines
126
126
  import { sendEvmTransaction, getCurrentUser } from "@coinbase/cdp-core";
127
127
 
128
128
  const user = await getCurrentUser();
@@ -148,7 +148,7 @@ console.log("Transaction Hash:", result.transactionHash);
148
148
  For networks other than those supported by the CDP APIs, your end user must sign the transaction, and then
149
149
  you must broadcast the transaction yourself. This example uses the public client from `viem` to broadcast the transaction.
150
150
 
151
- ```typescript
151
+ ```typescript lines
152
152
  import { signEvmTransaction, getCurrentUser } from "@coinbase/cdp-core";
153
153
  import { http, createPublicClient } from "viem";
154
154
  import { tron } from "viem/chains";
@@ -190,7 +190,7 @@ Smart Accounts provide advanced account abstraction features, including user ope
190
190
 
191
191
  Send user operations from a Smart Account:
192
192
 
193
- ```typescript
193
+ ```typescript lines
194
194
  import { sendUserOperation, getCurrentUser } from "@coinbase/cdp-core";
195
195
 
196
196
  const user = await getCurrentUser();
@@ -217,7 +217,7 @@ console.log("User Operation Hash:", result.userOperationHash);
217
217
 
218
218
  After sending a user operation, you can get its status and retrieve the result:
219
219
 
220
- ```typescript
220
+ ```typescript lines
221
221
  import { getUserOperation } from "@coinbase/cdp-core";
222
222
 
223
223
  // Get the status of a user operation
@@ -241,7 +241,7 @@ if (userOperationResult.status === "complete") {
241
241
 
242
242
  End users can sign EVM messages, hashes, and typed data to generate signatures for various onchain applications.
243
243
 
244
- ```typescript
244
+ ```typescript lines
245
245
  import { signEvmMessage, signEvmTypedData, getCurrentUser } from "@coinbase/cdp-core";
246
246
 
247
247
  const user = await getCurrentUser();
@@ -281,7 +281,7 @@ const typedDataResult = await signEvmTypedData({
281
281
 
282
282
  End users can export their private keys from their embedded wallet, allowing them to import it into an EVM-compatible wallet of their choice.
283
283
 
284
- ```typescript
284
+ ```typescript lines
285
285
  import { exportEvmAccount, getCurrentUser } from "@coinbase/cdp-core";
286
286
 
287
287
  const user = await getCurrentUser();
@@ -302,7 +302,7 @@ The core package includes an EIP-1193 compatible provider. This provider can be
302
302
  The provider is created by calling `createCDPEmbeddedWallet`, which exposes a `.provider` attribute. `createCDPEmbeddedWallet` must be called with the desired chains to support as well as the transports for these chains.
303
303
 
304
304
  The provider will initially connect to the first chain in the `chains` array. The transports are typically HTTP RPC endpoints, which are used internally for broadcasting non-Base transactions. For more information on transports, see [Wagmi's `createConfig` setup](https://wagmi.sh/react/api/createConfig).
305
- ```typescript
305
+ ```typescript lines
306
306
  import { base, mainnet } from "viem/chains";
307
307
  import { http } from "viem"
308
308
 
@@ -351,7 +351,7 @@ provider.on("disconnect", () => {
351
351
 
352
352
  The core package includes a `toViemAccount` utility function that enables wrapping an embedded wallet into a Viem account compatible interface. This allows the account to act as a drop-in replacement for any library or framework that accepts Viem accounts.
353
353
 
354
- ```typescript
354
+ ```typescript lines
355
355
  import { toViemAccount, getCurrentUser } from "@coinbase/cdp-core";
356
356
  import { createWalletClient } from "viem";
357
357
  import { mainnet } from "viem/chains";
package/dist/esm/index.js CHANGED
@@ -1,19 +1,21 @@
1
- import { exportEvmAccount as i, getAccessToken as s, getCurrentUser as m, getUserOperation as a, initialize as E, isSignedIn as p, onAuthStateChange as g, sendEvmTransaction as c, sendUserOperation as d, signEvmHash as v, signEvmMessage as T, signEvmTransaction as A, signEvmTypedData as f, signInWithEmail as O, signInWithSms as S, signOut as h, verifyEmailOTP as u, verifySmsOTP as x } from "./index2.js";
2
- import { APIError as P, ErrorType as l, HttpErrorType as D, SendEvmTransactionWithEndUserAccountBodyNetwork as I } from "@coinbase/cdp-api-client";
1
+ import { exportEvmAccount as i, getAccessToken as s, getCurrentUser as m, getUserOperation as a, initialize as E, isSignedIn as p, onAuthStateChange as g, sendEvmTransaction as c, sendUserOperation as d, signEvmHash as v, signEvmMessage as T, signEvmTransaction as f, signEvmTypedData as u, signInWithEmail as A, signInWithSms as l, signOut as x, verifyEmailOTP as O, verifySmsOTP as S } from "./index2.js";
2
+ import { APIError as y, ErrorType as P, HttpErrorType as D, SendEvmTransactionWithEndUserAccountBodyNetwork as I } from "@coinbase/cdp-api-client";
3
3
  import "viem";
4
- import { createCDPEmbeddedWallet as R } from "./index3.js";
4
+ import { createCDPEmbeddedWallet as C } from "./index3.js";
5
5
  import { EIP1193ProviderError as W, STANDARD_ERROR_CODES as k } from "./index4.js";
6
6
  import "ox";
7
7
  import "zustand";
8
- import { toViemAccount as N } from "./index5.js";
8
+ import { toViemAccount as M } from "./index5.js";
9
+ import { ModuleResolutionError as _ } from "./index6.js";
9
10
  export {
10
- P as APIError,
11
+ y as APIError,
11
12
  W as EIP1193ProviderError,
12
- l as ErrorType,
13
+ P as ErrorType,
13
14
  D as HttpErrorType,
15
+ _ as ModuleResolutionError,
14
16
  k as STANDARD_ERROR_CODES,
15
17
  I as SendEvmTransactionWithEndUserAccountBodyNetwork,
16
- R as createCDPEmbeddedWallet,
18
+ C as createCDPEmbeddedWallet,
17
19
  i as exportEvmAccount,
18
20
  s as getAccessToken,
19
21
  m as getCurrentUser,
@@ -25,12 +27,12 @@ export {
25
27
  d as sendUserOperation,
26
28
  v as signEvmHash,
27
29
  T as signEvmMessage,
28
- A as signEvmTransaction,
29
- f as signEvmTypedData,
30
- O as signInWithEmail,
31
- S as signInWithSms,
32
- h as signOut,
33
- N as toViemAccount,
34
- u as verifyEmailOTP,
35
- x as verifySmsOTP
30
+ f as signEvmTransaction,
31
+ u as signEvmTypedData,
32
+ A as signInWithEmail,
33
+ l as signInWithSms,
34
+ x as signOut,
35
+ M as toViemAccount,
36
+ O as verifyEmailOTP,
37
+ S as verifySmsOTP
36
38
  };
@@ -1,17 +1,23 @@
1
- async function a(e, n) {
2
- try {
3
- const r = new Uint8Array(Array.from(atob(n), (t) => t.charCodeAt(0))), o = await window.crypto.subtle.decrypt(
4
- {
5
- name: "RSA-OAEP"
6
- },
7
- e,
8
- r
9
- );
10
- return Array.from(new Uint8Array(o)).map((t) => t.toString(16).padStart(2, "0")).join("");
11
- } catch (r) {
12
- throw new Error(`Decryption failed: ${String(r)}`);
13
- }
1
+ async function a() {
2
+ const e = await window.crypto.subtle.generateKey(
3
+ {
4
+ name: "RSA-OAEP",
5
+ modulusLength: 4096,
6
+ publicExponent: new Uint8Array([1, 0, 1]),
7
+ // 65537
8
+ hash: { name: "SHA-256" }
9
+ },
10
+ !0,
11
+ // Allow key export for compatibility with Node version
12
+ ["encrypt", "decrypt"]
13
+ ), r = await window.crypto.subtle.exportKey("spki", e.publicKey), t = btoa(
14
+ Array.from(new Uint8Array(r)).map((n) => String.fromCharCode(n)).join("")
15
+ );
16
+ return {
17
+ privateKey: e.privateKey,
18
+ publicKeyBase64: t
19
+ };
14
20
  }
15
21
  export {
16
- a as decryptWithPrivateKey
22
+ a as createExportKeyPair
17
23
  };
@@ -1,13 +1,10 @@
1
- import { checkSigCryptoKey as i } from "./index105.js";
2
- import o from "./index101.js";
3
- const s = async (t, r, e) => {
4
- if (r instanceof Uint8Array) {
5
- if (!t.startsWith("HS"))
6
- throw new TypeError(o(r, "CryptoKey", "KeyObject", "JSON Web Key"));
7
- return crypto.subtle.importKey("raw", r, { hash: `SHA-${t.slice(-3)}`, name: "HMAC" }, !1, [e]);
1
+ const o = (t, r) => {
2
+ if (t.startsWith("RS") || t.startsWith("PS")) {
3
+ const { modulusLength: e } = r.algorithm;
4
+ if (typeof e != "number" || e < 2048)
5
+ throw new TypeError(`${t} requires key modulusLength to be 2048 bits or larger`);
8
6
  }
9
- return i(r, t, e), r;
10
7
  };
11
8
  export {
12
- s as default
9
+ o as default
13
10
  };
@@ -1,15 +1,13 @@
1
- function i(n, e, ...o) {
2
- if (o = o.filter(Boolean), o.length > 2) {
3
- const f = o.pop();
4
- n += `one of type ${o.join(", ")}, or ${f}.`;
5
- } else o.length === 2 ? n += `one of type ${o[0]} or ${o[1]}.` : n += `of type ${o[0]}.`;
6
- return e == null ? n += ` Received ${e}` : typeof e == "function" && e.name ? n += ` Received function ${e.name}` : typeof e == "object" && e != null && e.constructor?.name && (n += ` Received an instance of ${e.constructor.name}`), n;
7
- }
8
- const r = (n, ...e) => i("Key must be ", n, ...e);
9
- function t(n, e, ...o) {
10
- return i(`Key for the ${n} algorithm must be `, e, ...o);
11
- }
1
+ import { checkSigCryptoKey as i } from "./index106.js";
2
+ import o from "./index102.js";
3
+ const s = async (t, r, e) => {
4
+ if (r instanceof Uint8Array) {
5
+ if (!t.startsWith("HS"))
6
+ throw new TypeError(o(r, "CryptoKey", "KeyObject", "JSON Web Key"));
7
+ return crypto.subtle.importKey("raw", r, { hash: `SHA-${t.slice(-3)}`, name: "HMAC" }, !1, [e]);
8
+ }
9
+ return i(r, t, e), r;
10
+ };
12
11
  export {
13
- r as default,
14
- t as withAlg
12
+ s as default
15
13
  };
@@ -1,12 +1,15 @@
1
- function e(t) {
2
- return t?.[Symbol.toStringTag] === "CryptoKey";
1
+ function i(n, e, ...o) {
2
+ if (o = o.filter(Boolean), o.length > 2) {
3
+ const f = o.pop();
4
+ n += `one of type ${o.join(", ")}, or ${f}.`;
5
+ } else o.length === 2 ? n += `one of type ${o[0]} or ${o[1]}.` : n += `of type ${o[0]}.`;
6
+ return e == null ? n += ` Received ${e}` : typeof e == "function" && e.name ? n += ` Received function ${e.name}` : typeof e == "object" && e != null && e.constructor?.name && (n += ` Received an instance of ${e.constructor.name}`), n;
3
7
  }
4
- function r(t) {
5
- return t?.[Symbol.toStringTag] === "KeyObject";
8
+ const r = (n, ...e) => i("Key must be ", n, ...e);
9
+ function t(n, e, ...o) {
10
+ return i(`Key for the ${n} algorithm must be `, e, ...o);
6
11
  }
7
- const n = (t) => e(t) || r(t);
8
12
  export {
9
- n as default,
10
- e as isCryptoKey,
11
- r as isKeyObject
13
+ r as default,
14
+ t as withAlg
12
15
  };
@@ -1,19 +1,12 @@
1
- import n from "./index84.js";
2
- function r(t) {
3
- return n(t) && typeof t.kty == "string";
4
- }
5
- function o(t) {
6
- return t.kty !== "oct" && typeof t.d == "string";
7
- }
8
1
  function e(t) {
9
- return t.kty !== "oct" && typeof t.d > "u";
2
+ return t?.[Symbol.toStringTag] === "CryptoKey";
10
3
  }
11
- function c(t) {
12
- return t.kty === "oct" && typeof t.k == "string";
4
+ function r(t) {
5
+ return t?.[Symbol.toStringTag] === "KeyObject";
13
6
  }
7
+ const n = (t) => e(t) || r(t);
14
8
  export {
15
- r as isJWK,
16
- o as isPrivateJWK,
17
- e as isPublicJWK,
18
- c as isSecretJWK
9
+ n as default,
10
+ e as isCryptoKey,
11
+ r as isKeyObject
19
12
  };
@@ -1,83 +1,19 @@
1
- import { JOSENotSupported as s } from "./index71.js";
2
- function n(e) {
3
- let a, r;
4
- switch (e.kty) {
5
- case "RSA": {
6
- switch (e.alg) {
7
- case "PS256":
8
- case "PS384":
9
- case "PS512":
10
- a = { name: "RSA-PSS", hash: `SHA-${e.alg.slice(-3)}` }, r = e.d ? ["sign"] : ["verify"];
11
- break;
12
- case "RS256":
13
- case "RS384":
14
- case "RS512":
15
- a = { name: "RSASSA-PKCS1-v1_5", hash: `SHA-${e.alg.slice(-3)}` }, r = e.d ? ["sign"] : ["verify"];
16
- break;
17
- case "RSA-OAEP":
18
- case "RSA-OAEP-256":
19
- case "RSA-OAEP-384":
20
- case "RSA-OAEP-512":
21
- a = {
22
- name: "RSA-OAEP",
23
- hash: `SHA-${parseInt(e.alg.slice(-3), 10) || 1}`
24
- }, r = e.d ? ["decrypt", "unwrapKey"] : ["encrypt", "wrapKey"];
25
- break;
26
- default:
27
- throw new s('Invalid or unsupported JWK "alg" (Algorithm) Parameter value');
28
- }
29
- break;
30
- }
31
- case "EC": {
32
- switch (e.alg) {
33
- case "ES256":
34
- a = { name: "ECDSA", namedCurve: "P-256" }, r = e.d ? ["sign"] : ["verify"];
35
- break;
36
- case "ES384":
37
- a = { name: "ECDSA", namedCurve: "P-384" }, r = e.d ? ["sign"] : ["verify"];
38
- break;
39
- case "ES512":
40
- a = { name: "ECDSA", namedCurve: "P-521" }, r = e.d ? ["sign"] : ["verify"];
41
- break;
42
- case "ECDH-ES":
43
- case "ECDH-ES+A128KW":
44
- case "ECDH-ES+A192KW":
45
- case "ECDH-ES+A256KW":
46
- a = { name: "ECDH", namedCurve: e.crv }, r = e.d ? ["deriveBits"] : [];
47
- break;
48
- default:
49
- throw new s('Invalid or unsupported JWK "alg" (Algorithm) Parameter value');
50
- }
51
- break;
52
- }
53
- case "OKP": {
54
- switch (e.alg) {
55
- case "Ed25519":
56
- case "EdDSA":
57
- a = { name: "Ed25519" }, r = e.d ? ["sign"] : ["verify"];
58
- break;
59
- case "ECDH-ES":
60
- case "ECDH-ES+A128KW":
61
- case "ECDH-ES+A192KW":
62
- case "ECDH-ES+A256KW":
63
- a = { name: e.crv }, r = e.d ? ["deriveBits"] : [];
64
- break;
65
- default:
66
- throw new s('Invalid or unsupported JWK "alg" (Algorithm) Parameter value');
67
- }
68
- break;
69
- }
70
- default:
71
- throw new s('Invalid or unsupported JWK "kty" (Key Type) Parameter value');
72
- }
73
- return { algorithm: a, keyUsages: r };
1
+ import n from "./index87.js";
2
+ function r(t) {
3
+ return n(t) && typeof t.kty == "string";
4
+ }
5
+ function o(t) {
6
+ return t.kty !== "oct" && typeof t.d == "string";
7
+ }
8
+ function e(t) {
9
+ return t.kty !== "oct" && typeof t.d > "u";
10
+ }
11
+ function c(t) {
12
+ return t.kty === "oct" && typeof t.k == "string";
74
13
  }
75
- const i = async (e) => {
76
- if (!e.alg)
77
- throw new TypeError('"alg" argument is required when "jwk.alg" is not present');
78
- const { algorithm: a, keyUsages: r } = n(e), t = { ...e };
79
- return delete t.alg, delete t.use, crypto.subtle.importKey("jwk", t, a, e.ext ?? !e.d, e.key_ops ?? r);
80
- };
81
14
  export {
82
- i as default
15
+ r as isJWK,
16
+ o as isPrivateJWK,
17
+ e as isPublicJWK,
18
+ c as isSecretJWK
83
19
  };
@@ -1,81 +1,83 @@
1
- function r(t, e = "algorithm.name") {
2
- return new TypeError(`CryptoKey does not support this operation, its ${e} must be ${t}`);
3
- }
4
- function o(t, e) {
5
- return t.name === e;
6
- }
7
- function c(t) {
8
- return parseInt(t.name.slice(4), 10);
9
- }
10
- function h(t) {
11
- switch (t) {
12
- case "ES256":
13
- return "P-256";
14
- case "ES384":
15
- return "P-384";
16
- case "ES512":
17
- return "P-521";
18
- default:
19
- throw new Error("unreachable");
20
- }
21
- }
22
- function i(t, e) {
23
- if (!t.usages.includes(e))
24
- throw new TypeError(`CryptoKey does not support this operation, its usages must include ${e}.`);
25
- }
26
- function u(t, e, n) {
27
- switch (e) {
28
- case "HS256":
29
- case "HS384":
30
- case "HS512": {
31
- if (!o(t.algorithm, "HMAC"))
32
- throw r("HMAC");
33
- const a = parseInt(e.slice(2), 10);
34
- if (c(t.algorithm.hash) !== a)
35
- throw r(`SHA-${a}`, "algorithm.hash");
36
- break;
37
- }
38
- case "RS256":
39
- case "RS384":
40
- case "RS512": {
41
- if (!o(t.algorithm, "RSASSA-PKCS1-v1_5"))
42
- throw r("RSASSA-PKCS1-v1_5");
43
- const a = parseInt(e.slice(2), 10);
44
- if (c(t.algorithm.hash) !== a)
45
- throw r(`SHA-${a}`, "algorithm.hash");
1
+ import { JOSENotSupported as s } from "./index72.js";
2
+ function n(e) {
3
+ let a, r;
4
+ switch (e.kty) {
5
+ case "RSA": {
6
+ switch (e.alg) {
7
+ case "PS256":
8
+ case "PS384":
9
+ case "PS512":
10
+ a = { name: "RSA-PSS", hash: `SHA-${e.alg.slice(-3)}` }, r = e.d ? ["sign"] : ["verify"];
11
+ break;
12
+ case "RS256":
13
+ case "RS384":
14
+ case "RS512":
15
+ a = { name: "RSASSA-PKCS1-v1_5", hash: `SHA-${e.alg.slice(-3)}` }, r = e.d ? ["sign"] : ["verify"];
16
+ break;
17
+ case "RSA-OAEP":
18
+ case "RSA-OAEP-256":
19
+ case "RSA-OAEP-384":
20
+ case "RSA-OAEP-512":
21
+ a = {
22
+ name: "RSA-OAEP",
23
+ hash: `SHA-${parseInt(e.alg.slice(-3), 10) || 1}`
24
+ }, r = e.d ? ["decrypt", "unwrapKey"] : ["encrypt", "wrapKey"];
25
+ break;
26
+ default:
27
+ throw new s('Invalid or unsupported JWK "alg" (Algorithm) Parameter value');
28
+ }
46
29
  break;
47
30
  }
48
- case "PS256":
49
- case "PS384":
50
- case "PS512": {
51
- if (!o(t.algorithm, "RSA-PSS"))
52
- throw r("RSA-PSS");
53
- const a = parseInt(e.slice(2), 10);
54
- if (c(t.algorithm.hash) !== a)
55
- throw r(`SHA-${a}`, "algorithm.hash");
31
+ case "EC": {
32
+ switch (e.alg) {
33
+ case "ES256":
34
+ a = { name: "ECDSA", namedCurve: "P-256" }, r = e.d ? ["sign"] : ["verify"];
35
+ break;
36
+ case "ES384":
37
+ a = { name: "ECDSA", namedCurve: "P-384" }, r = e.d ? ["sign"] : ["verify"];
38
+ break;
39
+ case "ES512":
40
+ a = { name: "ECDSA", namedCurve: "P-521" }, r = e.d ? ["sign"] : ["verify"];
41
+ break;
42
+ case "ECDH-ES":
43
+ case "ECDH-ES+A128KW":
44
+ case "ECDH-ES+A192KW":
45
+ case "ECDH-ES+A256KW":
46
+ a = { name: "ECDH", namedCurve: e.crv }, r = e.d ? ["deriveBits"] : [];
47
+ break;
48
+ default:
49
+ throw new s('Invalid or unsupported JWK "alg" (Algorithm) Parameter value');
50
+ }
56
51
  break;
57
52
  }
58
- case "Ed25519":
59
- case "EdDSA": {
60
- if (!o(t.algorithm, "Ed25519"))
61
- throw r("Ed25519");
62
- break;
63
- }
64
- case "ES256":
65
- case "ES384":
66
- case "ES512": {
67
- if (!o(t.algorithm, "ECDSA"))
68
- throw r("ECDSA");
69
- const a = h(e);
70
- if (t.algorithm.namedCurve !== a)
71
- throw r(a, "algorithm.namedCurve");
53
+ case "OKP": {
54
+ switch (e.alg) {
55
+ case "Ed25519":
56
+ case "EdDSA":
57
+ a = { name: "Ed25519" }, r = e.d ? ["sign"] : ["verify"];
58
+ break;
59
+ case "ECDH-ES":
60
+ case "ECDH-ES+A128KW":
61
+ case "ECDH-ES+A192KW":
62
+ case "ECDH-ES+A256KW":
63
+ a = { name: e.crv }, r = e.d ? ["deriveBits"] : [];
64
+ break;
65
+ default:
66
+ throw new s('Invalid or unsupported JWK "alg" (Algorithm) Parameter value');
67
+ }
72
68
  break;
73
69
  }
74
70
  default:
75
- throw new TypeError("CryptoKey does not support this operation");
71
+ throw new s('Invalid or unsupported JWK "kty" (Key Type) Parameter value');
76
72
  }
77
- i(t, n);
73
+ return { algorithm: a, keyUsages: r };
78
74
  }
75
+ const i = async (e) => {
76
+ if (!e.alg)
77
+ throw new TypeError('"alg" argument is required when "jwk.alg" is not present');
78
+ const { algorithm: a, keyUsages: r } = n(e), t = { ...e };
79
+ return delete t.alg, delete t.use, crypto.subtle.importKey("jwk", t, a, e.ext ?? !e.d, e.key_ops ?? r);
80
+ };
79
81
  export {
80
- u as checkSigCryptoKey
82
+ i as default
81
83
  };
@@ -0,0 +1,81 @@
1
+ function r(t, e = "algorithm.name") {
2
+ return new TypeError(`CryptoKey does not support this operation, its ${e} must be ${t}`);
3
+ }
4
+ function o(t, e) {
5
+ return t.name === e;
6
+ }
7
+ function c(t) {
8
+ return parseInt(t.name.slice(4), 10);
9
+ }
10
+ function h(t) {
11
+ switch (t) {
12
+ case "ES256":
13
+ return "P-256";
14
+ case "ES384":
15
+ return "P-384";
16
+ case "ES512":
17
+ return "P-521";
18
+ default:
19
+ throw new Error("unreachable");
20
+ }
21
+ }
22
+ function i(t, e) {
23
+ if (!t.usages.includes(e))
24
+ throw new TypeError(`CryptoKey does not support this operation, its usages must include ${e}.`);
25
+ }
26
+ function u(t, e, n) {
27
+ switch (e) {
28
+ case "HS256":
29
+ case "HS384":
30
+ case "HS512": {
31
+ if (!o(t.algorithm, "HMAC"))
32
+ throw r("HMAC");
33
+ const a = parseInt(e.slice(2), 10);
34
+ if (c(t.algorithm.hash) !== a)
35
+ throw r(`SHA-${a}`, "algorithm.hash");
36
+ break;
37
+ }
38
+ case "RS256":
39
+ case "RS384":
40
+ case "RS512": {
41
+ if (!o(t.algorithm, "RSASSA-PKCS1-v1_5"))
42
+ throw r("RSASSA-PKCS1-v1_5");
43
+ const a = parseInt(e.slice(2), 10);
44
+ if (c(t.algorithm.hash) !== a)
45
+ throw r(`SHA-${a}`, "algorithm.hash");
46
+ break;
47
+ }
48
+ case "PS256":
49
+ case "PS384":
50
+ case "PS512": {
51
+ if (!o(t.algorithm, "RSA-PSS"))
52
+ throw r("RSA-PSS");
53
+ const a = parseInt(e.slice(2), 10);
54
+ if (c(t.algorithm.hash) !== a)
55
+ throw r(`SHA-${a}`, "algorithm.hash");
56
+ break;
57
+ }
58
+ case "Ed25519":
59
+ case "EdDSA": {
60
+ if (!o(t.algorithm, "Ed25519"))
61
+ throw r("Ed25519");
62
+ break;
63
+ }
64
+ case "ES256":
65
+ case "ES384":
66
+ case "ES512": {
67
+ if (!o(t.algorithm, "ECDSA"))
68
+ throw r("ECDSA");
69
+ const a = h(e);
70
+ if (t.algorithm.namedCurve !== a)
71
+ throw r(a, "algorithm.namedCurve");
72
+ break;
73
+ }
74
+ default:
75
+ throw new TypeError("CryptoKey does not support this operation");
76
+ }
77
+ i(t, n);
78
+ }
79
+ export {
80
+ u as checkSigCryptoKey
81
+ };