@coinbase/cdp-core 0.0.16 → 0.0.18

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.
@@ -1,16 +1,16 @@
1
- import { configureCdpApiClient as p, setAuthManager as w, initiateAuthentication as h, verifyEmailAuthentication as y, createEndUserEvmAccount as f, signEvmHashWithEndUserAccount as I, signEvmTransactionWithEndUserAccount as l, sendEvmTransactionWithEndUserAccount as v, signEvmMessageWithEndUserAccount as A, signEvmTypedDataWithEndUserAccount as E, exportEndUserEvmAccount as k } from "@coinbase/cdp-api-client";
2
- import { AuthManager as M } from "./index6.js";
1
+ import { configureCdpApiClient as w, setAuthManager as h, initiateAuthentication as y, verifyEmailAuthentication as f, createEndUserEvmAccount as I, signEvmHashWithEndUserAccount as l, signEvmTransactionWithEndUserAccount as A, sendEvmTransactionWithEndUserAccount as v, signEvmMessageWithEndUserAccount as E, signEvmTypedDataWithEndUserAccount as k, exportEndUserEvmAccount as M } from "@coinbase/cdp-api-client";
2
+ import { AuthManager as S } from "./index6.js";
3
3
  import { toAuthState as u } from "./index7.js";
4
4
  import { withAuth as i } from "./index8.js";
5
- import { createExportKeyPair as S } from "./index9.js";
6
- import { decryptWithPrivateKey as U } from "./index10.js";
7
- import { MockAuthManager as C } from "./index11.js";
5
+ import { createExportKeyPair as U } from "./index9.js";
6
+ import { decryptWithPrivateKey as C } from "./index10.js";
7
+ import { MockAuthManager as T } from "./index11.js";
8
8
  import { mockUser as d } from "./index12.js";
9
- import { isChainSupportedForCDPSends as T } from "./index13.js";
10
- import { getConfig as r, setCoreAuthManager as g, getCoreAuthManager as n, setConfig as j } from "./index14.js";
9
+ import { isChainSupportedForCDPSends as j } from "./index13.js";
10
+ import { getConfig as r, setCoreAuthManager as g, getCoreAuthManager as n, setConfig as P } from "./index14.js";
11
11
  import "viem";
12
12
  import { serializeTransaction as m } from "./index15.js";
13
- const q = async (e) => {
13
+ const B = async (e) => {
14
14
  if (!e.projectId)
15
15
  throw new Error("Project ID is required");
16
16
  let s;
@@ -19,19 +19,19 @@ const q = async (e) => {
19
19
  } catch {
20
20
  s = !0;
21
21
  }
22
- if (j(e), r().useMock) {
23
- g(new C(r().projectId));
22
+ if (P(e), r().useMock) {
23
+ g(new T(r().projectId));
24
24
  return;
25
25
  }
26
- if (p({
26
+ if (w({
27
27
  debugging: r().debugging,
28
28
  basePath: r().basePath
29
29
  }), s) {
30
- const t = new M(r().projectId);
31
- g(t), w(t);
30
+ const t = new S(r().projectId);
31
+ g(t), h(t);
32
32
  }
33
33
  await n().ensureInitialized();
34
- }, B = async (e) => {
34
+ }, V = async (e) => {
35
35
  if (r().useMock)
36
36
  return {
37
37
  message: "Mock sign in initiated",
@@ -39,7 +39,7 @@ const q = async (e) => {
39
39
  };
40
40
  if (await n().isSignedIn())
41
41
  throw new Error("User is already authenticated. Please sign out first.");
42
- const t = await h(r().projectId, {
42
+ const t = await y(r().projectId, {
43
43
  email: e.email,
44
44
  type: "email"
45
45
  });
@@ -47,7 +47,7 @@ const q = async (e) => {
47
47
  flowId: t.flowId,
48
48
  message: t.message
49
49
  };
50
- }, V = async (e) => {
50
+ }, G = async (e) => {
51
51
  if (r().useMock)
52
52
  return await n().setAuthState({
53
53
  accessToken: "mock-access-token",
@@ -60,28 +60,27 @@ const q = async (e) => {
60
60
  };
61
61
  if (await n().isSignedIn())
62
62
  throw new Error("User is already authenticated. Please sign out first.");
63
- const t = await y(r().projectId, {
63
+ const t = await f(r().projectId, {
64
64
  flowId: e.flowId,
65
65
  otp: e.otp
66
66
  });
67
67
  let a = u(t.accessToken, t.validUntil, t.endUser);
68
- if (!a.user.evmAccounts || a.user.evmAccounts.length === 0)
68
+ if (await n().setAuthState(a), !a.user.evmAccounts || a.user.evmAccounts.length === 0)
69
69
  try {
70
- const o = await f(r().projectId, a.user.userId, {
71
- walletSecretId: await n().getWalletSecretId(a)
70
+ const o = await n().getWalletSecretId(), p = await I(r().projectId, a.user.userId, {
71
+ walletSecretId: o
72
72
  });
73
- a = u(t.accessToken, t.validUntil, o);
73
+ a = u(t.accessToken, t.validUntil, p), await n().setAuthState(a);
74
74
  } catch (o) {
75
75
  throw new Error(`Failed to create EVM account: ${o}`);
76
76
  }
77
- await n().setAuthState(a);
78
77
  const c = await n().getUser();
79
78
  return {
80
79
  message: t.message,
81
80
  user: c,
82
81
  isNewUser: t.isNewEndUser
83
82
  };
84
- }, G = async () => n().getUser(), J = async () => n().isSignedIn(), L = async () => {
83
+ }, J = async () => n().getUser(), L = async () => n().isSignedIn(), Q = async () => {
85
84
  if (r().useMock) {
86
85
  await n().signOut();
87
86
  return;
@@ -89,18 +88,18 @@ const q = async (e) => {
89
88
  if (!await n().isSignedIn())
90
89
  throw new Error("User not signed in");
91
90
  await n().signOut();
92
- }, Q = (e) => {
91
+ }, R = (e) => {
93
92
  n().addAuthStateChangeCallback(e);
94
- }, R = async (e) => r().useMock ? { signature: "0x0" } : i(e, n(), async ({ user: s, walletSecretId: t }) => ({
95
- signature: (await I(r().projectId, s.userId, {
93
+ }, X = async (e) => r().useMock ? { signature: "0x0" } : i(e, n(), async ({ user: s, walletSecretId: t }) => ({
94
+ signature: (await l(r().projectId, s.userId, {
96
95
  hash: e.hash,
97
96
  address: e.evmAccount,
98
97
  walletSecretId: t
99
98
  })).signature
100
- })), X = async (e) => r().useMock ? { signedTransaction: "0x0" } : i(e, n(), async ({ user: s, walletSecretId: t }) => {
99
+ })), Y = async (e) => r().useMock ? { signedTransaction: "0x0" } : i(e, n(), async ({ user: s, walletSecretId: t }) => {
101
100
  const a = m(e.transaction);
102
101
  return {
103
- signedTransaction: (await l(
102
+ signedTransaction: (await A(
104
103
  r().projectId,
105
104
  s.userId,
106
105
  {
@@ -110,8 +109,8 @@ const q = async (e) => {
110
109
  }
111
110
  )).signedTransaction
112
111
  };
113
- }), Y = async (e) => {
114
- if (!T(e.network))
112
+ }), Z = async (e) => {
113
+ if (!j(e.network))
115
114
  throw new Error(`Chain ${e.network} is not supported by the CDP Apis`);
116
115
  if (r().useMock)
117
116
  return { transactionHash: "0x0" };
@@ -128,32 +127,32 @@ const q = async (e) => {
128
127
  }
129
128
  )).transactionHash
130
129
  }));
131
- }, Z = async (e) => r().useMock ? { signature: "0x0" } : i(e, n(), async ({ user: s, walletSecretId: t }) => ({
132
- signature: (await A(r().projectId, s.userId, {
130
+ }, _ = async (e) => r().useMock ? { signature: "0x0" } : i(e, n(), async ({ user: s, walletSecretId: t }) => ({
131
+ signature: (await E(r().projectId, s.userId, {
133
132
  message: e.message,
134
133
  address: e.evmAccount,
135
134
  walletSecretId: t
136
135
  })).signature
137
- })), _ = async (e) => r().useMock ? { signature: "0x0" } : i(e, n(), async ({ user: s, walletSecretId: t }) => ({
138
- signature: (await E(r().projectId, s.userId, {
136
+ })), ee = async (e) => r().useMock ? { signature: "0x0" } : i(e, n(), async ({ user: s, walletSecretId: t }) => ({
137
+ signature: (await k(r().projectId, s.userId, {
139
138
  typedData: e.typedData,
140
139
  address: e.evmAccount,
141
140
  walletSecretId: t
142
141
  })).signature
143
- })), ee = async (e) => {
142
+ })), te = async (e) => {
144
143
  if (r().useMock)
145
144
  return {
146
145
  privateKey: "mock-private-key"
147
146
  };
148
- const s = await S();
147
+ const s = await U();
149
148
  return i(e, n(), async ({ user: t, walletSecretId: a }) => {
150
- const c = await k(r().projectId, t.userId, {
149
+ const c = await M(r().projectId, t.userId, {
151
150
  address: e.evmAccount,
152
151
  walletSecretId: a,
153
152
  exportEncryptionKey: s.publicKeyBase64
154
153
  });
155
154
  return {
156
- privateKey: await U(
155
+ privateKey: await C(
157
156
  s.privateKey,
158
157
  c.encryptedPrivateKey
159
158
  )
@@ -161,17 +160,17 @@ const q = async (e) => {
161
160
  });
162
161
  };
163
162
  export {
164
- ee as exportEvmAccount,
165
- G as getCurrentUser,
166
- q as initialize,
167
- J as isSignedIn,
168
- Q as onAuthStateChange,
169
- Y as sendEvmTransaction,
170
- R as signEvmHash,
171
- Z as signEvmMessage,
172
- X as signEvmTransaction,
173
- _ as signEvmTypedData,
174
- B as signInWithEmail,
175
- L as signOut,
176
- V as verifyEmailOTP
163
+ te as exportEvmAccount,
164
+ J as getCurrentUser,
165
+ B as initialize,
166
+ L as isSignedIn,
167
+ R as onAuthStateChange,
168
+ Z as sendEvmTransaction,
169
+ X as signEvmHash,
170
+ _ as signEvmMessage,
171
+ Y as signEvmTransaction,
172
+ ee as signEvmTypedData,
173
+ V as signInWithEmail,
174
+ Q as signOut,
175
+ G as verifyEmailOTP
177
176
  };
@@ -1,13 +1,13 @@
1
- import { logOutEndUser as c, refreshAccessToken as o, registerTemporaryWalletSecret as u } from "@coinbase/cdp-api-client";
2
- import { SignJWT as S } from "./index17.js";
1
+ import { logOutEndUser as h, refreshAccessToken as l, registerTemporaryWalletSecret as c } from "@coinbase/cdp-api-client";
2
+ import { SignJWT as o } from "./index17.js";
3
3
  import "viem";
4
- import { toAuthState as w } from "./index7.js";
5
- import { createKeyPair as f } from "./index18.js";
6
- import { generateRandomId as n } from "./index19.js";
7
- import { hash as d } from "./index20.js";
8
- import { sortKeys as y } from "./index13.js";
9
- const h = 60 * 1e3;
10
- class P {
4
+ import { toAuthState as u } from "./index7.js";
5
+ import { createKeyPair as S } from "./index18.js";
6
+ import { generateRandomId as i } from "./index19.js";
7
+ import { hash as w } from "./index20.js";
8
+ import { sortKeys as f } from "./index13.js";
9
+ const n = 60 * 1e3;
10
+ class C {
11
11
  projectId;
12
12
  authState = null;
13
13
  walletSecret = null;
@@ -43,7 +43,7 @@ class P {
43
43
  */
44
44
  async signOut() {
45
45
  try {
46
- await c(this.projectId);
46
+ await h(this.projectId);
47
47
  } catch {
48
48
  } finally {
49
49
  await this.clearAuthState();
@@ -69,12 +69,12 @@ class P {
69
69
  /**
70
70
  * Gets the currently registered wallet secret ID. Rejects if the user is not signed in.
71
71
  *
72
- * @param authState - The auth state to use. If not provided, the current auth state will be used.
73
- *
74
72
  * @returns The wallet secret ID.
75
73
  */
76
- async getWalletSecretId(t) {
77
- return this.shouldRefreshWalletSecret() && await this.refreshWalletSecret(t), this.walletSecret.walletSecretId;
74
+ async getWalletSecretId() {
75
+ if (!await this.isSignedIn())
76
+ throw new Error("Cannot get wallet secret ID if the user is not signed in");
77
+ return this.shouldRefreshWalletSecret() && await this.refreshWalletSecret(), this.walletSecret.walletSecretId;
78
78
  }
79
79
  /**
80
80
  * Gets the X-Wallet-Auth header. Rejects if the user is not signed in.
@@ -87,15 +87,17 @@ class P {
87
87
  * @returns The X-Wallet-Auth header.
88
88
  */
89
89
  async getXWalletAuth(t) {
90
+ if (!await this.isSignedIn())
91
+ throw new Error("Cannot get X-Wallet-Auth header if the user is not signed in");
90
92
  this.shouldRefreshWalletSecret() && await this.refreshWalletSecret();
91
93
  const e = `${t.requestMethod} ${t.requestHost}${t.requestPath}`, a = Math.floor(Date.now() / 1e3), r = {
92
94
  uris: [e]
93
95
  };
94
96
  if (t.requestData && Object.keys(t.requestData).length > 0) {
95
- const s = y(t.requestData);
96
- r.reqHash = await d(new TextEncoder().encode(JSON.stringify(s)));
97
+ const s = f(t.requestData);
98
+ r.reqHash = await w(new TextEncoder().encode(JSON.stringify(s)));
97
99
  }
98
- return await new S(r).setProtectedHeader({ alg: "ES256", typ: "JWT" }).setIssuedAt(a).setNotBefore(a).setJti(await n()).sign(this.walletSecret.keyPair.privateKey);
100
+ return await new o(r).setProtectedHeader({ alg: "ES256", typ: "JWT" }).setIssuedAt(a).setNotBefore(a).setJti(await i()).sign(this.walletSecret.keyPair.privateKey);
99
101
  }
100
102
  /**
101
103
  * Sets the authentication state.
@@ -138,7 +140,7 @@ class P {
138
140
  * @returns True if the token should be refreshed, false otherwise.
139
141
  */
140
142
  shouldRefreshToken() {
141
- return !(this.authState && this.authState.expiresAt > Date.now() + h);
143
+ return !(this.authState && this.authState.expiresAt > Date.now() + n);
142
144
  }
143
145
  /**
144
146
  * Refreshes the access token and transitions the auth state accordingly.
@@ -148,9 +150,9 @@ class P {
148
150
  */
149
151
  async refreshAccessToken() {
150
152
  try {
151
- const t = await o(this.projectId, {
153
+ const t = await l(this.projectId, {
152
154
  grantType: "refresh_token"
153
- }), e = w(
155
+ }), e = u(
154
156
  t.accessToken,
155
157
  t.validUntil,
156
158
  t.endUser
@@ -166,30 +168,27 @@ class P {
166
168
  * @returns True if the wallet secret should be refreshed, false otherwise.
167
169
  */
168
170
  shouldRefreshWalletSecret() {
169
- return !(this.walletSecret && this.walletSecret.expiresAt > Date.now() + h);
171
+ return !(this.walletSecret && this.walletSecret.expiresAt > Date.now() + n);
170
172
  }
171
173
  /**
172
- * Refreshes the wallet secret.
174
+ * Refreshes the wallet secret. Assumes the user is signed in.
173
175
  *
174
- * @param authState - The auth state to use. If not provided, the current auth state will be used.
175
176
  * @returns The wallet secret.
176
177
  */
177
- async refreshWalletSecret(t) {
178
- if (!t && !await this.isSignedIn())
179
- throw new Error("Cannot refresh wallet secret if the auth state is not present or expired");
180
- const e = t ?? this.authState, a = this.walletSecret?.walletSecretId ?? await n(), r = this.walletSecret?.keyPair ?? await f(), s = e.expiresAt, l = new Date(s).toISOString();
181
- this.walletSecret = { walletSecretId: a, keyPair: r, expiresAt: s };
178
+ async refreshWalletSecret() {
179
+ const t = this.walletSecret?.walletSecretId ?? await i(), e = this.walletSecret?.keyPair ?? await S(), a = this.authState.expiresAt, r = new Date(a).toISOString();
180
+ this.walletSecret = { walletSecretId: t, keyPair: e, expiresAt: a };
182
181
  try {
183
- await u(this.projectId, e.user.userId, {
184
- walletSecretId: a,
185
- publicKey: r.publicKeyBase64,
186
- validUntil: l
182
+ await c(this.projectId, this.authState.user.userId, {
183
+ walletSecretId: t,
184
+ publicKey: e.publicKeyBase64,
185
+ validUntil: r
187
186
  });
188
- } catch (i) {
189
- throw this.walletSecret = null, i;
187
+ } catch (s) {
188
+ throw this.walletSecret = null, s;
190
189
  }
191
190
  }
192
191
  }
193
192
  export {
194
- P as AuthManager
193
+ C as AuthManager
195
194
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@coinbase/cdp-core",
3
- "version": "0.0.16",
3
+ "version": "0.0.18",
4
4
  "type": "module",
5
5
  "files": [
6
6
  "dist/**",
@@ -17,7 +17,7 @@
17
17
  "ox": "0.8.1",
18
18
  "viem": "^2.33.0",
19
19
  "zustand": "^5.0.6",
20
- "@coinbase/cdp-api-client": "^0.0.16"
20
+ "@coinbase/cdp-api-client": "^0.0.18"
21
21
  },
22
22
  "devDependencies": {
23
23
  "@size-limit/preset-big-lib": "^11.2.0",