@proofrails/sdk 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.
@@ -0,0 +1,546 @@
1
+ /**
2
+ * Common types used across the SDK
3
+ */
4
+ type Network = 'coston2' | 'flare';
5
+ type ReceiptStatus = 'pending' | 'anchored' | 'failed';
6
+ interface SDKConfig {
7
+ /** Your ProofRails API key */
8
+ apiKey?: string;
9
+ /** Admin token for administrative operations */
10
+ adminToken?: string;
11
+ /** Network to use (coston2 or flare) */
12
+ network?: Network;
13
+ /** Base URL of the middleware (optional, defaults to production) */
14
+ baseUrl?: string;
15
+ /** Request timeout in milliseconds */
16
+ timeout?: number;
17
+ /** Number of retry attempts for failed requests (default: 3) */
18
+ retries?: number;
19
+ /** Delay between retries in milliseconds (default: 1000) */
20
+ retryDelay?: number;
21
+ }
22
+ interface APIResponse<T> {
23
+ data: T;
24
+ success: boolean;
25
+ error?: string;
26
+ }
27
+ interface PaginatedResponse<T> {
28
+ items: T[];
29
+ total: number;
30
+ page: number;
31
+ limit: number;
32
+ }
33
+ declare class ProofRailsError extends Error {
34
+ code?: string | undefined;
35
+ statusCode?: number | undefined;
36
+ details?: unknown | undefined;
37
+ constructor(message: string, code?: string | undefined, statusCode?: number | undefined, details?: unknown | undefined);
38
+ }
39
+
40
+ /**
41
+ * Core HTTP client for ProofRails API
42
+ */
43
+
44
+ interface RateLimitInfo {
45
+ limit: number;
46
+ remaining: number;
47
+ reset: number;
48
+ resetDate: Date;
49
+ }
50
+ declare class APIClient {
51
+ private baseUrl;
52
+ private apiKey?;
53
+ private adminToken?;
54
+ private timeout;
55
+ private retries;
56
+ private retryDelay;
57
+ private rateLimitInfo;
58
+ constructor(config?: SDKConfig);
59
+ getRateLimitInfo(): RateLimitInfo | null;
60
+ get<T>(endpoint: string, options?: RequestInit): Promise<T>;
61
+ post<T>(endpoint: string, body?: unknown, options?: RequestInit): Promise<T>;
62
+ put<T>(endpoint: string, body?: unknown, options?: RequestInit): Promise<T>;
63
+ delete<T>(endpoint: string, options?: RequestInit): Promise<T>;
64
+ private request;
65
+ private shouldRetry;
66
+ private retryRequest;
67
+ private extractRateLimitInfo;
68
+ setApiKey(apiKey: string): void;
69
+ setAdminToken(adminToken: string): void;
70
+ getBaseUrl(): string;
71
+ }
72
+
73
+ /**
74
+ * Project and API key related types
75
+ */
76
+ interface Project {
77
+ projectId: string;
78
+ label?: string;
79
+ createdAt: string;
80
+ receiptsCount?: number;
81
+ }
82
+ interface APIKey {
83
+ apiKey: string;
84
+ projectId: string;
85
+ label?: string;
86
+ createdAt: string;
87
+ }
88
+ interface CreateProjectOptions {
89
+ label?: string;
90
+ }
91
+ interface CreateAPIKeyOptions {
92
+ projectId: string;
93
+ label?: string;
94
+ }
95
+ interface WhoAmIResponse {
96
+ projectId: string;
97
+ label?: string;
98
+ }
99
+
100
+ /**
101
+ * Project and API key management module
102
+ */
103
+
104
+ declare class ProjectsModule {
105
+ private client;
106
+ constructor(client: APIClient);
107
+ getInfo(): Promise<WhoAmIResponse>;
108
+ rotateKey(): Promise<APIKey>;
109
+ create(options?: CreateProjectOptions): Promise<APIKey>;
110
+ }
111
+ declare class AdminModule {
112
+ private client;
113
+ constructor(client: APIClient);
114
+ createKey(options: CreateAPIKeyOptions): Promise<APIKey>;
115
+ deleteKey(keyId: string): Promise<void>;
116
+ }
117
+
118
+ /**
119
+ * Receipt related types
120
+ */
121
+
122
+ interface CreateReceiptOptions {
123
+ transactionHash: string;
124
+ chain: Network;
125
+ amount: string | number;
126
+ currency: string;
127
+ sender: string;
128
+ receiver: string;
129
+ reference: string;
130
+ callbackUrl?: string;
131
+ }
132
+ interface Receipt {
133
+ id: string;
134
+ status: ReceiptStatus;
135
+ transactionHash: string;
136
+ chain: Network;
137
+ amount: string;
138
+ currency: string;
139
+ sender: string;
140
+ receiver: string;
141
+ reference: string;
142
+ createdAt: string;
143
+ anchoredAt?: string;
144
+ anchorTx?: string;
145
+ bundleHash?: string;
146
+ projectId?: string;
147
+ onChain?: boolean;
148
+ valid?: boolean;
149
+ }
150
+ interface ReceiptArtifacts {
151
+ pain001Url?: string;
152
+ pain002Url?: string;
153
+ pain007Url?: string;
154
+ pain008Url?: string;
155
+ camt054Url?: string;
156
+ bundleUrl?: string;
157
+ manifestUrl?: string;
158
+ }
159
+ interface ListReceiptsOptions {
160
+ limit?: number;
161
+ page?: number;
162
+ status?: ReceiptStatus;
163
+ }
164
+ interface ReceiptUpdate {
165
+ id: string;
166
+ status: ReceiptStatus;
167
+ anchorTx?: string;
168
+ bundleHash?: string;
169
+ timestamp: string;
170
+ }
171
+
172
+ /**
173
+ * Receipt operations module
174
+ */
175
+
176
+ declare class ReceiptsModule {
177
+ private client;
178
+ constructor(client: APIClient);
179
+ create(options: CreateReceiptOptions): Promise<Receipt>;
180
+ get(receiptId: string): Promise<Receipt>;
181
+ list(options?: ListReceiptsOptions): Promise<PaginatedResponse<Receipt>>;
182
+ getArtifacts(receiptId: string): Promise<ReceiptArtifacts>;
183
+ }
184
+
185
+ /**
186
+ * Verification related types
187
+ */
188
+ interface VerificationResult {
189
+ valid: boolean;
190
+ bundleHash: string;
191
+ onChain: boolean;
192
+ anchorTx?: string;
193
+ blockNumber?: number;
194
+ timestamp?: string;
195
+ signature?: string;
196
+ details?: Record<string, unknown>;
197
+ }
198
+ interface VerificationProof {
199
+ receiptId: string;
200
+ bundleHash: string;
201
+ anchorTx: string;
202
+ blockNumber: number;
203
+ timestamp: string;
204
+ signature: string;
205
+ network: string;
206
+ contractAddress: string;
207
+ }
208
+
209
+ /**
210
+ * Verification module
211
+ */
212
+
213
+ declare class VerificationModule {
214
+ private client;
215
+ constructor(client: APIClient);
216
+ byReceiptId(receiptId: string): Promise<VerificationResult>;
217
+ byUrl(bundleUrl: string): Promise<VerificationResult>;
218
+ byHash(bundleHash: string): Promise<VerificationResult>;
219
+ getProof(receiptId: string): Promise<VerificationProof>;
220
+ }
221
+
222
+ /**
223
+ * ISO 20022 message types
224
+ */
225
+ type ISOMessageType = 'pain.001' | 'pain.002' | 'pain.007' | 'pain.008' | 'camt.052' | 'camt.053' | 'camt.054';
226
+ interface ISOMessage {
227
+ type: ISOMessageType;
228
+ messageId: string;
229
+ url: string;
230
+ createdAt: string;
231
+ }
232
+ interface GenerateStatementOptions {
233
+ dateFrom?: string;
234
+ dateTo?: string;
235
+ accountId?: string;
236
+ }
237
+ interface Statement {
238
+ type: 'camt.052' | 'camt.053';
239
+ url: string;
240
+ downloadUrl: string;
241
+ messageId: string;
242
+ createdAt: string;
243
+ }
244
+
245
+ /**
246
+ * Statements module
247
+ */
248
+
249
+ declare class StatementsModule {
250
+ private client;
251
+ constructor(client: APIClient);
252
+ intraday(options?: GenerateStatementOptions): Promise<Statement>;
253
+ endOfDay(options?: GenerateStatementOptions): Promise<Statement>;
254
+ }
255
+
256
+ /**
257
+ * Events module for live updates
258
+ */
259
+
260
+ interface EventListener {
261
+ stop: () => void;
262
+ }
263
+ declare class EventsModule {
264
+ private client;
265
+ constructor(client: APIClient);
266
+ listen(receiptId: string, callback: (update: ReceiptUpdate) => void): EventListener;
267
+ }
268
+
269
+ /**
270
+ * Embed module for generating embeddable widgets
271
+ */
272
+
273
+ interface EmbedOptions {
274
+ theme?: 'light' | 'dark';
275
+ width?: string;
276
+ height?: string;
277
+ }
278
+ interface WidgetResult {
279
+ iframeHtml: string;
280
+ embedUrl: string;
281
+ }
282
+ declare class EmbedModule {
283
+ private client;
284
+ constructor(client: APIClient);
285
+ widget(receiptId: string, options?: EmbedOptions): WidgetResult;
286
+ fullPage(receiptId: string): string;
287
+ }
288
+
289
+ /**
290
+ * Payment template - For simple payments between two parties
291
+ */
292
+
293
+ interface PaymentTemplateOptions {
294
+ /** Payment amount */
295
+ amount: number;
296
+ /** Who is sending the payment */
297
+ from: string;
298
+ /** Who is receiving the payment */
299
+ to: string;
300
+ /** What the payment is for */
301
+ purpose: string;
302
+ /** Blockchain transaction hash */
303
+ transactionHash: string;
304
+ /** Sender's wallet address (optional, defaults to transaction sender) */
305
+ senderWallet?: string;
306
+ /** Receiver's wallet address (optional, defaults to transaction receiver) */
307
+ receiverWallet?: string;
308
+ /** Chain/network (optional, auto-detected if not provided) */
309
+ chain?: 'coston2' | 'flare';
310
+ /** Currency (optional, auto-detected if not provided) */
311
+ currency?: string;
312
+ }
313
+ declare function createPaymentReceipt(receiptsModule: ReceiptsModule, options: PaymentTemplateOptions): Promise<Receipt>;
314
+
315
+ /**
316
+ * Donation template - For charitable donations
317
+ */
318
+
319
+ interface DonationTemplateOptions {
320
+ /** Donation amount */
321
+ amount: number;
322
+ /** Name of the donor */
323
+ donor: string;
324
+ /** Name of the organization receiving the donation */
325
+ organization: string;
326
+ /** Campaign or cause name */
327
+ campaign: string;
328
+ /** Blockchain transaction hash */
329
+ transactionHash: string;
330
+ /** Donor's wallet address (optional) */
331
+ donorWallet?: string;
332
+ /** Organization's wallet address (optional) */
333
+ organizationWallet?: string;
334
+ }
335
+ declare function createDonationReceipt(receiptsModule: ReceiptsModule, options: DonationTemplateOptions): Promise<Receipt>;
336
+
337
+ /**
338
+ * Escrow template - For escrow releases
339
+ */
340
+
341
+ interface EscrowTemplateOptions {
342
+ /** Amount being released from escrow */
343
+ amount: number;
344
+ /** Buyer's name */
345
+ buyer: string;
346
+ /** Seller's name */
347
+ seller: string;
348
+ /** Escrow identifier */
349
+ escrowId: string;
350
+ /** Reason for release */
351
+ releaseReason: string;
352
+ /** Blockchain transaction hash */
353
+ transactionHash: string;
354
+ /** Buyer's wallet address (optional) */
355
+ buyerWallet?: string;
356
+ /** Seller's wallet address (optional) */
357
+ sellerWallet?: string;
358
+ }
359
+ declare function createEscrowReceipt(receiptsModule: ReceiptsModule, options: EscrowTemplateOptions): Promise<Receipt>;
360
+
361
+ /**
362
+ * Grant template - For grant disbursements
363
+ */
364
+
365
+ interface GrantTemplateOptions {
366
+ /** Grant amount */
367
+ amount: number;
368
+ /** Name of the grantee (recipient) */
369
+ grantee: string;
370
+ /** Name of the grantor (funder) */
371
+ grantor: string;
372
+ /** Grant identifier */
373
+ grantId: string;
374
+ /** Purpose of the grant */
375
+ purpose: string;
376
+ /** Blockchain transaction hash */
377
+ transactionHash: string;
378
+ /** Grantor's wallet address (optional) */
379
+ grantorWallet?: string;
380
+ /** Grantee's wallet address (optional) */
381
+ granteeWallet?: string;
382
+ }
383
+ declare function createGrantReceipt(receiptsModule: ReceiptsModule, options: GrantTemplateOptions): Promise<Receipt>;
384
+
385
+ /**
386
+ * Refund template - For refunds
387
+ */
388
+
389
+ interface RefundTemplateOptions {
390
+ /** Refund amount */
391
+ amount: number;
392
+ /** Original receipt ID being refunded */
393
+ originalPayment: string;
394
+ /** Reason for refund */
395
+ reason: string;
396
+ /** Customer name */
397
+ customer: string;
398
+ /** Blockchain transaction hash */
399
+ transactionHash: string;
400
+ /** Business wallet address (optional) */
401
+ businessWallet?: string;
402
+ /** Customer wallet address (optional) */
403
+ customerWallet?: string;
404
+ }
405
+ declare function createRefundReceipt(receiptsModule: ReceiptsModule, options: RefundTemplateOptions): Promise<Receipt>;
406
+
407
+ /**
408
+ * Main ProofRails SDK class
409
+ */
410
+
411
+ declare class ProofRails {
412
+ private client;
413
+ /** Project and API key management */
414
+ readonly project: ProjectsModule;
415
+ /** Admin operations (requires admin token) */
416
+ readonly admin: AdminModule;
417
+ /** Receipt operations */
418
+ readonly receipts: ReceiptsModule;
419
+ /** Verification operations */
420
+ readonly verify: VerificationModule;
421
+ /** Statement generation */
422
+ readonly statements: StatementsModule;
423
+ /** Live event updates */
424
+ readonly events: EventsModule;
425
+ /** Embeddable widgets */
426
+ readonly embed: EmbedModule;
427
+ /** Beginner-friendly templates */
428
+ readonly templates: {
429
+ payment: (options: PaymentTemplateOptions) => ReturnType<typeof createPaymentReceipt>;
430
+ donation: (options: DonationTemplateOptions) => ReturnType<typeof createDonationReceipt>;
431
+ escrow: (options: EscrowTemplateOptions) => ReturnType<typeof createEscrowReceipt>;
432
+ grant: (options: GrantTemplateOptions) => ReturnType<typeof createGrantReceipt>;
433
+ refund: (options: RefundTemplateOptions) => ReturnType<typeof createRefundReceipt>;
434
+ };
435
+ constructor(config?: SDKConfig);
436
+ /**
437
+ * Create a new project with API key (self-serve)
438
+ * This is a convenience method for beginners
439
+ */
440
+ static createProject(options?: {
441
+ label?: string;
442
+ network?: Network;
443
+ baseUrl?: string;
444
+ }): Promise<{
445
+ client: ProofRails;
446
+ apiKey: string;
447
+ projectId: string;
448
+ }>;
449
+ /**
450
+ * Update the API key for this client
451
+ */
452
+ setApiKey(apiKey: string): void;
453
+ /**
454
+ * Update the admin token for this client
455
+ */
456
+ setAdminToken(adminToken: string): void;
457
+ }
458
+
459
+ /**
460
+ * User-friendly error messages and helpers
461
+ */
462
+ interface FriendlyError {
463
+ title: string;
464
+ message: string;
465
+ solution?: string;
466
+ learnMore?: string;
467
+ }
468
+ declare function getFriendlyError(error: any): FriendlyError;
469
+ declare function formatErrorForDisplay(error: any): string;
470
+
471
+ /**
472
+ * Smart auto-detection utilities
473
+ */
474
+
475
+ interface ChainInfo {
476
+ id: number;
477
+ name: string;
478
+ network: Network;
479
+ currency: string;
480
+ rpcUrl: string;
481
+ explorerUrl: string;
482
+ }
483
+ declare const SUPPORTED_CHAINS: Record<number, ChainInfo>;
484
+ /**
485
+ * Auto-detect network from chain ID
486
+ */
487
+ declare function detectNetwork(chainId?: number): Network;
488
+ /**
489
+ * Auto-detect currency from chain ID
490
+ */
491
+ declare function detectCurrency(chainId?: number): string;
492
+ /**
493
+ * Get chain info from chain ID
494
+ */
495
+ declare function getChainInfo(chainId: number): ChainInfo | null;
496
+ /**
497
+ * Check if chain is supported
498
+ */
499
+ declare function isSupportedChain(chainId: number): boolean;
500
+ /**
501
+ * Get explorer URL for transaction
502
+ */
503
+ declare function getExplorerUrl(chainId: number, txHash: string): string;
504
+ /**
505
+ * Get explorer URL for address
506
+ */
507
+ declare function getAddressExplorerUrl(chainId: number, address: string): string;
508
+
509
+ /**
510
+ * Validation helpers for common inputs
511
+ */
512
+ interface ValidationResult {
513
+ isValid: boolean;
514
+ error?: string;
515
+ }
516
+ /**
517
+ * Validate Ethereum address
518
+ */
519
+ declare function validateAddress(address: string): ValidationResult;
520
+ /**
521
+ * Validate amount
522
+ */
523
+ declare function validateAmount(amount: string | number): ValidationResult;
524
+ /**
525
+ * Validate transaction hash
526
+ */
527
+ declare function validateTransactionHash(hash: string): ValidationResult;
528
+ /**
529
+ * Validate API key format
530
+ */
531
+ declare function validateApiKey(apiKey: string): ValidationResult;
532
+ /**
533
+ * Validate purpose/reference text
534
+ */
535
+ declare function validatePurpose(purpose: string): ValidationResult;
536
+ /**
537
+ * Validate all payment fields at once
538
+ */
539
+ declare function validatePayment(params: {
540
+ amount: string | number;
541
+ to: string;
542
+ purpose: string;
543
+ transactionHash?: string;
544
+ }): ValidationResult;
545
+
546
+ export { type APIKey, type APIResponse, type ChainInfo, type CreateAPIKeyOptions, type CreateProjectOptions, type CreateReceiptOptions, type DonationTemplateOptions, type EscrowTemplateOptions, type FriendlyError, type GenerateStatementOptions, type GrantTemplateOptions, type ISOMessage, type ISOMessageType, type ListReceiptsOptions, type Network, type PaginatedResponse, type PaymentTemplateOptions, type Project, ProofRails, ProofRailsError, type RateLimitInfo, type Receipt, type ReceiptArtifacts, type ReceiptStatus, type ReceiptUpdate, type RefundTemplateOptions, type SDKConfig, type Receipt as SDKReceipt, SUPPORTED_CHAINS, type Statement, type ValidationResult, type VerificationProof, type VerificationResult, type WhoAmIResponse, ProofRails as default, detectCurrency, detectNetwork, formatErrorForDisplay, getAddressExplorerUrl, getChainInfo, getExplorerUrl, getFriendlyError, isSupportedChain, validateAddress, validateAmount, validateApiKey, validatePayment, validatePurpose, validateTransactionHash };