@coinbase/cdp-core 0.0.10 → 0.0.12
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.
- package/README.md +24 -1
- package/dist/esm/index.js +19 -17
- package/dist/esm/index10.js +13 -115
- package/dist/esm/index100.js +11 -10
- package/dist/esm/index101.js +11 -15
- package/dist/esm/index102.js +9 -80
- package/dist/esm/index103.js +13 -75
- package/dist/esm/index104.js +83 -0
- package/dist/esm/index105.js +81 -0
- package/dist/esm/index11.js +117 -6
- package/dist/esm/index12.js +6 -16
- package/dist/esm/index13.js +16 -18
- package/dist/esm/index14.js +18 -172
- package/dist/esm/index15.js +169 -157
- package/dist/esm/index16.js +24 -46
- package/dist/esm/index17.js +37 -142
- package/dist/esm/index18.js +16 -55
- package/dist/esm/index19.js +4 -40
- package/dist/esm/index2.js +10 -10
- package/dist/esm/index20.js +5 -17
- package/dist/esm/index21.js +159 -4
- package/dist/esm/index22.js +48 -5
- package/dist/esm/index23.js +143 -6
- package/dist/esm/index24.js +56 -18
- package/dist/esm/index25.js +8 -45
- package/dist/esm/index26.js +17 -13
- package/dist/esm/index27.js +40 -50
- package/dist/esm/index28.js +13 -6
- package/dist/esm/index29.js +54 -20
- package/dist/esm/index3.js +4 -4
- package/dist/esm/index30.js +6 -11
- package/dist/esm/index31.js +21 -3
- package/dist/esm/index32.js +11 -3
- package/dist/esm/index33.js +3 -34
- package/dist/esm/index34.js +3 -12
- package/dist/esm/index35.js +34 -4
- package/dist/esm/index36.js +11 -27
- package/dist/esm/index37.js +4 -6
- package/dist/esm/index38.js +28 -24
- package/dist/esm/index39.js +6 -16
- package/dist/esm/index4.js +5 -4
- package/dist/esm/index40.js +22 -52
- package/dist/esm/index41.js +16 -23
- package/dist/esm/index42.js +53 -10
- package/dist/esm/index43.js +23 -27
- package/dist/esm/index44.js +9 -40
- package/dist/esm/index45.js +26 -54
- package/dist/esm/index46.js +42 -127
- package/dist/esm/index47.js +53 -10
- package/dist/esm/index48.js +125 -102
- package/dist/esm/index49.js +12 -6
- package/dist/esm/index5.js +92 -190
- package/dist/esm/index50.js +105 -3
- package/dist/esm/index51.js +6 -14
- package/dist/esm/index52.js +3 -43
- package/dist/esm/index53.js +14 -78
- package/dist/esm/index54.js +43 -2
- package/dist/esm/index55.js +79 -2
- package/dist/esm/index56.js +2 -13
- package/dist/esm/index57.js +2 -38
- package/dist/esm/index58.js +11 -5
- package/dist/esm/index59.js +32 -29
- package/dist/esm/index6.js +192 -12
- package/dist/esm/index60.js +6 -8
- package/dist/esm/index61.js +33 -9
- package/dist/esm/index62.js +8 -11
- package/dist/esm/index63.js +10 -13
- package/dist/esm/index64.js +11 -18
- package/dist/esm/index65.js +13 -5
- package/dist/esm/index66.js +19 -5
- package/dist/esm/index67.js +5 -10
- package/dist/esm/index68.js +5 -17
- package/dist/esm/index69.js +10 -22
- package/dist/esm/index7.js +12 -12
- package/dist/esm/index70.js +13 -47
- package/dist/esm/index71.js +23 -2
- package/dist/esm/index72.js +47 -17
- package/dist/esm/index73.js +2 -20
- package/dist/esm/index74.js +20 -79
- package/dist/esm/index75.js +18 -102
- package/dist/esm/index76.js +79 -32
- package/dist/esm/index77.js +103 -6
- package/dist/esm/index78.js +33 -10
- package/dist/esm/index79.js +6 -54
- package/dist/esm/index8.js +11 -20
- package/dist/esm/index80.js +54 -10
- package/dist/esm/index81.js +11 -2
- package/dist/esm/index82.js +2 -45
- package/dist/esm/index83.js +44 -13
- package/dist/esm/index84.js +13 -78
- package/dist/esm/index85.js +10 -5
- package/dist/esm/index86.js +78 -12
- package/dist/esm/index87.js +5 -36
- package/dist/esm/index88.js +19 -40
- package/dist/esm/index89.js +10 -21
- package/dist/esm/index9.js +20 -14
- package/dist/esm/index90.js +19 -9
- package/dist/esm/index91.js +89 -15
- package/dist/esm/index92.js +19 -92
- package/dist/esm/index93.js +115 -18
- package/dist/esm/index94.js +11 -116
- package/dist/esm/index95.js +36 -18
- package/dist/esm/index96.js +41 -27
- package/dist/esm/index97.js +18 -8
- package/dist/esm/index98.js +26 -9
- package/dist/esm/index99.js +8 -13
- package/dist/types/index.d.ts +4 -0
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -259,4 +259,27 @@ provider.on("connect", (connectInfo) => {
|
|
|
259
259
|
provider.on("disconnect", () => {
|
|
260
260
|
console.log("Disconnected from wallet");
|
|
261
261
|
});
|
|
262
|
-
```
|
|
262
|
+
```
|
|
263
|
+
|
|
264
|
+
### Viem Accounts
|
|
265
|
+
|
|
266
|
+
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.
|
|
267
|
+
|
|
268
|
+
```typescript
|
|
269
|
+
import { toViemAccount, getCurrentUser } from "@coinbase/cdp-core";
|
|
270
|
+
import { createWalletClient } from "viem";
|
|
271
|
+
import { mainnet } from "viem/chains";
|
|
272
|
+
import { http } from "viem";
|
|
273
|
+
|
|
274
|
+
const user = await getCurrentUser();
|
|
275
|
+
const evmAccount = user.evmAccounts[0];
|
|
276
|
+
|
|
277
|
+
const viemAccount = toViemAccount(evmAccount);
|
|
278
|
+
|
|
279
|
+
const client = createWalletClient({
|
|
280
|
+
account: viemAccount,
|
|
281
|
+
transport: http("https://example.com"),
|
|
282
|
+
chain: mainnet,
|
|
283
|
+
});
|
|
284
|
+
```
|
|
285
|
+
|
package/dist/esm/index.js
CHANGED
|
@@ -1,21 +1,23 @@
|
|
|
1
|
-
import { exportEvmAccount as
|
|
2
|
-
import { createCDPEmbeddedWallet as
|
|
3
|
-
import { EIP1193ProviderError as
|
|
1
|
+
import { exportEvmAccount as t, getCurrentUser as i, initialize as n, isSignedIn as o, onAuthStateChange as a, sendEvmTransaction as m, signEvmHash as s, signEvmMessage as E, signEvmTransaction as g, signEvmTypedData as v, signInWithEmail as c, signOut as d, verifyEmailOTP as p } from "./index2.js";
|
|
2
|
+
import { createCDPEmbeddedWallet as l } from "./index3.js";
|
|
3
|
+
import { EIP1193ProviderError as x, STANDARD_ERROR_CODES as A } from "./index4.js";
|
|
4
|
+
import { toViemAccount as T } from "./index5.js";
|
|
4
5
|
export {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
6
|
+
x as EIP1193ProviderError,
|
|
7
|
+
A as STANDARD_ERROR_CODES,
|
|
8
|
+
l as createCDPEmbeddedWallet,
|
|
9
|
+
t as exportEvmAccount,
|
|
10
|
+
i as getCurrentUser,
|
|
11
|
+
n as initialize,
|
|
12
|
+
o as isSignedIn,
|
|
13
|
+
a as onAuthStateChange,
|
|
14
|
+
m as sendEvmTransaction,
|
|
15
|
+
s as signEvmHash,
|
|
16
|
+
E as signEvmMessage,
|
|
16
17
|
g as signEvmTransaction,
|
|
17
18
|
v as signEvmTypedData,
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
19
|
+
c as signInWithEmail,
|
|
20
|
+
d as signOut,
|
|
21
|
+
T as toViemAccount,
|
|
22
|
+
p as verifyEmailOTP
|
|
21
23
|
};
|
package/dist/esm/index10.js
CHANGED
|
@@ -1,119 +1,17 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
* Awaitable method whose promise only resolves when the auth manager is ready to be used.
|
|
14
|
-
*
|
|
15
|
-
* @returns A promise that resolves when the auth manager is ready to be used.
|
|
16
|
-
*/
|
|
17
|
-
async ensureInitialized() {
|
|
18
|
-
return Promise.resolve();
|
|
19
|
-
}
|
|
20
|
-
/**
|
|
21
|
-
* Gets the current user, or null if there is no user signed in.
|
|
22
|
-
*
|
|
23
|
-
* @returns The current mock user if signed in, null otherwise.
|
|
24
|
-
*/
|
|
25
|
-
async getUser() {
|
|
26
|
-
return this.authState?.user ?? null;
|
|
27
|
-
}
|
|
28
|
-
/**
|
|
29
|
-
* Returns whether the user is signed in - i.e., whether there is an unexpired
|
|
30
|
-
* access token and user.
|
|
31
|
-
*
|
|
32
|
-
* @returns True if the mock user is signed in and token is not expired, false otherwise.
|
|
33
|
-
*/
|
|
34
|
-
async isSignedIn() {
|
|
35
|
-
return this.authState ? Promise.resolve(this.authState.expiresAt > Date.now()) : Promise.resolve(!1);
|
|
36
|
-
}
|
|
37
|
-
/**
|
|
38
|
-
* Signs out the user, clearing all authentication state.
|
|
39
|
-
* In the mock implementation, this simply clears the local auth state.
|
|
40
|
-
*/
|
|
41
|
-
async signOut() {
|
|
42
|
-
await this.clearAuthState();
|
|
43
|
-
}
|
|
44
|
-
/**
|
|
45
|
-
* Sets a callback to be called when the auth state changes.
|
|
46
|
-
* The callback is immediately called with the current auth state.
|
|
47
|
-
*
|
|
48
|
-
* @param callback - The function to call when the auth state changes.
|
|
49
|
-
* Will be called with the current user or null.
|
|
50
|
-
*/
|
|
51
|
-
addAuthStateChangeCallback(t) {
|
|
52
|
-
this.authStateChangeCallback = t, t(this.authState?.user ?? null);
|
|
53
|
-
}
|
|
54
|
-
/**
|
|
55
|
-
* Gets the access token. For mock, this always returns a valid token if signed in.
|
|
56
|
-
* If the current token is expired, it will be refreshed automatically.
|
|
57
|
-
*
|
|
58
|
-
* @returns The mock access token if signed in, null otherwise.
|
|
59
|
-
*/
|
|
60
|
-
async getToken() {
|
|
61
|
-
return this.authState ? (this.authState.expiresAt <= Date.now() && await this.refreshToken(), this.authState?.accessToken ?? null) : null;
|
|
62
|
-
}
|
|
63
|
-
/**
|
|
64
|
-
* Gets the currentlly registered wallet secret ID.
|
|
65
|
-
*
|
|
66
|
-
* @returns The wallet secret ID.
|
|
67
|
-
*/
|
|
68
|
-
async getWalletSecretId() {
|
|
69
|
-
return "mock-wallet-secret-id";
|
|
70
|
-
}
|
|
71
|
-
/**
|
|
72
|
-
* Gets the X-Wallet-Auth header value.
|
|
73
|
-
*
|
|
74
|
-
* @param _options - The options for the request.
|
|
75
|
-
* @param _options.requestMethod - The HTTP method of the request.
|
|
76
|
-
* @param _options.requestHost - The host of the request.
|
|
77
|
-
* @param _options.requestPath - The path of the request.
|
|
78
|
-
* @param _options.requestData - The data of the request.
|
|
79
|
-
* @returns The X-Wallet-Auth header value.
|
|
80
|
-
*/
|
|
81
|
-
async getXWalletAuth(t) {
|
|
82
|
-
if (!this.authState)
|
|
83
|
-
throw new Error("User not signed in");
|
|
84
|
-
return "x-wallet-auth";
|
|
85
|
-
}
|
|
86
|
-
/**
|
|
87
|
-
* Sets the authentication state.
|
|
88
|
-
* This will trigger the auth state change callback if one is registered.
|
|
89
|
-
*
|
|
90
|
-
* @param authState - The new authentication state to set.
|
|
91
|
-
*/
|
|
92
|
-
async setAuthState(t) {
|
|
93
|
-
this.authState = t, this.authStateChangeCallback && this.authStateChangeCallback(this.authState?.user ?? null);
|
|
94
|
-
}
|
|
95
|
-
/**
|
|
96
|
-
* Clears the authentication state.
|
|
97
|
-
* This will trigger the auth state change callback if one is registered.
|
|
98
|
-
*/
|
|
99
|
-
async clearAuthState() {
|
|
100
|
-
this.authState = null, this.authStateChangeCallback && this.authStateChangeCallback(null);
|
|
101
|
-
}
|
|
102
|
-
/**
|
|
103
|
-
* Refreshes the mock token.
|
|
104
|
-
* Only refreshes if there is an existing auth state (user is signed in).
|
|
105
|
-
* Sets a new mock token with a 24-hour expiration.
|
|
106
|
-
*
|
|
107
|
-
* @private
|
|
108
|
-
*/
|
|
109
|
-
async refreshToken() {
|
|
110
|
-
this.authState && await this.setAuthState({
|
|
111
|
-
accessToken: "mock-access-token",
|
|
112
|
-
expiresAt: Date.now() + 1e3 * 60 * 60 * 24,
|
|
113
|
-
user: e
|
|
114
|
-
});
|
|
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)}`);
|
|
115
13
|
}
|
|
116
14
|
}
|
|
117
15
|
export {
|
|
118
|
-
|
|
16
|
+
a as decryptWithPrivateKey
|
|
119
17
|
};
|
package/dist/esm/index100.js
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
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]);
|
|
8
|
+
}
|
|
9
|
+
return i(r, t, e), r;
|
|
10
|
+
};
|
|
8
11
|
export {
|
|
9
|
-
|
|
10
|
-
e as isCryptoKey,
|
|
11
|
-
r as isKeyObject
|
|
12
|
+
s as default
|
|
12
13
|
};
|
package/dist/esm/index101.js
CHANGED
|
@@ -1,19 +1,15 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
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;
|
|
4
7
|
}
|
|
5
|
-
|
|
6
|
-
|
|
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";
|
|
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);
|
|
13
11
|
}
|
|
14
12
|
export {
|
|
15
|
-
r as
|
|
16
|
-
|
|
17
|
-
e as isPublicJWK,
|
|
18
|
-
c as isSecretJWK
|
|
13
|
+
r as default,
|
|
14
|
+
t as withAlg
|
|
19
15
|
};
|
package/dist/esm/index102.js
CHANGED
|
@@ -1,83 +1,12 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
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
|
+
function e(t) {
|
|
2
|
+
return t?.[Symbol.toStringTag] === "CryptoKey";
|
|
74
3
|
}
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
return delete t.alg, delete t.use, crypto.subtle.importKey("jwk", t, a, e.ext ?? !e.d, e.key_ops ?? r);
|
|
80
|
-
};
|
|
4
|
+
function r(t) {
|
|
5
|
+
return t?.[Symbol.toStringTag] === "KeyObject";
|
|
6
|
+
}
|
|
7
|
+
const n = (t) => e(t) || r(t);
|
|
81
8
|
export {
|
|
82
|
-
|
|
9
|
+
n as default,
|
|
10
|
+
e as isCryptoKey,
|
|
11
|
+
r as isKeyObject
|
|
83
12
|
};
|
package/dist/esm/index103.js
CHANGED
|
@@ -1,81 +1,19 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
import n from "./index84.js";
|
|
2
|
+
function r(t) {
|
|
3
|
+
return n(t) && typeof t.kty == "string";
|
|
3
4
|
}
|
|
4
|
-
function o(t
|
|
5
|
-
return t.
|
|
5
|
+
function o(t) {
|
|
6
|
+
return t.kty !== "oct" && typeof t.d == "string";
|
|
6
7
|
}
|
|
7
|
-
function
|
|
8
|
-
return
|
|
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
|
-
}
|
|
8
|
+
function e(t) {
|
|
9
|
+
return t.kty !== "oct" && typeof t.d > "u";
|
|
21
10
|
}
|
|
22
|
-
function
|
|
23
|
-
|
|
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);
|
|
11
|
+
function c(t) {
|
|
12
|
+
return t.kty === "oct" && typeof t.k == "string";
|
|
78
13
|
}
|
|
79
14
|
export {
|
|
80
|
-
|
|
15
|
+
r as isJWK,
|
|
16
|
+
o as isPrivateJWK,
|
|
17
|
+
e as isPublicJWK,
|
|
18
|
+
c as isSecretJWK
|
|
81
19
|
};
|
|
@@ -0,0 +1,83 @@
|
|
|
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 };
|
|
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
|
+
};
|
|
81
|
+
export {
|
|
82
|
+
i as default
|
|
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
|
+
};
|