@proofrails/sdk 1.3.0 → 1.5.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/.env.example ADDED
@@ -0,0 +1,32 @@
1
+ # ProofRails SDK - Environment Configuration
2
+
3
+ # ============================================
4
+ # FOR SDK USERS (add this to your .env file)
5
+ # ============================================
6
+
7
+ # Required: Your ProofRails API Key
8
+ # Get this by running: ProofRails.createProject({ label: 'My App' })
9
+ PROOFRAILS_API_KEY=your_api_key_here
10
+
11
+ # ============================================
12
+ # OPTIONAL CONFIGURATION
13
+ # ============================================
14
+
15
+ # ✅ The SDK automatically connects to the production middleware:
16
+ # https://proofrails-clone-middleware.onrender.com
17
+ #
18
+ # You don't need to set this unless you're self-hosting!
19
+ # NEXT_PUBLIC_PROOFRAILS_BASE_URL=https://your-custom-deployment.com
20
+
21
+ # Admin token (only for administrative operations)
22
+ # PROOFRAILS_ADMIN_TOKEN=your_admin_token_here
23
+
24
+ # ============================================
25
+ # NETWORK CONFIGURATION (optional)
26
+ # ============================================
27
+
28
+ # Use Flare Testnet (Coston2) for development (default)
29
+ # PROOFRAILS_NETWORK=coston2
30
+
31
+ # Use Flare Mainnet for production
32
+ # PROOFRAILS_NETWORK=flare
@@ -0,0 +1,176 @@
1
+ # Beginner-Friendly Features
2
+
3
+ The ProofRails SDK includes powerful features designed specifically for developers with zero blockchain experience.
4
+
5
+ ## Smart Auto-Detection
6
+
7
+ The SDK automatically detects network settings from your connected wallet:
8
+
9
+ ```typescript
10
+ import { useProofRailsPayment } from '@proofrails/sdk/react';
11
+
12
+ const { send } = useProofRailsPayment(sdk);
13
+
14
+ // Just provide the essentials - SDK handles the rest!
15
+ await send({
16
+ amount: "10.0",
17
+ to: "0xRecipient...",
18
+ purpose: "Payment for services"
19
+ // ✨ No need to specify: chain, currency, sender
20
+ // SDK auto-detects from your wallet!
21
+ });
22
+ ```
23
+
24
+ ## Input Validation
25
+
26
+ Validate user inputs before sending to catch errors early:
27
+
28
+ ```typescript
29
+ import { validateAddress, validateAmount, validatePayment } from '@proofrails/sdk';
30
+
31
+ // Validate individual fields
32
+ const addressCheck = validateAddress("0x1234...");
33
+ if (!addressCheck.isValid) {
34
+ console.error(addressCheck.error);
35
+ // "Address must be 42 characters long (0x + 40 hex digits)"
36
+ }
37
+
38
+ const amountCheck = validateAmount("10.5");
39
+ if (!amountCheck.isValid) {
40
+ console.error(amountCheck.error);
41
+ }
42
+
43
+ // Or validate everything at once
44
+ const paymentCheck = validatePayment({
45
+ amount: "10.0",
46
+ to: "0xRecipient...",
47
+ purpose: "Payment"
48
+ });
49
+
50
+ if (!paymentCheck.isValid) {
51
+ alert(paymentCheck.error); // User-friendly message!
52
+ }
53
+ ```
54
+
55
+ ## Friendly Error Messages
56
+
57
+ Errors are automatically converted to helpful, actionable messages:
58
+
59
+ ```typescript
60
+ import { getFriendlyError, formatErrorForDisplay } from '@proofrails/sdk';
61
+
62
+ try {
63
+ await sdk.receipts.create({...});
64
+ } catch (error) {
65
+ // Get structured error info
66
+ const friendly = getFriendlyError(error);
67
+ console.log(friendly.title); // " Middleware Wallet Needs Funding"
68
+ console.log(friendly.message); // Clear explanation
69
+ console.log(friendly.solution); // "Send 0.5-1.0 C2FLR tokens..."
70
+ console.log(friendly.learnMore); // Link to docs
71
+
72
+ // Or get formatted string for display
73
+ const formatted = formatErrorForDisplay(error);
74
+ alert(formatted);
75
+ /*
76
+ Middleware Wallet Needs Funding
77
+ Your ProofRails middleware wallet doesn't have enough tokens...
78
+
79
+ Solution: Send 0.5-1.0 C2FLR tokens to your middleware wallet...
80
+
81
+ */
82
+ }
83
+ ```
84
+
85
+ ## Chain Utilities
86
+
87
+ Helper functions for working with different networks:
88
+
89
+ ```typescript
90
+ import {
91
+ detectNetwork,
92
+ detectCurrency,
93
+ getChainInfo,
94
+ getExplorerUrl
95
+ } from '@proofrails/sdk';
96
+
97
+ // Auto-detect from chain ID
98
+ const network = detectNetwork(114); // "coston2"
99
+ const currency = detectCurrency(114); // "C2FLR"
100
+
101
+ // Get full chain info
102
+ const info = getChainInfo(114);
103
+ console.log(info.name); // "Coston2"
104
+ console.log(info.rpcUrl); // "https://coston2-api.flare.network/ext/C/rpc"
105
+ console.log(info.explorerUrl); // "https://coston2-explorer.flare.network"
106
+
107
+ // Generate explorer links
108
+ const txUrl = getExplorerUrl(114, "0x123...");
109
+ // "https://coston2-explorer.flare.network/tx/0x123..."
110
+ ```
111
+
112
+ ## Complete Example
113
+
114
+ Here's a full example combining all beginner-friendly features:
115
+
116
+ ```typescript
117
+ import {
118
+ useProofRails,
119
+ useProofRailsPayment,
120
+ validatePayment,
121
+ formatErrorForDisplay
122
+ } from '@proofrails/sdk/react';
123
+
124
+ function PaymentForm() {
125
+ const sdk = useProofRails({ apiKey: 'your-key' });
126
+ const { send, loading, error } = useProofRailsPayment(sdk);
127
+
128
+ const handlePay = async (amount: string, to: string, purpose: string) => {
129
+ // 1. Validate inputs
130
+ const validation = validatePayment({ amount, to, purpose });
131
+ if (!validation.isValid) {
132
+ alert(validation.error);
133
+ return;
134
+ }
135
+
136
+ try {
137
+ // 2. Send payment (auto-detects chain & currency)
138
+ const receipt = await send({ amount, to, purpose });
139
+
140
+ alert(` Payment successful! Receipt ID: ${receipt.id}`);
141
+ } catch (err) {
142
+ // 3. Show friendly error
143
+ alert(formatErrorForDisplay(err));
144
+ }
145
+ };
146
+
147
+ return (
148
+ <div>
149
+ {/* Your form UI */}
150
+ {error && <div className="error">{error}</div>}
151
+ <button onClick={() => handlePay("10", "0x...", "Test")} disabled={loading}>
152
+ {loading ? 'Processing...' : 'Pay Now'}
153
+ </button>
154
+ </div>
155
+ );
156
+ }
157
+ ```
158
+
159
+ ## All Validation Functions
160
+
161
+ - `validateAddress(address)` - Check Ethereum address format
162
+ - `validateAmount(amount)` - Check amount is valid number > 0
163
+ - `validateTransactionHash(hash)` - Check transaction hash format
164
+ - `validateApiKey(key)` - Check API key format
165
+ - `validatePurpose(text)` - Check purpose/reference text
166
+ - `validatePayment(params)` - Validate all payment fields at once
167
+
168
+ ## Supported Chains
169
+
170
+ - **Flare** (Chain ID: 14)
171
+ - Network: `flare`
172
+ - Currency: `FLR`
173
+
174
+ - **Coston2** (Chain ID: 114) - Testnet
175
+ - Network: `coston2`
176
+ - Currency: `C2FLR`
package/README.md CHANGED
@@ -15,13 +15,48 @@ Create compliant, auditable payment receipts with just a few lines of code. No b
15
15
  - **Real time updates**, live receipt status via SSE
16
16
  - **Works everywhere**, TypeScript, JavaScript, Node.js, browsers
17
17
 
18
+ ## 🚀 Zero Configuration Required
19
+
20
+ The SDK automatically connects to our production middleware. **No backend setup needed!**
21
+
22
+ - ✅ Just install and use
23
+ - ✅ Automatic API endpoint configuration
24
+ - ✅ Built-in retry logic and error handling
25
+ - ✅ Production-ready out of the box
26
+
27
+
18
28
  ## Installation
19
29
 
20
30
  ```bash
21
- npm i @proofrails-sdk/sdk
31
+ npm install @proofrails/sdk
22
32
  # or
23
33
  yarn add @proofrails/sdk
24
- ````
34
+ # or
35
+ pnpm add @proofrails/sdk
36
+ ```
37
+
38
+ ## Getting Your API Key
39
+
40
+ Before using the SDK, you need an API key:
41
+
42
+ ### Option 1: Automatic (Recommended for Beginners)
43
+ ```javascript
44
+ import ProofRails from '@proofrails/sdk';
45
+
46
+ // Creates a new project and returns your API key
47
+ const { client, apiKey, projectId } = await ProofRails.createProject({
48
+ label: 'My App'
49
+ });
50
+
51
+ console.log('Save this API key:', apiKey);
52
+ // Use the client that's already configured
53
+ ```
54
+
55
+ ### Option 2: Manual
56
+ 1. Visit [https://www.flarestudio.xyz/sdk/proofrails-sdk/create-api-key](https://www.flarestudio.xyz/sdk/proofrails-sdk/create-api-key)
57
+ 2. Create a new project
58
+ 3. Copy your API key
59
+ 4. Use it in your code (see below)
25
60
 
26
61
  ## Quick Start
27
62
 
@@ -193,6 +228,64 @@ const listener = proofrails.events.listen('receipt-id', update => {
193
228
  });
194
229
  ```
195
230
 
231
+ ## React Integration (Hooks)
232
+
233
+ The SDK includes built-in React hooks for easy integration:
234
+
235
+ ### 1. Wrap your app with `ProofRailsProvider`
236
+
237
+ ```javascript
238
+ // app/providers.tsx
239
+ 'use client';
240
+
241
+ import { ProofRailsProvider } from '@proofrails/sdk/react';
242
+
243
+ export function Providers({ children }) {
244
+ // Option 1: Env var (recommended)
245
+ // PROOFRAILS_API_KEY must be set in .env.local
246
+ // NEXT_PUBLIC_PROOFRAILS_BASE_URL (optional)
247
+ return (
248
+ <ProofRailsProvider apiKey={process.env.NEXT_PUBLIC_PROOFRAILS_API_KEY}>
249
+ {children}
250
+ </ProofRailsProvider>
251
+ );
252
+ }
253
+ ```
254
+
255
+ ### 2. Use Hooks in Components
256
+
257
+ ```javascript
258
+ // app/payment/page.tsx
259
+ 'use client';
260
+
261
+ import { useProofRailsPayment } from '@proofrails/sdk/react';
262
+
263
+ export default function PaymentPage() {
264
+ const { createPayment, isLoading, error, receipt } = useProofRailsPayment();
265
+
266
+ const handlePay = async () => {
267
+ try {
268
+ await createPayment({
269
+ amount: 100,
270
+ from: '0x123...',
271
+ to: '0x456...',
272
+ purpose: 'Coffee',
273
+ transactionHash: '0xabc...'
274
+ });
275
+ alert('Receipt Created!');
276
+ } catch (err) {
277
+ console.error(err);
278
+ }
279
+ };
280
+
281
+ return (
282
+ <button onClick={handlePay} disabled={isLoading}>
283
+ {isLoading ? 'Processing...' : 'Create Receipt'}
284
+ </button>
285
+ );
286
+ }
287
+ ```
288
+
196
289
  ## Embeddable Widgets
197
290
 
198
291
  ### Generate Widget