@zkp2p/pay-shared 0.0.1 → 1.0.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/src/types.ts DELETED
@@ -1,464 +0,0 @@
1
- export const PaymentPlatform = {
2
- VENMO: 'venmo',
3
- CASHAPP: 'cashapp',
4
- REVOLUT: 'revolut',
5
- WISE: 'wise',
6
- ZELLE: 'zelle',
7
- PAYPAL: 'paypal',
8
- MONZO: 'monzo',
9
- N26: 'n26',
10
- } as const;
11
-
12
- export type PaymentPlatformType = typeof PaymentPlatform[keyof typeof PaymentPlatform];
13
-
14
- // Supported chains for USDC destination
15
- // Re-exported from chains.ts for backward compatibility
16
- import { SupportedChainsMap } from './chains.js';
17
- export const SupportedChains = SupportedChainsMap;
18
-
19
- export type SupportedChainId = keyof typeof SupportedChains;
20
-
21
- // Checkout mode determines how amounts are specified
22
- export const CheckoutMode = {
23
- EXACT_TOKEN: 'exact-token', // Merchant specifies USDC amount, customer pays variable fiat
24
- EXACT_FIAT: 'exact-fiat', // Merchant specifies fiat amount, customer pays fixed fiat, merchant receives variable USDC
25
- } as const;
26
-
27
- export type CheckoutModeType = typeof CheckoutMode[keyof typeof CheckoutMode];
28
-
29
- export const OrderStatus = {
30
- SIGNAL_SUBMITTED: 'SIGNAL_SUBMITTED', // Intent tx submitted, awaiting confirmation
31
- SIGNAL_MINED: 'SIGNAL_MINED', // Intent tx confirmed, intentHash available
32
- PAYMENT_SENT: 'PAYMENT_SENT', // User marked fiat payment as sent
33
- PROOF_VERIFIED: 'PROOF_VERIFIED', // ZKP proof verified by attestation service
34
- FULFILL_SUBMITTED: 'FULFILL_SUBMITTED', // Fulfill tx submitted, awaiting confirmation
35
- FULFILLED: 'FULFILLED', // Fulfill tx confirmed, payment complete
36
- CANCELLED: 'CANCELLED', // Order cancelled
37
- EXPIRED: 'EXPIRED', // Order expired
38
- FAILED: 'FAILED', // Order failed due to error
39
- } as const;
40
-
41
- export type OrderStatusType = typeof OrderStatus[keyof typeof OrderStatus];
42
-
43
- export const ProofAttemptStatus = {
44
- PENDING: 'PENDING', // Proof received, awaiting verification
45
- VERIFYING: 'VERIFYING', // Being verified by attestation service
46
- VERIFIED: 'VERIFIED', // Proof valid, fulfillment can proceed (Order tracks tx status)
47
- REJECTED: 'REJECTED', // Proof failed verification (can retry with new proof)
48
- } as const;
49
-
50
- export type ProofAttemptStatusType = typeof ProofAttemptStatus[keyof typeof ProofAttemptStatus];
51
-
52
- export const SessionStatus = {
53
- CREATED: 'CREATED',
54
- ACTIVE: 'ACTIVE',
55
- COMPLETED: 'COMPLETED',
56
- EXPIRED: 'EXPIRED',
57
- CANCELLED: 'CANCELLED',
58
- } as const;
59
-
60
- export type SessionStatusType = typeof SessionStatus[keyof typeof SessionStatus];
61
-
62
- // Structured error codes for Order failures
63
- export const OrderErrorCode = {
64
- // Proof verification errors
65
- PROOF_INVALID: 'PROOF_INVALID', // Proof data failed attestation verification
66
- PROOF_EXPIRED: 'PROOF_EXPIRED', // Proof timestamp too old
67
- PROOF_AMOUNT_MISMATCH: 'PROOF_AMOUNT_MISMATCH', // Payment amount doesn't match intent
68
- PROOF_RECIPIENT_MISMATCH: 'PROOF_RECIPIENT_MISMATCH', // Payment recipient doesn't match
69
-
70
- // Transaction errors
71
- FULFILL_TIMEOUT: 'FULFILL_TIMEOUT', // On-chain tx timed out
72
- FULFILL_REVERTED: 'FULFILL_REVERTED', // On-chain tx reverted
73
- SIGNAL_TIMEOUT: 'SIGNAL_TIMEOUT', // Signal intent tx timed out
74
- SIGNAL_REVERTED: 'SIGNAL_REVERTED', // Signal intent tx reverted
75
-
76
- // Infrastructure errors
77
- RPC_ERROR: 'RPC_ERROR', // RPC node connection/timeout
78
- RELAYER_ERROR: 'RELAYER_ERROR', // Relayer submission failed
79
- ATTESTATION_ERROR: 'ATTESTATION_ERROR', // Attestation service unavailable
80
-
81
- // Bridge errors (cross-chain)
82
- BRIDGE_QUOTE_ERROR: 'BRIDGE_QUOTE_ERROR', // Failed to get bridge quote
83
- BRIDGE_TIMEOUT: 'BRIDGE_TIMEOUT', // Bridge fill timed out
84
-
85
- // Fee threshold errors (exact-fiat mode)
86
- FEE_THRESHOLD_EXCEEDED: 'FEE_THRESHOLD_EXCEEDED', // Quote fee exceeds merchant's max
87
-
88
- // Generic
89
- SDK_ERROR: 'SDK_ERROR', // Unknown SDK error
90
- UNKNOWN_ERROR: 'UNKNOWN_ERROR', // Uncategorized error
91
- } as const;
92
-
93
- export type OrderErrorCodeType = typeof OrderErrorCode[keyof typeof OrderErrorCode];
94
-
95
- export const MerchantEnvironment = {
96
- LIVE: 'LIVE',
97
- SANDBOX: 'SANDBOX',
98
- } as const;
99
-
100
- export type MerchantEnvironmentType = typeof MerchantEnvironment[keyof typeof MerchantEnvironment];
101
-
102
- export type Merchant = {
103
- id: string;
104
- name: string;
105
- logoUrl?: string | null;
106
- disableBranding?: boolean;
107
- apiKey: string;
108
- environment?: MerchantEnvironmentType;
109
- sandboxMerchantId?: string | null;
110
- createdAt: string;
111
- updatedAt: string;
112
- // Checkout defaults
113
- defaultChainId?: number | null;
114
- defaultCheckoutMode?: 'exact-token' | 'exact-fiat' | null;
115
- defaultFiatCurrency?: string | null;
116
- defaultMaxFeePercentage?: number | null;
117
- // Payment platform configuration (empty array = all platforms enabled)
118
- enabledPaymentPlatforms?: PaymentPlatformType[] | null;
119
- };
120
-
121
- export type CreateMerchantRequest = {
122
- name: string;
123
- logoUrl?: string;
124
- };
125
-
126
- export type CheckoutSession = {
127
- id: string;
128
- merchantId: string;
129
- amountUsdc: string;
130
- netAmountUsdc?: string | null;
131
- destinationChainId: number;
132
- destinationToken: string;
133
- recipientAddress: string;
134
- status: SessionStatusType;
135
- selectedPaymentPlatform?: PaymentPlatformType | null;
136
- selectedFiatCurrency?: string | null;
137
- successUrl?: string | null;
138
- cancelUrl?: string | null;
139
- metadata?: Record<string, string> | null;
140
- expiresAt?: string | null; // Session expiry time (default: 1 hour from creation)
141
- activeOrderId?: string | null;
142
- // Checkout mode (exact-token or exact-fiat)
143
- checkoutMode?: CheckoutModeType;
144
- // Exact-fiat mode fields
145
- fiatAmount?: string | null; // Customer's fixed fiat payment (exact-fiat mode)
146
- fiatCurrency?: string | null; // e.g., "USD" (exact-fiat mode)
147
- maxFeePercentage?: number | null; // Merchant's max acceptable fee % (exact-fiat mode)
148
- // Bridge fields (populated when destinationChainId != Base)
149
- requiresBridge?: boolean;
150
- // Payment platform configuration (empty array = all platforms enabled)
151
- enabledPaymentPlatforms?: PaymentPlatformType[] | null;
152
- createdAt: string;
153
- updatedAt: string;
154
- // Included merchant relation (when fetched with include)
155
- merchant?: {
156
- id: string;
157
- name: string;
158
- logoUrl?: string | null;
159
- environment?: MerchantEnvironmentType;
160
- disableBranding?: boolean;
161
- } | null;
162
- // Included active order relation (when fetched with include)
163
- activeOrder?: Order | null;
164
- };
165
-
166
- export type QuoteIntent = {
167
- depositId: string;
168
- processorName: string;
169
- amount: string;
170
- toAddress: string;
171
- payeeDetails: string;
172
- processorIntentData: Record<string, unknown>;
173
- fiatCurrencyCode: string;
174
- chainId: string;
175
- };
176
-
177
- export type Quote = {
178
- fiatAmount: string;
179
- fiatAmountFormatted: string;
180
- tokenAmount: string;
181
- tokenAmountFormatted: string;
182
- paymentMethod: string;
183
- payeeAddress: string;
184
- conversionRate: string;
185
- intent: QuoteIntent;
186
- payeeData?: Record<string, string>;
187
- };
188
-
189
- export type Order = {
190
- id: string;
191
- sessionId: string;
192
- merchantId: string;
193
- status: OrderStatusType;
194
- intentHash?: string | null;
195
- signalTx?: string | null;
196
- fulfillTx?: string | null;
197
- paymentPlatform?: PaymentPlatformType | null;
198
- fiatCurrency?: string | null;
199
- amountUsdc?: string | null;
200
- netAmountUsdc?: string | null;
201
- conversionRate?: string | null;
202
- recipient?: string | null;
203
- quote?: Quote | null;
204
- expiresAt?: string | null;
205
- // Bridge execution data (populated for cross-chain orders)
206
- bridgeCommitmentData?: string | null; // Encoded BridgeCommitment (stored at signal)
207
- bridgeFulfillData?: string | null; // Encoded AcrossFulfillData (stored at fulfill)
208
- bridgeDepositTxHash?: string | null; // Across deposit tx hash
209
- // Bridge status tracking
210
- bridgeStatus?: 'pending' | 'filled' | 'expired' | null;
211
- bridgeFilledAt?: string | null;
212
- bridgeFilledTxHash?: string | null; // Fill tx on destination chain
213
- // Bridge info (populated when fetched with bridge data)
214
- bridgeInfo?: OrderBridgeInfo | null;
215
- // Proof tracking
216
- currentProofId?: string | null;
217
- // Cancellation tracking
218
- cancelTx?: string | null;
219
- cancelledAt?: string | null;
220
- cancelReason?: string | null;
221
- // Error tracking
222
- errorCode?: string | null;
223
- errorMessage?: string | null;
224
- errorDetails?: Record<string, unknown> | null;
225
- failedAt?: string | null;
226
- createdAt: string;
227
- updatedAt: string;
228
- };
229
-
230
- export type ProofAttempt = {
231
- id: string;
232
- orderId: string;
233
- intentHash: string;
234
- proof: Record<string, unknown>;
235
- submittedAt: string;
236
- status: ProofAttemptStatusType;
237
- verifiedAt?: string | null;
238
- attestationResponse?: Record<string, unknown> | null;
239
- verificationError?: string | null;
240
- verificationMessage?: string | null;
241
- txHash?: string | null;
242
- txSubmittedAt?: string | null;
243
- txConfirmedAt?: string | null;
244
- txError?: string | null;
245
- };
246
-
247
- // Base fields common to both checkout modes
248
- type CreateSessionBaseRequest = {
249
- merchantId: string;
250
- destinationChainId: number;
251
- destinationToken: string; // Destination token alias (USDC/USDT) or token address
252
- recipientAddress: string;
253
- successUrl?: string;
254
- cancelUrl?: string;
255
- metadata?: Record<string, string>;
256
- // Optional: Override merchant's enabled platforms for this session
257
- enabledPaymentPlatforms?: PaymentPlatformType[];
258
- };
259
-
260
- // Exact-token mode: merchant specifies USDC amount, customer pays variable fiat
261
- type CreateSessionExactTokenRequest = CreateSessionBaseRequest & {
262
- checkoutMode?: 'exact-token';
263
- amountUsdc: string;
264
- fiatAmount?: never;
265
- fiatCurrency?: never;
266
- maxFeePercentage?: never;
267
- };
268
-
269
- // Exact-fiat mode: merchant specifies fiat amount, customer pays fixed fiat
270
- type CreateSessionExactFiatRequest = CreateSessionBaseRequest & {
271
- checkoutMode: 'exact-fiat';
272
- amountUsdc?: never;
273
- fiatAmount: string; // Fixed fiat amount customer pays
274
- fiatCurrency?: string; // e.g., "USD" (optional: falls back to merchant defaultFiatCurrency)
275
- maxFeePercentage?: number; // Max acceptable fee % (optional, default 10%)
276
- };
277
-
278
- export type CreateSessionRequest = CreateSessionExactTokenRequest | CreateSessionExactFiatRequest;
279
-
280
- /**
281
- * Bridge information returned when session requires cross-chain bridging.
282
- * Amounts are in raw units (6 decimals for USDC).
283
- */
284
- export type BridgeInfo = {
285
- required: boolean;
286
- inputAmount?: string | null; // Amount on Base (includes fees)
287
- outputAmount?: string | null; // Expected output after fees
288
- feeAmount?: string | null; // Total bridge fee
289
- minOutputAmount?: string | null; // Merchant's requested amount
290
- estimatedTimeSeconds?: number | null;
291
- };
292
-
293
- export type CreateSessionResponse = {
294
- session: CheckoutSession;
295
- sessionToken: string; // Plain-text token for checkout authentication (only returned at creation)
296
- checkoutUrl: string; // Includes session ID and token
297
- bridge?: BridgeInfo;
298
- };
299
-
300
- export type StartSessionRequest = {
301
- paymentPlatform?: PaymentPlatformType;
302
- fiatCurrency?: string;
303
- };
304
-
305
- export type StartSessionResponse = {
306
- session: CheckoutSession;
307
- order: Order;
308
- quote: Quote;
309
- intentHash: string;
310
- expiresAt?: string | null;
311
- };
312
-
313
- export type MarkPaymentSentResponse = {
314
- order: Order;
315
- };
316
-
317
- export type FulfillIntentRequest = {
318
- intentHash: string;
319
- proof: Record<string, unknown> | string;
320
- };
321
-
322
- export type FulfillIntentResponse = {
323
- order: Order;
324
- proofAttemptId: string;
325
- txHash: string;
326
- };
327
-
328
- // Error response when proof verification fails
329
- // canRetry: true means user can submit new proof for the same maker
330
- export type FulfillIntentError = {
331
- proofAttemptId: string;
332
- errorCode?: string;
333
- errorMessage: string;
334
- canRetry: boolean;
335
- };
336
-
337
- // Response type for GET /api/orders/:id/proof-attempts
338
- export type ProofAttemptsResponse = {
339
- proofAttempts: ProofAttempt[];
340
- };
341
-
342
- // Response type for GET /api/orders/:orderId/bridge-status
343
- export interface BridgeStatusResponse {
344
- status: 'pending' | 'filled' | 'expired' | 'not_applicable';
345
- destinationChainId?: string; // bigint as string
346
- outputAmountUsdc?: string; // Estimated now, may be exact for filled status
347
- fillTxHash?: string; // Transaction on destination chain
348
- depositTxHash?: string; // Transaction on Base (fulfillTx)
349
- acrossTrackingUrl?: string; // URL to track on Across website
350
- estimatedFillTime?: number; // Seconds remaining (estimated)
351
- }
352
-
353
- /**
354
- * Bridge information included in order responses.
355
- * Tracks the status of cross-chain bridging via Across Protocol.
356
- */
357
- export interface OrderBridgeInfo {
358
- status: 'PENDING' | 'DEPOSITED' | 'FILLED' | 'EXPIRED';
359
- destinationChainId: string; // bigint as string
360
- outputAmountUsdc?: string; // Net amount after bridge fees (legacy: estimate/minimum)
361
- estimatedOutputAmountUsdc?: string; // Real Across-estimated output before fulfillment slippage buffer
362
- minOutputAmountUsdc?: string; // Buffered minimum output currently enforced at fulfill
363
- fillTxHash?: string | null; // Transaction on destination chain
364
- estimatedFillTime?: number | null; // Seconds (estimated)
365
- }
366
-
367
- // ============ Webhook Types ============
368
-
369
- export const WebhookEventType = {
370
- // Order lifecycle events
371
- ORDER_CREATED: 'order.created',
372
- ORDER_PAYMENT_SENT: 'order.payment_sent',
373
- ORDER_FULFILLED: 'order.fulfilled',
374
- ORDER_FAILED: 'order.failed',
375
- ORDER_EXPIRED: 'order.expired',
376
-
377
- // Session lifecycle events
378
- SESSION_STARTED: 'session.started',
379
- SESSION_COMPLETED: 'session.completed',
380
- SESSION_ABANDONED: 'session.abandoned',
381
- } as const;
382
-
383
- export type WebhookEventTypeValue = typeof WebhookEventType[keyof typeof WebhookEventType];
384
-
385
- export const WebhookDeliveryStatus = {
386
- PENDING: 'PENDING',
387
- DELIVERED: 'DELIVERED',
388
- FAILED: 'FAILED',
389
- } as const;
390
-
391
- export type WebhookDeliveryStatusType = typeof WebhookDeliveryStatus[keyof typeof WebhookDeliveryStatus];
392
-
393
- export type Webhook = {
394
- id: string;
395
- merchantId: string;
396
- url: string;
397
- events: WebhookEventTypeValue[];
398
- active: boolean;
399
- createdAt: string;
400
- updatedAt: string;
401
- // Note: secret is only returned on creation
402
- };
403
-
404
- export type WebhookWithSecret = Webhook & {
405
- secret: string;
406
- };
407
-
408
- export type WebhookDelivery = {
409
- id: string;
410
- webhookId: string;
411
- eventType: WebhookEventTypeValue;
412
- eventId: string;
413
- status: WebhookDeliveryStatusType;
414
- attempts: number;
415
- lastAttemptAt?: string | null;
416
- nextRetryAt?: string | null;
417
- responseCode?: number | null;
418
- createdAt: string;
419
- };
420
-
421
- export type WebhookPayload = {
422
- id: string;
423
- type: WebhookEventTypeValue;
424
- timestamp: string;
425
- environment: MerchantEnvironmentType;
426
- data: {
427
- session: CheckoutSession;
428
- order?: Order | null;
429
- txHash?: string | null;
430
- error?: {
431
- code: string;
432
- message: string;
433
- } | null;
434
- };
435
- };
436
-
437
- // Webhook management API types
438
- export type CreateWebhookRequest = {
439
- url: string;
440
- events?: WebhookEventTypeValue[];
441
- };
442
-
443
- export type CreateWebhookResponse = WebhookWithSecret;
444
-
445
- export type UpdateWebhookRequest = {
446
- url?: string;
447
- events?: WebhookEventTypeValue[];
448
- active?: boolean;
449
- };
450
-
451
- export type ListWebhooksResponse = {
452
- webhooks: Webhook[];
453
- };
454
-
455
- export type ListWebhookDeliveriesResponse = {
456
- deliveries: WebhookDelivery[];
457
- };
458
-
459
- export type TestWebhookResponse = {
460
- success: boolean;
461
- deliveryId: string;
462
- responseCode?: number | null;
463
- error?: string | null;
464
- };