@0xmonaco/core 0.0.0-develop-20260120180031

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 (130) hide show
  1. package/README.md +693 -0
  2. package/dist/api/applications/api.d.ts +44 -0
  3. package/dist/api/applications/api.d.ts.map +1 -0
  4. package/dist/api/applications/api.js +54 -0
  5. package/dist/api/applications/api.js.map +1 -0
  6. package/dist/api/applications/index.d.ts +5 -0
  7. package/dist/api/applications/index.d.ts.map +1 -0
  8. package/dist/api/applications/index.js +5 -0
  9. package/dist/api/applications/index.js.map +1 -0
  10. package/dist/api/auth/api.d.ts +201 -0
  11. package/dist/api/auth/api.d.ts.map +1 -0
  12. package/dist/api/auth/api.js +293 -0
  13. package/dist/api/auth/api.js.map +1 -0
  14. package/dist/api/auth/index.d.ts +5 -0
  15. package/dist/api/auth/index.d.ts.map +1 -0
  16. package/dist/api/auth/index.js +5 -0
  17. package/dist/api/auth/index.js.map +1 -0
  18. package/dist/api/base.d.ts +124 -0
  19. package/dist/api/base.d.ts.map +1 -0
  20. package/dist/api/base.js +284 -0
  21. package/dist/api/base.js.map +1 -0
  22. package/dist/api/fees/api.d.ts +71 -0
  23. package/dist/api/fees/api.d.ts.map +1 -0
  24. package/dist/api/fees/api.js +83 -0
  25. package/dist/api/fees/api.js.map +1 -0
  26. package/dist/api/fees/index.d.ts +7 -0
  27. package/dist/api/fees/index.d.ts.map +1 -0
  28. package/dist/api/fees/index.js +7 -0
  29. package/dist/api/fees/index.js.map +1 -0
  30. package/dist/api/index.d.ts +16 -0
  31. package/dist/api/index.d.ts.map +1 -0
  32. package/dist/api/index.js +16 -0
  33. package/dist/api/index.js.map +1 -0
  34. package/dist/api/market/api.d.ts +14 -0
  35. package/dist/api/market/api.d.ts.map +1 -0
  36. package/dist/api/market/api.js +70 -0
  37. package/dist/api/market/api.js.map +1 -0
  38. package/dist/api/market/index.d.ts +2 -0
  39. package/dist/api/market/index.d.ts.map +1 -0
  40. package/dist/api/market/index.js +2 -0
  41. package/dist/api/market/index.js.map +1 -0
  42. package/dist/api/orderbook/api.d.ts +16 -0
  43. package/dist/api/orderbook/api.d.ts.map +1 -0
  44. package/dist/api/orderbook/api.js +38 -0
  45. package/dist/api/orderbook/api.js.map +1 -0
  46. package/dist/api/orderbook/index.d.ts +2 -0
  47. package/dist/api/orderbook/index.d.ts.map +1 -0
  48. package/dist/api/orderbook/index.js +2 -0
  49. package/dist/api/orderbook/index.js.map +1 -0
  50. package/dist/api/profile/api.d.ts +119 -0
  51. package/dist/api/profile/api.d.ts.map +1 -0
  52. package/dist/api/profile/api.js +162 -0
  53. package/dist/api/profile/api.js.map +1 -0
  54. package/dist/api/profile/index.d.ts +7 -0
  55. package/dist/api/profile/index.d.ts.map +1 -0
  56. package/dist/api/profile/index.js +7 -0
  57. package/dist/api/profile/index.js.map +1 -0
  58. package/dist/api/trades/api.d.ts +45 -0
  59. package/dist/api/trades/api.d.ts.map +1 -0
  60. package/dist/api/trades/api.js +43 -0
  61. package/dist/api/trades/api.js.map +1 -0
  62. package/dist/api/trades/index.d.ts +2 -0
  63. package/dist/api/trades/index.d.ts.map +1 -0
  64. package/dist/api/trades/index.js +2 -0
  65. package/dist/api/trades/index.js.map +1 -0
  66. package/dist/api/trading/api.d.ts +213 -0
  67. package/dist/api/trading/api.d.ts.map +1 -0
  68. package/dist/api/trading/api.js +285 -0
  69. package/dist/api/trading/api.js.map +1 -0
  70. package/dist/api/trading/index.d.ts +5 -0
  71. package/dist/api/trading/index.d.ts.map +1 -0
  72. package/dist/api/trading/index.js +5 -0
  73. package/dist/api/trading/index.js.map +1 -0
  74. package/dist/api/vault/api.d.ts +257 -0
  75. package/dist/api/vault/api.d.ts.map +1 -0
  76. package/dist/api/vault/api.js +526 -0
  77. package/dist/api/vault/api.js.map +1 -0
  78. package/dist/api/vault/index.d.ts +5 -0
  79. package/dist/api/vault/index.d.ts.map +1 -0
  80. package/dist/api/vault/index.js +5 -0
  81. package/dist/api/vault/index.js.map +1 -0
  82. package/dist/api/websocket/index.d.ts +4 -0
  83. package/dist/api/websocket/index.d.ts.map +1 -0
  84. package/dist/api/websocket/index.js +4 -0
  85. package/dist/api/websocket/index.js.map +1 -0
  86. package/dist/api/websocket/types.d.ts +36 -0
  87. package/dist/api/websocket/types.d.ts.map +1 -0
  88. package/dist/api/websocket/types.js +2 -0
  89. package/dist/api/websocket/types.js.map +1 -0
  90. package/dist/api/websocket/utils.d.ts +9 -0
  91. package/dist/api/websocket/utils.d.ts.map +1 -0
  92. package/dist/api/websocket/utils.js +23 -0
  93. package/dist/api/websocket/utils.js.map +1 -0
  94. package/dist/api/websocket/websocket.d.ts +6 -0
  95. package/dist/api/websocket/websocket.d.ts.map +1 -0
  96. package/dist/api/websocket/websocket.js +345 -0
  97. package/dist/api/websocket/websocket.js.map +1 -0
  98. package/dist/errors/errors.d.ts +382 -0
  99. package/dist/errors/errors.d.ts.map +1 -0
  100. package/dist/errors/errors.js +801 -0
  101. package/dist/errors/errors.js.map +1 -0
  102. package/dist/errors/index.d.ts +2 -0
  103. package/dist/errors/index.d.ts.map +1 -0
  104. package/dist/errors/index.js +2 -0
  105. package/dist/errors/index.js.map +1 -0
  106. package/dist/index.d.ts +6 -0
  107. package/dist/index.d.ts.map +1 -0
  108. package/dist/index.js +6 -0
  109. package/dist/index.js.map +1 -0
  110. package/dist/networks/index.d.ts +2 -0
  111. package/dist/networks/index.d.ts.map +1 -0
  112. package/dist/networks/index.js +2 -0
  113. package/dist/networks/index.js.map +1 -0
  114. package/dist/networks/networks.d.ts +15 -0
  115. package/dist/networks/networks.d.ts.map +1 -0
  116. package/dist/networks/networks.js +34 -0
  117. package/dist/networks/networks.js.map +1 -0
  118. package/dist/sdk.d.ts +128 -0
  119. package/dist/sdk.d.ts.map +1 -0
  120. package/dist/sdk.js +272 -0
  121. package/dist/sdk.js.map +1 -0
  122. package/dist/utils/index.d.ts +2 -0
  123. package/dist/utils/index.d.ts.map +1 -0
  124. package/dist/utils/index.js +2 -0
  125. package/dist/utils/index.js.map +1 -0
  126. package/dist/utils/magnitude.d.ts +27 -0
  127. package/dist/utils/magnitude.d.ts.map +1 -0
  128. package/dist/utils/magnitude.js +32 -0
  129. package/dist/utils/magnitude.js.map +1 -0
  130. package/package.json +49 -0
@@ -0,0 +1,44 @@
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
+ }
44
+ //# sourceMappingURL=api.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"api.d.ts","sourceRoot":"","sources":["../../../src/api/applications/api.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AAEH,OAAO,KAAK,EAAE,yBAAyB,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAC;AAClF,OAAO,EAAE,OAAO,EAAE,MAAM,SAAS,CAAC;AAElC,qBAAa,mBAAoB,SAAQ,OAAQ,YAAW,eAAe;IACzE;;;;;;;;;;;;;;;;;;OAkBG;IACG,oBAAoB,IAAI,OAAO,CAAC,yBAAyB,CAAC;CAkBjE"}
@@ -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
+ };
52
+ }
53
+ }
54
+ //# sourceMappingURL=api.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"api.js","sourceRoot":"","sources":["../../../src/api/applications/api.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AAGH,OAAO,EAAE,OAAO,EAAE,MAAM,SAAS,CAAC;AAElC,MAAM,OAAO,mBAAoB,SAAQ,OAAO;IAC9C;;;;;;;;;;;;;;;;;;OAkBG;IACH,KAAK,CAAC,oBAAoB;QACxB,MAAM,IAAI,GAAG,MAAM,IAAI,CAAC,wBAAwB,CAM7C,6BAA6B,EAAE;YAChC,MAAM,EAAE,KAAK;SACd,CAAC,CAAC;QACH,OAAO;YACL,EAAE,EAAE,IAAI,CAAC,EAAE;YACX,IAAI,EAAE,IAAI,CAAC,IAAI;YACf,cAAc,EAAE,IAAI,CAAC,eAAe;YACpC,UAAU,EAAE,IAAI,CAAC,WAAW;YAC5B,oBAAoB,EAAE,IAAI,CAAC,sBAAsB;SAClD,CAAC;IACJ,CAAC;CACF"}
@@ -0,0 +1,5 @@
1
+ /**
2
+ * Applications API Module
3
+ */
4
+ export { ApplicationsAPIImpl } from "./api";
5
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/api/applications/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EAAE,mBAAmB,EAAE,MAAM,OAAO,CAAC"}
@@ -0,0 +1,5 @@
1
+ /**
2
+ * Applications API Module
3
+ */
4
+ export { ApplicationsAPIImpl } from "./api";
5
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/api/applications/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EAAE,mBAAmB,EAAE,MAAM,OAAO,CAAC"}
@@ -0,0 +1,201 @@
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 walletClient;
36
+ private readonly chain;
37
+ /**
38
+ * Creates a new AuthAPI instance.
39
+ *
40
+ * @param walletClient - The viem wallet client for signing operations
41
+ * @param chain - The blockchain network configuration
42
+ * @param apiUrl - The base URL for the Monaco API Gateway
43
+ */
44
+ constructor(walletClient: WalletClient, chain: Chain, apiUrl: string);
45
+ /**
46
+ * Complete authentication flow for frontend applications.
47
+ *
48
+ * This method handles the entire authentication process:
49
+ * 1. Creates a challenge
50
+ * 2. Signs the challenge message
51
+ * 3. Verifies the signature and returns JWT tokens
52
+ *
53
+ * @param clientId - Client ID of the application
54
+ * @returns Promise resolving to the verification response with JWT tokens
55
+ * @throws {APIError} When authentication fails
56
+ * @throws {InvalidConfigError} When wallet account is not available
57
+ *
58
+ * @example
59
+ * ```typescript
60
+ * // Complete authentication in one call
61
+ * const authResult = await authAPI.authenticate("my-app-client-id");
62
+ * console.log(`Access token: ${authResult.access_token}`);
63
+ * console.log(`User ID: ${authResult.user.id}`);
64
+ * ```
65
+ */
66
+ authenticate(clientId: string): Promise<AuthState>;
67
+ /**
68
+ * Signs a challenge message using the wallet client.
69
+ *
70
+ * Signs the provided message using the wallet's private key.
71
+ * This is used in the authentication flow to prove ownership of the wallet.
72
+ *
73
+ * @param message - The message to sign
74
+ * @returns Promise resolving to the signature
75
+ * @throws {InvalidConfigError} When wallet account is not available
76
+ * @throws {APIError} When signing fails
77
+ *
78
+ * @example
79
+ * ```typescript
80
+ * const challenge = await authAPI.createChallenge(address, clientId);
81
+ * const signature = await authAPI.signChallenge(challenge.message);
82
+ * console.log(`Signature: ${signature}`);
83
+ * ```
84
+ */
85
+ signChallenge(message: string): Promise<string>;
86
+ /**
87
+ * Creates a challenge for frontend authentication.
88
+ *
89
+ * Generates a unique nonce and message that the user must sign with their wallet.
90
+ * This is the first step in the authentication flow for frontend applications.
91
+ *
92
+ * @param address - Wallet address of the user
93
+ * @param clientId - Client ID of the application
94
+ * @returns Promise resolving to the challenge response
95
+ * @throws {APIError} When challenge creation fails
96
+ *
97
+ * @example
98
+ * ```typescript
99
+ * const challenge = await authAPI.createChallenge(
100
+ * "0x1234...",
101
+ * "my-app-client-id"
102
+ * );
103
+ * console.log(`Challenge message: ${challenge.message}`);
104
+ * console.log(`Nonce: ${challenge.nonce}`);
105
+ * ```
106
+ */
107
+ createChallenge(address: string, clientId: string): Promise<ChallengeResponse>;
108
+ /**
109
+ * Verifies a signature for frontend authentication.
110
+ *
111
+ * Validates the signature against the challenge and returns JWT tokens for
112
+ * authenticated API access. This is the second step in the authentication flow.
113
+ *
114
+ * @param address - Wallet address of the user
115
+ * @param signature - Signature of the challenge message
116
+ * @param nonce - Nonce from the challenge response
117
+ * @param clientId - Client ID of the application
118
+ * @returns Promise resolving to the verification response with JWT tokens
119
+ * @throws {APIError} When signature verification fails
120
+ *
121
+ * @example
122
+ * ```typescript
123
+ * // First create a challenge
124
+ * const challenge = await authAPI.createChallenge(address, clientId);
125
+ *
126
+ * // User signs the challenge message with their wallet
127
+ * const signature = await wallet.signMessage(challenge.message);
128
+ *
129
+ * // Verify the signature and get tokens
130
+ * const authResult = await authAPI.verifySignature(
131
+ * address,
132
+ * signature,
133
+ * challenge.nonce,
134
+ * clientId
135
+ * );
136
+ *
137
+ * console.log(`Access token: ${authResult.accessToken}`);
138
+ * console.log(`User ID: ${authResult.user.id}`);
139
+ * ```
140
+ */
141
+ verifySignature(address: string, signature: string, nonce: string, clientId: string): Promise<AuthState>;
142
+ /**
143
+ * Authenticates a backend service using a secret key.
144
+ *
145
+ * Returns JWT tokens for API access. This method is used for backend services
146
+ * that need to authenticate with the Monaco API Gateway.
147
+ *
148
+ * @param secretKey - Secret key of the application
149
+ * @returns Promise resolving to the backend auth response with JWT tokens
150
+ * @throws {APIError} When backend authentication fails
151
+ *
152
+ * @example
153
+ * ```typescript
154
+ * const backendAuth = await authAPI.authenticateBackend("my-secret-key");
155
+ * console.log(`Backend access token: ${backendAuth.accessToken}`);
156
+ * console.log(`Application: ${backendAuth.application.name}`);
157
+ * ```
158
+ */
159
+ authenticateBackend(secretKey: string): Promise<BackendAuthResponse>;
160
+ /**
161
+ * Refreshes an access token using a refresh token.
162
+ *
163
+ * Obtains a new access token using a valid refresh token. This is useful for
164
+ * maintaining long-term authentication without requiring the user to sign
165
+ * a new challenge.
166
+ *
167
+ * @param refreshToken - The refresh token to use
168
+ * @returns Promise resolving to new access and refresh tokens
169
+ * @throws {APIError} When token refresh fails
170
+ *
171
+ * @example
172
+ * ```typescript
173
+ * const newTokens = await authAPI.refreshToken(refreshToken);
174
+ * console.log(`New access token: ${newTokens.accessToken}`);
175
+ * console.log(`Expires at: ${new Date(newTokens.expiresAt * 1000)}`);
176
+ * ```
177
+ */
178
+ refreshToken(refreshToken: string): Promise<TokenRefreshResponse>;
179
+ /**
180
+ * Revokes a refresh token.
181
+ *
182
+ * Invalidates a refresh token, preventing it from being used to obtain
183
+ * new access tokens. This is useful for logout functionality or when
184
+ * a token has been compromised.
185
+ *
186
+ * **Important:** Pass the `refreshToken` from the authentication response.
187
+ * @param refreshToken - The refresh token to revoke (from authResult.refreshToken)
188
+ * @returns Promise resolving when the token is revoked
189
+ * @throws {APIError} When token revocation fails
190
+ *
191
+ * @example
192
+ * ```typescript
193
+ * // After authentication
194
+ * const authResult = await authAPI.authenticate(clientId);
195
+ * await authAPI.revokeToken(authResult.refreshToken);
196
+ * console.log("Token revoked successfully");
197
+ * ```
198
+ */
199
+ revokeToken(refreshToken: string): Promise<void>;
200
+ }
201
+ //# sourceMappingURL=api.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"api.d.ts","sourceRoot":"","sources":["../../../src/api/auth/api.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AAEH,OAAO,KAAK,EAAE,OAAO,EAAE,SAAS,EAAE,mBAAmB,EAAE,iBAAiB,EAAE,oBAAoB,EAAE,MAAM,iBAAiB,CAAC;AACxH,OAAO,KAAK,EAAE,KAAK,EAAE,YAAY,EAAE,MAAM,MAAM,CAAC;AAEhD,OAAO,EAAE,OAAO,EAAE,MAAM,SAAS,CAAC;AAElC,qBAAa,WAAY,SAAQ,OAAQ,YAAW,OAAO;IASvD,OAAO,CAAC,QAAQ,CAAC,YAAY;IAC7B,OAAO,CAAC,QAAQ,CAAC,KAAK;IATxB;;;;;;OAMG;gBAEgB,YAAY,EAAE,YAAY,EAC1B,KAAK,EAAE,KAAK,EAC7B,MAAM,EAAE,MAAM;IAKhB;;;;;;;;;;;;;;;;;;;;OAoBG;IACG,YAAY,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,SAAS,CAAC;IAgBxD;;;;;;;;;;;;;;;;;OAiBG;IACG,aAAa,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;IAarD;;;;;;;;;;;;;;;;;;;;OAoBG;IACG,eAAe,CAAC,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,iBAAiB,CAAC;IAqBpF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OAgCG;IACG,eAAe,CAAC,OAAO,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,SAAS,CAAC;IAiC9G;;;;;;;;;;;;;;;;OAgBG;IACG,mBAAmB,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,mBAAmB,CAAC;IA4B1E;;;;;;;;;;;;;;;;;OAiBG;IACG,YAAY,CAAC,YAAY,EAAE,MAAM,GAAG,OAAO,CAAC,oBAAoB,CAAC;IAiBvE;;;;;;;;;;;;;;;;;;;OAmBG;IACG,WAAW,CAAC,YAAY,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;CAQvD"}
@@ -0,0 +1,293 @@
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
+ /**
35
+ * Creates a new AuthAPI instance.
36
+ *
37
+ * @param walletClient - The viem wallet client for signing operations
38
+ * @param chain - The blockchain network configuration
39
+ * @param apiUrl - The base URL for the Monaco API Gateway
40
+ */
41
+ constructor(walletClient, chain, apiUrl) {
42
+ super(apiUrl);
43
+ this.walletClient = walletClient;
44
+ this.chain = chain;
45
+ }
46
+ /**
47
+ * Complete authentication flow for frontend applications.
48
+ *
49
+ * This method handles the entire authentication process:
50
+ * 1. Creates a challenge
51
+ * 2. Signs the challenge message
52
+ * 3. Verifies the signature and returns JWT tokens
53
+ *
54
+ * @param clientId - Client ID of the application
55
+ * @returns Promise resolving to the verification response with JWT tokens
56
+ * @throws {APIError} When authentication fails
57
+ * @throws {InvalidConfigError} When wallet account is not available
58
+ *
59
+ * @example
60
+ * ```typescript
61
+ * // Complete authentication in one call
62
+ * const authResult = await authAPI.authenticate("my-app-client-id");
63
+ * console.log(`Access token: ${authResult.access_token}`);
64
+ * console.log(`User ID: ${authResult.user.id}`);
65
+ * ```
66
+ */
67
+ async authenticate(clientId) {
68
+ const account = this.walletClient.account;
69
+ if (!account) {
70
+ throw new InvalidConfigError("No account available in wallet client", "account");
71
+ }
72
+ // 1. Create challenge
73
+ const challenge = await this.createChallenge(account.address, clientId);
74
+ // 2. Sign the challenge message
75
+ const signature = await this.signChallenge(challenge.message);
76
+ // 3. Verify signature and get tokens
77
+ return await this.verifySignature(account.address, signature, challenge.nonce, clientId);
78
+ }
79
+ /**
80
+ * Signs a challenge message using the wallet client.
81
+ *
82
+ * Signs the provided message using the wallet's private key.
83
+ * This is used in the authentication flow to prove ownership of the wallet.
84
+ *
85
+ * @param message - The message to sign
86
+ * @returns Promise resolving to the signature
87
+ * @throws {InvalidConfigError} When wallet account is not available
88
+ * @throws {APIError} When signing fails
89
+ *
90
+ * @example
91
+ * ```typescript
92
+ * const challenge = await authAPI.createChallenge(address, clientId);
93
+ * const signature = await authAPI.signChallenge(challenge.message);
94
+ * console.log(`Signature: ${signature}`);
95
+ * ```
96
+ */
97
+ async signChallenge(message) {
98
+ const account = this.walletClient.account;
99
+ if (!account) {
100
+ throw new InvalidConfigError("No account available in wallet client", "account");
101
+ }
102
+ // Sign the message using the wallet client
103
+ return await this.walletClient.signMessage({
104
+ account,
105
+ message,
106
+ });
107
+ }
108
+ /**
109
+ * Creates a challenge for frontend authentication.
110
+ *
111
+ * Generates a unique nonce and message that the user must sign with their wallet.
112
+ * This is the first step in the authentication flow for frontend applications.
113
+ *
114
+ * @param address - Wallet address of the user
115
+ * @param clientId - Client ID of the application
116
+ * @returns Promise resolving to the challenge response
117
+ * @throws {APIError} When challenge creation fails
118
+ *
119
+ * @example
120
+ * ```typescript
121
+ * const challenge = await authAPI.createChallenge(
122
+ * "0x1234...",
123
+ * "my-app-client-id"
124
+ * );
125
+ * console.log(`Challenge message: ${challenge.message}`);
126
+ * console.log(`Nonce: ${challenge.nonce}`);
127
+ * ```
128
+ */
129
+ async createChallenge(address, clientId) {
130
+ const responseBody = await this.makePublicRequest("/api/v1/auth/challenge", {
131
+ method: "POST",
132
+ body: JSON.stringify({
133
+ address,
134
+ client_id: clientId,
135
+ chain_id: this.chain.id,
136
+ }),
137
+ });
138
+ return {
139
+ nonce: responseBody.nonce,
140
+ message: responseBody.message,
141
+ expiresAt: responseBody.expires_at,
142
+ };
143
+ }
144
+ /**
145
+ * Verifies a signature for frontend authentication.
146
+ *
147
+ * Validates the signature against the challenge and returns JWT tokens for
148
+ * authenticated API access. This is the second step in the authentication flow.
149
+ *
150
+ * @param address - Wallet address of the user
151
+ * @param signature - Signature of the challenge message
152
+ * @param nonce - Nonce from the challenge response
153
+ * @param clientId - Client ID of the application
154
+ * @returns Promise resolving to the verification response with JWT tokens
155
+ * @throws {APIError} When signature verification fails
156
+ *
157
+ * @example
158
+ * ```typescript
159
+ * // First create a challenge
160
+ * const challenge = await authAPI.createChallenge(address, clientId);
161
+ *
162
+ * // User signs the challenge message with their wallet
163
+ * const signature = await wallet.signMessage(challenge.message);
164
+ *
165
+ * // Verify the signature and get tokens
166
+ * const authResult = await authAPI.verifySignature(
167
+ * address,
168
+ * signature,
169
+ * challenge.nonce,
170
+ * clientId
171
+ * );
172
+ *
173
+ * console.log(`Access token: ${authResult.accessToken}`);
174
+ * console.log(`User ID: ${authResult.user.id}`);
175
+ * ```
176
+ */
177
+ async verifySignature(address, signature, nonce, clientId) {
178
+ const responseBody = await this.makePublicRequest("/api/v1/auth/verify", {
179
+ method: "POST",
180
+ body: JSON.stringify({
181
+ address,
182
+ signature,
183
+ nonce,
184
+ client_id: clientId,
185
+ chain_id: this.chain.id,
186
+ }),
187
+ });
188
+ return {
189
+ accessToken: responseBody.access_token,
190
+ refreshToken: responseBody.refresh_token,
191
+ expiresAt: responseBody.expires_at,
192
+ user: {
193
+ id: responseBody.user.id,
194
+ address: responseBody.user.address,
195
+ username: responseBody.user.username,
196
+ },
197
+ };
198
+ }
199
+ /**
200
+ * Authenticates a backend service using a secret key.
201
+ *
202
+ * Returns JWT tokens for API access. This method is used for backend services
203
+ * that need to authenticate with the Monaco API Gateway.
204
+ *
205
+ * @param secretKey - Secret key of the application
206
+ * @returns Promise resolving to the backend auth response with JWT tokens
207
+ * @throws {APIError} When backend authentication fails
208
+ *
209
+ * @example
210
+ * ```typescript
211
+ * const backendAuth = await authAPI.authenticateBackend("my-secret-key");
212
+ * console.log(`Backend access token: ${backendAuth.accessToken}`);
213
+ * console.log(`Application: ${backendAuth.application.name}`);
214
+ * ```
215
+ */
216
+ async authenticateBackend(secretKey) {
217
+ const responseBody = await this.makePublicRequest("/api/v1/auth/backend", {
218
+ method: "POST",
219
+ body: JSON.stringify({
220
+ secret_key: secretKey,
221
+ chain_id: this.chain.id,
222
+ }),
223
+ });
224
+ return {
225
+ accessToken: responseBody.access_token,
226
+ expiresAt: responseBody.expires_at,
227
+ application: {
228
+ id: responseBody.application.id,
229
+ name: responseBody.application.name,
230
+ clientId: responseBody.application.client_id,
231
+ },
232
+ };
233
+ }
234
+ /**
235
+ * Refreshes an access token using a refresh token.
236
+ *
237
+ * Obtains a new access token using a valid refresh token. This is useful for
238
+ * maintaining long-term authentication without requiring the user to sign
239
+ * a new challenge.
240
+ *
241
+ * @param refreshToken - The refresh token to use
242
+ * @returns Promise resolving to new access and refresh tokens
243
+ * @throws {APIError} When token refresh fails
244
+ *
245
+ * @example
246
+ * ```typescript
247
+ * const newTokens = await authAPI.refreshToken(refreshToken);
248
+ * console.log(`New access token: ${newTokens.accessToken}`);
249
+ * console.log(`Expires at: ${new Date(newTokens.expiresAt * 1000)}`);
250
+ * ```
251
+ */
252
+ async refreshToken(refreshToken) {
253
+ const responseBody = await this.makePublicRequest("/api/v1/auth/refresh", {
254
+ method: "POST",
255
+ body: JSON.stringify({
256
+ refresh_token: refreshToken,
257
+ }),
258
+ });
259
+ return {
260
+ accessToken: responseBody.access_token,
261
+ expiresAt: responseBody.expires_at,
262
+ };
263
+ }
264
+ /**
265
+ * Revokes a refresh token.
266
+ *
267
+ * Invalidates a refresh token, preventing it from being used to obtain
268
+ * new access tokens. This is useful for logout functionality or when
269
+ * a token has been compromised.
270
+ *
271
+ * **Important:** Pass the `refreshToken` from the authentication response.
272
+ * @param refreshToken - The refresh token to revoke (from authResult.refreshToken)
273
+ * @returns Promise resolving when the token is revoked
274
+ * @throws {APIError} When token revocation fails
275
+ *
276
+ * @example
277
+ * ```typescript
278
+ * // After authentication
279
+ * const authResult = await authAPI.authenticate(clientId);
280
+ * await authAPI.revokeToken(authResult.refreshToken);
281
+ * console.log("Token revoked successfully");
282
+ * ```
283
+ */
284
+ async revokeToken(refreshToken) {
285
+ await this.makeAuthenticatedRequest("/api/v1/auth/revoke", {
286
+ method: "POST",
287
+ body: JSON.stringify({
288
+ refresh_token: refreshToken,
289
+ }),
290
+ });
291
+ }
292
+ }
293
+ //# sourceMappingURL=api.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"api.js","sourceRoot":"","sources":["../../../src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C;gBACnB,aAAa,EAAE,YAAY;aAC5B,CAAC;SACH,CAAC,CAAC;IACL,CAAC;CACF"}
@@ -0,0 +1,5 @@
1
+ /**
2
+ * Auth API Module
3
+ */
4
+ export { AuthAPIImpl } from "./api";
5
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/api/auth/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EAAE,WAAW,EAAE,MAAM,OAAO,CAAC"}
@@ -0,0 +1,5 @@
1
+ /**
2
+ * Auth API Module
3
+ */
4
+ export { AuthAPIImpl } from "./api";
5
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/api/auth/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EAAE,WAAW,EAAE,MAAM,OAAO,CAAC"}