@kheopskit/core 6.0.1 → 6.0.3
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/CHANGELOG.md +14 -0
- package/dist/{config-Bb-Zz8J1.d.ts → config-Csp5p0-Z.d.ts} +2 -2
- package/dist/{config-C-Xk2AP_.d.mts → config-qMsgD8tJ.d.mts} +2 -2
- package/dist/ethereum.d.mts +1 -1
- package/dist/ethereum.d.ts +1 -1
- package/dist/ethereum.js +57 -8
- package/dist/ethereum.js.map +1 -1
- package/dist/ethereum.mjs +58 -9
- package/dist/ethereum.mjs.map +1 -1
- package/dist/index.d.mts +3 -3
- package/dist/index.d.ts +3 -3
- package/dist/index.js +2 -2
- package/dist/index.mjs +2 -2
- package/dist/{instance-BJVnjk4r.mjs → instance--1tqj7s5.mjs} +2 -2
- package/dist/{instance-BJVnjk4r.mjs.map → instance--1tqj7s5.mjs.map} +1 -1
- package/dist/{instance-aPJrTyvW.js → instance-4DgSj55G.js} +2 -2
- package/dist/{instance-aPJrTyvW.js.map → instance-4DgSj55G.js.map} +1 -1
- package/dist/{instance-1BgEld17.d.ts → instance-CrdktIu7.d.ts} +2 -2
- package/dist/{instance-DDvZiq1L.d.mts → instance-Gv9IDCIY.d.mts} +2 -2
- package/dist/internal.d.mts +2 -2
- package/dist/internal.d.ts +2 -2
- package/dist/internal.js +1 -1
- package/dist/internal.mjs +1 -1
- package/dist/{platformAccounts-C1sFv82e.mjs → platformAccounts-3jM3W1fB.mjs} +4 -3
- package/dist/{platformAccounts-C1sFv82e.mjs.map → platformAccounts-3jM3W1fB.mjs.map} +1 -1
- package/dist/{platformAccounts-CqMSpl3X.js → platformAccounts-BcLoaZZb.js} +4 -3
- package/dist/{platformAccounts-CqMSpl3X.js.map → platformAccounts-BcLoaZZb.js.map} +1 -1
- package/dist/polkadot.d.mts +1 -1
- package/dist/polkadot.d.ts +1 -1
- package/dist/polkadot.js +6 -4
- package/dist/polkadot.js.map +1 -1
- package/dist/polkadot.mjs +6 -4
- package/dist/polkadot.mjs.map +1 -1
- package/dist/solana.d.mts +1 -1
- package/dist/solana.d.ts +1 -1
- package/dist/solana.js +1 -1
- package/dist/solana.mjs +1 -1
- package/dist/testing.d.mts +2 -2
- package/dist/testing.d.ts +2 -2
- package/dist/{types-BGeGUtxz.d.mts → types-C9aw2FEm.d.mts} +2 -1
- package/dist/{types-BGeGUtxz.d.ts → types-C9aw2FEm.d.ts} +2 -1
- package/dist/types-DaRMgleZ.js.map +1 -1
- package/dist/types-DcpmEGhO.mjs.map +1 -1
- package/dist/walletconnect.d.mts +1 -1
- package/dist/walletconnect.d.ts +1 -1
- package/dist/walletconnect.js +2 -2
- package/dist/walletconnect.js.map +1 -1
- package/dist/walletconnect.mjs +2 -2
- package/dist/walletconnect.mjs.map +1 -1
- package/package.json +1 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"walletconnect.js","names":["WALLET_CONNECT_WALLET_ID","BehaviorSubject","toConnectError","map","distinctUntilChanged","tap","Observable","of","from","shareReplay","switchMap"],"sources":["../src/api/walletconnect/plugin.ts"],"sourcesContent":["import type { AppKit } from \"@reown/appkit/core\";\nimport {\n\tBehaviorSubject,\n\tdistinctUntilChanged,\n\tfrom,\n\tmap,\n\tObservable,\n\tof,\n\tshareReplay,\n\tswitchMap,\n\ttap,\n} from \"rxjs\";\nimport { clearCachedObservablesByPrefix } from \"../../utils/getCachedObservable\";\nimport { WALLET_CONNECT_WALLET_ID } from \"../../utils/WalletId\";\nimport { type KheopskitError, toConnectError } from \"../errors\";\nimport type {\n\tAppKitInstance,\n\tWalletConnectMetadata,\n\tWalletConnectPlugin,\n\tWalletConnectWallet,\n\tWalletPlatform,\n} from \"../types\";\n\nexport type WalletConnectOptions = {\n\tprojectId: string;\n\tmetadata: WalletConnectMetadata;\n\t/**\n\t * Networks AppKit should enable. Pass `AppKitNetwork[]` from\n\t * `@reown/appkit/networks` (see\n\t * https://docs.reown.com/advanced/multichain/polkadot/dapp-integration-guide#walletconnect-code%2Fcomponent-setup).\n\t * Loosely typed (`unknown`) so the declarations don't depend on\n\t * `@reown/appkit`'s types; the value is forwarded to AppKit as-is.\n\t */\n\tnetworks: [unknown, ...unknown[]];\n\tthemeMode?: \"light\" | \"dark\";\n\tthemeVariables?: Record<string, string | number>;\n\t/**\n\t * Extra Reown AppKit `createAppKit` options (e.g. `featuredWalletIds`,\n\t * `features`, `allWallets`), forwarded as-is; see\n\t * https://docs.reown.com/appkit/javascript/core/options.\n\t * kheopskit sets `projectId`, `metadata`, `networks`, `themeMode`,\n\t * `themeVariables` and `universalProviderConfigOverride` itself, so those\n\t * keys are rejected here.\n\t */\n\tappKitOptions?: AppKitOptions;\n};\n\ntype KheopskitOwnedAppKitKey =\n\t| \"projectId\"\n\t| \"metadata\"\n\t| \"networks\"\n\t| \"themeMode\"\n\t| \"themeVariables\"\n\t| \"universalProviderConfigOverride\";\n\ntype AppKitOptions = Record<string, unknown> &\n\tPartial<Record<KheopskitOwnedAppKitKey, never>>;\n\n/**\n * Dynamically import @reown/appkit/core to avoid loading browser-only code\n * during SSR or in edge runtimes like Cloudflare Workers.\n * The AppKit library uses Lit for web components which requires document.\n */\nconst loadAppKit = async () => {\n\ttry {\n\t\tconst { createAppKit } = await import(\"@reown/appkit/core\");\n\t\treturn createAppKit;\n\t} catch (cause) {\n\t\tconsole.error(\n\t\t\t\"[kheopskit] WalletConnect is configured but @reown/appkit could not be loaded. \" +\n\t\t\t\t\"Install it with `pnpm add @reown/appkit` (or remove config.walletConnect). \" +\n\t\t\t\t\"WalletConnect wallets are disabled; injected wallets still work.\",\n\t\t\tcause,\n\t\t);\n\t\treturn null;\n\t}\n};\n\nconst WALLET_CONNECT_ICON =\n\t\"data:image/svg+xml;base64,PHN2ZyBmaWxsPSJub25lIiBoZWlnaHQ9IjQwMCIgdmlld0JveD0iMCAwIDQwMCA0MDAiIHdpZHRoPSI0MDAiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZ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2IDcuNzQ4MSAwbDUwLjE3ODkgNDguOTMyYy41MzUuNTIyIDEuNDAyLjUyMiAxLjkzNyAwbDUwLjE3Ny00OC45MzJjMi4xMzktMi4wODcgNS42MDgtMi4wODcgNy43NDggMGw1MC4xNzkgNDguOTMyYy41MzUuNTIyIDEuNDAyLjUyMiAxLjkzNyAwbDUwLjE3OS00OC45MzFjMi4xMzktMi4wODcgNS42MDgtMi4wODcgNy43NDggMHoiIGZpbGw9IiNmZmYiLz48L2c+PC9zdmc+\";\n\n// Once it exists, the appKit object should never be recreated. Anchored on\n// globalThis so it stays a single instance even if this module is duplicated\n// across bundle chunks (e.g. CJS subpath entries).\nconst APPKIT_SYMBOL = Symbol.for(\"kheopskit.cachedAppKit\");\n// The projectId AppKit was first initialised with. AppKit is a process-wide\n// singleton, so a later call with a different WalletConnect config is silently\n// ignored — we keep this to warn loudly instead of letting it pass unnoticed.\nconst APPKIT_PROJECT_ID_SYMBOL = Symbol.for(\"kheopskit.cachedAppKitProjectId\");\ntype AppKitGlobal = Record<\n\tsymbol,\n\tObservable<WalletConnectWallet | null> | string | undefined\n>;\nconst getCachedAppKit = ():\n\t| Observable<WalletConnectWallet | null>\n\t| undefined =>\n\t(globalThis as unknown as AppKitGlobal)[APPKIT_SYMBOL] as\n\t\t| Observable<WalletConnectWallet | null>\n\t\t| undefined;\nconst getCachedAppKitProjectId = (): string | undefined =>\n\t(globalThis as unknown as AppKitGlobal)[APPKIT_PROJECT_ID_SYMBOL] as\n\t\t| string\n\t\t| undefined;\nconst setCachedAppKit = (\n\tvalue: Observable<WalletConnectWallet | null> | undefined,\n\tprojectId?: string,\n): void => {\n\t(globalThis as unknown as AppKitGlobal)[APPKIT_SYMBOL] = value;\n\t(globalThis as unknown as AppKitGlobal)[APPKIT_PROJECT_ID_SYMBOL] = projectId;\n};\n\n/**\n * Clears the cached AppKit observable.\n * Use when configuration changes or for testing purposes.\n * Note: This does NOT destroy the appKit instance created by Reown.\n */\nexport const resetAppKitCache = (): void => {\n\tsetCachedAppKit(undefined);\n};\n\n/**\n * Keys are `accounts:<walletId>:...`; the trailing colon scopes the prefix to\n * this wallet and avoids matching a sibling whose id is a string prefix.\n */\nconst dropAccountsCache = (platform: WalletPlatform): void => {\n\tclearCachedObservablesByPrefix(\n\t\t`accounts:${WALLET_CONNECT_WALLET_ID}:${platform}:`,\n\t);\n};\n\nconst namespaceOf: Record<WalletPlatform, string> = {\n\tpolkadot: \"polkadot\",\n\tethereum: \"eip155\",\n\tsolana: \"solana\",\n};\nconst allPlatforms: WalletPlatform[] = [\"polkadot\", \"ethereum\", \"solana\"];\n\nconst methodsByNamespace: Record<string, string[]> = {\n\tpolkadot: [\"polkadot_signTransaction\", \"polkadot_signMessage\"],\n\tsolana: [\n\t\t\"solana_signTransaction\",\n\t\t\"solana_signMessage\",\n\t\t\"solana_signAndSendTransaction\",\n\t],\n};\n\n// AppKit turns every overridden namespace into a pairing request, and rejects\n// the pairing when one of them has no chains.\nconst methodsFor = (networks: unknown[]): Record<string, string[]> => {\n\tconst namespaces = new Set(\n\t\tnetworks.map(\n\t\t\t(network) => (network as { chainNamespace?: string }).chainNamespace,\n\t\t),\n\t);\n\treturn Object.fromEntries(\n\t\tObject.entries(methodsByNamespace).filter(([namespace]) =>\n\t\t\tnamespaces.has(namespace),\n\t\t),\n\t);\n};\n\nconst createConnector$ = (appKit: AppKit): Observable<WalletConnectWallet> => {\n\t// Exposed on the wallet as the (decoupled) AppKitInstance escape hatch.\n\tconst appKitInstance = appKit as unknown as AppKitInstance;\n\n\tconst status$ = new BehaviorSubject<{\n\t\tisPolkadotConnected: boolean;\n\t\tisEthereumConnected: boolean;\n\t\tisSolanaConnected: boolean;\n\t\topening: boolean;\n\t\terror: KheopskitError | undefined;\n\t}>({\n\t\tisPolkadotConnected: false,\n\t\tisEthereumConnected: false,\n\t\tisSolanaConnected: false,\n\t\topening: false,\n\t\terror: undefined,\n\t});\n\n\tconst syncProviders = (providers: Partial<Record<string, unknown>>) => {\n\t\tstatus$.next({\n\t\t\t...status$.value,\n\t\t\tisPolkadotConnected: !!providers.polkadot,\n\t\t\tisEthereumConnected: !!providers.eip155,\n\t\t\tisSolanaConnected: !!providers.solana,\n\t\t});\n\t};\n\n\tconst openModal = async () => {\n\t\tstatus$.next({ ...status$.value, opening: true, error: undefined });\n\t\ttry {\n\t\t\tawait appKit.open();\n\t\t} catch (cause) {\n\t\t\tconst error = toConnectError(\n\t\t\t\tcause,\n\t\t\t\tWALLET_CONNECT_WALLET_ID,\n\t\t\t\t() => false,\n\t\t\t);\n\t\t\tstatus$.next({ ...status$.value, opening: false, error });\n\t\t\tthrow error;\n\t\t}\n\t\tstatus$.next({ ...status$.value, opening: false });\n\t};\n\n\tconst enabledPlatforms = allPlatforms.filter((p) =>\n\t\t(appKit.chainNamespaces as string[]).includes(namespaceOf[p]),\n\t);\n\n\tlet prevConnected = {\n\t\tpolkadot: false,\n\t\tethereum: false,\n\t\tsolana: false,\n\t};\n\n\tconst wallet$ = status$.pipe(\n\t\tmap((s) => ({\n\t\t\tpolkadot: s.isPolkadotConnected,\n\t\t\tethereum: s.isEthereumConnected,\n\t\t\tsolana: s.isSolanaConnected,\n\t\t\topening: s.opening,\n\t\t\terror: s.error,\n\t\t})),\n\t\tdistinctUntilChanged(\n\t\t\t(a, b) =>\n\t\t\t\ta.polkadot === b.polkadot &&\n\t\t\t\ta.ethereum === b.ethereum &&\n\t\t\t\ta.solana === b.solana &&\n\t\t\t\ta.opening === b.opening &&\n\t\t\t\ta.error === b.error,\n\t\t),\n\t\ttap((connected) => {\n\t\t\t// Drop caches for namespaces that just went disconnected.\n\t\t\tfor (const platform of allPlatforms)\n\t\t\t\tif (prevConnected[platform] && !connected[platform])\n\t\t\t\t\tdropAccountsCache(platform);\n\t\t\tprevConnected = connected;\n\t\t}),\n\t\tmap(({ opening, error, ...connected }): WalletConnectWallet => {\n\t\t\tconst platforms = enabledPlatforms.filter((p) => connected[p]);\n\t\t\tconst isConnected = platforms.length > 0;\n\t\t\tconst walletInfo = appKit.getWalletInfo();\n\t\t\treturn {\n\t\t\t\tid: WALLET_CONNECT_WALLET_ID,\n\t\t\t\ttype: \"walletconnect\",\n\t\t\t\tplatforms,\n\t\t\t\tappKit: appKitInstance,\n\t\t\t\tname: walletInfo?.name ?? \"WalletConnect\",\n\t\t\t\ticon: walletInfo?.icon ?? WALLET_CONNECT_ICON,\n\t\t\t\t// One shared session: connecting opens the modal; the wallet\n\t\t\t\t// approves namespaces in that single pairing. Disconnect is\n\t\t\t\t// session-wide; re-pair to change the approved set.\n\t\t\t\tconnect: async () => {\n\t\t\t\t\tif (!isConnected) await openModal();\n\t\t\t\t},\n\t\t\t\tdisconnect: async () => {\n\t\t\t\t\tif (isConnected) await appKit.disconnect();\n\t\t\t\t},\n\t\t\t\tstatus: isConnected\n\t\t\t\t\t? \"connected\"\n\t\t\t\t\t: opening\n\t\t\t\t\t\t? \"connecting\"\n\t\t\t\t\t\t: \"disconnected\",\n\t\t\t\terror,\n\t\t\t\tisConnected,\n\t\t\t};\n\t\t}),\n\t);\n\n\treturn new Observable<WalletConnectWallet>((subscriber) => {\n\t\tfor (const platform of allPlatforms) dropAccountsCache(platform);\n\t\t// AppKit's subscribeProviders only reports later changes, so read the\n\t\t// state that may have changed while nobody observed.\n\t\tsyncProviders({\n\t\t\tpolkadot: appKit.getProvider(\"polkadot\"),\n\t\t\teip155: appKit.getProvider(\"eip155\"),\n\t\t\tsolana: appKit.getProvider(\"solana\"),\n\t\t});\n\t\tconst unsubProviders = appKit.subscribeProviders(syncProviders);\n\t\tconst sub = wallet$.subscribe(subscriber);\n\t\treturn () => {\n\t\t\tsub.unsubscribe();\n\t\t\tunsubProviders();\n\t\t};\n\t});\n};\n\n/**\n * @remarks\n * The AppKit instance is a process-wide singleton: a second instance would\n * share AppKit's module-level controller state with the first, so it is\n * created once. The **first** call with a `walletConnect` config wins;\n * later calls with a *different* `walletConnect` config reuse that first\n * instance. Call {@link resetAppKitCache} before re-initialising if the\n * WalletConnect config must change.\n *\n * One WalletConnect session is shared across every namespace, and a namespace\n * can only be established during the initial pairing — it can't be added to a\n * live session afterwards. So this exposes ONE connector (not one per platform):\n * `connect()` opens the modal; the wallet approves whichever namespaces it\n * supports in that single pairing, and `platforms` reflects what the live\n * session carries. Per-namespace accounts are derived by each platform plugin.\n */\nconst getWalletConnectWallet$ = (\n\twalletConnect: WalletConnectOptions,\n\tdebug: boolean,\n): Observable<WalletConnectWallet | null> => {\n\t// SSR guard - don't try to load AppKit on the server\n\tif (typeof window === \"undefined\") return of(null);\n\n\tconst cachedProjectId = getCachedAppKitProjectId();\n\tif (\n\t\tcachedProjectId !== undefined &&\n\t\tcachedProjectId !== walletConnect.projectId\n\t) {\n\t\tconsole.warn(\n\t\t\t\"[kheopskit] WalletConnect is already initialised with projectId %s; \" +\n\t\t\t\t\"AppKit is a process-wide singleton, so the first configuration wins and \" +\n\t\t\t\t\"projectId %s is ignored. Call resetAppKitCache() before re-initialising \" +\n\t\t\t\t\"if the WalletConnect config must change.\",\n\t\t\tcachedProjectId,\n\t\t\twalletConnect.projectId,\n\t\t);\n\t}\n\n\tlet cachedAppKit = getCachedAppKit();\n\tif (!cachedAppKit) {\n\t\t// Dynamic import avoids loading @reown/appkit at module-eval time (SSR /\n\t\t// edge runtimes like Cloudflare Workers).\n\t\tconst connectorOnce$ = from(loadAppKit()).pipe(\n\t\t\tmap((createAppKit) =>\n\t\t\t\t// @reown/appkit missing (optional peer dep) — degrade gracefully.\n\t\t\t\tcreateAppKit\n\t\t\t\t\t? createConnector$(\n\t\t\t\t\t\t\tcreateAppKit({\n\t\t\t\t\t\t\t\tallWallets: \"HIDE\",\n\t\t\t\t\t\t\t\tdebug,\n\t\t\t\t\t\t\t\tallowUnsupportedChain: true,\n\t\t\t\t\t\t\t\t...walletConnect.appKitOptions,\n\t\t\t\t\t\t\t\tprojectId: walletConnect.projectId,\n\t\t\t\t\t\t\t\tmetadata: walletConnect.metadata,\n\t\t\t\t\t\t\t\tnetworks: walletConnect.networks as never,\n\t\t\t\t\t\t\t\tthemeMode: walletConnect.themeMode,\n\t\t\t\t\t\t\t\tthemeVariables: walletConnect.themeVariables as never,\n\t\t\t\t\t\t\t\tuniversalProviderConfigOverride: {\n\t\t\t\t\t\t\t\t\tmethods: methodsFor(walletConnect.networks),\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t}),\n\t\t\t\t\t\t)\n\t\t\t\t\t: null,\n\t\t\t),\n\t\t\tshareReplay({ refCount: false, bufferSize: 1 }),\n\t\t);\n\t\tcachedAppKit = connectorOnce$.pipe(\n\t\t\tswitchMap((wallet$) => wallet$ ?? of(null)),\n\t\t\tshareReplay({ refCount: true, bufferSize: 1 }),\n\t\t);\n\t\tsetCachedAppKit(cachedAppKit, walletConnect.projectId);\n\t}\n\n\treturn cachedAppKit;\n};\n\n/**\n * The WalletConnect plugin for `config.walletConnect`. Importing this entry is\n * what brings Reown AppKit into a dapp's bundle; a dapp that never imports it\n * ships none of it.\n *\n * @example\n * ```ts\n * import { walletConnect } from \"@kheopskit/core/walletconnect\";\n *\n * createKheopskit({\n * platforms: [polkadot()],\n * walletConnect: walletConnect({ projectId, metadata, networks }),\n * });\n * ```\n */\nexport const walletConnect = (\n\toptions: WalletConnectOptions,\n): WalletConnectPlugin => ({\n\ttype: \"walletconnect\",\n\tmetadata: options.metadata,\n\tgetWallet$: ({ config }) => getWalletConnectWallet$(options, config.debug),\n});\n"],"mappings":";;;;;;;;;;AA+DA,MAAM,aAAa,YAAY;CAC9B,IAAI;EACH,MAAM,EAAE,iBAAiB,MAAM,OAAO;EACtC,OAAO;CACR,SAAS,OAAO;EACf,QAAQ,MACP,8NAGA,KACD;EACA,OAAO;CACR;AACD;AAEA,MAAM,sBACL;AAKD,MAAM,gBAAgB,OAAO,IAAI,wBAAwB;AAIzD,MAAM,2BAA2B,OAAO,IAAI,iCAAiC;AAK7E,MAAM,wBAGJ,WAAuC;AAGzC,MAAM,iCACJ,WAAuC;AAGzC,MAAM,mB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tD;CAEA,OAAO;AACR;;;;;;;;;;;;;;;;AAiBA,MAAa,iBACZ,aAC0B;CAC1B,MAAM;CACN,UAAU,QAAQ;CAClB,aAAa,EAAE,aAAa,wBAAwB,SAAS,OAAO,KAAK;AAC1E"}
|
|
1
|
+
{"version":3,"file":"walletconnect.js","names":["WALLET_CONNECT_WALLET_ID","BehaviorSubject","toConnectError","map","distinctUntilChanged","tap","Observable","of","from","shareReplay","switchMap"],"sources":["../src/api/walletconnect/plugin.ts"],"sourcesContent":["import type { AppKit } from \"@reown/appkit/core\";\nimport {\n\tBehaviorSubject,\n\tdistinctUntilChanged,\n\tfrom,\n\tmap,\n\tObservable,\n\tof,\n\tshareReplay,\n\tswitchMap,\n\ttap,\n} from \"rxjs\";\nimport { clearCachedObservablesByPrefix } from \"../../utils/getCachedObservable\";\nimport { WALLET_CONNECT_WALLET_ID } from \"../../utils/WalletId\";\nimport { type KheopskitError, toConnectError } from \"../errors\";\nimport type {\n\tAppKitInstance,\n\tWalletConnectMetadata,\n\tWalletConnectPlugin,\n\tWalletConnectWallet,\n\tWalletPlatform,\n} from \"../types\";\n\nexport type WalletConnectOptions = {\n\tprojectId: string;\n\tmetadata: WalletConnectMetadata;\n\t/**\n\t * Networks AppKit should enable. Pass `AppKitNetwork[]` from\n\t * `@reown/appkit/networks` (see\n\t * https://docs.reown.com/advanced/multichain/polkadot/dapp-integration-guide#walletconnect-code%2Fcomponent-setup).\n\t * Loosely typed (`unknown`) so the declarations don't depend on\n\t * `@reown/appkit`'s types; the value is forwarded to AppKit as-is.\n\t */\n\tnetworks: [unknown, ...unknown[]];\n\tthemeMode?: \"light\" | \"dark\";\n\tthemeVariables?: Record<string, string | number>;\n\t/**\n\t * Extra Reown AppKit `createAppKit` options (e.g. `featuredWalletIds`,\n\t * `features`, `allWallets`), forwarded as-is; see\n\t * https://docs.reown.com/appkit/javascript/core/options.\n\t * kheopskit sets `projectId`, `metadata`, `networks`, `themeMode`,\n\t * `themeVariables` and `universalProviderConfigOverride` itself, so those\n\t * keys are rejected here.\n\t */\n\tappKitOptions?: AppKitOptions;\n};\n\ntype KheopskitOwnedAppKitKey =\n\t| \"projectId\"\n\t| \"metadata\"\n\t| \"networks\"\n\t| \"themeMode\"\n\t| \"themeVariables\"\n\t| \"universalProviderConfigOverride\";\n\ntype AppKitOptions = Record<string, unknown> &\n\tPartial<Record<KheopskitOwnedAppKitKey, never>>;\n\n/**\n * Dynamically import @reown/appkit/core to avoid loading browser-only code\n * during SSR or in edge runtimes like Cloudflare Workers.\n * The AppKit library uses Lit for web components which requires document.\n */\nconst loadAppKit = async () => {\n\ttry {\n\t\tconst { createAppKit } = await import(\"@reown/appkit/core\");\n\t\treturn createAppKit;\n\t} catch (cause) {\n\t\tconsole.error(\n\t\t\t\"[kheopskit] WalletConnect is configured but @reown/appkit could not be loaded. \" +\n\t\t\t\t\"Install it with `pnpm add @reown/appkit` (or remove config.walletConnect). \" +\n\t\t\t\t\"WalletConnect wallets are disabled; injected wallets still work.\",\n\t\t\tcause,\n\t\t);\n\t\treturn null;\n\t}\n};\n\nconst WALLET_CONNECT_ICON =\n\t\"data:image/svg+xml;base64,PHN2ZyBmaWxsPSJub25lIiBoZWlnaHQ9IjQwMCIgdmlld0JveD0iMCAwIDQwMCA0MDAiIHdpZHRoPSI0MDAiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgeG1sbnM6eGxpbms9Imh0dHA6Ly93d3cudzMub3JnLzE5OTkveGxpbmsiPjxjbGlwUGF0aCBpZD0iYSI+PHBhdGggZD0ibTAgMGg0MDB2NDAwaC00MDB6Ii8+PC9jbGlwUGF0aD48ZyBjbGlwLXBhdGg9InVybCgjYSkiPjxjaXJjbGUgY3g9IjIwMCIgY3k9IjIwMCIgZmlsbD0iIzMzOTZmZiIgcj0iMTk5LjUiIHN0cm9rZT0iIzY2YjFmZiIvPjxwYXRoIGQ9Im0xMjIuNTE5IDE0OC45NjVjNDIuNzkxLTQxLjcyOSAxMTIuMTcxLTQxLjcyOSAxNTQuOTYyIDBsNS4xNSA1LjAyMmMyLjE0IDIuMDg2IDIuMTQgNS40NjkgMCA3LjU1NWwtMTcuNjE3IDE3LjE4Yy0xLjA3IDEuMDQzLTIuODA0IDEuMDQzLTMuODc0IDBsLTcuMDg3LTYuOTExYy0yOS44NTMtMjkuMTExLTc4LjI1My0yOS4xMTEtMTA4LjEwNiAwbC03LjU5IDcuNDAxYy0xLjA3IDEuMDQzLTIuODA0IDEuMDQzLTMuODc0IDBsLTE3LjYxNy0xNy4xOGMtMi4xNC0yLjA4Ni0yLjE0LTUuNDY5IDAtNy41NTV6bTE5MS4zOTcgMzUuNTI5IDE1LjY3OSAxNS4yOWMyLjE0IDIuMDg2IDIuMTQgNS40NjkgMCA3LjU1NWwtNzAuNyA2OC45NDRjLTIuMTM5IDIuMDg3LTUuNjA4IDIuMDg3LTcuNzQ4IDBsLTUwLjE3OC00OC45MzFjLS41MzUtLjUyMi0xLjQwMi0uNTIyLTEuOTM3IDBsLTUwLjE3OCA0OC45MzFjLTIuMTM5IDIuMDg3LTUuNjA4IDIuMDg3LTcuNzQ4IDBsLTcwLjcwMTUtNjguOTQ1Yy0yLjEzOTYtMi4wODYtMi4xMzk2LTUuNDY5IDAtNy41NTVsMTUuNjc5NS0xNS4yOWMyLjEzOTYtMi4wODYgNS42MDg1LTIuMDg2IDcuNzQ4MSAwbDUwLjE3ODkgNDguOTMyYy41MzUuNTIyIDEuNDAyLjUyMiAxLjkzNyAwbDUwLjE3Ny00OC45MzJjMi4xMzktMi4wODcgNS42MDgtMi4wODcgNy43NDggMGw1MC4xNzkgNDguOTMyYy41MzUuNTIyIDEuNDAyLjUyMiAxLjkzNyAwbDUwLjE3OS00OC45MzFjMi4xMzktMi4wODcgNS42MDgtMi4wODcgNy43NDggMHoiIGZpbGw9IiNmZmYiLz48L2c+PC9zdmc+\";\n\n// Once it exists, the appKit object should never be recreated. Anchored on\n// globalThis so it stays a single instance even if this module is duplicated\n// across bundle chunks (e.g. CJS subpath entries).\nconst APPKIT_SYMBOL = Symbol.for(\"kheopskit.cachedAppKit\");\n// The projectId AppKit was first initialised with. AppKit is a process-wide\n// singleton, so a later call with a different WalletConnect config is silently\n// ignored — we keep this to warn loudly instead of letting it pass unnoticed.\nconst APPKIT_PROJECT_ID_SYMBOL = Symbol.for(\"kheopskit.cachedAppKitProjectId\");\ntype AppKitGlobal = Record<\n\tsymbol,\n\tObservable<WalletConnectWallet | null> | string | undefined\n>;\nconst getCachedAppKit = ():\n\t| Observable<WalletConnectWallet | null>\n\t| undefined =>\n\t(globalThis as unknown as AppKitGlobal)[APPKIT_SYMBOL] as\n\t\t| Observable<WalletConnectWallet | null>\n\t\t| undefined;\nconst getCachedAppKitProjectId = (): string | undefined =>\n\t(globalThis as unknown as AppKitGlobal)[APPKIT_PROJECT_ID_SYMBOL] as\n\t\t| string\n\t\t| undefined;\nconst setCachedAppKit = (\n\tvalue: Observable<WalletConnectWallet | null> | undefined,\n\tprojectId?: string,\n): void => {\n\t(globalThis as unknown as AppKitGlobal)[APPKIT_SYMBOL] = value;\n\t(globalThis as unknown as AppKitGlobal)[APPKIT_PROJECT_ID_SYMBOL] = projectId;\n};\n\n/**\n * Clears the cached AppKit observable.\n * Use when configuration changes or for testing purposes.\n * Note: This does NOT destroy the appKit instance created by Reown.\n */\nexport const resetAppKitCache = (): void => {\n\tsetCachedAppKit(undefined);\n};\n\n/**\n * Keys are `accounts:<walletId>:...`; the trailing colon scopes the prefix to\n * this wallet and avoids matching a sibling whose id is a string prefix.\n */\nconst dropAccountsCache = (platform: WalletPlatform): void => {\n\tclearCachedObservablesByPrefix(\n\t\t`accounts:${WALLET_CONNECT_WALLET_ID}:${platform}:`,\n\t);\n};\n\nconst namespaceOf: Record<WalletPlatform, string> = {\n\tpolkadot: \"polkadot\",\n\tethereum: \"eip155\",\n\tsolana: \"solana\",\n};\nconst allPlatforms: WalletPlatform[] = [\"polkadot\", \"ethereum\", \"solana\"];\n\nconst methodsByNamespace: Record<string, string[]> = {\n\tpolkadot: [\"polkadot_signTransaction\", \"polkadot_signMessage\"],\n\tsolana: [\n\t\t\"solana_signTransaction\",\n\t\t\"solana_signMessage\",\n\t\t\"solana_signAndSendTransaction\",\n\t],\n};\n\n// AppKit turns every overridden namespace into a pairing request, and rejects\n// the pairing when one of them has no chains.\nconst methodsFor = (networks: unknown[]): Record<string, string[]> => {\n\tconst namespaces = new Set(\n\t\tnetworks.map(\n\t\t\t(network) => (network as { chainNamespace?: string }).chainNamespace,\n\t\t),\n\t);\n\treturn Object.fromEntries(\n\t\tObject.entries(methodsByNamespace).filter(([namespace]) =>\n\t\t\tnamespaces.has(namespace),\n\t\t),\n\t);\n};\n\nconst createConnector$ = (appKit: AppKit): Observable<WalletConnectWallet> => {\n\t// Exposed on the wallet as the (decoupled) AppKitInstance escape hatch.\n\tconst appKitInstance = appKit as unknown as AppKitInstance;\n\n\tconst status$ = new BehaviorSubject<{\n\t\tisPolkadotConnected: boolean;\n\t\tisEthereumConnected: boolean;\n\t\tisSolanaConnected: boolean;\n\t\topening: boolean;\n\t\terror: KheopskitError | undefined;\n\t}>({\n\t\tisPolkadotConnected: false,\n\t\tisEthereumConnected: false,\n\t\tisSolanaConnected: false,\n\t\topening: false,\n\t\terror: undefined,\n\t});\n\n\tconst syncProviders = (providers: Partial<Record<string, unknown>>) => {\n\t\tstatus$.next({\n\t\t\t...status$.value,\n\t\t\tisPolkadotConnected: !!providers.polkadot,\n\t\t\tisEthereumConnected: !!providers.eip155,\n\t\t\tisSolanaConnected: !!providers.solana,\n\t\t});\n\t};\n\n\tconst openModal = async () => {\n\t\tstatus$.next({ ...status$.value, opening: true, error: undefined });\n\t\ttry {\n\t\t\tawait appKit.open();\n\t\t} catch (cause) {\n\t\t\tconst error = toConnectError(\n\t\t\t\tcause,\n\t\t\t\tWALLET_CONNECT_WALLET_ID,\n\t\t\t\t() => false,\n\t\t\t);\n\t\t\tstatus$.next({ ...status$.value, opening: false, error });\n\t\t\tthrow error;\n\t\t}\n\t\tstatus$.next({ ...status$.value, opening: false });\n\t};\n\n\tconst enabledPlatforms = allPlatforms.filter((p) =>\n\t\t(appKit.chainNamespaces as string[]).includes(namespaceOf[p]),\n\t);\n\n\tlet prevConnected = {\n\t\tpolkadot: false,\n\t\tethereum: false,\n\t\tsolana: false,\n\t};\n\n\tconst wallet$ = status$.pipe(\n\t\tmap((s) => ({\n\t\t\tpolkadot: s.isPolkadotConnected,\n\t\t\tethereum: s.isEthereumConnected,\n\t\t\tsolana: s.isSolanaConnected,\n\t\t\topening: s.opening,\n\t\t\terror: s.error,\n\t\t})),\n\t\tdistinctUntilChanged(\n\t\t\t(a, b) =>\n\t\t\t\ta.polkadot === b.polkadot &&\n\t\t\t\ta.ethereum === b.ethereum &&\n\t\t\t\ta.solana === b.solana &&\n\t\t\t\ta.opening === b.opening &&\n\t\t\t\ta.error === b.error,\n\t\t),\n\t\ttap((connected) => {\n\t\t\t// Drop caches for namespaces that just went disconnected.\n\t\t\tfor (const platform of allPlatforms)\n\t\t\t\tif (prevConnected[platform] && !connected[platform])\n\t\t\t\t\tdropAccountsCache(platform);\n\t\t\tprevConnected = connected;\n\t\t}),\n\t\tmap(({ opening, error, ...connected }): WalletConnectWallet => {\n\t\t\tconst platforms = enabledPlatforms.filter((p) => connected[p]);\n\t\t\tconst isConnected = platforms.length > 0;\n\t\t\tconst walletInfo = appKit.getWalletInfo();\n\t\t\treturn {\n\t\t\t\tid: WALLET_CONNECT_WALLET_ID,\n\t\t\t\ttype: \"walletconnect\",\n\t\t\t\tplatforms,\n\t\t\t\tappKit: appKitInstance,\n\t\t\t\tname: walletInfo?.name || \"WalletConnect\",\n\t\t\t\ticon: walletInfo?.icon || WALLET_CONNECT_ICON,\n\t\t\t\t// One shared session: connecting opens the modal; the wallet\n\t\t\t\t// approves namespaces in that single pairing. Disconnect is\n\t\t\t\t// session-wide; re-pair to change the approved set.\n\t\t\t\tconnect: async () => {\n\t\t\t\t\tif (!isConnected) await openModal();\n\t\t\t\t},\n\t\t\t\tdisconnect: async () => {\n\t\t\t\t\tif (isConnected) await appKit.disconnect();\n\t\t\t\t},\n\t\t\t\tstatus: isConnected\n\t\t\t\t\t? \"connected\"\n\t\t\t\t\t: opening\n\t\t\t\t\t\t? \"connecting\"\n\t\t\t\t\t\t: \"disconnected\",\n\t\t\t\terror,\n\t\t\t\tisConnected,\n\t\t\t};\n\t\t}),\n\t);\n\n\treturn new Observable<WalletConnectWallet>((subscriber) => {\n\t\tfor (const platform of allPlatforms) dropAccountsCache(platform);\n\t\t// AppKit's subscribeProviders only reports later changes, so read the\n\t\t// state that may have changed while nobody observed.\n\t\tsyncProviders({\n\t\t\tpolkadot: appKit.getProvider(\"polkadot\"),\n\t\t\teip155: appKit.getProvider(\"eip155\"),\n\t\t\tsolana: appKit.getProvider(\"solana\"),\n\t\t});\n\t\tconst unsubProviders = appKit.subscribeProviders(syncProviders);\n\t\tconst sub = wallet$.subscribe(subscriber);\n\t\treturn () => {\n\t\t\tsub.unsubscribe();\n\t\t\tunsubProviders();\n\t\t};\n\t});\n};\n\n/**\n * @remarks\n * The AppKit instance is a process-wide singleton: a second instance would\n * share AppKit's module-level controller state with the first, so it is\n * created once. The **first** call with a `walletConnect` config wins;\n * later calls with a *different* `walletConnect` config reuse that first\n * instance. Call {@link resetAppKitCache} before re-initialising if the\n * WalletConnect config must change.\n *\n * One WalletConnect session is shared across every namespace, and a namespace\n * can only be established during the initial pairing — it can't be added to a\n * live session afterwards. So this exposes ONE connector (not one per platform):\n * `connect()` opens the modal; the wallet approves whichever namespaces it\n * supports in that single pairing, and `platforms` reflects what the live\n * session carries. Per-namespace accounts are derived by each platform plugin.\n */\nconst getWalletConnectWallet$ = (\n\twalletConnect: WalletConnectOptions,\n\tdebug: boolean,\n): Observable<WalletConnectWallet | null> => {\n\t// SSR guard - don't try to load AppKit on the server\n\tif (typeof window === \"undefined\") return of(null);\n\n\tconst cachedProjectId = getCachedAppKitProjectId();\n\tif (\n\t\tcachedProjectId !== undefined &&\n\t\tcachedProjectId !== walletConnect.projectId\n\t) {\n\t\tconsole.warn(\n\t\t\t\"[kheopskit] WalletConnect is already initialised with projectId %s; \" +\n\t\t\t\t\"AppKit is a process-wide singleton, so the first configuration wins and \" +\n\t\t\t\t\"projectId %s is ignored. Call resetAppKitCache() before re-initialising \" +\n\t\t\t\t\"if the WalletConnect config must change.\",\n\t\t\tcachedProjectId,\n\t\t\twalletConnect.projectId,\n\t\t);\n\t}\n\n\tlet cachedAppKit = getCachedAppKit();\n\tif (!cachedAppKit) {\n\t\t// Dynamic import avoids loading @reown/appkit at module-eval time (SSR /\n\t\t// edge runtimes like Cloudflare Workers).\n\t\tconst connectorOnce$ = from(loadAppKit()).pipe(\n\t\t\tmap((createAppKit) =>\n\t\t\t\t// @reown/appkit missing (optional peer dep) — degrade gracefully.\n\t\t\t\tcreateAppKit\n\t\t\t\t\t? createConnector$(\n\t\t\t\t\t\t\tcreateAppKit({\n\t\t\t\t\t\t\t\tallWallets: \"HIDE\",\n\t\t\t\t\t\t\t\tdebug,\n\t\t\t\t\t\t\t\tallowUnsupportedChain: true,\n\t\t\t\t\t\t\t\t...walletConnect.appKitOptions,\n\t\t\t\t\t\t\t\tprojectId: walletConnect.projectId,\n\t\t\t\t\t\t\t\tmetadata: walletConnect.metadata,\n\t\t\t\t\t\t\t\tnetworks: walletConnect.networks as never,\n\t\t\t\t\t\t\t\tthemeMode: walletConnect.themeMode,\n\t\t\t\t\t\t\t\tthemeVariables: walletConnect.themeVariables as never,\n\t\t\t\t\t\t\t\tuniversalProviderConfigOverride: {\n\t\t\t\t\t\t\t\t\tmethods: methodsFor(walletConnect.networks),\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t}),\n\t\t\t\t\t\t)\n\t\t\t\t\t: null,\n\t\t\t),\n\t\t\tshareReplay({ refCount: false, bufferSize: 1 }),\n\t\t);\n\t\tcachedAppKit = connectorOnce$.pipe(\n\t\t\tswitchMap((wallet$) => wallet$ ?? of(null)),\n\t\t\tshareReplay({ refCount: true, bufferSize: 1 }),\n\t\t);\n\t\tsetCachedAppKit(cachedAppKit, walletConnect.projectId);\n\t}\n\n\treturn cachedAppKit;\n};\n\n/**\n * The WalletConnect plugin for `config.walletConnect`. Importing this entry is\n * what brings Reown AppKit into a dapp's bundle; a dapp that never imports it\n * ships none of it.\n *\n * @example\n * ```ts\n * import { walletConnect } from \"@kheopskit/core/walletconnect\";\n *\n * createKheopskit({\n * platforms: [polkadot()],\n * walletConnect: walletConnect({ projectId, metadata, networks }),\n * });\n * ```\n */\nexport const walletConnect = (\n\toptions: WalletConnectOptions,\n): WalletConnectPlugin => ({\n\ttype: \"walletconnect\",\n\tmetadata: options.metadata,\n\tgetWallet$: ({ config }) => getWalletConnectWallet$(options, config.debug),\n});\n"],"mappings":";;;;;;;;;;AA+DA,MAAM,aAAa,YAAY;CAC9B,IAAI;EACH,MAAM,EAAE,iBAAiB,MAAM,OAAO;EACtC,OAAO;CACR,SAAS,OAAO;EACf,QAAQ,MACP,8NAGA,KACD;EACA,OAAO;CACR;AACD;AAEA,MAAM,sBACL;AAKD,MAAM,gBAAgB,OAAO,IAAI,wBAAwB;AAIzD,MAAM,2BAA2B,OAAO,IAAI,iCAAiC;AAK7E,MAAM,wBAGJ,WAAuC;AAGzC,MAAM,iCACJ,WAAuC;AAGzC,MAAM,mBACL,OACA,cACU;CACV,WAAwC,iBAAiB;CACzD,WAAwC,4BAA4B;AACrE;;;;;;AAOA,MAAa,yBAA+B;CAC3C,gBAAgB,KAAA,CAAS;AAC1B;;;;;AAMA,MAAM,qBAAqB,aAAmC;CAC7D,cAAA,+BACC,YAAYA,cAAAA,yBAAyB,GAAG,SAAS,EAClD;AACD;AAEA,MAAM,cAA8C;CACnD,UAAU;CACV,UAAU;CACV,QAAQ;AACT;AACA,MAAM,eAAiC;CAAC;CAAY;CAAY;AAAQ;AAExE,MAAM,qBAA+C;CACpD,UAAU,CAAC,4BAA4B,sBAAsB;CAC7D,QAAQ;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GAAOC,KAAAA,GAAAA,CAAG,IAAI;CAEjD,MAAM,kBAAkB,yBAAyB;CACjD,IACC,oBAAoB,KAAA,KACpB,oBAAoB,cAAc,WAElC,QAAQ,KACP,gQAIA,iBACA,cAAc,SACf;CAGD,IAAI,eAAe,gBAAgB;CACnC,IAAI,CAAC,cAAc;EA2BlB,gBAAA,GAxBuBC,KAAAA,KAAAA,CAAK,WAAW,CAAC,CAAC,CAAC,MAAA,GACzCL,KAAAA,IAAAA,EAAK,iBAEJ,eACG,iBACA,aAAa;GACZ,YAAY;GACZ;GACA,uBAAuB;GACvB,GAAG,cAAc;GACjB,WAAW,cAAc;GACzB,UAAU,cAAc;GACxB,UAAU,cAAc;GACxB,WAAW,cAAc;GACzB,gBAAgB,cAAc;GAC9B,iCAAiC,EAChC,SAAS,WAAW,cAAc,QAAQ,EAC3C;EACD,CAAC,CACF,IACC,IACJ,IAAA,GACAM,KAAAA,YAAAA,CAAY;GAAE,UAAU;GAAO,YAAY;EAAE,CAAC,CAEnB,CAAC,CAAC,MAAA,GAC7BC,KAAAA,UAAAA,EAAW,YAAY,YAAA,GAAWH,KAAAA,GAAAA,CAAG,IAAI,CAAC,IAAA,GAC1CE,KAAAA,YAAAA,CAAY;GAAE,UAAU;GAAM,YAAY;EAAE,CAAC,CAC9C;EACA,gBAAgB,cAAc,cAAc,SAAS;CACtD;CAEA,OAAO;AACR;;;;;;;;;;;;;;;;AAiBA,MAAa,iBACZ,aAC0B;CAC1B,MAAM;CACN,UAAU,QAAQ;CAClB,aAAa,EAAE,aAAa,wBAAwB,SAAS,OAAO,KAAK;AAC1E"}
|
package/dist/walletconnect.mjs
CHANGED
|
@@ -125,8 +125,8 @@ const createConnector$ = (appKit) => {
|
|
|
125
125
|
type: "walletconnect",
|
|
126
126
|
platforms,
|
|
127
127
|
appKit: appKitInstance,
|
|
128
|
-
name: walletInfo?.name
|
|
129
|
-
icon: walletInfo?.icon
|
|
128
|
+
name: walletInfo?.name || "WalletConnect",
|
|
129
|
+
icon: walletInfo?.icon || WALLET_CONNECT_ICON,
|
|
130
130
|
connect: async () => {
|
|
131
131
|
if (!isConnected) await openModal();
|
|
132
132
|
},
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"walletconnect.mjs","names":[],"sources":["../src/api/walletconnect/plugin.ts"],"sourcesContent":["import type { AppKit } from \"@reown/appkit/core\";\nimport {\n\tBehaviorSubject,\n\tdistinctUntilChanged,\n\tfrom,\n\tmap,\n\tObservable,\n\tof,\n\tshareReplay,\n\tswitchMap,\n\ttap,\n} from \"rxjs\";\nimport { clearCachedObservablesByPrefix } from \"../../utils/getCachedObservable\";\nimport { WALLET_CONNECT_WALLET_ID } from \"../../utils/WalletId\";\nimport { type KheopskitError, toConnectError } from \"../errors\";\nimport type {\n\tAppKitInstance,\n\tWalletConnectMetadata,\n\tWalletConnectPlugin,\n\tWalletConnectWallet,\n\tWalletPlatform,\n} from \"../types\";\n\nexport type WalletConnectOptions = {\n\tprojectId: string;\n\tmetadata: WalletConnectMetadata;\n\t/**\n\t * Networks AppKit should enable. Pass `AppKitNetwork[]` from\n\t * `@reown/appkit/networks` (see\n\t * https://docs.reown.com/advanced/multichain/polkadot/dapp-integration-guide#walletconnect-code%2Fcomponent-setup).\n\t * Loosely typed (`unknown`) so the declarations don't depend on\n\t * `@reown/appkit`'s types; the value is forwarded to AppKit as-is.\n\t */\n\tnetworks: [unknown, ...unknown[]];\n\tthemeMode?: \"light\" | \"dark\";\n\tthemeVariables?: Record<string, string | number>;\n\t/**\n\t * Extra Reown AppKit `createAppKit` options (e.g. `featuredWalletIds`,\n\t * `features`, `allWallets`), forwarded as-is; see\n\t * https://docs.reown.com/appkit/javascript/core/options.\n\t * kheopskit sets `projectId`, `metadata`, `networks`, `themeMode`,\n\t * `themeVariables` and `universalProviderConfigOverride` itself, so those\n\t * keys are rejected here.\n\t */\n\tappKitOptions?: AppKitOptions;\n};\n\ntype KheopskitOwnedAppKitKey =\n\t| \"projectId\"\n\t| \"metadata\"\n\t| \"networks\"\n\t| \"themeMode\"\n\t| \"themeVariables\"\n\t| \"universalProviderConfigOverride\";\n\ntype AppKitOptions = Record<string, unknown> &\n\tPartial<Record<KheopskitOwnedAppKitKey, never>>;\n\n/**\n * Dynamically import @reown/appkit/core to avoid loading browser-only code\n * during SSR or in edge runtimes like Cloudflare Workers.\n * The AppKit library uses Lit for web components which requires document.\n */\nconst loadAppKit = async () => {\n\ttry {\n\t\tconst { createAppKit } = await import(\"@reown/appkit/core\");\n\t\treturn createAppKit;\n\t} catch (cause) {\n\t\tconsole.error(\n\t\t\t\"[kheopskit] WalletConnect is configured but @reown/appkit could not be loaded. \" +\n\t\t\t\t\"Install it with `pnpm add @reown/appkit` (or remove config.walletConnect). \" +\n\t\t\t\t\"WalletConnect wallets are disabled; injected wallets still work.\",\n\t\t\tcause,\n\t\t);\n\t\treturn null;\n\t}\n};\n\nconst WALLET_CONNECT_ICON =\n\t\"data:image/svg+xml;base64,PHN2ZyBmaWxsPSJub25lIiBoZWlnaHQ9IjQwMCIgdmlld0JveD0iMCAwIDQwMCA0MDAiIHdpZHRoPSI0MDAiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgeG1sbnM6eGxpbms9Imh0dHA6Ly93d3cudzMub3JnLzE5OTkveGxpbmsiPjxjbGlwUGF0aCBpZD0iYSI+PHBhdGggZD0ibTAgMGg0MDB2NDAwaC00MDB6Ii8+PC9jbGlwUGF0aD48ZyBjbGlwLXBhdGg9InVybCgjYSkiPjxjaXJjbGUgY3g9IjIwMCIgY3k9IjIwMCIgZmlsbD0iIzMzOTZmZiIgcj0iMTk5LjUiIHN0cm9rZT0iIzY2YjFmZiIvPjxwYXRoIGQ9Im0xMjIuNTE5IDE0OC45NjVjNDIuNzkxLTQxLjcyOSAxMTIuMTcxLTQxLjcyOSAxNTQuOTYyIDBsNS4xNSA1LjAyMmMyLjE0IDIuMDg2IDIuMTQgNS40NjkgMCA3LjU1NWwtMTcuNjE3IDE3LjE4Yy0xLjA3IDEuMDQzLTIuODA0IDEuMDQzLTMuODc0IDBsLTcuMDg3LTYuOTExYy0yOS44NTMtMjkuMTExLTc4LjI1My0yOS4xMTEtMTA4LjEwNiAwbC03LjU5IDcuNDAxYy0xLjA3IDEuMDQzLTIuODA0IDEuMDQzLTMuODc0IDBsLTE3LjYxNy0xNy4xOGMtMi4xNC0yLjA4Ni0yLjE0LTUuNDY5IDAtNy41NTV6bTE5MS4zOTcgMzUuNTI5IDE1LjY3OSAxNS4yOWMyLjE0IDIuMDg2IDIuMTQgNS40NjkgMCA3LjU1NWwtNzAuNyA2OC45NDRjLTIuMTM5IDIuMDg3LTUuNjA4IDIuMDg3LTcuNzQ4IDBsLTUwLjE3OC00OC45MzFjLS41MzUtLjUyMi0xLjQwMi0uNTIyLTEuOTM3IDBsLTUwLjE3OCA0OC45MzFjLTIuMTM5IDIuMDg3LTUuNjA4IDIuMDg3LTcuNzQ4IDBsLTcwLjcwMTUtNjguOTQ1Yy0yLjEzOTYtMi4wODYtMi4xMzk2LTUuNDY5IDAtNy41NTVsMTUuNjc5NS0xNS4yOWMyLjEzOTYtMi4wODYgNS42MDg1LTIuMDg2IDcuNzQ4MSAwbDUwLjE3ODkgNDguOTMyYy41MzUuNTIyIDEuNDAyLjUyMiAxLjkzNyAwbDUwLjE3Ny00OC45MzJjMi4xMzktMi4wODcgNS42MDgtMi4wODcgNy43NDggMGw1MC4xNzkgNDguOTMyYy41MzUuNTIyIDEuNDAyLjUyMiAxLjkzNyAwbDUwLjE3OS00OC45MzFjMi4xMzktMi4wODcgNS42MDgtMi4wODcgNy43NDggMHoiIGZpbGw9IiNmZmYiLz48L2c+PC9zdmc+\";\n\n// Once it exists, the appKit object should never be recreated. Anchored on\n// globalThis so it stays a single instance even if this module is duplicated\n// across bundle chunks (e.g. CJS subpath entries).\nconst APPKIT_SYMBOL = Symbol.for(\"kheopskit.cachedAppKit\");\n// The projectId AppKit was first initialised with. AppKit is a process-wide\n// singleton, so a later call with a different WalletConnect config is silently\n// ignored — we keep this to warn loudly instead of letting it pass unnoticed.\nconst APPKIT_PROJECT_ID_SYMBOL = Symbol.for(\"kheopskit.cachedAppKitProjectId\");\ntype AppKitGlobal = Record<\n\tsymbol,\n\tObservable<WalletConnectWallet | null> | string | undefined\n>;\nconst getCachedAppKit = ():\n\t| Observable<WalletConnectWallet | null>\n\t| undefined =>\n\t(globalThis as unknown as AppKitGlobal)[APPKIT_SYMBOL] as\n\t\t| Observable<WalletConnectWallet | null>\n\t\t| undefined;\nconst getCachedAppKitProjectId = (): string | undefined =>\n\t(globalThis as unknown as AppKitGlobal)[APPKIT_PROJECT_ID_SYMBOL] as\n\t\t| string\n\t\t| undefined;\nconst setCachedAppKit = (\n\tvalue: Observable<WalletConnectWallet | null> | undefined,\n\tprojectId?: string,\n): void => {\n\t(globalThis as unknown as AppKitGlobal)[APPKIT_SYMBOL] = value;\n\t(globalThis as unknown as AppKitGlobal)[APPKIT_PROJECT_ID_SYMBOL] = projectId;\n};\n\n/**\n * Clears the cached AppKit observable.\n * Use when configuration changes or for testing purposes.\n * Note: This does NOT destroy the appKit instance created by Reown.\n */\nexport const resetAppKitCache = (): void => {\n\tsetCachedAppKit(undefined);\n};\n\n/**\n * Keys are `accounts:<walletId>:...`; the trailing colon scopes the prefix to\n * this wallet and avoids matching a sibling whose id is a string prefix.\n */\nconst dropAccountsCache = (platform: WalletPlatform): void => {\n\tclearCachedObservablesByPrefix(\n\t\t`accounts:${WALLET_CONNECT_WALLET_ID}:${platform}:`,\n\t);\n};\n\nconst namespaceOf: Record<WalletPlatform, string> = {\n\tpolkadot: \"polkadot\",\n\tethereum: \"eip155\",\n\tsolana: \"solana\",\n};\nconst allPlatforms: WalletPlatform[] = [\"polkadot\", \"ethereum\", \"solana\"];\n\nconst methodsByNamespace: Record<string, string[]> = {\n\tpolkadot: [\"polkadot_signTransaction\", \"polkadot_signMessage\"],\n\tsolana: [\n\t\t\"solana_signTransaction\",\n\t\t\"solana_signMessage\",\n\t\t\"solana_signAndSendTransaction\",\n\t],\n};\n\n// AppKit turns every overridden namespace into a pairing request, and rejects\n// the pairing when one of them has no chains.\nconst methodsFor = (networks: unknown[]): Record<string, string[]> => {\n\tconst namespaces = new Set(\n\t\tnetworks.map(\n\t\t\t(network) => (network as { chainNamespace?: string }).chainNamespace,\n\t\t),\n\t);\n\treturn Object.fromEntries(\n\t\tObject.entries(methodsByNamespace).filter(([namespace]) =>\n\t\t\tnamespaces.has(namespace),\n\t\t),\n\t);\n};\n\nconst createConnector$ = (appKit: AppKit): Observable<WalletConnectWallet> => {\n\t// Exposed on the wallet as the (decoupled) AppKitInstance escape hatch.\n\tconst appKitInstance = appKit as unknown as AppKitInstance;\n\n\tconst status$ = new BehaviorSubject<{\n\t\tisPolkadotConnected: boolean;\n\t\tisEthereumConnected: boolean;\n\t\tisSolanaConnected: boolean;\n\t\topening: boolean;\n\t\terror: KheopskitError | undefined;\n\t}>({\n\t\tisPolkadotConnected: false,\n\t\tisEthereumConnected: false,\n\t\tisSolanaConnected: false,\n\t\topening: false,\n\t\terror: undefined,\n\t});\n\n\tconst syncProviders = (providers: Partial<Record<string, unknown>>) => {\n\t\tstatus$.next({\n\t\t\t...status$.value,\n\t\t\tisPolkadotConnected: !!providers.polkadot,\n\t\t\tisEthereumConnected: !!providers.eip155,\n\t\t\tisSolanaConnected: !!providers.solana,\n\t\t});\n\t};\n\n\tconst openModal = async () => {\n\t\tstatus$.next({ ...status$.value, opening: true, error: undefined });\n\t\ttry {\n\t\t\tawait appKit.open();\n\t\t} catch (cause) {\n\t\t\tconst error = toConnectError(\n\t\t\t\tcause,\n\t\t\t\tWALLET_CONNECT_WALLET_ID,\n\t\t\t\t() => false,\n\t\t\t);\n\t\t\tstatus$.next({ ...status$.value, opening: false, error });\n\t\t\tthrow error;\n\t\t}\n\t\tstatus$.next({ ...status$.value, opening: false });\n\t};\n\n\tconst enabledPlatforms = allPlatforms.filter((p) =>\n\t\t(appKit.chainNamespaces as string[]).includes(namespaceOf[p]),\n\t);\n\n\tlet prevConnected = {\n\t\tpolkadot: false,\n\t\tethereum: false,\n\t\tsolana: false,\n\t};\n\n\tconst wallet$ = status$.pipe(\n\t\tmap((s) => ({\n\t\t\tpolkadot: s.isPolkadotConnected,\n\t\t\tethereum: s.isEthereumConnected,\n\t\t\tsolana: s.isSolanaConnected,\n\t\t\topening: s.opening,\n\t\t\terror: s.error,\n\t\t})),\n\t\tdistinctUntilChanged(\n\t\t\t(a, b) =>\n\t\t\t\ta.polkadot === b.polkadot &&\n\t\t\t\ta.ethereum === b.ethereum &&\n\t\t\t\ta.solana === b.solana &&\n\t\t\t\ta.opening === b.opening &&\n\t\t\t\ta.error === b.error,\n\t\t),\n\t\ttap((connected) => {\n\t\t\t// Drop caches for namespaces that just went disconnected.\n\t\t\tfor (const platform of allPlatforms)\n\t\t\t\tif (prevConnected[platform] && !connected[platform])\n\t\t\t\t\tdropAccountsCache(platform);\n\t\t\tprevConnected = connected;\n\t\t}),\n\t\tmap(({ opening, error, ...connected }): WalletConnectWallet => {\n\t\t\tconst platforms = enabledPlatforms.filter((p) => connected[p]);\n\t\t\tconst isConnected = platforms.length > 0;\n\t\t\tconst walletInfo = appKit.getWalletInfo();\n\t\t\treturn {\n\t\t\t\tid: WALLET_CONNECT_WALLET_ID,\n\t\t\t\ttype: \"walletconnect\",\n\t\t\t\tplatforms,\n\t\t\t\tappKit: appKitInstance,\n\t\t\t\tname: walletInfo?.name ?? \"WalletConnect\",\n\t\t\t\ticon: walletInfo?.icon ?? WALLET_CONNECT_ICON,\n\t\t\t\t// One shared session: connecting opens the modal; the wallet\n\t\t\t\t// approves namespaces in that single pairing. Disconnect is\n\t\t\t\t// session-wide; re-pair to change the approved set.\n\t\t\t\tconnect: async () => {\n\t\t\t\t\tif (!isConnected) await openModal();\n\t\t\t\t},\n\t\t\t\tdisconnect: async () => {\n\t\t\t\t\tif (isConnected) await appKit.disconnect();\n\t\t\t\t},\n\t\t\t\tstatus: isConnected\n\t\t\t\t\t? \"connected\"\n\t\t\t\t\t: opening\n\t\t\t\t\t\t? \"connecting\"\n\t\t\t\t\t\t: \"disconnected\",\n\t\t\t\terror,\n\t\t\t\tisConnected,\n\t\t\t};\n\t\t}),\n\t);\n\n\treturn new Observable<WalletConnectWallet>((subscriber) => {\n\t\tfor (const platform of allPlatforms) dropAccountsCache(platform);\n\t\t// AppKit's subscribeProviders only reports later changes, so read the\n\t\t// state that may have changed while nobody observed.\n\t\tsyncProviders({\n\t\t\tpolkadot: appKit.getProvider(\"polkadot\"),\n\t\t\teip155: appKit.getProvider(\"eip155\"),\n\t\t\tsolana: appKit.getProvider(\"solana\"),\n\t\t});\n\t\tconst unsubProviders = appKit.subscribeProviders(syncProviders);\n\t\tconst sub = wallet$.subscribe(subscriber);\n\t\treturn () => {\n\t\t\tsub.unsubscribe();\n\t\t\tunsubProviders();\n\t\t};\n\t});\n};\n\n/**\n * @remarks\n * The AppKit instance is a process-wide singleton: a second instance would\n * share AppKit's module-level controller state with the first, so it is\n * created once. The **first** call with a `walletConnect` config wins;\n * later calls with a *different* `walletConnect` config reuse that first\n * instance. Call {@link resetAppKitCache} before re-initialising if the\n * WalletConnect config must change.\n *\n * One WalletConnect session is shared across every namespace, and a namespace\n * can only be established during the initial pairing — it can't be added to a\n * live session afterwards. So this exposes ONE connector (not one per platform):\n * `connect()` opens the modal; the wallet approves whichever namespaces it\n * supports in that single pairing, and `platforms` reflects what the live\n * session carries. Per-namespace accounts are derived by each platform plugin.\n */\nconst getWalletConnectWallet$ = (\n\twalletConnect: WalletConnectOptions,\n\tdebug: boolean,\n): Observable<WalletConnectWallet | null> => {\n\t// SSR guard - don't try to load AppKit on the server\n\tif (typeof window === \"undefined\") return of(null);\n\n\tconst cachedProjectId = getCachedAppKitProjectId();\n\tif (\n\t\tcachedProjectId !== undefined &&\n\t\tcachedProjectId !== walletConnect.projectId\n\t) {\n\t\tconsole.warn(\n\t\t\t\"[kheopskit] WalletConnect is already initialised with projectId %s; \" +\n\t\t\t\t\"AppKit is a process-wide singleton, so the first configuration wins and \" +\n\t\t\t\t\"projectId %s is ignored. Call resetAppKitCache() before re-initialising \" +\n\t\t\t\t\"if the WalletConnect config must change.\",\n\t\t\tcachedProjectId,\n\t\t\twalletConnect.projectId,\n\t\t);\n\t}\n\n\tlet cachedAppKit = getCachedAppKit();\n\tif (!cachedAppKit) {\n\t\t// Dynamic import avoids loading @reown/appkit at module-eval time (SSR /\n\t\t// edge runtimes like Cloudflare Workers).\n\t\tconst connectorOnce$ = from(loadAppKit()).pipe(\n\t\t\tmap((createAppKit) =>\n\t\t\t\t// @reown/appkit missing (optional peer dep) — degrade gracefully.\n\t\t\t\tcreateAppKit\n\t\t\t\t\t? createConnector$(\n\t\t\t\t\t\t\tcreateAppKit({\n\t\t\t\t\t\t\t\tallWallets: \"HIDE\",\n\t\t\t\t\t\t\t\tdebug,\n\t\t\t\t\t\t\t\tallowUnsupportedChain: true,\n\t\t\t\t\t\t\t\t...walletConnect.appKitOptions,\n\t\t\t\t\t\t\t\tprojectId: walletConnect.projectId,\n\t\t\t\t\t\t\t\tmetadata: walletConnect.metadata,\n\t\t\t\t\t\t\t\tnetworks: walletConnect.networks as never,\n\t\t\t\t\t\t\t\tthemeMode: walletConnect.themeMode,\n\t\t\t\t\t\t\t\tthemeVariables: walletConnect.themeVariables as never,\n\t\t\t\t\t\t\t\tuniversalProviderConfigOverride: {\n\t\t\t\t\t\t\t\t\tmethods: methodsFor(walletConnect.networks),\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t}),\n\t\t\t\t\t\t)\n\t\t\t\t\t: null,\n\t\t\t),\n\t\t\tshareReplay({ refCount: false, bufferSize: 1 }),\n\t\t);\n\t\tcachedAppKit = connectorOnce$.pipe(\n\t\t\tswitchMap((wallet$) => wallet$ ?? of(null)),\n\t\t\tshareReplay({ refCount: true, bufferSize: 1 }),\n\t\t);\n\t\tsetCachedAppKit(cachedAppKit, walletConnect.projectId);\n\t}\n\n\treturn cachedAppKit;\n};\n\n/**\n * The WalletConnect plugin for `config.walletConnect`. Importing this entry is\n * what brings Reown AppKit into a dapp's bundle; a dapp that never imports it\n * ships none of it.\n *\n * @example\n * ```ts\n * import { walletConnect } from \"@kheopskit/core/walletconnect\";\n *\n * createKheopskit({\n * platforms: [polkadot()],\n * walletConnect: walletConnect({ projectId, metadata, networks }),\n * });\n * ```\n */\nexport const walletConnect = (\n\toptions: WalletConnectOptions,\n): WalletConnectPlugin => ({\n\ttype: \"walletconnect\",\n\tmetadata: options.metadata,\n\tgetWallet$: ({ config }) => getWalletConnectWallet$(options, config.debug),\n});\n"],"mappings":";;;;;;;;;AA+DA,MAAM,aAAa,YAAY;CAC9B,IAAI;EACH,MAAM,EAAE,iBAAiB,MAAM,OAAO;EACtC,OAAO;CACR,SAAS,OAAO;EACf,QAAQ,MACP,8NAGA,KACD;EACA,OAAO;CACR;AACD;AAEA,MAAM,sBACL;AAKD,MAAM,gBAAgB,OAAO,IAAI,wBAAwB;AAIzD,MAAM,2BAA2B,OAAO,IAAI,iCAAiC;AAK7E,MAAM,wBAGJ,WAAuC;AAGzC,MAAM,iCACJ,WAAuC;AAGzC,MAAM,mBACL,OACA,cACU;CACV,WAAwC,iBAAiB;CACzD,WAAwC,4BAA4B;AACrE;;;;;;AAOA,MAAa,yBAA+B;CAC3C,gBAAgB,KAAA,CAAS;AAC1B;;;;;AAMA,MAAM,qBAAqB,aAAmC;CAC7D,+BACC,YAAY,yBAAyB,GAAG,SAAS,EAClD;AACD;AAEA,MAAM,cAA8C;CACnD,UAAU;CACV,UAAU;CACV,QAAQ;AACT;AACA,MAAM,eAAiC;CAAC;CAAY;CAAY;AAAQ;AAExE,MAAM,qBAA+C;CACpD,UAAU,CAAC,4BAA4B,sBAAsB;CAC7D,QAAQ;EACP;EACA;EACA;CACD;AACD;AAIA,MAAM,cAAc,aAAkD;CACrE,MAAM,aAAa,IAAI,IACtB,SAAS,KACP,YAAa,QAAwC,cACvD,CACD;CACA,O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eACG,iBACA,aAAa;GACZ,YAAY;GACZ;GACA,uBAAuB;GACvB,GAAG,cAAc;GACjB,WAAW,cAAc;GACzB,UAAU,cAAc;GACxB,UAAU,cAAc;GACxB,WAAW,cAAc;GACzB,gBAAgB,cAAc;GAC9B,iCAAiC,EAChC,SAAS,WAAW,cAAc,QAAQ,EAC3C;EACD,CAAC,CACF,IACC,IACJ,GACA,YAAY;GAAE,UAAU;GAAO,YAAY;EAAE,CAAC,CAEnB,CAAC,CAAC,KAC7B,WAAW,YAAY,WAAW,GAAG,IAAI,CAAC,GAC1C,YAAY;GAAE,UAAU;GAAM,YAAY;EAAE,CAAC,CAC9C;EACA,gBAAgB,cAAc,cAAc,SAAS;CACtD;CAEA,OAAO;AACR;;;;;;;;;;;;;;;;AAiBA,MAAa,iBACZ,aAC0B;CAC1B,MAAM;CACN,UAAU,QAAQ;CAClB,aAAa,EAAE,aAAa,wBAAwB,SAAS,OAAO,KAAK;AAC1E"}
|
|
1
|
+
{"version":3,"file":"walletconnect.mjs","names":[],"sources":["../src/api/walletconnect/plugin.ts"],"sourcesContent":["import type { AppKit } from \"@reown/appkit/core\";\nimport {\n\tBehaviorSubject,\n\tdistinctUntilChanged,\n\tfrom,\n\tmap,\n\tObservable,\n\tof,\n\tshareReplay,\n\tswitchMap,\n\ttap,\n} from \"rxjs\";\nimport { clearCachedObservablesByPrefix } from \"../../utils/getCachedObservable\";\nimport { WALLET_CONNECT_WALLET_ID } from \"../../utils/WalletId\";\nimport { type KheopskitError, toConnectError } from \"../errors\";\nimport type {\n\tAppKitInstance,\n\tWalletConnectMetadata,\n\tWalletConnectPlugin,\n\tWalletConnectWallet,\n\tWalletPlatform,\n} from \"../types\";\n\nexport type WalletConnectOptions = {\n\tprojectId: string;\n\tmetadata: WalletConnectMetadata;\n\t/**\n\t * Networks AppKit should enable. Pass `AppKitNetwork[]` from\n\t * `@reown/appkit/networks` (see\n\t * https://docs.reown.com/advanced/multichain/polkadot/dapp-integration-guide#walletconnect-code%2Fcomponent-setup).\n\t * Loosely typed (`unknown`) so the declarations don't depend on\n\t * `@reown/appkit`'s types; the value is forwarded to AppKit as-is.\n\t */\n\tnetworks: [unknown, ...unknown[]];\n\tthemeMode?: \"light\" | \"dark\";\n\tthemeVariables?: Record<string, string | number>;\n\t/**\n\t * Extra Reown AppKit `createAppKit` options (e.g. `featuredWalletIds`,\n\t * `features`, `allWallets`), forwarded as-is; see\n\t * https://docs.reown.com/appkit/javascript/core/options.\n\t * kheopskit sets `projectId`, `metadata`, `networks`, `themeMode`,\n\t * `themeVariables` and `universalProviderConfigOverride` itself, so those\n\t * keys are rejected here.\n\t */\n\tappKitOptions?: AppKitOptions;\n};\n\ntype KheopskitOwnedAppKitKey =\n\t| \"projectId\"\n\t| \"metadata\"\n\t| \"networks\"\n\t| \"themeMode\"\n\t| \"themeVariables\"\n\t| \"universalProviderConfigOverride\";\n\ntype AppKitOptions = Record<string, unknown> &\n\tPartial<Record<KheopskitOwnedAppKitKey, never>>;\n\n/**\n * Dynamically import @reown/appkit/core to avoid loading browser-only code\n * during SSR or in edge runtimes like Cloudflare Workers.\n * The AppKit library uses Lit for web components which requires document.\n */\nconst loadAppKit = async () => {\n\ttry {\n\t\tconst { createAppKit } = await import(\"@reown/appkit/core\");\n\t\treturn createAppKit;\n\t} catch (cause) {\n\t\tconsole.error(\n\t\t\t\"[kheopskit] WalletConnect is configured but @reown/appkit could not be loaded. \" +\n\t\t\t\t\"Install it with `pnpm add @reown/appkit` (or remove config.walletConnect). \" +\n\t\t\t\t\"WalletConnect wallets are disabled; injected wallets still work.\",\n\t\t\tcause,\n\t\t);\n\t\treturn null;\n\t}\n};\n\nconst WALLET_CONNECT_ICON =\n\t\"data:image/svg+xml;base64,PHN2ZyBmaWxsPSJub25lIiBoZWlnaHQ9IjQwMCIgdmlld0JveD0iMCAwIDQwMCA0MDAiIHdpZHRoPSI0MDAiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgeG1sbnM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ODkgNDguOTMyYy41MzUuNTIyIDEuNDAyLjUyMiAxLjkzNyAwbDUwLjE3Ny00OC45MzJjMi4xMzktMi4wODcgNS42MDgtMi4wODcgNy43NDggMGw1MC4xNzkgNDguOTMyYy41MzUuNTIyIDEuNDAyLjUyMiAxLjkzNyAwbDUwLjE3OS00OC45MzFjMi4xMzktMi4wODcgNS42MDgtMi4wODcgNy43NDggMHoiIGZpbGw9IiNmZmYiLz48L2c+PC9zdmc+\";\n\n// Once it exists, the appKit object should never be recreated. Anchored on\n// globalThis so it stays a single instance even if this module is duplicated\n// across bundle chunks (e.g. CJS subpath entries).\nconst APPKIT_SYMBOL = Symbol.for(\"kheopskit.cachedAppKit\");\n// The projectId AppKit was first initialised with. AppKit is a process-wide\n// singleton, so a later call with a different WalletConnect config is silently\n// ignored — we keep this to warn loudly instead of letting it pass unnoticed.\nconst APPKIT_PROJECT_ID_SYMBOL = Symbol.for(\"kheopskit.cachedAppKitProjectId\");\ntype AppKitGlobal = Record<\n\tsymbol,\n\tObservable<WalletConnectWallet | null> | string | undefined\n>;\nconst getCachedAppKit = ():\n\t| Observable<WalletConnectWallet | null>\n\t| undefined =>\n\t(globalThis as unknown as AppKitGlobal)[APPKIT_SYMBOL] as\n\t\t| Observable<WalletConnectWallet | null>\n\t\t| undefined;\nconst getCachedAppKitProjectId = (): string | undefined =>\n\t(globalThis as unknown as AppKitGlobal)[APPKIT_PROJECT_ID_SYMBOL] as\n\t\t| string\n\t\t| undefined;\nconst setCachedAppKit = (\n\tvalue: Observable<WalletConnectWallet | null> | undefined,\n\tprojectId?: string,\n): void => {\n\t(globalThis as unknown as AppKitGlobal)[APPKIT_SYMBOL] = value;\n\t(globalThis as unknown as AppKitGlobal)[APPKIT_PROJECT_ID_SYMBOL] = projectId;\n};\n\n/**\n * Clears the cached AppKit observable.\n * Use when configuration changes or for testing purposes.\n * Note: This does NOT destroy the appKit instance created by Reown.\n */\nexport const resetAppKitCache = (): void => {\n\tsetCachedAppKit(undefined);\n};\n\n/**\n * Keys are `accounts:<walletId>:...`; the trailing colon scopes the prefix to\n * this wallet and avoids matching a sibling whose id is a string prefix.\n */\nconst dropAccountsCache = (platform: WalletPlatform): void => {\n\tclearCachedObservablesByPrefix(\n\t\t`accounts:${WALLET_CONNECT_WALLET_ID}:${platform}:`,\n\t);\n};\n\nconst namespaceOf: Record<WalletPlatform, string> = {\n\tpolkadot: \"polkadot\",\n\tethereum: \"eip155\",\n\tsolana: \"solana\",\n};\nconst allPlatforms: WalletPlatform[] = [\"polkadot\", \"ethereum\", \"solana\"];\n\nconst methodsByNamespace: Record<string, string[]> = {\n\tpolkadot: [\"polkadot_signTransaction\", \"polkadot_signMessage\"],\n\tsolana: [\n\t\t\"solana_signTransaction\",\n\t\t\"solana_signMessage\",\n\t\t\"solana_signAndSendTransaction\",\n\t],\n};\n\n// AppKit turns every overridden namespace into a pairing request, and rejects\n// the pairing when one of them has no chains.\nconst methodsFor = (networks: unknown[]): Record<string, string[]> => {\n\tconst namespaces = new Set(\n\t\tnetworks.map(\n\t\t\t(network) => (network as { chainNamespace?: string }).chainNamespace,\n\t\t),\n\t);\n\treturn Object.fromEntries(\n\t\tObject.entries(methodsByNamespace).filter(([namespace]) =>\n\t\t\tnamespaces.has(namespace),\n\t\t),\n\t);\n};\n\nconst createConnector$ = (appKit: AppKit): Observable<WalletConnectWallet> => {\n\t// Exposed on the wallet as the (decoupled) AppKitInstance escape hatch.\n\tconst appKitInstance = appKit as unknown as AppKitInstance;\n\n\tconst status$ = new BehaviorSubject<{\n\t\tisPolkadotConnected: boolean;\n\t\tisEthereumConnected: boolean;\n\t\tisSolanaConnected: boolean;\n\t\topening: boolean;\n\t\terror: KheopskitError | undefined;\n\t}>({\n\t\tisPolkadotConnected: false,\n\t\tisEthereumConnected: false,\n\t\tisSolanaConnected: false,\n\t\topening: false,\n\t\terror: undefined,\n\t});\n\n\tconst syncProviders = (providers: Partial<Record<string, unknown>>) => {\n\t\tstatus$.next({\n\t\t\t...status$.value,\n\t\t\tisPolkadotConnected: !!providers.polkadot,\n\t\t\tisEthereumConnected: !!providers.eip155,\n\t\t\tisSolanaConnected: !!providers.solana,\n\t\t});\n\t};\n\n\tconst openModal = async () => {\n\t\tstatus$.next({ ...status$.value, opening: true, error: undefined });\n\t\ttry {\n\t\t\tawait appKit.open();\n\t\t} catch (cause) {\n\t\t\tconst error = toConnectError(\n\t\t\t\tcause,\n\t\t\t\tWALLET_CONNECT_WALLET_ID,\n\t\t\t\t() => false,\n\t\t\t);\n\t\t\tstatus$.next({ ...status$.value, opening: false, error });\n\t\t\tthrow error;\n\t\t}\n\t\tstatus$.next({ ...status$.value, opening: false });\n\t};\n\n\tconst enabledPlatforms = allPlatforms.filter((p) =>\n\t\t(appKit.chainNamespaces as string[]).includes(namespaceOf[p]),\n\t);\n\n\tlet prevConnected = {\n\t\tpolkadot: false,\n\t\tethereum: false,\n\t\tsolana: false,\n\t};\n\n\tconst wallet$ = status$.pipe(\n\t\tmap((s) => ({\n\t\t\tpolkadot: s.isPolkadotConnected,\n\t\t\tethereum: s.isEthereumConnected,\n\t\t\tsolana: s.isSolanaConnected,\n\t\t\topening: s.opening,\n\t\t\terror: s.error,\n\t\t})),\n\t\tdistinctUntilChanged(\n\t\t\t(a, b) =>\n\t\t\t\ta.polkadot === b.polkadot &&\n\t\t\t\ta.ethereum === b.ethereum &&\n\t\t\t\ta.solana === b.solana &&\n\t\t\t\ta.opening === b.opening &&\n\t\t\t\ta.error === b.error,\n\t\t),\n\t\ttap((connected) => {\n\t\t\t// Drop caches for namespaces that just went disconnected.\n\t\t\tfor (const platform of allPlatforms)\n\t\t\t\tif (prevConnected[platform] && !connected[platform])\n\t\t\t\t\tdropAccountsCache(platform);\n\t\t\tprevConnected = connected;\n\t\t}),\n\t\tmap(({ opening, error, ...connected }): WalletConnectWallet => {\n\t\t\tconst platforms = enabledPlatforms.filter((p) => connected[p]);\n\t\t\tconst isConnected = platforms.length > 0;\n\t\t\tconst walletInfo = appKit.getWalletInfo();\n\t\t\treturn {\n\t\t\t\tid: WALLET_CONNECT_WALLET_ID,\n\t\t\t\ttype: \"walletconnect\",\n\t\t\t\tplatforms,\n\t\t\t\tappKit: appKitInstance,\n\t\t\t\tname: walletInfo?.name || \"WalletConnect\",\n\t\t\t\ticon: walletInfo?.icon || WALLET_CONNECT_ICON,\n\t\t\t\t// One shared session: connecting opens the modal; the wallet\n\t\t\t\t// approves namespaces in that single pairing. Disconnect is\n\t\t\t\t// session-wide; re-pair to change the approved set.\n\t\t\t\tconnect: async () => {\n\t\t\t\t\tif (!isConnected) await openModal();\n\t\t\t\t},\n\t\t\t\tdisconnect: async () => {\n\t\t\t\t\tif (isConnected) await appKit.disconnect();\n\t\t\t\t},\n\t\t\t\tstatus: isConnected\n\t\t\t\t\t? \"connected\"\n\t\t\t\t\t: opening\n\t\t\t\t\t\t? \"connecting\"\n\t\t\t\t\t\t: \"disconnected\",\n\t\t\t\terror,\n\t\t\t\tisConnected,\n\t\t\t};\n\t\t}),\n\t);\n\n\treturn new Observable<WalletConnectWallet>((subscriber) => {\n\t\tfor (const platform of allPlatforms) dropAccountsCache(platform);\n\t\t// AppKit's subscribeProviders only reports later changes, so read the\n\t\t// state that may have changed while nobody observed.\n\t\tsyncProviders({\n\t\t\tpolkadot: appKit.getProvider(\"polkadot\"),\n\t\t\teip155: appKit.getProvider(\"eip155\"),\n\t\t\tsolana: appKit.getProvider(\"solana\"),\n\t\t});\n\t\tconst unsubProviders = appKit.subscribeProviders(syncProviders);\n\t\tconst sub = wallet$.subscribe(subscriber);\n\t\treturn () => {\n\t\t\tsub.unsubscribe();\n\t\t\tunsubProviders();\n\t\t};\n\t});\n};\n\n/**\n * @remarks\n * The AppKit instance is a process-wide singleton: a second instance would\n * share AppKit's module-level controller state with the first, so it is\n * created once. The **first** call with a `walletConnect` config wins;\n * later calls with a *different* `walletConnect` config reuse that first\n * instance. Call {@link resetAppKitCache} before re-initialising if the\n * WalletConnect config must change.\n *\n * One WalletConnect session is shared across every namespace, and a namespace\n * can only be established during the initial pairing — it can't be added to a\n * live session afterwards. So this exposes ONE connector (not one per platform):\n * `connect()` opens the modal; the wallet approves whichever namespaces it\n * supports in that single pairing, and `platforms` reflects what the live\n * session carries. Per-namespace accounts are derived by each platform plugin.\n */\nconst getWalletConnectWallet$ = (\n\twalletConnect: WalletConnectOptions,\n\tdebug: boolean,\n): Observable<WalletConnectWallet | null> => {\n\t// SSR guard - don't try to load AppKit on the server\n\tif (typeof window === \"undefined\") return of(null);\n\n\tconst cachedProjectId = getCachedAppKitProjectId();\n\tif (\n\t\tcachedProjectId !== undefined &&\n\t\tcachedProjectId !== walletConnect.projectId\n\t) {\n\t\tconsole.warn(\n\t\t\t\"[kheopskit] WalletConnect is already initialised with projectId %s; \" +\n\t\t\t\t\"AppKit is a process-wide singleton, so the first configuration wins and \" +\n\t\t\t\t\"projectId %s is ignored. Call resetAppKitCache() before re-initialising \" +\n\t\t\t\t\"if the WalletConnect config must change.\",\n\t\t\tcachedProjectId,\n\t\t\twalletConnect.projectId,\n\t\t);\n\t}\n\n\tlet cachedAppKit = getCachedAppKit();\n\tif (!cachedAppKit) {\n\t\t// Dynamic import avoids loading @reown/appkit at module-eval time (SSR /\n\t\t// edge runtimes like Cloudflare Workers).\n\t\tconst connectorOnce$ = from(loadAppKit()).pipe(\n\t\t\tmap((createAppKit) =>\n\t\t\t\t// @reown/appkit missing (optional peer dep) — degrade gracefully.\n\t\t\t\tcreateAppKit\n\t\t\t\t\t? createConnector$(\n\t\t\t\t\t\t\tcreateAppKit({\n\t\t\t\t\t\t\t\tallWallets: \"HIDE\",\n\t\t\t\t\t\t\t\tdebug,\n\t\t\t\t\t\t\t\tallowUnsupportedChain: true,\n\t\t\t\t\t\t\t\t...walletConnect.appKitOptions,\n\t\t\t\t\t\t\t\tprojectId: walletConnect.projectId,\n\t\t\t\t\t\t\t\tmetadata: walletConnect.metadata,\n\t\t\t\t\t\t\t\tnetworks: walletConnect.networks as never,\n\t\t\t\t\t\t\t\tthemeMode: walletConnect.themeMode,\n\t\t\t\t\t\t\t\tthemeVariables: walletConnect.themeVariables as never,\n\t\t\t\t\t\t\t\tuniversalProviderConfigOverride: {\n\t\t\t\t\t\t\t\t\tmethods: methodsFor(walletConnect.networks),\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t}),\n\t\t\t\t\t\t)\n\t\t\t\t\t: null,\n\t\t\t),\n\t\t\tshareReplay({ refCount: false, bufferSize: 1 }),\n\t\t);\n\t\tcachedAppKit = connectorOnce$.pipe(\n\t\t\tswitchMap((wallet$) => wallet$ ?? of(null)),\n\t\t\tshareReplay({ refCount: true, bufferSize: 1 }),\n\t\t);\n\t\tsetCachedAppKit(cachedAppKit, walletConnect.projectId);\n\t}\n\n\treturn cachedAppKit;\n};\n\n/**\n * The WalletConnect plugin for `config.walletConnect`. Importing this entry is\n * what brings Reown AppKit into a dapp's bundle; a dapp that never imports it\n * ships none of it.\n *\n * @example\n * ```ts\n * import { walletConnect } from \"@kheopskit/core/walletconnect\";\n *\n * createKheopskit({\n * platforms: [polkadot()],\n * walletConnect: walletConnect({ projectId, metadata, networks }),\n * });\n * ```\n */\nexport const walletConnect = (\n\toptions: WalletConnectOptions,\n): WalletConnectPlugin => ({\n\ttype: \"walletconnect\",\n\tmetadata: options.metadata,\n\tgetWallet$: ({ config }) => getWalletConnectWallet$(options, config.debug),\n});\n"],"mappings":";;;;;;;;;AA+DA,MAAM,aAAa,YAAY;CAC9B,IAAI;EACH,MAAM,EAAE,iBAAiB,MAAM,OAAO;EACtC,OAAO;CACR,SAAS,OAAO;EACf,QAAQ,MACP,8NAGA,KACD;EACA,OAAO;CACR;AACD;AAEA,MAAM,sBACL;AAKD,MAAM,gBAAgB,OAAO,IAAI,wBAAwB;AAIzD,MAAM,2BAA2B,OAAO,IAAI,iCAAiC;AAK7E,MAAM,wBAGJ,WAAuC;AAGzC,MAAM,iCACJ,WAAuC;AAGzC,MAAM,mBACL,OACA,cACU;CACV,WAAwC,iBAAiB;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AAiB,OAAO,mBAAmB,aAAa;EAC9D,MAAM,MAAM,QAAQ,UAAU,UAAU;EACxC,aAAa;GACZ,IAAI,YAAY;GAChB,eAAe;EAChB;CACD,CAAC;AACF;;;;;;;;;;;;;;;;;AAkBA,MAAM,2BACL,eACA,UAC4C;CAE5C,IAAI,OAAO,WAAW,aAAa,OAAO,GAAG,IAAI;CAEjD,MAAM,kBAAkB,yBAAyB;CACjD,IACC,oBAAoB,KAAA,KACpB,oBAAoB,cAAc,WAElC,QAAQ,KACP,gQAIA,iBACA,cAAc,SACf;CAGD,IAAI,eAAe,gBAAgB;CACnC,IAAI,CAAC,cAAc;EA2BlB,eAxBuB,KAAK,WAAW,CAAC,CAAC,CAAC,KACzC,KAAK,iBAEJ,eACG,iBACA,aAAa;GACZ,YAAY;GACZ;GACA,uBAAuB;GACvB,GAAG,cAAc;GACjB,WAAW,cAAc;GACzB,UAAU,cAAc;GACxB,UAAU,cAAc;GACxB,WAAW,cAAc;GACzB,gBAAgB,cAAc;GAC9B,iCAAiC,EAChC,SAAS,WAAW,cAAc,QAAQ,EAC3C;EACD,CAAC,CACF,IACC,IACJ,GACA,YAAY;GAAE,UAAU;GAAO,YAAY;EAAE,CAAC,CAEnB,CAAC,CAAC,KAC7B,WAAW,YAAY,WAAW,GAAG,IAAI,CAAC,GAC1C,YAAY;GAAE,UAAU;GAAM,YAAY;EAAE,CAAC,CAC9C;EACA,gBAAgB,cAAc,cAAc,SAAS;CACtD;CAEA,OAAO;AACR;;;;;;;;;;;;;;;;AAiBA,MAAa,iBACZ,aAC0B;CAC1B,MAAM;CACN,UAAU,QAAQ;CAClB,aAAa,EAAE,aAAa,wBAAwB,SAAS,OAAO,KAAK;AAC1E"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kheopskit/core",
|
|
3
|
-
"version": "6.0.
|
|
3
|
+
"version": "6.0.3",
|
|
4
4
|
"description": "Framework-agnostic wallet and account discovery for Polkadot, Ethereum and Solana dapps. Upgrading from v5? Read MIGRATING_TO_V6.md, shipped in this package.",
|
|
5
5
|
"private": false,
|
|
6
6
|
"sideEffects": false,
|