@solana/kit-plugin-wallet 0.0.0 → 0.13.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/LICENSE +22 -0
- package/README.md +401 -0
- package/dist/index.browser.cjs +622 -0
- package/dist/index.browser.cjs.map +1 -0
- package/dist/index.browser.mjs +614 -0
- package/dist/index.browser.mjs.map +1 -0
- package/dist/index.node.cjs +100 -0
- package/dist/index.node.cjs.map +1 -0
- package/dist/index.node.mjs +91 -0
- package/dist/index.node.mjs.map +1 -0
- package/dist/index.react-native.mjs +91 -0
- package/dist/index.react-native.mjs.map +1 -0
- package/dist/react/index.browser.cjs +77 -0
- package/dist/react/index.browser.cjs.map +1 -0
- package/dist/react/index.browser.mjs +59 -0
- package/dist/react/index.browser.mjs.map +1 -0
- package/dist/react/index.node.cjs +77 -0
- package/dist/react/index.node.cjs.map +1 -0
- package/dist/react/index.node.mjs +59 -0
- package/dist/react/index.node.mjs.map +1 -0
- package/dist/react/index.react-native.mjs +59 -0
- package/dist/react/index.react-native.mjs.map +1 -0
- package/dist/types/index.d.ts +4 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/react/index.d.ts +235 -0
- package/dist/types/react/index.d.ts.map +1 -0
- package/dist/types/status.d.ts +31 -0
- package/dist/types/status.d.ts.map +1 -0
- package/dist/types/store.d.ts +16 -0
- package/dist/types/store.d.ts.map +1 -0
- package/dist/types/types.d.ts +384 -0
- package/dist/types/types.d.ts.map +1 -0
- package/dist/types/wallet.d.ts +133 -0
- package/dist/types/wallet.d.ts.map +1 -0
- package/package.json +103 -7
- package/src/__typetests__/wallet-typetest.ts +129 -0
- package/src/index.ts +3 -0
- package/src/react/__typetests__/index.ts +108 -0
- package/src/react/index.ts +301 -0
- package/src/status.ts +33 -0
- package/src/store.ts +990 -0
- package/src/types/global.d.ts +6 -0
- package/src/types.ts +407 -0
- package/src/wallet.ts +218 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Anza
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
|
6
|
+
a copy of this software and associated documentation files (the
|
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
|
11
|
+
the following conditions:
|
|
12
|
+
|
|
13
|
+
The above copyright notice and this permission notice shall be
|
|
14
|
+
included in all copies or substantial portions of the Software.
|
|
15
|
+
|
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,401 @@
|
|
|
1
|
+
# Kit Plugins ➤ Wallet
|
|
2
|
+
|
|
3
|
+
[![npm][npm-image]][npm-url]
|
|
4
|
+
[![npm-downloads][npm-downloads-image]][npm-url]
|
|
5
|
+
|
|
6
|
+
[npm-downloads-image]: https://img.shields.io/npm/dm/@solana/kit-plugin-wallet.svg?style=flat
|
|
7
|
+
[npm-image]: https://img.shields.io/npm/v/@solana/kit-plugin-wallet.svg?style=flat&label=%40solana%2Fkit-plugin-wallet
|
|
8
|
+
[npm-url]: https://www.npmjs.com/package/@solana/kit-plugin-wallet
|
|
9
|
+
|
|
10
|
+
This package provides plugins that add browser wallet support to your Kit clients using [wallet-standard](https://github.com/wallet-standard/wallet-standard). They handle wallet discovery, connection lifecycle, account selection, and signer creation.
|
|
11
|
+
|
|
12
|
+
Four plugin functions are exported — each adds a `client.wallet` namespace, but they differ in how the connected wallet's signer is exposed on the client:
|
|
13
|
+
|
|
14
|
+
| Plugin | `client.payer` | `client.identity` | Use case |
|
|
15
|
+
| --------------------- | -------------- | ----------------- | ------------------------------------------------------------- |
|
|
16
|
+
| `walletSigner` | wallet signer | wallet signer | Most dApps — wallet pays fees and signs as the user identity. |
|
|
17
|
+
| `walletPayer` | wallet signer | — | Wallet pays fees; identity is managed separately. |
|
|
18
|
+
| `walletIdentity` | — | wallet signer | Backend relayer pays fees; wallet provides user identity. |
|
|
19
|
+
| `walletWithoutSigner` | — | — | Wallet state only; payer and identity are managed separately. |
|
|
20
|
+
|
|
21
|
+
## Installation
|
|
22
|
+
|
|
23
|
+
```sh
|
|
24
|
+
pnpm install @solana/kit-plugin-wallet
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## Quick start
|
|
28
|
+
|
|
29
|
+
```ts
|
|
30
|
+
import { createClient } from '@solana/kit';
|
|
31
|
+
import { solanaRpc } from '@solana/kit-plugin-rpc';
|
|
32
|
+
import { walletSigner } from '@solana/kit-plugin-wallet';
|
|
33
|
+
import { planAndSendTransactions } from '@solana/kit-plugin-instruction-plan';
|
|
34
|
+
|
|
35
|
+
const client = createClient()
|
|
36
|
+
.use(walletSigner({ chain: 'solana:mainnet' }))
|
|
37
|
+
.use(solanaRpc({ rpcUrl: 'https://api.mainnet-beta.solana.com' }))
|
|
38
|
+
.use(planAndSendTransactions());
|
|
39
|
+
|
|
40
|
+
// Read discovered wallets from state
|
|
41
|
+
const { wallets } = client.wallet.getState();
|
|
42
|
+
|
|
43
|
+
// Connect a wallet
|
|
44
|
+
await client.wallet.connect(wallets[0]);
|
|
45
|
+
|
|
46
|
+
// client.payer and client.identity are now the connected wallet's signer
|
|
47
|
+
await client.sendTransaction([myInstruction]);
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
## `walletSigner` plugin
|
|
51
|
+
|
|
52
|
+
Syncs the connected wallet's signer to both `client.payer` and `client.identity`. This is the most common choice for dApps where the user's wallet pays fees and signs as the transaction identity.
|
|
53
|
+
|
|
54
|
+
```ts
|
|
55
|
+
import { walletSigner } from '@solana/kit-plugin-wallet';
|
|
56
|
+
|
|
57
|
+
const client = createClient()
|
|
58
|
+
.use(walletSigner({ chain: 'solana:mainnet' }))
|
|
59
|
+
.use(solanaRpc({ rpcUrl: 'https://api.mainnet-beta.solana.com' }))
|
|
60
|
+
.use(planAndSendTransactions());
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
## `walletPayer` plugin
|
|
64
|
+
|
|
65
|
+
Syncs the connected wallet's signer to `client.payer` only. Use this when you need the wallet as the fee payer but don't need `client.identity`. For most dApps, prefer `walletSigner` which sets both.
|
|
66
|
+
|
|
67
|
+
```ts
|
|
68
|
+
import { walletPayer } from '@solana/kit-plugin-wallet';
|
|
69
|
+
|
|
70
|
+
const client = createClient()
|
|
71
|
+
.use(walletPayer({ chain: 'solana:mainnet' }))
|
|
72
|
+
.use(solanaRpc({ rpcUrl: 'https://api.mainnet-beta.solana.com' }))
|
|
73
|
+
.use(planAndSendTransactions());
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
## `walletIdentity` plugin
|
|
77
|
+
|
|
78
|
+
Syncs the connected wallet's signer to `client.identity` only. Use this when a separate payer (e.g. a backend relayer) pays transaction fees, but the user's wallet is needed as the identity signer.
|
|
79
|
+
|
|
80
|
+
```ts
|
|
81
|
+
import { payer } from '@solana/kit-plugin-signer';
|
|
82
|
+
import { walletIdentity } from '@solana/kit-plugin-wallet';
|
|
83
|
+
|
|
84
|
+
const client = createClient()
|
|
85
|
+
.use(payer(relayerKeypair))
|
|
86
|
+
.use(walletIdentity({ chain: 'solana:mainnet' }))
|
|
87
|
+
.use(solanaRpc({ rpcUrl: 'https://api.mainnet-beta.solana.com' }))
|
|
88
|
+
.use(planAndSendTransactions());
|
|
89
|
+
|
|
90
|
+
// client.payer is always relayerKeypair
|
|
91
|
+
// client.identity is the connected wallet's signer
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
## `walletWithoutSigner` plugin
|
|
95
|
+
|
|
96
|
+
Adds `client.wallet` without setting `client.payer` or `client.identity`. Use this alongside separate `payer()` and/or `identity()` plugins, or when the wallet's signer is used explicitly in instructions.
|
|
97
|
+
|
|
98
|
+
```ts
|
|
99
|
+
import { payer } from '@solana/kit-plugin-signer';
|
|
100
|
+
import { walletWithoutSigner } from '@solana/kit-plugin-wallet';
|
|
101
|
+
|
|
102
|
+
const client = createClient()
|
|
103
|
+
.use(payer(backendKeypair))
|
|
104
|
+
.use(walletWithoutSigner({ chain: 'solana:mainnet' }))
|
|
105
|
+
.use(solanaRpc({ rpcUrl: 'https://api.mainnet-beta.solana.com' }))
|
|
106
|
+
.use(planAndSendTransactions());
|
|
107
|
+
|
|
108
|
+
// client.payer is always backendKeypair
|
|
109
|
+
// client.wallet.getState().connected?.signer for manual use
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
## State and actions
|
|
113
|
+
|
|
114
|
+
All wallet state is accessed via `client.wallet.getState()`, which returns a referentially stable `WalletState` object (new reference only when something changes).
|
|
115
|
+
|
|
116
|
+
- **`getState().wallets`** — All discovered wallets that support the configured chain.
|
|
117
|
+
|
|
118
|
+
```ts
|
|
119
|
+
const { wallets } = client.wallet.getState();
|
|
120
|
+
for (const w of wallets) {
|
|
121
|
+
console.log(w.name, w.icon);
|
|
122
|
+
}
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
- **`getState().connected`** — The active connection (wallet, account, and signer), or `null` when disconnected.
|
|
126
|
+
|
|
127
|
+
```ts
|
|
128
|
+
const { connected } = client.wallet.getState();
|
|
129
|
+
console.log(connected?.account.address);
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
- **`getState().status`** — The current connection status: `'pending'`, `'disconnected'`, `'connecting'`, `'connected'`, `'disconnecting'`, or `'reconnecting'`.
|
|
133
|
+
|
|
134
|
+
- **`connect(wallet)`** — Connect to a wallet and select the first newly authorized account.
|
|
135
|
+
|
|
136
|
+
```ts
|
|
137
|
+
const accounts = await client.wallet.connect(selectedWallet);
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
A successful call resolves only once the wallet is connected; any failure rejects. Connecting to a different wallet keeps the current one connected until the new connection succeeds — so if the attempt fails (the user declines the prompt, the wallet authorizes no accounts, or it becomes unavailable), the previous connection is left untouched rather than torn down. The same holds for `signIn`, which rejects rather than resolving with its sign-in output when the connection can't be established. While the attempt is in flight, `status` is `'connecting'` but `getState().connected` still describes the existing connection.
|
|
141
|
+
|
|
142
|
+
If another `connect` or `signIn` starts before this one resolves (e.g. an accidental double-click), the newer request wins and owns the connection — the superseded call rejects with a `DOMException` whose `name` is `'AbortError'`. Ignore it the way you'd ignore any aborted operation:
|
|
143
|
+
|
|
144
|
+
```ts
|
|
145
|
+
try {
|
|
146
|
+
await client.wallet.connect(selectedWallet);
|
|
147
|
+
} catch (e) {
|
|
148
|
+
if ((e as Error).name !== 'AbortError') throw e; // a real failure
|
|
149
|
+
}
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
- **`disconnect(wallet?)`** — Disconnects an authorized wallet. If `wallet` is not provided, disconnects the active wallet.
|
|
153
|
+
|
|
154
|
+
- **`selectAccount(account)`** — Switch to a different account within any already-authorized wallet without reconnecting.
|
|
155
|
+
|
|
156
|
+
```ts
|
|
157
|
+
client.wallet.selectAccount(accounts[0]);
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
- **`signMessage(message)`** — Sign a raw message with the connected account.
|
|
161
|
+
|
|
162
|
+
```ts
|
|
163
|
+
const signature = await client.wallet.signMessage(new TextEncoder().encode('Hello'));
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
- **`signIn(wallet, input)`** — Sign In With Solana (SIWS-as-connect). Connects the wallet, calls `solana:signIn`, and sets up full connection state. Pass `{}` for `input` if no sign-in customization is needed. To sign in with the already-connected wallet, pass `getState().connected.wallet`.
|
|
167
|
+
|
|
168
|
+
```ts
|
|
169
|
+
const output = await client.wallet.signIn(selectedWallet, { domain: window.location.host });
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
## React hooks
|
|
173
|
+
|
|
174
|
+
The `@solana/kit-plugin-wallet/react` subpath exposes hooks for reading wallet state and driving wallet actions from React components. Install the optional peer dependencies (`react` and [`@solana/react`](https://www.npmjs.com/package/@solana/react)) and render your tree inside a `ClientProvider` whose client has a wallet plugin installed.
|
|
175
|
+
|
|
176
|
+
The **state** hooks subscribe via `useSyncExternalStore`, each to a single slice of `WalletState`, so a component only re-renders when the slice it reads changes:
|
|
177
|
+
|
|
178
|
+
| Hook | Returns |
|
|
179
|
+
| -------------------- | -------------------------------------------------------------------------------- |
|
|
180
|
+
| `useWalletStatus` | The current `WalletStatus`. |
|
|
181
|
+
| `useConnectedWallet` | The active connection (`{ account, signer, wallet }`) or null. |
|
|
182
|
+
| `useWallets` | The discovered wallets for the client's chain. |
|
|
183
|
+
| `useIsWalletReady` | `false` while the initial warm-up is in progress (`'pending'`/`'reconnecting'`). |
|
|
184
|
+
|
|
185
|
+
The **action** hooks wrap the async wallet actions with `useAction` from `@solana/react`, returning its result (`dispatch`, `dispatchAsync`, `data`, `error`, `status`, `isRunning`, `reset`, …). The hook manages the `AbortSignal` internally, so an in-flight call is aborted when a newer one is dispatched:
|
|
186
|
+
|
|
187
|
+
| Hook | Wraps |
|
|
188
|
+
| ------------------ | ------------------------------------------------------------------------- |
|
|
189
|
+
| `useConnect` | `client.wallet.connect` — `dispatch(wallet)` |
|
|
190
|
+
| `useDisconnect` | `client.wallet.disconnect` — `dispatch()` |
|
|
191
|
+
| `useSignIn` | `client.wallet.signIn` — `dispatch(wallet, input)` |
|
|
192
|
+
| `useSignMessage` | `client.wallet.signMessage` — `dispatch(message)` |
|
|
193
|
+
| `useSelectAccount` | `client.wallet.selectAccount` — returns the synchronous callback directly |
|
|
194
|
+
|
|
195
|
+
```tsx
|
|
196
|
+
import { useConnect, useConnectedWallet, useWallets, useWalletStatus } from '@solana/kit-plugin-wallet/react';
|
|
197
|
+
|
|
198
|
+
function WalletButton() {
|
|
199
|
+
const status = useWalletStatus();
|
|
200
|
+
const wallets = useWallets();
|
|
201
|
+
const connected = useConnectedWallet();
|
|
202
|
+
const { dispatch: connect, isRunning } = useConnect();
|
|
203
|
+
|
|
204
|
+
if (status === 'pending') return null; // avoid flashing UI before auto-reconnect resolves
|
|
205
|
+
|
|
206
|
+
if (connected) return <p>Connected: {connected.account.address}</p>;
|
|
207
|
+
|
|
208
|
+
return wallets.map(wallet => (
|
|
209
|
+
<button key={wallet.name} disabled={isRunning} onClick={() => connect(wallet)}>
|
|
210
|
+
Connect {wallet.name}
|
|
211
|
+
</button>
|
|
212
|
+
));
|
|
213
|
+
}
|
|
214
|
+
```
|
|
215
|
+
|
|
216
|
+
### Gating UI on the initial warm-up
|
|
217
|
+
|
|
218
|
+
A freshly built client runs a silent auto-reconnect on mount, briefly passing through `'pending'` and `'reconnecting'` before it settles. If you render wallet-dependent UI immediately, a persisted wallet flashes "disconnected" for a frame before it reconnects. `useIsWalletReady` reports `false` for the duration of that warm-up and `true` once it settles, so you can hold back the wallet-dependent parts while painting the rest of the app:
|
|
219
|
+
|
|
220
|
+
```tsx
|
|
221
|
+
import { useIsWalletReady, WalletReadyGate } from '@solana/kit-plugin-wallet/react';
|
|
222
|
+
|
|
223
|
+
// Hide a whole subtree until the connection settles:
|
|
224
|
+
function App() {
|
|
225
|
+
return (
|
|
226
|
+
<WalletReadyGate fallback={<Spinner />}>
|
|
227
|
+
<WalletDependentUI />
|
|
228
|
+
</WalletReadyGate>
|
|
229
|
+
);
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
// Or read the boolean directly for finer control (e.g. disabling a button):
|
|
233
|
+
function ConnectButton() {
|
|
234
|
+
const isReady = useIsWalletReady();
|
|
235
|
+
return <button disabled={!isReady}>Connect</button>;
|
|
236
|
+
}
|
|
237
|
+
```
|
|
238
|
+
|
|
239
|
+
`WalletReadyGate` is a thin wrapper over `useIsWalletReady`. Its props are shaped like React's `<Suspense fallback>`, but it is synchronous — it renders `fallback` directly off the reactive status rather than suspending, so it needs no `<Suspense>` ancestor. Both gate on the same warm-up set as `whenReady()` below, so they stay in sync — and like `whenReady()`, they ignore user-initiated `'connecting'` / `'disconnecting'`. To genuinely suspend a subtree, throw the Suspense-safe `whenReady()` promise from your own component instead.
|
|
240
|
+
|
|
241
|
+
### Advanced: seamless client swaps with `whenReady()`
|
|
242
|
+
|
|
243
|
+
> Most apps don't need this. It only matters if you **rebuild the client at runtime** — for example, to change chain — since each wallet plugin is bound to a single chain.
|
|
244
|
+
|
|
245
|
+
Every freshly built client runs a silent auto-reconnect that briefly passes through the `'pending'` and `'reconnecting'` statuses. If you publish the new client as soon as you build it, the UI may flash that reconnecting state on every switch. `client.wallet.whenReady()` returns a promise that resolves once the status has left `'pending'` / `'reconnecting'`, so you can build the new client, wait for it to settle, and only then swap it in — keeping the previous client live until the new one is ready:
|
|
246
|
+
|
|
247
|
+
```ts
|
|
248
|
+
import { createClient } from '@solana/kit';
|
|
249
|
+
import { walletSigner } from '@solana/kit-plugin-wallet';
|
|
250
|
+
|
|
251
|
+
// On a chain switch, hold the previous client until the rebuilt one is ready:
|
|
252
|
+
const next = createClient().use(walletSigner({ chain }));
|
|
253
|
+
await next.wallet.whenReady();
|
|
254
|
+
swapInClient(next); // publish `next`, then dispose the client it replaced
|
|
255
|
+
```
|
|
256
|
+
|
|
257
|
+
`whenReady()` deliberately does **not** wait for user-initiated `connect` / `disconnect` / `signIn` (those pass through `'connecting'` / `'disconnecting'`), so it only ever blocks on the initial warm-up — a normal connect stays interactive.
|
|
258
|
+
|
|
259
|
+
Disposing a client mid-warm-up settles its status to `'disconnected'` and resolves any pending `whenReady()` promise — a superseded client never leaves an `await` hanging. Check `getState()` after awaiting if you need to tell a ready connection from a disposed (or never-connected) client.
|
|
260
|
+
|
|
261
|
+
## Framework integration
|
|
262
|
+
|
|
263
|
+
The plugin exposes `subscribe` and `getState` for binding wallet state to any UI framework. To gate UI on the initial auto-reconnect warm-up without React, pass `getState().status` through `isWalletWarmingUp` — the same predicate that backs `whenReady()` and the [React readiness helpers](#gating-ui-on-the-initial-warm-up), so all three stay in sync:
|
|
264
|
+
|
|
265
|
+
```ts
|
|
266
|
+
import { isWalletWarmingUp } from '@solana/kit-plugin-wallet';
|
|
267
|
+
|
|
268
|
+
client.wallet.subscribe(() => {
|
|
269
|
+
const { status } = client.wallet.getState();
|
|
270
|
+
setLoading(isWalletWarmingUp(status)); // true while 'pending' / 'reconnecting'
|
|
271
|
+
});
|
|
272
|
+
```
|
|
273
|
+
|
|
274
|
+
**React** — prefer the ready-made [React hooks](#react-hooks) above; they wrap the pattern below. To bind state manually (e.g. to read the whole snapshot at once), use `useSyncExternalStore` for concurrent-mode-safe rendering:
|
|
275
|
+
|
|
276
|
+
```tsx
|
|
277
|
+
import { useSyncExternalStore } from 'react';
|
|
278
|
+
|
|
279
|
+
function useWalletState() {
|
|
280
|
+
return useSyncExternalStore(client.wallet.subscribe, client.wallet.getState);
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
function App() {
|
|
284
|
+
const { wallets, connected, status } = useWalletState();
|
|
285
|
+
|
|
286
|
+
if (status === 'pending') return null; // avoid flashing a connect button before auto-reconnect
|
|
287
|
+
|
|
288
|
+
if (!connected) {
|
|
289
|
+
return wallets.map(w => (
|
|
290
|
+
<button key={w.name} onClick={() => client.wallet.connect(w)}>
|
|
291
|
+
{w.name}
|
|
292
|
+
</button>
|
|
293
|
+
));
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
return <p>Connected: {connected.account.address}</p>;
|
|
297
|
+
}
|
|
298
|
+
```
|
|
299
|
+
|
|
300
|
+
**Vue** — use a `shallowRef` composable:
|
|
301
|
+
|
|
302
|
+
```ts
|
|
303
|
+
import { onMounted, onUnmounted, shallowRef } from 'vue';
|
|
304
|
+
|
|
305
|
+
function useWalletState() {
|
|
306
|
+
const state = shallowRef(client.wallet.getState());
|
|
307
|
+
onMounted(() => {
|
|
308
|
+
const unsub = client.wallet.subscribe(() => {
|
|
309
|
+
state.value = client.wallet.getState();
|
|
310
|
+
});
|
|
311
|
+
onUnmounted(unsub);
|
|
312
|
+
});
|
|
313
|
+
return state;
|
|
314
|
+
}
|
|
315
|
+
```
|
|
316
|
+
|
|
317
|
+
**Svelte** — wrap in a `readable` store:
|
|
318
|
+
|
|
319
|
+
```ts
|
|
320
|
+
import { readable } from 'svelte/store';
|
|
321
|
+
|
|
322
|
+
export const walletState = readable(client.wallet.getState(), set => {
|
|
323
|
+
return client.wallet.subscribe(() => set(client.wallet.getState()));
|
|
324
|
+
});
|
|
325
|
+
```
|
|
326
|
+
|
|
327
|
+
### Reactivity: observing `payer` / `identity`
|
|
328
|
+
|
|
329
|
+
Because the connected wallet's signer is dynamic, the plugins follow Kit's
|
|
330
|
+
reactive-capability convention. The variants that set `client.payer` /
|
|
331
|
+
`client.identity` also install a sibling `subscribeToPayer` /
|
|
332
|
+
`subscribeToIdentity` function, so reactive consumers can observe signer
|
|
333
|
+
changes without naming the wallet plugin:
|
|
334
|
+
|
|
335
|
+
```ts
|
|
336
|
+
const client = createClient().use(walletSigner({ chain: 'solana:mainnet' }));
|
|
337
|
+
|
|
338
|
+
const unsubscribe = client.subscribeToPayer(() => {
|
|
339
|
+
// Re-read the current value; it may now throw if disconnected.
|
|
340
|
+
console.log('payer is now', client.payer);
|
|
341
|
+
});
|
|
342
|
+
```
|
|
343
|
+
|
|
344
|
+
`walletSigner` installs both hooks, `walletPayer` installs `subscribeToPayer`,
|
|
345
|
+
`walletIdentity` installs `subscribeToIdentity`, and `walletWithoutSigner`
|
|
346
|
+
installs neither. The listener fires on connect, account switch, and
|
|
347
|
+
disconnect. For the full
|
|
348
|
+
wallet state (including the discovered-wallet list), use
|
|
349
|
+
`client.wallet.subscribe` instead.
|
|
350
|
+
|
|
351
|
+
## Configuration
|
|
352
|
+
|
|
353
|
+
```ts
|
|
354
|
+
walletSigner({
|
|
355
|
+
chain: 'solana:mainnet', // required
|
|
356
|
+
storage: sessionStorage, // default: localStorage (null to disable)
|
|
357
|
+
storageKey: 'my-app:wallet', // default: 'kit-wallet'
|
|
358
|
+
autoConnect: false, // default: true (disable silent reconnect)
|
|
359
|
+
filter: w => w.features.includes('solana:signAndSendTransaction'), // optional
|
|
360
|
+
});
|
|
361
|
+
```
|
|
362
|
+
|
|
363
|
+
## Persistence
|
|
364
|
+
|
|
365
|
+
By default the plugin uses `localStorage` to remember the last connected wallet and auto-reconnects on the next page load. Pass `storage: null` to disable, or provide a custom adapter (e.g. `sessionStorage` or an IndexedDB wrapper).
|
|
366
|
+
|
|
367
|
+
## SSR / server-side rendering
|
|
368
|
+
|
|
369
|
+
All four wallet plugins are safe to include in a shared client that runs on both server and browser. On the server, `status` stays `'pending'` permanently, all actions throw, and no registry listeners or storage reads are made. In the browser the plugin initializes normally.
|
|
370
|
+
|
|
371
|
+
React Native is treated the same way as the server: wallet-standard browser discovery is not available, so the plugin returns the same inert stub (`status` stays `'pending'`, actions throw). Native wallet integration would need a different discovery mechanism and is out of scope for this plugin.
|
|
372
|
+
|
|
373
|
+
```ts
|
|
374
|
+
const client = createClient()
|
|
375
|
+
.use(solanaRpc({ rpcUrl: 'https://api.mainnet-beta.solana.com' }))
|
|
376
|
+
.use(walletSigner({ chain: 'solana:mainnet' }))
|
|
377
|
+
.use(planAndSendTransactions());
|
|
378
|
+
|
|
379
|
+
// Server: status === 'pending', client.payer throws
|
|
380
|
+
// Browser: auto-connects, client.payer becomes the wallet signer
|
|
381
|
+
```
|
|
382
|
+
|
|
383
|
+
## Cleanup
|
|
384
|
+
|
|
385
|
+
The plugin implements `[Symbol.dispose]`, so it integrates with the `using` declaration or explicit disposal:
|
|
386
|
+
|
|
387
|
+
```ts
|
|
388
|
+
{
|
|
389
|
+
using client = createClient().use(walletSigner({ chain: 'solana:mainnet' }));
|
|
390
|
+
// registry listeners and storage subscriptions are cleaned up on scope exit
|
|
391
|
+
}
|
|
392
|
+
```
|
|
393
|
+
|
|
394
|
+
Or call `[Symbol.dispose]()` explicitly when you're done with the client:
|
|
395
|
+
|
|
396
|
+
```ts
|
|
397
|
+
const client = createClient().use(walletSigner({ chain: 'solana:mainnet' }));
|
|
398
|
+
|
|
399
|
+
// ... later, when the client is no longer needed
|
|
400
|
+
client[Symbol.dispose]();
|
|
401
|
+
```
|