@liberfi.io/wallet-connector 0.2.72 → 0.2.74
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/dist/index.d.mts +1 -1
- package/dist/index.d.ts +1 -1
- 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 +8 -5
package/dist/index.d.mts
CHANGED
|
@@ -82,7 +82,7 @@ type AuthStatus = "unauthenticated" | "authenticating" | "authenticated" | "deau
|
|
|
82
82
|
interface AuthContextValue {
|
|
83
83
|
/** authenticated user profile */
|
|
84
84
|
user: AuthenticatedUser | null;
|
|
85
|
-
/**
|
|
85
|
+
/** Authentication status. `authenticated` always implies a non-null user token. */
|
|
86
86
|
status: AuthStatus;
|
|
87
87
|
/** sign in to the IdP */
|
|
88
88
|
signIn: () => void | Promise<void>;
|
package/dist/index.d.ts
CHANGED
|
@@ -82,7 +82,7 @@ type AuthStatus = "unauthenticated" | "authenticating" | "authenticated" | "deau
|
|
|
82
82
|
interface AuthContextValue {
|
|
83
83
|
/** authenticated user profile */
|
|
84
84
|
user: AuthenticatedUser | null;
|
|
85
|
-
/**
|
|
85
|
+
/** Authentication status. `authenticated` always implies a non-null user token. */
|
|
86
86
|
status: AuthStatus;
|
|
87
87
|
/** sign in to the IdP */
|
|
88
88
|
signIn: () => void | Promise<void>;
|
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(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(),
|
|
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(),s=react.useRef(r);return s.current=r,react.useCallback(async(...p)=>{if(s.current!=="authenticated"){s.current==="unauthenticated"&&Promise.resolve().then(()=>o()).catch(()=>{});return}await t(...p);},[o,t])}var i=react.createContext(null);function l(){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}=l();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 st({children:t,...e}){let r=e.status==="authenticated"&&!e.user?.accessToken?{...e,status:"authenticating"}:e;return jsxRuntime.jsx(n.Provider,{value:r,children:t})}function mt({children:t,...e}){return jsxRuntime.jsx(i.Provider,{value:e,children:t})}exports.AuthProvider=st;exports.WalletConnectorProvider=mt;exports.useAuth=u;exports.useAuthCallback=M;exports.useConnectedWallet=H;exports.useSwitchEvmWalletsToChain=_;exports.useWalletConnector=l;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/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,O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","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"]}
|
|
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","normalizedValue","jsx","WalletConnectorProvider"],"mappings":"sJAsBO,IAAMA,CAAAA,CAAcC,oBAAuC,IAAI,CAAA,CCX/D,SAASC,CAAAA,EAAU,CACxB,IAAMC,CAAAA,CAAUC,gBAAAA,CAAWJ,CAAW,CAAA,CACtC,GAAIG,CAAAA,GAAY,IAAA,CACd,MAAM,IAAI,KAAA,CAAM,6CAA6C,CAAA,CAE/D,OAAOA,CACT,CCyBO,SAASE,CAAAA,CACdC,CAAAA,CACAC,CAAAA,CACwB,CACxB,GAAM,CAAE,MAAA,CAAAC,CAAAA,CAAQ,MAAA,CAAAC,CAAO,CAAA,CAAIP,CAAAA,EAAQ,CAG7BQ,CAAAA,CAAYC,aAAeH,CAAM,CAAA,CACvC,OAAAE,CAAAA,CAAU,OAAA,CAAUF,CAAAA,CAEbI,iBAAAA,CACL,MAAA,GAAUC,IAA2B,CACnC,GAAIH,CAAAA,CAAU,OAAA,GAAY,gBAAiB,CACrCA,CAAAA,CAAU,OAAA,GAAY,iBAAA,EAIxB,QAAQ,OAAA,EAAQ,CACb,IAAA,CAAK,IAAMD,CAAAA,EAAQ,CAAA,CACnB,KAAA,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CACF,CCpBO,SAASU,EAAAA,CAAa,CAAE,QAAA,CAAAC,CAAAA,CAAU,GAAGC,CAAM,EAAsB,CACtE,IAAMC,CAAAA,CACJD,CAAAA,CAAM,SAAW,eAAA,EAAmB,CAACA,CAAAA,CAAM,IAAA,EAAM,YAC7C,CAAE,GAAGA,CAAAA,CAAO,MAAA,CAAQ,gBAA0B,CAAA,CAC9CA,CAAAA,CACN,OACEE,eAAC/B,CAAAA,CAAY,QAAA,CAAZ,CAAqB,KAAA,CAAO8B,EAC1B,QAAA,CAAAF,CAAAA,CACH,CAEJ,CCZO,SAASI,EAAAA,CAAwB,CACtC,QAAA,CAAAJ,CAAAA,CACA,GAAGC,CACL,EAAiC,CAC/B,OACEE,cAAAA,CAACjB,CAAAA,CAAuB,SAAvB,CAAgC,KAAA,CAAOe,CAAAA,CACrC,QAAA,CAAAD,EACH,CAEJ","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. `authenticated` always implies a non-null user token. */\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 void deps;\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 [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 const normalizedValue =\n value.status === \"authenticated\" && !value.user?.accessToken\n ? { ...value, status: \"authenticating\" as const }\n : value;\n return (\n <AuthContext.Provider value={normalizedValue}>\n {children}\n </AuthContext.Provider>\n );\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(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(),
|
|
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(),s=useRef(r);return s.current=r,useCallback(async(...p)=>{if(s.current!=="authenticated"){s.current==="unauthenticated"&&Promise.resolve().then(()=>o()).catch(()=>{});return}await t(...p);},[o,t])}var i=createContext(null);function l(){let t=useContext(i);if(t===null)throw new Error("useWalletConnector must be used within a WalletConnectorProvider");return t}function a(){let{wallets:t}=l();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 st({children:t,...e}){let r=e.status==="authenticated"&&!e.user?.accessToken?{...e,status:"authenticating"}:e;return jsx(n.Provider,{value:r,children:t})}function mt({children:t,...e}){return jsx(i.Provider,{value:e,children:t})}export{st as AuthProvider,mt as WalletConnectorProvider,u as useAuth,M as useAuthCallback,H as useConnectedWallet,_ as useSwitchEvmWalletsToChain,l 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/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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AA,CAAKC,gBAAAA,CAAiBF,CAAK,CAAA,CACjC,OAAOG,OAAAA,CACL,IAAML,CAAAA,CAAQ,IAAA,CAAMM,CAAAA,EAAMA,CAAAA,CAAE,cAAA,GAAmBH,CAAAA,EAAMG,CAAAA,CAAE,WAAW,EAClE,CAACN,CAAAA,CAASG,CAAE,CACd,CACF,CCEO,SAASI,CAAAA,EAA6B,CAC3C,IAAMP,CAAAA,CAAUD,CAAAA,EAAW,CAE3B,OAAOJ,YACL,MAAOO,CAAAA,EAAiB,CACtB,GAAIE,iBAAiBF,CAAK,CAAA,GAAMM,cAAAA,CAAe,GAAA,CAAK,OAEpD,IAAMC,CAAAA,CAAaT,CAAAA,CAAQ,MAAA,CACxBM,CAAAA,EACCA,CAAAA,CAAE,cAAA,GAAmBE,cAAAA,CAAe,KAAOF,CAAAA,CAAE,WACjD,CAAA,CACA,MAAM,OAAA,CAAQ,GAAA,CAAIG,CAAAA,CAAW,GAAA,CAAKH,GAAMA,CAAAA,CAAE,WAAA,CAAYJ,CAAK,CAAC,CAAC,EAC/D,CAAA,CACA,CAACF,CAAO,CACV,CACF,CCpBO,SAASU,EAAAA,CAAa,CAAE,SAAAC,CAAAA,CAAU,GAAGC,CAAM,CAAA,CAAsB,CACtE,OAAOC,GAAAA,CAAC9B,CAAAA,CAAY,SAAZ,CAAqB,KAAA,CAAO6B,CAAAA,CAAQ,QAAA,CAAAD,CAAAA,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"]}
|
|
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","normalizedValue","jsx","WalletConnectorProvider"],"mappings":"4MAsBO,IAAMA,CAAAA,CAAcC,cAAuC,IAAI,CAAA,CCX/D,SAASC,CAAAA,EAAU,CACxB,IAAMC,CAAAA,CAAUC,UAAAA,CAAWJ,CAAW,CAAA,CACtC,GAAIG,CAAAA,GAAY,IAAA,CACd,MAAM,IAAI,KAAA,CAAM,6CAA6C,CAAA,CAE/D,OAAOA,CACT,CCyBO,SAASE,CAAAA,CACdC,CAAAA,CACAC,CAAAA,CACwB,CACxB,GAAM,CAAE,MAAA,CAAAC,CAAAA,CAAQ,MAAA,CAAAC,CAAO,CAAA,CAAIP,CAAAA,EAAQ,CAG7BQ,CAAAA,CAAYC,OAAeH,CAAM,CAAA,CACvC,OAAAE,CAAAA,CAAU,OAAA,CAAUF,CAAAA,CAEbI,WAAAA,CACL,MAAA,GAAUC,IAA2B,CACnC,GAAIH,CAAAA,CAAU,OAAA,GAAY,gBAAiB,CACrCA,CAAAA,CAAU,OAAA,GAAY,iBAAA,EAIxB,QAAQ,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,CAACJ,CAAAA,CAAQH,CAAQ,CACnB,CACF,CC/CO,IAAMQ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CAAM,IAAA,EAAM,YAC7C,CAAE,GAAGA,CAAAA,CAAO,MAAA,CAAQ,gBAA0B,CAAA,CAC9CA,CAAAA,CACN,OACEE,IAAC/B,CAAAA,CAAY,QAAA,CAAZ,CAAqB,KAAA,CAAO8B,EAC1B,QAAA,CAAAF,CAAAA,CACH,CAEJ,CCZO,SAASI,EAAAA,CAAwB,CACtC,QAAA,CAAAJ,CAAAA,CACA,GAAGC,CACL,EAAiC,CAC/B,OACEE,GAAAA,CAACjB,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. `authenticated` always implies a non-null user token. */\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 void deps;\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 [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 const normalizedValue =\n value.status === \"authenticated\" && !value.user?.accessToken\n ? { ...value, status: \"authenticating\" as const }\n : value;\n return (\n <AuthContext.Provider value={normalizedValue}>\n {children}\n </AuthContext.Provider>\n );\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.2.
|
|
3
|
+
"version": "0.2.74",
|
|
4
4
|
"description": "Base Wallet Connector for Liberfi React SDK",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "dist/index.mjs",
|
|
@@ -14,13 +14,14 @@
|
|
|
14
14
|
"access": "public"
|
|
15
15
|
},
|
|
16
16
|
"dependencies": {
|
|
17
|
-
"@liberfi.io/types": "0.4.
|
|
18
|
-
"@liberfi.io/utils": "0.2.
|
|
17
|
+
"@liberfi.io/types": "0.4.75",
|
|
18
|
+
"@liberfi.io/utils": "0.2.74"
|
|
19
19
|
},
|
|
20
20
|
"devDependencies": {
|
|
21
21
|
"@testing-library/dom": "^10.4.1",
|
|
22
22
|
"@testing-library/react": "^16.3.0",
|
|
23
23
|
"@types/jest": "^29.5.3",
|
|
24
|
+
"@types/node": "^24.5.0",
|
|
24
25
|
"@types/react": "^19.1.13",
|
|
25
26
|
"@types/react-dom": "^19.1.9",
|
|
26
27
|
"jest": "^29.6.1",
|
|
@@ -31,7 +32,7 @@
|
|
|
31
32
|
"ts-jest": "^29.4.6",
|
|
32
33
|
"tsup": "^8.5.0",
|
|
33
34
|
"typescript": "^5.9.2",
|
|
34
|
-
"tsconfig": "0.1.
|
|
35
|
+
"tsconfig": "0.1.258"
|
|
35
36
|
},
|
|
36
37
|
"peerDependencies": {
|
|
37
38
|
"react": ">=18",
|
|
@@ -40,6 +41,8 @@
|
|
|
40
41
|
"scripts": {
|
|
41
42
|
"dev": "tsup --watch",
|
|
42
43
|
"build": "rimraf -rf dist && tsup",
|
|
43
|
-
"
|
|
44
|
+
"typecheck": "tsc --noEmit -p tsconfig.json",
|
|
45
|
+
"lint": "eslint src",
|
|
46
|
+
"test": "jest --detectOpenHandles"
|
|
44
47
|
}
|
|
45
48
|
}
|