@4mica/sdk 1.2.17 → 1.3.1

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 (74) hide show
  1. package/README.md +59 -25
  2. package/dist/index.cjs +5513 -0
  3. package/dist/index.cjs.map +1 -0
  4. package/dist/index.d.cts +2730 -0
  5. package/dist/{src/abi/core4mica.d.ts → index.d.ts} +1032 -1
  6. package/dist/index.js +5439 -0
  7. package/dist/index.js.map +1 -0
  8. package/dist/models-B15ouNYG.d.cts +386 -0
  9. package/dist/models-B15ouNYG.d.ts +386 -0
  10. package/dist/server.cjs +172 -0
  11. package/dist/server.cjs.map +1 -0
  12. package/dist/server.d.cts +119 -0
  13. package/dist/server.d.ts +119 -0
  14. package/dist/server.js +165 -0
  15. package/dist/server.js.map +1 -0
  16. package/package.json +52 -43
  17. package/dist/package.json +0 -80
  18. package/dist/src/abi/core4mica.js +0 -2200
  19. package/dist/src/abi/erc20.d.ts +0 -151
  20. package/dist/src/abi/erc20.js +0 -201
  21. package/dist/src/auth.d.ts +0 -62
  22. package/dist/src/auth.js +0 -218
  23. package/dist/src/bls.d.ts +0 -6
  24. package/dist/src/bls.js +0 -247
  25. package/dist/src/chain.d.ts +0 -2
  26. package/dist/src/chain.js +0 -26
  27. package/dist/src/client/index.d.ts +0 -71
  28. package/dist/src/client/index.js +0 -120
  29. package/dist/src/client/recipient.d.ts +0 -129
  30. package/dist/src/client/recipient.js +0 -257
  31. package/dist/src/client/shared.d.ts +0 -11
  32. package/dist/src/client/shared.js +0 -17
  33. package/dist/src/client/user.d.ts +0 -109
  34. package/dist/src/client/user.js +0 -151
  35. package/dist/src/config.d.ts +0 -103
  36. package/dist/src/config.js +0 -197
  37. package/dist/src/constants.d.ts +0 -4
  38. package/dist/src/constants.js +0 -7
  39. package/dist/src/contract.d.ts +0 -55
  40. package/dist/src/contract.js +0 -312
  41. package/dist/src/debug.d.ts +0 -2
  42. package/dist/src/debug.js +0 -5
  43. package/dist/src/errors.d.ts +0 -58
  44. package/dist/src/errors.js +0 -81
  45. package/dist/src/guarantee.d.ts +0 -25
  46. package/dist/src/guarantee.js +0 -272
  47. package/dist/src/http.d.ts +0 -11
  48. package/dist/src/http.js +0 -57
  49. package/dist/src/index.d.ts +0 -17
  50. package/dist/src/index.js +0 -33
  51. package/dist/src/models.d.ts +0 -261
  52. package/dist/src/models.js +0 -360
  53. package/dist/src/networks.d.ts +0 -40
  54. package/dist/src/networks.js +0 -57
  55. package/dist/src/payment.d.ts +0 -82
  56. package/dist/src/payment.js +0 -67
  57. package/dist/src/rpc.d.ts +0 -38
  58. package/dist/src/rpc.js +0 -161
  59. package/dist/src/serde.d.ts +0 -5
  60. package/dist/src/serde.js +0 -33
  61. package/dist/src/signing.d.ts +0 -170
  62. package/dist/src/signing.js +0 -311
  63. package/dist/src/utils.d.ts +0 -11
  64. package/dist/src/utils.js +0 -90
  65. package/dist/src/validation.d.ts +0 -32
  66. package/dist/src/validation.js +0 -84
  67. package/dist/src/wallet/cdp.d.ts +0 -13
  68. package/dist/src/wallet/cdp.js +0 -78
  69. package/dist/src/wallet/index.d.ts +0 -2
  70. package/dist/src/wallet/index.js +0 -5
  71. package/dist/src/x402/index.d.ts +0 -90
  72. package/dist/src/x402/index.js +0 -243
  73. package/dist/src/x402/models.d.ts +0 -75
  74. package/dist/src/x402/models.js +0 -2
package/README.md CHANGED
@@ -11,10 +11,49 @@ The official TypeScript SDK for interacting with the 4Mica payment network.
11
11
  payments. This SDK provides:
12
12
 
13
13
  - **User Client**: deposit collateral, sign payments, and manage withdrawals in ETH or ERC20 tokens
14
- - **Recipient Client**: create payment tabs, verify payment guarantees, and claim from user collateral
14
+ - **Recipient Client**: issue and verify payment guarantees, and claim net credit from cleared settlement cycles
15
15
  - **X402 Flow Helper**: generate X-PAYMENT headers for 402-protected HTTP resources via an X402-compatible service
16
+ - **Server paywall** (`@4mica/sdk/server`): a runtime-neutral, edge-safe primitive for gating a route behind an x402 payment
16
17
  - **Admin RPCs**: manage user suspension and admin API keys (when authorized)
17
18
 
19
+ ## Universal: runtime-neutral core + thin adapters
20
+
21
+ `@4mica/sdk` is runtime-neutral (Node, Bun, Deno, edge). HTTP uses the global `fetch`, and the
22
+ `@4mica/sdk/server` subpath is `Buffer`-free so it runs on edge runtimes.
23
+
24
+ - **Runtime adapters** — env-driven client/paywall factories: `@4mica/sdk-node`, `@4mica/sdk-bun`, `@4mica/sdk-deno`
25
+ - **Framework adapters** — thin x402 paywall middleware: `@4mica/sdk-next`, `@4mica/sdk-express`, `@4mica/sdk-hono` (Nuxt / SvelteKit / Remix coming soon)
26
+
27
+ ### Server paywall
28
+
29
+ Gate any route behind a payment. When no valid `X-PAYMENT` header is present, the paywall returns a
30
+ `402` with the x402 payment requirements; otherwise it verifies the payment and lets the request
31
+ through, adding an `X-PAYMENT-RESPONSE` header.
32
+
33
+ ```ts
34
+ import { createPaywall } from "@4mica/sdk/server";
35
+
36
+ const paywall = createPaywall(client.rpc, {
37
+ payTo: "0x…",
38
+ asset: "0x0000000000000000000000000000000000000000",
39
+ network: "base-sepolia",
40
+ amount: "1000",
41
+ tabEndpoint: "https://your-recipient.example/tab",
42
+ });
43
+
44
+ // Web-standard (Hono, Next route handlers, SvelteKit, Remix, Deno, Bun.serve):
45
+ const result = await paywall.handle(request);
46
+ if (result instanceof Response) return result; // 402
47
+ // ...run your handler, then merge result.headers onto the response
48
+
49
+ // Or the low-level, framework-agnostic primitive:
50
+ const decision = await paywall.protect({ method, url, header: (n) => headers.get(n) });
51
+ ```
52
+
53
+ Prefer a framework adapter (`@4mica/sdk-express`, `@4mica/sdk-hono`, `@4mica/sdk-next`) for
54
+ idiomatic middleware. The paywall only **verifies** payment; on-chain settlement (`remunerate`)
55
+ remains an out-of-band recipient operation.
56
+
18
57
  ## Installation
19
58
 
20
59
  ```bash
@@ -161,8 +200,8 @@ Env vars: `4MICA_BEARER_TOKEN`, `4MICA_AUTH_URL`, `4MICA_AUTH_REFRESH_MARGIN_SEC
161
200
 
162
201
  The SDK exposes three main entry points:
163
202
 
164
- - `client.user`: payer-side operations (collateral, signing, withdrawals)
165
- - `client.recipient`: recipient-side operations (tabs, guarantees, remuneration)
203
+ - `client.user`: payer-side operations (collateral, signing, withdrawals, net-debit settlement)
204
+ - `client.recipient`: recipient-side operations (guarantees, cycle-clearing net-credit settlement)
166
205
  - `X402Flow`: helper for 402-protected HTTP resources
167
206
 
168
207
  ### End-to-end Example (Base Sepolia + x402 v2)
@@ -170,10 +209,10 @@ The SDK exposes three main entry points:
170
209
  See `examples/base-sepolia-x402-facilitator-e2e.ts` for a full flow in the `examples` folder:
171
210
 
172
211
  - deposit collateral
173
- - create/get a tab via facilitator
212
+ - resolve a payment session via the facilitator's `tabEndpoint` (returns the next `reqId`)
174
213
  - issue and verify V1 + V2 guarantees
175
- - `payTab` in ERC20 for V1
176
- - remunerate V2 only after `wachai-validation-sdk` returns a passing ERC-8004 validation
214
+ - settle a cleared cycle: payer `payNetDebit`, recipient `claimNetCredit`
215
+ - settle V2 only after `wachai-validation-sdk` returns a passing ERC-8004 validation
177
216
  - submit + finalize withdrawal
178
217
 
179
218
  Run it with:
@@ -300,18 +339,24 @@ Notes:
300
339
  - `signPayment` and `signPaymentV2` always use EIP-712 signing and will error if the scheme is not 4mica.
301
340
  - `UserClient.signPayment` supports `SigningScheme.EIP712` (default) and `SigningScheme.EIP191`.
302
341
  - `settlePayment` only hits `/settle`; resource servers should still call `/verify` first when enforcing access.
303
- - `RecipientClient.remunerate` requires the optional `@noble/curves` dependency for BLS decoding.
342
+ - `RecipientClient.claimNetCredit` requires the optional `@noble/curves` dependency for BLS decoding.
304
343
 
305
344
  ### API Methods Summary
306
345
 
307
- #### UserClient Methods
346
+ Settlement is **cycle-based**: core nets each participant's obligations for a clearing cycle into a
347
+ single net-debit or net-credit committed to an on-chain Merkle root. Participants settle by fetching
348
+ their prepared clearing action (contract address, amount, and Merkle proof) from core, then calling
349
+ the `ClearingHouse`. `cycleId` is the on-chain `bytes32` cycle identifier.
350
+
351
+ #### UserClient Methods (payer / net-debtor)
308
352
 
309
353
  - `approveErc20(token, amount)`
310
354
  - `deposit(amount, erc20Token?)`
311
355
  - `getUser()`
312
- - `getTabPaymentStatus(tabId)`
313
356
  - `signPayment(claims, scheme?)`
314
- - `payTab(tabId, reqId, amount, recipientAddress, erc20Token?)`
357
+ - `getClearingPayNetDebitAction(cycleId)` fetch the prepared `payNetDebit` action
358
+ - `payNetDebit(cycleId)` — settle the signer's committed net debit on-chain
359
+ - `markDefaulted(cycleId, debtor)` — mark a debtor defaulted past the finality deadline
315
360
  - `requestWithdrawal(amount, erc20Token?)`
316
361
  - `cancelWithdrawal(erc20Token?)`
317
362
  - `finalizeWithdrawal(erc20Token?)`
@@ -319,26 +364,16 @@ Notes:
319
364
  ERC20 approval behavior:
320
365
 
321
366
  - `deposit(amount, erc20Token)` requires a prior `approveErc20(token, amount)` call.
322
- - `payTab(...)` auto-approves the Core4Mica contract when paying an ERC20 tab and the current
323
- allowance is below the payment amount.
324
367
  - `approveErc20` returns `undefined` when the existing allowance is already sufficient.
325
368
 
326
- #### RecipientClient Methods
369
+ #### RecipientClient Methods (payee / net-creditor)
327
370
 
328
- - `createTab(userAddress, recipientAddress, erc20Token?, ttl?, guaranteeVersion?)`
329
- - `getTabPaymentStatus(tabId)`
330
371
  - `issuePaymentGuarantee(claims, signature, scheme)` — accepts V1 or V2 claims
331
372
  - `verifyPaymentGuarantee(cert)`
332
- - `remunerate(cert)` — requires `@noble/curves` peer dependency
333
- - `listSettledTabs()`
334
- - `listPendingRemunerations()`
335
- - `getTab(tabId)`
336
- - `listRecipientTabs(settlementStatuses?)`
337
- - `getTabGuarantees(tabId)`
338
- - `getLatestGuarantee(tabId)`
339
- - `getGuarantee(tabId, reqId)`
373
+ - `getClearingParticipantProof(cycleId)` — this recipient's committed position + Merkle proof
374
+ - `getClearingClaimNetCreditAction(cycleId)` — fetch the prepared `claimNetCredit` action
375
+ - `claimNetCredit(cycleId)` — claim the committed net credit on-chain (requires `@noble/curves`)
340
376
  - `listRecipientPayments()`
341
- - `getCollateralEventsForTab(tabId)`
342
377
  - `getUserAssetBalance(userAddress, assetAddress)`
343
378
 
344
379
  #### Admin / RPC Methods
@@ -368,7 +403,6 @@ import {
368
403
  const baseClaims = PaymentGuaranteeRequestClaims.new(
369
404
  userAddress,
370
405
  recipientAddress,
371
- tabId,
372
406
  amount,
373
407
  timestamp,
374
408
  erc20Token,