@coinbase/cdp-sdk 0.0.0 → 1.0.1

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 (87) hide show
  1. package/README.md +165 -0
  2. package/dist/accounts/evm/toEvmServerAccount.d.ts +19 -0
  3. package/dist/accounts/evm/toEvmServerAccount.js +42 -0
  4. package/dist/accounts/evm/toEvmSmartAccount.d.ts +24 -0
  5. package/dist/accounts/evm/toEvmSmartAccount.js +24 -0
  6. package/dist/accounts/types.d.ts +45 -0
  7. package/dist/accounts/types.js +2 -0
  8. package/dist/actions/evm/sendUserOperation.d.ts +95 -0
  9. package/dist/actions/evm/sendUserOperation.js +87 -0
  10. package/dist/actions/evm/waitForUserOperation.d.ts +63 -0
  11. package/dist/actions/evm/waitForUserOperation.js +60 -0
  12. package/dist/analytics.d.ts +30 -0
  13. package/dist/analytics.js +89 -0
  14. package/dist/auth/errors.d.ts +45 -0
  15. package/dist/auth/errors.js +62 -0
  16. package/dist/auth/hooks/axios/index.d.ts +1 -0
  17. package/dist/auth/hooks/axios/index.js +17 -0
  18. package/dist/auth/hooks/axios/withAuth.d.ts +37 -0
  19. package/dist/auth/hooks/axios/withAuth.js +73 -0
  20. package/dist/auth/index.d.ts +4 -0
  21. package/dist/auth/index.js +43 -0
  22. package/dist/auth/utils/http.d.ts +60 -0
  23. package/dist/auth/utils/http.js +72 -0
  24. package/dist/auth/utils/index.d.ts +2 -0
  25. package/dist/auth/utils/index.js +18 -0
  26. package/dist/auth/utils/jwt.d.ts +88 -0
  27. package/dist/auth/utils/jwt.js +235 -0
  28. package/dist/client/cdp.d.ts +43 -0
  29. package/dist/client/cdp.js +40 -0
  30. package/dist/client/evm/evm.d.ts +387 -0
  31. package/dist/client/evm/evm.js +520 -0
  32. package/dist/client/evm/evm.types.d.ts +254 -0
  33. package/dist/client/evm/evm.types.js +2 -0
  34. package/dist/client/evm/index.d.ts +1 -0
  35. package/dist/client/evm/index.js +5 -0
  36. package/dist/client/solana/index.d.ts +1 -0
  37. package/dist/client/solana/index.js +5 -0
  38. package/dist/client/solana/solana.d.ts +168 -0
  39. package/dist/client/solana/solana.js +207 -0
  40. package/dist/client/solana/solana.types.d.ts +94 -0
  41. package/dist/client/solana/solana.types.js +2 -0
  42. package/dist/constants.d.ts +4 -0
  43. package/dist/constants.js +7 -0
  44. package/dist/errors.d.ts +11 -0
  45. package/dist/errors.js +21 -0
  46. package/dist/index.d.ts +3 -0
  47. package/dist/index.js +6 -0
  48. package/dist/openapi-client/cdpApiClient.d.ts +50 -0
  49. package/dist/openapi-client/cdpApiClient.js +146 -0
  50. package/dist/openapi-client/errors.d.ts +53 -0
  51. package/dist/openapi-client/errors.js +70 -0
  52. package/dist/openapi-client/generated/coinbaseDeveloperPlatformAPIs.schemas.d.ts +345 -0
  53. package/dist/openapi-client/generated/coinbaseDeveloperPlatformAPIs.schemas.js +54 -0
  54. package/dist/openapi-client/generated/evm-accounts/evm-accounts.d.ts +62 -0
  55. package/dist/openapi-client/generated/evm-accounts/evm-accounts.js +85 -0
  56. package/dist/openapi-client/generated/evm-accounts/evm-accounts.msw.d.ts +17 -0
  57. package/dist/openapi-client/generated/evm-accounts/evm-accounts.msw.js +113 -0
  58. package/dist/openapi-client/generated/evm-smart-accounts/evm-smart-accounts.d.ts +48 -0
  59. package/dist/openapi-client/generated/evm-smart-accounts/evm-smart-accounts.js +66 -0
  60. package/dist/openapi-client/generated/evm-smart-accounts/evm-smart-accounts.msw.d.ts +15 -0
  61. package/dist/openapi-client/generated/evm-smart-accounts/evm-smart-accounts.msw.js +99 -0
  62. package/dist/openapi-client/generated/faucets/faucets.d.ts +48 -0
  63. package/dist/openapi-client/generated/faucets/faucets.js +51 -0
  64. package/dist/openapi-client/generated/faucets/faucets.msw.d.ts +7 -0
  65. package/dist/openapi-client/generated/faucets/faucets.msw.js +43 -0
  66. package/dist/openapi-client/generated/index.msw.d.ts +4 -0
  67. package/dist/openapi-client/generated/index.msw.js +20 -0
  68. package/dist/openapi-client/generated/solana-accounts/solana-accounts.d.ts +61 -0
  69. package/dist/openapi-client/generated/solana-accounts/solana-accounts.js +79 -0
  70. package/dist/openapi-client/generated/solana-accounts/solana-accounts.msw.d.ts +15 -0
  71. package/dist/openapi-client/generated/solana-accounts/solana-accounts.msw.js +99 -0
  72. package/dist/openapi-client/index.d.ts +55 -0
  73. package/dist/openapi-client/index.js +65 -0
  74. package/dist/types/calls.d.ts +13 -0
  75. package/dist/types/calls.js +4 -0
  76. package/dist/types/contract.d.ts +30 -0
  77. package/dist/types/contract.js +4 -0
  78. package/dist/types/misc.d.ts +3 -0
  79. package/dist/types/misc.js +3 -0
  80. package/dist/types/multicall.d.ts +9 -0
  81. package/dist/types/multicall.js +2 -0
  82. package/dist/types/utils.d.ts +196 -0
  83. package/dist/types/utils.js +4 -0
  84. package/dist/utils/wait.d.ts +28 -0
  85. package/dist/utils/wait.js +34 -0
  86. package/package.json +72 -5
  87. package/index.js +0 -1
@@ -0,0 +1,235 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ Object.defineProperty(exports, "__esModule", { value: true });
36
+ exports.generateJwt = generateJwt;
37
+ exports.generateWalletJwt = generateWalletJwt;
38
+ const crypto = __importStar(require("crypto"));
39
+ const crypto_1 = require("crypto");
40
+ const jose_1 = require("jose");
41
+ const errors_1 = require("../errors");
42
+ /**
43
+ * Generates a JWT (also known as a Bearer token) for authenticating with Coinbase's REST APIs.
44
+ * Supports both EC (ES256) and Ed25519 (EdDSA) keys.
45
+ *
46
+ * @param options - The configuration options for generating the JWT
47
+ * @returns The generated JWT (Bearer token) string
48
+ * @throws {Error} If required parameters are missing, invalid, or if JWT signing fails
49
+ */
50
+ async function generateJwt(options) {
51
+ // Validate required parameters
52
+ if (!options.apiKeyId) {
53
+ throw new Error("Key name is required");
54
+ }
55
+ if (!options.apiKeySecret) {
56
+ throw new Error("Private key is required");
57
+ }
58
+ if (!options.requestMethod || !options.requestHost || !options.requestPath) {
59
+ throw new Error("Request details (method, host, path) are required");
60
+ }
61
+ const now = Math.floor(Date.now() / 1000);
62
+ const expiresIn = options.expiresIn || 120; // Default to 120 seconds if not specified
63
+ const uri = `${options.requestMethod} ${options.requestHost}${options.requestPath}`;
64
+ // Prepare the JWT payload
65
+ const claims = {
66
+ sub: options.apiKeyId,
67
+ iss: "cdp",
68
+ aud: ["cdp_service"],
69
+ uris: [uri],
70
+ };
71
+ // Generate random nonce for the header
72
+ const randomNonce = nonce();
73
+ // Determine if we're using EC or Edwards key based on the key format
74
+ if (isValidECKey(options.apiKeySecret)) {
75
+ return await buildECJWT(options.apiKeySecret, options.apiKeyId, claims, now, expiresIn, randomNonce);
76
+ }
77
+ else if (isValidEd25519Key(options.apiKeySecret)) {
78
+ return await buildEdwardsJWT(options.apiKeySecret, options.apiKeyId, claims, now, expiresIn, randomNonce);
79
+ }
80
+ else {
81
+ throw new Error("Invalid key format - must be either PEM EC key or base64 Ed25519 key");
82
+ }
83
+ }
84
+ /**
85
+ * Generates a wallet authentication JWT for the given API endpoint URL.
86
+ * Used for authenticating with specific endpoints that require wallet authentication.
87
+ *
88
+ * @param options - The configuration options for generating the JWT
89
+ * @returns The generated JWT (Bearer token) string
90
+ * @throws {UndefinedWalletSecretError} If the Wallet Secret is not defined.
91
+ * @throws {InvalidWalletSecretFormatError} If the private key is not in the correct format or signing fails.
92
+ */
93
+ async function generateWalletJwt(options) {
94
+ if (!options.walletSecret) {
95
+ throw new errors_1.UndefinedWalletSecretError("Wallet Secret is not defined");
96
+ }
97
+ const uri = `${options.requestMethod} ${options.requestHost}${options.requestPath}`;
98
+ const now = Math.floor(Date.now() / 1000);
99
+ const claims = {
100
+ uris: [uri],
101
+ };
102
+ if (Object.keys(options.requestData).length > 0) {
103
+ claims.req = options.requestData;
104
+ }
105
+ try {
106
+ const ecKey = (0, crypto_1.createPrivateKey)({
107
+ key: options.walletSecret,
108
+ format: "der",
109
+ type: "pkcs8",
110
+ encoding: "base64",
111
+ });
112
+ return await new jose_1.SignJWT(claims)
113
+ .setProtectedHeader({ alg: "ES256", typ: "JWT" })
114
+ .setIssuedAt(now)
115
+ .setNotBefore(now)
116
+ .setJti(nonce())
117
+ .sign(ecKey);
118
+ }
119
+ catch (error) {
120
+ throw new errors_1.InvalidWalletSecretFormatError("Could not create the EC key: " + error);
121
+ }
122
+ }
123
+ /**
124
+ * Determines if a string could be a valid Ed25519 key
125
+ *
126
+ * @param str - The string to test
127
+ * @returns True if the string could be a valid Ed25519 key, false otherwise
128
+ */
129
+ function isValidEd25519Key(str) {
130
+ try {
131
+ const decoded = Buffer.from(str, "base64");
132
+ return decoded.length === 64;
133
+ }
134
+ catch {
135
+ return false;
136
+ }
137
+ }
138
+ /**
139
+ * Determines if a string is a valid EC private key in PEM format
140
+ *
141
+ * @param str - The string to test
142
+ * @returns True if the string is a valid EC private key in PEM format
143
+ */
144
+ function isValidECKey(str) {
145
+ try {
146
+ // Attempt to create a private key object - will throw if invalid
147
+ const key = (0, crypto_1.createPrivateKey)(str);
148
+ // Check if it's an EC key by examining its asymmetric key type
149
+ return key.asymmetricKeyType === "ec";
150
+ }
151
+ catch {
152
+ return false;
153
+ }
154
+ }
155
+ /**
156
+ * Builds a JWT using an EC key.
157
+ *
158
+ * @param privateKey - The EC private key in PEM format
159
+ * @param keyName - The key name/ID
160
+ * @param claims - The JWT claims
161
+ * @param now - Current timestamp in seconds
162
+ * @param expiresIn - Number of seconds until the token expires
163
+ * @param nonce - Random nonce for the JWT header
164
+ * @returns A JWT token signed with an EC key
165
+ * @throws {Error} If key conversion, import, or signing fails
166
+ */
167
+ async function buildECJWT(privateKey, keyName, claims, now, expiresIn, nonce) {
168
+ try {
169
+ // Convert to PKCS8 format
170
+ const keyObj = (0, crypto_1.createPrivateKey)(privateKey);
171
+ const pkcs8Key = keyObj.export({ type: "pkcs8", format: "pem" }).toString();
172
+ // Import the key for signing
173
+ const ecKey = await (0, jose_1.importPKCS8)(pkcs8Key, "ES256");
174
+ // Sign and return the JWT
175
+ return await new jose_1.SignJWT(claims)
176
+ .setProtectedHeader({ alg: "ES256", kid: keyName, typ: "JWT", nonce })
177
+ .setIssuedAt(Math.floor(now))
178
+ .setNotBefore(Math.floor(now))
179
+ .setExpirationTime(Math.floor(now + expiresIn))
180
+ .sign(ecKey);
181
+ }
182
+ catch (error) {
183
+ throw new Error(`Failed to generate EC JWT: ${error.message}`);
184
+ }
185
+ }
186
+ /**
187
+ * Builds a JWT using an Ed25519 key.
188
+ *
189
+ * @param privateKey - The Ed25519 private key in base64 format
190
+ * @param keyName - The key name/ID
191
+ * @param claims - The JWT claims
192
+ * @param now - Current timestamp in seconds
193
+ * @param expiresIn - Number of seconds until the token expires
194
+ * @param nonce - Random nonce for the JWT header
195
+ * @returns A JWT token using an Ed25519 key
196
+ * @throws {Error} If key parsing, import, or signing fails
197
+ */
198
+ async function buildEdwardsJWT(privateKey, keyName, claims, now, expiresIn, nonce) {
199
+ try {
200
+ // Decode the base64 key (expecting 64 bytes: 32 for seed + 32 for public key)
201
+ const decoded = Buffer.from(privateKey, "base64");
202
+ if (decoded.length !== 64) {
203
+ throw new Error("Invalid Ed25519 key length");
204
+ }
205
+ const seed = decoded.subarray(0, 32);
206
+ const publicKey = decoded.subarray(32);
207
+ // Create JWK from the key components
208
+ const jwk = {
209
+ kty: "OKP",
210
+ crv: "Ed25519",
211
+ d: seed.toString("base64url"),
212
+ x: publicKey.toString("base64url"),
213
+ };
214
+ // Import the key for signing
215
+ const key = await (0, jose_1.importJWK)(jwk, "EdDSA");
216
+ // Sign and return the JWT
217
+ return await new jose_1.SignJWT(claims)
218
+ .setProtectedHeader({ alg: "EdDSA", kid: keyName, typ: "JWT", nonce })
219
+ .setIssuedAt(Math.floor(now))
220
+ .setNotBefore(Math.floor(now))
221
+ .setExpirationTime(Math.floor(now + expiresIn))
222
+ .sign(key);
223
+ }
224
+ catch (error) {
225
+ throw new Error(`Failed to generate Ed25519 JWT: ${error.message}`);
226
+ }
227
+ }
228
+ /**
229
+ * Generates a random nonce for the JWT.
230
+ *
231
+ * @returns {string} The generated nonce.
232
+ */
233
+ function nonce() {
234
+ return crypto.randomBytes(16).toString("hex");
235
+ }
@@ -0,0 +1,43 @@
1
+ import { EvmClient } from "./evm";
2
+ import { SolanaClient } from "./solana";
3
+ interface CdpClientOptions {
4
+ /** The API key ID. */
5
+ apiKeyId: string;
6
+ /** The API key secret. */
7
+ apiKeySecret: string;
8
+ /** The wallet secret. */
9
+ walletSecret: string;
10
+ /** Whether to enable debugging. */
11
+ debugging?: boolean;
12
+ /** The host URL to connect to. */
13
+ basePath?: string;
14
+ }
15
+ /**
16
+ * The main client for interacting with the CDP API.
17
+ */
18
+ export declare class CdpClient {
19
+ /** Namespace containing all EVM methods. */
20
+ evm: EvmClient;
21
+ /** Namespace containing all Solana methods. */
22
+ solana: SolanaClient;
23
+ /**
24
+ * The CdpClient is the main class for interacting with the CDP API.
25
+ *
26
+ * There are a few required parameters that are configured in the [CDP Portal](https://portal.cdp.coinbase.com/projects/api-keys):
27
+ * - **CDP Secret API Key** (`apiKeyId` & `apiKeySecret`): These are used to authenticate requests to the entire suite of
28
+ * APIs offered on Coinbase Developer Platform.
29
+ * [Read more about CDP API keys](https://docs.cdp.coinbase.com/get-started/docs/cdp-api-keys).
30
+ * - **Wallet Secret** (`walletSecret`): This secret is used specifically to authenticate requests to `POST`, and `DELETE`
31
+ * endpoints in the EVM and Solana Account APIs.
32
+ *
33
+ * The CdpClient is namespaced by chain type: `evm` or `solana`.
34
+ *
35
+ * As an example, to create a new EVM account, use `cdp.evm.createAccount()`.
36
+ *
37
+ * To create a new Solana account, use `cdp.solana.createAccount()`.
38
+ *
39
+ * @param {CdpClientOptions} options - Configuration options for the CdpClient.
40
+ */
41
+ constructor(options: CdpClientOptions);
42
+ }
43
+ export {};
@@ -0,0 +1,40 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CdpClient = void 0;
4
+ const openapi_client_1 = require("../openapi-client");
5
+ const package_json_1 = require("../../package.json");
6
+ const evm_1 = require("./evm");
7
+ const solana_1 = require("./solana");
8
+ /**
9
+ * The main client for interacting with the CDP API.
10
+ */
11
+ class CdpClient {
12
+ /**
13
+ * The CdpClient is the main class for interacting with the CDP API.
14
+ *
15
+ * There are a few required parameters that are configured in the [CDP Portal](https://portal.cdp.coinbase.com/projects/api-keys):
16
+ * - **CDP Secret API Key** (`apiKeyId` & `apiKeySecret`): These are used to authenticate requests to the entire suite of
17
+ * APIs offered on Coinbase Developer Platform.
18
+ * [Read more about CDP API keys](https://docs.cdp.coinbase.com/get-started/docs/cdp-api-keys).
19
+ * - **Wallet Secret** (`walletSecret`): This secret is used specifically to authenticate requests to `POST`, and `DELETE`
20
+ * endpoints in the EVM and Solana Account APIs.
21
+ *
22
+ * The CdpClient is namespaced by chain type: `evm` or `solana`.
23
+ *
24
+ * As an example, to create a new EVM account, use `cdp.evm.createAccount()`.
25
+ *
26
+ * To create a new Solana account, use `cdp.solana.createAccount()`.
27
+ *
28
+ * @param {CdpClientOptions} options - Configuration options for the CdpClient.
29
+ */
30
+ constructor(options) {
31
+ openapi_client_1.CdpOpenApiClient.configure({
32
+ ...options,
33
+ source: "sdk",
34
+ sourceVersion: package_json_1.version,
35
+ });
36
+ this.evm = new evm_1.EvmClient();
37
+ this.solana = new solana_1.SolanaClient();
38
+ }
39
+ }
40
+ exports.CdpClient = CdpClient;