@coinbase/cdp-sdk 1.38.5 → 1.39.0

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 (74) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/README.md +17 -0
  3. package/_cjs/analytics.js +164 -2
  4. package/_cjs/analytics.js.map +1 -1
  5. package/_cjs/auth/utils/http.js +2 -1
  6. package/_cjs/auth/utils/http.js.map +1 -1
  7. package/_cjs/client/end-user/endUser.js +82 -0
  8. package/_cjs/client/end-user/endUser.js.map +1 -1
  9. package/_cjs/index.js.map +1 -1
  10. package/_cjs/openapi-client/generated/coinbaseDeveloperPlatformAPIs.schemas.js +19 -2
  11. package/_cjs/openapi-client/generated/coinbaseDeveloperPlatformAPIs.schemas.js.map +1 -1
  12. package/_cjs/openapi-client/generated/end-user-accounts/end-user-accounts.js +25 -1
  13. package/_cjs/openapi-client/generated/end-user-accounts/end-user-accounts.js.map +1 -1
  14. package/_cjs/openapi-client/generated/onramp/onramp.js +1 -1
  15. package/_cjs/openapi-client/generated/sql-api-alpha/sql-api-alpha.js +1 -2
  16. package/_cjs/openapi-client/generated/sql-api-alpha/sql-api-alpha.js.map +1 -1
  17. package/_cjs/openapi-client/generated/webhooks/webhooks.js +130 -0
  18. package/_cjs/openapi-client/generated/webhooks/webhooks.js.map +1 -0
  19. package/_cjs/openapi-client/index.js +2 -0
  20. package/_cjs/openapi-client/index.js.map +1 -1
  21. package/_cjs/version.js +1 -1
  22. package/_esm/analytics.js +163 -1
  23. package/_esm/analytics.js.map +1 -1
  24. package/_esm/auth/utils/http.js +2 -1
  25. package/_esm/auth/utils/http.js.map +1 -1
  26. package/_esm/client/end-user/endUser.js +83 -1
  27. package/_esm/client/end-user/endUser.js.map +1 -1
  28. package/_esm/index.js.map +1 -1
  29. package/_esm/openapi-client/generated/coinbaseDeveloperPlatformAPIs.schemas.js +17 -0
  30. package/_esm/openapi-client/generated/coinbaseDeveloperPlatformAPIs.schemas.js.map +1 -1
  31. package/_esm/openapi-client/generated/end-user-accounts/end-user-accounts.js +22 -0
  32. package/_esm/openapi-client/generated/end-user-accounts/end-user-accounts.js.map +1 -1
  33. package/_esm/openapi-client/generated/onramp/onramp.js +1 -1
  34. package/_esm/openapi-client/generated/sql-api-alpha/sql-api-alpha.js +1 -2
  35. package/_esm/openapi-client/generated/sql-api-alpha/sql-api-alpha.js.map +1 -1
  36. package/_esm/openapi-client/generated/webhooks/webhooks.js +122 -0
  37. package/_esm/openapi-client/generated/webhooks/webhooks.js.map +1 -0
  38. package/_esm/openapi-client/index.js +2 -0
  39. package/_esm/openapi-client/index.js.map +1 -1
  40. package/_esm/version.js +1 -1
  41. package/_types/analytics.d.ts +27 -0
  42. package/_types/analytics.d.ts.map +1 -1
  43. package/_types/auth/utils/http.d.ts.map +1 -1
  44. package/_types/client/end-user/endUser.d.ts +65 -2
  45. package/_types/client/end-user/endUser.d.ts.map +1 -1
  46. package/_types/client/end-user/endUser.types.d.ts +9 -0
  47. package/_types/client/end-user/endUser.types.d.ts.map +1 -1
  48. package/_types/index.d.ts +2 -1
  49. package/_types/index.d.ts.map +1 -1
  50. package/_types/openapi-client/generated/coinbaseDeveloperPlatformAPIs.schemas.d.ts +369 -38
  51. package/_types/openapi-client/generated/coinbaseDeveloperPlatformAPIs.schemas.d.ts.map +1 -1
  52. package/_types/openapi-client/generated/end-user-accounts/end-user-accounts.d.ts +16 -1
  53. package/_types/openapi-client/generated/end-user-accounts/end-user-accounts.d.ts.map +1 -1
  54. package/_types/openapi-client/generated/onramp/onramp.d.ts +1 -1
  55. package/_types/openapi-client/generated/sql-api-alpha/sql-api-alpha.d.ts +1 -2
  56. package/_types/openapi-client/generated/sql-api-alpha/sql-api-alpha.d.ts.map +1 -1
  57. package/_types/openapi-client/generated/webhooks/webhooks.d.ts +117 -0
  58. package/_types/openapi-client/generated/webhooks/webhooks.d.ts.map +1 -0
  59. package/_types/openapi-client/index.d.ts +7 -0
  60. package/_types/openapi-client/index.d.ts.map +1 -1
  61. package/_types/version.d.ts +1 -1
  62. package/analytics.ts +225 -1
  63. package/auth/utils/http.ts +2 -1
  64. package/client/end-user/endUser.ts +99 -2
  65. package/client/end-user/endUser.types.ts +12 -0
  66. package/index.ts +2 -1
  67. package/openapi-client/generated/coinbaseDeveloperPlatformAPIs.schemas.ts +390 -38
  68. package/openapi-client/generated/end-user-accounts/end-user-accounts.ts +31 -0
  69. package/openapi-client/generated/onramp/onramp.ts +1 -1
  70. package/openapi-client/generated/sql-api-alpha/sql-api-alpha.ts +1 -2
  71. package/openapi-client/generated/webhooks/webhooks.ts +185 -0
  72. package/openapi-client/index.ts +2 -0
  73. package/package.json +1 -1
  74. package/version.ts +1 -1
package/analytics.ts CHANGED
@@ -50,6 +50,9 @@ type ActionEventData = {
50
50
 
51
51
  type EventData = ErrorEventData | ActionEventData;
52
52
 
53
+ // Symbol to store the original method on wrapped functions
54
+ const ORIGINAL_METHOD = Symbol("originalMethod");
55
+
53
56
  // This is a public client id for the analytics service
54
57
  const publicClientId = "54f2ee2fb3d2b901a829940d70fbfc13";
55
58
 
@@ -61,6 +64,23 @@ export const Analytics = {
61
64
  trackAction,
62
65
  };
63
66
 
67
+ /*
68
+ * Deprecated implementation - kept for test compatibility
69
+ * Shares the same identifier reference as Analytics
70
+ */
71
+ export const AnalyticsDeprecated = {
72
+ get identifier() {
73
+ return Analytics.identifier;
74
+ },
75
+ set identifier(value: string) {
76
+ Analytics.identifier = value;
77
+ },
78
+ wrapClassWithErrorTracking: wrapClassWithErrorTrackingDeprecated,
79
+ wrapObjectMethodsWithErrorTracking: wrapObjectMethodsWithErrorTrackingDeprecated,
80
+ sendEvent,
81
+ trackAction,
82
+ };
83
+
64
84
  /**
65
85
  * Sends an analytics event to the default endpoint
66
86
  *
@@ -150,6 +170,129 @@ function trackAction(params: {
150
170
  });
151
171
  }
152
172
 
173
+ /**
174
+ * Gets the original method from a wrapped method, or returns the method itself if it's not wrapped.
175
+ *
176
+ * @param method - The method to get the original version of.
177
+ * @returns The original unwrapped method, or the method itself if it's not wrapped.
178
+ */
179
+ function getOriginalMethod(
180
+ method: (this: unknown, ...args: unknown[]) => Promise<unknown>,
181
+ ): (this: unknown, ...args: unknown[]) => Promise<unknown> {
182
+ return (
183
+ (method as { [ORIGINAL_METHOD]?: (this: unknown, ...args: unknown[]) => Promise<unknown> })[
184
+ ORIGINAL_METHOD
185
+ ] || method
186
+ );
187
+ }
188
+
189
+ /**
190
+ * Creates an interceptor function that prevents recursive calls by checking if the instance is already executing.
191
+ *
192
+ * @param executingInstances - A WeakSet tracking instances that are currently executing.
193
+ * @param fallbackMethod - The method to call if recursion is not detected.
194
+ * @returns A function that intercepts calls and prevents recursion.
195
+ */
196
+ function createRecursiveInterceptor(
197
+ executingInstances: WeakSet<object>,
198
+ fallbackMethod: (...args: unknown[]) => Promise<unknown>,
199
+ ): (...args: unknown[]) => Promise<unknown> {
200
+ return function (this: unknown, ...callArgs: unknown[]) {
201
+ if (executingInstances.has(this as object)) {
202
+ return Promise.resolve(callArgs[0]);
203
+ }
204
+ return fallbackMethod.apply(this, callArgs);
205
+ };
206
+ }
207
+
208
+ /**
209
+ * Executes a method with recursion protection by tracking executing instances.
210
+ *
211
+ * @param executingInstances - A WeakSet tracking instances that are currently executing.
212
+ * @param originalMethod - The original method to execute.
213
+ * @param context - The context (this) to bind the method to.
214
+ * @param args - The arguments to pass to the method.
215
+ * @returns The result of executing the original method.
216
+ */
217
+ async function executeWithRecursionProtection<T>(
218
+ executingInstances: WeakSet<object>,
219
+ originalMethod: (this: unknown, ...args: unknown[]) => Promise<T>,
220
+ context: unknown,
221
+ args: unknown[],
222
+ ): Promise<T> {
223
+ if (executingInstances.has(context as object)) {
224
+ return args[0] as T;
225
+ }
226
+ executingInstances.add(context as object);
227
+ try {
228
+ return await originalMethod.apply(context, args);
229
+ } finally {
230
+ executingInstances.delete(context as object);
231
+ }
232
+ }
233
+
234
+ /**
235
+ * Handles an error that occurred in a method by sending an analytics event and rethrowing the error.
236
+ *
237
+ * @param error - The error that occurred.
238
+ * @param methodName - The name of the method where the error occurred.
239
+ */
240
+ async function handleMethodError(error: unknown, methodName: string): Promise<void> {
241
+ if (!shouldTrackError(error)) {
242
+ throw error;
243
+ }
244
+
245
+ const { message, stack } = error as Error;
246
+ sendEvent({
247
+ method: methodName,
248
+ message,
249
+ stack,
250
+ name: "error",
251
+ }).catch(() => {
252
+ // ignore error
253
+ });
254
+
255
+ throw error;
256
+ }
257
+
258
+ /**
259
+ * Creates a wrapper function that adds error tracking and recursion protection to a method.
260
+ *
261
+ * @param originalMethod - The original method to wrap.
262
+ * @param methodName - The name of the method being wrapped.
263
+ * @param executingInstances - A WeakSet tracking instances that are currently executing.
264
+ * @param setMethod - A function to set the method implementation.
265
+ * @param getMethod - A function to get the current method implementation.
266
+ * @returns A wrapped version of the method with error tracking and recursion protection.
267
+ */
268
+ function createErrorTrackingWrapper(
269
+ originalMethod: (this: unknown, ...args: unknown[]) => Promise<unknown>,
270
+ methodName: string,
271
+ executingInstances: WeakSet<object>,
272
+ setMethod: (method: (...args: unknown[]) => Promise<unknown>) => void,
273
+ getMethod: () => (...args: unknown[]) => Promise<unknown>,
274
+ ): (this: unknown, ...args: unknown[]) => Promise<unknown> {
275
+ return async function (this: unknown, ...args: unknown[]) {
276
+ const previousMethod = getMethod();
277
+ const recursiveInterceptor = createRecursiveInterceptor(executingInstances, previousMethod);
278
+ setMethod(recursiveInterceptor);
279
+
280
+ try {
281
+ const result = await executeWithRecursionProtection(
282
+ executingInstances,
283
+ originalMethod,
284
+ this,
285
+ args,
286
+ );
287
+ setMethod(previousMethod);
288
+ return result;
289
+ } catch (error) {
290
+ setMethod(previousMethod);
291
+ return handleMethodError(error, methodName);
292
+ }
293
+ };
294
+ }
295
+
153
296
  /**
154
297
  * Wraps all methods of a class with error tracking.
155
298
  *
@@ -165,6 +308,83 @@ function wrapClassWithErrorTracking(ClassToWrap: any): void {
165
308
  name => name !== "constructor" && typeof ClassToWrap.prototype[name] === "function",
166
309
  );
167
310
 
311
+ for (const method of methods) {
312
+ const currentMethod = ClassToWrap.prototype[method];
313
+ const originalMethod = getOriginalMethod(currentMethod);
314
+ const executingInstances = new WeakSet<object>();
315
+
316
+ const wrappedMethod = createErrorTrackingWrapper(
317
+ originalMethod,
318
+ method,
319
+ executingInstances,
320
+ newMethod => {
321
+ ClassToWrap.prototype[method] = newMethod as (
322
+ this: unknown,
323
+ ...args: unknown[]
324
+ ) => Promise<unknown>;
325
+ },
326
+ () => ClassToWrap.prototype[method] as (...args: unknown[]) => Promise<unknown>,
327
+ );
328
+
329
+ (wrappedMethod as unknown as { [ORIGINAL_METHOD]: unknown })[ORIGINAL_METHOD] = originalMethod;
330
+ ClassToWrap.prototype[method] = wrappedMethod;
331
+ }
332
+ }
333
+
334
+ /**
335
+ * Wraps all methods of an object with error tracking.
336
+ *
337
+ * @param object - The object whose methods should be wrapped.
338
+ */
339
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
340
+ function wrapObjectMethodsWithErrorTracking(object: any): void {
341
+ if (process.env.DISABLE_CDP_ERROR_REPORTING === "true") {
342
+ return;
343
+ }
344
+
345
+ const methods = Object.getOwnPropertyNames(object).filter(
346
+ name => name !== "constructor" && typeof object[name] === "function",
347
+ );
348
+
349
+ for (const method of methods) {
350
+ const currentMethod = object[method];
351
+ const originalMethod = getOriginalMethod(currentMethod);
352
+ const executingInstances = new WeakSet<object>();
353
+
354
+ const wrappedMethod = createErrorTrackingWrapper(
355
+ originalMethod,
356
+ method,
357
+ executingInstances,
358
+ newMethod => {
359
+ object[method] = newMethod;
360
+ },
361
+ () => object[method] as (...args: unknown[]) => Promise<unknown>,
362
+ );
363
+
364
+ (wrappedMethod as unknown as { [ORIGINAL_METHOD]: unknown })[ORIGINAL_METHOD] = originalMethod;
365
+ object[method] = wrappedMethod;
366
+ }
367
+ }
368
+
369
+ /**
370
+ * Wraps all methods of a class with error tracking.
371
+ *
372
+ * @deprecated This is the old implementation that has a bug with methods calling themselves via prototype.
373
+ * Use Analytics.wrapClassWithErrorTracking instead.
374
+ * Kept for test compatibility.
375
+ *
376
+ * @param ClassToWrap - The class whose prototype methods should be wrapped.
377
+ */
378
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
379
+ function wrapClassWithErrorTrackingDeprecated(ClassToWrap: any): void {
380
+ if (process.env.DISABLE_CDP_ERROR_REPORTING === "true") {
381
+ return;
382
+ }
383
+
384
+ const methods = Object.getOwnPropertyNames(ClassToWrap.prototype).filter(
385
+ name => name !== "constructor" && typeof ClassToWrap.prototype[name] === "function",
386
+ );
387
+
168
388
  for (const method of methods) {
169
389
  const originalMethod = ClassToWrap.prototype[method];
170
390
  ClassToWrap.prototype[method] = async function (...args: unknown[]) {
@@ -195,10 +415,14 @@ function wrapClassWithErrorTracking(ClassToWrap: any): void {
195
415
  /**
196
416
  * Wraps all methods of an object with error tracking.
197
417
  *
418
+ * @deprecated This is the old implementation that has a bug with methods calling themselves via object property.
419
+ * Use Analytics.wrapObjectMethodsWithErrorTracking instead.
420
+ * Kept for test compatibility.
421
+ *
198
422
  * @param object - The object whose methods should be wrapped.
199
423
  */
200
424
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
201
- function wrapObjectMethodsWithErrorTracking(object: any): void {
425
+ function wrapObjectMethodsWithErrorTrackingDeprecated(object: any): void {
202
426
  if (process.env.DISABLE_CDP_ERROR_REPORTING === "true") {
203
427
  return;
204
428
  }
@@ -133,7 +133,8 @@ function requiresWalletAuth(requestMethod: string, requestPath: string): boolean
133
133
  return (
134
134
  (requestPath?.includes("/accounts") ||
135
135
  requestPath?.includes("/spend-permissions") ||
136
- requestPath?.includes("/user-operations/prepare-and-send")) &&
136
+ requestPath?.includes("/user-operations/prepare-and-send") ||
137
+ requestPath?.endsWith("/end-users")) &&
137
138
  (requestMethod === "POST" || requestMethod === "DELETE" || requestMethod === "PUT")
138
139
  );
139
140
  }
@@ -1,11 +1,108 @@
1
- import { type ValidateAccessTokenOptions } from "./endUser.types.js";
1
+ import { randomUUID } from "crypto";
2
+
3
+ import {
4
+ type ValidateAccessTokenOptions,
5
+ type ListEndUsersOptions,
6
+ type CreateEndUserOptions,
7
+ } from "./endUser.types.js";
2
8
  import { Analytics } from "../../analytics.js";
3
- import { CdpOpenApiClient, type EndUser } from "../../openapi-client/index.js";
9
+ import {
10
+ CdpOpenApiClient,
11
+ type EndUser,
12
+ type ListEndUsers200,
13
+ } from "../../openapi-client/index.js";
4
14
 
5
15
  /**
6
16
  * The CDP end user client.
7
17
  */
8
18
  export class CDPEndUserClient {
19
+ /**
20
+ * Creates an end user. An end user is an entity that can own CDP EVM accounts,
21
+ * EVM smart accounts, and/or Solana accounts.
22
+ *
23
+ * @param options - The options for creating an end user.
24
+ *
25
+ * @returns A promise that resolves to the created end user.
26
+ *
27
+ * @example **Create an end user with an email authentication method**
28
+ * ```ts
29
+ * const endUser = await cdp.endUser.createEndUser({
30
+ * authenticationMethods: [
31
+ * { type: "email", email: "user@example.com" }
32
+ * ]
33
+ * });
34
+ * console.log(endUser.userId);
35
+ * ```
36
+ *
37
+ * @example **Create an end user with an EVM EOA account**
38
+ * ```ts
39
+ * const endUser = await cdp.endUser.createEndUser({
40
+ * authenticationMethods: [
41
+ * { type: "email", email: "user@example.com" }
42
+ * ],
43
+ * evmAccount: { createSmartAccount: false }
44
+ * });
45
+ * ```
46
+ */
47
+ async createEndUser(options: CreateEndUserOptions): Promise<EndUser> {
48
+ Analytics.trackAction({
49
+ action: "create_end_user",
50
+ });
51
+
52
+ const userId = options.userId ?? randomUUID();
53
+
54
+ return CdpOpenApiClient.createEndUser({
55
+ ...options,
56
+ userId,
57
+ });
58
+ }
59
+
60
+ /**
61
+ * Lists end users belonging to the developer's CDP Project.
62
+ * By default, the response is sorted by creation date in ascending order and paginated to 20 users per page.
63
+ *
64
+ * @param options - The options for listing end users.
65
+ *
66
+ * @returns A promise that resolves to a paginated list of end users.
67
+ *
68
+ * @example **List all end users**
69
+ * ```ts
70
+ * const result = await cdp.endUsers.listEndUsers();
71
+ * console.log(result.endUsers);
72
+ * ```
73
+ *
74
+ * @example **With pagination**
75
+ * ```ts
76
+ * let page = await cdp.endUsers.listEndUsers({ pageSize: 10 });
77
+ *
78
+ * while (page.nextPageToken) {
79
+ * page = await cdp.endUsers.listEndUsers({
80
+ * pageSize: 10,
81
+ * pageToken: page.nextPageToken
82
+ * });
83
+ * }
84
+ * ```
85
+ *
86
+ * @example **With sorting**
87
+ * ```ts
88
+ * const result = await cdp.endUsers.listEndUsers({
89
+ * sort: ['createdAt=desc']
90
+ * });
91
+ * ```
92
+ */
93
+ async listEndUsers(options: ListEndUsersOptions = {}): Promise<ListEndUsers200> {
94
+ Analytics.trackAction({
95
+ action: "list_end_users",
96
+ });
97
+
98
+ const params = {
99
+ ...options,
100
+ ...(options.sort && { sort: options.sort.join(",") }),
101
+ };
102
+
103
+ return CdpOpenApiClient.listEndUsers(params as ListEndUsersOptions);
104
+ }
105
+
9
106
  /**
10
107
  * Validates an end user's access token. Throws an error if the access token is invalid.
11
108
  *
@@ -1,3 +1,5 @@
1
+ import type { CreateEndUserBody, ListEndUsersParams } from "../../openapi-client/index.js";
2
+
1
3
  /**
2
4
  * The options for validating an access token.
3
5
  */
@@ -7,3 +9,13 @@ export interface ValidateAccessTokenOptions {
7
9
  */
8
10
  accessToken: string;
9
11
  }
12
+
13
+ /**
14
+ * The options for listing end users.
15
+ */
16
+ export type ListEndUsersOptions = ListEndUsersParams;
17
+
18
+ /**
19
+ * The options for creating an end user.
20
+ */
21
+ export type CreateEndUserOptions = CreateEndUserBody;
package/index.ts CHANGED
@@ -9,7 +9,8 @@ export {
9
9
  } from "./policies/types.js";
10
10
  export { NetworkError } from "./openapi-client/errors.js";
11
11
  export type { SpendPermission, SpendPermissionInput } from "./spend-permissions/types.js";
12
- export type { SpendPermissionNetwork } from "./openapi-client/index.js";
12
+ export type { SpendPermissionNetwork, ListEndUsers200, EndUser } from "./openapi-client/index.js";
13
+ export type { ListEndUsersOptions } from "./client/end-user/endUser.types.js";
13
14
  export {
14
15
  SPEND_PERMISSION_MANAGER_ABI as spendPermissionManagerAbi,
15
16
  SPEND_PERMISSION_MANAGER_ADDRESS as spendPermissionManagerAddress,