@mixrpay/agent-sdk 0.1.1 → 0.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/README.md +208 -221
- package/dist/index.cjs +555 -51
- package/dist/index.d.cts +356 -239
- package/dist/index.d.ts +356 -239
- package/dist/index.js +552 -37
- package/package.json +1 -1
package/dist/index.d.cts
CHANGED
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
import { Hex } from 'viem';
|
|
2
|
-
|
|
3
1
|
/**
|
|
4
2
|
* MixrPay Agent SDK - Type definitions
|
|
5
3
|
*
|
|
@@ -124,29 +122,6 @@ interface PaymentEvent {
|
|
|
124
122
|
/** URL that triggered the payment */
|
|
125
123
|
url?: string;
|
|
126
124
|
}
|
|
127
|
-
/**
|
|
128
|
-
* Payment requirements parsed from a 402 response.
|
|
129
|
-
*
|
|
130
|
-
* This represents what the server is asking for in exchange for the resource.
|
|
131
|
-
*/
|
|
132
|
-
interface PaymentRequirements {
|
|
133
|
-
/** Address to send payment to */
|
|
134
|
-
recipient: string;
|
|
135
|
-
/** Amount in USDC minor units (6 decimals). 1000000 = $1.00 */
|
|
136
|
-
amount: bigint;
|
|
137
|
-
/** Currency code (currently only "USDC" is supported) */
|
|
138
|
-
currency: string;
|
|
139
|
-
/** Chain ID (8453 for Base mainnet, 84532 for Base Sepolia) */
|
|
140
|
-
chainId: number;
|
|
141
|
-
/** URL where the payment should be submitted */
|
|
142
|
-
facilitatorUrl: string;
|
|
143
|
-
/** Unique nonce for this payment request */
|
|
144
|
-
nonce: string;
|
|
145
|
-
/** Unix timestamp when this payment request expires */
|
|
146
|
-
expiresAt: number;
|
|
147
|
-
/** Human-readable description of the payment */
|
|
148
|
-
description?: string;
|
|
149
|
-
}
|
|
150
125
|
/**
|
|
151
126
|
* Information about a session key.
|
|
152
127
|
*/
|
|
@@ -222,95 +197,142 @@ interface DiagnosticsResult {
|
|
|
222
197
|
walletAddress: string;
|
|
223
198
|
}
|
|
224
199
|
/**
|
|
225
|
-
*
|
|
200
|
+
* Options for creating a session authorization with a MixrPay merchant.
|
|
201
|
+
*/
|
|
202
|
+
interface CreateSessionOptions {
|
|
203
|
+
/**
|
|
204
|
+
* The merchant's MixrPay public key.
|
|
205
|
+
* Format: `pk_live_...` or `pk_test_...`
|
|
206
|
+
*/
|
|
207
|
+
merchantPublicKey: string;
|
|
208
|
+
/**
|
|
209
|
+
* Maximum spending limit for this session in USD.
|
|
210
|
+
* @default 25.00
|
|
211
|
+
*/
|
|
212
|
+
spendingLimitUsd?: number;
|
|
213
|
+
/**
|
|
214
|
+
* Number of days this session should be valid.
|
|
215
|
+
* @default 7
|
|
216
|
+
*/
|
|
217
|
+
durationDays?: number;
|
|
218
|
+
}
|
|
219
|
+
/**
|
|
220
|
+
* Status of a session authorization.
|
|
221
|
+
*/
|
|
222
|
+
type SessionAuthStatus = 'pending' | 'active' | 'expired' | 'revoked';
|
|
223
|
+
/**
|
|
224
|
+
* A session authorization with a MixrPay merchant.
|
|
226
225
|
*
|
|
227
|
-
* This
|
|
226
|
+
* This represents a user-approved spending permission for a specific merchant.
|
|
228
227
|
*/
|
|
229
|
-
interface
|
|
230
|
-
/**
|
|
231
|
-
|
|
232
|
-
/**
|
|
233
|
-
|
|
234
|
-
/**
|
|
235
|
-
|
|
236
|
-
/**
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
/** Unix timestamp after which the authorization is valid */
|
|
249
|
-
validAfter: string;
|
|
250
|
-
/** Unix timestamp before which the authorization is valid */
|
|
251
|
-
validBefore: string;
|
|
252
|
-
/** Unique nonce (32 bytes hex) */
|
|
253
|
-
nonce: string;
|
|
254
|
-
};
|
|
255
|
-
};
|
|
228
|
+
interface SessionAuthorization {
|
|
229
|
+
/** Unique session ID */
|
|
230
|
+
id: string;
|
|
231
|
+
/** Merchant ID */
|
|
232
|
+
merchantId: string;
|
|
233
|
+
/** Merchant name */
|
|
234
|
+
merchantName: string;
|
|
235
|
+
/** Current session status */
|
|
236
|
+
status: SessionAuthStatus;
|
|
237
|
+
/** Maximum spending limit in USD */
|
|
238
|
+
spendingLimitUsd: number;
|
|
239
|
+
/** Amount already spent in USD */
|
|
240
|
+
amountUsedUsd: number;
|
|
241
|
+
/** Remaining spending limit in USD */
|
|
242
|
+
remainingLimitUsd: number;
|
|
243
|
+
/** When the session expires */
|
|
244
|
+
expiresAt: Date;
|
|
245
|
+
/** When the session was created */
|
|
246
|
+
createdAt: Date;
|
|
256
247
|
}
|
|
257
248
|
/**
|
|
258
|
-
*
|
|
249
|
+
* Options for calling a MixrPay merchant's API.
|
|
259
250
|
*/
|
|
260
|
-
interface
|
|
261
|
-
/**
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
/**
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
251
|
+
interface CallMerchantApiOptions {
|
|
252
|
+
/**
|
|
253
|
+
* The merchant API URL to call.
|
|
254
|
+
*/
|
|
255
|
+
url: string;
|
|
256
|
+
/**
|
|
257
|
+
* HTTP method (default: 'POST').
|
|
258
|
+
*/
|
|
259
|
+
method?: 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE';
|
|
260
|
+
/**
|
|
261
|
+
* Request body (will be JSON-serialized if object).
|
|
262
|
+
*/
|
|
263
|
+
body?: unknown;
|
|
264
|
+
/**
|
|
265
|
+
* Additional headers to include.
|
|
266
|
+
*/
|
|
267
|
+
headers?: Record<string, string>;
|
|
268
|
+
/**
|
|
269
|
+
* The merchant's MixrPay public key.
|
|
270
|
+
* Required to identify which session to use.
|
|
271
|
+
*/
|
|
272
|
+
merchantPublicKey: string;
|
|
273
|
+
/**
|
|
274
|
+
* Expected price in USD for this request.
|
|
275
|
+
* Used for client-side validation.
|
|
276
|
+
*/
|
|
277
|
+
priceUsd?: number;
|
|
278
|
+
/**
|
|
279
|
+
* Feature slug for tracking/analytics.
|
|
280
|
+
*/
|
|
281
|
+
feature?: string;
|
|
269
282
|
}
|
|
270
283
|
/**
|
|
271
|
-
*
|
|
284
|
+
* Options for charging against a session.
|
|
272
285
|
*/
|
|
273
|
-
interface
|
|
274
|
-
/**
|
|
275
|
-
|
|
276
|
-
/**
|
|
277
|
-
|
|
278
|
-
/**
|
|
279
|
-
|
|
280
|
-
/** Unix timestamp after which the authorization is valid */
|
|
281
|
-
validAfter: bigint;
|
|
282
|
-
/** Unix timestamp before which the authorization is valid */
|
|
283
|
-
validBefore: bigint;
|
|
284
|
-
/** Unique nonce (32 bytes) */
|
|
285
|
-
nonce: `0x${string}`;
|
|
286
|
+
interface ChargeSessionOptions {
|
|
287
|
+
/** Feature slug for tracking */
|
|
288
|
+
feature?: string;
|
|
289
|
+
/** Idempotency key to prevent double-charges */
|
|
290
|
+
idempotencyKey?: string;
|
|
291
|
+
/** Additional metadata */
|
|
292
|
+
metadata?: Record<string, unknown>;
|
|
286
293
|
}
|
|
287
294
|
/**
|
|
288
|
-
*
|
|
295
|
+
* Result of a session charge.
|
|
289
296
|
*/
|
|
290
|
-
interface
|
|
291
|
-
/**
|
|
292
|
-
|
|
293
|
-
/**
|
|
294
|
-
|
|
295
|
-
/**
|
|
296
|
-
|
|
297
|
-
/**
|
|
298
|
-
|
|
297
|
+
interface ChargeResult {
|
|
298
|
+
/** Whether the charge succeeded */
|
|
299
|
+
success: boolean;
|
|
300
|
+
/** Charge ID */
|
|
301
|
+
chargeId: string;
|
|
302
|
+
/** Amount charged in USD */
|
|
303
|
+
amountUsd: number;
|
|
304
|
+
/** Transaction hash (if on-chain) */
|
|
305
|
+
txHash?: string;
|
|
306
|
+
/** Remaining session balance in USD */
|
|
307
|
+
remainingSessionBalanceUsd: number;
|
|
299
308
|
}
|
|
300
309
|
/**
|
|
301
|
-
*
|
|
310
|
+
* Statistics about session authorizations.
|
|
311
|
+
*
|
|
312
|
+
* This provides an overview of all sessions managed by the wallet.
|
|
302
313
|
*/
|
|
303
|
-
interface
|
|
304
|
-
/**
|
|
314
|
+
interface SessionStats {
|
|
315
|
+
/** Number of active sessions */
|
|
316
|
+
activeCount: number;
|
|
317
|
+
/** Number of expired sessions */
|
|
318
|
+
expiredCount: number;
|
|
319
|
+
/** Number of revoked sessions */
|
|
320
|
+
revokedCount: number;
|
|
321
|
+
/** Total amount authorized across all active sessions in USD */
|
|
322
|
+
totalAuthorizedUsd: number;
|
|
323
|
+
/** Total amount spent across all sessions in USD */
|
|
305
324
|
totalSpentUsd: number;
|
|
306
|
-
/**
|
|
307
|
-
|
|
308
|
-
/**
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
325
|
+
/** Total remaining across all active sessions in USD */
|
|
326
|
+
totalRemainingUsd: number;
|
|
327
|
+
/** List of active sessions (summary) */
|
|
328
|
+
activeSessions: Array<{
|
|
329
|
+
id: string;
|
|
330
|
+
merchantName: string;
|
|
331
|
+
merchantPublicKey: string;
|
|
332
|
+
spendingLimitUsd: number;
|
|
333
|
+
remainingUsd: number;
|
|
334
|
+
expiresAt: Date;
|
|
335
|
+
}>;
|
|
314
336
|
}
|
|
315
337
|
|
|
316
338
|
/**
|
|
@@ -337,13 +359,7 @@ interface SessionKeyStatsResponse {
|
|
|
337
359
|
*/
|
|
338
360
|
|
|
339
361
|
/** Current SDK version */
|
|
340
|
-
declare const SDK_VERSION = "0.
|
|
341
|
-
/** Default API base URL - override with baseUrl option or MIXRPAY_BASE_URL env var */
|
|
342
|
-
declare const DEFAULT_BASE_URL: string;
|
|
343
|
-
/** Default x402 facilitator URL */
|
|
344
|
-
declare const DEFAULT_FACILITATOR_URL = "https://x402.org/facilitator";
|
|
345
|
-
/** Default request timeout in milliseconds */
|
|
346
|
-
declare const DEFAULT_TIMEOUT = 30000;
|
|
362
|
+
declare const SDK_VERSION = "0.3.0";
|
|
347
363
|
/** Supported networks */
|
|
348
364
|
declare const NETWORKS: {
|
|
349
365
|
readonly BASE_MAINNET: {
|
|
@@ -587,6 +603,148 @@ declare class AgentWallet {
|
|
|
587
603
|
* @param level - 'debug' | 'info' | 'warn' | 'error' | 'none'
|
|
588
604
|
*/
|
|
589
605
|
setLogLevel(level: LogLevel): void;
|
|
606
|
+
/**
|
|
607
|
+
* Create the X-Session-Auth header for secure API authentication.
|
|
608
|
+
* Uses signature-based authentication - private key is NEVER transmitted.
|
|
609
|
+
*
|
|
610
|
+
* @returns Headers object with X-Session-Auth
|
|
611
|
+
*/
|
|
612
|
+
private getSessionAuthHeaders;
|
|
613
|
+
/**
|
|
614
|
+
* Get an existing session or create a new one with a MixrPay merchant.
|
|
615
|
+
*
|
|
616
|
+
* This is the recommended way to interact with MixrPay-enabled APIs.
|
|
617
|
+
* If an active session exists, it will be returned. Otherwise, a new
|
|
618
|
+
* session authorization request will be created and confirmed.
|
|
619
|
+
*
|
|
620
|
+
* @param options - Session creation options
|
|
621
|
+
* @returns Active session authorization
|
|
622
|
+
*
|
|
623
|
+
* @throws {MixrPayError} If merchant is not found or session creation fails
|
|
624
|
+
*
|
|
625
|
+
* @example
|
|
626
|
+
* ```typescript
|
|
627
|
+
* const session = await wallet.getOrCreateSession({
|
|
628
|
+
* merchantPublicKey: 'pk_live_abc123...',
|
|
629
|
+
* spendingLimitUsd: 25.00,
|
|
630
|
+
* durationDays: 7,
|
|
631
|
+
* });
|
|
632
|
+
*
|
|
633
|
+
* console.log(`Session active: $${session.remainingLimitUsd} remaining`);
|
|
634
|
+
* ```
|
|
635
|
+
*/
|
|
636
|
+
getOrCreateSession(options: CreateSessionOptions): Promise<SessionAuthorization>;
|
|
637
|
+
/**
|
|
638
|
+
* Get session status for a specific merchant.
|
|
639
|
+
*
|
|
640
|
+
* @param merchantPublicKey - The merchant's public key
|
|
641
|
+
* @returns Session authorization or null if not found
|
|
642
|
+
*/
|
|
643
|
+
getSessionByMerchant(merchantPublicKey: string): Promise<SessionAuthorization | null>;
|
|
644
|
+
/**
|
|
645
|
+
* List all session authorizations for this wallet.
|
|
646
|
+
*
|
|
647
|
+
* @returns Array of session authorizations
|
|
648
|
+
*
|
|
649
|
+
* @example
|
|
650
|
+
* ```typescript
|
|
651
|
+
* const sessions = await wallet.listSessions();
|
|
652
|
+
* for (const session of sessions) {
|
|
653
|
+
* console.log(`${session.merchantName}: $${session.remainingLimitUsd} remaining`);
|
|
654
|
+
* }
|
|
655
|
+
* ```
|
|
656
|
+
*/
|
|
657
|
+
listSessions(): Promise<SessionAuthorization[]>;
|
|
658
|
+
/**
|
|
659
|
+
* Revoke a session authorization.
|
|
660
|
+
*
|
|
661
|
+
* After revocation, no further charges can be made against this session.
|
|
662
|
+
*
|
|
663
|
+
* @param sessionId - The session ID to revoke
|
|
664
|
+
* @returns true if revoked successfully
|
|
665
|
+
*
|
|
666
|
+
* @example
|
|
667
|
+
* ```typescript
|
|
668
|
+
* const revoked = await wallet.revokeSession('sess_abc123');
|
|
669
|
+
* if (revoked) {
|
|
670
|
+
* console.log('Session revoked successfully');
|
|
671
|
+
* }
|
|
672
|
+
* ```
|
|
673
|
+
*/
|
|
674
|
+
revokeSession(sessionId: string): Promise<boolean>;
|
|
675
|
+
/**
|
|
676
|
+
* Get statistics about all session authorizations.
|
|
677
|
+
*
|
|
678
|
+
* This provides an overview of active, expired, and revoked sessions,
|
|
679
|
+
* along with aggregate spending information.
|
|
680
|
+
*
|
|
681
|
+
* @returns Session statistics
|
|
682
|
+
*
|
|
683
|
+
* @example
|
|
684
|
+
* ```typescript
|
|
685
|
+
* const stats = await wallet.getSessionStats();
|
|
686
|
+
* console.log(`Active sessions: ${stats.activeCount}`);
|
|
687
|
+
* console.log(`Total authorized: $${stats.totalAuthorizedUsd.toFixed(2)}`);
|
|
688
|
+
* console.log(`Total remaining: $${stats.totalRemainingUsd.toFixed(2)}`);
|
|
689
|
+
*
|
|
690
|
+
* for (const session of stats.activeSessions) {
|
|
691
|
+
* console.log(`${session.merchantName}: $${session.remainingUsd} remaining`);
|
|
692
|
+
* }
|
|
693
|
+
* ```
|
|
694
|
+
*/
|
|
695
|
+
getSessionStats(): Promise<SessionStats>;
|
|
696
|
+
/**
|
|
697
|
+
* Charge against an active session authorization.
|
|
698
|
+
*
|
|
699
|
+
* This is useful when you need to manually charge a session outside of
|
|
700
|
+
* the `callMerchantApi()` flow.
|
|
701
|
+
*
|
|
702
|
+
* @param sessionId - The session ID to charge
|
|
703
|
+
* @param amountUsd - Amount to charge in USD
|
|
704
|
+
* @param options - Additional charge options
|
|
705
|
+
* @returns Charge result
|
|
706
|
+
*
|
|
707
|
+
* @example
|
|
708
|
+
* ```typescript
|
|
709
|
+
* const result = await wallet.chargeSession('sess_abc123', 0.05, {
|
|
710
|
+
* feature: 'ai-generation',
|
|
711
|
+
* idempotencyKey: 'unique-key-123',
|
|
712
|
+
* });
|
|
713
|
+
*
|
|
714
|
+
* console.log(`Charged $${result.amountUsd}, remaining: $${result.remainingSessionBalanceUsd}`);
|
|
715
|
+
* ```
|
|
716
|
+
*/
|
|
717
|
+
chargeSession(sessionId: string, amountUsd: number, options?: ChargeSessionOptions): Promise<ChargeResult>;
|
|
718
|
+
/**
|
|
719
|
+
* Call a MixrPay merchant's API with automatic session management.
|
|
720
|
+
*
|
|
721
|
+
* This is the recommended way to interact with MixrPay-enabled APIs.
|
|
722
|
+
* It automatically:
|
|
723
|
+
* 1. Gets or creates a session authorization
|
|
724
|
+
* 2. Adds the `X-Mixr-Session` header to the request
|
|
725
|
+
* 3. Handles payment errors and session expiration
|
|
726
|
+
*
|
|
727
|
+
* @param options - API call options
|
|
728
|
+
* @returns Response from the merchant API
|
|
729
|
+
*
|
|
730
|
+
* @example
|
|
731
|
+
* ```typescript
|
|
732
|
+
* const response = await wallet.callMerchantApi({
|
|
733
|
+
* url: 'https://api.merchant.com/generate',
|
|
734
|
+
* merchantPublicKey: 'pk_live_abc123...',
|
|
735
|
+
* method: 'POST',
|
|
736
|
+
* body: { prompt: 'Hello world' },
|
|
737
|
+
* priceUsd: 0.05,
|
|
738
|
+
* });
|
|
739
|
+
*
|
|
740
|
+
* const data = await response.json();
|
|
741
|
+
* ```
|
|
742
|
+
*/
|
|
743
|
+
callMerchantApi(options: CallMerchantApiOptions): Promise<Response>;
|
|
744
|
+
/**
|
|
745
|
+
* Parse session response data into SessionAuthorization object.
|
|
746
|
+
*/
|
|
747
|
+
private parseSessionResponse;
|
|
590
748
|
}
|
|
591
749
|
|
|
592
750
|
/**
|
|
@@ -785,177 +943,136 @@ declare class InvalidSessionKeyError extends MixrPayError {
|
|
|
785
943
|
constructor(reason?: string);
|
|
786
944
|
}
|
|
787
945
|
/**
|
|
788
|
-
* Thrown when
|
|
946
|
+
* Thrown when a session authorization has expired.
|
|
789
947
|
*
|
|
790
|
-
*
|
|
791
|
-
*
|
|
792
|
-
* - Missing required payment fields
|
|
793
|
-
* - Invalid payment parameters
|
|
794
|
-
* - Protocol version mismatch
|
|
948
|
+
* Session authorizations have an expiration date set when created. Once expired,
|
|
949
|
+
* the session can no longer be used for payments.
|
|
795
950
|
*
|
|
796
951
|
* ## Resolution
|
|
797
|
-
*
|
|
952
|
+
* Create a new session with the merchant using `wallet.getOrCreateSession()`.
|
|
953
|
+
* The SDK will automatically create a new session on the next `callMerchantApi()` call.
|
|
798
954
|
*
|
|
799
955
|
* @example
|
|
800
956
|
* ```typescript
|
|
801
957
|
* catch (error) {
|
|
802
|
-
* if (error instanceof
|
|
803
|
-
* console.log(`
|
|
804
|
-
* //
|
|
958
|
+
* if (error instanceof SessionExpiredError) {
|
|
959
|
+
* console.log(`Session ${error.sessionId} expired at ${error.expiredAt}`);
|
|
960
|
+
* // SDK will auto-create new session on next callMerchantApi()
|
|
805
961
|
* }
|
|
806
962
|
* }
|
|
807
963
|
* ```
|
|
808
964
|
*/
|
|
809
|
-
declare class
|
|
810
|
-
/**
|
|
811
|
-
readonly
|
|
812
|
-
|
|
965
|
+
declare class SessionExpiredError extends MixrPayError {
|
|
966
|
+
/** ID of the expired session */
|
|
967
|
+
readonly sessionId: string;
|
|
968
|
+
/** When the session expired (ISO string or Date) */
|
|
969
|
+
readonly expiredAt?: string;
|
|
970
|
+
constructor(sessionId: string, expiredAt?: string);
|
|
813
971
|
}
|
|
814
972
|
/**
|
|
815
|
-
*
|
|
973
|
+
* Thrown when a payment would exceed the session's spending limit.
|
|
816
974
|
*
|
|
817
|
-
*
|
|
818
|
-
*
|
|
975
|
+
* Each session authorization has a spending limit set by the user. Once the limit
|
|
976
|
+
* is reached, no more payments can be made until a new session is created.
|
|
977
|
+
*
|
|
978
|
+
* ## Resolution
|
|
979
|
+
* - Create a new session with a higher limit
|
|
980
|
+
* - Wait for the current session to be renewed (if auto-renewal is enabled)
|
|
819
981
|
*
|
|
820
982
|
* @example
|
|
821
983
|
* ```typescript
|
|
822
|
-
*
|
|
823
|
-
*
|
|
824
|
-
* }
|
|
825
|
-
*
|
|
826
|
-
* console.log('MixrPay error:', error.code, error.message);
|
|
827
|
-
* } else {
|
|
828
|
-
* console.log('Other error:', error);
|
|
984
|
+
* catch (error) {
|
|
985
|
+
* if (error instanceof SessionLimitExceededError) {
|
|
986
|
+
* console.log(`Session limit: $${error.limit}, requested: $${error.requested}`);
|
|
987
|
+
* console.log(`Remaining: $${error.remaining}`);
|
|
829
988
|
* }
|
|
830
989
|
* }
|
|
831
990
|
* ```
|
|
832
991
|
*/
|
|
833
|
-
declare
|
|
992
|
+
declare class SessionLimitExceededError extends MixrPayError {
|
|
993
|
+
/** ID of the session */
|
|
994
|
+
readonly sessionId?: string;
|
|
995
|
+
/** The session's spending limit in USD */
|
|
996
|
+
readonly limit: number;
|
|
997
|
+
/** The amount requested in USD */
|
|
998
|
+
readonly requested: number;
|
|
999
|
+
/** Remaining balance in the session (limit - used) */
|
|
1000
|
+
readonly remaining: number;
|
|
1001
|
+
constructor(limit: number, requested: number, remaining: number, sessionId?: string);
|
|
1002
|
+
}
|
|
834
1003
|
/**
|
|
835
|
-
*
|
|
1004
|
+
* Thrown when a session ID is invalid or not found.
|
|
836
1005
|
*
|
|
837
|
-
*
|
|
838
|
-
*
|
|
1006
|
+
* This can happen if:
|
|
1007
|
+
* - The session ID was incorrectly formatted
|
|
1008
|
+
* - The session was deleted
|
|
1009
|
+
* - The session belongs to a different user/merchant
|
|
1010
|
+
*
|
|
1011
|
+
* ## Resolution
|
|
1012
|
+
* Create a new session using `wallet.getOrCreateSession()`.
|
|
839
1013
|
*
|
|
840
1014
|
* @example
|
|
841
1015
|
* ```typescript
|
|
842
1016
|
* catch (error) {
|
|
843
|
-
*
|
|
844
|
-
*
|
|
1017
|
+
* if (error instanceof SessionNotFoundError) {
|
|
1018
|
+
* console.log(`Session ${error.sessionId} not found`);
|
|
1019
|
+
* const newSession = await wallet.getOrCreateSession({ ... });
|
|
1020
|
+
* }
|
|
845
1021
|
* }
|
|
846
1022
|
* ```
|
|
847
1023
|
*/
|
|
848
|
-
declare
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
*
|
|
853
|
-
* Session keys are derived private keys with on-chain spending limits that enable
|
|
854
|
-
* AI agents to sign USDC transferWithAuthorization transactions autonomously.
|
|
855
|
-
*/
|
|
856
|
-
|
|
857
|
-
/**
|
|
858
|
-
* Represents a session key for signing x402 payments.
|
|
859
|
-
*
|
|
860
|
-
* The session key format is: sk_live_{64_hex_chars} or sk_test_{64_hex_chars}
|
|
861
|
-
*/
|
|
862
|
-
declare class SessionKey {
|
|
863
|
-
private readonly privateKey;
|
|
864
|
-
private readonly account;
|
|
865
|
-
readonly address: `0x${string}`;
|
|
866
|
-
readonly isTest: boolean;
|
|
867
|
-
private constructor();
|
|
868
|
-
/**
|
|
869
|
-
* Parse a session key string into a SessionKey object.
|
|
870
|
-
*
|
|
871
|
-
* @param sessionKey - Session key in format sk_live_... or sk_test_...
|
|
872
|
-
* @returns SessionKey object
|
|
873
|
-
* @throws InvalidSessionKeyError if the format is invalid
|
|
874
|
-
*/
|
|
875
|
-
static fromString(sessionKey: string): SessionKey;
|
|
876
|
-
/**
|
|
877
|
-
* Sign EIP-712 typed data for TransferWithAuthorization.
|
|
878
|
-
*
|
|
879
|
-
* @param domain - EIP-712 domain
|
|
880
|
-
* @param message - Transfer authorization message
|
|
881
|
-
* @returns Hex-encoded signature
|
|
882
|
-
*/
|
|
883
|
-
signTransferAuthorization(domain: EIP712Domain, message: TransferWithAuthorizationMessage): Promise<Hex>;
|
|
884
|
-
/**
|
|
885
|
-
* Get the chain ID based on whether this is a test key.
|
|
886
|
-
*/
|
|
887
|
-
getDefaultChainId(): number;
|
|
1024
|
+
declare class SessionNotFoundError extends MixrPayError {
|
|
1025
|
+
/** The session ID that was not found */
|
|
1026
|
+
readonly sessionId: string;
|
|
1027
|
+
constructor(sessionId: string);
|
|
888
1028
|
}
|
|
889
1029
|
/**
|
|
890
|
-
*
|
|
1030
|
+
* Thrown when attempting to use a session that has been revoked.
|
|
891
1031
|
*
|
|
892
|
-
*
|
|
893
|
-
*
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
fromAddress: `0x${string}`;
|
|
897
|
-
toAddress: `0x${string}`;
|
|
898
|
-
value: bigint;
|
|
899
|
-
validAfter: bigint;
|
|
900
|
-
validBefore: bigint;
|
|
901
|
-
nonce: Hex;
|
|
902
|
-
chainId: number;
|
|
903
|
-
}): {
|
|
904
|
-
domain: EIP712Domain;
|
|
905
|
-
message: TransferWithAuthorizationMessage;
|
|
906
|
-
};
|
|
907
|
-
/**
|
|
908
|
-
* Generate a random 32-byte nonce as hex.
|
|
909
|
-
*/
|
|
910
|
-
declare function generateNonce(): Hex;
|
|
911
|
-
|
|
912
|
-
/**
|
|
913
|
-
* MixrPay Agent SDK - x402 Protocol Handling
|
|
1032
|
+
* Sessions can be revoked by:
|
|
1033
|
+
* - The user (wallet owner)
|
|
1034
|
+
* - The merchant
|
|
1035
|
+
* - Automatically by the system (e.g., suspicious activity)
|
|
914
1036
|
*
|
|
915
|
-
*
|
|
916
|
-
*
|
|
917
|
-
*/
|
|
918
|
-
|
|
919
|
-
/**
|
|
920
|
-
* Parse payment requirements from a 402 response.
|
|
921
|
-
*
|
|
922
|
-
* The 402 response can include payment requirements in:
|
|
923
|
-
* 1. X-Payment-Required header (JSON)
|
|
924
|
-
* 2. WWW-Authenticate header (per x402 spec)
|
|
925
|
-
* 3. Response body (JSON)
|
|
1037
|
+
* ## Resolution
|
|
1038
|
+
* Create a new session using `wallet.getOrCreateSession()`.
|
|
926
1039
|
*
|
|
927
|
-
* @
|
|
928
|
-
*
|
|
929
|
-
*
|
|
1040
|
+
* @example
|
|
1041
|
+
* ```typescript
|
|
1042
|
+
* catch (error) {
|
|
1043
|
+
* if (error instanceof SessionRevokedError) {
|
|
1044
|
+
* console.log(`Session ${error.sessionId} was revoked`);
|
|
1045
|
+
* if (error.reason) console.log(`Reason: ${error.reason}`);
|
|
1046
|
+
* }
|
|
1047
|
+
* }
|
|
1048
|
+
* ```
|
|
930
1049
|
*/
|
|
931
|
-
declare
|
|
1050
|
+
declare class SessionRevokedError extends MixrPayError {
|
|
1051
|
+
/** The session ID that was revoked */
|
|
1052
|
+
readonly sessionId: string;
|
|
1053
|
+
/** Optional reason for revocation */
|
|
1054
|
+
readonly reason?: string;
|
|
1055
|
+
constructor(sessionId: string, reason?: string);
|
|
1056
|
+
}
|
|
932
1057
|
/**
|
|
933
|
-
*
|
|
934
|
-
*
|
|
935
|
-
* This creates an EIP-3009 transferWithAuthorization, signs it with the session key,
|
|
936
|
-
* and encodes it for the X-PAYMENT header.
|
|
1058
|
+
* Check if an error is a MixrPay SDK error.
|
|
937
1059
|
*
|
|
938
|
-
* @param
|
|
939
|
-
* @
|
|
940
|
-
* @param walletAddress - The smart wallet address that holds USDC
|
|
941
|
-
* @returns Base64-encoded JSON string for the X-PAYMENT header
|
|
942
|
-
*/
|
|
943
|
-
declare function buildXPaymentHeader(requirements: PaymentRequirements, sessionKey: SessionKey, walletAddress: `0x${string}`): Promise<string>;
|
|
944
|
-
/**
|
|
945
|
-
* Check if payment requirements have expired.
|
|
946
|
-
*/
|
|
947
|
-
declare function isPaymentExpired(requirements: PaymentRequirements): boolean;
|
|
948
|
-
/**
|
|
949
|
-
* Get payment amount in USD (USDC has 6 decimals).
|
|
950
|
-
*/
|
|
951
|
-
declare function getAmountUsd(requirements: PaymentRequirements): number;
|
|
952
|
-
/**
|
|
953
|
-
* Validate that a payment amount is within acceptable limits.
|
|
1060
|
+
* @param error - The error to check
|
|
1061
|
+
* @returns true if the error is a MixrPayError or subclass
|
|
954
1062
|
*
|
|
955
|
-
* @
|
|
956
|
-
*
|
|
957
|
-
*
|
|
1063
|
+
* @example
|
|
1064
|
+
* ```typescript
|
|
1065
|
+
* try {
|
|
1066
|
+
* await wallet.fetch(...);
|
|
1067
|
+
* } catch (error) {
|
|
1068
|
+
* if (isMixrPayError(error)) {
|
|
1069
|
+
* console.log('MixrPay error:', error.code, error.message);
|
|
1070
|
+
* } else {
|
|
1071
|
+
* console.log('Other error:', error);
|
|
1072
|
+
* }
|
|
1073
|
+
* }
|
|
1074
|
+
* ```
|
|
958
1075
|
*/
|
|
959
|
-
declare function
|
|
1076
|
+
declare function isMixrPayError(error: unknown): error is MixrPayError;
|
|
960
1077
|
|
|
961
|
-
export { AgentWallet, type AgentWalletConfig,
|
|
1078
|
+
export { AgentWallet, type AgentWalletConfig, type CallMerchantApiOptions, type ChargeResult, InsufficientBalanceError, InvalidSessionKeyError, MixrPayError, type PaymentEvent, PaymentFailedError, SDK_VERSION, type SessionAuthorization, SessionExpiredError, SessionKeyExpiredError, SessionLimitExceededError, SessionNotFoundError, SessionRevokedError, type SessionStats, SpendingLimitExceededError, type SpendingStats, isMixrPayError };
|