@coinbase/cdp-sdk 1.1.2 → 1.3.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.
- package/CHANGELOG.md +91 -0
- package/LICENSE +21 -0
- package/README.md +88 -1
- package/{dist → _cjs}/accounts/evm/toEvmServerAccount.js +1 -0
- package/_cjs/accounts/evm/toEvmServerAccount.js.map +1 -0
- package/{dist → _cjs}/accounts/evm/toEvmSmartAccount.js +1 -0
- package/_cjs/accounts/evm/toEvmSmartAccount.js.map +1 -0
- package/{dist → _cjs}/accounts/types.js +1 -0
- package/_cjs/accounts/types.js.map +1 -0
- package/{dist → _cjs}/actions/evm/sendUserOperation.js +1 -0
- package/_cjs/actions/evm/sendUserOperation.js.map +1 -0
- package/{dist → _cjs}/actions/evm/waitForUserOperation.js +10 -9
- package/_cjs/actions/evm/waitForUserOperation.js.map +1 -0
- package/{dist → _cjs}/analytics.js +32 -12
- package/_cjs/analytics.js.map +1 -0
- package/{dist → _cjs}/auth/errors.js +1 -0
- package/_cjs/auth/errors.js.map +1 -0
- package/{dist → _cjs}/auth/hooks/axios/index.js +2 -1
- package/_cjs/auth/hooks/axios/index.js.map +1 -0
- package/{dist → _cjs}/auth/hooks/axios/withAuth.js +4 -2
- package/_cjs/auth/hooks/axios/withAuth.js.map +1 -0
- package/{dist → _cjs}/auth/index.js +4 -3
- package/_cjs/auth/index.js.map +1 -0
- package/{dist → _cjs}/auth/utils/http.js +6 -5
- package/_cjs/auth/utils/http.js.map +1 -0
- package/{dist → _cjs}/auth/utils/index.js +3 -2
- package/_cjs/auth/utils/index.js.map +1 -0
- package/{dist → _cjs}/auth/utils/jwt.js +4 -3
- package/_cjs/auth/utils/jwt.js.map +1 -0
- package/_cjs/client/cdp.js +101 -0
- package/_cjs/client/cdp.js.map +1 -0
- package/_cjs/client/evm/evm.js +641 -0
- package/_cjs/client/evm/evm.js.map +1 -0
- package/{dist → _cjs}/client/evm/evm.types.js +1 -0
- package/_cjs/client/evm/evm.types.js.map +1 -0
- package/{dist → _cjs}/client/solana/index.js +3 -2
- package/_cjs/client/solana/index.js.map +1 -0
- package/{dist → _cjs}/client/solana/solana.js +9 -8
- package/_cjs/client/solana/solana.js.map +1 -0
- package/{dist → _cjs}/client/solana/solana.types.js +1 -0
- package/_cjs/client/solana/solana.types.js.map +1 -0
- package/{dist → _cjs}/constants.js +1 -0
- package/_cjs/constants.js.map +1 -0
- package/{dist → _cjs}/errors.js +1 -0
- package/_cjs/errors.js.map +1 -0
- package/{dist → _cjs}/index.js +4 -3
- package/_cjs/index.js.map +1 -0
- package/_cjs/openapi-client/cdpApiClient.js +116 -0
- package/_cjs/openapi-client/cdpApiClient.js.map +1 -0
- package/{dist → _cjs}/openapi-client/errors.js +44 -3
- package/_cjs/openapi-client/errors.js.map +1 -0
- package/_cjs/openapi-client/generated/coinbaseDeveloperPlatformAPIs.schemas.js +66 -0
- package/_cjs/openapi-client/generated/coinbaseDeveloperPlatformAPIs.schemas.js.map +1 -0
- package/_cjs/openapi-client/generated/evm-accounts/evm-accounts.js +125 -0
- package/_cjs/openapi-client/generated/evm-accounts/evm-accounts.js.map +1 -0
- package/_cjs/openapi-client/generated/evm-accounts/evm-accounts.msw.js +160 -0
- package/_cjs/openapi-client/generated/evm-accounts/evm-accounts.msw.js.map +1 -0
- package/_cjs/openapi-client/generated/evm-smart-accounts/evm-smart-accounts.js +70 -0
- package/_cjs/openapi-client/generated/evm-smart-accounts/evm-smart-accounts.js.map +1 -0
- package/_cjs/openapi-client/generated/evm-smart-accounts/evm-smart-accounts.msw.js +188 -0
- package/_cjs/openapi-client/generated/evm-smart-accounts/evm-smart-accounts.msw.js.map +1 -0
- package/_cjs/openapi-client/generated/evm-token-balances/evm-token-balances.js +15 -0
- package/_cjs/openapi-client/generated/evm-token-balances/evm-token-balances.js.map +1 -0
- package/_cjs/openapi-client/generated/evm-token-balances/evm-token-balances.msw.js +45 -0
- package/_cjs/openapi-client/generated/evm-token-balances/evm-token-balances.msw.js.map +1 -0
- package/{dist → _cjs}/openapi-client/generated/faucets/faucets.js +20 -14
- package/_cjs/openapi-client/generated/faucets/faucets.js.map +1 -0
- package/{dist → _cjs}/openapi-client/generated/faucets/faucets.msw.js +16 -12
- package/_cjs/openapi-client/generated/faucets/faucets.msw.js.map +1 -0
- package/{dist → _cjs}/openapi-client/generated/index.msw.js +6 -4
- package/_cjs/openapi-client/generated/index.msw.js.map +1 -0
- package/{dist → _cjs}/openapi-client/generated/solana-accounts/solana-accounts.js +38 -35
- package/_cjs/openapi-client/generated/solana-accounts/solana-accounts.js.map +1 -0
- package/{dist → _cjs}/openapi-client/generated/solana-accounts/solana-accounts.msw.js +68 -35
- package/_cjs/openapi-client/generated/solana-accounts/solana-accounts.msw.js.map +1 -0
- package/{dist → _cjs}/openapi-client/index.js +16 -11
- package/_cjs/openapi-client/index.js.map +1 -0
- package/_cjs/package.json +1 -0
- package/{dist → _cjs}/types/calls.js +1 -0
- package/_cjs/types/calls.js.map +1 -0
- package/{dist → _cjs}/types/contract.js +1 -0
- package/_cjs/types/contract.js.map +1 -0
- package/{dist → _cjs}/types/misc.js +1 -0
- package/_cjs/types/misc.js.map +1 -0
- package/{dist → _cjs}/types/multicall.js +1 -0
- package/_cjs/types/multicall.js.map +1 -0
- package/{dist → _cjs}/types/utils.js +1 -0
- package/_cjs/types/utils.js.map +1 -0
- package/{dist → _cjs}/utils/wait.js +3 -2
- package/_cjs/utils/wait.js.map +1 -0
- package/_cjs/version.js +5 -0
- package/_cjs/version.js.map +1 -0
- package/_esm/accounts/evm/toEvmServerAccount.js +40 -0
- package/_esm/accounts/evm/toEvmServerAccount.js.map +1 -0
- package/_esm/accounts/evm/toEvmSmartAccount.js +22 -0
- package/_esm/accounts/evm/toEvmSmartAccount.js.map +1 -0
- package/_esm/accounts/types.js +2 -0
- package/_esm/accounts/types.js.map +1 -0
- package/_esm/actions/evm/sendUserOperation.js +85 -0
- package/_esm/actions/evm/sendUserOperation.js.map +1 -0
- package/_esm/actions/evm/waitForUserOperation.js +58 -0
- package/_esm/actions/evm/waitForUserOperation.js.map +1 -0
- package/_esm/analytics.js +101 -0
- package/_esm/analytics.js.map +1 -0
- package/_esm/auth/errors.js +57 -0
- package/_esm/auth/errors.js.map +1 -0
- package/_esm/auth/hooks/axios/index.js +2 -0
- package/_esm/auth/hooks/axios/index.js.map +1 -0
- package/_esm/auth/hooks/axios/withAuth.js +72 -0
- package/_esm/auth/hooks/axios/withAuth.js.map +1 -0
- package/_esm/auth/index.js +5 -0
- package/_esm/auth/index.js.map +1 -0
- package/_esm/auth/utils/http.js +70 -0
- package/_esm/auth/utils/http.js.map +1 -0
- package/_esm/auth/utils/index.js +3 -0
- package/_esm/auth/utils/index.js.map +1 -0
- package/_esm/auth/utils/jwt.js +199 -0
- package/_esm/auth/utils/jwt.js.map +1 -0
- package/{dist → _esm}/client/cdp.js +16 -13
- package/_esm/client/cdp.js.map +1 -0
- package/{dist → _esm}/client/evm/evm.js +146 -30
- package/_esm/client/evm/evm.js.map +1 -0
- package/_esm/client/evm/evm.types.js +2 -0
- package/_esm/client/evm/evm.types.js.map +1 -0
- package/_esm/client/solana/index.js +2 -0
- package/_esm/client/solana/index.js.map +1 -0
- package/_esm/client/solana/solana.js +204 -0
- package/_esm/client/solana/solana.js.map +1 -0
- package/_esm/client/solana/solana.types.js +2 -0
- package/_esm/client/solana/solana.types.js.map +1 -0
- package/_esm/constants.js +5 -0
- package/_esm/constants.js.map +1 -0
- package/_esm/errors.js +18 -0
- package/_esm/errors.js.map +1 -0
- package/_esm/index.js +3 -0
- package/_esm/index.js.map +1 -0
- package/_esm/openapi-client/cdpApiClient.js +108 -0
- package/_esm/openapi-client/cdpApiClient.js.map +1 -0
- package/_esm/openapi-client/errors.js +104 -0
- package/_esm/openapi-client/errors.js.map +1 -0
- package/_esm/openapi-client/generated/coinbaseDeveloperPlatformAPIs.schemas.js +63 -0
- package/_esm/openapi-client/generated/coinbaseDeveloperPlatformAPIs.schemas.js.map +1 -0
- package/_esm/openapi-client/generated/evm-accounts/evm-accounts.js +114 -0
- package/_esm/openapi-client/generated/evm-accounts/evm-accounts.js.map +1 -0
- package/_esm/openapi-client/generated/evm-accounts/evm-accounts.msw.js +140 -0
- package/_esm/openapi-client/generated/evm-accounts/evm-accounts.msw.js.map +1 -0
- package/_esm/openapi-client/generated/evm-smart-accounts/evm-smart-accounts.js +61 -0
- package/_esm/openapi-client/generated/evm-smart-accounts/evm-smart-accounts.js.map +1 -0
- package/_esm/openapi-client/generated/evm-smart-accounts/evm-smart-accounts.msw.js +172 -0
- package/_esm/openapi-client/generated/evm-smart-accounts/evm-smart-accounts.msw.js.map +1 -0
- package/_esm/openapi-client/generated/evm-token-balances/evm-token-balances.js +11 -0
- package/_esm/openapi-client/generated/evm-token-balances/evm-token-balances.js.map +1 -0
- package/_esm/openapi-client/generated/evm-token-balances/evm-token-balances.msw.js +39 -0
- package/_esm/openapi-client/generated/evm-token-balances/evm-token-balances.msw.js.map +1 -0
- package/_esm/openapi-client/generated/faucets/faucets.js +52 -0
- package/_esm/openapi-client/generated/faucets/faucets.js.map +1 -0
- package/_esm/openapi-client/generated/faucets/faucets.msw.js +39 -0
- package/_esm/openapi-client/generated/faucets/faucets.msw.js.map +1 -0
- package/_esm/openapi-client/generated/index.msw.js +6 -0
- package/_esm/openapi-client/generated/index.msw.js.map +1 -0
- package/_esm/openapi-client/generated/solana-accounts/solana-accounts.js +73 -0
- package/_esm/openapi-client/generated/solana-accounts/solana-accounts.js.map +1 -0
- package/_esm/openapi-client/generated/solana-accounts/solana-accounts.msw.js +116 -0
- package/_esm/openapi-client/generated/solana-accounts/solana-accounts.msw.js.map +1 -0
- package/_esm/openapi-client/index.js +31 -0
- package/_esm/openapi-client/index.js.map +1 -0
- package/_esm/package.json +1 -0
- package/_esm/types/calls.js +4 -0
- package/_esm/types/calls.js.map +1 -0
- package/_esm/types/contract.js +4 -0
- package/_esm/types/contract.js.map +1 -0
- package/_esm/types/misc.js +3 -0
- package/_esm/types/misc.js.map +1 -0
- package/_esm/types/multicall.js +2 -0
- package/_esm/types/multicall.js.map +1 -0
- package/_esm/types/utils.js +4 -0
- package/_esm/types/utils.js.map +1 -0
- package/_esm/utils/wait.js +32 -0
- package/_esm/utils/wait.js.map +1 -0
- package/_esm/version.js +2 -0
- package/_esm/version.js.map +1 -0
- package/{dist → _types}/accounts/evm/toEvmServerAccount.d.ts +3 -2
- package/_types/accounts/evm/toEvmServerAccount.d.ts.map +1 -0
- package/{dist → _types}/accounts/evm/toEvmSmartAccount.d.ts +3 -2
- package/_types/accounts/evm/toEvmSmartAccount.d.ts.map +1 -0
- package/{dist → _types}/accounts/types.d.ts +3 -2
- package/_types/accounts/types.d.ts.map +1 -0
- package/{dist → _types}/actions/evm/sendUserOperation.d.ts +5 -5
- package/_types/actions/evm/sendUserOperation.d.ts.map +1 -0
- package/{dist → _types}/actions/evm/waitForUserOperation.d.ts +4 -3
- package/_types/actions/evm/waitForUserOperation.d.ts.map +1 -0
- package/{dist → _types}/analytics.d.ts +16 -0
- package/_types/analytics.d.ts.map +1 -0
- package/{dist → _types}/auth/errors.d.ts +1 -0
- package/_types/auth/errors.d.ts.map +1 -0
- package/_types/auth/hooks/axios/index.d.ts +2 -0
- package/_types/auth/hooks/axios/index.d.ts.map +1 -0
- package/{dist → _types}/auth/hooks/axios/withAuth.d.ts +1 -0
- package/_types/auth/hooks/axios/withAuth.d.ts.map +1 -0
- package/_types/auth/index.d.ts +5 -0
- package/_types/auth/index.d.ts.map +1 -0
- package/{dist → _types}/auth/utils/http.d.ts +1 -0
- package/_types/auth/utils/http.d.ts.map +1 -0
- package/_types/auth/utils/index.d.ts +3 -0
- package/_types/auth/utils/index.d.ts.map +1 -0
- package/{dist → _types}/auth/utils/jwt.d.ts +1 -0
- package/_types/auth/utils/jwt.d.ts.map +1 -0
- package/{dist → _types}/client/cdp.d.ts +3 -2
- package/_types/client/cdp.d.ts.map +1 -0
- package/{dist → _types}/client/evm/evm.d.ts +86 -3
- package/_types/client/evm/evm.d.ts.map +1 -0
- package/{dist → _types}/client/evm/evm.types.d.ts +104 -7
- package/_types/client/evm/evm.types.d.ts.map +1 -0
- package/_types/client/solana/index.d.ts +2 -0
- package/_types/client/solana/index.d.ts.map +1 -0
- package/{dist → _types}/client/solana/solana.d.ts +2 -1
- package/_types/client/solana/solana.d.ts.map +1 -0
- package/{dist → _types}/client/solana/solana.types.d.ts +2 -1
- package/_types/client/solana/solana.types.d.ts.map +1 -0
- package/{dist → _types}/constants.d.ts +1 -0
- package/_types/constants.d.ts.map +1 -0
- package/{dist → _types}/errors.d.ts +1 -0
- package/_types/errors.d.ts.map +1 -0
- package/_types/index.d.ts +4 -0
- package/_types/index.d.ts.map +1 -0
- package/{dist → _types}/openapi-client/cdpApiClient.d.ts +1 -0
- package/_types/openapi-client/cdpApiClient.d.ts.map +1 -0
- package/{dist → _types}/openapi-client/errors.d.ts +30 -2
- package/_types/openapi-client/errors.d.ts.map +1 -0
- package/{dist → _types}/openapi-client/generated/coinbaseDeveloperPlatformAPIs.schemas.d.ts +100 -14
- package/_types/openapi-client/generated/coinbaseDeveloperPlatformAPIs.schemas.d.ts.map +1 -0
- package/_types/openapi-client/generated/evm-accounts/evm-accounts.d.ts +91 -0
- package/_types/openapi-client/generated/evm-accounts/evm-accounts.d.ts.map +1 -0
- package/{dist → _types}/openapi-client/generated/evm-accounts/evm-accounts.msw.d.ts +5 -2
- package/_types/openapi-client/generated/evm-accounts/evm-accounts.msw.d.ts.map +1 -0
- package/{dist → _types}/openapi-client/generated/evm-smart-accounts/evm-smart-accounts.d.ts +22 -20
- package/_types/openapi-client/generated/evm-smart-accounts/evm-smart-accounts.d.ts.map +1 -0
- package/{dist → _types}/openapi-client/generated/evm-smart-accounts/evm-smart-accounts.msw.d.ts +3 -2
- package/_types/openapi-client/generated/evm-smart-accounts/evm-smart-accounts.msw.d.ts.map +1 -0
- package/_types/openapi-client/generated/evm-token-balances/evm-token-balances.d.ts +20 -0
- package/_types/openapi-client/generated/evm-token-balances/evm-token-balances.d.ts.map +1 -0
- package/_types/openapi-client/generated/evm-token-balances/evm-token-balances.msw.d.ts +6 -0
- package/_types/openapi-client/generated/evm-token-balances/evm-token-balances.msw.d.ts.map +1 -0
- package/{dist → _types}/openapi-client/generated/faucets/faucets.d.ts +11 -9
- package/_types/openapi-client/generated/faucets/faucets.d.ts.map +1 -0
- package/{dist → _types}/openapi-client/generated/faucets/faucets.msw.d.ts +3 -2
- package/_types/openapi-client/generated/faucets/faucets.msw.d.ts.map +1 -0
- package/_types/openapi-client/generated/index.msw.d.ts +6 -0
- package/_types/openapi-client/generated/index.msw.d.ts.map +1 -0
- package/{dist → _types}/openapi-client/generated/solana-accounts/solana-accounts.d.ts +21 -20
- package/_types/openapi-client/generated/solana-accounts/solana-accounts.d.ts.map +1 -0
- package/{dist → _types}/openapi-client/generated/solana-accounts/solana-accounts.msw.d.ts +3 -2
- package/_types/openapi-client/generated/solana-accounts/solana-accounts.msw.d.ts.map +1 -0
- package/_types/openapi-client/index.d.ts +61 -0
- package/_types/openapi-client/index.d.ts.map +1 -0
- package/{dist → _types}/types/calls.d.ts +4 -3
- package/_types/types/calls.d.ts.map +1 -0
- package/{dist → _types}/types/contract.d.ts +3 -2
- package/_types/types/contract.d.ts.map +1 -0
- package/_types/types/misc.d.ts +41 -0
- package/_types/types/misc.d.ts.map +1 -0
- package/{dist → _types}/types/multicall.d.ts +2 -1
- package/_types/types/multicall.d.ts.map +1 -0
- package/{dist → _types}/types/utils.d.ts +1 -0
- package/_types/types/utils.d.ts.map +1 -0
- package/{dist → _types}/utils/wait.d.ts +1 -0
- package/_types/utils/wait.d.ts.map +1 -0
- package/_types/version.d.ts +2 -0
- package/_types/version.d.ts.map +1 -0
- package/accounts/evm/toEvmServerAccount.ts +60 -0
- package/accounts/evm/toEvmSmartAccount.ts +37 -0
- package/accounts/types.ts +53 -0
- package/actions/evm/sendUserOperation.ts +163 -0
- package/actions/evm/waitForUserOperation.ts +115 -0
- package/analytics.ts +142 -0
- package/auth/README.md +145 -0
- package/auth/errors.ts +59 -0
- package/auth/hooks/axios/index.ts +1 -0
- package/auth/hooks/axios/withAuth.ts +121 -0
- package/auth/index.ts +5 -0
- package/auth/utils/http.ts +146 -0
- package/auth/utils/index.ts +2 -0
- package/auth/utils/jwt.ts +324 -0
- package/client/cdp.ts +116 -0
- package/client/evm/evm.ts +739 -0
- package/client/evm/evm.types.ts +408 -0
- package/client/solana/index.ts +1 -0
- package/client/solana/solana.ts +237 -0
- package/client/solana/solana.types.ts +114 -0
- package/constants.ts +4 -0
- package/errors.ts +17 -0
- package/index.ts +4 -0
- package/openapi-client/cdpApiClient.ts +221 -0
- package/openapi-client/errors.ts +132 -0
- package/openapi-client/generated/coinbaseDeveloperPlatformAPIs.schemas.ts +512 -0
- package/openapi-client/generated/evm-accounts/evm-accounts.msw.ts +271 -0
- package/openapi-client/generated/evm-accounts/evm-accounts.ts +194 -0
- package/openapi-client/generated/evm-smart-accounts/evm-smart-accounts.msw.ts +283 -0
- package/openapi-client/generated/evm-smart-accounts/evm-smart-accounts.ts +132 -0
- package/openapi-client/generated/evm-token-balances/evm-token-balances.msw.ts +57 -0
- package/openapi-client/generated/evm-token-balances/evm-token-balances.ts +37 -0
- package/openapi-client/generated/faucets/faucets.msw.ts +76 -0
- package/openapi-client/generated/faucets/faucets.ts +84 -0
- package/openapi-client/generated/index.msw.ts +5 -0
- package/openapi-client/generated/solana-accounts/solana-accounts.msw.ts +219 -0
- package/openapi-client/generated/solana-accounts/solana-accounts.ts +143 -0
- package/openapi-client/index.ts +36 -0
- package/package.json +31 -66
- package/types/calls.ts +40 -0
- package/types/contract.ts +126 -0
- package/types/misc.ts +44 -0
- package/types/multicall.ts +29 -0
- package/types/utils.ts +288 -0
- package/utils/wait.ts +52 -0
- package/version.ts +1 -0
- package/dist/auth/hooks/axios/index.d.ts +0 -1
- package/dist/auth/index.d.ts +0 -4
- package/dist/auth/utils/index.d.ts +0 -2
- package/dist/client/evm/index.d.ts +0 -1
- package/dist/client/evm/index.js +0 -5
- package/dist/client/solana/index.d.ts +0 -1
- package/dist/index.d.ts +0 -3
- package/dist/openapi-client/cdpApiClient.js +0 -146
- package/dist/openapi-client/generated/coinbaseDeveloperPlatformAPIs.schemas.js +0 -54
- package/dist/openapi-client/generated/evm-accounts/evm-accounts.d.ts +0 -62
- package/dist/openapi-client/generated/evm-accounts/evm-accounts.js +0 -85
- package/dist/openapi-client/generated/evm-accounts/evm-accounts.msw.js +0 -113
- package/dist/openapi-client/generated/evm-smart-accounts/evm-smart-accounts.js +0 -66
- package/dist/openapi-client/generated/evm-smart-accounts/evm-smart-accounts.msw.js +0 -99
- package/dist/openapi-client/generated/index.msw.d.ts +0 -4
- package/dist/openapi-client/index.d.ts +0 -55
- package/dist/types/misc.d.ts +0 -3
package/analytics.ts
ADDED
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import md5 from "md5";
|
|
2
|
+
|
|
3
|
+
import { CdpClient } from "./client/cdp.js";
|
|
4
|
+
import { EvmClient } from "./client/evm/evm.js";
|
|
5
|
+
import { SolanaClient } from "./client/solana/solana.js";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* The data in an error event
|
|
9
|
+
*/
|
|
10
|
+
type ErrorEventData = {
|
|
11
|
+
/**
|
|
12
|
+
* The API method where the error occurred, e.g. createAccount, getAccount
|
|
13
|
+
*/
|
|
14
|
+
method: string;
|
|
15
|
+
/**
|
|
16
|
+
* The error message
|
|
17
|
+
*/
|
|
18
|
+
message: string;
|
|
19
|
+
/**
|
|
20
|
+
* The error stack trace
|
|
21
|
+
*/
|
|
22
|
+
stack?: string;
|
|
23
|
+
/**
|
|
24
|
+
* The name of the event. This should match the name in AEC
|
|
25
|
+
*/
|
|
26
|
+
name: "error";
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
type EventData = ErrorEventData;
|
|
30
|
+
|
|
31
|
+
// This is a public client id for the analytics service
|
|
32
|
+
const publicClientId = "54f2ee2fb3d2b901a829940d70fbfc13";
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* AnalyticsConfig singleton class for holding the API key ID
|
|
36
|
+
*/
|
|
37
|
+
export class AnalyticsConfig {
|
|
38
|
+
/**
|
|
39
|
+
* The API key ID
|
|
40
|
+
*/
|
|
41
|
+
public static apiKeyId: string;
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Sets the API key ID
|
|
45
|
+
*
|
|
46
|
+
* @param apiKeyId - The API key ID
|
|
47
|
+
*/
|
|
48
|
+
public static set(apiKeyId: string): void {
|
|
49
|
+
AnalyticsConfig.apiKeyId = apiKeyId;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Sends an analytics event to the default endpoint
|
|
55
|
+
*
|
|
56
|
+
* @param event - The event data containing event-specific fields
|
|
57
|
+
* @returns Promise that resolves when the event is sent
|
|
58
|
+
*/
|
|
59
|
+
export async function sendEvent(event: EventData): Promise<void> {
|
|
60
|
+
const timestamp = Date.now();
|
|
61
|
+
|
|
62
|
+
const enhancedEvent = {
|
|
63
|
+
user_id: AnalyticsConfig.apiKeyId,
|
|
64
|
+
event_type: event.name,
|
|
65
|
+
platform: "server",
|
|
66
|
+
timestamp,
|
|
67
|
+
event_properties: {
|
|
68
|
+
project_name: "cdp-sdk",
|
|
69
|
+
cdp_sdk_language: "typescript",
|
|
70
|
+
...event,
|
|
71
|
+
},
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
const events = [enhancedEvent];
|
|
75
|
+
const stringifiedEventData = JSON.stringify(events);
|
|
76
|
+
const uploadTime = timestamp.toString();
|
|
77
|
+
|
|
78
|
+
const checksum = md5(stringifiedEventData + uploadTime);
|
|
79
|
+
|
|
80
|
+
const analyticsServiceData = {
|
|
81
|
+
client: publicClientId,
|
|
82
|
+
e: stringifiedEventData,
|
|
83
|
+
checksum,
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
const apiEndpoint = "https://cca-lite.coinbase.com";
|
|
87
|
+
const eventPath = "/amp";
|
|
88
|
+
const eventEndPoint = `${apiEndpoint}${eventPath}`;
|
|
89
|
+
|
|
90
|
+
await fetch(eventEndPoint, {
|
|
91
|
+
method: "POST",
|
|
92
|
+
mode: "no-cors",
|
|
93
|
+
headers: {
|
|
94
|
+
"Content-Type": "application/json",
|
|
95
|
+
},
|
|
96
|
+
body: JSON.stringify(analyticsServiceData),
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Wraps all methods of a class with error tracking.
|
|
102
|
+
*
|
|
103
|
+
* @param ClassToWrap - The class whose prototype methods should be wrapped.
|
|
104
|
+
*/
|
|
105
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
106
|
+
function wrapClassWithErrorTracking(ClassToWrap: any): void {
|
|
107
|
+
const methodNames = Object.getOwnPropertyNames(ClassToWrap.prototype).filter(
|
|
108
|
+
name => name !== "constructor" && typeof ClassToWrap.prototype[name] === "function",
|
|
109
|
+
);
|
|
110
|
+
|
|
111
|
+
for (const methodName of methodNames) {
|
|
112
|
+
const originalMethod = ClassToWrap.prototype[methodName];
|
|
113
|
+
ClassToWrap.prototype[methodName] = async function (...args: unknown[]) {
|
|
114
|
+
try {
|
|
115
|
+
return await originalMethod.apply(this, args);
|
|
116
|
+
} catch (error) {
|
|
117
|
+
if (!(error instanceof Error)) {
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
const { message, stack } = error;
|
|
122
|
+
|
|
123
|
+
if (process.env.DISABLE_CDP_ERROR_REPORTING !== "true") {
|
|
124
|
+
sendEvent({
|
|
125
|
+
method: String(methodName),
|
|
126
|
+
message,
|
|
127
|
+
stack,
|
|
128
|
+
name: "error",
|
|
129
|
+
}).catch(() => {
|
|
130
|
+
// ignore error
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
throw error;
|
|
135
|
+
}
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
wrapClassWithErrorTracking(CdpClient);
|
|
141
|
+
wrapClassWithErrorTracking(EvmClient);
|
|
142
|
+
wrapClassWithErrorTracking(SolanaClient);
|
package/auth/README.md
ADDED
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
### Overview
|
|
2
|
+
|
|
3
|
+
The following methods can be used to authenticate your requests to the [Coinbase Developer Platform (CDP)](https://docs.cdp.coinbase.com/). Choose the method that best suits your needs:
|
|
4
|
+
|
|
5
|
+
| Method | Difficulty | Description |
|
|
6
|
+
| :-- | :-- | :-- |
|
|
7
|
+
| [Use an Axios request client](#use-an-axios-request-interceptor) | Easy | Use an [Axios](https://axios-http.com/docs/intro) client with a pre-configured interceptor that automatically handles authentication for all requests. |
|
|
8
|
+
| [Generate your authorization headers](#generate-your-authorization-headers) | Intermediate | Generate authentication headers and apply them to your preferred HTTP client. |
|
|
9
|
+
| [Generate a JWT](#generate-a-jwt) | Advanced | Generate a JWT token, manually create your authentication headers, and apply them to your preferred HTTP client. |
|
|
10
|
+
|
|
11
|
+
Visit the [CDP Authentication docs](https://docs.cdp.coinbase.com/api-v2/docs/authentication) for more details.
|
|
12
|
+
|
|
13
|
+
### Generate a JWT
|
|
14
|
+
|
|
15
|
+
The following example shows how to generate a JWT token, which can then be injected manually into your `Authorization` header to authenticate REST API requests to the [CDP APIs](https://docs.cdp.coinbase.com/api-v2/docs/welcome) using the HTTP request library of your choice.
|
|
16
|
+
|
|
17
|
+
**Step 1**: Install the required package:
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
npm install @coinbase/cdp-sdk
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
**Step 2**: Generate a JWT:
|
|
24
|
+
|
|
25
|
+
```typescript
|
|
26
|
+
import { generateJwt } from "@coinbase/cdp-sdk/auth";
|
|
27
|
+
|
|
28
|
+
const jwt = await generateJwt({
|
|
29
|
+
apiKeyId: "YOUR_API_KEY_ID",
|
|
30
|
+
apiKeySecret: "YOUR_API_KEY_SECRET",
|
|
31
|
+
requestMethod: "GET",
|
|
32
|
+
requestHost: "api.cdp.coinbase.com",
|
|
33
|
+
requestPath: "/platform/v2/evm/accounts",
|
|
34
|
+
expiresIn: 120, // optional (defaults to 120 seconds)
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
console.log(jwt);
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
For information about the above parameters, please refer to the [Authentication parameters](#authentication-parameters) section.
|
|
41
|
+
|
|
42
|
+
**Step 3**: Use your JWT (Bearer token) in the `Authorization` header of your HTTP request:
|
|
43
|
+
|
|
44
|
+
```bash
|
|
45
|
+
curl -L 'https://api.cdp.coinbase.com/platform/v2/evm/accounts' \
|
|
46
|
+
-H 'Content-Type: application/json' \
|
|
47
|
+
-H 'Accept: application/json' \
|
|
48
|
+
-H 'Authorization: Bearer $jwt'
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
### Generate your authorization headers
|
|
52
|
+
|
|
53
|
+
The following example shows how to generate the required authentication headers for authenticating a request to the [CDP REST APIs](https://docs.cdp.coinbase.com/api-v2/docs/welcome), using the HTTP request library of your choice.
|
|
54
|
+
|
|
55
|
+
**Step 1**: Install the required package:
|
|
56
|
+
|
|
57
|
+
```bash
|
|
58
|
+
npm install @coinbase/cdp-sdk
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
**Step 2**: Generate authorization headers:
|
|
62
|
+
|
|
63
|
+
```typescript
|
|
64
|
+
import { getAuthHeaders } from "@coinbase/cdp-sdk/auth";
|
|
65
|
+
|
|
66
|
+
const headers = await getAuthHeaders({
|
|
67
|
+
apiKeyId: "YOUR_API_KEY_ID",
|
|
68
|
+
apiKeySecret: "YOUR_API_KEY_SECRET",
|
|
69
|
+
walletSecret: "YOUR_WALLET_SECRET",
|
|
70
|
+
requestMethod: "POST",
|
|
71
|
+
requestHost: "api.cdp.coinbase.com",
|
|
72
|
+
requestPath: "/platform/v2/evm/accounts",
|
|
73
|
+
requestBody: {
|
|
74
|
+
name: "MyAccount",
|
|
75
|
+
},
|
|
76
|
+
expiresIn: 120, // optional (defaults to 120 seconds)
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
console.log(headers);
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
For information about the above parameters, please refer to the [Authentication parameters](#authentication-parameters) section.
|
|
83
|
+
|
|
84
|
+
### Use an Axios request interceptor
|
|
85
|
+
|
|
86
|
+
**Step 1**: Install the required packages:
|
|
87
|
+
|
|
88
|
+
```bash
|
|
89
|
+
npm install @coinbase/cdp-sdk axios
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
**Step 2**: Create an authenticated Axios client:
|
|
93
|
+
|
|
94
|
+
The following example shows how to use an [Axios](https://axios-http.com/docs/intro) HTTP client with a pre-configured interceptor to authenticate your requests to the CDP REST APIs. This client will automatically add the appropriate authentication headers to each request.
|
|
95
|
+
|
|
96
|
+
```typescript
|
|
97
|
+
import axios from "axios";
|
|
98
|
+
import { axiosHooks } from "@coinbase/cdp-sdk/auth";
|
|
99
|
+
|
|
100
|
+
// Create an Axios instance
|
|
101
|
+
const axiosClient = axios.create({
|
|
102
|
+
baseURL: "https://api.cdp.coinbase.com",
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
// Add authentication to the client
|
|
106
|
+
axiosHooks.withAuth(axiosClient, {
|
|
107
|
+
apiKeyId: "YOUR_API_KEY_ID",
|
|
108
|
+
apiKeySecret: "YOUR_API_KEY_SECRET",
|
|
109
|
+
walletSecret: "YOUR_WALLET_SECRET",
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
// Make authenticated requests (example)
|
|
113
|
+
// The appropriate authentication headers will be automatically added to the request
|
|
114
|
+
try {
|
|
115
|
+
const response = await axiosClient.post("/platform/v2/evm/accounts", {
|
|
116
|
+
name: "MyAccount",
|
|
117
|
+
});
|
|
118
|
+
console.log(response.data);
|
|
119
|
+
} catch (error) {
|
|
120
|
+
console.error("Request failed:", error);
|
|
121
|
+
}
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
The Axios interceptor will automatically:
|
|
125
|
+
|
|
126
|
+
- Generate a JWT for each request
|
|
127
|
+
- Add the JWT to the `Authorization` header
|
|
128
|
+
- Set the appropriate `Content-Type` header
|
|
129
|
+
- Add wallet authentication when required
|
|
130
|
+
|
|
131
|
+
For information about the above parameters, please refer to the [Authentication parameters](#authentication-parameters) section.
|
|
132
|
+
|
|
133
|
+
### Authentication parameters
|
|
134
|
+
|
|
135
|
+
The following table provides more context of many of the authentication parameters used in the examples above:
|
|
136
|
+
|
|
137
|
+
| Parameter | Required | Description |
|
|
138
|
+
| :-- | :-- | :-- |
|
|
139
|
+
| `apiKeyId` | true | The unique identifier for your API key. Supported formats are:<br/>- `xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx`<br/>- `organizations/{orgId}/apiKeys/{keyId}` |
|
|
140
|
+
| `apiKeySecret` | true | Your API key secret. Supported formats are:<br/>- Edwards key (Ed25519): `xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx==`<br/>- Elliptic Curve key (ES256): `-----BEGIN EC PRIVATE KEY-----\n...\n...\n...==\n-----END EC PRIVATE KEY-----\n` |
|
|
141
|
+
| `requestMethod` | true | The HTTP method for the API request you're authenticating (ie, `GET`, `POST`, `PUT`, `DELETE`). |
|
|
142
|
+
| `requestHost` | true | The API host you're calling (ie, `api.cdp.coinbase.com`). |
|
|
143
|
+
| `requestPath` | true | The path of the specific API endpoint you're calling (ie, `/platform/v1/wallets`). |
|
|
144
|
+
| `requestBody` | false | Optional request body data. |
|
|
145
|
+
| `expiresIn` | false | The JWT expiration time in seconds. After this time, the JWT will no longer be valid, and a new one must be generated. Defaults to `120` (ie, 2 minutes) if not specified. |
|
package/auth/errors.ts
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A class of errors for capturing stack traces.
|
|
3
|
+
*/
|
|
4
|
+
class BaseError extends Error {
|
|
5
|
+
/**
|
|
6
|
+
* Base error constructor.
|
|
7
|
+
*
|
|
8
|
+
* @param message - The message to display.
|
|
9
|
+
*/
|
|
10
|
+
constructor(message: string) {
|
|
11
|
+
super(message);
|
|
12
|
+
this.name = this.constructor.name;
|
|
13
|
+
if (Error.captureStackTrace) {
|
|
14
|
+
Error.captureStackTrace(this, this.constructor);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* An error for invalid API key format.
|
|
21
|
+
*/
|
|
22
|
+
export class InvalidAPIKeyFormatError extends BaseError {
|
|
23
|
+
/**
|
|
24
|
+
* Invalid API key format error constructor.
|
|
25
|
+
*
|
|
26
|
+
* @param message - The message to display.
|
|
27
|
+
*/
|
|
28
|
+
constructor(message: string) {
|
|
29
|
+
super("Invalid API key format: " + message);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* An error for invalid Wallet Secret format.
|
|
35
|
+
*/
|
|
36
|
+
export class InvalidWalletSecretFormatError extends BaseError {
|
|
37
|
+
/**
|
|
38
|
+
* Invalid Wallet Secret format error constructor.
|
|
39
|
+
*
|
|
40
|
+
* @param message - The message to display.
|
|
41
|
+
*/
|
|
42
|
+
constructor(message: string) {
|
|
43
|
+
super("Invalid Wallet Secret format: " + message);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* An error for an undefined Wallet Secret.
|
|
49
|
+
*/
|
|
50
|
+
export class UndefinedWalletSecretError extends BaseError {
|
|
51
|
+
/**
|
|
52
|
+
* Undefined Wallet Secret error constructor.
|
|
53
|
+
*
|
|
54
|
+
* @param message - The message to display.
|
|
55
|
+
*/
|
|
56
|
+
constructor(message: string) {
|
|
57
|
+
super("Undefined Wallet Secret: " + message);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./withAuth.js";
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import { AxiosInstance, AxiosHeaders } from "axios";
|
|
2
|
+
|
|
3
|
+
import { getAuthHeaders } from "../../utils/http.js";
|
|
4
|
+
|
|
5
|
+
export interface AuthInterceptorOptions {
|
|
6
|
+
/**
|
|
7
|
+
* The API key ID
|
|
8
|
+
*
|
|
9
|
+
* Examples:
|
|
10
|
+
* 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'
|
|
11
|
+
* 'organizations/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/apiKeys/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'
|
|
12
|
+
*/
|
|
13
|
+
apiKeyId: string;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* The API key secret
|
|
17
|
+
*
|
|
18
|
+
* Examples:
|
|
19
|
+
* 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx==' (Edwards key (Ed25519))
|
|
20
|
+
* '-----BEGIN EC PRIVATE KEY-----\n...\n...\n...==\n-----END EC PRIVATE KEY-----\n' (EC key (ES256))
|
|
21
|
+
*/
|
|
22
|
+
apiKeySecret: string;
|
|
23
|
+
|
|
24
|
+
/** The Wallet Secret */
|
|
25
|
+
walletSecret?: string;
|
|
26
|
+
|
|
27
|
+
/** The source of the request */
|
|
28
|
+
source?: string;
|
|
29
|
+
|
|
30
|
+
/** The version of the source of the request */
|
|
31
|
+
sourceVersion?: string;
|
|
32
|
+
|
|
33
|
+
/** Optional expiration time in seconds (defaults to 120) */
|
|
34
|
+
expiresIn?: number;
|
|
35
|
+
|
|
36
|
+
/** Whether to log request/response details */
|
|
37
|
+
debug?: boolean;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Axios interceptor for adding the JWT to the Authorization header.
|
|
42
|
+
*
|
|
43
|
+
* @param axiosClient - The Axios client instance to add the interceptor to
|
|
44
|
+
* @param options - Options for the request including API keys and debug flag
|
|
45
|
+
* @returns The modified request configuration with the Authorization header added
|
|
46
|
+
*/
|
|
47
|
+
export function withAuth(axiosClient: AxiosInstance, options: AuthInterceptorOptions) {
|
|
48
|
+
axiosClient.interceptors.request.use(async axiosConfig => {
|
|
49
|
+
const method = axiosConfig.method?.toString().toUpperCase() || "GET";
|
|
50
|
+
|
|
51
|
+
if (!axiosConfig.url) {
|
|
52
|
+
throw new Error("URL is required for authentication");
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const fullyQualifiedURL = axiosClient.getUri() + axiosConfig.url;
|
|
56
|
+
|
|
57
|
+
// Parse URL to get host and path
|
|
58
|
+
const url = new URL(fullyQualifiedURL);
|
|
59
|
+
|
|
60
|
+
// Get authentication headers
|
|
61
|
+
const headers = await getAuthHeaders({
|
|
62
|
+
apiKeyId: options.apiKeyId,
|
|
63
|
+
apiKeySecret: options.apiKeySecret,
|
|
64
|
+
requestMethod: method,
|
|
65
|
+
requestHost: url.host,
|
|
66
|
+
requestPath: url.pathname,
|
|
67
|
+
requestBody: axiosConfig.data,
|
|
68
|
+
walletSecret: options.walletSecret,
|
|
69
|
+
source: options.source,
|
|
70
|
+
sourceVersion: options.sourceVersion,
|
|
71
|
+
expiresIn: options.expiresIn,
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
// Add headers to request config
|
|
75
|
+
axiosConfig.headers = new AxiosHeaders({
|
|
76
|
+
...axiosConfig.headers,
|
|
77
|
+
...headers,
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
if (options.debug) {
|
|
81
|
+
console.log("Request:", {
|
|
82
|
+
method,
|
|
83
|
+
url: fullyQualifiedURL,
|
|
84
|
+
headers: axiosConfig.headers,
|
|
85
|
+
data: axiosConfig.data,
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
return axiosConfig;
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
if (options.debug) {
|
|
93
|
+
axiosClient.interceptors.response.use(
|
|
94
|
+
response => {
|
|
95
|
+
console.log("Response:", {
|
|
96
|
+
status: response.status,
|
|
97
|
+
statusText: response.statusText,
|
|
98
|
+
headers: response.headers,
|
|
99
|
+
data: response.data,
|
|
100
|
+
});
|
|
101
|
+
return response;
|
|
102
|
+
},
|
|
103
|
+
error => {
|
|
104
|
+
// Ensure we have access to the error response details
|
|
105
|
+
const errorDetails = {
|
|
106
|
+
status: error.response?.status,
|
|
107
|
+
statusText: error.response?.statusText,
|
|
108
|
+
headers: error.response?.headers,
|
|
109
|
+
data: error.response?.data,
|
|
110
|
+
message: error.message,
|
|
111
|
+
cause: error.cause,
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
console.error("Response Error:", errorDetails);
|
|
115
|
+
return Promise.reject(error);
|
|
116
|
+
},
|
|
117
|
+
);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
return axiosClient;
|
|
121
|
+
}
|
package/auth/index.ts
ADDED
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import { generateWalletJwt, generateJwt } from "./jwt.js";
|
|
2
|
+
import { version } from "../../version.js";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Options for generating authentication headers for API requests.
|
|
6
|
+
*/
|
|
7
|
+
export interface GetAuthHeadersOptions {
|
|
8
|
+
/**
|
|
9
|
+
* The API key ID
|
|
10
|
+
*
|
|
11
|
+
* Examples:
|
|
12
|
+
* 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'
|
|
13
|
+
* 'organizations/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/apiKeys/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'
|
|
14
|
+
*/
|
|
15
|
+
apiKeyId: string;
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* The API key secret
|
|
19
|
+
*
|
|
20
|
+
* Examples:
|
|
21
|
+
* 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx==' (Edwards key (Ed25519))
|
|
22
|
+
* '-----BEGIN EC PRIVATE KEY-----\n...\n...\n...==\n-----END EC PRIVATE KEY-----\n' (EC key (ES256))
|
|
23
|
+
*/
|
|
24
|
+
apiKeySecret: string;
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* The HTTP method for the request (e.g. 'GET', 'POST')
|
|
28
|
+
*/
|
|
29
|
+
requestMethod: string;
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* The host for the request (e.g. 'api.cdp.coinbase.com')
|
|
33
|
+
*/
|
|
34
|
+
requestHost: string;
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* The path for the request (e.g. '/platform/v1/wallets')
|
|
38
|
+
*/
|
|
39
|
+
requestPath: string;
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Optional request body data
|
|
43
|
+
*/
|
|
44
|
+
requestBody?: unknown;
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* The Wallet Secret for wallet authentication
|
|
48
|
+
*/
|
|
49
|
+
walletSecret?: string;
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* The source identifier for the request
|
|
53
|
+
*/
|
|
54
|
+
source?: string;
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* The version of the source making the request
|
|
58
|
+
*/
|
|
59
|
+
sourceVersion?: string;
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Optional expiration time in seconds (defaults to 120)
|
|
63
|
+
*/
|
|
64
|
+
expiresIn?: number;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Gets authentication headers for a request.
|
|
69
|
+
*
|
|
70
|
+
* @param options - The configuration options for generating auth headers
|
|
71
|
+
* @returns Object containing the authentication headers
|
|
72
|
+
*/
|
|
73
|
+
export async function getAuthHeaders(
|
|
74
|
+
options: GetAuthHeadersOptions,
|
|
75
|
+
): Promise<Record<string, string>> {
|
|
76
|
+
const headers: Record<string, string> = {};
|
|
77
|
+
|
|
78
|
+
// Generate and add JWT token
|
|
79
|
+
const jwt = await generateJwt({
|
|
80
|
+
apiKeyId: options.apiKeyId,
|
|
81
|
+
apiKeySecret: options.apiKeySecret,
|
|
82
|
+
requestMethod: options.requestMethod,
|
|
83
|
+
requestHost: options.requestHost,
|
|
84
|
+
requestPath: options.requestPath,
|
|
85
|
+
expiresIn: options.expiresIn,
|
|
86
|
+
});
|
|
87
|
+
headers["Authorization"] = `Bearer ${jwt}`;
|
|
88
|
+
headers["Content-Type"] = "application/json";
|
|
89
|
+
|
|
90
|
+
// Add wallet auth if needed
|
|
91
|
+
if (requiresWalletAuth(options.requestMethod, options.requestPath)) {
|
|
92
|
+
if (!options.walletSecret) {
|
|
93
|
+
throw new Error(
|
|
94
|
+
"Wallet Secret not configured. Please set the CDP_WALLET_SECRET environment variable, or pass it as an option to the CdpClient constructor.",
|
|
95
|
+
);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
const walletAuthToken = await generateWalletJwt({
|
|
99
|
+
walletSecret: options.walletSecret,
|
|
100
|
+
requestMethod: options.requestMethod,
|
|
101
|
+
requestHost: options.requestHost,
|
|
102
|
+
requestPath: options.requestPath,
|
|
103
|
+
requestData: options.requestBody || {},
|
|
104
|
+
});
|
|
105
|
+
headers["X-Wallet-Auth"] = walletAuthToken;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// Add correlation data
|
|
109
|
+
headers["Correlation-Context"] = getCorrelationData(options.source, options.sourceVersion);
|
|
110
|
+
|
|
111
|
+
return headers;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Returns true if the request indicated by the method and URL requires wallet authentication.
|
|
116
|
+
*
|
|
117
|
+
* @param requestMethod - The HTTP method of the request
|
|
118
|
+
* @param requestPath - The URL path of the request
|
|
119
|
+
* @returns True if the request requires wallet authentication, false otherwise
|
|
120
|
+
*/
|
|
121
|
+
function requiresWalletAuth(requestMethod: string, requestPath: string): boolean {
|
|
122
|
+
return (
|
|
123
|
+
requestPath?.includes("/accounts") && (requestMethod === "POST" || requestMethod === "DELETE")
|
|
124
|
+
);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Returns encoded correlation data including the SDK version and language.
|
|
129
|
+
*
|
|
130
|
+
* @param source - The source identifier for the request
|
|
131
|
+
* @param sourceVersion - The version of the source making the request
|
|
132
|
+
* @returns Encoded correlation data as a query string
|
|
133
|
+
*/
|
|
134
|
+
function getCorrelationData(source?: string, sourceVersion?: string): string {
|
|
135
|
+
const data = {
|
|
136
|
+
sdk_version: version,
|
|
137
|
+
sdk_language: "typescript",
|
|
138
|
+
source: source || "sdk-auth",
|
|
139
|
+
};
|
|
140
|
+
if (sourceVersion) {
|
|
141
|
+
data["source_version"] = sourceVersion;
|
|
142
|
+
}
|
|
143
|
+
return Object.keys(data)
|
|
144
|
+
.map(key => `${key}=${encodeURIComponent(data[key])}`)
|
|
145
|
+
.join(",");
|
|
146
|
+
}
|