@nibgate/sdk 0.1.0 → 0.1.2

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 CHANGED
@@ -17,6 +17,8 @@ import { createCircleGatewayServer, createNibgateServer } from '@nibgate/sdk/ser
17
17
 
18
18
  This works with Next.js, React apps with an API backend, Express, NestJS, Remix, SvelteKit, Astro SSR, custom servers, and CMS/plugin environments. Plain HTML can use the widget and browser events, but real gating still requires a server, edge function, API route, or signed file endpoint.
19
19
 
20
+ Agents integrating Nibgate should read [`SKILL.md`](./SKILL.md) or the public copy at [https://nibgate.xyz/skill.md](https://nibgate.xyz/skill.md) for the compact operating guide.
21
+
20
22
  ## Usage
21
23
 
22
24
  First paste the widget script from your Nibgate dashboard into your site:
package/SKILL.md ADDED
@@ -0,0 +1,163 @@
1
+ ---
2
+ name: nibgate-sdk
3
+ description: Use when integrating, auditing, documenting, or modifying creator-owned paid content flows with the @nibgate/sdk package. Covers installing the package, adding the Nibgate widget, defining resources, protecting server routes, emitting content/unlock events, creating nibgate.json discovery metadata, wiring Circle Gateway/x402 one-time unlocks, and avoiding fake payment or client-only gating patterns.
4
+ ---
5
+
6
+ # Nibgate SDK
7
+
8
+ Use `@nibgate/sdk` in the creator-owned site that actually serves the content. Nibgate Hub verifies the site, indexes public metadata, and records events; the creator site keeps the protected payload and payment receiver logic.
9
+
10
+ ## Install
11
+
12
+ ```bash
13
+ npm install @nibgate/sdk
14
+ ```
15
+
16
+ Use browser helpers from `@nibgate/sdk` and access enforcement from `@nibgate/sdk/server`:
17
+
18
+ ```ts
19
+ import { checkResourceAccess, setupResourcePage, trackResourcePage } from '@nibgate/sdk'
20
+ import { createCircleGatewayServer, manifestResponse } from '@nibgate/sdk/server'
21
+ ```
22
+
23
+ ## Site Widget
24
+
25
+ Paste the dashboard widget into the verified site layout:
26
+
27
+ ```html
28
+ <script async src="https://nibgate.xyz/widget.js"
29
+ data-nibgate-site="site_..."
30
+ data-nibgate-token="ngv_..."
31
+ data-nibgate-api="https://api.nibgate.xyz">
32
+ </script>
33
+ ```
34
+
35
+ The widget proves ownership and receives browser-side content, view, unlock, receipt, and reputation events. If the widget loads after app code, package events queue and flush when ready.
36
+
37
+ ## Resource Shape
38
+
39
+ Define one stable resource per paid route, CMS row, media item, file, or API product:
40
+
41
+ ```ts
42
+ const resource = {
43
+ id: post.id,
44
+ title: post.title,
45
+ description: post.excerpt,
46
+ type: 'article',
47
+ price: '0.01',
48
+ currency: 'USDC',
49
+ recipient: post.creatorWallet,
50
+ path: `/posts/${post.slug}`,
51
+ url: `https://creator.example/posts/${post.slug}`,
52
+ tags: ['essay', 'research'],
53
+ access: { humans: 'paid', agents: 'paid' },
54
+ unlock: { mode: 'one_time' }
55
+ }
56
+ ```
57
+
58
+ Allowed public content types are `article`, `image`, `music`, and `video`. Use stable external ids; changing ids breaks continuity for Explore, receipts, and reputation.
59
+
60
+ ## Discovery Metadata
61
+
62
+ Expose public metadata at `/nibgate.json`:
63
+
64
+ ```ts
65
+ import { manifestResponse } from '@nibgate/sdk/server'
66
+
67
+ export function GET() {
68
+ return manifestResponse({
69
+ origin: process.env.NIBGATE_SITE_ORIGIN,
70
+ resources: [resource]
71
+ })
72
+ }
73
+ ```
74
+
75
+ Never include the protected payload in `nibgate.json`. It is only for public cards, indexing, and agent-readable discovery.
76
+
77
+ ## Browser Page Wiring
78
+
79
+ Track the resource page and wire unlock UI:
80
+
81
+ ```ts
82
+ import { setupResourcePage, trackResourcePage } from '@nibgate/sdk'
83
+
84
+ trackResourcePage(resource, { source: 'creator-site' })
85
+
86
+ setupResourcePage(resource, {
87
+ source: 'creator-site',
88
+ accessPath: '/api/nibgate/access',
89
+ payPath: '/api/nibgate/pay',
90
+ button: '[data-nibgate-unlock]',
91
+ status: '[data-nibgate-status]',
92
+ createPaymentSignature: walletGatewayAdapter.pay
93
+ })
94
+ ```
95
+
96
+ For custom flows, use `checkResourceAccess(resource, options)` directly. The browser can start checkout and report state, but the server must verify payment and issue unlock proof before returning protected content.
97
+
98
+ ## Server Access Route
99
+
100
+ Protect paid content in a server route, API handler, middleware, guard, or signed file endpoint:
101
+
102
+ ```ts
103
+ import { createCircleGatewayServer } from '@nibgate/sdk/server'
104
+
105
+ const nibgate = createCircleGatewayServer({
106
+ origin: process.env.NIBGATE_SITE_ORIGIN,
107
+ secret: process.env.NIBGATE_SECRET,
108
+ network: process.env.NIBGATE_PAYMENT_NETWORK || 'eip155:5042002'
109
+ })
110
+
111
+ export function GET(request: Request) {
112
+ return nibgate.accessResponse(request, resource, {
113
+ getContent: async () => new Response(protectedHtml, {
114
+ headers: { 'content-type': 'text/html; charset=utf-8' }
115
+ })
116
+ })
117
+ }
118
+ ```
119
+
120
+ The route should return a real `402 Payment Required` challenge until the request has a valid payment proof or signed unlock proof.
121
+
122
+ ## Required Env Vars
123
+
124
+ Creator site:
125
+
126
+ ```bash
127
+ NIBGATE_SITE_ORIGIN=https://creator.example
128
+ NIBGATE_SITE_ID=site_...
129
+ NIBGATE_SITE_TOKEN=ngv_...
130
+ NIBGATE_API_BASE=https://api.nibgate.xyz
131
+ NIBGATE_SECRET=server_only_unlock_secret
132
+ NIBGATE_PAYMENT_MODE=circle-gateway
133
+ NIBGATE_PAYMENT_NETWORK=eip155:5042002
134
+ NIBGATE_SELLER_ADDRESS=0xCreatorReceiver
135
+ ```
136
+
137
+ Only expose values with `NEXT_PUBLIC_` or client-side env names when they are intentionally public, such as site id, public token, API base, or reputation contract address. Never ship `NIBGATE_SECRET`, private keys, R2 credentials, Resend keys, database URLs, or privileged payment credentials to browser code.
138
+
139
+ ## Payments
140
+
141
+ For v1, treat `unlock.mode: 'one_time'` with Circle Gateway/x402 as the production path. A Gateway/x402 payment signature is a payment proof. A normal wallet message signature is not payment.
142
+
143
+ Use `NIBGATE_BUYER_PRIVATE_KEY` only in local demos or controlled tests. In production, the visitor or agent wallet signs/pays the returned `PAYMENT-REQUIRED` challenge.
144
+
145
+ ## Reputation
146
+
147
+ After a verified unlock, rating UI can submit onchain ratings for the same resource. Reputation-critical inputs should be tied to unlock/payment proof and indexed from chain activity. Page views, scroll depth, and referrers are analytics signals, not trust by themselves.
148
+
149
+ ## Never Do These
150
+
151
+ - Do not hide paid HTML with CSS or client state after rendering it publicly.
152
+ - Do not fake payment ids, receipts, unlocks, or successful access.
153
+ - Do not replace Gateway/x402 payment signatures with ordinary wallet message signatures.
154
+ - Do not store backend secrets in browser env vars.
155
+ - Do not let mutable titles or slugs be the only resource identity.
156
+ - Do not expose private content in `nibgate.json`, meta tags, JSON-LD, page source, or static builds.
157
+
158
+ ## Framework Notes
159
+
160
+ - **Next.js**: use route handlers for `/nibgate.json` and `/api/nibgate/access`; protect content before rendering paid payloads.
161
+ - **Express/NestJS**: use middleware, controllers, or guards around protected routes.
162
+ - **Astro/SvelteKit/Remix**: use SSR/server routes or endpoints; plain static builds need a protected API or signed URL for private payloads.
163
+ - **CMS apps**: save Nibgate resource settings beside each content record, including type, price, currency, receiver wallet, access policy, unlock mode, and discovery preference.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nibgate/sdk",
3
- "version": "0.1.0",
3
+ "version": "0.1.2",
4
4
  "description": "Framework-agnostic browser and server package for creator-owned gated content, unlock events, and receipts.",
5
5
  "type": "module",
6
6
  "sideEffects": false,
@@ -39,7 +39,8 @@
39
39
  },
40
40
  "files": [
41
41
  "src",
42
- "README.md"
42
+ "README.md",
43
+ "SKILL.md"
43
44
  ],
44
45
  "keywords": [
45
46
  "nibgate",
@@ -1,5 +1,7 @@
1
1
  import { stringifyJson } from './json.js';
2
2
 
3
+ const runtimeImport = new Function('specifier', 'return import(specifier)');
4
+
3
5
  function encodeBase64(value) {
4
6
  const text = typeof value === 'string' ? value : stringifyJson(value);
5
7
  if (typeof Buffer !== 'undefined') return Buffer.from(text).toString('base64');
@@ -18,8 +20,8 @@ export async function createCircleGatewayBrowserAdapter(options = {}) {
18
20
  }
19
21
 
20
22
  const circleClientModule = options.clientModule || (options.clientModuleUrl
21
- ? await import(options.clientModuleUrl)
22
- : await import('@circle-fin/x402-batching/client'));
23
+ ? await runtimeImport(options.clientModuleUrl)
24
+ : await runtimeImport('@circle-fin/x402-batching/client'));
23
25
  const { BatchEvmScheme } = circleClientModule;
24
26
  const scheme = new BatchEvmScheme(signer);
25
27
  const network = options.network || options.chainId && `eip155:${options.chainId}` || 'eip155:5042002';
package/src/index.d.ts CHANGED
@@ -39,7 +39,7 @@ export type NibgateResource = {
39
39
  image?: string;
40
40
  description?: string;
41
41
  summary?: string;
42
- tags?: string[] | string;
42
+ tags?: readonly string[] | string;
43
43
  access?: NibgateAccessMode | NibgateAccessPolicy;
44
44
  unlock?: NibgateUnlockMode | NibgateUnlockPolicy;
45
45
  [key: string]: unknown;
package/src/server.d.ts CHANGED
@@ -18,7 +18,7 @@ export type NibgateServerResource = {
18
18
  image?: string;
19
19
  description?: string;
20
20
  summary?: string;
21
- tags?: string[] | string;
21
+ tags?: readonly string[] | string;
22
22
  currency?: string;
23
23
  access?: NibgateAccessMode | NibgateAccessPolicy;
24
24
  unlock?: NibgateUnlockMode | NibgateUnlockPolicy;