@0xmonaco/core 0.7.7 → 0.7.8

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 (77) hide show
  1. package/dist/api/applications/api.d.ts +43 -0
  2. package/dist/api/applications/api.js +54 -0
  3. package/dist/api/applications/index.d.ts +4 -0
  4. package/dist/api/applications/index.js +4 -0
  5. package/dist/api/auth/api.d.ts +206 -0
  6. package/dist/api/auth/api.js +305 -0
  7. package/dist/api/auth/index.d.ts +4 -0
  8. package/dist/api/auth/index.js +4 -0
  9. package/dist/api/base.d.ts +123 -0
  10. package/dist/api/base.js +286 -0
  11. package/dist/api/fees/api.d.ts +72 -0
  12. package/dist/api/fees/api.js +90 -0
  13. package/dist/api/fees/index.d.ts +6 -0
  14. package/dist/api/fees/index.js +6 -0
  15. package/dist/api/index.d.ts +18 -0
  16. package/dist/api/index.js +18 -0
  17. package/dist/api/margin-accounts/api.d.ts +12 -0
  18. package/dist/api/margin-accounts/api.js +69 -0
  19. package/dist/api/margin-accounts/index.d.ts +1 -0
  20. package/dist/api/margin-accounts/index.js +1 -0
  21. package/dist/api/market/api.d.ts +20 -0
  22. package/dist/api/market/api.js +97 -0
  23. package/dist/api/market/index.d.ts +1 -0
  24. package/dist/api/market/index.js +1 -0
  25. package/dist/api/orderbook/api.d.ts +15 -0
  26. package/dist/api/orderbook/api.js +37 -0
  27. package/dist/api/orderbook/index.d.ts +1 -0
  28. package/dist/api/orderbook/index.js +1 -0
  29. package/dist/api/perp/index.d.ts +1 -0
  30. package/dist/api/perp/index.js +1 -0
  31. package/dist/api/perp/routes.d.ts +133 -0
  32. package/dist/api/perp/routes.js +85 -0
  33. package/dist/api/positions/api.d.ts +12 -0
  34. package/dist/api/positions/api.js +86 -0
  35. package/dist/api/positions/index.d.ts +1 -0
  36. package/dist/api/positions/index.js +1 -0
  37. package/dist/api/profile/api.d.ts +191 -0
  38. package/dist/api/profile/api.js +259 -0
  39. package/dist/api/profile/index.d.ts +6 -0
  40. package/dist/api/profile/index.js +6 -0
  41. package/dist/api/trades/api.d.ts +44 -0
  42. package/dist/api/trades/api.js +42 -0
  43. package/dist/api/trades/index.d.ts +1 -0
  44. package/dist/api/trades/index.js +1 -0
  45. package/dist/api/trading/api.d.ts +297 -0
  46. package/dist/api/trading/api.js +481 -0
  47. package/dist/api/trading/index.d.ts +4 -0
  48. package/dist/api/trading/index.js +4 -0
  49. package/dist/api/vault/api.d.ts +261 -0
  50. package/dist/api/vault/api.js +506 -0
  51. package/dist/api/vault/index.d.ts +4 -0
  52. package/dist/api/vault/index.js +4 -0
  53. package/dist/api/websocket/index.d.ts +3 -0
  54. package/dist/api/websocket/index.js +3 -0
  55. package/dist/api/websocket/types.d.ts +41 -0
  56. package/dist/api/websocket/types.js +0 -0
  57. package/dist/api/websocket/utils.d.ts +8 -0
  58. package/dist/api/websocket/utils.js +22 -0
  59. package/dist/api/websocket/websocket.d.ts +5 -0
  60. package/dist/api/websocket/websocket.js +556 -0
  61. package/dist/errors/errors.d.ts +381 -0
  62. package/dist/errors/errors.js +815 -0
  63. package/dist/errors/index.d.ts +1 -0
  64. package/dist/errors/index.js +1 -0
  65. package/dist/index.d.ts +5 -0
  66. package/dist/index.js +5 -0
  67. package/dist/networks/index.d.ts +1 -0
  68. package/dist/networks/index.js +1 -0
  69. package/dist/networks/networks.d.ts +21 -0
  70. package/dist/networks/networks.js +46 -0
  71. package/dist/sdk.d.ts +134 -0
  72. package/dist/sdk.js +294 -0
  73. package/dist/utils/index.d.ts +1 -0
  74. package/dist/utils/index.js +1 -0
  75. package/dist/utils/magnitude.d.ts +26 -0
  76. package/dist/utils/magnitude.js +31 -0
  77. package/package.json +3 -3
@@ -0,0 +1,43 @@
1
+ /**
2
+ * Applications API Implementation
3
+ *
4
+ * Handles application configuration operations. All operations go through
5
+ * the API Gateway and require valid authentication.
6
+ *
7
+ * This class provides an interface for retrieving application configuration
8
+ * on the Monaco protocol.
9
+ *
10
+ * @example
11
+ * ```typescript
12
+ * const applicationsAPI = new ApplicationsAPIImpl(apiUrl);
13
+ *
14
+ * // Get application configuration
15
+ * const config = await applicationsAPI.getApplicationConfig();
16
+ * console.log(`App name: ${config.name}`);
17
+ * console.log(`Allowed origins: ${config.allowedOrigins.join(', ')}`);
18
+ * ```
19
+ */
20
+ import type { ApplicationConfigResponse, ApplicationsAPI } from "@0xmonaco/types";
21
+ import { BaseAPI } from "../base";
22
+ export declare class ApplicationsAPIImpl extends BaseAPI implements ApplicationsAPI {
23
+ /**
24
+ * Gets the configuration for the authenticated application.
25
+ *
26
+ * Returns the application's configuration including allowed origins,
27
+ * webhook URL, and other settings. Requires valid authentication.
28
+ *
29
+ * @returns Promise resolving to the application configuration
30
+ * @throws {APIError} When the request fails or authentication is invalid
31
+ *
32
+ * @example
33
+ * ```typescript
34
+ * const config = await applicationsAPI.getApplicationConfig();
35
+ * console.log(`Application: ${config.name}`);
36
+ * console.log(`ID: ${config.id}`);
37
+ * if (config.webhookUrl) {
38
+ * console.log(`Webhook: ${config.webhookUrl}`);
39
+ * }
40
+ * ```
41
+ */
42
+ getApplicationConfig(): Promise<ApplicationConfigResponse>;
43
+ }
@@ -0,0 +1,54 @@
1
+ /**
2
+ * Applications API Implementation
3
+ *
4
+ * Handles application configuration operations. All operations go through
5
+ * the API Gateway and require valid authentication.
6
+ *
7
+ * This class provides an interface for retrieving application configuration
8
+ * on the Monaco protocol.
9
+ *
10
+ * @example
11
+ * ```typescript
12
+ * const applicationsAPI = new ApplicationsAPIImpl(apiUrl);
13
+ *
14
+ * // Get application configuration
15
+ * const config = await applicationsAPI.getApplicationConfig();
16
+ * console.log(`App name: ${config.name}`);
17
+ * console.log(`Allowed origins: ${config.allowedOrigins.join(', ')}`);
18
+ * ```
19
+ */
20
+ import { BaseAPI } from "../base";
21
+ export class ApplicationsAPIImpl extends BaseAPI {
22
+ /**
23
+ * Gets the configuration for the authenticated application.
24
+ *
25
+ * Returns the application's configuration including allowed origins,
26
+ * webhook URL, and other settings. Requires valid authentication.
27
+ *
28
+ * @returns Promise resolving to the application configuration
29
+ * @throws {APIError} When the request fails or authentication is invalid
30
+ *
31
+ * @example
32
+ * ```typescript
33
+ * const config = await applicationsAPI.getApplicationConfig();
34
+ * console.log(`Application: ${config.name}`);
35
+ * console.log(`ID: ${config.id}`);
36
+ * if (config.webhookUrl) {
37
+ * console.log(`Webhook: ${config.webhookUrl}`);
38
+ * }
39
+ * ```
40
+ */
41
+ async getApplicationConfig() {
42
+ const data = await this.makeAuthenticatedRequest("/api/v1/applications/config", {
43
+ method: "GET",
44
+ });
45
+ return {
46
+ id: data.id,
47
+ name: data.name,
48
+ allowedOrigins: data.allowed_origins,
49
+ webhookUrl: data.webhook_url,
50
+ vaultContractAddress: data.vault_contract_address,
51
+ clientId: data.client_id,
52
+ };
53
+ }
54
+ }
@@ -0,0 +1,4 @@
1
+ /**
2
+ * Applications API Module
3
+ */
4
+ export { ApplicationsAPIImpl } from "./api";
@@ -0,0 +1,4 @@
1
+ /**
2
+ * Applications API Module
3
+ */
4
+ export { ApplicationsAPIImpl } from "./api";
@@ -0,0 +1,206 @@
1
+ /**
2
+ * Auth API Implementation
3
+ *
4
+ * Handles authentication operations including challenge creation, signature verification,
5
+ * and backend authentication. All operations go through the API Gateway.
6
+ *
7
+ * This class provides a complete interface for authentication on the Monaco protocol,
8
+ * including frontend wallet authentication and backend service authentication.
9
+ *
10
+ * @example
11
+ * ```typescript
12
+ * const authAPI = new AuthAPIImpl(walletClient, chain, apiUrl);
13
+ *
14
+ * // Complete authentication flow
15
+ * const authResult = await authAPI.authenticate(clientId);
16
+ *
17
+ * // Or step-by-step flow
18
+ * const challenge = await authAPI.createChallenge(userAddress, clientId);
19
+ * const signature = await authAPI.signChallenge(challenge.message);
20
+ * const authResult = await authAPI.verifySignature(
21
+ * userAddress,
22
+ * signature,
23
+ * challenge.nonce,
24
+ * clientId
25
+ * );
26
+ *
27
+ * // Authenticate backend service
28
+ * const backendAuth = await authAPI.authenticateBackend(secretKey);
29
+ * ```
30
+ */
31
+ import type { AuthAPI, AuthState, BackendAuthResponse, ChallengeResponse, TokenRefreshResponse } from "@0xmonaco/types";
32
+ import type { Chain, WalletClient } from "viem";
33
+ import { BaseAPI } from "../base";
34
+ export declare class AuthAPIImpl extends BaseAPI implements AuthAPI {
35
+ private readonly chain;
36
+ private walletClient;
37
+ /**
38
+ * Creates a new AuthAPI instance.
39
+ *
40
+ * @param walletClient - The viem wallet client for signing operations (optional)
41
+ * @param chain - The blockchain network configuration
42
+ * @param apiUrl - The base URL for the Monaco API Gateway
43
+ */
44
+ constructor(walletClient: WalletClient | undefined, chain: Chain, apiUrl: string);
45
+ /**
46
+ * Sets the wallet client for signing operations.
47
+ * Used when the wallet becomes available after SDK initialization.
48
+ */
49
+ setWalletClient(walletClient: WalletClient): void;
50
+ /**
51
+ * Complete authentication flow for frontend applications.
52
+ *
53
+ * This method handles the entire authentication process:
54
+ * 1. Creates a challenge
55
+ * 2. Signs the challenge message
56
+ * 3. Verifies the signature and returns JWT tokens
57
+ *
58
+ * @param clientId - Client ID of the application
59
+ * @returns Promise resolving to the verification response with JWT tokens
60
+ * @throws {APIError} When authentication fails
61
+ * @throws {InvalidConfigError} When wallet account is not available
62
+ *
63
+ * @example
64
+ * ```typescript
65
+ * // Complete authentication in one call
66
+ * const authResult = await authAPI.authenticate("my-app-client-id");
67
+ * console.log(`Access token: ${authResult.access_token}`);
68
+ * console.log(`User ID: ${authResult.user.id}`);
69
+ * ```
70
+ */
71
+ authenticate(clientId: string): Promise<AuthState>;
72
+ /**
73
+ * Signs a challenge message using the wallet client.
74
+ *
75
+ * Signs the provided message using the wallet's private key.
76
+ * This is used in the authentication flow to prove ownership of the wallet.
77
+ *
78
+ * @param message - The message to sign
79
+ * @returns Promise resolving to the signature
80
+ * @throws {InvalidConfigError} When wallet account is not available
81
+ * @throws {APIError} When signing fails
82
+ *
83
+ * @example
84
+ * ```typescript
85
+ * const challenge = await authAPI.createChallenge(address, clientId);
86
+ * const signature = await authAPI.signChallenge(challenge.message);
87
+ * console.log(`Signature: ${signature}`);
88
+ * ```
89
+ */
90
+ signChallenge(message: string): Promise<string>;
91
+ /**
92
+ * Creates a challenge for frontend authentication.
93
+ *
94
+ * Generates a unique nonce and message that the user must sign with their wallet.
95
+ * This is the first step in the authentication flow for frontend applications.
96
+ *
97
+ * @param address - Wallet address of the user
98
+ * @param clientId - Client ID of the application
99
+ * @returns Promise resolving to the challenge response
100
+ * @throws {APIError} When challenge creation fails
101
+ *
102
+ * @example
103
+ * ```typescript
104
+ * const challenge = await authAPI.createChallenge(
105
+ * "0x1234...",
106
+ * "my-app-client-id"
107
+ * );
108
+ * console.log(`Challenge message: ${challenge.message}`);
109
+ * console.log(`Nonce: ${challenge.nonce}`);
110
+ * ```
111
+ */
112
+ createChallenge(address: string, clientId: string): Promise<ChallengeResponse>;
113
+ /**
114
+ * Verifies a signature for frontend authentication.
115
+ *
116
+ * Validates the signature against the challenge and returns JWT tokens for
117
+ * authenticated API access. This is the second step in the authentication flow.
118
+ *
119
+ * @param address - Wallet address of the user
120
+ * @param signature - Signature of the challenge message
121
+ * @param nonce - Nonce from the challenge response
122
+ * @param clientId - Client ID of the application
123
+ * @returns Promise resolving to the verification response with JWT tokens
124
+ * @throws {APIError} When signature verification fails
125
+ *
126
+ * @example
127
+ * ```typescript
128
+ * // First create a challenge
129
+ * const challenge = await authAPI.createChallenge(address, clientId);
130
+ *
131
+ * // User signs the challenge message with their wallet
132
+ * const signature = await wallet.signMessage(challenge.message);
133
+ *
134
+ * // Verify the signature and get tokens
135
+ * const authResult = await authAPI.verifySignature(
136
+ * address,
137
+ * signature,
138
+ * challenge.nonce,
139
+ * clientId
140
+ * );
141
+ *
142
+ * console.log(`Access token: ${authResult.accessToken}`);
143
+ * console.log(`User ID: ${authResult.user.id}`);
144
+ * ```
145
+ */
146
+ verifySignature(address: string, signature: string, nonce: string, clientId: string): Promise<AuthState>;
147
+ /**
148
+ * Authenticates a backend service using a secret key.
149
+ *
150
+ * Returns JWT tokens for API access. This method is used for backend services
151
+ * that need to authenticate with the Monaco API Gateway.
152
+ *
153
+ * @param secretKey - Secret key of the application
154
+ * @returns Promise resolving to the backend auth response with JWT tokens
155
+ * @throws {APIError} When backend authentication fails
156
+ *
157
+ * @example
158
+ * ```typescript
159
+ * const backendAuth = await authAPI.authenticateBackend("my-secret-key");
160
+ * console.log(`Backend access token: ${backendAuth.accessToken}`);
161
+ * console.log(`Application: ${backendAuth.application.name}`);
162
+ * ```
163
+ */
164
+ authenticateBackend(secretKey: string): Promise<BackendAuthResponse>;
165
+ /**
166
+ * Refreshes an access token using a refresh token.
167
+ *
168
+ * Obtains a new access token using a valid refresh token. This is useful for
169
+ * maintaining long-term authentication without requiring the user to sign
170
+ * a new challenge.
171
+ *
172
+ * @param refreshToken - The refresh token to use
173
+ * @returns Promise resolving to new access and refresh tokens
174
+ * @throws {APIError} When token refresh fails
175
+ *
176
+ * @example
177
+ * ```typescript
178
+ * const newTokens = await authAPI.refreshToken(refreshToken);
179
+ * console.log(`New access token: ${newTokens.accessToken}`);
180
+ * console.log(`Expires at: ${new Date(newTokens.expiresAt * 1000)}`);
181
+ * ```
182
+ */
183
+ refreshToken(refreshToken: string): Promise<TokenRefreshResponse>;
184
+ /**
185
+ * Revokes the current session's refresh token.
186
+ *
187
+ * Invalidates the refresh token associated with the current access token,
188
+ * preventing it from being used to obtain new access tokens. This is useful
189
+ * for logout functionality or when a token has been compromised.
190
+ *
191
+ * The server identifies the token to revoke from the access token in the
192
+ * Authorization header — no request body is needed.
193
+ *
194
+ * @returns Promise resolving when the token is revoked
195
+ * @throws {APIError} When token revocation fails
196
+ *
197
+ * @example
198
+ * ```typescript
199
+ * // After authentication
200
+ * const authResult = await authAPI.authenticate(clientId);
201
+ * await authAPI.revokeToken();
202
+ * console.log("Token revoked successfully");
203
+ * ```
204
+ */
205
+ revokeToken(): Promise<void>;
206
+ }
@@ -0,0 +1,305 @@
1
+ /**
2
+ * Auth API Implementation
3
+ *
4
+ * Handles authentication operations including challenge creation, signature verification,
5
+ * and backend authentication. All operations go through the API Gateway.
6
+ *
7
+ * This class provides a complete interface for authentication on the Monaco protocol,
8
+ * including frontend wallet authentication and backend service authentication.
9
+ *
10
+ * @example
11
+ * ```typescript
12
+ * const authAPI = new AuthAPIImpl(walletClient, chain, apiUrl);
13
+ *
14
+ * // Complete authentication flow
15
+ * const authResult = await authAPI.authenticate(clientId);
16
+ *
17
+ * // Or step-by-step flow
18
+ * const challenge = await authAPI.createChallenge(userAddress, clientId);
19
+ * const signature = await authAPI.signChallenge(challenge.message);
20
+ * const authResult = await authAPI.verifySignature(
21
+ * userAddress,
22
+ * signature,
23
+ * challenge.nonce,
24
+ * clientId
25
+ * );
26
+ *
27
+ * // Authenticate backend service
28
+ * const backendAuth = await authAPI.authenticateBackend(secretKey);
29
+ * ```
30
+ */
31
+ import { InvalidConfigError } from "../../errors";
32
+ import { BaseAPI } from "../base";
33
+ export class AuthAPIImpl extends BaseAPI {
34
+ chain;
35
+ walletClient;
36
+ /**
37
+ * Creates a new AuthAPI instance.
38
+ *
39
+ * @param walletClient - The viem wallet client for signing operations (optional)
40
+ * @param chain - The blockchain network configuration
41
+ * @param apiUrl - The base URL for the Monaco API Gateway
42
+ */
43
+ constructor(walletClient, chain, apiUrl) {
44
+ super(apiUrl);
45
+ this.chain = chain;
46
+ this.walletClient = walletClient;
47
+ }
48
+ /**
49
+ * Sets the wallet client for signing operations.
50
+ * Used when the wallet becomes available after SDK initialization.
51
+ */
52
+ setWalletClient(walletClient) {
53
+ this.walletClient = walletClient;
54
+ }
55
+ /**
56
+ * Complete authentication flow for frontend applications.
57
+ *
58
+ * This method handles the entire authentication process:
59
+ * 1. Creates a challenge
60
+ * 2. Signs the challenge message
61
+ * 3. Verifies the signature and returns JWT tokens
62
+ *
63
+ * @param clientId - Client ID of the application
64
+ * @returns Promise resolving to the verification response with JWT tokens
65
+ * @throws {APIError} When authentication fails
66
+ * @throws {InvalidConfigError} When wallet account is not available
67
+ *
68
+ * @example
69
+ * ```typescript
70
+ * // Complete authentication in one call
71
+ * const authResult = await authAPI.authenticate("my-app-client-id");
72
+ * console.log(`Access token: ${authResult.access_token}`);
73
+ * console.log(`User ID: ${authResult.user.id}`);
74
+ * ```
75
+ */
76
+ async authenticate(clientId) {
77
+ if (!this.walletClient) {
78
+ throw new InvalidConfigError("Wallet client not set. Connect a wallet first.", "walletClient");
79
+ }
80
+ const account = this.walletClient.account;
81
+ if (!account) {
82
+ throw new InvalidConfigError("No account available in wallet client", "account");
83
+ }
84
+ // 1. Create challenge
85
+ const challenge = await this.createChallenge(account.address, clientId);
86
+ // 2. Sign the challenge message
87
+ const signature = await this.signChallenge(challenge.message);
88
+ // 3. Verify signature and get tokens
89
+ return await this.verifySignature(account.address, signature, challenge.nonce, clientId);
90
+ }
91
+ /**
92
+ * Signs a challenge message using the wallet client.
93
+ *
94
+ * Signs the provided message using the wallet's private key.
95
+ * This is used in the authentication flow to prove ownership of the wallet.
96
+ *
97
+ * @param message - The message to sign
98
+ * @returns Promise resolving to the signature
99
+ * @throws {InvalidConfigError} When wallet account is not available
100
+ * @throws {APIError} When signing fails
101
+ *
102
+ * @example
103
+ * ```typescript
104
+ * const challenge = await authAPI.createChallenge(address, clientId);
105
+ * const signature = await authAPI.signChallenge(challenge.message);
106
+ * console.log(`Signature: ${signature}`);
107
+ * ```
108
+ */
109
+ async signChallenge(message) {
110
+ if (!this.walletClient) {
111
+ throw new InvalidConfigError("Wallet client not set. Connect a wallet first.", "walletClient");
112
+ }
113
+ const account = this.walletClient.account;
114
+ if (!account) {
115
+ throw new InvalidConfigError("No account available in wallet client", "account");
116
+ }
117
+ // Sign the message using the wallet client
118
+ return await this.walletClient.signMessage({
119
+ account,
120
+ message,
121
+ });
122
+ }
123
+ /**
124
+ * Creates a challenge for frontend authentication.
125
+ *
126
+ * Generates a unique nonce and message that the user must sign with their wallet.
127
+ * This is the first step in the authentication flow for frontend applications.
128
+ *
129
+ * @param address - Wallet address of the user
130
+ * @param clientId - Client ID of the application
131
+ * @returns Promise resolving to the challenge response
132
+ * @throws {APIError} When challenge creation fails
133
+ *
134
+ * @example
135
+ * ```typescript
136
+ * const challenge = await authAPI.createChallenge(
137
+ * "0x1234...",
138
+ * "my-app-client-id"
139
+ * );
140
+ * console.log(`Challenge message: ${challenge.message}`);
141
+ * console.log(`Nonce: ${challenge.nonce}`);
142
+ * ```
143
+ */
144
+ async createChallenge(address, clientId) {
145
+ const responseBody = await this.makePublicRequest("/api/v1/auth/challenge", {
146
+ method: "POST",
147
+ body: JSON.stringify({
148
+ address,
149
+ client_id: clientId,
150
+ chain_id: this.chain.id,
151
+ }),
152
+ });
153
+ return {
154
+ nonce: responseBody.nonce,
155
+ message: responseBody.message,
156
+ expiresAt: responseBody.expires_at,
157
+ };
158
+ }
159
+ /**
160
+ * Verifies a signature for frontend authentication.
161
+ *
162
+ * Validates the signature against the challenge and returns JWT tokens for
163
+ * authenticated API access. This is the second step in the authentication flow.
164
+ *
165
+ * @param address - Wallet address of the user
166
+ * @param signature - Signature of the challenge message
167
+ * @param nonce - Nonce from the challenge response
168
+ * @param clientId - Client ID of the application
169
+ * @returns Promise resolving to the verification response with JWT tokens
170
+ * @throws {APIError} When signature verification fails
171
+ *
172
+ * @example
173
+ * ```typescript
174
+ * // First create a challenge
175
+ * const challenge = await authAPI.createChallenge(address, clientId);
176
+ *
177
+ * // User signs the challenge message with their wallet
178
+ * const signature = await wallet.signMessage(challenge.message);
179
+ *
180
+ * // Verify the signature and get tokens
181
+ * const authResult = await authAPI.verifySignature(
182
+ * address,
183
+ * signature,
184
+ * challenge.nonce,
185
+ * clientId
186
+ * );
187
+ *
188
+ * console.log(`Access token: ${authResult.accessToken}`);
189
+ * console.log(`User ID: ${authResult.user.id}`);
190
+ * ```
191
+ */
192
+ async verifySignature(address, signature, nonce, clientId) {
193
+ const responseBody = await this.makePublicRequest("/api/v1/auth/verify", {
194
+ method: "POST",
195
+ body: JSON.stringify({
196
+ address,
197
+ signature,
198
+ nonce,
199
+ client_id: clientId,
200
+ chain_id: this.chain.id,
201
+ }),
202
+ });
203
+ return {
204
+ accessToken: responseBody.access_token,
205
+ refreshToken: responseBody.refresh_token,
206
+ expiresAt: responseBody.expires_at,
207
+ user: {
208
+ id: responseBody.user.id,
209
+ address: responseBody.user.address,
210
+ username: responseBody.user.username,
211
+ },
212
+ };
213
+ }
214
+ /**
215
+ * Authenticates a backend service using a secret key.
216
+ *
217
+ * Returns JWT tokens for API access. This method is used for backend services
218
+ * that need to authenticate with the Monaco API Gateway.
219
+ *
220
+ * @param secretKey - Secret key of the application
221
+ * @returns Promise resolving to the backend auth response with JWT tokens
222
+ * @throws {APIError} When backend authentication fails
223
+ *
224
+ * @example
225
+ * ```typescript
226
+ * const backendAuth = await authAPI.authenticateBackend("my-secret-key");
227
+ * console.log(`Backend access token: ${backendAuth.accessToken}`);
228
+ * console.log(`Application: ${backendAuth.application.name}`);
229
+ * ```
230
+ */
231
+ async authenticateBackend(secretKey) {
232
+ const responseBody = await this.makePublicRequest("/api/v1/auth/backend", {
233
+ method: "POST",
234
+ body: JSON.stringify({
235
+ secret_key: secretKey,
236
+ chain_id: this.chain.id,
237
+ }),
238
+ });
239
+ return {
240
+ accessToken: responseBody.access_token,
241
+ expiresAt: responseBody.expires_at,
242
+ application: {
243
+ id: responseBody.application.id,
244
+ name: responseBody.application.name,
245
+ clientId: responseBody.application.client_id,
246
+ },
247
+ };
248
+ }
249
+ /**
250
+ * Refreshes an access token using a refresh token.
251
+ *
252
+ * Obtains a new access token using a valid refresh token. This is useful for
253
+ * maintaining long-term authentication without requiring the user to sign
254
+ * a new challenge.
255
+ *
256
+ * @param refreshToken - The refresh token to use
257
+ * @returns Promise resolving to new access and refresh tokens
258
+ * @throws {APIError} When token refresh fails
259
+ *
260
+ * @example
261
+ * ```typescript
262
+ * const newTokens = await authAPI.refreshToken(refreshToken);
263
+ * console.log(`New access token: ${newTokens.accessToken}`);
264
+ * console.log(`Expires at: ${new Date(newTokens.expiresAt * 1000)}`);
265
+ * ```
266
+ */
267
+ async refreshToken(refreshToken) {
268
+ const responseBody = await this.makePublicRequest("/api/v1/auth/refresh", {
269
+ method: "POST",
270
+ body: JSON.stringify({
271
+ refresh_token: refreshToken,
272
+ }),
273
+ });
274
+ return {
275
+ accessToken: responseBody.access_token,
276
+ expiresAt: responseBody.expires_at,
277
+ };
278
+ }
279
+ /**
280
+ * Revokes the current session's refresh token.
281
+ *
282
+ * Invalidates the refresh token associated with the current access token,
283
+ * preventing it from being used to obtain new access tokens. This is useful
284
+ * for logout functionality or when a token has been compromised.
285
+ *
286
+ * The server identifies the token to revoke from the access token in the
287
+ * Authorization header — no request body is needed.
288
+ *
289
+ * @returns Promise resolving when the token is revoked
290
+ * @throws {APIError} When token revocation fails
291
+ *
292
+ * @example
293
+ * ```typescript
294
+ * // After authentication
295
+ * const authResult = await authAPI.authenticate(clientId);
296
+ * await authAPI.revokeToken();
297
+ * console.log("Token revoked successfully");
298
+ * ```
299
+ */
300
+ async revokeToken() {
301
+ await this.makeAuthenticatedRequest("/api/v1/auth/revoke", {
302
+ method: "POST",
303
+ });
304
+ }
305
+ }
@@ -0,0 +1,4 @@
1
+ /**
2
+ * Auth API Module
3
+ */
4
+ export { AuthAPIImpl } from "./api";
@@ -0,0 +1,4 @@
1
+ /**
2
+ * Auth API Module
3
+ */
4
+ export { AuthAPIImpl } from "./api";