@coinbase/cdp-api-client 0.0.93 → 0.0.95

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.
@@ -2,88 +2,102 @@ import { Analytics as o } from "./index2.js";
2
2
  import "./index3.js";
3
3
  import l from "axios";
4
4
  import { ERROR_DOCS_PAGE_URL as c } from "./index9.js";
5
- import { UnknownApiError as g, HttpErrorType as u, isOpenAPIError as R, APIError as d, UnknownError as y } from "./index8.js";
5
+ import { UnknownApiError as y, HttpErrorType as u, isOpenAPIError as g, APIError as d, UnknownError as k } from "./index8.js";
6
6
  const w = () => typeof window < "u" && typeof document < "u";
7
7
  let n = l.create({
8
8
  baseURL: "https://api.cdp.coinbase.com/platform"
9
9
  }), i = null;
10
- const X = (r) => {
11
- n = l.create({
12
- baseURL: r.basePath || "https://api.cdp.coinbase.com/platform",
10
+ const O = (t) => {
11
+ if (n = l.create({
12
+ baseURL: t.basePath || "https://api.cdp.coinbase.com/platform",
13
13
  // Only enable cookies in browser environments where they're supported
14
14
  withCredentials: w()
15
- }), n.interceptors.request.use(async (e) => {
16
- const t = U(e), s = m(e);
17
- if (!i || v(s.pathname) || A(s.pathname) && !await i.isSignedIn() || !$(s.pathname))
18
- return e;
15
+ }), n.interceptors.request.use(async (r) => {
16
+ const e = U(r), s = f(r);
17
+ if (!i || v(s.pathname) || A(s.pathname) && !await i.isSignedIn() || !I(s.pathname))
18
+ return r;
19
19
  const a = await i.getXWalletAuth({
20
- requestMethod: t,
20
+ requestMethod: e,
21
21
  requestHost: s.host,
22
22
  requestPath: s.pathname,
23
- requestData: e.data
23
+ requestData: r.data
24
24
  });
25
- return a && (e.headers["X-Wallet-Auth"] = a), e;
26
- }), n.interceptors.request.use(async (e) => {
27
- const t = m(e);
28
- if (!i || v(t.pathname) || A(t.pathname) && !await i.isSignedIn())
29
- return e;
25
+ return a && (r.headers["X-Wallet-Auth"] = a), r;
26
+ }), n.interceptors.request.use(async (r) => {
27
+ const e = f(r);
28
+ if (!i || v(e.pathname) || A(e.pathname) && !await i.isSignedIn())
29
+ return r;
30
30
  const s = await i.getToken();
31
- return s && s !== "" && (e.headers.Authorization = `Bearer ${s}`), e;
32
- }), w() || n.interceptors.request.use(async (e) => {
33
- let t = e.headers["User-Agent"];
34
- return t = t ? `${t} CDP/reactnative` : "CDP/reactnative", e.headers["User-Agent"] = t, e;
35
- }), r.refreshTokenStorage && _(r.refreshTokenStorage), r.generateAssertion && k(r.generateAssertion), r.debugging && (n.interceptors.request.use((e) => (console.log("Request:", e), e)), n.interceptors.response.use((e) => (console.log("Response:", e), e)));
36
- }, O = (r) => {
37
- i = r;
38
- }, k = (r) => {
31
+ return s && s !== "" && (r.headers.Authorization = `Bearer ${s}`), r;
32
+ }), !w()) {
33
+ const r = t.platform ? `CDP/reactnative/${t.platform}` : "CDP/reactnative";
34
+ n.interceptors.request.use(async (e) => {
35
+ let s = e.headers["User-Agent"];
36
+ return s = s ? `${s} ${r}` : r, e.headers["User-Agent"] = s, e;
37
+ });
38
+ }
39
+ t.refreshTokenStorage && P(t.refreshTokenStorage), t.appAttestation && E(t.appAttestation, t.platform), t.debugging && (n.interceptors.request.use((r) => (console.log("Request:", r), r)), n.interceptors.response.use((r) => (console.log("Response:", r), r)));
40
+ }, D = (t) => {
41
+ i = t;
42
+ }, E = (t, r) => {
39
43
  n.interceptors.request.use(async (e) => {
40
- const t = m(e);
41
- if (!I(t.pathname))
44
+ const s = f(e);
45
+ if (!q(s.pathname) || !t.isEnabled())
42
46
  return e;
43
47
  try {
44
- const s = t.pathname.match(/\/projects\/([^/]+)\//);
45
- if (!s || !s[1])
46
- return e;
47
- const p = `/v2/embedded-wallet-api/projects/${s[1]}/attestation/challenge`, f = (await n.post(p)).data.challenge;
48
- if (!f)
49
- return console.warn("Failed to fetch assertion challenge"), e;
50
- const h = await r(f);
51
- return h.keyId ? (e.headers["X-App-Attestation-Assertion"] = h.assertion, e.headers["X-App-Attestation-Key-ID"] = h.keyId, e.headers["X-App-Attestation-Challenge"] = f) : e.headers["X-Integrity-Token"] = h.assertion, e;
52
- } catch (s) {
48
+ if (r === "ios") {
49
+ const a = s.pathname.match(/\/projects\/([^/]+)\//);
50
+ if (!a || !a[1])
51
+ return e;
52
+ const R = `/v2/embedded-wallet-api/projects/${a[1]}/attestation/challenge`, h = (await n.post(R)).data.challenge;
53
+ if (!h)
54
+ return console.warn("Failed to fetch assertion challenge"), e;
55
+ const p = await t.generateAssertion(h);
56
+ if (!p)
57
+ return e;
58
+ e.headers["X-App-Attestation-Assertion"] = p.assertion, e.headers["X-App-Attestation-Key-ID"] = p.keyId, e.headers["X-App-Attestation-Challenge"] = h;
59
+ } else {
60
+ const a = await t.generateAssertion(JSON.stringify(e.data));
61
+ if (!a)
62
+ return e;
63
+ e.headers["X-Integrity-Token"] = a.assertion, e.headers["X-Package-Name"] = t.appIdentifier;
64
+ }
65
+ return e;
66
+ } catch (a) {
53
67
  throw new Error(
54
- `App attestation required but assertion generation failed: ${s instanceof Error ? s.message : String(s)}`
68
+ `App attestation required but assertion generation failed: ${a instanceof Error ? a.message : String(a)}`
55
69
  );
56
70
  }
57
71
  });
58
- }, E = (r, e) => ({
59
- ...r,
72
+ }, _ = (t, r) => ({
73
+ ...t,
60
74
  headers: {
61
- ...r.headers || {},
62
- "X-Idempotency-Key": e
75
+ ...t.headers || {},
76
+ "X-Idempotency-Key": r
63
77
  }
64
- }), D = async (r, e) => {
65
- S(r), e && e !== "" && (r = E(r, e));
78
+ }), z = async (t, r) => {
79
+ S(t), r && r !== "" && (t = _(t, r));
66
80
  try {
67
- return (await n(r)).data;
68
- } catch (t) {
69
- if (l.isAxiosError(t) && !t.response)
70
- throw new g(
81
+ return (await n(t)).data;
82
+ } catch (e) {
83
+ if (l.isAxiosError(e) && !e.response)
84
+ throw new y(
71
85
  u.unknown,
72
- t.cause instanceof Error ? t.cause.message : t.message,
73
- t.cause
86
+ e.cause instanceof Error ? e.cause.message : e.message,
87
+ e.cause
74
88
  );
75
- if (l.isAxiosError(t) && t.response) {
76
- if (R(t.response.data))
89
+ if (l.isAxiosError(e) && e.response) {
90
+ if (g(e.response.data))
77
91
  throw new d(
78
- t.response.status,
79
- t.response.data.errorType,
80
- t.response.data.errorMessage,
81
- t.response.data.correlationId,
82
- t.response.data.errorLink,
83
- t.cause
92
+ e.response.status,
93
+ e.response.data.errorType,
94
+ e.response.data.errorMessage,
95
+ e.response.data.correlationId,
96
+ e.response.data.errorLink,
97
+ e.cause
84
98
  );
85
99
  {
86
- const s = t.response.status;
100
+ const s = e.response.status;
87
101
  switch (s) {
88
102
  case 401:
89
103
  throw new d(
@@ -92,7 +106,7 @@ const X = (r) => {
92
106
  "Unauthorized.",
93
107
  void 0,
94
108
  `${c}#unauthorized`,
95
- t.cause
109
+ e.cause
96
110
  );
97
111
  case 404:
98
112
  throw new d(
@@ -101,7 +115,7 @@ const X = (r) => {
101
115
  "API not found.",
102
116
  void 0,
103
117
  `${c}#not_found`,
104
- t.cause
118
+ e.cause
105
119
  );
106
120
  case 502:
107
121
  throw new d(
@@ -110,7 +124,7 @@ const X = (r) => {
110
124
  "Bad gateway.",
111
125
  void 0,
112
126
  `${c}`,
113
- t.cause
127
+ e.cause
114
128
  );
115
129
  case 503:
116
130
  throw new d(
@@ -119,47 +133,44 @@ const X = (r) => {
119
133
  "Service unavailable. Please try again later.",
120
134
  void 0,
121
135
  `${c}`,
122
- t.cause
136
+ e.cause
123
137
  );
124
138
  default: {
125
139
  let a = "";
126
- if (t.response.data)
140
+ if (e.response.data)
127
141
  try {
128
- a = JSON.stringify(t.response.data);
142
+ a = JSON.stringify(e.response.data);
129
143
  } catch {
130
- a = String(t.response.data);
144
+ a = String(e.response.data);
131
145
  }
132
- const p = a ? `An unexpected error occurred: ${a}` : "An unexpected error occurred.";
146
+ const m = a ? `An unexpected error occurred: ${a}` : "An unexpected error occurred.";
133
147
  throw new d(
134
148
  s,
135
149
  u.unexpected_error,
136
- p,
150
+ m,
137
151
  void 0,
138
152
  `${c}`,
139
- t.cause
153
+ e.cause
140
154
  );
141
155
  }
142
156
  }
143
157
  }
144
158
  }
145
- throw new y(
146
- "Something went wrong. Please reach out at https://discord.com/channels/1220414409550336183/1271495764580896789 for help.",
147
- t instanceof Error ? t : void 0
148
- );
159
+ throw new k("Something went wrong.", e instanceof Error ? e : void 0);
149
160
  }
150
- }, _ = (r) => {
161
+ }, P = (t) => {
151
162
  n.interceptors.response.use(
152
- async (e) => {
163
+ async (r) => {
153
164
  try {
154
- if (T(e)) {
165
+ if ($(r)) {
155
166
  o.sendSessionRefreshEvent({
156
167
  name: "refresh_token_response"
157
168
  });
158
- const t = e.data?.refreshToken;
159
- t && (o.sendSessionRefreshEvent({
169
+ const e = r.data?.refreshToken;
170
+ e && (o.sendSessionRefreshEvent({
160
171
  name: "refresh_token_received"
161
172
  }), await Promise.race([
162
- r.setRefreshToken(t),
173
+ t.setRefreshToken(e),
163
174
  new Promise(
164
175
  (s, a) => setTimeout(() => a(new Error("setRefreshToken timed out after 5000ms")), 5e3)
165
176
  )
@@ -167,71 +178,71 @@ const X = (r) => {
167
178
  name: "refresh_token_stored"
168
179
  }));
169
180
  }
170
- } catch (t) {
171
- const s = t instanceof Error ? t.message : String(t);
181
+ } catch (e) {
182
+ const s = e instanceof Error ? e.message : String(e);
172
183
  o.sendSessionRefreshEvent({
173
184
  name: "refresh_token_store_failed",
174
185
  error_message: s
175
- }), console.warn("Failed to store refresh token:", t);
186
+ }), console.warn("Failed to store refresh token:", e);
176
187
  }
177
- return e;
188
+ return r;
178
189
  },
179
- (e) => Promise.reject(e)
180
- ), n.interceptors.request.use(async (e) => {
190
+ (r) => Promise.reject(r)
191
+ ), n.interceptors.request.use(async (r) => {
181
192
  try {
182
- if (b(e.url)) {
183
- const t = await r.getRefreshToken();
184
- t ? (o.sendSessionRefreshEvent({
193
+ if (T(r.url)) {
194
+ const e = await t.getRefreshToken();
195
+ e ? (o.sendSessionRefreshEvent({
185
196
  name: "refresh_token_retrieved"
186
- }), e.data = {
187
- ...e.data,
188
- refreshToken: t
197
+ }), r.data = {
198
+ ...r.data,
199
+ refreshToken: e
189
200
  }) : o.sendSessionRefreshEvent({
190
201
  name: "refresh_token_missing"
191
202
  });
192
203
  }
193
- P(e.url) && await r.removeRefreshToken();
194
- } catch (t) {
195
- const s = t instanceof Error ? t.message : String(t);
204
+ b(r.url) && await t.removeRefreshToken();
205
+ } catch (e) {
206
+ const s = e instanceof Error ? e.message : String(e);
196
207
  o.sendSessionRefreshEvent({
197
208
  name: "refresh_token_retrieve_failed",
198
209
  error_message: s
199
- }), console.warn("Failed to retrieve refresh token:", t);
210
+ }), console.warn("Failed to retrieve refresh token:", e);
200
211
  }
201
- return e;
212
+ return r;
202
213
  });
203
- }, P = (r) => r ? /^\/v2\/embedded-wallet-api\/projects\/[^/]+\/auth\/logout$/.test(r) : !1, b = (r) => r ? /^\/v2\/embedded-wallet-api\/projects\/[^/]+\/auth\/(refresh|logout)$/.test(r) : !1, T = (r) => {
204
- const e = r.config.url;
205
- return e ? /^\/v2\/embedded-wallet-api\/projects\/[^/]+\/auth\/(verify\/(email|sms|oauth\/.*)|refresh)$/.test(
206
- e
214
+ }, b = (t) => t ? /^\/v2\/embedded-wallet-api\/projects\/[^/]+\/auth\/logout$/.test(t) : !1, T = (t) => t ? /^\/v2\/embedded-wallet-api\/projects\/[^/]+\/auth\/(refresh|logout)$/.test(t) : !1, $ = (t) => {
215
+ const r = t.config.url;
216
+ return r ? /^\/v2\/embedded-wallet-api\/projects\/[^/]+\/auth\/(verify\/(email|sms|oauth\/.*)|refresh)$/.test(
217
+ r
207
218
  ) : !1;
208
- }, S = (r) => {
219
+ }, S = (t) => {
209
220
  if (!n.getUri() || n.getUri() === "")
210
221
  throw new Error("CDP client URI not configured. Call configure() first.");
211
- if (!r.url || r.url === "")
222
+ if (!t.url || t.url === "")
212
223
  throw new Error("AxiosRequestConfig URL is empty. This should never happen.");
213
- if (!r.method || r.method === "")
224
+ if (!t.method || t.method === "")
214
225
  throw new Error("AxiosRequestConfig method is empty. This should never happen.");
215
- }, U = (r) => r.method?.toString().toUpperCase() || "GET", m = (r) => {
216
- if (!r.url)
226
+ }, U = (t) => t.method?.toString().toUpperCase() || "GET", f = (t) => {
227
+ if (!t.url)
217
228
  throw new Error("URL is required for authentication");
218
- const e = n.getUri() + r.url;
219
- return new URL(e);
220
- }, v = (r) => {
221
- const e = /^\/platform\/v2\/embedded-wallet-api\/projects\/[^/]+\/auth\/(refresh|logout)$/, t = /^\/platform\/v2\/embedded-wallet-api\/projects\/[^/]+\/auth\/(mfa)$/, s = /^\/platform\/v2\/embedded-wallet-api\/projects\/[^/]+\/config$/, a = /^\/platform\/v2\/embedded-wallet-api\/projects\/[^/]+\/attestation\/(challenge|register)$/;
222
- return e.test(r) || t.test(r) || s.test(r) || a.test(r);
223
- }, $ = (r) => !/^\/platform\/v2\/embedded-wallet-api\/projects\/[^/]+\/auth\/custom\/authenticate$/.test(r), A = (r) => {
224
- const e = /^\/platform\/v2\/embedded-wallet-api\/projects\/[^/]+\/auth\/(init)$/, t = /^\/platform\/v2\/embedded-wallet-api\/projects\/[^/]+\/auth\/verify\//;
225
- return e.test(r) || t.test(r);
226
- }, I = (r) => [
229
+ const r = n.getUri() + t.url;
230
+ return new URL(r);
231
+ }, v = (t) => {
232
+ const r = /^\/platform\/v2\/embedded-wallet-api\/projects\/[^/]+\/auth\/(refresh|logout)$/, e = /^\/platform\/v2\/embedded-wallet-api\/projects\/[^/]+\/auth\/(mfa)$/, s = /^\/platform\/v2\/embedded-wallet-api\/projects\/[^/]+\/config$/, a = /^\/platform\/v2\/embedded-wallet-api\/projects\/[^/]+\/attestation\/(challenge|register)$/;
233
+ return r.test(t) || e.test(t) || s.test(t) || a.test(t);
234
+ }, I = (t) => !/^\/platform\/v2\/embedded-wallet-api\/projects\/[^/]+\/auth\/custom\/authenticate$/.test(t), A = (t) => {
235
+ const r = /^\/platform\/v2\/embedded-wallet-api\/projects\/[^/]+\/auth\/(init)$/, e = /^\/platform\/v2\/embedded-wallet-api\/projects\/[^/]+\/auth\/verify\//;
236
+ return r.test(t) || e.test(t);
237
+ }, q = (t) => [
227
238
  // Auth flows: prevent account takeover on mobile clients
228
239
  /\/auth\/verify\//,
229
240
  // VerifyEmailAuthentication, VerifySmsAuthentication, VerifyOAuthCode, VerifyOAuthEndUserIdentity
230
241
  /\/auth\/refresh$/
231
242
  // RefreshAccessToken
232
- ].some((t) => t.test(r));
243
+ ].some((e) => e.test(t));
233
244
  export {
234
- D as cdpApiClient,
235
- X as configureCdpApiClient,
236
- O as setAuthManager
245
+ z as cdpApiClient,
246
+ O as configureCdpApiClient,
247
+ D as setAuthManager
237
248
  };