@liberfi.io/wallet-connector 0.1.185 → 0.2.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 +38 -15
- package/dist/index.d.mts +42 -25
- package/dist/index.d.ts +42 -25
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +1 -1
- package/dist/index.mjs.map +1 -1
- package/package.json +4 -4
package/README.md
CHANGED
|
@@ -166,29 +166,37 @@ Returns the full `AuthContextValue`. Throws if used outside `AuthProvider`.
|
|
|
166
166
|
function useAuth(): AuthContextValue;
|
|
167
167
|
```
|
|
168
168
|
|
|
169
|
-
#### `useAuthCallback<
|
|
169
|
+
#### `useAuthCallback<A>(callback, deps?)`
|
|
170
170
|
|
|
171
|
-
Wraps a callback so it only executes when the user is authenticated.
|
|
171
|
+
Wraps a callback so it only executes when the user is authenticated. The contract is **fire-and-forget**: the input callback's return value is always discarded — to consume a callback's result, capture it inside the callback itself (e.g. `setState`, toast, analytics).
|
|
172
|
+
|
|
173
|
+
Behavior by auth status:
|
|
174
|
+
|
|
175
|
+
- `authenticated` — executes the callback. The callback's return value is discarded; rejections **do** propagate to the returned Promise so callers may `await` and `try/catch` to handle the callback's own failures.
|
|
176
|
+
- `unauthenticated` — triggers `signIn()` fire-and-forget and resolves immediately. Errors from `signIn()` are silenced here — observe them via the auth provider's own error channel (e.g. `PrivyAuthProvider`'s `onError`).
|
|
177
|
+
- `authenticating` / `deauthenticating` — no-op; resolves immediately without re-triggering sign-in.
|
|
172
178
|
|
|
173
179
|
```typescript
|
|
174
|
-
type AuthGuardedCallback<
|
|
175
|
-
...args:
|
|
176
|
-
) => Promise<
|
|
180
|
+
type AuthGuardedCallback<A extends readonly unknown[]> = (
|
|
181
|
+
...args: A
|
|
182
|
+
) => Promise<void>;
|
|
177
183
|
|
|
178
|
-
function useAuthCallback<
|
|
179
|
-
callback:
|
|
184
|
+
function useAuthCallback<A extends unknown[]>(
|
|
185
|
+
callback: (...args: A) => void | Promise<void>,
|
|
180
186
|
deps?: DependencyList,
|
|
181
|
-
): AuthGuardedCallback<
|
|
187
|
+
): AuthGuardedCallback<A>;
|
|
182
188
|
```
|
|
183
189
|
|
|
184
|
-
The return type `
|
|
190
|
+
The input callback's return type is constrained to `void | Promise<void>` so the API surface is unambiguously fire-and-forget. A callback returning `Promise<value>` will be rejected at compile time, forcing the consumer to drop the value or refactor.
|
|
191
|
+
|
|
192
|
+
#### `useSwitchEvmWalletsToChain()`
|
|
185
193
|
|
|
186
|
-
|
|
194
|
+
Returns a function that switches **all connected EVM wallets** to a given chain. Non-EVM chains (e.g. Solana) are a no-op since Solana has no chain-switching concept within its namespace. Fail-fast on the first wallet's rejection (`Promise.all`); previously-switched wallets are NOT rolled back.
|
|
187
195
|
|
|
188
|
-
|
|
196
|
+
> **Per-wallet usage**: do NOT use this hook to switch a single wallet. Call `wallet.switchChain(chain)` directly on the [`EvmWalletAdapter`](#evmwalletadapter). This hook is the "sync the whole wallet set to a UI-selected chain" primitive.
|
|
189
197
|
|
|
190
198
|
```typescript
|
|
191
|
-
function
|
|
199
|
+
function useSwitchEvmWalletsToChain(): (chain: Chain) => Promise<void>;
|
|
192
200
|
```
|
|
193
201
|
|
|
194
202
|
### Constants
|
|
@@ -269,14 +277,14 @@ function TradeButton() {
|
|
|
269
277
|
}
|
|
270
278
|
```
|
|
271
279
|
|
|
272
|
-
### Switching Chains
|
|
280
|
+
### Switching Chains (UI selector — sync all EVM wallets)
|
|
273
281
|
|
|
274
282
|
```tsx
|
|
275
283
|
import { Chain } from "@liberfi.io/types";
|
|
276
|
-
import {
|
|
284
|
+
import { useSwitchEvmWalletsToChain } from "@liberfi.io/wallet-connector";
|
|
277
285
|
|
|
278
286
|
function ChainSwitcher() {
|
|
279
|
-
const switchChain =
|
|
287
|
+
const switchChain = useSwitchEvmWalletsToChain();
|
|
280
288
|
|
|
281
289
|
return (
|
|
282
290
|
<button onClick={() => switchChain(Chain.ETHEREUM)}>
|
|
@@ -286,6 +294,21 @@ function ChainSwitcher() {
|
|
|
286
294
|
}
|
|
287
295
|
```
|
|
288
296
|
|
|
297
|
+
For switching a **single** wallet (e.g. temporarily for a transaction), call `wallet.switchChain(chain)` directly:
|
|
298
|
+
|
|
299
|
+
```tsx
|
|
300
|
+
import { Chain } from "@liberfi.io/types";
|
|
301
|
+
import {
|
|
302
|
+
useConnectedWallet,
|
|
303
|
+
type EvmWalletAdapter,
|
|
304
|
+
} from "@liberfi.io/wallet-connector";
|
|
305
|
+
|
|
306
|
+
async function trade(evmWallet: EvmWalletAdapter) {
|
|
307
|
+
await evmWallet.switchChain(Chain.POLYGON);
|
|
308
|
+
// ... do the trade ...
|
|
309
|
+
}
|
|
310
|
+
```
|
|
311
|
+
|
|
289
312
|
### Building a Custom Implementation
|
|
290
313
|
|
|
291
314
|
To create a new wallet connector implementation, implement `WalletAdapter` (and optionally `EvmWalletAdapter`) and wrap the providers:
|
package/dist/index.d.mts
CHANGED
|
@@ -105,24 +105,40 @@ declare function useAuth(): AuthContextValue;
|
|
|
105
105
|
/**
|
|
106
106
|
* The return type of {@link useAuthCallback}.
|
|
107
107
|
*
|
|
108
|
-
*
|
|
109
|
-
*
|
|
110
|
-
*
|
|
108
|
+
* Always resolves to `void` — the wrapper is fire-and-forget by design.
|
|
109
|
+
* Any value returned by the input callback is discarded. To consume a
|
|
110
|
+
* callback's result, capture it inside the callback itself (e.g. setState,
|
|
111
|
+
* toast). For sign-in errors, observe via the auth provider's own channel
|
|
112
|
+
* (e.g. `PrivyAuthProvider`'s `onError`).
|
|
113
|
+
*
|
|
114
|
+
* Rejections from the input callback DO propagate to the returned Promise,
|
|
115
|
+
* so callers may `await` and `try/catch` to handle the callback's own
|
|
116
|
+
* failures. Rejections from `signIn()` are silenced — they are observable
|
|
117
|
+
* elsewhere.
|
|
111
118
|
*/
|
|
112
|
-
type AuthGuardedCallback<
|
|
119
|
+
type AuthGuardedCallback<A extends readonly unknown[]> = (...args: A) => Promise<void>;
|
|
113
120
|
/**
|
|
114
121
|
* Wraps a callback so it only executes when the user is authenticated.
|
|
115
122
|
*
|
|
116
|
-
*
|
|
117
|
-
* -
|
|
118
|
-
*
|
|
119
|
-
*
|
|
123
|
+
* Behavior by auth status:
|
|
124
|
+
* - `authenticated`: executes the callback; the callback's return value is
|
|
125
|
+
* discarded, but any rejection propagates to the returned Promise.
|
|
126
|
+
* - `unauthenticated`: triggers `signIn()` fire-and-forget and resolves
|
|
127
|
+
* immediately. Errors from `signIn()` are silenced here — observe them
|
|
128
|
+
* via the auth provider's own error channel.
|
|
129
|
+
* - `authenticating` / `deauthenticating`: no-op; resolves immediately
|
|
130
|
+
* without re-triggering sign-in.
|
|
131
|
+
*
|
|
132
|
+
* The input callback's return type is constrained to `void | Promise<void>`
|
|
133
|
+
* so the API surface is unambiguously fire-and-forget. To consume a
|
|
134
|
+
* callback's result, capture it inside the callback itself rather than
|
|
135
|
+
* via the wrapper's return value.
|
|
120
136
|
*
|
|
121
137
|
* @param callback - The function to guard behind authentication.
|
|
122
138
|
* @param deps - Optional dependency list (same semantics as `useCallback`).
|
|
123
|
-
* @returns A wrapped callback
|
|
139
|
+
* @returns A wrapped callback returning `Promise<void>`.
|
|
124
140
|
*/
|
|
125
|
-
declare function useAuthCallback<
|
|
141
|
+
declare function useAuthCallback<A extends unknown[]>(callback: (...args: A) => void | Promise<void>, deps?: DependencyList): AuthGuardedCallback<A>;
|
|
126
142
|
|
|
127
143
|
/**
|
|
128
144
|
* Returns the first connected wallet matching the given chain's namespace,
|
|
@@ -137,15 +153,25 @@ declare function useAuthCallback<T extends (...args: any[]) => any>(callback: T,
|
|
|
137
153
|
declare function useConnectedWallet(chain: Chain): WalletAdapter | undefined;
|
|
138
154
|
|
|
139
155
|
/**
|
|
140
|
-
* Returns a function that switches all connected EVM wallets to the given chain.
|
|
156
|
+
* Returns a function that switches **all connected EVM wallets** to the given chain.
|
|
157
|
+
*
|
|
158
|
+
* Semantics:
|
|
159
|
+
*
|
|
160
|
+
* - If `chain` is in the EVM namespace, every connected wallet whose
|
|
161
|
+
* `chainNamespace === ChainNamespace.EVM` is switched concurrently.
|
|
162
|
+
* - If `chain` is NOT in the EVM namespace (e.g. Solana), the call is a no-op
|
|
163
|
+
* (Solana has no chain-switching concept within its namespace).
|
|
164
|
+
* - Fail-fast on first wallet rejection (`Promise.all`); previously-switched
|
|
165
|
+
* wallets are NOT rolled back. Subscribe to per-wallet status separately
|
|
166
|
+
* if rollback is needed.
|
|
141
167
|
*
|
|
142
|
-
*
|
|
143
|
-
*
|
|
144
|
-
*
|
|
168
|
+
* **Do NOT use this hook to switch a single wallet** — call
|
|
169
|
+
* `wallet.switchChain(chain)` directly on the {@link EvmWalletAdapter}. This
|
|
170
|
+
* hook is the "sync the whole wallet set to a UI-selected chain" primitive.
|
|
145
171
|
*
|
|
146
172
|
* @returns An async function `(chain: Chain) => Promise<void>`.
|
|
147
173
|
*/
|
|
148
|
-
declare function
|
|
174
|
+
declare function useSwitchEvmWalletsToChain(): (chain: Chain) => Promise<void>;
|
|
149
175
|
|
|
150
176
|
interface WalletConnectorContextValue {
|
|
151
177
|
/**
|
|
@@ -204,13 +230,4 @@ type WalletConnectorProviderProps = PropsWithChildren<WalletConnectorContextValu
|
|
|
204
230
|
*/
|
|
205
231
|
declare function WalletConnectorProvider({ children, ...value }: WalletConnectorProviderProps): react_jsx_runtime.JSX.Element;
|
|
206
232
|
|
|
207
|
-
|
|
208
|
-
interface Window {
|
|
209
|
-
__LIBERFI_VERSION__?: {
|
|
210
|
-
[key: string]: string;
|
|
211
|
-
};
|
|
212
|
-
}
|
|
213
|
-
}
|
|
214
|
-
declare const _default: "0.1.185";
|
|
215
|
-
|
|
216
|
-
export { type AuthGuardedCallback, AuthProvider, type AuthProviderProps, type AuthStatus, type AuthenticatedUser, type Eip1193Provider, type EvmWalletAdapter, type WalletAdapter, WalletConnectorProvider, type WalletConnectorProviderProps, useAuth, useAuthCallback, useConnectedWallet, useSwitchChain, useWalletConnector, useWallets, _default as version };
|
|
233
|
+
export { type AuthGuardedCallback, AuthProvider, type AuthProviderProps, type AuthStatus, type AuthenticatedUser, type Eip1193Provider, type EvmWalletAdapter, type WalletAdapter, WalletConnectorProvider, type WalletConnectorProviderProps, useAuth, useAuthCallback, useConnectedWallet, useSwitchEvmWalletsToChain, useWalletConnector, useWallets };
|
package/dist/index.d.ts
CHANGED
|
@@ -105,24 +105,40 @@ declare function useAuth(): AuthContextValue;
|
|
|
105
105
|
/**
|
|
106
106
|
* The return type of {@link useAuthCallback}.
|
|
107
107
|
*
|
|
108
|
-
*
|
|
109
|
-
*
|
|
110
|
-
*
|
|
108
|
+
* Always resolves to `void` — the wrapper is fire-and-forget by design.
|
|
109
|
+
* Any value returned by the input callback is discarded. To consume a
|
|
110
|
+
* callback's result, capture it inside the callback itself (e.g. setState,
|
|
111
|
+
* toast). For sign-in errors, observe via the auth provider's own channel
|
|
112
|
+
* (e.g. `PrivyAuthProvider`'s `onError`).
|
|
113
|
+
*
|
|
114
|
+
* Rejections from the input callback DO propagate to the returned Promise,
|
|
115
|
+
* so callers may `await` and `try/catch` to handle the callback's own
|
|
116
|
+
* failures. Rejections from `signIn()` are silenced — they are observable
|
|
117
|
+
* elsewhere.
|
|
111
118
|
*/
|
|
112
|
-
type AuthGuardedCallback<
|
|
119
|
+
type AuthGuardedCallback<A extends readonly unknown[]> = (...args: A) => Promise<void>;
|
|
113
120
|
/**
|
|
114
121
|
* Wraps a callback so it only executes when the user is authenticated.
|
|
115
122
|
*
|
|
116
|
-
*
|
|
117
|
-
* -
|
|
118
|
-
*
|
|
119
|
-
*
|
|
123
|
+
* Behavior by auth status:
|
|
124
|
+
* - `authenticated`: executes the callback; the callback's return value is
|
|
125
|
+
* discarded, but any rejection propagates to the returned Promise.
|
|
126
|
+
* - `unauthenticated`: triggers `signIn()` fire-and-forget and resolves
|
|
127
|
+
* immediately. Errors from `signIn()` are silenced here — observe them
|
|
128
|
+
* via the auth provider's own error channel.
|
|
129
|
+
* - `authenticating` / `deauthenticating`: no-op; resolves immediately
|
|
130
|
+
* without re-triggering sign-in.
|
|
131
|
+
*
|
|
132
|
+
* The input callback's return type is constrained to `void | Promise<void>`
|
|
133
|
+
* so the API surface is unambiguously fire-and-forget. To consume a
|
|
134
|
+
* callback's result, capture it inside the callback itself rather than
|
|
135
|
+
* via the wrapper's return value.
|
|
120
136
|
*
|
|
121
137
|
* @param callback - The function to guard behind authentication.
|
|
122
138
|
* @param deps - Optional dependency list (same semantics as `useCallback`).
|
|
123
|
-
* @returns A wrapped callback
|
|
139
|
+
* @returns A wrapped callback returning `Promise<void>`.
|
|
124
140
|
*/
|
|
125
|
-
declare function useAuthCallback<
|
|
141
|
+
declare function useAuthCallback<A extends unknown[]>(callback: (...args: A) => void | Promise<void>, deps?: DependencyList): AuthGuardedCallback<A>;
|
|
126
142
|
|
|
127
143
|
/**
|
|
128
144
|
* Returns the first connected wallet matching the given chain's namespace,
|
|
@@ -137,15 +153,25 @@ declare function useAuthCallback<T extends (...args: any[]) => any>(callback: T,
|
|
|
137
153
|
declare function useConnectedWallet(chain: Chain): WalletAdapter | undefined;
|
|
138
154
|
|
|
139
155
|
/**
|
|
140
|
-
* Returns a function that switches all connected EVM wallets to the given chain.
|
|
156
|
+
* Returns a function that switches **all connected EVM wallets** to the given chain.
|
|
157
|
+
*
|
|
158
|
+
* Semantics:
|
|
159
|
+
*
|
|
160
|
+
* - If `chain` is in the EVM namespace, every connected wallet whose
|
|
161
|
+
* `chainNamespace === ChainNamespace.EVM` is switched concurrently.
|
|
162
|
+
* - If `chain` is NOT in the EVM namespace (e.g. Solana), the call is a no-op
|
|
163
|
+
* (Solana has no chain-switching concept within its namespace).
|
|
164
|
+
* - Fail-fast on first wallet rejection (`Promise.all`); previously-switched
|
|
165
|
+
* wallets are NOT rolled back. Subscribe to per-wallet status separately
|
|
166
|
+
* if rollback is needed.
|
|
141
167
|
*
|
|
142
|
-
*
|
|
143
|
-
*
|
|
144
|
-
*
|
|
168
|
+
* **Do NOT use this hook to switch a single wallet** — call
|
|
169
|
+
* `wallet.switchChain(chain)` directly on the {@link EvmWalletAdapter}. This
|
|
170
|
+
* hook is the "sync the whole wallet set to a UI-selected chain" primitive.
|
|
145
171
|
*
|
|
146
172
|
* @returns An async function `(chain: Chain) => Promise<void>`.
|
|
147
173
|
*/
|
|
148
|
-
declare function
|
|
174
|
+
declare function useSwitchEvmWalletsToChain(): (chain: Chain) => Promise<void>;
|
|
149
175
|
|
|
150
176
|
interface WalletConnectorContextValue {
|
|
151
177
|
/**
|
|
@@ -204,13 +230,4 @@ type WalletConnectorProviderProps = PropsWithChildren<WalletConnectorContextValu
|
|
|
204
230
|
*/
|
|
205
231
|
declare function WalletConnectorProvider({ children, ...value }: WalletConnectorProviderProps): react_jsx_runtime.JSX.Element;
|
|
206
232
|
|
|
207
|
-
|
|
208
|
-
interface Window {
|
|
209
|
-
__LIBERFI_VERSION__?: {
|
|
210
|
-
[key: string]: string;
|
|
211
|
-
};
|
|
212
|
-
}
|
|
213
|
-
}
|
|
214
|
-
declare const _default: "0.1.185";
|
|
215
|
-
|
|
216
|
-
export { type AuthGuardedCallback, AuthProvider, type AuthProviderProps, type AuthStatus, type AuthenticatedUser, type Eip1193Provider, type EvmWalletAdapter, type WalletAdapter, WalletConnectorProvider, type WalletConnectorProviderProps, useAuth, useAuthCallback, useConnectedWallet, useSwitchChain, useWalletConnector, useWallets, _default as version };
|
|
233
|
+
export { type AuthGuardedCallback, AuthProvider, type AuthProviderProps, type AuthStatus, type AuthenticatedUser, type Eip1193Provider, type EvmWalletAdapter, type WalletAdapter, WalletConnectorProvider, type WalletConnectorProviderProps, useAuth, useAuthCallback, useConnectedWallet, useSwitchEvmWalletsToChain, useWalletConnector, useWallets };
|
package/dist/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
'use strict';var react=require('react'),utils=require('@liberfi.io/utils'),types=require('@liberfi.io/types'),jsxRuntime=require('react/jsx-runtime');var n=react.createContext(
|
|
1
|
+
'use strict';var react=require('react'),utils=require('@liberfi.io/utils'),types=require('@liberfi.io/types'),jsxRuntime=require('react/jsx-runtime');var n=react.createContext(null);function u(){let t=react.useContext(n);if(t===null)throw new Error("useAuth must be used within an AuthProvider");return t}function M(t,e){let{status:r,signIn:o}=u(),l=react.useRef(r);return l.current=r,react.useCallback(async(...c)=>{if(l.current!=="authenticated"){l.current==="unauthenticated"&&Promise.resolve().then(()=>o()).catch(()=>{});return}await t(...c);},[...e||[],o,t])}var i=react.createContext(null);function s(){let t=react.useContext(i);if(t===null)throw new Error("useWalletConnector must be used within a WalletConnectorProvider");return t}function a(){let{wallets:t}=s();return t}function H(t){let e=a(),r=utils.chainToNamespace(t);return react.useMemo(()=>e.find(o=>o.chainNamespace===r&&o.isConnected),[e,r])}function _(){let t=a();return react.useCallback(async e=>{if(utils.chainToNamespace(e)!==types.ChainNamespace.EVM)return;let r=t.filter(o=>o.chainNamespace===types.ChainNamespace.EVM&&o.isConnected);await Promise.all(r.map(o=>o.switchChain(e)));},[t])}function lt({children:t,...e}){return jsxRuntime.jsx(n.Provider,{value:e,children:t})}function mt({children:t,...e}){return jsxRuntime.jsx(i.Provider,{value:e,children:t})}exports.AuthProvider=lt;exports.WalletConnectorProvider=mt;exports.useAuth=u;exports.useAuthCallback=M;exports.useConnectedWallet=H;exports.useSwitchEvmWalletsToChain=_;exports.useWalletConnector=s;exports.useWallets=a;//# sourceMappingURL=index.js.map
|
|
2
2
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/providers/AuthContext.ts","../src/hooks/useAuth.ts","../src/hooks/useAuthCallback.ts","../src/providers/WalletConnectorContext.ts","../src/hooks/useWalletConnector.ts","../src/hooks/useWallets.ts","../src/hooks/useConnectedWallet.ts","../src/hooks/useSwitchChain.ts","../src/providers/AuthProvider.tsx","../src/providers/WalletConnectorProvider.tsx","../src/version.ts"],"names":["AuthContext","createContext","useAuth","context","useContext","useAuthCallback","callback","deps","status","signIn","statusRef","useRef","useCallback","args","WalletConnectorContext","useWalletConnector","useWallets","wallets","useConnectedWallet","chain","ns","chainToNamespace","useMemo","w","useSwitchChain","ChainNamespace","evmWallets","AuthProvider","children","value","jsx","WalletConnectorProvider","version_default"],"mappings":"sJAsBO,IAAMA,CAAAA,CAAcC,mBAAAA,CACzB,EACF,CAAA,CCbO,SAASC,CAAAA,EAAU,CACxB,IAAMC,CAAAA,CAAUC,gBAAAA,CAAWJ,CAAW,CAAA,CACtC,GAAI,CAACG,CAAAA,CACH,MAAM,IAAI,KAAA,CAAM,4CAA4C,CAAA,CAE9D,OAAOA,CACT,CCUO,SAASE,CAAAA,CACdC,CAAAA,CACAC,EACwB,CACxB,GAAM,CAAE,MAAA,CAAAC,EAAQ,MAAA,CAAAC,CAAO,CAAA,CAAIP,CAAAA,GAErBQ,CAAAA,CAAYC,YAAAA,CAAeH,CAAM,CAAA,CACvC,OAAAE,CAAAA,CAAU,OAAA,CAAUF,CAAAA,CAEbI,kBACL,MAAA,GACKC,CAAAA,GAC6C,CAChD,GAAIH,EAAU,OAAA,GAAY,eAAA,CAAiB,CACrCA,CAAAA,CAAU,UAAY,iBAAA,EACxBD,CAAAA,EAAO,CAET,MACF,CACA,OAAOH,CAAAA,CAAS,GAAGO,CAAI,CACzB,CAAA,CACA,CAAC,GAAIN,CAAAA,EAAQ,EAAC,CAAIE,CAAAA,CAAQH,CAAQ,CACpC,CACF,CCzBO,IAAMQ,CAAAA,CACXb,mBAAAA,CAA2C,EAAiC,CAAA,CCfvE,SAASc,CAAAA,EAAqB,CACnC,IAAMZ,CAAAA,CAAUC,gBAAAA,CAAWU,CAAsB,EACjD,GAAI,CAACX,CAAAA,CACH,MAAM,IAAI,KAAA,CACR,kEACF,CAAA,CAEF,OAAOA,CACT,CCVO,SAASa,CAAAA,EAAa,CAC3B,GAAM,CAAE,OAAA,CAAAC,CAAQ,EAAIF,CAAAA,EAAmB,C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mBAAA,EAAuB,EAAC,CAC5D,OAAO,mBAAA,CAAoB,8BAA8B,CAAA,CAAI,SAAA,CAAA,KAGxDI,CAAAA,CAAQ","file":"index.js","sourcesContent":["import { createContext } from \"react\";\nimport { AuthenticatedUser } from \"../types\";\n\nexport type AuthStatus =\n | \"unauthenticated\"\n | \"authenticating\"\n | \"authenticated\"\n | \"deauthenticating\";\n\nexport interface AuthContextValue {\n /** authenticated user profile */\n user: AuthenticatedUser | null;\n /** authentication status */\n status: AuthStatus;\n /** sign in to the IdP */\n signIn: () => void | Promise<void>;\n /** sign out from the IdP */\n signOut: () => void | Promise<void>;\n /** refresh the access token */\n refreshAccessToken: () => void | Promise<void>;\n}\n\nexport const AuthContext = createContext<AuthContextValue>(\n {} as AuthContextValue,\n);\n","import { useContext } from \"react\";\nimport { AuthContext } from \"../providers/AuthContext\";\n\n/**\n * Returns the auth context value.\n *\n * Must be used within an {@link AuthProvider}.\n * Throws if no provider is found in the component tree.\n *\n * @returns The current {@link AuthContextValue} (user, status, signIn, signOut, refreshAccessToken).\n */\nexport function useAuth() {\n const context = useContext(AuthContext);\n if (!context) {\n throw new Error(\"useAuth must be used within a AuthProvider\");\n }\n return context;\n}\n","/* eslint-disable @typescript-eslint/no-explicit-any */\nimport { DependencyList, useCallback, useRef } from \"react\";\nimport { useAuth } from \"./useAuth\";\n\n/**\n * The return type of {@link useAuthCallback}.\n *\n * Accepts the same parameters as `T` but always returns a `Promise` that\n * resolves to `Awaited<ReturnType<T>>` when authenticated, or `undefined`\n * when the user is not yet authenticated.\n */\nexport type AuthGuardedCallback<T extends (...args: any[]) => any> = (\n ...args: Parameters<T>\n) => Promise<Awaited<ReturnType<T>> | undefined>;\n\n/**\n * Wraps a callback so it only executes when the user is authenticated.\n *\n * - **authenticated**: executes the callback and returns its result.\n * - **unauthenticated**: triggers `signIn()` and returns `undefined`.\n * - **authenticating**: returns `undefined` without re-triggering sign-in\n * (avoids conflicting with an in-progress auth flow).\n *\n * @param callback - The function to guard behind authentication.\n * @param deps - Optional dependency list (same semantics as `useCallback`).\n * @returns A wrapped callback that may resolve to `undefined` when not authenticated.\n */\nexport function useAuthCallback<T extends (...args: any[]) => any>(\n callback: T,\n deps?: DependencyList,\n): AuthGuardedCallback<T> {\n const { status, signIn } = useAuth();\n\n const statusRef = useRef<string>(status);\n statusRef.current = status;\n\n return useCallback(\n async (\n ...args: Parameters<T>\n ): Promise<Awaited<ReturnType<T>> | undefined> => {\n if (statusRef.current !== \"authenticated\") {\n if (statusRef.current === \"unauthenticated\") {\n signIn();\n }\n return;\n }\n return callback(...args);\n },\n [...(deps || []), signIn, callback],\n );\n}\n","import { createContext } from \"react\";\nimport { WalletAdapter } from \"../types\";\n\nexport interface WalletConnectorContextValue {\n /**\n * detecting: is detecting connected wallets\n * connecting: is connecting to the first wallet\n * connected: is connected to at least one wallet\n * disconnecting: is disconnecting from the last connected wallet\n * disconnected: is disconnected from all wallets\n */\n status:\n | \"detecting\"\n | \"connecting\"\n | \"connected\"\n | \"disconnecting\"\n | \"disconnected\";\n // all wallets that can be used to take onchain actions when connected\n wallets: Array<WalletAdapter>;\n // if no wallets are connected, connect one or multiple wallets\n connect: () => Promise<void>;\n // disconnect all wallets\n disconnect: () => Promise<void>;\n}\n\nexport const WalletConnectorContext =\n createContext<WalletConnectorContextValue>({} as WalletConnectorContextValue);\n","import { useContext } from \"react\";\nimport { WalletConnectorContext } from \"../providers/WalletConnectorContext\";\n\n/**\n * Returns the wallet connector context value.\n *\n * Must be used within a {@link WalletConnectorProvider}.\n * Throws if no provider is found in the component tree.\n *\n * @returns The current {@link WalletConnectorContextValue} (status, wallets, connect, disconnect).\n */\nexport function useWalletConnector() {\n const context = useContext(WalletConnectorContext);\n if (!context) {\n throw new Error(\n \"useWalletConnector must be used within a WalletConnectorProvider\",\n );\n }\n return context;\n}\n","import { useWalletConnector } from \"./useWalletConnector\";\n\n/**\n * Convenience hook that returns the `wallets` array from the wallet connector context.\n *\n * Shorthand for `useWalletConnector().wallets`.\n *\n * @returns All wallets that can be used to take on-chain actions when connected.\n */\nexport function useWallets() {\n const { wallets } = useWalletConnector();\n return wallets;\n}\n","import { useMemo } from \"react\";\nimport type { Chain } from \"@liberfi.io/types\";\nimport { chainToNamespace } from \"@liberfi.io/utils\";\nimport type { WalletAdapter } from \"../types/WalletAdapter\";\nimport { useWallets } from \"./useWallets\";\n\n/**\n * Returns the first connected wallet matching the given chain's namespace,\n * or `undefined` if none is connected.\n *\n * Shorthand for the common pattern:\n * ```ts\n * const wallets = useWallets();\n * const wallet = wallets.find(w => w.chainNamespace === ns && w.isConnected);\n * ```\n */\nexport function useConnectedWallet(chain: Chain): WalletAdapter | undefined {\n const wallets = useWallets();\n const ns = chainToNamespace(chain);\n return useMemo(\n () => wallets.find((w) => w.chainNamespace === ns && w.isConnected),\n [wallets, ns],\n );\n}\n","import { useCallback } from \"react\";\nimport { Chain, ChainNamespace } from \"@liberfi.io/types\";\nimport { chainToNamespace } from \"@liberfi.io/utils\";\nimport { EvmWalletAdapter } from \"../types\";\nimport { useWallets } from \"./useWallets\";\n\n/**\n * Returns a function that switches all connected EVM wallets to the given chain.\n *\n * Solana chains are automatically skipped (no-op). For EVM chains, the returned\n * function calls `switchChain` on every connected wallet that implements\n * {@link EvmWalletAdapter} in parallel.\n *\n * @returns An async function `(chain: Chain) => Promise<void>`.\n */\nexport function useSwitchChain() {\n const wallets = useWallets();\n\n return useCallback(\n async (chain: Chain) => {\n if (chainToNamespace(chain) === ChainNamespace.SOLANA) return;\n\n const evmWallets = wallets.filter(\n (w): w is EvmWalletAdapter => \"switchChain\" in w && w.isConnected,\n );\n await Promise.all(evmWallets.map((w) => w.switchChain(chain)));\n },\n [wallets],\n );\n}\n","import { PropsWithChildren } from \"react\";\nimport {\n AuthContext,\n type AuthContextValue,\n type AuthStatus,\n} from \"./AuthContext\";\n\nexport type { AuthStatus };\n\n/** Props for {@link AuthProvider}. */\nexport type AuthProviderProps = PropsWithChildren<AuthContextValue>;\n\n/**\n * Provides authentication state to the component tree.\n *\n * This is a passthrough provider — it accepts the full context value as props\n * and forwards it to React context. Implementation providers (e.g.\n * `PrivyAuthProvider`) typically wrap this component and supply the concrete\n * `user`, `status`, `signIn`, `signOut`, and `refreshAccessToken` values.\n */\nexport function AuthProvider({ children, ...value }: AuthProviderProps) {\n return <AuthContext.Provider value={value}>{children}</AuthContext.Provider>;\n}\n","import { PropsWithChildren } from \"react\";\nimport {\n WalletConnectorContext,\n WalletConnectorContextValue,\n} from \"./WalletConnectorContext\";\n\n/** Props for {@link WalletConnectorProvider}. */\nexport type WalletConnectorProviderProps =\n PropsWithChildren<WalletConnectorContextValue>;\n\n/**\n * Provides wallet connection state to the component tree.\n *\n * This is a passthrough provider — it accepts the full context value as props\n * and forwards it to React context. Implementation providers (e.g.\n * `PrivyWalletConnectorProvider`) typically wrap this component and supply\n * the concrete `status`, `wallets`, `connect`, and `disconnect` values.\n */\nexport function WalletConnectorProvider({\n children,\n ...value\n}: WalletConnectorProviderProps) {\n return (\n <WalletConnectorContext.Provider value={value}>\n {children}\n </WalletConnectorContext.Provider>\n );\n}\n","declare global {\n interface Window {\n __LIBERFI_VERSION__?: {\n [key: string]: string;\n };\n }\n}\nif (typeof window !== \"undefined\") {\n window.__LIBERFI_VERSION__ = window.__LIBERFI_VERSION__ || {};\n window.__LIBERFI_VERSION__[\"@liberfi.io/wallet-connector\"] = \"0.1.185\";\n}\n\nexport default \"0.1.185\";\n"]}
|
|
1
|
+
{"version":3,"sources":["../src/providers/AuthContext.ts","../src/hooks/useAuth.ts","../src/hooks/useAuthCallback.ts","../src/providers/WalletConnectorContext.ts","../src/hooks/useWalletConnector.ts","../src/hooks/useWallets.ts","../src/hooks/useConnectedWallet.ts","../src/hooks/useSwitchEvmWalletsToChain.ts","../src/providers/AuthProvider.tsx","../src/providers/WalletConnectorProvider.tsx"],"names":["AuthContext","createContext","useAuth","context","useContext","useAuthCallback","callback","deps","status","signIn","statusRef","useRef","useCallback","args","WalletConnectorContext","useWalletConnector","useWallets","wallets","useConnectedWallet","chain","ns","chainToNamespace","useMemo","w","useSwitchEvmWalletsToChain","ChainNamespace","evmWallets","AuthProvider","children","value","jsx","WalletConnectorProvider"],"mappings":"sJAsBO,IAAMA,CAAAA,CAAcC,mBAAAA,CAAuC,IAAI,CAAA,CCX/D,SAASC,GAAU,CACxB,IAAMC,CAAAA,CAAUC,gBAAAA,CAAWJ,CAAW,CAAA,CACtC,GAAIG,CAAAA,GAAY,KACd,MAAM,IAAI,KAAA,CAAM,6CAA6C,CAAA,CAE/D,OAAOA,CACT,CCyBO,SAASE,CAAAA,CACdC,CAAAA,CACAC,EACwB,CACxB,GAAM,CAAE,MAAA,CAAAC,CAAAA,CAAQ,MAAA,CAAAC,CAAO,CAAA,CAAIP,GAAQ,CAE7BQ,CAAAA,CAAYC,YAAAA,CAAeH,CAAM,CAAA,CACvC,OAAAE,CAAAA,CAAU,OAAA,CAAUF,EAEbI,iBAAAA,CACL,MAAA,GAAUC,CAAAA,GAA2B,CACnC,GAAIH,CAAAA,CAAU,OAAA,GAAY,eAAA,CAAiB,CACrCA,EAAU,OAAA,GAAY,iBAAA,EAIxB,OAAA,CAAQ,OAAA,EAAQ,CACb,IAAA,CAAK,IAAMD,CAAAA,EAAQ,CAAA,CACnB,KAAA,CAAM,IAAM,CAGb,CAAC,CAAA,CAEL,MACF,CACA,MAAMH,CAAAA,CAAS,GAAGO,CAAI,EACxB,CAAA,CACA,CAAC,GAAIN,CAAAA,EAAQ,EAAC,CAAIE,CAAAA,CAAQH,CAAQ,CACpC,CACF,CC9CO,IAAMQ,CAAAA,CACXb,mBAAAA,CAAkD,IAAI,CAAA,CCfjD,SAASc,CAAAA,EAAqB,CACnC,IAAMZ,CAAAA,CAAUC,gBAAAA,CAAWU,CAAsB,CAAA,CACjD,GAAIX,IAAY,IAAA,CACd,MAAM,IAAI,KAAA,CACR,kEACF,CAAA,CAEF,OAAOA,CACT,CCVO,SAASa,CAAAA,EAAa,CAC3B,GAAM,CAAE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.js","sourcesContent":["import { createContext } from \"react\";\nimport { AuthenticatedUser } from \"../types\";\n\nexport type AuthStatus =\n | \"unauthenticated\"\n | \"authenticating\"\n | \"authenticated\"\n | \"deauthenticating\";\n\nexport interface AuthContextValue {\n /** authenticated user profile */\n user: AuthenticatedUser | null;\n /** authentication status */\n status: AuthStatus;\n /** sign in to the IdP */\n signIn: () => void | Promise<void>;\n /** sign out from the IdP */\n signOut: () => void | Promise<void>;\n /** refresh the access token */\n refreshAccessToken: () => void | Promise<void>;\n}\n\nexport const AuthContext = createContext<AuthContextValue | null>(null);\n","import { useContext } from \"react\";\nimport { AuthContext } from \"../providers/AuthContext\";\n\n/**\n * Returns the auth context value.\n *\n * Must be used within an {@link AuthProvider}.\n * Throws if no provider is found in the component tree.\n *\n * @returns The current {@link AuthContextValue} (user, status, signIn, signOut, refreshAccessToken).\n */\nexport function useAuth() {\n const context = useContext(AuthContext);\n if (context === null) {\n throw new Error(\"useAuth must be used within an AuthProvider\");\n }\n return context;\n}\n","import { DependencyList, useCallback, useRef } from \"react\";\nimport { useAuth } from \"./useAuth\";\n\n/**\n * The return type of {@link useAuthCallback}.\n *\n * Always resolves to `void` — the wrapper is fire-and-forget by design.\n * Any value returned by the input callback is discarded. To consume a\n * callback's result, capture it inside the callback itself (e.g. setState,\n * toast). For sign-in errors, observe via the auth provider's own channel\n * (e.g. `PrivyAuthProvider`'s `onError`).\n *\n * Rejections from the input callback DO propagate to the returned Promise,\n * so callers may `await` and `try/catch` to handle the callback's own\n * failures. Rejections from `signIn()` are silenced — they are observable\n * elsewhere.\n */\nexport type AuthGuardedCallback<A extends readonly unknown[]> = (\n ...args: A\n) => Promise<void>;\n\n/**\n * Wraps a callback so it only executes when the user is authenticated.\n *\n * Behavior by auth status:\n * - `authenticated`: executes the callback; the callback's return value is\n * discarded, but any rejection propagates to the returned Promise.\n * - `unauthenticated`: triggers `signIn()` fire-and-forget and resolves\n * immediately. Errors from `signIn()` are silenced here — observe them\n * via the auth provider's own error channel.\n * - `authenticating` / `deauthenticating`: no-op; resolves immediately\n * without re-triggering sign-in.\n *\n * The input callback's return type is constrained to `void | Promise<void>`\n * so the API surface is unambiguously fire-and-forget. To consume a\n * callback's result, capture it inside the callback itself rather than\n * via the wrapper's return value.\n *\n * @param callback - The function to guard behind authentication.\n * @param deps - Optional dependency list (same semantics as `useCallback`).\n * @returns A wrapped callback returning `Promise<void>`.\n */\nexport function useAuthCallback<A extends unknown[]>(\n callback: (...args: A) => void | Promise<void>,\n deps?: DependencyList,\n): AuthGuardedCallback<A> {\n const { status, signIn } = useAuth();\n\n const statusRef = useRef<string>(status);\n statusRef.current = status;\n\n return useCallback(\n async (...args: A): Promise<void> => {\n if (statusRef.current !== \"authenticated\") {\n if (statusRef.current === \"unauthenticated\") {\n // signIn may return void or Promise<void>. Use Promise.resolve().then(...)\n // (instead of Promise.resolve(signIn())) so a synchronous throw is also\n // captured by .catch. Keep fire-and-forget: do NOT await.\n Promise.resolve()\n .then(() => signIn())\n .catch(() => {\n // Silently swallow; signIn errors are observed via the auth\n // provider's own error channel (e.g. PrivyAuthProvider's onError).\n });\n }\n return;\n }\n await callback(...args);\n },\n [...(deps || []), signIn, callback],\n );\n}\n","import { createContext } from \"react\";\nimport { WalletAdapter } from \"../types\";\n\nexport interface WalletConnectorContextValue {\n /**\n * detecting: is detecting connected wallets\n * connecting: is connecting to the first wallet\n * connected: is connected to at least one wallet\n * disconnecting: is disconnecting from the last connected wallet\n * disconnected: is disconnected from all wallets\n */\n status:\n | \"detecting\"\n | \"connecting\"\n | \"connected\"\n | \"disconnecting\"\n | \"disconnected\";\n // all wallets that can be used to take onchain actions when connected\n wallets: Array<WalletAdapter>;\n // if no wallets are connected, connect one or multiple wallets\n connect: () => Promise<void>;\n // disconnect all wallets\n disconnect: () => Promise<void>;\n}\n\nexport const WalletConnectorContext =\n createContext<WalletConnectorContextValue | null>(null);\n","import { useContext } from \"react\";\nimport { WalletConnectorContext } from \"../providers/WalletConnectorContext\";\n\n/**\n * Returns the wallet connector context value.\n *\n * Must be used within a {@link WalletConnectorProvider}.\n * Throws if no provider is found in the component tree.\n *\n * @returns The current {@link WalletConnectorContextValue} (status, wallets, connect, disconnect).\n */\nexport function useWalletConnector() {\n const context = useContext(WalletConnectorContext);\n if (context === null) {\n throw new Error(\n \"useWalletConnector must be used within a WalletConnectorProvider\",\n );\n }\n return context;\n}\n","import { useWalletConnector } from \"./useWalletConnector\";\n\n/**\n * Convenience hook that returns the `wallets` array from the wallet connector context.\n *\n * Shorthand for `useWalletConnector().wallets`.\n *\n * @returns All wallets that can be used to take on-chain actions when connected.\n */\nexport function useWallets() {\n const { wallets } = useWalletConnector();\n return wallets;\n}\n","import { useMemo } from \"react\";\nimport type { Chain } from \"@liberfi.io/types\";\nimport { chainToNamespace } from \"@liberfi.io/utils\";\nimport type { WalletAdapter } from \"../types/WalletAdapter\";\nimport { useWallets } from \"./useWallets\";\n\n/**\n * Returns the first connected wallet matching the given chain's namespace,\n * or `undefined` if none is connected.\n *\n * Shorthand for the common pattern:\n * ```ts\n * const wallets = useWallets();\n * const wallet = wallets.find(w => w.chainNamespace === ns && w.isConnected);\n * ```\n */\nexport function useConnectedWallet(chain: Chain): WalletAdapter | undefined {\n const wallets = useWallets();\n const ns = chainToNamespace(chain);\n return useMemo(\n () => wallets.find((w) => w.chainNamespace === ns && w.isConnected),\n [wallets, ns],\n );\n}\n","import { useCallback } from \"react\";\nimport { Chain, ChainNamespace } from \"@liberfi.io/types\";\nimport { chainToNamespace } from \"@liberfi.io/utils\";\nimport { EvmWalletAdapter } from \"../types\";\nimport { useWallets } from \"./useWallets\";\n\n/**\n * Returns a function that switches **all connected EVM wallets** to the given chain.\n *\n * Semantics:\n *\n * - If `chain` is in the EVM namespace, every connected wallet whose\n * `chainNamespace === ChainNamespace.EVM` is switched concurrently.\n * - If `chain` is NOT in the EVM namespace (e.g. Solana), the call is a no-op\n * (Solana has no chain-switching concept within its namespace).\n * - Fail-fast on first wallet rejection (`Promise.all`); previously-switched\n * wallets are NOT rolled back. Subscribe to per-wallet status separately\n * if rollback is needed.\n *\n * **Do NOT use this hook to switch a single wallet** — call\n * `wallet.switchChain(chain)` directly on the {@link EvmWalletAdapter}. This\n * hook is the \"sync the whole wallet set to a UI-selected chain\" primitive.\n *\n * @returns An async function `(chain: Chain) => Promise<void>`.\n */\nexport function useSwitchEvmWalletsToChain() {\n const wallets = useWallets();\n\n return useCallback(\n async (chain: Chain) => {\n if (chainToNamespace(chain) !== ChainNamespace.EVM) return;\n\n const evmWallets = wallets.filter(\n (w): w is EvmWalletAdapter =>\n w.chainNamespace === ChainNamespace.EVM && w.isConnected,\n );\n await Promise.all(evmWallets.map((w) => w.switchChain(chain)));\n },\n [wallets],\n );\n}\n","import { PropsWithChildren } from \"react\";\nimport {\n AuthContext,\n type AuthContextValue,\n type AuthStatus,\n} from \"./AuthContext\";\n\nexport type { AuthStatus };\n\n/** Props for {@link AuthProvider}. */\nexport type AuthProviderProps = PropsWithChildren<AuthContextValue>;\n\n/**\n * Provides authentication state to the component tree.\n *\n * This is a passthrough provider — it accepts the full context value as props\n * and forwards it to React context. Implementation providers (e.g.\n * `PrivyAuthProvider`) typically wrap this component and supply the concrete\n * `user`, `status`, `signIn`, `signOut`, and `refreshAccessToken` values.\n */\nexport function AuthProvider({ children, ...value }: AuthProviderProps) {\n return <AuthContext.Provider value={value}>{children}</AuthContext.Provider>;\n}\n","import { PropsWithChildren } from \"react\";\nimport {\n WalletConnectorContext,\n WalletConnectorContextValue,\n} from \"./WalletConnectorContext\";\n\n/** Props for {@link WalletConnectorProvider}. */\nexport type WalletConnectorProviderProps =\n PropsWithChildren<WalletConnectorContextValue>;\n\n/**\n * Provides wallet connection state to the component tree.\n *\n * This is a passthrough provider — it accepts the full context value as props\n * and forwards it to React context. Implementation providers (e.g.\n * `PrivyWalletConnectorProvider`) typically wrap this component and supply\n * the concrete `status`, `wallets`, `connect`, and `disconnect` values.\n */\nexport function WalletConnectorProvider({\n children,\n ...value\n}: WalletConnectorProviderProps) {\n return (\n <WalletConnectorContext.Provider value={value}>\n {children}\n </WalletConnectorContext.Provider>\n );\n}\n"]}
|
package/dist/index.mjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import {createContext,useContext,useRef,useCallback,useMemo}from'react';import {chainToNamespace}from'@liberfi.io/utils';import {ChainNamespace}from'@liberfi.io/types';import {jsx}from'react/jsx-runtime';var n=createContext(
|
|
1
|
+
import {createContext,useContext,useRef,useCallback,useMemo}from'react';import {chainToNamespace}from'@liberfi.io/utils';import {ChainNamespace}from'@liberfi.io/types';import {jsx}from'react/jsx-runtime';var n=createContext(null);function u(){let t=useContext(n);if(t===null)throw new Error("useAuth must be used within an AuthProvider");return t}function M(t,e){let{status:r,signIn:o}=u(),l=useRef(r);return l.current=r,useCallback(async(...c)=>{if(l.current!=="authenticated"){l.current==="unauthenticated"&&Promise.resolve().then(()=>o()).catch(()=>{});return}await t(...c);},[...e||[],o,t])}var i=createContext(null);function s(){let t=useContext(i);if(t===null)throw new Error("useWalletConnector must be used within a WalletConnectorProvider");return t}function a(){let{wallets:t}=s();return t}function H(t){let e=a(),r=chainToNamespace(t);return useMemo(()=>e.find(o=>o.chainNamespace===r&&o.isConnected),[e,r])}function _(){let t=a();return useCallback(async e=>{if(chainToNamespace(e)!==ChainNamespace.EVM)return;let r=t.filter(o=>o.chainNamespace===ChainNamespace.EVM&&o.isConnected);await Promise.all(r.map(o=>o.switchChain(e)));},[t])}function lt({children:t,...e}){return jsx(n.Provider,{value:e,children:t})}function mt({children:t,...e}){return jsx(i.Provider,{value:e,children:t})}export{lt as AuthProvider,mt as WalletConnectorProvider,u as useAuth,M as useAuthCallback,H as useConnectedWallet,_ as useSwitchEvmWalletsToChain,s as useWalletConnector,a as useWallets};//# sourceMappingURL=index.mjs.map
|
|
2
2
|
//# sourceMappingURL=index.mjs.map
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/providers/AuthContext.ts","../src/hooks/useAuth.ts","../src/hooks/useAuthCallback.ts","../src/providers/WalletConnectorContext.ts","../src/hooks/useWalletConnector.ts","../src/hooks/useWallets.ts","../src/hooks/useConnectedWallet.ts","../src/hooks/useSwitchChain.ts","../src/providers/AuthProvider.tsx","../src/providers/WalletConnectorProvider.tsx","../src/version.ts"],"names":["AuthContext","createContext","useAuth","context","useContext","useAuthCallback","callback","deps","status","signIn","statusRef","useRef","useCallback","args","WalletConnectorContext","useWalletConnector","useWallets","wallets","useConnectedWallet","chain","ns","chainToNamespace","useMemo","w","useSwitchChain","ChainNamespace","evmWallets","AuthProvider","children","value","jsx","WalletConnectorProvider","version_default"],"mappings":"4MAsBO,IAAMA,CAAAA,CAAcC,aAAAA,CACzB,EACF,CAAA,CCbO,SAASC,CAAAA,EAAU,CACxB,IAAMC,CAAAA,CAAUC,UAAAA,CAAWJ,CAAW,CAAA,CACtC,GAAI,CAACG,CAAAA,CACH,MAAM,IAAI,KAAA,CAAM,4CAA4C,CAAA,CAE9D,OAAOA,CACT,CCUO,SAASE,CAAAA,CACdC,CAAAA,CACAC,EACwB,CACxB,GAAM,CAAE,MAAA,CAAAC,EAAQ,MAAA,CAAAC,CAAO,CAAA,CAAIP,CAAAA,GAErBQ,CAAAA,CAAYC,MAAAA,CAAeH,CAAM,CAAA,CACvC,OAAAE,CAAAA,CAAU,OAAA,CAAUF,CAAAA,CAEbI,YACL,MAAA,GACKC,CAAAA,GAC6C,CAChD,GAAIH,EAAU,OAAA,GAAY,eAAA,CAAiB,CACrCA,CAAAA,CAAU,UAAY,iBAAA,EACxBD,CAAAA,EAAO,CAET,MACF,CACA,OAAOH,CAAAA,CAAS,GAAGO,CAAI,CACzB,CAAA,CACA,CAAC,GAAIN,CAAAA,EAAQ,EAAC,CAAIE,CAAAA,CAAQH,CAAQ,CACpC,CACF,CCzBO,IAAMQ,CAAAA,CACXb,aAAAA,CAA2C,EAAiC,CAAA,CCfvE,SAASc,CAAAA,EAAqB,CACnC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CAAAA,CACA,GAAGC,CACL,CAAA,CAAiC,CAC/B,OACEC,IAAChB,CAAAA,CAAuB,QAAA,CAAvB,CAAgC,KAAA,CAAOe,EACrC,QAAA,CAAAD,CAAAA,CACH,CAEJ,CCpBI,OAAO,MAAA,CAAW,GAAA,GACpB,MAAA,CAAO,oBAAsB,MAAA,CAAO,mBAAA,EAAuB,EAAC,CAC5D,OAAO,mBAAA,CAAoB,8BAA8B,CAAA,CAAI,SAAA,CAAA,KAGxDI,CAAAA,CAAQ","file":"index.mjs","sourcesContent":["import { createContext } from \"react\";\nimport { AuthenticatedUser } from \"../types\";\n\nexport type AuthStatus =\n | \"unauthenticated\"\n | \"authenticating\"\n | \"authenticated\"\n | \"deauthenticating\";\n\nexport interface AuthContextValue {\n /** authenticated user profile */\n user: AuthenticatedUser | null;\n /** authentication status */\n status: AuthStatus;\n /** sign in to the IdP */\n signIn: () => void | Promise<void>;\n /** sign out from the IdP */\n signOut: () => void | Promise<void>;\n /** refresh the access token */\n refreshAccessToken: () => void | Promise<void>;\n}\n\nexport const AuthContext = createContext<AuthContextValue>(\n {} as AuthContextValue,\n);\n","import { useContext } from \"react\";\nimport { AuthContext } from \"../providers/AuthContext\";\n\n/**\n * Returns the auth context value.\n *\n * Must be used within an {@link AuthProvider}.\n * Throws if no provider is found in the component tree.\n *\n * @returns The current {@link AuthContextValue} (user, status, signIn, signOut, refreshAccessToken).\n */\nexport function useAuth() {\n const context = useContext(AuthContext);\n if (!context) {\n throw new Error(\"useAuth must be used within a AuthProvider\");\n }\n return context;\n}\n","/* eslint-disable @typescript-eslint/no-explicit-any */\nimport { DependencyList, useCallback, useRef } from \"react\";\nimport { useAuth } from \"./useAuth\";\n\n/**\n * The return type of {@link useAuthCallback}.\n *\n * Accepts the same parameters as `T` but always returns a `Promise` that\n * resolves to `Awaited<ReturnType<T>>` when authenticated, or `undefined`\n * when the user is not yet authenticated.\n */\nexport type AuthGuardedCallback<T extends (...args: any[]) => any> = (\n ...args: Parameters<T>\n) => Promise<Awaited<ReturnType<T>> | undefined>;\n\n/**\n * Wraps a callback so it only executes when the user is authenticated.\n *\n * - **authenticated**: executes the callback and returns its result.\n * - **unauthenticated**: triggers `signIn()` and returns `undefined`.\n * - **authenticating**: returns `undefined` without re-triggering sign-in\n * (avoids conflicting with an in-progress auth flow).\n *\n * @param callback - The function to guard behind authentication.\n * @param deps - Optional dependency list (same semantics as `useCallback`).\n * @returns A wrapped callback that may resolve to `undefined` when not authenticated.\n */\nexport function useAuthCallback<T extends (...args: any[]) => any>(\n callback: T,\n deps?: DependencyList,\n): AuthGuardedCallback<T> {\n const { status, signIn } = useAuth();\n\n const statusRef = useRef<string>(status);\n statusRef.current = status;\n\n return useCallback(\n async (\n ...args: Parameters<T>\n ): Promise<Awaited<ReturnType<T>> | undefined> => {\n if (statusRef.current !== \"authenticated\") {\n if (statusRef.current === \"unauthenticated\") {\n signIn();\n }\n return;\n }\n return callback(...args);\n },\n [...(deps || []), signIn, callback],\n );\n}\n","import { createContext } from \"react\";\nimport { WalletAdapter } from \"../types\";\n\nexport interface WalletConnectorContextValue {\n /**\n * detecting: is detecting connected wallets\n * connecting: is connecting to the first wallet\n * connected: is connected to at least one wallet\n * disconnecting: is disconnecting from the last connected wallet\n * disconnected: is disconnected from all wallets\n */\n status:\n | \"detecting\"\n | \"connecting\"\n | \"connected\"\n | \"disconnecting\"\n | \"disconnected\";\n // all wallets that can be used to take onchain actions when connected\n wallets: Array<WalletAdapter>;\n // if no wallets are connected, connect one or multiple wallets\n connect: () => Promise<void>;\n // disconnect all wallets\n disconnect: () => Promise<void>;\n}\n\nexport const WalletConnectorContext =\n createContext<WalletConnectorContextValue>({} as WalletConnectorContextValue);\n","import { useContext } from \"react\";\nimport { WalletConnectorContext } from \"../providers/WalletConnectorContext\";\n\n/**\n * Returns the wallet connector context value.\n *\n * Must be used within a {@link WalletConnectorProvider}.\n * Throws if no provider is found in the component tree.\n *\n * @returns The current {@link WalletConnectorContextValue} (status, wallets, connect, disconnect).\n */\nexport function useWalletConnector() {\n const context = useContext(WalletConnectorContext);\n if (!context) {\n throw new Error(\n \"useWalletConnector must be used within a WalletConnectorProvider\",\n );\n }\n return context;\n}\n","import { useWalletConnector } from \"./useWalletConnector\";\n\n/**\n * Convenience hook that returns the `wallets` array from the wallet connector context.\n *\n * Shorthand for `useWalletConnector().wallets`.\n *\n * @returns All wallets that can be used to take on-chain actions when connected.\n */\nexport function useWallets() {\n const { wallets } = useWalletConnector();\n return wallets;\n}\n","import { useMemo } from \"react\";\nimport type { Chain } from \"@liberfi.io/types\";\nimport { chainToNamespace } from \"@liberfi.io/utils\";\nimport type { WalletAdapter } from \"../types/WalletAdapter\";\nimport { useWallets } from \"./useWallets\";\n\n/**\n * Returns the first connected wallet matching the given chain's namespace,\n * or `undefined` if none is connected.\n *\n * Shorthand for the common pattern:\n * ```ts\n * const wallets = useWallets();\n * const wallet = wallets.find(w => w.chainNamespace === ns && w.isConnected);\n * ```\n */\nexport function useConnectedWallet(chain: Chain): WalletAdapter | undefined {\n const wallets = useWallets();\n const ns = chainToNamespace(chain);\n return useMemo(\n () => wallets.find((w) => w.chainNamespace === ns && w.isConnected),\n [wallets, ns],\n );\n}\n","import { useCallback } from \"react\";\nimport { Chain, ChainNamespace } from \"@liberfi.io/types\";\nimport { chainToNamespace } from \"@liberfi.io/utils\";\nimport { EvmWalletAdapter } from \"../types\";\nimport { useWallets } from \"./useWallets\";\n\n/**\n * Returns a function that switches all connected EVM wallets to the given chain.\n *\n * Solana chains are automatically skipped (no-op). For EVM chains, the returned\n * function calls `switchChain` on every connected wallet that implements\n * {@link EvmWalletAdapter} in parallel.\n *\n * @returns An async function `(chain: Chain) => Promise<void>`.\n */\nexport function useSwitchChain() {\n const wallets = useWallets();\n\n return useCallback(\n async (chain: Chain) => {\n if (chainToNamespace(chain) === ChainNamespace.SOLANA) return;\n\n const evmWallets = wallets.filter(\n (w): w is EvmWalletAdapter => \"switchChain\" in w && w.isConnected,\n );\n await Promise.all(evmWallets.map((w) => w.switchChain(chain)));\n },\n [wallets],\n );\n}\n","import { PropsWithChildren } from \"react\";\nimport {\n AuthContext,\n type AuthContextValue,\n type AuthStatus,\n} from \"./AuthContext\";\n\nexport type { AuthStatus };\n\n/** Props for {@link AuthProvider}. */\nexport type AuthProviderProps = PropsWithChildren<AuthContextValue>;\n\n/**\n * Provides authentication state to the component tree.\n *\n * This is a passthrough provider — it accepts the full context value as props\n * and forwards it to React context. Implementation providers (e.g.\n * `PrivyAuthProvider`) typically wrap this component and supply the concrete\n * `user`, `status`, `signIn`, `signOut`, and `refreshAccessToken` values.\n */\nexport function AuthProvider({ children, ...value }: AuthProviderProps) {\n return <AuthContext.Provider value={value}>{children}</AuthContext.Provider>;\n}\n","import { PropsWithChildren } from \"react\";\nimport {\n WalletConnectorContext,\n WalletConnectorContextValue,\n} from \"./WalletConnectorContext\";\n\n/** Props for {@link WalletConnectorProvider}. */\nexport type WalletConnectorProviderProps =\n PropsWithChildren<WalletConnectorContextValue>;\n\n/**\n * Provides wallet connection state to the component tree.\n *\n * This is a passthrough provider — it accepts the full context value as props\n * and forwards it to React context. Implementation providers (e.g.\n * `PrivyWalletConnectorProvider`) typically wrap this component and supply\n * the concrete `status`, `wallets`, `connect`, and `disconnect` values.\n */\nexport function WalletConnectorProvider({\n children,\n ...value\n}: WalletConnectorProviderProps) {\n return (\n <WalletConnectorContext.Provider value={value}>\n {children}\n </WalletConnectorContext.Provider>\n );\n}\n","declare global {\n interface Window {\n __LIBERFI_VERSION__?: {\n [key: string]: string;\n };\n }\n}\nif (typeof window !== \"undefined\") {\n window.__LIBERFI_VERSION__ = window.__LIBERFI_VERSION__ || {};\n window.__LIBERFI_VERSION__[\"@liberfi.io/wallet-connector\"] = \"0.1.185\";\n}\n\nexport default \"0.1.185\";\n"]}
|
|
1
|
+
{"version":3,"sources":["../src/providers/AuthContext.ts","../src/hooks/useAuth.ts","../src/hooks/useAuthCallback.ts","../src/providers/WalletConnectorContext.ts","../src/hooks/useWalletConnector.ts","../src/hooks/useWallets.ts","../src/hooks/useConnectedWallet.ts","../src/hooks/useSwitchEvmWalletsToChain.ts","../src/providers/AuthProvider.tsx","../src/providers/WalletConnectorProvider.tsx"],"names":["AuthContext","createContext","useAuth","context","useContext","useAuthCallback","callback","deps","status","signIn","statusRef","useRef","useCallback","args","WalletConnectorContext","useWalletConnector","useWallets","wallets","useConnectedWallet","chain","ns","chainToNamespace","useMemo","w","useSwitchEvmWalletsToChain","ChainNamespace","evmWallets","AuthProvider","children","value","jsx","WalletConnectorProvider"],"mappings":"4MAsBO,IAAMA,CAAAA,CAAcC,aAAAA,CAAuC,IAAI,CAAA,CCX/D,SAASC,GAAU,CACxB,IAAMC,CAAAA,CAAUC,UAAAA,CAAWJ,CAAW,CAAA,CACtC,GAAIG,CAAAA,GAAY,KACd,MAAM,IAAI,KAAA,CAAM,6CAA6C,CAAA,CAE/D,OAAOA,CACT,CCyBO,SAASE,CAAAA,CACdC,CAAAA,CACAC,EACwB,CACxB,GAAM,CAAE,MAAA,CAAAC,CAAAA,CAAQ,MAAA,CAAAC,CAAO,CAAA,CAAIP,GAAQ,CAE7BQ,CAAAA,CAAYC,MAAAA,CAAeH,CAAM,CAAA,CACvC,OAAAE,CAAAA,CAAU,OAAA,CAAUF,EAEbI,WAAAA,CACL,MAAA,GAAUC,CAAAA,GAA2B,CACnC,GAAIH,CAAAA,CAAU,OAAA,GAAY,eAAA,CAAiB,CACrCA,EAAU,OAAA,GAAY,iBAAA,EAIxB,OAAA,CAAQ,OAAA,EAAQ,CACb,IAAA,CAAK,IAAMD,CAAAA,EAAQ,CAAA,CACnB,KAAA,CAAM,IAAM,CAGb,CAAC,CAAA,CAEL,MACF,CACA,MAAMH,CAAAA,CAAS,GAAGO,CAAI,EACxB,CAAA,CACA,CAAC,GAAIN,CAAAA,EAAQ,EAAC,CAAIE,CAAAA,CAAQH,CAAQ,CACpC,CACF,CC9CO,IAAMQ,CAAAA,CACXb,aAAAA,CAAkD,IAAI,CAAA,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AAA,CAAS,CACvD,CCJO,SAASG,EAAAA,CAAwB,CACtC,QAAA,CAAAH,CAAAA,CACA,GAAGC,CACL,EAAiC,CAC/B,OACEC,GAAAA,CAAChB,CAAAA,CAAuB,SAAvB,CAAgC,KAAA,CAAOe,CAAAA,CACrC,QAAA,CAAAD,EACH,CAEJ","file":"index.mjs","sourcesContent":["import { createContext } from \"react\";\nimport { AuthenticatedUser } from \"../types\";\n\nexport type AuthStatus =\n | \"unauthenticated\"\n | \"authenticating\"\n | \"authenticated\"\n | \"deauthenticating\";\n\nexport interface AuthContextValue {\n /** authenticated user profile */\n user: AuthenticatedUser | null;\n /** authentication status */\n status: AuthStatus;\n /** sign in to the IdP */\n signIn: () => void | Promise<void>;\n /** sign out from the IdP */\n signOut: () => void | Promise<void>;\n /** refresh the access token */\n refreshAccessToken: () => void | Promise<void>;\n}\n\nexport const AuthContext = createContext<AuthContextValue | null>(null);\n","import { useContext } from \"react\";\nimport { AuthContext } from \"../providers/AuthContext\";\n\n/**\n * Returns the auth context value.\n *\n * Must be used within an {@link AuthProvider}.\n * Throws if no provider is found in the component tree.\n *\n * @returns The current {@link AuthContextValue} (user, status, signIn, signOut, refreshAccessToken).\n */\nexport function useAuth() {\n const context = useContext(AuthContext);\n if (context === null) {\n throw new Error(\"useAuth must be used within an AuthProvider\");\n }\n return context;\n}\n","import { DependencyList, useCallback, useRef } from \"react\";\nimport { useAuth } from \"./useAuth\";\n\n/**\n * The return type of {@link useAuthCallback}.\n *\n * Always resolves to `void` — the wrapper is fire-and-forget by design.\n * Any value returned by the input callback is discarded. To consume a\n * callback's result, capture it inside the callback itself (e.g. setState,\n * toast). For sign-in errors, observe via the auth provider's own channel\n * (e.g. `PrivyAuthProvider`'s `onError`).\n *\n * Rejections from the input callback DO propagate to the returned Promise,\n * so callers may `await` and `try/catch` to handle the callback's own\n * failures. Rejections from `signIn()` are silenced — they are observable\n * elsewhere.\n */\nexport type AuthGuardedCallback<A extends readonly unknown[]> = (\n ...args: A\n) => Promise<void>;\n\n/**\n * Wraps a callback so it only executes when the user is authenticated.\n *\n * Behavior by auth status:\n * - `authenticated`: executes the callback; the callback's return value is\n * discarded, but any rejection propagates to the returned Promise.\n * - `unauthenticated`: triggers `signIn()` fire-and-forget and resolves\n * immediately. Errors from `signIn()` are silenced here — observe them\n * via the auth provider's own error channel.\n * - `authenticating` / `deauthenticating`: no-op; resolves immediately\n * without re-triggering sign-in.\n *\n * The input callback's return type is constrained to `void | Promise<void>`\n * so the API surface is unambiguously fire-and-forget. To consume a\n * callback's result, capture it inside the callback itself rather than\n * via the wrapper's return value.\n *\n * @param callback - The function to guard behind authentication.\n * @param deps - Optional dependency list (same semantics as `useCallback`).\n * @returns A wrapped callback returning `Promise<void>`.\n */\nexport function useAuthCallback<A extends unknown[]>(\n callback: (...args: A) => void | Promise<void>,\n deps?: DependencyList,\n): AuthGuardedCallback<A> {\n const { status, signIn } = useAuth();\n\n const statusRef = useRef<string>(status);\n statusRef.current = status;\n\n return useCallback(\n async (...args: A): Promise<void> => {\n if (statusRef.current !== \"authenticated\") {\n if (statusRef.current === \"unauthenticated\") {\n // signIn may return void or Promise<void>. Use Promise.resolve().then(...)\n // (instead of Promise.resolve(signIn())) so a synchronous throw is also\n // captured by .catch. Keep fire-and-forget: do NOT await.\n Promise.resolve()\n .then(() => signIn())\n .catch(() => {\n // Silently swallow; signIn errors are observed via the auth\n // provider's own error channel (e.g. PrivyAuthProvider's onError).\n });\n }\n return;\n }\n await callback(...args);\n },\n [...(deps || []), signIn, callback],\n );\n}\n","import { createContext } from \"react\";\nimport { WalletAdapter } from \"../types\";\n\nexport interface WalletConnectorContextValue {\n /**\n * detecting: is detecting connected wallets\n * connecting: is connecting to the first wallet\n * connected: is connected to at least one wallet\n * disconnecting: is disconnecting from the last connected wallet\n * disconnected: is disconnected from all wallets\n */\n status:\n | \"detecting\"\n | \"connecting\"\n | \"connected\"\n | \"disconnecting\"\n | \"disconnected\";\n // all wallets that can be used to take onchain actions when connected\n wallets: Array<WalletAdapter>;\n // if no wallets are connected, connect one or multiple wallets\n connect: () => Promise<void>;\n // disconnect all wallets\n disconnect: () => Promise<void>;\n}\n\nexport const WalletConnectorContext =\n createContext<WalletConnectorContextValue | null>(null);\n","import { useContext } from \"react\";\nimport { WalletConnectorContext } from \"../providers/WalletConnectorContext\";\n\n/**\n * Returns the wallet connector context value.\n *\n * Must be used within a {@link WalletConnectorProvider}.\n * Throws if no provider is found in the component tree.\n *\n * @returns The current {@link WalletConnectorContextValue} (status, wallets, connect, disconnect).\n */\nexport function useWalletConnector() {\n const context = useContext(WalletConnectorContext);\n if (context === null) {\n throw new Error(\n \"useWalletConnector must be used within a WalletConnectorProvider\",\n );\n }\n return context;\n}\n","import { useWalletConnector } from \"./useWalletConnector\";\n\n/**\n * Convenience hook that returns the `wallets` array from the wallet connector context.\n *\n * Shorthand for `useWalletConnector().wallets`.\n *\n * @returns All wallets that can be used to take on-chain actions when connected.\n */\nexport function useWallets() {\n const { wallets } = useWalletConnector();\n return wallets;\n}\n","import { useMemo } from \"react\";\nimport type { Chain } from \"@liberfi.io/types\";\nimport { chainToNamespace } from \"@liberfi.io/utils\";\nimport type { WalletAdapter } from \"../types/WalletAdapter\";\nimport { useWallets } from \"./useWallets\";\n\n/**\n * Returns the first connected wallet matching the given chain's namespace,\n * or `undefined` if none is connected.\n *\n * Shorthand for the common pattern:\n * ```ts\n * const wallets = useWallets();\n * const wallet = wallets.find(w => w.chainNamespace === ns && w.isConnected);\n * ```\n */\nexport function useConnectedWallet(chain: Chain): WalletAdapter | undefined {\n const wallets = useWallets();\n const ns = chainToNamespace(chain);\n return useMemo(\n () => wallets.find((w) => w.chainNamespace === ns && w.isConnected),\n [wallets, ns],\n );\n}\n","import { useCallback } from \"react\";\nimport { Chain, ChainNamespace } from \"@liberfi.io/types\";\nimport { chainToNamespace } from \"@liberfi.io/utils\";\nimport { EvmWalletAdapter } from \"../types\";\nimport { useWallets } from \"./useWallets\";\n\n/**\n * Returns a function that switches **all connected EVM wallets** to the given chain.\n *\n * Semantics:\n *\n * - If `chain` is in the EVM namespace, every connected wallet whose\n * `chainNamespace === ChainNamespace.EVM` is switched concurrently.\n * - If `chain` is NOT in the EVM namespace (e.g. Solana), the call is a no-op\n * (Solana has no chain-switching concept within its namespace).\n * - Fail-fast on first wallet rejection (`Promise.all`); previously-switched\n * wallets are NOT rolled back. Subscribe to per-wallet status separately\n * if rollback is needed.\n *\n * **Do NOT use this hook to switch a single wallet** — call\n * `wallet.switchChain(chain)` directly on the {@link EvmWalletAdapter}. This\n * hook is the \"sync the whole wallet set to a UI-selected chain\" primitive.\n *\n * @returns An async function `(chain: Chain) => Promise<void>`.\n */\nexport function useSwitchEvmWalletsToChain() {\n const wallets = useWallets();\n\n return useCallback(\n async (chain: Chain) => {\n if (chainToNamespace(chain) !== ChainNamespace.EVM) return;\n\n const evmWallets = wallets.filter(\n (w): w is EvmWalletAdapter =>\n w.chainNamespace === ChainNamespace.EVM && w.isConnected,\n );\n await Promise.all(evmWallets.map((w) => w.switchChain(chain)));\n },\n [wallets],\n );\n}\n","import { PropsWithChildren } from \"react\";\nimport {\n AuthContext,\n type AuthContextValue,\n type AuthStatus,\n} from \"./AuthContext\";\n\nexport type { AuthStatus };\n\n/** Props for {@link AuthProvider}. */\nexport type AuthProviderProps = PropsWithChildren<AuthContextValue>;\n\n/**\n * Provides authentication state to the component tree.\n *\n * This is a passthrough provider — it accepts the full context value as props\n * and forwards it to React context. Implementation providers (e.g.\n * `PrivyAuthProvider`) typically wrap this component and supply the concrete\n * `user`, `status`, `signIn`, `signOut`, and `refreshAccessToken` values.\n */\nexport function AuthProvider({ children, ...value }: AuthProviderProps) {\n return <AuthContext.Provider value={value}>{children}</AuthContext.Provider>;\n}\n","import { PropsWithChildren } from \"react\";\nimport {\n WalletConnectorContext,\n WalletConnectorContextValue,\n} from \"./WalletConnectorContext\";\n\n/** Props for {@link WalletConnectorProvider}. */\nexport type WalletConnectorProviderProps =\n PropsWithChildren<WalletConnectorContextValue>;\n\n/**\n * Provides wallet connection state to the component tree.\n *\n * This is a passthrough provider — it accepts the full context value as props\n * and forwards it to React context. Implementation providers (e.g.\n * `PrivyWalletConnectorProvider`) typically wrap this component and supply\n * the concrete `status`, `wallets`, `connect`, and `disconnect` values.\n */\nexport function WalletConnectorProvider({\n children,\n ...value\n}: WalletConnectorProviderProps) {\n return (\n <WalletConnectorContext.Provider value={value}>\n {children}\n </WalletConnectorContext.Provider>\n );\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@liberfi.io/wallet-connector",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "Base Wallet Connector for Liberfi React SDK",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "dist/index.mjs",
|
|
@@ -14,8 +14,8 @@
|
|
|
14
14
|
"access": "public"
|
|
15
15
|
},
|
|
16
16
|
"dependencies": {
|
|
17
|
-
"@liberfi.io/types": "0.
|
|
18
|
-
"@liberfi.io/utils": "0.
|
|
17
|
+
"@liberfi.io/types": "0.4.1",
|
|
18
|
+
"@liberfi.io/utils": "0.2.0"
|
|
19
19
|
},
|
|
20
20
|
"devDependencies": {
|
|
21
21
|
"@testing-library/dom": "^10.4.1",
|
|
@@ -31,7 +31,7 @@
|
|
|
31
31
|
"ts-jest": "^29.4.6",
|
|
32
32
|
"tsup": "^8.5.0",
|
|
33
33
|
"typescript": "^5.9.2",
|
|
34
|
-
"tsconfig": "0.1.
|
|
34
|
+
"tsconfig": "0.1.184"
|
|
35
35
|
},
|
|
36
36
|
"peerDependencies": {
|
|
37
37
|
"react": ">=18",
|