@ffaerber/swarm-connect 0.4.0 → 0.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/README.md CHANGED
@@ -15,7 +15,8 @@ A React connect-button and wizard for [Ethereum Swarm](https://www.ethswarm.org/
15
15
  - 🐝 **Bee node detection** — checks a Bee node's `/health` endpoint and surfaces its version.
16
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`.
17
17
  - 🎟️ **Postage stamp selection** — fetches available stamps from `/stamps` and lets the user pick one.
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`).
18
+ - 🎛️ **Per-dApp requirements** (`requirements: { xdai, xbzz, postageStamp }`) — each dApp declares what "connected" means for it. Disabled requirements drop their step from the modal and from `isFullyConnected`. E.g. a dApp that manages stamps itself uses `{ xdai: true, xbzz: false, postageStamp: false }`.
19
+ - 💸 **Node-wallet funding** (`xbzz: true`) — for dApps that buy stamps themselves: shows the Bee node's own wallet (`/wallet`) and lets the user top it up with xDAI + xBZZ from their connected wallet (one-time setup). The dApp then buys stamps programmatically via `stamps.createStamp()` — the modal itself never purchases.
19
20
  - ✅ **At-a-glance status** — the button shows status dots for every gated step.
20
21
  - 🧩 **Headless hooks** — use the `useSwarmConnect` / `useBeeNode` / `usePostageStamps` / `useNodeWallet` hooks to build your own UI.
21
22
  - 🎨 **Self-contained dark theme** — scoped CSS variables and inline styles, no CSS import required.
@@ -73,12 +74,12 @@ Provides the wagmi and React Query context. Configured for the Gnosis chain with
73
74
 
74
75
  ### `<SwarmConnectButton>`
75
76
 
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.
77
+ The connect button. Opens a dark-themed modal with sequential, gated steps — wallet network (Gnosis) xDAI balance Bee node node wallet → postage stamp — where each step unlocks only once the previous one is satisfied. Which steps appear depends on `requirements`: wallet, network, and Bee node are always present; the xDAI balance, node-wallet funding, and stamp-selection steps are included only when their requirement is enabled, and the numbering adapts. 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.
77
78
 
78
79
  | Prop | Type | Default | Description |
79
80
  | --- | --- | --- | --- |
80
81
  | `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. |
82
+ | `requirements` | `SwarmConnectRequirements` | `{ xdai: true, xbzz: false, postageStamp: true }` | Which requirements this dApp needs; disabled ones drop their step. See [Requirements](#requirements). |
82
83
  | `label` | `string` | auto | Overrides the button label. Defaults to `Connect to Swarm`, or the truncated address once fully connected. |
83
84
 
84
85
  ### `<SwarmConnectModal>`
@@ -101,14 +102,18 @@ function Status() {
101
102
  nodeWallet, // { address?, xdai?, xbzz?, isLoading, error?, isFunded, refresh() } — the node's own wallet
102
103
  beeApiUrl, // current Bee node URL
103
104
  setBeeApiUrl, // change the Bee node URL at runtime, then re-check
104
- stampMode, // 'select' | 'create'
105
+ requirements, // resolved { xdai, xbzz, postageStamp } booleans
105
106
  isWalletConnected,
106
107
  address,
107
108
  isOnGnosis,
108
109
  chainId,
109
110
  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' })
111
+ isFullyConnected, // wallet + Gnosis + node + every enabled requirement
112
+ } = useSwarmConnect({
113
+ beeApiUrl: 'http://localhost:1633',
114
+ // this dApp needs user gas, but manages stamps itself:
115
+ requirements: { xdai: true, xbzz: false, postageStamp: false },
116
+ })
112
117
 
113
118
  return <span>{isFullyConnected ? 'Ready' : 'Not connected'}</span>
114
119
  }
@@ -124,7 +129,7 @@ const { isRunning, isChecking, version, error, check } = useBeeNode('http://loca
124
129
 
125
130
  ### `usePostageStamps(beeApiUrl?)`
126
131
 
127
- Fetches, selects, and (in create-mode UIs) buys postage stamps.
132
+ Fetches, selects, and (for dApps that buy stamps themselves) creates postage stamps.
128
133
 
129
134
  ```tsx
130
135
  const { stamps, isLoading, error, fetchStamps, selectedStampId, selectStamp,
@@ -150,28 +155,43 @@ const { address, xdai, xbzz, isLoading, error, isFunded, refresh } =
150
155
 
151
156
  ```ts
152
157
  interface SwarmConnectConfig {
153
- beeApiUrl?: string // initial Bee node URL; defaults to http://localhost:1633
154
- stampMode?: 'select' | 'create' // defaults to 'select'
158
+ beeApiUrl?: string // initial Bee node URL; defaults to http://localhost:1633
159
+ requirements?: SwarmConnectRequirements // which steps this dApp needs; see below
155
160
  }
156
161
  ```
157
162
 
158
163
  `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
164
 
160
- ### Stamp modes
165
+ ### Requirements
161
166
 
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.
167
+ Every dApp needs a connected wallet, the Gnosis chain, and a reachable Bee node those steps are always present. The rest is per-dApp via `requirements`; a disabled requirement drops its step from the modal and is ignored by `isFullyConnected`:
163
168
 
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}`).
169
+ ```ts
170
+ interface SwarmConnectRequirements {
171
+ xdai?: boolean // default true — user wallet must hold xDAI for gas
172
+ xbzz?: boolean // default false — node wallet funded (xDAI + xBZZ) so the dApp can buy stamps
173
+ postageStamp?: boolean // default true — user must select a postage stamp in the modal
174
+ }
175
+ ```
176
+
177
+ - **`xdai`** — adds the *Balance* step: shows the connected wallet's native xDAI on Gnosis and links a faucet while it's empty. Disable for read-only dApps that never transact from the user's wallet.
178
+ - **`xbzz`** — adds the *Node wallet* step: shows the Bee node's own xDAI/xBZZ balances (`GET /wallet`) 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). Enable when your dApp buys stamps itself — purchasing is your dApp's job via `stamps.createStamp({ amount, depth, label })` (`POST /stamps/{amount}/{depth}`); the modal never buys.
179
+ - **`postageStamp`** — adds the *Postage stamp* step where the user picks an existing stamp. Disable when the dApp manages stamps itself (e.g. it creates and tracks its own batches).
180
+
181
+ Example — a dApp that needs user gas but manages stamps itself:
182
+
183
+ ```tsx
184
+ <SwarmConnectButton requirements={{ xdai: true, xbzz: false, postageStamp: false }} />
185
+ ```
166
186
 
167
- "Fully connected" requires all of the following:
187
+ "Fully connected" requires all of the following (skipping disabled requirements):
168
188
 
169
189
  1. A connected wallet.
170
190
  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).
191
+ 3. *(`xdai`)* A non-zero xDAI balance on that wallet.
172
192
  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.
193
+ 5. *(`xbzz`)* The node's wallet funded with xDAI + xBZZ.
194
+ 6. *(`postageStamp`)* A selected postage stamp.
175
195
 
176
196
  ## Development
177
197
 
@@ -184,7 +204,7 @@ npm run build # build the library + type declarations to dist/
184
204
 
185
205
  ### Demo app
186
206
 
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).
207
+ `npm run dev` serves a playground in [`example/`](./example) for testing sign-in end to end. It shows **four connection scenarios side by side** — classic stamp selection, dApp-managed stamps (`postageStamp: false`), dApp-buys-stamps with node funding (`xbzz: true`), and a minimal node-only flow — each with its own `useSwarmConnect` instance and modal, so you can see how the gated steps adapt to `requirements`. Once a scenario is fully connected its card shows the live state: wallet address, chain, xDAI balance, Bee node URL + version, the node's overlay address, node-wallet balances, and the selected postage stamp (as applicable). Point it at a running Bee node (defaults to `http://localhost:1633`, editable in each modal).
188
208
 
189
209
  **`ENOSPC: System limit for number of file watchers reached`?** Your machine's inotify watch limit is exhausted. Either:
190
210
 
@@ -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, stampMode, label }: SwarmConnectButtonProps): import("react").JSX.Element;
5
+ export declare function SwarmConnectButton({ beeApiUrl, requirements, label }: SwarmConnectButtonProps): import("react").JSX.Element;
6
6
  export {};
@@ -1,16 +1,17 @@
1
- import { BeeNodeStatus, NodeWalletState, PostageStampsState, StampMode } from '../types';
1
+ import { BeeNodeStatus, NodeWalletState, PostageStampsState, SwarmConnectRequirements } from '../types';
2
2
  interface SwarmConnectModalProps {
3
3
  onClose: () => void;
4
4
  beeNode: BeeNodeStatus & {
5
5
  check: () => void;
6
+ disconnect: () => void;
6
7
  };
7
8
  stamps: PostageStampsState;
8
9
  beeApiUrl: string;
9
10
  setBeeApiUrl: (url: string) => void;
10
- /** 'create' adds the node-wallet funding step and the buy-stamp form. */
11
- stampMode?: StampMode;
11
+ /** Per-dApp requirements; disabled ones drop their step. See SwarmConnectRequirements. */
12
+ requirements?: SwarmConnectRequirements;
12
13
  /** Pass the instance from useSwarmConnect to share state; created internally otherwise. */
13
14
  nodeWallet?: NodeWalletState;
14
15
  }
15
- export declare function SwarmConnectModal({ onClose, beeNode, stamps, beeApiUrl, setBeeApiUrl, stampMode, nodeWallet: nodeWalletProp, }: SwarmConnectModalProps): import("react").JSX.Element;
16
+ export declare function SwarmConnectModal({ onClose, beeNode, stamps, beeApiUrl, setBeeApiUrl, requirements, nodeWallet: nodeWalletProp, }: SwarmConnectModalProps): import("react").JSX.Element;
16
17
  export {};
@@ -27,9 +27,8 @@ export declare function NodeWalletStep({ locked, nodeWallet }: {
27
27
  locked: boolean;
28
28
  nodeWallet: NodeWalletState;
29
29
  }): import("react").JSX.Element;
30
- export declare function StampStep({ stamps, locked, lockedHint, canCreate }: {
30
+ export declare function StampStep({ stamps, locked, lockedHint }: {
31
31
  stamps: PostageStampsState;
32
32
  locked: boolean;
33
33
  lockedHint: string;
34
- canCreate: boolean;
35
34
  }): import("react").JSX.Element;
@@ -8,6 +8,9 @@ export declare const BZZ_DECIMALS = 16;
8
8
  /** Suggested one-time top-up amounts for the Bee node's wallet. */
9
9
  export declare const DEFAULT_FUND_XDAI = "0.1";
10
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";
11
+ /** Default per-dApp requirements; see SwarmConnectRequirements. */
12
+ export declare const DEFAULT_REQUIREMENTS: {
13
+ readonly xdai: true;
14
+ readonly xbzz: false;
15
+ readonly postageStamp: true;
16
+ };
@@ -1,5 +1,6 @@
1
1
  export declare function useBeeNode(beeApiUrl?: string): {
2
2
  check: () => Promise<void>;
3
+ disconnect: () => void;
3
4
  isRunning: boolean;
4
5
  isChecking: boolean;
5
6
  version?: string;
package/dist/index.d.ts CHANGED
@@ -5,4 +5,4 @@ export { useSwarmConnect } from './hooks/useSwarmConnect';
5
5
  export { useBeeNode } from './hooks/useBeeNode';
6
6
  export { usePostageStamps } from './hooks/usePostageStamps';
7
7
  export { useNodeWallet } from './hooks/useNodeWallet';
8
- export type { SwarmConnectConfig, SwarmConnectState, StampMode, BeeNodeStatus, PostageStamp, PostageStampsState, CreateStampOptions, BalanceState, NodeWalletState, } from './types';
8
+ export type { SwarmConnectConfig, SwarmConnectState, SwarmConnectRequirements, BeeNodeStatus, PostageStamp, PostageStampsState, CreateStampOptions, BalanceState, NodeWalletState, } from './types';