@kheopskit/core 6.0.0 → 6.0.2
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 +12 -0
- package/dist/{config-D1jjip6f.js → config-B3TqJq0m.js} +2 -2
- package/dist/{config-D1jjip6f.js.map → config-B3TqJq0m.js.map} +1 -1
- package/dist/{config-Btb40ehu.mjs → config-WwW0zov1.mjs} +2 -2
- package/dist/{config-Btb40ehu.mjs.map → config-WwW0zov1.mjs.map} +1 -1
- package/dist/{errors-DVaVewcH.js → errors-B6Z21vkI.js} +5 -5
- package/dist/{errors-DVaVewcH.js.map → errors-B6Z21vkI.js.map} +1 -1
- package/dist/{errors-BBTNA4Pk.mjs → errors-DszTFbnO.mjs} +5 -5
- package/dist/{errors-BBTNA4Pk.mjs.map → errors-DszTFbnO.mjs.map} +1 -1
- package/dist/ethereum.js +63 -9
- package/dist/ethereum.js.map +1 -1
- package/dist/ethereum.mjs +64 -10
- package/dist/ethereum.mjs.map +1 -1
- package/dist/index.js +3 -3
- package/dist/index.mjs +3 -3
- package/dist/{injectedWallets-Do5__knh.mjs → injectedWallets-CUo7rVqB.mjs} +2 -2
- package/dist/{injectedWallets-Do5__knh.mjs.map → injectedWallets-CUo7rVqB.mjs.map} +1 -1
- package/dist/{injectedWallets-CeYCeVh5.js → injectedWallets-Cfn3caW3.js} +2 -2
- package/dist/{injectedWallets-CeYCeVh5.js.map → injectedWallets-Cfn3caW3.js.map} +1 -1
- package/dist/{instance-DmBE3vtX.mjs → instance-BJVnjk4r.mjs} +3 -3
- package/dist/{instance-DmBE3vtX.mjs.map → instance-BJVnjk4r.mjs.map} +1 -1
- package/dist/{instance-CBN_ifH_.js → instance-aPJrTyvW.js} +3 -3
- package/dist/{instance-CBN_ifH_.js.map → instance-aPJrTyvW.js.map} +1 -1
- package/dist/internal.js +1 -1
- package/dist/internal.mjs +1 -1
- package/dist/polkadot.js +2 -2
- package/dist/polkadot.mjs +2 -2
- package/dist/solana.js +2 -2
- package/dist/solana.mjs +2 -2
- package/dist/testing.js +1 -1
- package/dist/testing.mjs +1 -1
- package/dist/walletconnect.js +1 -1
- package/dist/walletconnect.mjs +1 -1
- package/package.json +3 -3
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,17 @@
|
|
|
1
1
|
# @kheopskit/core
|
|
2
2
|
|
|
3
|
+
## 6.0.2
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- [#196](https://github.com/kheopskit/kheopskit/pull/196) [`843ec8e`](https://github.com/kheopskit/kheopskit/commit/843ec8e8d28d9607eb913c590a6925f12880317d) Thanks [@0xKheops](https://github.com/0xKheops)! - Fixed: an injected Ethereum wallet's accounts and chain show in the same `kheopskit$` emission that shows the wallet connected, on a user connect and on a reload's reconnect, instead of one emission later. A wallet's late `eth_accounts` or `eth_chainId` answer no longer overwrites an `accountsChanged`, `chainChanged` or `connect` event sent after it was asked. A connect no longer waits forever for a wallet that never answers `eth_chainId`: after 2 seconds its accounts show without a chain.
|
|
8
|
+
|
|
9
|
+
## 6.0.1
|
|
10
|
+
|
|
11
|
+
### Patch Changes
|
|
12
|
+
|
|
13
|
+
- [#193](https://github.com/kheopskit/kheopskit/pull/193) [`9b27768`](https://github.com/kheopskit/kheopskit/commit/9b2776849925029cf22c5d2ad4deddc25d68c572) Thanks [@0xKheops](https://github.com/0xKheops)! - Fixed: injected Ethereum accounts take their chain from the EIP-1193 `connect` event, so they no longer report no chain after the wallet disconnects and reconnects the site (for example, Talisman's Disconnect All then Connect All).
|
|
14
|
+
|
|
3
15
|
## 6.0.0
|
|
4
16
|
|
|
5
17
|
### Major Changes
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
const require_errors = require("./errors-
|
|
1
|
+
const require_errors = require("./errors-B6Z21vkI.js");
|
|
2
2
|
//#region src/api/config.ts
|
|
3
3
|
/**
|
|
4
4
|
* Default storage key for persisting wallet connection state.
|
|
@@ -49,4 +49,4 @@ Object.defineProperty(exports, "resolveConfig", {
|
|
|
49
49
|
}
|
|
50
50
|
});
|
|
51
51
|
|
|
52
|
-
//# sourceMappingURL=config-
|
|
52
|
+
//# sourceMappingURL=config-B3TqJq0m.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"config-
|
|
1
|
+
{"version":3,"file":"config-B3TqJq0m.js","names":["KheopskitError"],"sources":["../src/api/config.ts"],"sourcesContent":["import { KheopskitError } from \"./errors\";\nimport type { KheopskitConfig, KheopskitPlatform } from \"./types\";\n\n/**\n * Default storage key for persisting wallet connection state.\n * Can be overridden via config.storageKey to avoid conflicts\n * when running multiple dapps on the same domain.\n */\nexport const DEFAULT_STORAGE_KEY = \"kheopskit\";\n\nexport const DEFAULT_HYDRATION_GRACE_PERIOD_MS = 500;\nexport const DEFAULT_HYDRATION_TIMEOUT_MS = 3000;\n\nconst DEFAULTS = {\n\tautoReconnect: true,\n\tdebug: false,\n\tstorageKey: DEFAULT_STORAGE_KEY,\n\thydrationGracePeriod: DEFAULT_HYDRATION_GRACE_PERIOD_MS,\n\thydrationTimeout: DEFAULT_HYDRATION_TIMEOUT_MS,\n} satisfies Omit<KheopskitConfig, \"platforms\" | \"walletConnect\">;\n\nexport const resolveConfig = <\n\tconst P extends readonly KheopskitPlatform[] = readonly KheopskitPlatform[],\n>(\n\tconfig?: Partial<KheopskitConfig<P>>,\n): KheopskitConfig<P> => {\n\tconst platforms = (config?.platforms ?? []) as P;\n\n\t// Guard the v4 breaking change: platforms are plugin instances, not strings.\n\t// Catches JS callers (no tsc) and gives agents a fix that names the doc.\n\tconst invalidPlatforms = (platforms as readonly unknown[]).filter(\n\t\t(p) =>\n\t\t\ttypeof p !== \"object\" ||\n\t\t\tp === null ||\n\t\t\ttypeof (p as { getWallets$?: unknown }).getWallets$ !== \"function\",\n\t);\n\tif (invalidPlatforms.length > 0) {\n\t\tthrow new Error(\n\t\t\t\"[kheopskit] config.platforms must contain plugin instances created by the \" +\n\t\t\t\t\"per-platform factories (e.g. platforms: [polkadot(), ethereum(), solana()] \" +\n\t\t\t\t'imported from \"@kheopskit/core/<platform>\"). ' +\n\t\t\t\t`Invalid entries: ${JSON.stringify(invalidPlatforms)}. ` +\n\t\t\t\t'String platform names like \"polkadot\" were removed in v4 — see MIGRATING_TO_V4.md.',\n\t\t);\n\t}\n\n\tif (\n\t\tconfig?.walletConnect !== undefined &&\n\t\ttypeof (config.walletConnect as { getWallet$?: unknown } | null)\n\t\t\t?.getWallet$ !== \"function\"\n\t) {\n\t\tthrow new KheopskitError(\n\t\t\t\"INVALID_CONFIG\",\n\t\t\t\"config.walletConnect must be created by walletConnect() imported from \" +\n\t\t\t\t'\"@kheopskit/core/walletconnect\", e.g. walletConnect: walletConnect({ projectId, metadata, networks }). ' +\n\t\t\t\t\"Raw WalletConnect options were removed in v6, see MIGRATING_TO_V6.md.\",\n\t\t);\n\t}\n\n\tif (platforms.length === 0) {\n\t\tconsole.warn(\n\t\t\t\"[kheopskit] No platforms configured; wallets and accounts will be empty. \" +\n\t\t\t\t'Pass e.g. platforms: [polkadot()] from \"@kheopskit/core/polkadot\".',\n\t\t);\n\t}\n\n\treturn Object.assign({}, DEFAULTS, config, { platforms });\n};\n"],"mappings":";;;;;;;AAQA,MAAa,sBAAsB;AAEnC,MAAa,oCAAoC;AACjD,MAAa,+BAA+B;AAE5C,MAAM,WAAW;CAChB,eAAe;CACf,OAAO;CACP,YAAY;CACZ,sBAAA;CACA,kBAAkB;AACnB;AAEA,MAAa,iBAGZ,WACwB;CACxB,MAAM,YAAa,QAAQ,aAAa,CAAC;CAIzC,MAAM,mBAAoB,UAAiC,QACzD,MACA,OAAO,MAAM,YACb,MAAM,QACN,OAAQ,EAAgC,gBAAgB,UAC1D;CACA,IAAI,iBAAiB,SAAS,GAC7B,MAAM,IAAI,MACT,sNAGqB,KAAK,UAAU,gBAAgB,EAAE,qFAEvD;CAGD,IACC,QAAQ,kBAAkB,KAAA,KAC1B,OAAQ,OAAO,eACZ,eAAe,YAElB,MAAM,IAAIA,eAAAA,eACT,kBACA,sPAGD;CAGD,IAAI,UAAU,WAAW,GACxB,QAAQ,KACP,+IAED;CAGD,OAAO,OAAO,OAAO,CAAC,GAAG,UAAU,QAAQ,EAAE,UAAU,CAAC;AACzD"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { t as KheopskitError } from "./errors-
|
|
1
|
+
import { t as KheopskitError } from "./errors-DszTFbnO.mjs";
|
|
2
2
|
//#region src/api/config.ts
|
|
3
3
|
/**
|
|
4
4
|
* Default storage key for persisting wallet connection state.
|
|
@@ -26,4 +26,4 @@ const resolveConfig = (config) => {
|
|
|
26
26
|
//#endregion
|
|
27
27
|
export { resolveConfig as i, DEFAULT_HYDRATION_TIMEOUT_MS as n, DEFAULT_STORAGE_KEY as r, DEFAULT_HYDRATION_GRACE_PERIOD_MS as t };
|
|
28
28
|
|
|
29
|
-
//# sourceMappingURL=config-
|
|
29
|
+
//# sourceMappingURL=config-WwW0zov1.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"config-
|
|
1
|
+
{"version":3,"file":"config-WwW0zov1.mjs","names":[],"sources":["../src/api/config.ts"],"sourcesContent":["import { KheopskitError } from \"./errors\";\nimport type { KheopskitConfig, KheopskitPlatform } from \"./types\";\n\n/**\n * Default storage key for persisting wallet connection state.\n * Can be overridden via config.storageKey to avoid conflicts\n * when running multiple dapps on the same domain.\n */\nexport const DEFAULT_STORAGE_KEY = \"kheopskit\";\n\nexport const DEFAULT_HYDRATION_GRACE_PERIOD_MS = 500;\nexport const DEFAULT_HYDRATION_TIMEOUT_MS = 3000;\n\nconst DEFAULTS = {\n\tautoReconnect: true,\n\tdebug: false,\n\tstorageKey: DEFAULT_STORAGE_KEY,\n\thydrationGracePeriod: DEFAULT_HYDRATION_GRACE_PERIOD_MS,\n\thydrationTimeout: DEFAULT_HYDRATION_TIMEOUT_MS,\n} satisfies Omit<KheopskitConfig, \"platforms\" | \"walletConnect\">;\n\nexport const resolveConfig = <\n\tconst P extends readonly KheopskitPlatform[] = readonly KheopskitPlatform[],\n>(\n\tconfig?: Partial<KheopskitConfig<P>>,\n): KheopskitConfig<P> => {\n\tconst platforms = (config?.platforms ?? []) as P;\n\n\t// Guard the v4 breaking change: platforms are plugin instances, not strings.\n\t// Catches JS callers (no tsc) and gives agents a fix that names the doc.\n\tconst invalidPlatforms = (platforms as readonly unknown[]).filter(\n\t\t(p) =>\n\t\t\ttypeof p !== \"object\" ||\n\t\t\tp === null ||\n\t\t\ttypeof (p as { getWallets$?: unknown }).getWallets$ !== \"function\",\n\t);\n\tif (invalidPlatforms.length > 0) {\n\t\tthrow new Error(\n\t\t\t\"[kheopskit] config.platforms must contain plugin instances created by the \" +\n\t\t\t\t\"per-platform factories (e.g. platforms: [polkadot(), ethereum(), solana()] \" +\n\t\t\t\t'imported from \"@kheopskit/core/<platform>\"). ' +\n\t\t\t\t`Invalid entries: ${JSON.stringify(invalidPlatforms)}. ` +\n\t\t\t\t'String platform names like \"polkadot\" were removed in v4 — see MIGRATING_TO_V4.md.',\n\t\t);\n\t}\n\n\tif (\n\t\tconfig?.walletConnect !== undefined &&\n\t\ttypeof (config.walletConnect as { getWallet$?: unknown } | null)\n\t\t\t?.getWallet$ !== \"function\"\n\t) {\n\t\tthrow new KheopskitError(\n\t\t\t\"INVALID_CONFIG\",\n\t\t\t\"config.walletConnect must be created by walletConnect() imported from \" +\n\t\t\t\t'\"@kheopskit/core/walletconnect\", e.g. walletConnect: walletConnect({ projectId, metadata, networks }). ' +\n\t\t\t\t\"Raw WalletConnect options were removed in v6, see MIGRATING_TO_V6.md.\",\n\t\t);\n\t}\n\n\tif (platforms.length === 0) {\n\t\tconsole.warn(\n\t\t\t\"[kheopskit] No platforms configured; wallets and accounts will be empty. \" +\n\t\t\t\t'Pass e.g. platforms: [polkadot()] from \"@kheopskit/core/polkadot\".',\n\t\t);\n\t}\n\n\treturn Object.assign({}, DEFAULTS, config, { platforms });\n};\n"],"mappings":";;;;;;;AAQA,MAAa,sBAAsB;AAEnC,MAAa,oCAAoC;AACjD,MAAa,+BAA+B;AAE5C,MAAM,WAAW;CAChB,eAAe;CACf,OAAO;CACP,YAAY;CACZ,sBAAA;CACA,kBAAkB;AACnB;AAEA,MAAa,iBAGZ,WACwB;CACxB,MAAM,YAAa,QAAQ,aAAa,CAAC;CAIzC,MAAM,mBAAoB,UAAiC,QACzD,MACA,OAAO,MAAM,YACb,MAAM,QACN,OAAQ,EAAgC,gBAAgB,UAC1D;CACA,IAAI,iBAAiB,SAAS,GAC7B,MAAM,IAAI,MACT,sNAGqB,KAAK,UAAU,gBAAgB,EAAE,qFAEvD;CAGD,IACC,QAAQ,kBAAkB,KAAA,KAC1B,OAAQ,OAAO,eACZ,eAAe,YAElB,MAAM,IAAI,eACT,kBACA,sPAGD;CAGD,IAAI,UAAU,WAAW,GACxB,QAAQ,KACP,+IAED;CAGD,OAAO,OAAO,OAAO,CAAC,GAAG,UAAU,QAAQ,EAAE,UAAU,CAAC;AACzD"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
//#region \0@oxc-project+runtime@0.
|
|
1
|
+
//#region \0@oxc-project+runtime@0.154.0/helpers/esm/typeof.js
|
|
2
2
|
function _typeof(o) {
|
|
3
3
|
"@babel/helpers - typeof";
|
|
4
4
|
return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function(o) {
|
|
@@ -8,7 +8,7 @@ function _typeof(o) {
|
|
|
8
8
|
}, _typeof(o);
|
|
9
9
|
}
|
|
10
10
|
//#endregion
|
|
11
|
-
//#region \0@oxc-project+runtime@0.
|
|
11
|
+
//#region \0@oxc-project+runtime@0.154.0/helpers/esm/toPrimitive.js
|
|
12
12
|
function toPrimitive(t, r) {
|
|
13
13
|
if ("object" != _typeof(t) || !t) return t;
|
|
14
14
|
var e = t[Symbol.toPrimitive];
|
|
@@ -20,13 +20,13 @@ function toPrimitive(t, r) {
|
|
|
20
20
|
return ("string" === r ? String : Number)(t);
|
|
21
21
|
}
|
|
22
22
|
//#endregion
|
|
23
|
-
//#region \0@oxc-project+runtime@0.
|
|
23
|
+
//#region \0@oxc-project+runtime@0.154.0/helpers/esm/toPropertyKey.js
|
|
24
24
|
function toPropertyKey(t) {
|
|
25
25
|
var i = toPrimitive(t, "string");
|
|
26
26
|
return "symbol" == _typeof(i) ? i : i + "";
|
|
27
27
|
}
|
|
28
28
|
//#endregion
|
|
29
|
-
//#region \0@oxc-project+runtime@0.
|
|
29
|
+
//#region \0@oxc-project+runtime@0.154.0/helpers/esm/defineProperty.js
|
|
30
30
|
function _defineProperty(e, r, t) {
|
|
31
31
|
return (r = toPropertyKey(r)) in e ? Object.defineProperty(e, r, {
|
|
32
32
|
value: t,
|
|
@@ -153,4 +153,4 @@ Object.defineProperty(exports, "walletPendingError", {
|
|
|
153
153
|
}
|
|
154
154
|
});
|
|
155
155
|
|
|
156
|
-
//# sourceMappingURL=errors-
|
|
156
|
+
//# sourceMappingURL=errors-B6Z21vkI.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"errors-
|
|
1
|
+
{"version":3,"file":"errors-B6Z21vkI.js","names":[],"sources":["../src/api/errors.ts"],"sourcesContent":["import type { WalletAccountId } from \"../utils/WalletAccountId\";\nimport type { WalletId } from \"../utils/WalletId\";\n\n/**\n * Stable error codes thrown by kheopskit. Catch a {@link KheopskitError} and\n * branch on `error.code` instead of matching message strings.\n */\nexport type KheopskitErrorCode =\n\t/**\n\t * This `connect()` or `disconnect()` call was abandoned. For `connect()`:\n\t * its `signal` fired before the platform answered, `disconnect()` was\n\t * called while it pended, its wallet disappeared, or the last `kheopskit$`\n\t * subscriber unsubscribed.\n\t * For either call on an injected wallet: the wallet object came from a\n\t * subscription that had already ended, in which case the call rejects\n\t * without asking the platform and without changing the remembered list. A\n\t * placeholder's `connect()` rejects the same way, its `disconnect()`\n\t * resolves, and the WalletConnect connector still calls AppKit.\n\t */\n\t| \"ABORTED\"\n\t/** The user refused the wallet's connect prompt. `cause` holds the platform error. */\n\t| \"USER_REJECTED\"\n\t/** The wallet's connect call failed for another reason. `cause` holds the platform error. */\n\t| \"CONNECT_FAILED\"\n\t/**\n\t * `connect()` was called on a placeholder wallet restored from the cache:\n\t * on `kheopskit$`, hydration ended without kheopskit discovering that\n\t * wallet; on `getHydratedSnapshot` and the React provider's initial\n\t * snapshot, always.\n\t */\n\t| \"WALLET_PENDING\"\n\t/**\n\t * `waitForAccount()` found the account's wallet settled without listing it\n\t * as a live account, or not connected.\n\t */\n\t| \"ACCOUNT_UNAVAILABLE\"\n\t/** The wallet does not advertise a capability kheopskit needs to proceed. */\n\t| \"FEATURE_NOT_SUPPORTED\"\n\t/** No active WalletConnect session for the requested operation. */\n\t| \"NO_SESSION\"\n\t/** No provider available for the requested namespace. */\n\t| \"NO_PROVIDER\"\n\t/** The installed polkadot-api version exposes neither `getTxCreatorFromPjs` (v3) nor `getPolkadotSignerFromPjs` (v2). */\n\t| \"UNSUPPORTED_VERSION\"\n\t/** The requested chain cannot be used for this operation (e.g. a Solana cluster with no CAIP-2 id over WalletConnect). */\n\t| \"UNSUPPORTED_CHAIN\"\n\t/** `createKheopskit` received a config value it cannot use. The message names the fix. */\n\t| \"INVALID_CONFIG\";\n\n/**\n * Error thrown by kheopskit wallet/account operations. Carries a stable\n * {@link KheopskitErrorCode} and, when relevant, the offending wallet id.\n *\n * @example\n * ```ts\n * try {\n * await wallet.connect();\n * } catch (error) {\n * if (error instanceof KheopskitError && error.code === \"USER_REJECTED\") {\n * // the user closed or refused the wallet prompt\n * } else throw error;\n * }\n * ```\n */\nexport class KheopskitError extends Error {\n\treadonly code: KheopskitErrorCode;\n\t/** The wallet id this error relates to, when applicable. */\n\treadonly walletId?: WalletId;\n\n\tconstructor(\n\t\tcode: KheopskitErrorCode,\n\t\tmessage: string,\n\t\toptions?: { walletId?: WalletId; cause?: unknown },\n\t) {\n\t\tsuper(`[kheopskit] ${message}`, { cause: options?.cause });\n\t\tthis.name = \"KheopskitError\";\n\t\tthis.code = code;\n\t\tthis.walletId = options?.walletId;\n\t}\n}\n\nexport const abortedError = (\n\twalletId: WalletId,\n\toperation: \"connect\" | \"disconnect\",\n): KheopskitError =>\n\tnew KheopskitError(\n\t\t\"ABORTED\",\n\t\t`${operation} of wallet ${walletId} was aborted`,\n\t\t{ walletId },\n\t);\n\nexport const walletPendingError = (walletId: WalletId): KheopskitError =>\n\tnew KheopskitError(\n\t\t\"WALLET_PENDING\",\n\t\t`wallet ${walletId} was not discovered before hydration ended`,\n\t\t{ walletId },\n\t);\n\nexport const accountUnavailableError = (\n\taccountId: WalletAccountId,\n\twalletId: WalletId,\n): KheopskitError =>\n\tnew KheopskitError(\n\t\t\"ACCOUNT_UNAVAILABLE\",\n\t\t`wallet ${walletId} settled without listing account ${accountId}`,\n\t\t{ walletId },\n\t);\n\nexport const toConnectError = (\n\terror: unknown,\n\twalletId: WalletId,\n\tisUserRejection: (error: unknown) => boolean,\n): KheopskitError => {\n\tif (error instanceof KheopskitError) return error;\n\treturn isUserRejection(error)\n\t\t? new KheopskitError(\n\t\t\t\t\"USER_REJECTED\",\n\t\t\t\t`the user rejected the connect request of wallet ${walletId}`,\n\t\t\t\t{ walletId, cause: error },\n\t\t\t)\n\t\t: new KheopskitError(\n\t\t\t\t\"CONNECT_FAILED\",\n\t\t\t\t`wallet ${walletId} failed to connect`,\n\t\t\t\t{ walletId, cause: error },\n\t\t\t);\n};\n\nconst EIP1193_USER_REJECTED_REQUEST = 4001;\n\nexport const isEip1193UserRejection = (error: unknown): boolean =>\n\ttypeof error === \"object\" &&\n\terror !== null &&\n\t(error as { code?: unknown }).code === EIP1193_USER_REJECTED_REQUEST;\n\ntype RefusalMessage = {\n\tpattern: RegExp;\n\twallet: string;\n\tevidence: \"live\" | \"inferred\";\n};\n\n/**\n * How Polkadot extensions and Wallet Standard wallets reject a refused connect\n * prompt. They send a plain `Error`, so the message is the only signal.\n * `evidence` records whether the row was observed live or inferred from the\n * wallet's source or documentation.\n */\nconst REFUSAL_MESSAGES: readonly RefusalMessage[] = [\n\t{\n\t\tpattern: /^Site .+ has not been authorised for Talisman yet$/,\n\t\twallet: \"Talisman 3.11.0, Reject button or prompt window closed\",\n\t\tevidence: \"live\",\n\t},\n\t{\n\t\tpattern: /^Connection request was (rejected|cancelled) by the user\\.$/,\n\t\twallet: \"polkadot-js extension (extension-base 0.64 State.ts)\",\n\t\tevidence: \"inferred\",\n\t},\n\t{\n\t\tpattern: /^(Rejected|Cancelled)$/,\n\t\twallet: \"SubWallet (Extension.ts)\",\n\t\tevidence: \"inferred\",\n\t},\n\t{\n\t\tpattern: /^User rejected the request\\.$/,\n\t\twallet: \"Phantom (Wallet Standard)\",\n\t\tevidence: \"inferred\",\n\t},\n];\n\nexport const isRefusalMessage = (error: unknown): boolean => {\n\tconst message = (error as { message?: unknown } | null | undefined)?.message;\n\treturn (\n\t\ttypeof message === \"string\" &&\n\t\tREFUSAL_MESSAGES.some((row) => row.pattern.test(message))\n\t);\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAgEA,IAAa,iBAAb,cAAoC,MAAM;CAKzC,YACC,MACA,SACA,SACC;EACD,MAAM,eAAe,WAAW,EAAE,OAAO,SAAS,MAAM,CAAC;EATjD,gBAAA,MAAA,QAAA,KAAA,CAAA;EAEA,gBAAA,MAAA,YAAA,KAAA,CAAA;EAQR,KAAK,OAAO;EACZ,KAAK,OAAO;EACZ,KAAK,WAAW,SAAS;CAC1B;AACD;AAEA,MAAa,gBACZ,UACA,cAEA,IAAI,eACH,WACA,GAAG,UAAU,aAAa,SAAS,eACnC,EAAE,SAAS,CACZ;AAED,MAAa,sBAAsB,aAClC,IAAI,eACH,kBACA,UAAU,SAAS,6CACnB,EAAE,SAAS,CACZ;AAED,MAAa,2BACZ,WACA,aAEA,IAAI,eACH,uBACA,UAAU,SAAS,mCAAmC,aACtD,EAAE,SAAS,CACZ;AAED,MAAa,kBACZ,OACA,UACA,oBACoB;CACpB,IAAI,iBAAiB,gBAAgB,OAAO;CAC5C,OAAO,gBAAgB,KAAK,IACzB,IAAI,eACJ,iBACA,mDAAmD,YACnD;EAAE;EAAU,OAAO;CAAM,CAC1B,IACC,IAAI,eACJ,kBACA,UAAU,SAAS,qBACnB;EAAE;EAAU,OAAO;CAAM,CAC1B;AACH;AAEA,MAAM,gCAAgC;AAEtC,MAAa,0BAA0B,UACtC,OAAO,UAAU,YACjB,UAAU,QACT,MAA6B,SAAS;;;;;;;AAcxC,MAAM,mBAA8C;CACnD;EACC,SAAS;EACT,QAAQ;EACR,UAAU;CACX;CACA;EACC,SAAS;EACT,QAAQ;EACR,UAAU;CACX;CACA;EACC,SAAS;EACT,QAAQ;EACR,UAAU;CACX;CACA;EACC,SAAS;EACT,QAAQ;EACR,UAAU;CACX;AACD;AAEA,MAAa,oBAAoB,UAA4B;CAC5D,MAAM,UAAW,OAAoD;CACrE,OACC,OAAO,YAAY,YACnB,iBAAiB,MAAM,QAAQ,IAAI,QAAQ,KAAK,OAAO,CAAC;AAE1D"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
//#region \0@oxc-project+runtime@0.
|
|
1
|
+
//#region \0@oxc-project+runtime@0.154.0/helpers/esm/typeof.js
|
|
2
2
|
function _typeof(o) {
|
|
3
3
|
"@babel/helpers - typeof";
|
|
4
4
|
return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function(o) {
|
|
@@ -8,7 +8,7 @@ function _typeof(o) {
|
|
|
8
8
|
}, _typeof(o);
|
|
9
9
|
}
|
|
10
10
|
//#endregion
|
|
11
|
-
//#region \0@oxc-project+runtime@0.
|
|
11
|
+
//#region \0@oxc-project+runtime@0.154.0/helpers/esm/toPrimitive.js
|
|
12
12
|
function toPrimitive(t, r) {
|
|
13
13
|
if ("object" != _typeof(t) || !t) return t;
|
|
14
14
|
var e = t[Symbol.toPrimitive];
|
|
@@ -20,13 +20,13 @@ function toPrimitive(t, r) {
|
|
|
20
20
|
return ("string" === r ? String : Number)(t);
|
|
21
21
|
}
|
|
22
22
|
//#endregion
|
|
23
|
-
//#region \0@oxc-project+runtime@0.
|
|
23
|
+
//#region \0@oxc-project+runtime@0.154.0/helpers/esm/toPropertyKey.js
|
|
24
24
|
function toPropertyKey(t) {
|
|
25
25
|
var i = toPrimitive(t, "string");
|
|
26
26
|
return "symbol" == _typeof(i) ? i : i + "";
|
|
27
27
|
}
|
|
28
28
|
//#endregion
|
|
29
|
-
//#region \0@oxc-project+runtime@0.
|
|
29
|
+
//#region \0@oxc-project+runtime@0.154.0/helpers/esm/defineProperty.js
|
|
30
30
|
function _defineProperty(e, r, t) {
|
|
31
31
|
return (r = toPropertyKey(r)) in e ? Object.defineProperty(e, r, {
|
|
32
32
|
value: t,
|
|
@@ -112,4 +112,4 @@ const isRefusalMessage = (error) => {
|
|
|
112
112
|
//#endregion
|
|
113
113
|
export { isRefusalMessage as a, isEip1193UserRejection as i, abortedError as n, toConnectError as o, accountUnavailableError as r, walletPendingError as s, KheopskitError as t };
|
|
114
114
|
|
|
115
|
-
//# sourceMappingURL=errors-
|
|
115
|
+
//# sourceMappingURL=errors-DszTFbnO.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"errors-
|
|
1
|
+
{"version":3,"file":"errors-DszTFbnO.mjs","names":[],"sources":["../src/api/errors.ts"],"sourcesContent":["import type { WalletAccountId } from \"../utils/WalletAccountId\";\nimport type { WalletId } from \"../utils/WalletId\";\n\n/**\n * Stable error codes thrown by kheopskit. Catch a {@link KheopskitError} and\n * branch on `error.code` instead of matching message strings.\n */\nexport type KheopskitErrorCode =\n\t/**\n\t * This `connect()` or `disconnect()` call was abandoned. For `connect()`:\n\t * its `signal` fired before the platform answered, `disconnect()` was\n\t * called while it pended, its wallet disappeared, or the last `kheopskit$`\n\t * subscriber unsubscribed.\n\t * For either call on an injected wallet: the wallet object came from a\n\t * subscription that had already ended, in which case the call rejects\n\t * without asking the platform and without changing the remembered list. A\n\t * placeholder's `connect()` rejects the same way, its `disconnect()`\n\t * resolves, and the WalletConnect connector still calls AppKit.\n\t */\n\t| \"ABORTED\"\n\t/** The user refused the wallet's connect prompt. `cause` holds the platform error. */\n\t| \"USER_REJECTED\"\n\t/** The wallet's connect call failed for another reason. `cause` holds the platform error. */\n\t| \"CONNECT_FAILED\"\n\t/**\n\t * `connect()` was called on a placeholder wallet restored from the cache:\n\t * on `kheopskit$`, hydration ended without kheopskit discovering that\n\t * wallet; on `getHydratedSnapshot` and the React provider's initial\n\t * snapshot, always.\n\t */\n\t| \"WALLET_PENDING\"\n\t/**\n\t * `waitForAccount()` found the account's wallet settled without listing it\n\t * as a live account, or not connected.\n\t */\n\t| \"ACCOUNT_UNAVAILABLE\"\n\t/** The wallet does not advertise a capability kheopskit needs to proceed. */\n\t| \"FEATURE_NOT_SUPPORTED\"\n\t/** No active WalletConnect session for the requested operation. */\n\t| \"NO_SESSION\"\n\t/** No provider available for the requested namespace. */\n\t| \"NO_PROVIDER\"\n\t/** The installed polkadot-api version exposes neither `getTxCreatorFromPjs` (v3) nor `getPolkadotSignerFromPjs` (v2). */\n\t| \"UNSUPPORTED_VERSION\"\n\t/** The requested chain cannot be used for this operation (e.g. a Solana cluster with no CAIP-2 id over WalletConnect). */\n\t| \"UNSUPPORTED_CHAIN\"\n\t/** `createKheopskit` received a config value it cannot use. The message names the fix. */\n\t| \"INVALID_CONFIG\";\n\n/**\n * Error thrown by kheopskit wallet/account operations. Carries a stable\n * {@link KheopskitErrorCode} and, when relevant, the offending wallet id.\n *\n * @example\n * ```ts\n * try {\n * await wallet.connect();\n * } catch (error) {\n * if (error instanceof KheopskitError && error.code === \"USER_REJECTED\") {\n * // the user closed or refused the wallet prompt\n * } else throw error;\n * }\n * ```\n */\nexport class KheopskitError extends Error {\n\treadonly code: KheopskitErrorCode;\n\t/** The wallet id this error relates to, when applicable. */\n\treadonly walletId?: WalletId;\n\n\tconstructor(\n\t\tcode: KheopskitErrorCode,\n\t\tmessage: string,\n\t\toptions?: { walletId?: WalletId; cause?: unknown },\n\t) {\n\t\tsuper(`[kheopskit] ${message}`, { cause: options?.cause });\n\t\tthis.name = \"KheopskitError\";\n\t\tthis.code = code;\n\t\tthis.walletId = options?.walletId;\n\t}\n}\n\nexport const abortedError = (\n\twalletId: WalletId,\n\toperation: \"connect\" | \"disconnect\",\n): KheopskitError =>\n\tnew KheopskitError(\n\t\t\"ABORTED\",\n\t\t`${operation} of wallet ${walletId} was aborted`,\n\t\t{ walletId },\n\t);\n\nexport const walletPendingError = (walletId: WalletId): KheopskitError =>\n\tnew KheopskitError(\n\t\t\"WALLET_PENDING\",\n\t\t`wallet ${walletId} was not discovered before hydration ended`,\n\t\t{ walletId },\n\t);\n\nexport const accountUnavailableError = (\n\taccountId: WalletAccountId,\n\twalletId: WalletId,\n): KheopskitError =>\n\tnew KheopskitError(\n\t\t\"ACCOUNT_UNAVAILABLE\",\n\t\t`wallet ${walletId} settled without listing account ${accountId}`,\n\t\t{ walletId },\n\t);\n\nexport const toConnectError = (\n\terror: unknown,\n\twalletId: WalletId,\n\tisUserRejection: (error: unknown) => boolean,\n): KheopskitError => {\n\tif (error instanceof KheopskitError) return error;\n\treturn isUserRejection(error)\n\t\t? new KheopskitError(\n\t\t\t\t\"USER_REJECTED\",\n\t\t\t\t`the user rejected the connect request of wallet ${walletId}`,\n\t\t\t\t{ walletId, cause: error },\n\t\t\t)\n\t\t: new KheopskitError(\n\t\t\t\t\"CONNECT_FAILED\",\n\t\t\t\t`wallet ${walletId} failed to connect`,\n\t\t\t\t{ walletId, cause: error },\n\t\t\t);\n};\n\nconst EIP1193_USER_REJECTED_REQUEST = 4001;\n\nexport const isEip1193UserRejection = (error: unknown): boolean =>\n\ttypeof error === \"object\" &&\n\terror !== null &&\n\t(error as { code?: unknown }).code === EIP1193_USER_REJECTED_REQUEST;\n\ntype RefusalMessage = {\n\tpattern: RegExp;\n\twallet: string;\n\tevidence: \"live\" | \"inferred\";\n};\n\n/**\n * How Polkadot extensions and Wallet Standard wallets reject a refused connect\n * prompt. They send a plain `Error`, so the message is the only signal.\n * `evidence` records whether the row was observed live or inferred from the\n * wallet's source or documentation.\n */\nconst REFUSAL_MESSAGES: readonly RefusalMessage[] = [\n\t{\n\t\tpattern: /^Site .+ has not been authorised for Talisman yet$/,\n\t\twallet: \"Talisman 3.11.0, Reject button or prompt window closed\",\n\t\tevidence: \"live\",\n\t},\n\t{\n\t\tpattern: /^Connection request was (rejected|cancelled) by the user\\.$/,\n\t\twallet: \"polkadot-js extension (extension-base 0.64 State.ts)\",\n\t\tevidence: \"inferred\",\n\t},\n\t{\n\t\tpattern: /^(Rejected|Cancelled)$/,\n\t\twallet: \"SubWallet (Extension.ts)\",\n\t\tevidence: \"inferred\",\n\t},\n\t{\n\t\tpattern: /^User rejected the request\\.$/,\n\t\twallet: \"Phantom (Wallet Standard)\",\n\t\tevidence: \"inferred\",\n\t},\n];\n\nexport const isRefusalMessage = (error: unknown): boolean => {\n\tconst message = (error as { message?: unknown } | null | undefined)?.message;\n\treturn (\n\t\ttypeof message === \"string\" &&\n\t\tREFUSAL_MESSAGES.some((row) => row.pattern.test(message))\n\t);\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAgEA,IAAa,iBAAb,cAAoC,MAAM;CAKzC,YACC,MACA,SACA,SACC;EACD,MAAM,eAAe,WAAW,EAAE,OAAO,SAAS,MAAM,CAAC;EATjD,gBAAA,MAAA,QAAA,KAAA,CAAA;EAEA,gBAAA,MAAA,YAAA,KAAA,CAAA;EAQR,KAAK,OAAO;EACZ,KAAK,OAAO;EACZ,KAAK,WAAW,SAAS;CAC1B;AACD;AAEA,MAAa,gBACZ,UACA,cAEA,IAAI,eACH,WACA,GAAG,UAAU,aAAa,SAAS,eACnC,EAAE,SAAS,CACZ;AAED,MAAa,sBAAsB,aAClC,IAAI,eACH,kBACA,UAAU,SAAS,6CACnB,EAAE,SAAS,CACZ;AAED,MAAa,2BACZ,WACA,aAEA,IAAI,eACH,uBACA,UAAU,SAAS,mCAAmC,aACtD,EAAE,SAAS,CACZ;AAED,MAAa,kBACZ,OACA,UACA,oBACoB;CACpB,IAAI,iBAAiB,gBAAgB,OAAO;CAC5C,OAAO,gBAAgB,KAAK,IACzB,IAAI,eACJ,iBACA,mDAAmD,YACnD;EAAE;EAAU,OAAO;CAAM,CAC1B,IACC,IAAI,eACJ,kBACA,UAAU,SAAS,qBACnB;EAAE;EAAU,OAAO;CAAM,CAC1B;AACH;AAEA,MAAM,gCAAgC;AAEtC,MAAa,0BAA0B,UACtC,OAAO,UAAU,YACjB,UAAU,QACT,MAA6B,SAAS;;;;;;;AAcxC,MAAM,mBAA8C;CACnD;EACC,SAAS;EACT,QAAQ;EACR,UAAU;CACX;CACA;EACC,SAAS;EACT,QAAQ;EACR,UAAU;CACX;CACA;EACC,SAAS;EACT,QAAQ;EACR,UAAU;CACX;CACA;EACC,SAAS;EACT,QAAQ;EACR,UAAU;CACX;AACD;AAEA,MAAa,oBAAoB,UAA4B;CAC5D,MAAM,UAAW,OAAoD;CACrE,OACC,OAAO,YAAY,YACnB,iBAAiB,MAAM,QAAQ,IAAI,QAAQ,KAAK,OAAO,CAAC;AAE1D"}
|
package/dist/ethereum.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
2
|
const require_types = require("./types-DaRMgleZ.js");
|
|
3
3
|
const require_platformAccounts = require("./platformAccounts-CqMSpl3X.js");
|
|
4
|
-
const require_errors = require("./errors-
|
|
5
|
-
const require_injectedWallets = require("./injectedWallets-
|
|
4
|
+
const require_errors = require("./errors-B6Z21vkI.js");
|
|
5
|
+
const require_injectedWallets = require("./injectedWallets-Cfn3caW3.js");
|
|
6
6
|
let rxjs = require("rxjs");
|
|
7
7
|
let viem = require("viem");
|
|
8
8
|
let mipd = require("mipd");
|
|
@@ -23,11 +23,43 @@ const toCaipNetworkId = (value) => {
|
|
|
23
23
|
const chainId = normalizeEvmChainId(value);
|
|
24
24
|
return chainId === void 0 ? void 0 : `eip155:${chainId}`;
|
|
25
25
|
};
|
|
26
|
+
const lastReported = /* @__PURE__ */ new WeakMap();
|
|
27
|
+
const CHAIN_ANSWER_TIMEOUT_MS = 2e3;
|
|
28
|
+
const readChainId = (provider) => (0, rxjs.firstValueFrom)((0, rxjs.race)(provider.request({ method: "eth_chainId" }).then(normalizeEvmChainId, () => void 0), (0, rxjs.timer)(CHAIN_ANSWER_TIMEOUT_MS).pipe((0, rxjs.map)(() => void 0))));
|
|
29
|
+
const requestAccountsOnChain = async (provider) => {
|
|
30
|
+
const accountChanges = [];
|
|
31
|
+
const chainChanges = [];
|
|
32
|
+
const handleAccountsChanged = (addresses) => {
|
|
33
|
+
accountChanges.push(addresses);
|
|
34
|
+
};
|
|
35
|
+
const handleChainChanged = (chainId) => {
|
|
36
|
+
chainChanges.push(normalizeEvmChainId(chainId));
|
|
37
|
+
};
|
|
38
|
+
provider.on("accountsChanged", handleAccountsChanged);
|
|
39
|
+
provider.on("chainChanged", handleChainChanged);
|
|
40
|
+
try {
|
|
41
|
+
const addresses = await provider.request({ method: "eth_requestAccounts" });
|
|
42
|
+
const chainChangesBeforeRead = chainChanges.length;
|
|
43
|
+
const chainId = await readChainId(provider);
|
|
44
|
+
lastReported.set(provider, {
|
|
45
|
+
addresses: accountChanges.at(-1) ?? addresses,
|
|
46
|
+
chainId: chainChanges.length > chainChangesBeforeRead ? chainChanges.at(-1) : chainId
|
|
47
|
+
});
|
|
48
|
+
} finally {
|
|
49
|
+
provider.removeListener("accountsChanged", handleAccountsChanged);
|
|
50
|
+
provider.removeListener("chainChanged", handleChainChanged);
|
|
51
|
+
}
|
|
52
|
+
};
|
|
26
53
|
const getInjectedWalletAccounts$ = (wallet) => {
|
|
27
54
|
if (!wallet.isConnected) return (0, rxjs.of)([]);
|
|
28
55
|
return require_types.getCachedObservable$(`accounts:${wallet.id}`, () => new rxjs.Observable((subscriber) => {
|
|
29
56
|
const addresses$ = new rxjs.ReplaySubject(1);
|
|
30
57
|
const chainId$ = new rxjs.ReplaySubject(1);
|
|
58
|
+
const reported = lastReported.get(wallet.provider);
|
|
59
|
+
if (reported) {
|
|
60
|
+
addresses$.next(reported.addresses);
|
|
61
|
+
chainId$.next(reported.chainId);
|
|
62
|
+
}
|
|
31
63
|
const getAccount = (address, chainId) => {
|
|
32
64
|
const client = (0, viem.createWalletClient)({
|
|
33
65
|
account: address,
|
|
@@ -43,27 +75,51 @@ const getInjectedWalletAccounts$ = (wallet) => {
|
|
|
43
75
|
walletId: wallet.id
|
|
44
76
|
};
|
|
45
77
|
};
|
|
78
|
+
let accountEvents = 0;
|
|
79
|
+
let chainEvents = 0;
|
|
46
80
|
const handleAccountsChanged = (addrs) => {
|
|
81
|
+
accountEvents++;
|
|
47
82
|
addresses$.next(addrs);
|
|
48
83
|
};
|
|
49
84
|
const handleChainChanged = (chainIdHex) => {
|
|
85
|
+
chainEvents++;
|
|
50
86
|
chainId$.next(normalizeEvmChainId(chainIdHex));
|
|
51
87
|
};
|
|
88
|
+
const handleConnect = (connectInfo) => {
|
|
89
|
+
chainEvents++;
|
|
90
|
+
chainId$.next(normalizeEvmChainId(connectInfo?.chainId));
|
|
91
|
+
};
|
|
52
92
|
const handleDisconnect = () => {
|
|
93
|
+
chainEvents++;
|
|
53
94
|
chainId$.next(void 0);
|
|
54
95
|
};
|
|
55
96
|
wallet.provider.on("accountsChanged", handleAccountsChanged);
|
|
56
97
|
wallet.provider.on("chainChanged", handleChainChanged);
|
|
98
|
+
wallet.provider.on("connect", handleConnect);
|
|
57
99
|
wallet.provider.on("disconnect", handleDisconnect);
|
|
58
|
-
|
|
100
|
+
const accountEventsBeforeRead = accountEvents;
|
|
101
|
+
const accountsOutdated = () => accountEvents !== accountEventsBeforeRead;
|
|
102
|
+
wallet.provider.request({ method: "eth_accounts" }).then((addrs) => {
|
|
103
|
+
if (!accountsOutdated()) addresses$.next(addrs);
|
|
104
|
+
}).catch((err) => {
|
|
59
105
|
console.error("Failed to get accounts", err);
|
|
60
|
-
addresses$.next([]);
|
|
106
|
+
if (!accountsOutdated()) addresses$.next([]);
|
|
107
|
+
});
|
|
108
|
+
const chainEventsBeforeRead = chainEvents;
|
|
109
|
+
const chainOutdated = () => chainEvents !== chainEventsBeforeRead;
|
|
110
|
+
wallet.provider.request({ method: "eth_chainId" }).then((chainIdHex) => {
|
|
111
|
+
if (!chainOutdated()) chainId$.next(normalizeEvmChainId(chainIdHex));
|
|
112
|
+
}).catch(() => {
|
|
113
|
+
if (!chainOutdated()) chainId$.next(void 0);
|
|
61
114
|
});
|
|
62
|
-
|
|
63
|
-
|
|
115
|
+
const sub = (0, rxjs.combineLatest)([addresses$, chainId$]).pipe((0, rxjs.tap)(([addresses, chainId]) => lastReported.set(wallet.provider, {
|
|
116
|
+
addresses,
|
|
117
|
+
chainId
|
|
118
|
+
})), (0, rxjs.map)(([addresses, chainId]) => addresses.map((addr) => getAccount(addr, chainId)))).subscribe(subscriber);
|
|
64
119
|
return () => {
|
|
65
120
|
wallet.provider.removeListener("accountsChanged", handleAccountsChanged);
|
|
66
121
|
wallet.provider.removeListener("chainChanged", handleChainChanged);
|
|
122
|
+
wallet.provider.removeListener("connect", handleConnect);
|
|
67
123
|
wallet.provider.removeListener("disconnect", handleDisconnect);
|
|
68
124
|
sub.unsubscribe();
|
|
69
125
|
};
|
|
@@ -183,9 +239,7 @@ const providersDetails$ = new rxjs.Observable((subscriber) => {
|
|
|
183
239
|
const getEthereumWallets$ = (store) => require_injectedWallets.createInjectedWallets$(store, {
|
|
184
240
|
sources$: providersDetails$,
|
|
185
241
|
getWalletId: (pd) => require_types.getWalletId("ethereum", pd.info.rdns),
|
|
186
|
-
connect:
|
|
187
|
-
await pd.provider.request({ method: "eth_requestAccounts" });
|
|
188
|
-
},
|
|
242
|
+
connect: (pd) => requestAccountsOnChain(pd.provider),
|
|
189
243
|
isUserRejection: require_errors.isEip1193UserRejection,
|
|
190
244
|
onDisconnected: (walletId) => require_types.clearCachedObservable(`accounts:${walletId}`),
|
|
191
245
|
buildWallet: ({ source: pd, walletId, status, error, isConnected, connect, disconnect }) => ({
|
package/dist/ethereum.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ethereum.js","names":["of","getCachedObservable$","Observable","ReplaySubject","createWalletClient","custom","getWalletAccountId","getAddress","combineLatest","map","shareReplay","getCaip10Addresses","getSessionCaip10s","distinctUntilChanged","createPlatformAccounts$","Observable","createMipdStore","shareReplay","createInjectedWallets$","getWalletId","isEip1193UserRejection","clearCachedObservable"],"sources":["../src/api/ethereum/accounts.ts","../src/api/ethereum/wallets.ts","../src/api/ethereum/plugin.ts"],"sourcesContent":["import {\n\tcombineLatest,\n\tdistinctUntilChanged,\n\tmap,\n\tObservable,\n\tof,\n\tReplaySubject,\n\tshareReplay,\n} from \"rxjs\";\nimport {\n\tcreateWalletClient,\n\tcustom,\n\ttype EIP1193Provider,\n\tgetAddress,\n} from \"viem\";\nimport { getWalletAccountId } from \"../../utils\";\nimport { getCachedObservable$ } from \"../../utils/getCachedObservable\";\nimport {\n\tcreatePlatformAccounts$,\n\tgetCaip10Addresses,\n\tgetSessionCaip10s,\n\ttype LiveWalletConnectWallet,\n} from \"../platformAccounts\";\nimport type { WalletConnectWallet } from \"../types\";\nimport type {\n\tEthereumAccount,\n\tEthereumInjectedWallet,\n\tEthereumWallet,\n} from \"./types\";\n\nconst normalizeEvmChainId = (value: unknown): number | undefined => {\n\tlet raw = value;\n\n\tif (typeof raw === \"string\" && raw.startsWith(\"eip155:\")) {\n\t\traw = raw.slice(\"eip155:\".length);\n\t}\n\n\tif (typeof raw === \"bigint\") {\n\t\treturn raw >= 0n ? Number(raw) : undefined;\n\t}\n\n\tif (typeof raw === \"number\") {\n\t\treturn Number.isInteger(raw) && raw >= 0 ? raw : undefined;\n\t}\n\n\tif (typeof raw === \"string\") {\n\t\tconst normalized = raw.trim().toLowerCase();\n\t\tif (!normalized) return undefined;\n\t\tconst parsed = normalized.startsWith(\"0x\")\n\t\t\t? Number.parseInt(normalized, 16)\n\t\t\t: Number.parseInt(normalized, 10);\n\t\treturn Number.isNaN(parsed) ? undefined : parsed;\n\t}\n\n\treturn undefined;\n};\n\nconst toCaipNetworkId = (value: unknown): string | undefined => {\n\tconst chainId = normalizeEvmChainId(value);\n\treturn chainId === undefined ? undefined : `eip155:${chainId}`;\n};\n\nconst getInjectedWalletAccounts$ = (\n\twallet: EthereumInjectedWallet,\n): Observable<EthereumAccount[]> => {\n\tif (!wallet.isConnected) return of([]);\n\n\treturn getCachedObservable$(`accounts:${wallet.id}`, () =>\n\t\tnew Observable<EthereumAccount[]>((subscriber) => {\n\t\t\tconst addresses$ = new ReplaySubject<string[]>(1);\n\t\t\tconst chainId$ = new ReplaySubject<number | undefined>(1);\n\n\t\t\tconst getAccount = (\n\t\t\t\taddress: string,\n\t\t\t\tchainId: number | undefined,\n\t\t\t): EthereumAccount => {\n\t\t\t\tconst client = createWalletClient({\n\t\t\t\t\taccount: address as `0x${string}`,\n\t\t\t\t\ttransport: custom(wallet.provider as EIP1193Provider),\n\t\t\t\t});\n\n\t\t\t\treturn {\n\t\t\t\t\tid: getWalletAccountId(wallet.id, address),\n\t\t\t\t\tplatform: \"ethereum\",\n\t\t\t\t\tclient,\n\t\t\t\t\taddress: getAddress(address),\n\t\t\t\t\tchainId,\n\t\t\t\t\twalletName: wallet.name,\n\t\t\t\t\twalletId: wallet.id,\n\t\t\t\t};\n\t\t\t};\n\n\t\t\tconst handleAccountsChanged = (addrs: string[]) => {\n\t\t\t\taddresses$.next(addrs);\n\t\t\t};\n\n\t\t\tconst handleChainChanged = (chainIdHex: unknown) => {\n\t\t\t\tchainId$.next(normalizeEvmChainId(chainIdHex));\n\t\t\t};\n\n\t\t\tconst handleDisconnect = () => {\n\t\t\t\tchainId$.next(undefined);\n\t\t\t};\n\n\t\t\t// Subscribe to provider events\n\t\t\twallet.provider.on(\"accountsChanged\", handleAccountsChanged);\n\t\t\twallet.provider.on(\"chainChanged\", handleChainChanged);\n\t\t\twallet.provider.on(\"disconnect\", handleDisconnect);\n\n\t\t\t// Fetch initial values\n\t\t\twallet.provider\n\t\t\t\t.request({ method: \"eth_accounts\" })\n\t\t\t\t.then((addrs: string[]) => addresses$.next(addrs))\n\t\t\t\t.catch((err) => {\n\t\t\t\t\tconsole.error(\"Failed to get accounts\", err);\n\t\t\t\t\taddresses$.next([]);\n\t\t\t\t});\n\n\t\t\twallet.provider\n\t\t\t\t.request({ method: \"eth_chainId\" })\n\t\t\t\t.then(handleChainChanged)\n\t\t\t\t.catch(() => chainId$.next(undefined));\n\n\t\t\t// Combine addresses + chainId into account list\n\t\t\tconst sub = combineLatest([addresses$, chainId$])\n\t\t\t\t.pipe(\n\t\t\t\t\tmap(([addresses, chainId]) =>\n\t\t\t\t\t\taddresses.map((addr) => getAccount(addr, chainId)),\n\t\t\t\t\t),\n\t\t\t\t)\n\t\t\t\t.subscribe(subscriber);\n\n\t\t\treturn () => {\n\t\t\t\twallet.provider.removeListener(\n\t\t\t\t\t\"accountsChanged\",\n\t\t\t\t\thandleAccountsChanged,\n\t\t\t\t);\n\t\t\t\twallet.provider.removeListener(\"chainChanged\", handleChainChanged);\n\t\t\t\twallet.provider.removeListener(\"disconnect\", handleDisconnect);\n\t\t\t\tsub.unsubscribe();\n\t\t\t};\n\t\t}).pipe(shareReplay({ refCount: true, bufferSize: 1 })),\n\t);\n};\n\nconst wrapWalletConnectProvider = (\n\tprovider: EIP1193Provider,\n\tsessionTopic: string,\n\tcaipNetworkId: string | undefined,\n): EIP1193Provider => {\n\treturn new Proxy(provider, {\n\t\tget(target, prop, receiver) {\n\t\t\tif (prop !== \"request\") return Reflect.get(target, prop, receiver);\n\n\t\t\t// biome-ignore lint/suspicious/noExplicitAny: EIP-1193 request args\n\t\t\treturn (args: any) => {\n\t\t\t\tif (!args || typeof args !== \"object\" || !args.method)\n\t\t\t\t\treturn target.request(args);\n\t\t\t\t// Build a new object rather than mutating the caller's args: the\n\t\t\t\t// EIP-1193 request object is owned by viem and may be frozen or reused.\n\t\t\t\tconst next = { ...args };\n\t\t\t\tif (next.topic === undefined) next.topic = sessionTopic;\n\t\t\t\tif (next.chainId === undefined && caipNetworkId !== undefined)\n\t\t\t\t\tnext.chainId = caipNetworkId;\n\t\t\t\t// WalletConnect's UniversalProvider reads the target CAIP chain id from\n\t\t\t\t// the SECOND positional argument of `request(args, chainId)`, not from a\n\t\t\t\t// field on the args object. This AppKit instance has no native eip155\n\t\t\t\t// adapter, so the provider has no default chain — without the positional\n\t\t\t\t// chain id every call is rejected (\"Unsupported chains\" / \"Method not\n\t\t\t\t// found\"). Pass it positionally so the request routes to the session's\n\t\t\t\t// chain.\n\t\t\t\treturn (\n\t\t\t\t\ttarget.request as (a: unknown, chainId?: string) => Promise<unknown>\n\t\t\t\t)(next, caipNetworkId);\n\t\t\t};\n\t\t},\n\t});\n};\n\nconst sameAddresses = (a: string[], b: string[]) =>\n\ta.length === b.length && a.every((addr, i) => addr === b[i]);\n\nconst getWalletConnectAccounts$ = (\n\twallet: LiveWalletConnectWallet,\n): Observable<EthereumAccount[]> => {\n\tconst provider = wallet.appKit.getProvider(\"eip155\");\n\n\tif (!wallet.platforms.includes(\"ethereum\") || !provider?.session)\n\t\treturn of([]);\n\n\treturn getCachedObservable$(`accounts:${wallet.id}:ethereum:`, () =>\n\t\tnew Observable<EthereumAccount[]>((subscriber) => {\n\t\t\tconst caipNetworkId$ = new ReplaySubject<string | undefined>(1);\n\t\t\tconst addresses$ = new ReplaySubject<string[]>(1);\n\n\t\t\t// AppKit's getAccount(\"eip155\").allAccounts is empty because this AppKit\n\t\t\t// instance has no native eip155 adapter — eip155 runs through the\n\t\t\t// WalletConnect UniversalProvider, so the session is the source of truth\n\t\t\t// (mirrors the polkadot/solana AppKit paths). Accounts are CAIP-10 strings\n\t\t\t// (\"eip155:<chainRef>:<address>\"), one entry per chain, so dedupe to unique\n\t\t\t// addresses. Read live on each change so switching/adding accounts is\n\t\t\t// reflected.\n\t\t\tconst readAddresses = (): string[] => {\n\t\t\t\tconst session = provider.session;\n\t\t\t\tif (!session) return [];\n\t\t\t\tconst addresses = new Set<string>();\n\t\t\t\tfor (const raw of getCaip10Addresses(\n\t\t\t\t\tgetSessionCaip10s(session, \"eip155\"),\n\t\t\t\t)) {\n\t\t\t\t\ttry {\n\t\t\t\t\t\taddresses.add(getAddress(raw));\n\t\t\t\t\t} catch {\n\t\t\t\t\t\t// skip malformed CAIP-10 address\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\treturn [...addresses];\n\t\t\t};\n\n\t\t\t// Derive the CAIP network id from the session's eip155 CAIP-10 accounts\n\t\t\t// (\"eip155:<ref>:<addr>\") as a synchronous fallback for eth_chainId.\n\t\t\tconst readCaipNetworkId = (): string | undefined => {\n\t\t\t\tconst session = provider.session;\n\t\t\t\tif (!session) return undefined;\n\t\t\t\tfor (const account of getSessionCaip10s(session, \"eip155\")) {\n\t\t\t\t\tconst ref = account.split(\":\")[1];\n\t\t\t\t\tif (ref) return `eip155:${ref}`;\n\t\t\t\t}\n\t\t\t\treturn undefined;\n\t\t\t};\n\n\t\t\tconst handleChainChanged = (chainId: unknown) => {\n\t\t\t\tconst caipNetworkId = toCaipNetworkId(chainId);\n\t\t\t\tif (caipNetworkId) {\n\t\t\t\t\tcaipNetworkId$.next(caipNetworkId);\n\t\t\t\t}\n\t\t\t};\n\n\t\t\tconst handleAccountsChanged = () => addresses$.next(readAddresses());\n\n\t\t\tprovider.on(\"chainChanged\", handleChainChanged);\n\t\t\tprovider.on(\"accountsChanged\", handleAccountsChanged);\n\t\t\tprovider.on(\"session_update\", handleAccountsChanged);\n\t\t\t// Seed the chain id from the session synchronously so the account list can\n\t\t\t// surface even if eth_chainId never resolves; eth_chainId then refines it.\n\t\t\t// Without this seed, combineLatest below never fires when eth_chainId\n\t\t\t// rejects or returns an unparseable value, and the accounts silently never\n\t\t\t// appear (the injected path seeds undefined on failure for the same reason).\n\t\t\tcaipNetworkId$.next(readCaipNetworkId());\n\t\t\tprovider\n\t\t\t\t.request({ method: \"eth_chainId\" })\n\t\t\t\t.then(handleChainChanged)\n\t\t\t\t.catch(() => {});\n\t\t\taddresses$.next(readAddresses());\n\n\t\t\tconst sub = combineLatest([\n\t\t\t\tcaipNetworkId$.pipe(distinctUntilChanged()),\n\t\t\t\taddresses$.pipe(distinctUntilChanged(sameAddresses)),\n\t\t\t])\n\t\t\t\t.pipe(\n\t\t\t\t\tmap(([caipNetworkId, addresses]) => {\n\t\t\t\t\t\tconst chainId = normalizeEvmChainId(caipNetworkId);\n\t\t\t\t\t\tconst transport = custom(\n\t\t\t\t\t\t\twrapWalletConnectProvider(\n\t\t\t\t\t\t\t\tprovider as unknown as EIP1193Provider,\n\t\t\t\t\t\t\t\t// biome-ignore lint/style/noNonNullAssertion: legacy\n\t\t\t\t\t\t\t\tprovider.session!.topic,\n\t\t\t\t\t\t\t\tcaipNetworkId,\n\t\t\t\t\t\t\t),\n\t\t\t\t\t\t);\n\t\t\t\t\t\treturn addresses.map((addr): EthereumAccount => {\n\t\t\t\t\t\t\tconst client = createWalletClient({\n\t\t\t\t\t\t\t\taccount: addr as `0x${string}`,\n\t\t\t\t\t\t\t\ttransport,\n\t\t\t\t\t\t\t});\n\n\t\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\t\tid: getWalletAccountId(wallet.id, addr),\n\t\t\t\t\t\t\t\tplatform: \"ethereum\",\n\t\t\t\t\t\t\t\twalletName: wallet.name,\n\t\t\t\t\t\t\t\twalletId: wallet.id,\n\t\t\t\t\t\t\t\taddress: addr as `0x${string}`,\n\t\t\t\t\t\t\t\tclient,\n\t\t\t\t\t\t\t\tchainId,\n\t\t\t\t\t\t\t};\n\t\t\t\t\t\t});\n\t\t\t\t\t}),\n\t\t\t\t)\n\t\t\t\t.subscribe(subscriber);\n\n\t\t\treturn () => {\n\t\t\t\tprovider.off(\"chainChanged\", handleChainChanged);\n\t\t\t\tprovider.off(\"accountsChanged\", handleAccountsChanged);\n\t\t\t\tprovider.off(\"session_update\", handleAccountsChanged);\n\t\t\t\tsub.unsubscribe();\n\t\t\t};\n\t\t}).pipe(shareReplay({ refCount: true, bufferSize: 1 })),\n\t);\n};\n\nexport const getEthereumAccounts$ = (\n\tethereumWallets: Observable<(EthereumWallet | WalletConnectWallet)[]>,\n) =>\n\tcreatePlatformAccounts$({\n\t\twallets$: ethereumWallets,\n\t\tgetInjectedAccounts$: getInjectedWalletAccounts$,\n\t\tgetWalletConnectAccounts$,\n\t\t// Re-emit on chain switches, not just account id changes.\n\t\taccountChangeKey: (account) => `${account.id}|${account.chainId ?? \"\"}`,\n\t});\n","import {\n\tcreateStore as createMipdStore,\n\ttype EIP6963ProviderDetail,\n} from \"mipd\";\nimport { Observable, shareReplay } from \"rxjs\";\nimport type { EIP1193Provider } from \"viem\";\nimport { clearCachedObservable } from \"../../utils/getCachedObservable\";\nimport { getWalletId } from \"../../utils/WalletId\";\nimport { isEip1193UserRejection } from \"../errors\";\nimport { createInjectedWallets$ } from \"../injectedWallets\";\nimport type { KheopskitStore } from \"../store\";\nimport type { EthereumInjectedWallet } from \"./types\";\n\n/**\n * Observable that emits EIP-6963 provider details from injected wallets.\n * Returns empty array during SSR since browser wallet APIs are not available.\n */\nconst providersDetails$ = new Observable<EIP6963ProviderDetail[]>(\n\t(subscriber) => {\n\t\t// Guard against SSR - mipd requires browser APIs\n\t\tif (typeof window === \"undefined\") {\n\t\t\tsubscriber.next([]);\n\t\t\treturn () => {};\n\t\t}\n\n\t\tconst mipdStore = createMipdStore();\n\n\t\tconst unsubscribe = mipdStore.subscribe((providerDetails) => {\n\t\t\tsubscriber.next(providerDetails as EIP6963ProviderDetail[]);\n\t\t});\n\n\t\tconst providerDetails = mipdStore.getProviders();\n\n\t\tsubscriber.next(providerDetails as EIP6963ProviderDetail[]);\n\n\t\treturn () => {\n\t\t\tunsubscribe();\n\t\t\tmipdStore.destroy();\n\t\t};\n\t},\n).pipe(shareReplay({ refCount: true, bufferSize: 1 }));\n\n// The shared WalletConnect connector is emitted once by core (see\n// `getWallets$`), not per platform — so this returns only injected wallets.\nexport const getEthereumWallets$ = (store: KheopskitStore) =>\n\tcreateInjectedWallets$<EIP6963ProviderDetail, EthereumInjectedWallet>(store, {\n\t\tsources$: providersDetails$,\n\t\tgetWalletId: (pd) => getWalletId(\"ethereum\", pd.info.rdns),\n\t\tconnect: async (pd) => {\n\t\t\tawait (pd.provider as EIP1193Provider).request({\n\t\t\t\tmethod: \"eth_requestAccounts\",\n\t\t\t});\n\t\t},\n\t\tisUserRejection: isEip1193UserRejection,\n\t\t// Drop the cached account observable so a later reconnect rebuilds it\n\t\t// against the current provider, not a stale closure.\n\t\tonDisconnected: (walletId) => clearCachedObservable(`accounts:${walletId}`),\n\t\tbuildWallet: ({\n\t\t\tsource: pd,\n\t\t\twalletId,\n\t\t\tstatus,\n\t\t\terror,\n\t\t\tisConnected,\n\t\t\tconnect,\n\t\t\tdisconnect,\n\t\t}) => ({\n\t\t\tplatform: \"ethereum\",\n\t\t\ttype: \"injected\",\n\t\t\tid: walletId,\n\t\t\tname: pd.info.name,\n\t\t\ticon: pd.info.icon,\n\t\t\tprovider: pd.provider as EIP1193Provider,\n\t\t\tstatus,\n\t\t\terror,\n\t\t\tisConnected,\n\t\t\tsourceId: pd.info.rdns,\n\t\t\tconnect,\n\t\t\tdisconnect,\n\t\t}),\n\t});\n","import type { KheopskitPlatform, PlatformContext } from \"../types\";\nimport { getEthereumAccounts$ } from \"./accounts\";\nimport type { EthereumAccount, EthereumWallet } from \"./types\";\nimport { getEthereumWallets$ } from \"./wallets\";\n\n/**\n * Ethereum platform plugin. Pass to `createKheopskit({ platforms: [ethereum()] })`.\n *\n * @example\n * ```ts\n * import { ethereum } from \"@kheopskit/core/ethereum\";\n * ethereum();\n * ```\n */\nexport const ethereum = (): KheopskitPlatform<\n\t\"ethereum\",\n\tEthereumWallet,\n\tEthereumAccount\n> => ({\n\tplatform: \"ethereum\",\n\tgetWallets$: (ctx: PlatformContext) => getEthereumWallets$(ctx.store),\n\tgetAccounts$: (wallets$) => getEthereumAccounts$(wallets$),\n});\n"],"mappings":";;;;;;;;;AA8BA,MAAM,uBAAuB,UAAuC;CACnE,IAAI,MAAM;CAEV,IAAI,OAAO,QAAQ,YAAY,IAAI,WAAW,SAAS,GACtD,MAAM,IAAI,MAAM,CAAgB;CAGjC,IAAI,OAAO,QAAQ,UAClB,OAAO,OAAO,KAAK,OAAO,GAAG,IAAI,KAAA;CAGlC,IAAI,OAAO,QAAQ,UAClB,OAAO,OAAO,UAAU,GAAG,KAAK,OAAO,IAAI,MAAM,KAAA;CAGlD,IAAI,OAAO,QAAQ,UAAU;EAC5B,MAAM,aAAa,IAAI,KAAK,CAAC,CAAC,YAAY;EAC1C,IAAI,CAAC,YAAY,OAAO,KAAA;EACxB,MAAM,SAAS,WAAW,WAAW,IAAI,IACtC,OAAO,SAAS,YAAY,EAAE,IAC9B,OAAO,SAAS,YAAY,EAAE;EACjC,OAAO,OAAO,MAAM,MAAM,IAAI,KAAA,IAAY;CAC3C;AAGD;AAEA,MAAM,mBAAmB,UAAuC;CAC/D,MAAM,UAAU,oBAAoB,KAAK;CACzC,OAAO,YAAY,KAAA,IAAY,KAAA,IAAY,UAAU;AACtD;AAEA,MAAM,8BACL,WACmC;CACnC,IAAI,CAAC,OAAO,aAAa,QAAA,GAAOA,KAAAA,GAAAA,CAAG,CAAC,CAAC;CAErC,OAAOC,cAAAA,qBAAqB,YAAY,OAAO,YAC9C,IAAIC,KAAAA,YAA+B,eAAe;EACjD,MAAM,aAAa,IAAIC,KAAAA,cAAwB,CAAC;EAChD,MAAM,WAAW,IAAIA,KAAAA,cAAkC,CAAC;EAExD,MAAM,cACL,SACA,YACqB;GACrB,MAAM,UAAA,GAASC,KAAAA,mBAAAA,CAAmB;IACjC,SAAS;IACT,YAAA,GAAWC,KAAA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C9C,QAAQ,sBACT,CAAC;CACF;CACA,iBAAiBC,eAAAA;CAGjB,iBAAiB,aAAaC,cAAAA,sBAAsB,YAAY,UAAU;CAC1E,cAAc,EACb,QAAQ,IACR,UACA,QACA,OACA,aACA,SACA,kBACM;EACN,UAAU;EACV,MAAM;EACN,IAAI;EACJ,MAAM,GAAG,KAAK;EACd,MAAM,GAAG,KAAK;EACd,UAAU,GAAG;EACb;EACA;EACA;EACA,UAAU,GAAG,KAAK;EAClB;EACA;CACD;AACD,CAAC;;;;;;;;;;;;ACjEF,MAAa,kBAIP;CACL,UAAU;CACV,cAAc,QAAyB,oBAAoB,IAAI,KAAK;CACpE,eAAe,aAAa,qBAAqB,QAAQ;AAC1D"}
|
|
1
|
+
{"version":3,"file":"ethereum.js","names":["firstValueFrom","race","timer","map","of","getCachedObservable$","Observable","ReplaySubject","createWalletClient","custom","getWalletAccountId","getAddress","combineLatest","tap","shareReplay","getCaip10Addresses","getSessionCaip10s","distinctUntilChanged","createPlatformAccounts$","Observable","createMipdStore","shareReplay","createInjectedWallets$","getWalletId","isEip1193UserRejection","clearCachedObservable"],"sources":["../src/api/ethereum/accounts.ts","../src/api/ethereum/wallets.ts","../src/api/ethereum/plugin.ts"],"sourcesContent":["import {\n\tcombineLatest,\n\tdistinctUntilChanged,\n\tfirstValueFrom,\n\tmap,\n\tObservable,\n\tof,\n\tReplaySubject,\n\trace,\n\tshareReplay,\n\ttap,\n\ttimer,\n} from \"rxjs\";\nimport {\n\tcreateWalletClient,\n\tcustom,\n\ttype EIP1193Provider,\n\tgetAddress,\n} from \"viem\";\nimport { getWalletAccountId } from \"../../utils\";\nimport { getCachedObservable$ } from \"../../utils/getCachedObservable\";\nimport {\n\tcreatePlatformAccounts$,\n\tgetCaip10Addresses,\n\tgetSessionCaip10s,\n\ttype LiveWalletConnectWallet,\n} from \"../platformAccounts\";\nimport type { WalletConnectWallet } from \"../types\";\nimport type {\n\tEthereumAccount,\n\tEthereumInjectedWallet,\n\tEthereumWallet,\n} from \"./types\";\n\nconst normalizeEvmChainId = (value: unknown): number | undefined => {\n\tlet raw = value;\n\n\tif (typeof raw === \"string\" && raw.startsWith(\"eip155:\")) {\n\t\traw = raw.slice(\"eip155:\".length);\n\t}\n\n\tif (typeof raw === \"bigint\") {\n\t\treturn raw >= 0n ? Number(raw) : undefined;\n\t}\n\n\tif (typeof raw === \"number\") {\n\t\treturn Number.isInteger(raw) && raw >= 0 ? raw : undefined;\n\t}\n\n\tif (typeof raw === \"string\") {\n\t\tconst normalized = raw.trim().toLowerCase();\n\t\tif (!normalized) return undefined;\n\t\tconst parsed = normalized.startsWith(\"0x\")\n\t\t\t? Number.parseInt(normalized, 16)\n\t\t\t: Number.parseInt(normalized, 10);\n\t\treturn Number.isNaN(parsed) ? undefined : parsed;\n\t}\n\n\treturn undefined;\n};\n\nconst toCaipNetworkId = (value: unknown): string | undefined => {\n\tconst chainId = normalizeEvmChainId(value);\n\treturn chainId === undefined ? undefined : `eip155:${chainId}`;\n};\n\ntype ReportedAccounts = { addresses: string[]; chainId: number | undefined };\n\nconst lastReported = new WeakMap<EIP1193Provider, ReportedAccounts>();\n\nconst CHAIN_ANSWER_TIMEOUT_MS = 2_000;\n\nconst readChainId = (provider: EIP1193Provider) =>\n\tfirstValueFrom(\n\t\trace(\n\t\t\tprovider\n\t\t\t\t.request({ method: \"eth_chainId\" })\n\t\t\t\t.then(normalizeEvmChainId, () => undefined),\n\t\t\ttimer(CHAIN_ANSWER_TIMEOUT_MS).pipe(map(() => undefined)),\n\t\t),\n\t);\n\nexport const requestAccountsOnChain = async (provider: EIP1193Provider) => {\n\tconst accountChanges: string[][] = [];\n\tconst chainChanges: (number | undefined)[] = [];\n\tconst handleAccountsChanged = (addresses: string[]) => {\n\t\taccountChanges.push(addresses);\n\t};\n\tconst handleChainChanged = (chainId: unknown) => {\n\t\tchainChanges.push(normalizeEvmChainId(chainId));\n\t};\n\tprovider.on(\"accountsChanged\", handleAccountsChanged);\n\tprovider.on(\"chainChanged\", handleChainChanged);\n\ttry {\n\t\tconst addresses = await provider.request({\n\t\t\tmethod: \"eth_requestAccounts\",\n\t\t});\n\t\tconst chainChangesBeforeRead = chainChanges.length;\n\t\tconst chainId = await readChainId(provider);\n\t\tlastReported.set(provider, {\n\t\t\taddresses: accountChanges.at(-1) ?? addresses,\n\t\t\tchainId:\n\t\t\t\tchainChanges.length > chainChangesBeforeRead\n\t\t\t\t\t? chainChanges.at(-1)\n\t\t\t\t\t: chainId,\n\t\t});\n\t} finally {\n\t\tprovider.removeListener(\"accountsChanged\", handleAccountsChanged);\n\t\tprovider.removeListener(\"chainChanged\", handleChainChanged);\n\t}\n};\n\nconst getInjectedWalletAccounts$ = (\n\twallet: EthereumInjectedWallet,\n): Observable<EthereumAccount[]> => {\n\tif (!wallet.isConnected) return of([]);\n\n\treturn getCachedObservable$(`accounts:${wallet.id}`, () =>\n\t\tnew Observable<EthereumAccount[]>((subscriber) => {\n\t\t\tconst addresses$ = new ReplaySubject<string[]>(1);\n\t\t\tconst chainId$ = new ReplaySubject<number | undefined>(1);\n\t\t\tconst reported = lastReported.get(wallet.provider);\n\t\t\tif (reported) {\n\t\t\t\taddresses$.next(reported.addresses);\n\t\t\t\tchainId$.next(reported.chainId);\n\t\t\t}\n\n\t\t\tconst getAccount = (\n\t\t\t\taddress: string,\n\t\t\t\tchainId: number | undefined,\n\t\t\t): EthereumAccount => {\n\t\t\t\tconst client = createWalletClient({\n\t\t\t\t\taccount: address as `0x${string}`,\n\t\t\t\t\ttransport: custom(wallet.provider as EIP1193Provider),\n\t\t\t\t});\n\n\t\t\t\treturn {\n\t\t\t\t\tid: getWalletAccountId(wallet.id, address),\n\t\t\t\t\tplatform: \"ethereum\",\n\t\t\t\t\tclient,\n\t\t\t\t\taddress: getAddress(address),\n\t\t\t\t\tchainId,\n\t\t\t\t\twalletName: wallet.name,\n\t\t\t\t\twalletId: wallet.id,\n\t\t\t\t};\n\t\t\t};\n\n\t\t\tlet accountEvents = 0;\n\t\t\tlet chainEvents = 0;\n\n\t\t\tconst handleAccountsChanged = (addrs: string[]) => {\n\t\t\t\taccountEvents++;\n\t\t\t\taddresses$.next(addrs);\n\t\t\t};\n\n\t\t\tconst handleChainChanged = (chainIdHex: unknown) => {\n\t\t\t\tchainEvents++;\n\t\t\t\tchainId$.next(normalizeEvmChainId(chainIdHex));\n\t\t\t};\n\n\t\t\tconst handleConnect = (connectInfo: { chainId?: unknown }) => {\n\t\t\t\tchainEvents++;\n\t\t\t\tchainId$.next(normalizeEvmChainId(connectInfo?.chainId));\n\t\t\t};\n\n\t\t\tconst handleDisconnect = () => {\n\t\t\t\tchainEvents++;\n\t\t\t\tchainId$.next(undefined);\n\t\t\t};\n\n\t\t\t// Subscribe to provider events\n\t\t\twallet.provider.on(\"accountsChanged\", handleAccountsChanged);\n\t\t\twallet.provider.on(\"chainChanged\", handleChainChanged);\n\t\t\twallet.provider.on(\"connect\", handleConnect);\n\t\t\twallet.provider.on(\"disconnect\", handleDisconnect);\n\n\t\t\tconst accountEventsBeforeRead = accountEvents;\n\t\t\tconst accountsOutdated = () => accountEvents !== accountEventsBeforeRead;\n\t\t\twallet.provider\n\t\t\t\t.request({ method: \"eth_accounts\" })\n\t\t\t\t.then((addrs: string[]) => {\n\t\t\t\t\tif (!accountsOutdated()) addresses$.next(addrs);\n\t\t\t\t})\n\t\t\t\t.catch((err) => {\n\t\t\t\t\tconsole.error(\"Failed to get accounts\", err);\n\t\t\t\t\tif (!accountsOutdated()) addresses$.next([]);\n\t\t\t\t});\n\n\t\t\tconst chainEventsBeforeRead = chainEvents;\n\t\t\tconst chainOutdated = () => chainEvents !== chainEventsBeforeRead;\n\t\t\twallet.provider\n\t\t\t\t.request({ method: \"eth_chainId\" })\n\t\t\t\t.then((chainIdHex: unknown) => {\n\t\t\t\t\tif (!chainOutdated()) chainId$.next(normalizeEvmChainId(chainIdHex));\n\t\t\t\t})\n\t\t\t\t.catch(() => {\n\t\t\t\t\tif (!chainOutdated()) chainId$.next(undefined);\n\t\t\t\t});\n\n\t\t\t// Combine addresses + chainId into account list\n\t\t\tconst sub = combineLatest([addresses$, chainId$])\n\t\t\t\t.pipe(\n\t\t\t\t\ttap(([addresses, chainId]) =>\n\t\t\t\t\t\tlastReported.set(wallet.provider, { addresses, chainId }),\n\t\t\t\t\t),\n\t\t\t\t\tmap(([addresses, chainId]) =>\n\t\t\t\t\t\taddresses.map((addr) => getAccount(addr, chainId)),\n\t\t\t\t\t),\n\t\t\t\t)\n\t\t\t\t.subscribe(subscriber);\n\n\t\t\treturn () => {\n\t\t\t\twallet.provider.removeListener(\n\t\t\t\t\t\"accountsChanged\",\n\t\t\t\t\thandleAccountsChanged,\n\t\t\t\t);\n\t\t\t\twallet.provider.removeListener(\"chainChanged\", handleChainChanged);\n\t\t\t\twallet.provider.removeListener(\"connect\", handleConnect);\n\t\t\t\twallet.provider.removeListener(\"disconnect\", handleDisconnect);\n\t\t\t\tsub.unsubscribe();\n\t\t\t};\n\t\t}).pipe(shareReplay({ refCount: true, bufferSize: 1 })),\n\t);\n};\n\nconst wrapWalletConnectProvider = (\n\tprovider: EIP1193Provider,\n\tsessionTopic: string,\n\tcaipNetworkId: string | undefined,\n): EIP1193Provider => {\n\treturn new Proxy(provider, {\n\t\tget(target, prop, receiver) {\n\t\t\tif (prop !== \"request\") return Reflect.get(target, prop, receiver);\n\n\t\t\t// biome-ignore lint/suspicious/noExplicitAny: EIP-1193 request args\n\t\t\treturn (args: any) => {\n\t\t\t\tif (!args || typeof args !== \"object\" || !args.method)\n\t\t\t\t\treturn target.request(args);\n\t\t\t\t// Build a new object rather than mutating the caller's args: the\n\t\t\t\t// EIP-1193 request object is owned by viem and may be frozen or reused.\n\t\t\t\tconst next = { ...args };\n\t\t\t\tif (next.topic === undefined) next.topic = sessionTopic;\n\t\t\t\tif (next.chainId === undefined && caipNetworkId !== undefined)\n\t\t\t\t\tnext.chainId = caipNetworkId;\n\t\t\t\t// WalletConnect's UniversalProvider reads the target CAIP chain id from\n\t\t\t\t// the SECOND positional argument of `request(args, chainId)`, not from a\n\t\t\t\t// field on the args object. This AppKit instance has no native eip155\n\t\t\t\t// adapter, so the provider has no default chain — without the positional\n\t\t\t\t// chain id every call is rejected (\"Unsupported chains\" / \"Method not\n\t\t\t\t// found\"). Pass it positionally so the request routes to the session's\n\t\t\t\t// chain.\n\t\t\t\treturn (\n\t\t\t\t\ttarget.request as (a: unknown, chainId?: string) => Promise<unknown>\n\t\t\t\t)(next, caipNetworkId);\n\t\t\t};\n\t\t},\n\t});\n};\n\nconst sameAddresses = (a: string[], b: string[]) =>\n\ta.length === b.length && a.every((addr, i) => addr === b[i]);\n\nconst getWalletConnectAccounts$ = (\n\twallet: LiveWalletConnectWallet,\n): Observable<EthereumAccount[]> => {\n\tconst provider = wallet.appKit.getProvider(\"eip155\");\n\n\tif (!wallet.platforms.includes(\"ethereum\") || !provider?.session)\n\t\treturn of([]);\n\n\treturn getCachedObservable$(`accounts:${wallet.id}:ethereum:`, () =>\n\t\tnew Observable<EthereumAccount[]>((subscriber) => {\n\t\t\tconst caipNetworkId$ = new ReplaySubject<string | undefined>(1);\n\t\t\tconst addresses$ = new ReplaySubject<string[]>(1);\n\n\t\t\t// AppKit's getAccount(\"eip155\").allAccounts is empty because this AppKit\n\t\t\t// instance has no native eip155 adapter — eip155 runs through the\n\t\t\t// WalletConnect UniversalProvider, so the session is the source of truth\n\t\t\t// (mirrors the polkadot/solana AppKit paths). Accounts are CAIP-10 strings\n\t\t\t// (\"eip155:<chainRef>:<address>\"), one entry per chain, so dedupe to unique\n\t\t\t// addresses. Read live on each change so switching/adding accounts is\n\t\t\t// reflected.\n\t\t\tconst readAddresses = (): string[] => {\n\t\t\t\tconst session = provider.session;\n\t\t\t\tif (!session) return [];\n\t\t\t\tconst addresses = new Set<string>();\n\t\t\t\tfor (const raw of getCaip10Addresses(\n\t\t\t\t\tgetSessionCaip10s(session, \"eip155\"),\n\t\t\t\t)) {\n\t\t\t\t\ttry {\n\t\t\t\t\t\taddresses.add(getAddress(raw));\n\t\t\t\t\t} catch {\n\t\t\t\t\t\t// skip malformed CAIP-10 address\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\treturn [...addresses];\n\t\t\t};\n\n\t\t\t// Derive the CAIP network id from the session's eip155 CAIP-10 accounts\n\t\t\t// (\"eip155:<ref>:<addr>\") as a synchronous fallback for eth_chainId.\n\t\t\tconst readCaipNetworkId = (): string | undefined => {\n\t\t\t\tconst session = provider.session;\n\t\t\t\tif (!session) return undefined;\n\t\t\t\tfor (const account of getSessionCaip10s(session, \"eip155\")) {\n\t\t\t\t\tconst ref = account.split(\":\")[1];\n\t\t\t\t\tif (ref) return `eip155:${ref}`;\n\t\t\t\t}\n\t\t\t\treturn undefined;\n\t\t\t};\n\n\t\t\tconst handleChainChanged = (chainId: unknown) => {\n\t\t\t\tconst caipNetworkId = toCaipNetworkId(chainId);\n\t\t\t\tif (caipNetworkId) {\n\t\t\t\t\tcaipNetworkId$.next(caipNetworkId);\n\t\t\t\t}\n\t\t\t};\n\n\t\t\tconst handleAccountsChanged = () => addresses$.next(readAddresses());\n\n\t\t\tprovider.on(\"chainChanged\", handleChainChanged);\n\t\t\tprovider.on(\"accountsChanged\", handleAccountsChanged);\n\t\t\tprovider.on(\"session_update\", handleAccountsChanged);\n\t\t\t// Seed the chain id from the session synchronously so the account list can\n\t\t\t// surface even if eth_chainId never resolves; eth_chainId then refines it.\n\t\t\t// Without this seed, combineLatest below never fires when eth_chainId\n\t\t\t// rejects or returns an unparseable value, and the accounts silently never\n\t\t\t// appear (the injected path seeds undefined on failure for the same reason).\n\t\t\tcaipNetworkId$.next(readCaipNetworkId());\n\t\t\tprovider\n\t\t\t\t.request({ method: \"eth_chainId\" })\n\t\t\t\t.then(handleChainChanged)\n\t\t\t\t.catch(() => {});\n\t\t\taddresses$.next(readAddresses());\n\n\t\t\tconst sub = combineLatest([\n\t\t\t\tcaipNetworkId$.pipe(distinctUntilChanged()),\n\t\t\t\taddresses$.pipe(distinctUntilChanged(sameAddresses)),\n\t\t\t])\n\t\t\t\t.pipe(\n\t\t\t\t\tmap(([caipNetworkId, addresses]) => {\n\t\t\t\t\t\tconst chainId = normalizeEvmChainId(caipNetworkId);\n\t\t\t\t\t\tconst transport = custom(\n\t\t\t\t\t\t\twrapWalletConnectProvider(\n\t\t\t\t\t\t\t\tprovider as unknown as EIP1193Provider,\n\t\t\t\t\t\t\t\t// biome-ignore lint/style/noNonNullAssertion: legacy\n\t\t\t\t\t\t\t\tprovider.session!.topic,\n\t\t\t\t\t\t\t\tcaipNetworkId,\n\t\t\t\t\t\t\t),\n\t\t\t\t\t\t);\n\t\t\t\t\t\treturn addresses.map((addr): EthereumAccount => {\n\t\t\t\t\t\t\tconst client = createWalletClient({\n\t\t\t\t\t\t\t\taccount: addr as `0x${string}`,\n\t\t\t\t\t\t\t\ttransport,\n\t\t\t\t\t\t\t});\n\n\t\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\t\tid: getWalletAccountId(wallet.id, addr),\n\t\t\t\t\t\t\t\tplatform: \"ethereum\",\n\t\t\t\t\t\t\t\twalletName: wallet.name,\n\t\t\t\t\t\t\t\twalletId: wallet.id,\n\t\t\t\t\t\t\t\taddress: addr as `0x${string}`,\n\t\t\t\t\t\t\t\tclient,\n\t\t\t\t\t\t\t\tchainId,\n\t\t\t\t\t\t\t};\n\t\t\t\t\t\t});\n\t\t\t\t\t}),\n\t\t\t\t)\n\t\t\t\t.subscribe(subscriber);\n\n\t\t\treturn () => {\n\t\t\t\tprovider.off(\"chainChanged\", handleChainChanged);\n\t\t\t\tprovider.off(\"accountsChanged\", handleAccountsChanged);\n\t\t\t\tprovider.off(\"session_update\", handleAccountsChanged);\n\t\t\t\tsub.unsubscribe();\n\t\t\t};\n\t\t}).pipe(shareReplay({ refCount: true, bufferSize: 1 })),\n\t);\n};\n\nexport const getEthereumAccounts$ = (\n\tethereumWallets: Observable<(EthereumWallet | WalletConnectWallet)[]>,\n) =>\n\tcreatePlatformAccounts$({\n\t\twallets$: ethereumWallets,\n\t\tgetInjectedAccounts$: getInjectedWalletAccounts$,\n\t\tgetWalletConnectAccounts$,\n\t\t// Re-emit on chain switches, not just account id changes.\n\t\taccountChangeKey: (account) => `${account.id}|${account.chainId ?? \"\"}`,\n\t});\n","import {\n\tcreateStore as createMipdStore,\n\ttype EIP6963ProviderDetail,\n} from \"mipd\";\nimport { Observable, shareReplay } from \"rxjs\";\nimport type { EIP1193Provider } from \"viem\";\nimport { clearCachedObservable } from \"../../utils/getCachedObservable\";\nimport { getWalletId } from \"../../utils/WalletId\";\nimport { isEip1193UserRejection } from \"../errors\";\nimport { createInjectedWallets$ } from \"../injectedWallets\";\nimport type { KheopskitStore } from \"../store\";\nimport { requestAccountsOnChain } from \"./accounts\";\nimport type { EthereumInjectedWallet } from \"./types\";\n\n/**\n * Observable that emits EIP-6963 provider details from injected wallets.\n * Returns empty array during SSR since browser wallet APIs are not available.\n */\nconst providersDetails$ = new Observable<EIP6963ProviderDetail[]>(\n\t(subscriber) => {\n\t\t// Guard against SSR - mipd requires browser APIs\n\t\tif (typeof window === \"undefined\") {\n\t\t\tsubscriber.next([]);\n\t\t\treturn () => {};\n\t\t}\n\n\t\tconst mipdStore = createMipdStore();\n\n\t\tconst unsubscribe = mipdStore.subscribe((providerDetails) => {\n\t\t\tsubscriber.next(providerDetails as EIP6963ProviderDetail[]);\n\t\t});\n\n\t\tconst providerDetails = mipdStore.getProviders();\n\n\t\tsubscriber.next(providerDetails as EIP6963ProviderDetail[]);\n\n\t\treturn () => {\n\t\t\tunsubscribe();\n\t\t\tmipdStore.destroy();\n\t\t};\n\t},\n).pipe(shareReplay({ refCount: true, bufferSize: 1 }));\n\n// The shared WalletConnect connector is emitted once by core (see\n// `getWallets$`), not per platform — so this returns only injected wallets.\nexport const getEthereumWallets$ = (store: KheopskitStore) =>\n\tcreateInjectedWallets$<EIP6963ProviderDetail, EthereumInjectedWallet>(store, {\n\t\tsources$: providersDetails$,\n\t\tgetWalletId: (pd) => getWalletId(\"ethereum\", pd.info.rdns),\n\t\tconnect: (pd) => requestAccountsOnChain(pd.provider as EIP1193Provider),\n\t\tisUserRejection: isEip1193UserRejection,\n\t\t// Drop the cached account observable so a later reconnect rebuilds it\n\t\t// against the current provider, not a stale closure.\n\t\tonDisconnected: (walletId) => clearCachedObservable(`accounts:${walletId}`),\n\t\tbuildWallet: ({\n\t\t\tsource: pd,\n\t\t\twalletId,\n\t\t\tstatus,\n\t\t\terror,\n\t\t\tisConnected,\n\t\t\tconnect,\n\t\t\tdisconnect,\n\t\t}) => ({\n\t\t\tplatform: \"ethereum\",\n\t\t\ttype: \"injected\",\n\t\t\tid: walletId,\n\t\t\tname: pd.info.name,\n\t\t\ticon: pd.info.icon,\n\t\t\tprovider: pd.provider as EIP1193Provider,\n\t\t\tstatus,\n\t\t\terror,\n\t\t\tisConnected,\n\t\t\tsourceId: pd.info.rdns,\n\t\t\tconnect,\n\t\t\tdisconnect,\n\t\t}),\n\t});\n","import type { KheopskitPlatform, PlatformContext } from \"../types\";\nimport { getEthereumAccounts$ } from \"./accounts\";\nimport type { EthereumAccount, EthereumWallet } from \"./types\";\nimport { getEthereumWallets$ } from \"./wallets\";\n\n/**\n * Ethereum platform plugin. Pass to `createKheopskit({ platforms: [ethereum()] })`.\n *\n * @example\n * ```ts\n * import { ethereum } from \"@kheopskit/core/ethereum\";\n * ethereum();\n * ```\n */\nexport const ethereum = (): KheopskitPlatform<\n\t\"ethereum\",\n\tEthereumWallet,\n\tEthereumAccount\n> => ({\n\tplatform: \"ethereum\",\n\tgetWallets$: (ctx: PlatformContext) => getEthereumWallets$(ctx.store),\n\tgetAccounts$: (wallets$) => getEthereumAccounts$(wallets$),\n});\n"],"mappings":";;;;;;;;;AAkCA,MAAM,uBAAuB,UAAuC;CACnE,IAAI,MAAM;CAEV,IAAI,OAAO,QAAQ,YAAY,IAAI,WAAW,SAAS,GACtD,MAAM,IAAI,MAAM,CAAgB;CAGjC,IAAI,OAAO,QAAQ,UAClB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AC5E,UAAU;CACV,cAAc,OAAOC,cAAAA,YAAY,YAAY,GAAG,KAAK,IAAI;CACzD,UAAU,OAAO,uBAAuB,GAAG,QAA2B;CACtE,iBAAiBC,eAAAA;CAGjB,iBAAiB,aAAaC,cAAAA,sBAAsB,YAAY,UAAU;CAC1E,cAAc,EACb,QAAQ,IACR,UACA,QACA,OACA,aACA,SACA,kBACM;EACN,UAAU;EACV,MAAM;EACN,IAAI;EACJ,MAAM,GAAG,KAAK;EACd,MAAM,GAAG,KAAK;EACd,UAAU,GAAG;EACb;EACA;EACA;EACA,UAAU,GAAG,KAAK;EAClB;EACA;CACD;AACD,CAAC;;;;;;;;;;;;AC9DF,MAAa,kBAIP;CACL,UAAU;CACV,cAAc,QAAyB,oBAAoB,IAAI,KAAK;CACpE,eAAe,aAAa,qBAAqB,QAAQ;AAC1D"}
|
package/dist/ethereum.mjs
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { d as getCachedObservable$, i as getWalletId, l as clearCachedObservable, n as isWalletConnectWallet, t as isInjectedWallet } from "./types-DcpmEGhO.mjs";
|
|
2
2
|
import { d as isEthereumAddress, i as getSessionCaip10s, o as getWalletAccountId, r as getCaip10Addresses, t as createPlatformAccounts$ } from "./platformAccounts-C1sFv82e.mjs";
|
|
3
|
-
import { i as isEip1193UserRejection } from "./errors-
|
|
4
|
-
import { t as createInjectedWallets$ } from "./injectedWallets-
|
|
5
|
-
import { Observable, ReplaySubject, combineLatest, distinctUntilChanged, map, of, shareReplay } from "rxjs";
|
|
3
|
+
import { i as isEip1193UserRejection } from "./errors-DszTFbnO.mjs";
|
|
4
|
+
import { t as createInjectedWallets$ } from "./injectedWallets-CUo7rVqB.mjs";
|
|
5
|
+
import { Observable, ReplaySubject, combineLatest, distinctUntilChanged, firstValueFrom, map, of, race, shareReplay, tap, timer } from "rxjs";
|
|
6
6
|
import { createWalletClient, custom, getAddress } from "viem";
|
|
7
7
|
import { createStore } from "mipd";
|
|
8
8
|
//#region src/api/ethereum/accounts.ts
|
|
@@ -22,11 +22,43 @@ const toCaipNetworkId = (value) => {
|
|
|
22
22
|
const chainId = normalizeEvmChainId(value);
|
|
23
23
|
return chainId === void 0 ? void 0 : `eip155:${chainId}`;
|
|
24
24
|
};
|
|
25
|
+
const lastReported = /* @__PURE__ */ new WeakMap();
|
|
26
|
+
const CHAIN_ANSWER_TIMEOUT_MS = 2e3;
|
|
27
|
+
const readChainId = (provider) => firstValueFrom(race(provider.request({ method: "eth_chainId" }).then(normalizeEvmChainId, () => void 0), timer(CHAIN_ANSWER_TIMEOUT_MS).pipe(map(() => void 0))));
|
|
28
|
+
const requestAccountsOnChain = async (provider) => {
|
|
29
|
+
const accountChanges = [];
|
|
30
|
+
const chainChanges = [];
|
|
31
|
+
const handleAccountsChanged = (addresses) => {
|
|
32
|
+
accountChanges.push(addresses);
|
|
33
|
+
};
|
|
34
|
+
const handleChainChanged = (chainId) => {
|
|
35
|
+
chainChanges.push(normalizeEvmChainId(chainId));
|
|
36
|
+
};
|
|
37
|
+
provider.on("accountsChanged", handleAccountsChanged);
|
|
38
|
+
provider.on("chainChanged", handleChainChanged);
|
|
39
|
+
try {
|
|
40
|
+
const addresses = await provider.request({ method: "eth_requestAccounts" });
|
|
41
|
+
const chainChangesBeforeRead = chainChanges.length;
|
|
42
|
+
const chainId = await readChainId(provider);
|
|
43
|
+
lastReported.set(provider, {
|
|
44
|
+
addresses: accountChanges.at(-1) ?? addresses,
|
|
45
|
+
chainId: chainChanges.length > chainChangesBeforeRead ? chainChanges.at(-1) : chainId
|
|
46
|
+
});
|
|
47
|
+
} finally {
|
|
48
|
+
provider.removeListener("accountsChanged", handleAccountsChanged);
|
|
49
|
+
provider.removeListener("chainChanged", handleChainChanged);
|
|
50
|
+
}
|
|
51
|
+
};
|
|
25
52
|
const getInjectedWalletAccounts$ = (wallet) => {
|
|
26
53
|
if (!wallet.isConnected) return of([]);
|
|
27
54
|
return getCachedObservable$(`accounts:${wallet.id}`, () => new Observable((subscriber) => {
|
|
28
55
|
const addresses$ = new ReplaySubject(1);
|
|
29
56
|
const chainId$ = new ReplaySubject(1);
|
|
57
|
+
const reported = lastReported.get(wallet.provider);
|
|
58
|
+
if (reported) {
|
|
59
|
+
addresses$.next(reported.addresses);
|
|
60
|
+
chainId$.next(reported.chainId);
|
|
61
|
+
}
|
|
30
62
|
const getAccount = (address, chainId) => {
|
|
31
63
|
const client = createWalletClient({
|
|
32
64
|
account: address,
|
|
@@ -42,27 +74,51 @@ const getInjectedWalletAccounts$ = (wallet) => {
|
|
|
42
74
|
walletId: wallet.id
|
|
43
75
|
};
|
|
44
76
|
};
|
|
77
|
+
let accountEvents = 0;
|
|
78
|
+
let chainEvents = 0;
|
|
45
79
|
const handleAccountsChanged = (addrs) => {
|
|
80
|
+
accountEvents++;
|
|
46
81
|
addresses$.next(addrs);
|
|
47
82
|
};
|
|
48
83
|
const handleChainChanged = (chainIdHex) => {
|
|
84
|
+
chainEvents++;
|
|
49
85
|
chainId$.next(normalizeEvmChainId(chainIdHex));
|
|
50
86
|
};
|
|
87
|
+
const handleConnect = (connectInfo) => {
|
|
88
|
+
chainEvents++;
|
|
89
|
+
chainId$.next(normalizeEvmChainId(connectInfo?.chainId));
|
|
90
|
+
};
|
|
51
91
|
const handleDisconnect = () => {
|
|
92
|
+
chainEvents++;
|
|
52
93
|
chainId$.next(void 0);
|
|
53
94
|
};
|
|
54
95
|
wallet.provider.on("accountsChanged", handleAccountsChanged);
|
|
55
96
|
wallet.provider.on("chainChanged", handleChainChanged);
|
|
97
|
+
wallet.provider.on("connect", handleConnect);
|
|
56
98
|
wallet.provider.on("disconnect", handleDisconnect);
|
|
57
|
-
|
|
99
|
+
const accountEventsBeforeRead = accountEvents;
|
|
100
|
+
const accountsOutdated = () => accountEvents !== accountEventsBeforeRead;
|
|
101
|
+
wallet.provider.request({ method: "eth_accounts" }).then((addrs) => {
|
|
102
|
+
if (!accountsOutdated()) addresses$.next(addrs);
|
|
103
|
+
}).catch((err) => {
|
|
58
104
|
console.error("Failed to get accounts", err);
|
|
59
|
-
addresses$.next([]);
|
|
105
|
+
if (!accountsOutdated()) addresses$.next([]);
|
|
106
|
+
});
|
|
107
|
+
const chainEventsBeforeRead = chainEvents;
|
|
108
|
+
const chainOutdated = () => chainEvents !== chainEventsBeforeRead;
|
|
109
|
+
wallet.provider.request({ method: "eth_chainId" }).then((chainIdHex) => {
|
|
110
|
+
if (!chainOutdated()) chainId$.next(normalizeEvmChainId(chainIdHex));
|
|
111
|
+
}).catch(() => {
|
|
112
|
+
if (!chainOutdated()) chainId$.next(void 0);
|
|
60
113
|
});
|
|
61
|
-
|
|
62
|
-
|
|
114
|
+
const sub = combineLatest([addresses$, chainId$]).pipe(tap(([addresses, chainId]) => lastReported.set(wallet.provider, {
|
|
115
|
+
addresses,
|
|
116
|
+
chainId
|
|
117
|
+
})), map(([addresses, chainId]) => addresses.map((addr) => getAccount(addr, chainId)))).subscribe(subscriber);
|
|
63
118
|
return () => {
|
|
64
119
|
wallet.provider.removeListener("accountsChanged", handleAccountsChanged);
|
|
65
120
|
wallet.provider.removeListener("chainChanged", handleChainChanged);
|
|
121
|
+
wallet.provider.removeListener("connect", handleConnect);
|
|
66
122
|
wallet.provider.removeListener("disconnect", handleDisconnect);
|
|
67
123
|
sub.unsubscribe();
|
|
68
124
|
};
|
|
@@ -182,9 +238,7 @@ const providersDetails$ = new Observable((subscriber) => {
|
|
|
182
238
|
const getEthereumWallets$ = (store) => createInjectedWallets$(store, {
|
|
183
239
|
sources$: providersDetails$,
|
|
184
240
|
getWalletId: (pd) => getWalletId("ethereum", pd.info.rdns),
|
|
185
|
-
connect:
|
|
186
|
-
await pd.provider.request({ method: "eth_requestAccounts" });
|
|
187
|
-
},
|
|
241
|
+
connect: (pd) => requestAccountsOnChain(pd.provider),
|
|
188
242
|
isUserRejection: isEip1193UserRejection,
|
|
189
243
|
onDisconnected: (walletId) => clearCachedObservable(`accounts:${walletId}`),
|
|
190
244
|
buildWallet: ({ source: pd, walletId, status, error, isConnected, connect, disconnect }) => ({
|