@ffaerber/swarm-connect 0.3.0 → 0.4.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/README.md CHANGED
@@ -1,16 +1,24 @@
1
1
  # swarm-connect
2
2
 
3
+ [![npm version](https://img.shields.io/npm/v/@ffaerber/swarm-connect.svg)](https://www.npmjs.com/package/@ffaerber/swarm-connect)
4
+ [![npm downloads](https://img.shields.io/npm/dm/@ffaerber/swarm-connect.svg)](https://www.npmjs.com/package/@ffaerber/swarm-connect)
5
+ [![license](https://img.shields.io/npm/l/@ffaerber/swarm-connect.svg)](./LICENSE)
6
+
3
7
  A React connect-button and wizard for [Ethereum Swarm](https://www.ethswarm.org/) on the [Gnosis](https://www.gnosis.io/) chain. Drop in a single `<SwarmConnectButton />` and let users connect their wallet, verify a running Bee node, and pick a postage stamp — all from one modal.
4
8
 
9
+ 📦 **npm:** [`@ffaerber/swarm-connect`](https://www.npmjs.com/package/@ffaerber/swarm-connect)
10
+
5
11
  ## Features
6
12
 
13
+ - 🪜 **Gated sequential flow** — steps unlock in order: wallet → Gnosis network → xDAI gas → Bee node → *(node wallet)* → postage stamp.
14
+ - 🦊 **Wallet connect** — wallet connection via [wagmi](https://wagmi.sh/) connectors, pinned to the Gnosis chain (ID `100`), with an xDAI balance/gas check.
7
15
  - 🐝 **Bee node detection** — checks a Bee node's `/health` endpoint and surfaces its version.
8
- - 🔧 **Editable node URL** — users can change the Bee node hostname from the modal and reconnect (defaults to `http://localhost:1633`).
16
+ - 🔧 **Editable node URL** — users can change the Bee node hostname from the modal and reconnect (defaults to `http://localhost:1633`); the chosen URL is persisted in `localStorage`.
9
17
  - 🎟️ **Postage stamp selection** — fetches available stamps from `/stamps` and lets the user pick one.
10
- - 🦊 **Wallet connect** — injected-wallet connection via [wagmi](https://wagmi.sh/), pinned to Gnosis chain (ID `100`).
11
- - ✅ **At-a-glance status** — the button shows status dots for node, stamp, wallet, and network.
12
- - 🧩 **Headless hooks** — use the `useSwarmConnect` / `useBeeNode` / `usePostageStamps` hooks to build your own UI.
13
- - 🎨 **Zero-dependency styling** — inline styles, no CSS imports required.
18
+ - 💸 **Stamp-create mode** (`stampMode: 'create'`) for dApps that buy stamps themselves: shows the Bee node's own wallet (`/wallet`), lets the user top it up with xDAI + xBZZ from their connected wallet (one-time setup), and buy stamps right from the modal (`POST /stamps`).
19
+ - ✅ **At-a-glance status** — the button shows status dots for every gated step.
20
+ - 🧩 **Headless hooks** — use the `useSwarmConnect` / `useBeeNode` / `usePostageStamps` / `useNodeWallet` hooks to build your own UI.
21
+ - 🎨 **Self-contained dark theme** — scoped CSS variables and inline styles, no CSS import required.
14
22
 
15
23
  ## Installation
16
24
 
@@ -65,11 +73,12 @@ Provides the wagmi and React Query context. Configured for the Gnosis chain with
65
73
 
66
74
  ### `<SwarmConnectButton>`
67
75
 
68
- The connect button. Opens a two-tab modal (**Swarm** for the Bee node + postage stamp, **Wallet** for connecting your wallet).
76
+ The connect button. Opens a dark-themed modal with sequential, gated steps — **1.** wallet, **2.** network (Gnosis), **3.** xDAI balance, **4.** Bee node, and **5.** postage stamp — where each step unlocks only once the previous one is satisfied. With `stampMode="create"` a **node wallet** step is inserted before the stamp step (making it six steps): it reads the Bee node's own wallet and, if it's empty, lets the user send it xDAI + xBZZ from their connected wallet so the node can buy postage stamps. The widget ships its own scoped styles (no CSS import required); the `Space Grotesk` / `Inter` / `JetBrains Mono` fonts are used when present and fall back to system fonts otherwise.
69
77
 
70
78
  | Prop | Type | Default | Description |
71
79
  | --- | --- | --- | --- |
72
80
  | `beeApiUrl` | `string` | `http://localhost:1633` | Base URL of the Bee node API. |
81
+ | `stampMode` | `'select' \| 'create'` | `'select'` | `'select'`: only pick existing stamps. `'create'`: also fund the node wallet and buy stamps from the modal. |
73
82
  | `label` | `string` | auto | Overrides the button label. Defaults to `Connect to Swarm`, or the truncated address once fully connected. |
74
83
 
75
84
  ### `<SwarmConnectModal>`
@@ -80,7 +89,7 @@ The modal rendered by `SwarmConnectButton`. Exported for advanced use if you wan
80
89
 
81
90
  ### `useSwarmConnect(config?)`
82
91
 
83
- The top-level hook combining node, stamp, wallet, and network state.
92
+ The top-level hook combining wallet, network, balance, node, node-wallet, and stamp state.
84
93
 
85
94
  ```tsx
86
95
  import { useSwarmConnect } from '@ffaerber/swarm-connect'
@@ -88,15 +97,18 @@ import { useSwarmConnect } from '@ffaerber/swarm-connect'
88
97
  function Status() {
89
98
  const {
90
99
  beeNode, // { isRunning, isChecking, version?, error?, check() }
91
- stamps, // { stamps, isLoading, error?, fetchStamps(), selectedStampId?, selectStamp() }
100
+ stamps, // { stamps, isLoading, error?, fetchStamps(), selectedStampId?, selectStamp(), createStamp(), isCreating, createError? }
101
+ nodeWallet, // { address?, xdai?, xbzz?, isLoading, error?, isFunded, refresh() } — the node's own wallet
92
102
  beeApiUrl, // current Bee node URL
93
103
  setBeeApiUrl, // change the Bee node URL at runtime, then re-check
104
+ stampMode, // 'select' | 'create'
94
105
  isWalletConnected,
95
106
  address,
96
107
  isOnGnosis,
97
108
  chainId,
98
- isFullyConnected, // node running + stamp selected + wallet connected + on Gnosis
99
- } = useSwarmConnect({ beeApiUrl: 'http://localhost:1633' })
109
+ balance, // { xdai?, isLoading, hasGas } native xDAI on Gnosis
110
+ isFullyConnected, // wallet + Gnosis + xDAI gas + node (+ funded node wallet in create mode) + stamp
111
+ } = useSwarmConnect({ beeApiUrl: 'http://localhost:1633', stampMode: 'create' })
100
112
 
101
113
  return <span>{isFullyConnected ? 'Ready' : 'Not connected'}</span>
102
114
  }
@@ -112,39 +124,78 @@ const { isRunning, isChecking, version, error, check } = useBeeNode('http://loca
112
124
 
113
125
  ### `usePostageStamps(beeApiUrl?)`
114
126
 
115
- Fetches and selects postage stamps.
127
+ Fetches, selects, and (in create-mode UIs) buys postage stamps.
116
128
 
117
129
  ```tsx
118
- const { stamps, isLoading, error, fetchStamps, selectedStampId, selectStamp } =
130
+ const { stamps, isLoading, error, fetchStamps, selectedStampId, selectStamp,
131
+ createStamp, isCreating, createError } =
119
132
  usePostageStamps('http://localhost:1633')
133
+
134
+ // Buy a batch via the node (cost = 2^depth × amount PLUR, paid by the node wallet):
135
+ const batchID = await createStamp({ amount: '1000000000', depth: 20, label: 'my-app' })
120
136
  ```
121
137
 
138
+ ### `useNodeWallet(beeApiUrl?)`
139
+
140
+ Reads the Bee node's **own** wallet — the one that pays for postage stamps — from `GET /wallet` and `GET /addresses`.
141
+
142
+ ```tsx
143
+ const { address, xdai, xbzz, isLoading, error, isFunded, refresh } =
144
+ useNodeWallet('http://localhost:1633')
145
+ ```
146
+
147
+ `isFunded` is true once the node wallet holds both xDAI (gas) and xBZZ (storage payment) — funding it is a **one-time setup**; returning users with a funded node skip the step automatically.
148
+
122
149
  ## Configuration
123
150
 
124
151
  ```ts
125
152
  interface SwarmConnectConfig {
126
- beeApiUrl?: string // initial Bee node URL; defaults to http://localhost:1633
153
+ beeApiUrl?: string // initial Bee node URL; defaults to http://localhost:1633
154
+ stampMode?: 'select' | 'create' // defaults to 'select'
127
155
  }
128
156
  ```
129
157
 
130
- `beeApiUrl` is only the **initial** value. Users can edit the node URL from the modal's Swarm tab (or programmatically via `setBeeApiUrl` from `useSwarmConnect`), which re-checks the node at the new address. This is useful when the Bee node runs on a non-default host or port.
158
+ `beeApiUrl` is only the **initial** value. Users can edit the node URL from the modal's Bee node step (or programmatically via `setBeeApiUrl` from `useSwarmConnect`), which re-checks the node at the new address and persists the choice in `localStorage` so it survives sign-out / sign-in. This is useful when the Bee node runs on a non-default host or port.
159
+
160
+ ### Stamp modes
161
+
162
+ Swarm splits responsibilities between two wallets: the **user's wallet** only needs xDAI for gas, while the **Bee node's wallet** needs xDAI *and* xBZZ because it is the one buying postage stamps on chain.
163
+
164
+ - **`'select'`** (default) — the dApp only uses stamps the user already created. No xBZZ, no node funding, no on-chain spending from the modal.
165
+ - **`'create'`** — the dApp can buy stamps. The modal gains a *node wallet* step that shows the node's xDAI/xBZZ balances and, while they're empty, offers a one-time top-up (a native xDAI transfer plus an ERC-20 xBZZ transfer to the node's address). The stamp step then also offers a *buy stamp* form (`POST /stamps/{amount}/{depth}`).
131
166
 
132
167
  "Fully connected" requires all of the following:
133
168
 
134
- 1. A reachable Bee node (`/health` responds OK).
135
- 2. A selected postage stamp.
136
- 3. A connected wallet.
137
- 4. The wallet on the Gnosis chain (chain ID `100`).
169
+ 1. A connected wallet.
170
+ 2. The wallet on the Gnosis chain (chain ID `100`).
171
+ 3. A non-zero xDAI balance on that wallet (gas for its own transactions).
172
+ 4. A reachable Bee node (`/health` responds OK).
173
+ 5. *(create mode only)* The node's wallet funded with xDAI + xBZZ.
174
+ 6. A selected postage stamp.
138
175
 
139
176
  ## Development
140
177
 
141
178
  ```bash
142
179
  npm install
143
- npm run dev # start Vite dev server
180
+ npm run dev # start the demo app (example/) on the Vite dev server
144
181
  npm run type-check # type-check without emitting
145
182
  npm run build # build the library + type declarations to dist/
146
183
  ```
147
184
 
185
+ ### Demo app
186
+
187
+ `npm run dev` serves a small playground in [`example/`](./example) for testing sign-in end to end. It renders the connect button and, once you're fully connected, shows the live state — wallet address, chain, xDAI balance, Bee node URL + version, the node's overlay (Bee) address, and the selected postage stamp. Point it at a running Bee node (defaults to `http://localhost:1633`, editable in the modal).
188
+
189
+ **`ENOSPC: System limit for number of file watchers reached`?** Your machine's inotify watch limit is exhausted. Either:
190
+
191
+ - **Quick fix** — run the dev server in polling mode: `npm run dev:poll`.
192
+ - **Permanent fix** — raise the system limit:
193
+
194
+ ```bash
195
+ echo 'fs.inotify.max_user_watches=524288' | sudo tee /etc/sysctl.d/99-inotify.conf
196
+ sudo sysctl -p /etc/sysctl.d/99-inotify.conf
197
+ ```
198
+
148
199
  The library is built with Vite in library mode and ships ESM (`swarm-connect.js`), CommonJS (`swarm-connect.umd.cjs`), and TypeScript declarations. `react`, `react-dom`, `wagmi`, `viem`, and `@tanstack/react-query` are externalized.
149
200
 
150
201
  ## License
@@ -2,5 +2,5 @@ import { SwarmConnectConfig } from '../types';
2
2
  interface SwarmConnectButtonProps extends SwarmConnectConfig {
3
3
  label?: string;
4
4
  }
5
- export declare function SwarmConnectButton({ beeApiUrl, label }: SwarmConnectButtonProps): import("react").JSX.Element;
5
+ export declare function SwarmConnectButton({ beeApiUrl, stampMode, label }: SwarmConnectButtonProps): import("react").JSX.Element;
6
6
  export {};
@@ -1,4 +1,4 @@
1
- import { BeeNodeStatus, PostageStampsState } from '../types';
1
+ import { BeeNodeStatus, NodeWalletState, PostageStampsState, StampMode } from '../types';
2
2
  interface SwarmConnectModalProps {
3
3
  onClose: () => void;
4
4
  beeNode: BeeNodeStatus & {
@@ -7,6 +7,10 @@ interface SwarmConnectModalProps {
7
7
  stamps: PostageStampsState;
8
8
  beeApiUrl: string;
9
9
  setBeeApiUrl: (url: string) => void;
10
+ /** 'create' adds the node-wallet funding step and the buy-stamp form. */
11
+ stampMode?: StampMode;
12
+ /** Pass the instance from useSwarmConnect to share state; created internally otherwise. */
13
+ nodeWallet?: NodeWalletState;
10
14
  }
11
- export declare function SwarmConnectModal({ onClose, beeNode, stamps, beeApiUrl, setBeeApiUrl }: SwarmConnectModalProps): import("react").JSX.Element;
15
+ export declare function SwarmConnectModal({ onClose, beeNode, stamps, beeApiUrl, setBeeApiUrl, stampMode, nodeWallet: nodeWalletProp, }: SwarmConnectModalProps): import("react").JSX.Element;
12
16
  export {};
@@ -0,0 +1,53 @@
1
+ import { CSSProperties, ReactNode } from 'react';
2
+ type Tone = 'ok' | 'bad' | 'warn';
3
+ type StepState = 'locked' | 'active' | 'done';
4
+ export declare function HexIcon({ size, stroke, glow, children }: {
5
+ size?: number;
6
+ stroke?: string;
7
+ glow?: boolean;
8
+ children: ReactNode;
9
+ }): import("react").JSX.Element;
10
+ export declare function Spinner({ size }: {
11
+ size?: number;
12
+ }): import("react").JSX.Element;
13
+ export declare function Badge({ children, tone }: {
14
+ children: ReactNode;
15
+ tone: Tone;
16
+ }): import("react").JSX.Element;
17
+ export declare function StatusRow({ tone, title, sub, children }: {
18
+ tone: Tone | 'flat';
19
+ title?: string;
20
+ sub?: string;
21
+ children?: ReactNode;
22
+ }): import("react").JSX.Element;
23
+ export declare function GhostBtn({ children, onClick, disabled }: {
24
+ children: ReactNode;
25
+ onClick?: () => void;
26
+ disabled?: boolean;
27
+ }): import("react").JSX.Element;
28
+ export declare function PrimaryBtn({ children, onClick, pending, block, style }: {
29
+ children: ReactNode;
30
+ onClick?: () => void;
31
+ pending?: boolean;
32
+ block?: boolean;
33
+ style?: CSSProperties;
34
+ }): import("react").JSX.Element;
35
+ export declare function LockedNotice({ children }: {
36
+ children: ReactNode;
37
+ }): import("react").JSX.Element;
38
+ /** Numbered, gated step header with a hex badge. */
39
+ export declare function StepLabel({ step, state, children, hint }: {
40
+ step: number;
41
+ state: StepState;
42
+ children: ReactNode;
43
+ hint?: ReactNode;
44
+ }): import("react").JSX.Element;
45
+ /** The bee / hex brand mark. */
46
+ export declare function BeeMark({ size }: {
47
+ size?: number;
48
+ }): import("react").JSX.Element;
49
+ /** Row of progress dots, one per gated step. */
50
+ export declare function StepDots({ states }: {
51
+ states: boolean[];
52
+ }): import("react").JSX.Element;
53
+ export {};
@@ -0,0 +1,35 @@
1
+ import { BalanceState, BeeNodeStatus, NodeWalletState, PostageStampsState } from '../types';
2
+ export declare function WalletStep({ isConnected, address }: {
3
+ isConnected: boolean;
4
+ address?: string;
5
+ }): import("react").JSX.Element;
6
+ export declare function NetworkStep({ locked, isOnGnosis, chainId }: {
7
+ locked: boolean;
8
+ isOnGnosis: boolean;
9
+ chainId?: number;
10
+ }): import("react").JSX.Element;
11
+ export declare function BalanceStep({ locked, balance }: {
12
+ locked: boolean;
13
+ balance: BalanceState;
14
+ }): import("react").JSX.Element;
15
+ export declare function NodeUrlInput({ value, disabled, onSubmit }: {
16
+ value: string;
17
+ disabled: boolean;
18
+ onSubmit: (url: string) => void;
19
+ }): import("react").JSX.Element;
20
+ export declare function BeeNodeStep({ node, beeApiUrl }: {
21
+ node: BeeNodeStatus & {
22
+ check: () => void;
23
+ };
24
+ beeApiUrl: string;
25
+ }): import("react").JSX.Element;
26
+ export declare function NodeWalletStep({ locked, nodeWallet }: {
27
+ locked: boolean;
28
+ nodeWallet: NodeWalletState;
29
+ }): import("react").JSX.Element;
30
+ export declare function StampStep({ stamps, locked, lockedHint, canCreate }: {
31
+ stamps: PostageStampsState;
32
+ locked: boolean;
33
+ lockedHint: string;
34
+ canCreate: boolean;
35
+ }): import("react").JSX.Element;
@@ -1,23 +1,13 @@
1
1
  export declare const GNOSIS_CHAIN_ID = 100;
2
2
  export declare const DEFAULT_BEE_API_URL = "http://localhost:1633";
3
3
  export declare const BEE_API_URL_STORAGE_KEY = "swarm-connect:bee-api-url";
4
- export declare const STYLES: {
5
- readonly colors: {
6
- readonly primary: "#1a56db";
7
- readonly primaryHover: "#1e429f";
8
- readonly success: "#0e9f6e";
9
- readonly successLight: "#f0fdf4";
10
- readonly error: "#f05252";
11
- readonly errorLight: "#fff5f5";
12
- readonly warning: "#ff8a4c";
13
- readonly warningLight: "#fffbf5";
14
- readonly border: "#e5e7eb";
15
- readonly text: "#111827";
16
- readonly muted: "#6b7280";
17
- readonly bg: "#ffffff";
18
- readonly overlay: "rgba(0,0,0,0.5)";
19
- readonly stepInactive: "#d1d5db";
20
- readonly stepActive: "#1a56db";
21
- readonly stepDone: "#0e9f6e";
22
- };
23
- };
4
+ /** xBZZ (bridged BZZ) ERC-20 on Gnosis chain. */
5
+ export declare const BZZ_TOKEN_ADDRESS: "0xdBF3Ea6F5beE45c02255B2c26a16F300502F68da";
6
+ /** BZZ uses 16 decimals (1 xBZZ = 1e16 PLUR), unlike the usual 18. */
7
+ export declare const BZZ_DECIMALS = 16;
8
+ /** Suggested one-time top-up amounts for the Bee node's wallet. */
9
+ export declare const DEFAULT_FUND_XDAI = "0.1";
10
+ export declare const DEFAULT_FUND_XBZZ = "0.5";
11
+ /** Suggested values for buying a postage stamp (cost = 2^depth × amount PLUR). */
12
+ export declare const DEFAULT_STAMP_DEPTH = 20;
13
+ export declare const DEFAULT_STAMP_AMOUNT = "1000000000";
@@ -0,0 +1,7 @@
1
+ import { NodeWalletState } from '../types';
2
+ /**
3
+ * Reads the Bee node's OWN wallet (GET /wallet) and Ethereum address
4
+ * (GET /addresses). This is the wallet that pays for postage stamps —
5
+ * separate from the user's browser wallet.
6
+ */
7
+ export declare function useNodeWallet(beeApiUrl?: string): NodeWalletState;
package/dist/index.d.ts CHANGED
@@ -4,4 +4,5 @@ export { SwarmConnectProvider } from './context/SwarmConnectProvider';
4
4
  export { useSwarmConnect } from './hooks/useSwarmConnect';
5
5
  export { useBeeNode } from './hooks/useBeeNode';
6
6
  export { usePostageStamps } from './hooks/usePostageStamps';
7
- export type { SwarmConnectConfig, SwarmConnectState, BeeNodeStatus, PostageStamp, PostageStampsState, } from './types';
7
+ export { useNodeWallet } from './hooks/useNodeWallet';
8
+ export type { SwarmConnectConfig, SwarmConnectState, StampMode, BeeNodeStatus, PostageStamp, PostageStampsState, CreateStampOptions, BalanceState, NodeWalletState, } from './types';