@kheopskit/core 0.1.1 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.mjs CHANGED
@@ -105,6 +105,7 @@ var hydrateAccount = (cached) => {
105
105
  return {
106
106
  id: cached.id,
107
107
  platform: "polkadot",
108
+ type: cached.polkadotAccountType ?? "sr25519",
108
109
  address: cached.address,
109
110
  name: cached.name,
110
111
  walletId: cached.walletId,
@@ -119,6 +120,7 @@ var hydrateAccount = (cached) => {
119
120
  id: cached.id,
120
121
  platform: "ethereum",
121
122
  address: cached.address,
123
+ chainId: cached.chainId,
122
124
  walletId: cached.walletId,
123
125
  walletName: cached.walletName,
124
126
  isWalletDefault: false,
@@ -141,6 +143,8 @@ var serializeAccount = (account) => ({
141
143
  platform: account.platform,
142
144
  address: account.address,
143
145
  name: "name" in account ? account.name : void 0,
146
+ chainId: account.platform === "ethereum" ? account.chainId : void 0,
147
+ polkadotAccountType: account.platform === "polkadot" ? account.type : void 0,
144
148
  walletId: account.walletId,
145
149
  walletName: account.walletName
146
150
  });
@@ -430,12 +434,28 @@ var DEFAULT_STORAGE_KEY = "kheopskit";
430
434
  var DEFAULT_CONFIG = {
431
435
  autoReconnect: true,
432
436
  platforms: ["polkadot"],
437
+ polkadotAccountTypes: ["sr25519", "ed25519", "ecdsa"],
433
438
  debug: false,
434
439
  storageKey: DEFAULT_STORAGE_KEY,
435
440
  hydrationGracePeriod: 500
436
441
  };
442
+ var VALID_POLKADOT_ACCOUNT_TYPES = /* @__PURE__ */ new Set([
443
+ "sr25519",
444
+ "ed25519",
445
+ "ecdsa",
446
+ "ethereum"
447
+ ]);
437
448
  var resolveConfig = (config) => {
438
- return Object.assign({}, DEFAULT_CONFIG, config);
449
+ const resolved = Object.assign({}, DEFAULT_CONFIG, config);
450
+ const invalid = resolved.polkadotAccountTypes.filter(
451
+ (t) => !VALID_POLKADOT_ACCOUNT_TYPES.has(t)
452
+ );
453
+ if (invalid.length > 0) {
454
+ console.warn(
455
+ `[kheopskit] Unknown polkadotAccountTypes: ${JSON.stringify(invalid)}. Valid values: "sr25519", "ed25519", "ecdsa", "ethereum".`
456
+ );
457
+ }
458
+ return resolved;
439
459
  };
440
460
 
441
461
  // src/api/kheopskit.ts
@@ -693,12 +713,37 @@ var getWalletAccountId = (walletId, address) => {
693
713
  };
694
714
 
695
715
  // src/api/ethereum/accounts.ts
716
+ var normalizeEvmChainId = (value) => {
717
+ let raw = value;
718
+ if (typeof raw === "string" && raw.startsWith("eip155:")) {
719
+ raw = raw.slice("eip155:".length);
720
+ }
721
+ if (typeof raw === "bigint") {
722
+ return raw >= 0n ? Number(raw) : void 0;
723
+ }
724
+ if (typeof raw === "number") {
725
+ return Number.isInteger(raw) && raw >= 0 ? raw : void 0;
726
+ }
727
+ if (typeof raw === "string") {
728
+ const normalized = raw.trim().toLowerCase();
729
+ if (!normalized) return void 0;
730
+ const parsed = normalized.startsWith("0x") ? Number.parseInt(normalized, 16) : Number.parseInt(normalized, 10);
731
+ return Number.isNaN(parsed) ? void 0 : parsed;
732
+ }
733
+ return void 0;
734
+ };
735
+ var toCaipNetworkId = (value) => {
736
+ const chainId = normalizeEvmChainId(value);
737
+ return chainId === void 0 ? void 0 : `eip155:${chainId}`;
738
+ };
696
739
  var getInjectedWalletAccounts$ = (wallet) => {
697
740
  if (!wallet.isConnected) return of2([]);
698
741
  return getCachedObservable$(
699
742
  `accounts:${wallet.id}`,
700
743
  () => new Observable3((subscriber) => {
701
- const getAccount = (address, i) => {
744
+ const addresses$ = new ReplaySubject(1);
745
+ const chainId$ = new ReplaySubject(1);
746
+ const getAccount = (address, i, chainId) => {
702
747
  const client = createWalletClient({
703
748
  account: address,
704
749
  transport: custom(wallet.provider)
@@ -708,26 +753,42 @@ var getInjectedWalletAccounts$ = (wallet) => {
708
753
  platform: "ethereum",
709
754
  client,
710
755
  address: getAddress(address),
756
+ chainId,
711
757
  walletName: wallet.name,
712
758
  walletId: wallet.id,
713
759
  isWalletDefault: i === 0
714
760
  };
715
761
  };
716
- const handleAccountsChanged = (addresses) => {
717
- subscriber.next(addresses.map(getAccount));
762
+ const handleAccountsChanged = (addrs) => {
763
+ addresses$.next(addrs);
764
+ };
765
+ const handleChainChanged = (chainIdHex) => {
766
+ chainId$.next(normalizeEvmChainId(chainIdHex));
767
+ };
768
+ const handleDisconnect = () => {
769
+ chainId$.next(void 0);
718
770
  };
719
771
  wallet.provider.on("accountsChanged", handleAccountsChanged);
720
- wallet.provider.request({ method: "eth_accounts" }).then((addresses) => {
721
- subscriber.next(addresses.map(getAccount));
722
- }).catch((err) => {
772
+ wallet.provider.on("chainChanged", handleChainChanged);
773
+ wallet.provider.on("disconnect", handleDisconnect);
774
+ wallet.provider.request({ method: "eth_accounts" }).then((addrs) => addresses$.next(addrs)).catch((err) => {
723
775
  console.error("Failed to get accounts", err);
724
- subscriber.next([]);
776
+ addresses$.next([]);
725
777
  });
778
+ wallet.provider.request({ method: "eth_chainId" }).then(handleChainChanged).catch(() => chainId$.next(void 0));
779
+ const sub = combineLatest3([addresses$, chainId$]).pipe(
780
+ map3(
781
+ ([addresses, chainId]) => addresses.map((addr, i) => getAccount(addr, i, chainId))
782
+ )
783
+ ).subscribe(subscriber);
726
784
  return () => {
727
785
  wallet.provider.removeListener(
728
786
  "accountsChanged",
729
787
  handleAccountsChanged
730
788
  );
789
+ wallet.provider.removeListener("chainChanged", handleChainChanged);
790
+ wallet.provider.removeListener("disconnect", handleDisconnect);
791
+ sub.unsubscribe();
731
792
  };
732
793
  }).pipe(shareReplay3({ refCount: true, bufferSize: 1 }))
733
794
  );
@@ -756,24 +817,29 @@ var getAppKitAccounts$ = (wallet) => {
756
817
  () => new Observable3((subscriber) => {
757
818
  const caipNetworkId$ = new ReplaySubject(1);
758
819
  const handleChainChanged = (chainId) => {
759
- caipNetworkId$.next(`eip155:${chainId}`);
820
+ const caipNetworkId = toCaipNetworkId(chainId);
821
+ if (caipNetworkId) {
822
+ caipNetworkId$.next(caipNetworkId);
823
+ }
760
824
  };
761
825
  provider.on("chainChanged", handleChainChanged);
762
826
  provider.request({ method: "eth_chainId" }).then(handleChainChanged);
763
827
  const sub = caipNetworkId$.pipe(
764
828
  distinctUntilChanged2(),
765
- map3(
766
- (caipNetworkId) => custom(
829
+ map3((caipNetworkId) => {
830
+ const chainId = normalizeEvmChainId(caipNetworkId);
831
+ const transport = custom(
767
832
  wrapWalletConnectProvider(
768
833
  provider,
769
834
  // biome-ignore lint/style/noNonNullAssertion: legacy
770
835
  provider.session.topic,
771
836
  caipNetworkId
772
837
  )
773
- )
774
- ),
838
+ );
839
+ return { transport, chainId };
840
+ }),
775
841
  map3(
776
- (transport) => account.allAccounts.map((acc, i) => {
842
+ ({ transport, chainId }) => account.allAccounts.map((acc, i) => {
777
843
  const client = createWalletClient({
778
844
  account: acc.address,
779
845
  transport
@@ -785,6 +851,7 @@ var getAppKitAccounts$ = (wallet) => {
785
851
  walletId: wallet.id,
786
852
  address: acc.address,
787
853
  client,
854
+ chainId,
788
855
  isWalletDefault: i === 0
789
856
  };
790
857
  })
@@ -819,7 +886,9 @@ var getEthereumAccounts$ = (ethereumWallets) => new Observable3((subscriber) =>
819
886
  );
820
887
  var isSameAccountsList = (a, b) => {
821
888
  if (a.length !== b.length) return false;
822
- return a.every((account, i) => account.id === b[i]?.id);
889
+ return a.every(
890
+ (account, i) => account.id === b[i]?.id && account.chainId === b[i]?.chainId
891
+ );
823
892
  };
824
893
 
825
894
  // src/api/polkadot/accounts.ts
@@ -841,6 +910,7 @@ var getInjectedWalletAccounts$2 = (wallet) => {
841
910
  const getAccount = (account) => ({
842
911
  id: getWalletAccountId(wallet.id, account.address),
843
912
  ...account,
913
+ type: account.type ?? "sr25519",
844
914
  platform: "polkadot",
845
915
  walletName: wallet.name,
846
916
  walletId: wallet.id
@@ -910,12 +980,19 @@ var getAppKitAccounts$2 = (wallet) => {
910
980
  polkadotSigner: getAppKitPolkadotSigner(wallet.appKit, acc.address),
911
981
  genesisHash: null,
912
982
  name: `${wallet.name} Polkadot`,
983
+ // WalletConnect (Reown AppKit) doesn't expose account key type;
984
+ // default to sr25519, which is the most common Polkadot key type.
913
985
  type: "sr25519"
914
986
  })
915
987
  )
916
988
  );
917
989
  };
918
- var getPolkadotAccounts$ = (polkadotWallets$) => new Observable4((subscriber) => {
990
+ var getPolkadotAccounts$ = (polkadotWallets$, polkadotAccountTypes) => new Observable4((subscriber) => {
991
+ if (polkadotAccountTypes.length === 0) {
992
+ console.warn(
993
+ "[kheopskit] config.polkadotAccountTypes is empty; all Polkadot accounts will be filtered out."
994
+ );
995
+ }
919
996
  const sub = polkadotWallets$.pipe(
920
997
  map4((wallets) => wallets.filter((w) => w.isConnected)),
921
998
  switchMap3(
@@ -924,7 +1001,9 @@ var getPolkadotAccounts$ = (polkadotWallets$) => new Observable4((subscriber) =>
924
1001
  ...wallets.filter((w) => w.type === "appKit").map(getAppKitAccounts$2)
925
1002
  ]) : of3([])
926
1003
  ),
927
- map4((accounts) => accounts.flat()),
1004
+ map4(
1005
+ (accounts) => accounts.flat().filter((account) => polkadotAccountTypes.includes(account.type))
1006
+ ),
928
1007
  distinctUntilChanged3(isSameAccountsList2)
929
1008
  ).subscribe(subscriber);
930
1009
  return () => {
@@ -946,7 +1025,8 @@ var getAccounts$ = (config, wallets) => {
946
1025
  return getPolkadotAccounts$(
947
1026
  wallets.pipe(
948
1027
  map5((w) => w.filter((w2) => w2.platform === "polkadot"))
949
- )
1028
+ ),
1029
+ config.polkadotAccountTypes
950
1030
  );
951
1031
  case "ethereum":
952
1032
  return getEthereumAccounts$(
@@ -970,6 +1050,34 @@ var getAccounts$ = (config, wallets) => {
970
1050
  // src/api/store.ts
971
1051
  import { uniq } from "lodash-es";
972
1052
  var DEFAULT_SETTINGS = {};
1053
+ var toCompactPolkadotAccountType = (type) => {
1054
+ switch (type) {
1055
+ case "sr25519":
1056
+ return 0;
1057
+ case "ed25519":
1058
+ return 1;
1059
+ case "ecdsa":
1060
+ return 2;
1061
+ case "ethereum":
1062
+ return 3;
1063
+ default:
1064
+ return null;
1065
+ }
1066
+ };
1067
+ var fromCompactPolkadotAccountType = (type) => {
1068
+ switch (type) {
1069
+ case 0:
1070
+ return "sr25519";
1071
+ case 1:
1072
+ return "ed25519";
1073
+ case 2:
1074
+ return "ecdsa";
1075
+ case 3:
1076
+ return "ethereum";
1077
+ default:
1078
+ return void 0;
1079
+ }
1080
+ };
973
1081
  var createKheopskitStore = (options = {}) => {
974
1082
  const { ssrCookies, storageKey = DEFAULT_STORAGE_KEY } = options;
975
1083
  const storage = ssrCookies !== void 0 ? createCompactCookieStorage(ssrCookies) : safeLocalStorage;
@@ -1045,7 +1153,9 @@ var toCompactStore = (data) => {
1045
1153
  (account) => [
1046
1154
  account.walletId,
1047
1155
  account.address,
1048
- account.name ?? null
1156
+ account.name ?? null,
1157
+ account.chainId ?? null,
1158
+ toCompactPolkadotAccountType(account.polkadotAccountType)
1049
1159
  ]
1050
1160
  );
1051
1161
  return {
@@ -1070,13 +1180,15 @@ var fromCompactStore = (data) => {
1070
1180
  };
1071
1181
  });
1072
1182
  const accounts = (data.a ?? []).map((item) => {
1073
- const [walletId, address, name] = item;
1183
+ const [walletId, address, name, chainId, polkadotAccountType] = item;
1074
1184
  const { platform } = parseWalletId(walletId);
1075
1185
  return {
1076
1186
  id: getWalletAccountId(walletId, address),
1077
1187
  platform,
1078
1188
  address,
1079
1189
  name: name ?? void 0,
1190
+ chainId: chainId ?? void 0,
1191
+ polkadotAccountType: platform === "polkadot" ? fromCompactPolkadotAccountType(polkadotAccountType) : void 0,
1080
1192
  walletId,
1081
1193
  walletName: walletNameMap.get(walletId) ?? walletId
1082
1194
  };
@@ -1435,7 +1547,9 @@ var getKheopskit$ = (config, ssrCookies, existingStore) => {
1435
1547
  }
1436
1548
  return wallet;
1437
1549
  });
1438
- const cachedAccounts = cachedState.accounts.map(hydrateAccount);
1550
+ const cachedAccounts = cachedState.accounts.map(hydrateAccount).filter(
1551
+ (account) => account.platform !== "polkadot" || kc.polkadotAccountTypes.includes(account.type)
1552
+ );
1439
1553
  if (kc.debug && cachedWallets.length > 0) {
1440
1554
  console.debug("[kheopskit] hydrating from cache:", {
1441
1555
  wallets: cachedWallets.length,
@@ -1567,7 +1681,9 @@ var getKheopskit$ = (config, ssrCookies, existingStore) => {
1567
1681
  var arraysEqual = (a, b) => a.length === b.length && a.every((v, i) => v === b[i]);
1568
1682
  var statesEqual = (a, b) => a.isHydrating === b.isHydrating && a.wallets.length === b.wallets.length && a.accounts.length === b.accounts.length && a.wallets.every(
1569
1683
  (w, i) => w.id === b.wallets[i]?.id && w.isConnected === b.wallets[i]?.isConnected
1570
- ) && a.accounts.every((acc, i) => acc.id === b.accounts[i]?.id);
1684
+ ) && a.accounts.every(
1685
+ (acc, i) => acc.id === b.accounts[i]?.id && (acc.platform !== "ethereum" || acc.chainId === b.accounts[i]?.chainId)
1686
+ );
1571
1687
  export {
1572
1688
  DEFAULT_STORAGE_KEY,
1573
1689
  clearAllCachedObservables,