@moon-x/core 0.10.1 → 0.11.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/lib/index.js CHANGED
@@ -39,16 +39,17 @@ __export(lib_exports, {
39
39
  broadcastEthereumRaw: () => broadcastEthereumRaw,
40
40
  broadcastSolanaSigned: () => broadcastSolanaSigned,
41
41
  bs58ToBytes: () => bs58ToBytes,
42
+ buildChainIndex: () => buildChainIndex,
42
43
  buildUrl: () => buildUrl,
43
44
  canonicalAlgorithmFor: () => canonicalAlgorithmFor,
44
45
  createManualSiweMessage: () => createManualSiweMessage,
46
+ entryCaip2: () => entryCaip2,
45
47
  estimateEvmGas: () => estimateEvmGas,
46
48
  estimateEvmGasReserve: () => estimateEvmGasReserve,
47
49
  formatPasskeyLabel: () => formatPasskeyLabel,
48
50
  fromB64url: () => fromB64url,
49
51
  generatePasskeyUserId: () => generatePasskeyUserId,
50
52
  getChainById: () => getChainById,
51
- getDefaultChain: () => getDefaultChain,
52
53
  getEvmGasPrice: () => getEvmGasPrice,
53
54
  getEvmMaxPriorityFeePerGas: () => getEvmMaxPriorityFeePerGas,
54
55
  getEvmNonce: () => getEvmNonce,
@@ -57,14 +58,21 @@ __export(lib_exports, {
57
58
  getSOLTransfersFromInstructions: () => getSOLTransfersFromInstructions,
58
59
  getTransferInfoFromInstructions: () => getTransferInfoFromInstructions,
59
60
  isChainSupported: () => isChainSupported,
61
+ isEvmEntry: () => isEvmEntry,
62
+ normalizeChainItem: () => normalizeChainItem,
63
+ normalizeChainKey: () => normalizeChainKey,
60
64
  parseDerivationPath: () => parseDerivationPath,
61
65
  prepareEvmSendTransaction: () => prepareEvmSendTransaction,
62
66
  prepareEvmSignTransaction: () => prepareEvmSignTransaction,
67
+ resolveChainEntry: () => resolveChainEntry,
63
68
  resolveEvmChain: () => resolveEvmChain,
69
+ resolveEvmChainEntry: () => resolveEvmChainEntry,
64
70
  resolvePasskeyMetadata: () => resolvePasskeyMetadata,
71
+ resolveRpcUrl: () => resolveRpcUrl,
72
+ resolveSolanaChainEntry: () => resolveSolanaChainEntry,
73
+ resolveWsUrl: () => resolveWsUrl,
65
74
  sanitizeUserData: () => sanitizeUserData,
66
75
  serializeEthereumTransaction: () => serializeEthereumTransaction,
67
- setChainRpcUrl: () => setChainRpcUrl,
68
76
  toB64url: () => toB64url,
69
77
  validateChainConfig: () => validateChainConfig,
70
78
  verifyIdToken: () => verifyIdToken
@@ -362,18 +370,6 @@ function resolvePasskeyMetadata(input) {
362
370
 
363
371
  // src/lib/ethereum/chains.ts
364
372
  var import_viem = require("viem");
365
- function setChainRpcUrl(chain, rpcUrl) {
366
- return {
367
- ...chain,
368
- rpcUrls: {
369
- ...chain.rpcUrls,
370
- default: {
371
- http: [rpcUrl],
372
- webSocket: chain.rpcUrls.default.webSocket
373
- }
374
- }
375
- };
376
- }
377
373
  function getRpcUrl(chain) {
378
374
  return chain.rpcUrls.default.http[0];
379
375
  }
@@ -506,35 +502,152 @@ async function estimateEvmGasReserve(rpcUrl, chainId, opts = {}) {
506
502
  return Number((0, import_viem.formatEther)(reserveWei));
507
503
  }
508
504
  function getChainById(chains, chainId) {
509
- return chains.find((chain) => chain.id === chainId);
505
+ for (const item of chains) {
506
+ const entry = normalizeChainItem(item);
507
+ if (isEvmEntry(entry) && entry.chain.id === chainId) return entry.chain;
508
+ }
509
+ return void 0;
510
510
  }
511
511
  function isChainSupported(chains, chainId) {
512
- return chains.some((chain) => chain.id === chainId);
512
+ return getChainById(chains, chainId) !== void 0;
513
513
  }
514
- function validateChainConfig(config) {
515
- const { defaultChain, supportedChains } = config;
516
- if (supportedChains && supportedChains.length === 0) {
517
- throw new Error("supportedChains cannot be an empty array");
514
+ function isEvmEntry(entry) {
515
+ return "chain" in entry;
516
+ }
517
+ function normalizeChainItem(item) {
518
+ if ("chain" in item) return item;
519
+ if (typeof item.id === "number") {
520
+ return { chain: item };
518
521
  }
519
- if (defaultChain && supportedChains) {
520
- const isDefaultInSupported = supportedChains.some(
521
- (chain) => chain.id === defaultChain.id
522
- );
523
- if (!isDefaultInSupported) {
522
+ return item;
523
+ }
524
+ function entryCaip2(entry) {
525
+ return isEvmEntry(entry) ? `eip155:${entry.chain.id}` : entry.id;
526
+ }
527
+ function slug(s) {
528
+ return s.toLowerCase().replace(/[^a-z0-9]/g, "");
529
+ }
530
+ function aliasesFor(entry) {
531
+ const keys = [];
532
+ if (isEvmEntry(entry)) {
533
+ const { chain } = entry;
534
+ keys.push(`eip155:${chain.id}`, String(chain.id));
535
+ if (chain.name) {
536
+ const s = slug(chain.name);
537
+ keys.push(`eth:${s}`, s);
538
+ }
539
+ if (chain.network) {
540
+ const s = slug(chain.network);
541
+ keys.push(`eth:${s}`, s);
542
+ }
543
+ } else {
544
+ keys.push(entry.id);
545
+ const cluster = entry.id.split(":")[1];
546
+ if (cluster) keys.push(`solana:${cluster}`, `sol:${cluster}`, cluster);
547
+ }
548
+ return keys;
549
+ }
550
+ function normalizeChainKey(key) {
551
+ if (typeof key === "number") return `eip155:${key}`;
552
+ if (typeof key === "string") return key.trim().toLowerCase();
553
+ const id = key?.id;
554
+ throw new Error(
555
+ typeof id === "number" ? `Invalid chain reference: expected an alias, CAIP-2 id, or numeric chainId, but got a chain object. Use "eip155:${id}" or ${id}.` : `Invalid chain reference: expected a string alias / CAIP-2 id or a numeric chainId, got ${typeof key}.`
556
+ );
557
+ }
558
+ function buildChainIndex(chains = []) {
559
+ const entries = chains.map(normalizeChainItem);
560
+ const byAlias = /* @__PURE__ */ new Map();
561
+ for (const entry of entries) {
562
+ for (const alias of aliasesFor(entry)) {
563
+ const key = alias.toLowerCase();
564
+ if (!byAlias.has(key)) byAlias.set(key, entry);
565
+ }
566
+ }
567
+ return { entries, byAlias };
568
+ }
569
+ function resolveChainEntry(chains, selector, defaultSelector) {
570
+ const index = buildChainIndex(chains ?? []);
571
+ if (selector != null) {
572
+ return index.byAlias.get(normalizeChainKey(selector));
573
+ }
574
+ if (defaultSelector != null) {
575
+ const fallback = index.byAlias.get(normalizeChainKey(defaultSelector));
576
+ if (fallback) return fallback;
577
+ }
578
+ return index.entries[0];
579
+ }
580
+ function resolveSolanaChainEntry(chains, selector, defaultSelector) {
581
+ const index = buildChainIndex(chains ?? []);
582
+ const asSolana = (k) => {
583
+ if (k == null) return void 0;
584
+ const e = index.byAlias.get(normalizeChainKey(k));
585
+ return e && !isEvmEntry(e) ? e : void 0;
586
+ };
587
+ if (selector != null) return asSolana(selector);
588
+ return asSolana(defaultSelector) ?? index.entries.find((e) => !isEvmEntry(e));
589
+ }
590
+ function resolveEvmChainEntry(opts) {
591
+ const index = buildChainIndex(opts.chains ?? []);
592
+ const lookup = (k) => {
593
+ if (k == null) return void 0;
594
+ const e = index.byAlias.get(normalizeChainKey(k));
595
+ return e && isEvmEntry(e) ? e : void 0;
596
+ };
597
+ if (opts.selector != null) return lookup(opts.selector);
598
+ if (opts.requestedChainId != null) return lookup(opts.requestedChainId);
599
+ return lookup(opts.walletChainId) ?? lookup(opts.defaultSelector) ?? index.entries.find(isEvmEntry);
600
+ }
601
+ function resolveRpcUrl(entry, override) {
602
+ if (override) return override;
603
+ if (isEvmEntry(entry)) return entry.rpcUrl ?? getRpcUrl(entry.chain);
604
+ return entry.rpcUrl;
605
+ }
606
+ function resolveWsUrl(entry, override) {
607
+ if (override) return override;
608
+ if (isEvmEntry(entry)) {
609
+ return entry.wsUrl ?? entry.chain.rpcUrls.default.webSocket?.[0];
610
+ }
611
+ return entry.wsUrl;
612
+ }
613
+ function validateChainConfig(config) {
614
+ const { chains, defaultChain } = config;
615
+ if (defaultChain != null) {
616
+ if (typeof defaultChain !== "string" && typeof defaultChain !== "number") {
617
+ const id = defaultChain.id;
618
+ throw new Error(
619
+ `defaultChain must be a chain reference (alias, CAIP-2 id, or numeric chainId), not a chain object.${typeof id === "number" ? ` Use "eip155:${id}" or ${id}.` : ""}`
620
+ );
621
+ }
622
+ if (!chains || chains.length === 0) {
524
623
  throw new Error(
525
- `defaultChain (${defaultChain.name}, ID: ${defaultChain.id}) must be included in supportedChains`
624
+ `defaultChain "${defaultChain}" is set but no \`chains\` are configured. Add the chain to \`chains\`, or remove \`defaultChain\`.`
526
625
  );
527
626
  }
528
627
  }
529
- }
530
- function getDefaultChain(config) {
531
- if (config.defaultChain) {
532
- return config.defaultChain;
628
+ if (!chains) return;
629
+ if (chains.length === 0) {
630
+ throw new Error("chains cannot be an empty array");
533
631
  }
534
- if (config.supportedChains && config.supportedChains.length > 0) {
535
- return config.supportedChains[0];
632
+ const index = buildChainIndex(chains);
633
+ const seen = /* @__PURE__ */ new Set();
634
+ for (const entry of index.entries) {
635
+ const id = entryCaip2(entry);
636
+ if (seen.has(id)) {
637
+ throw new Error(`Duplicate chain in config: ${id}`);
638
+ }
639
+ seen.add(id);
640
+ if (!isEvmEntry(entry) && !entry.rpcUrl) {
641
+ throw new Error(`Solana chain "${entry.id}" is missing an rpcUrl`);
642
+ }
643
+ }
644
+ if (defaultChain != null && !index.byAlias.get(normalizeChainKey(defaultChain))) {
645
+ throw new Error(
646
+ `defaultChain "${defaultChain}" is not in the configured chains (${[
647
+ ...seen
648
+ ].join(", ")})`
649
+ );
536
650
  }
537
- return void 0;
538
651
  }
539
652
 
540
653
  // src/lib/ethereum/siwe.ts
@@ -615,54 +728,91 @@ ${fields.join("\n")}`;
615
728
 
616
729
  // src/lib/ethereum/tx-prep.ts
617
730
  var import_viem3 = require("viem");
731
+ function syntheticChain(chainId, rpcUrl) {
732
+ return {
733
+ id: chainId,
734
+ name: `chain-${chainId}`,
735
+ nativeCurrency: { name: "Ether", symbol: "ETH", decimals: 18 },
736
+ rpcUrls: { default: { http: [rpcUrl] } }
737
+ };
738
+ }
618
739
  function resolveEvmChain({
740
+ chains,
741
+ chainSelector,
619
742
  requestedChainId,
620
743
  walletChainId,
621
- supportedChains,
622
744
  defaultChain,
745
+ rpcUrlOverride,
623
746
  strict = false
624
747
  }) {
625
- let target;
626
- if (requestedChainId && supportedChains) {
627
- target = getChainById(supportedChains, Number(requestedChainId));
628
- }
629
- if (!target && walletChainId && supportedChains) {
630
- target = getChainById(supportedChains, walletChainId);
748
+ if (chainSelector != null) {
749
+ const entry2 = resolveEvmChainEntry({ chains, selector: chainSelector });
750
+ if (!entry2) {
751
+ throw new Error(
752
+ `chain "${chainSelector}" is not in the configured chains.`
753
+ );
754
+ }
755
+ return { chain: entry2.chain, rpcUrl: resolveRpcUrl(entry2, rpcUrlOverride) };
631
756
  }
632
- if (!target) {
633
- target = defaultChain;
757
+ if (requestedChainId != null) {
758
+ const entry2 = resolveEvmChainEntry({ chains, requestedChainId });
759
+ if (entry2) {
760
+ return {
761
+ chain: entry2.chain,
762
+ rpcUrl: resolveRpcUrl(entry2, rpcUrlOverride)
763
+ };
764
+ }
765
+ if (rpcUrlOverride) {
766
+ return {
767
+ chain: syntheticChain(Number(requestedChainId), rpcUrlOverride),
768
+ rpcUrl: rpcUrlOverride
769
+ };
770
+ }
771
+ if (strict) {
772
+ throw new Error(
773
+ `Requested chainId ${requestedChainId} is not in the configured chains.`
774
+ );
775
+ }
634
776
  }
635
- if (!target) {
636
- throw new Error(
637
- "No chain configured. Please set ethereum.defaultChain in MoonKeyProvider config."
638
- );
777
+ const entry = resolveEvmChainEntry({
778
+ chains,
779
+ walletChainId,
780
+ defaultSelector: defaultChain
781
+ });
782
+ if (entry) {
783
+ return { chain: entry.chain, rpcUrl: resolveRpcUrl(entry, rpcUrlOverride) };
639
784
  }
640
- if (strict && requestedChainId && Number(requestedChainId) !== target.id) {
641
- throw new Error(
642
- `Requested chainId ${requestedChainId} is not in the configured supportedChains.`
643
- );
785
+ if (rpcUrlOverride && requestedChainId != null) {
786
+ return {
787
+ chain: syntheticChain(Number(requestedChainId), rpcUrlOverride),
788
+ rpcUrl: rpcUrlOverride
789
+ };
644
790
  }
645
- return target;
791
+ throw new Error(
792
+ "No chain configured. Set `chains` (and optionally `defaultChain`) in MoonKeyProvider config, or pass an `rpcUrl` together with the transaction's `chainId`."
793
+ );
646
794
  }
647
795
  async function prepareEvmSignTransaction({
648
796
  transaction,
649
797
  wallet,
650
- supportedChains,
651
- defaultChain
798
+ chains,
799
+ defaultChain,
800
+ chainSelector
652
801
  }) {
653
802
  if (typeof transaction === "string") {
654
803
  return { serializedTransaction: transaction, chain: void 0 };
655
804
  }
656
805
  const txObj = transaction;
657
806
  const transactionType = txObj.type ?? 2;
658
- const chain = resolveEvmChain({
807
+ const { chain } = resolveEvmChain({
808
+ chains,
809
+ chainSelector,
659
810
  requestedChainId: txObj.chainId,
660
811
  walletChainId: wallet.chainId,
661
- supportedChains,
662
812
  defaultChain,
663
813
  strict: false
664
814
  });
665
- const derivedChainId = txObj.chainId || chain.id;
815
+ const derivedChainId = chainSelector != null ? chain.id : txObj.chainId || chain.id;
666
816
  const formattedTransaction = {
667
817
  to: txObj.to,
668
818
  value: txObj.value ? typeof txObj.value === "bigint" ? txObj.value : (0, import_viem3.parseEther)(txObj.value.toString()) : BigInt(0),
@@ -694,17 +844,20 @@ async function prepareEvmSignTransaction({
694
844
  async function prepareEvmSendTransaction({
695
845
  transaction,
696
846
  wallet,
697
- supportedChains,
698
- defaultChain
847
+ chains,
848
+ defaultChain,
849
+ chainSelector,
850
+ rpcUrlOverride
699
851
  }) {
700
- const chain = resolveEvmChain({
852
+ const { chain, rpcUrl } = resolveEvmChain({
853
+ chains,
854
+ chainSelector,
701
855
  requestedChainId: transaction.chainId,
702
856
  walletChainId: wallet.chainId,
703
- supportedChains,
704
857
  defaultChain,
858
+ rpcUrlOverride,
705
859
  strict: true
706
860
  });
707
- const rpcUrl = getRpcUrl(chain);
708
861
  if (!rpcUrl) {
709
862
  throw new Error(
710
863
  `No RPC URL found for chain ${chain.name}. Chain may be missing rpcUrls configuration.`
@@ -1566,16 +1719,17 @@ var bs58ToBytes = async (value) => {
1566
1719
  broadcastEthereumRaw,
1567
1720
  broadcastSolanaSigned,
1568
1721
  bs58ToBytes,
1722
+ buildChainIndex,
1569
1723
  buildUrl,
1570
1724
  canonicalAlgorithmFor,
1571
1725
  createManualSiweMessage,
1726
+ entryCaip2,
1572
1727
  estimateEvmGas,
1573
1728
  estimateEvmGasReserve,
1574
1729
  formatPasskeyLabel,
1575
1730
  fromB64url,
1576
1731
  generatePasskeyUserId,
1577
1732
  getChainById,
1578
- getDefaultChain,
1579
1733
  getEvmGasPrice,
1580
1734
  getEvmMaxPriorityFeePerGas,
1581
1735
  getEvmNonce,
@@ -1584,14 +1738,21 @@ var bs58ToBytes = async (value) => {
1584
1738
  getSOLTransfersFromInstructions,
1585
1739
  getTransferInfoFromInstructions,
1586
1740
  isChainSupported,
1741
+ isEvmEntry,
1742
+ normalizeChainItem,
1743
+ normalizeChainKey,
1587
1744
  parseDerivationPath,
1588
1745
  prepareEvmSendTransaction,
1589
1746
  prepareEvmSignTransaction,
1747
+ resolveChainEntry,
1590
1748
  resolveEvmChain,
1749
+ resolveEvmChainEntry,
1591
1750
  resolvePasskeyMetadata,
1751
+ resolveRpcUrl,
1752
+ resolveSolanaChainEntry,
1753
+ resolveWsUrl,
1592
1754
  sanitizeUserData,
1593
1755
  serializeEthereumTransaction,
1594
- setChainRpcUrl,
1595
1756
  toB64url,
1596
1757
  validateChainConfig,
1597
1758
  verifyIdToken
@@ -8,16 +8,17 @@ import {
8
8
  broadcastEthereumRaw,
9
9
  broadcastSolanaSigned,
10
10
  bs58ToBytes,
11
+ buildChainIndex,
11
12
  buildUrl,
12
13
  canonicalAlgorithmFor,
13
14
  createManualSiweMessage,
15
+ entryCaip2,
14
16
  estimateEvmGas,
15
17
  estimateEvmGasReserve,
16
18
  formatPasskeyLabel,
17
19
  fromB64url,
18
20
  generatePasskeyUserId,
19
21
  getChainById,
20
- getDefaultChain,
21
22
  getEvmGasPrice,
22
23
  getEvmMaxPriorityFeePerGas,
23
24
  getEvmNonce,
@@ -26,18 +27,25 @@ import {
26
27
  getSOLTransfersFromInstructions,
27
28
  getTransferInfoFromInstructions,
28
29
  isChainSupported,
30
+ isEvmEntry,
31
+ normalizeChainItem,
32
+ normalizeChainKey,
29
33
  parseDerivationPath,
30
34
  prepareEvmSendTransaction,
31
35
  prepareEvmSignTransaction,
36
+ resolveChainEntry,
32
37
  resolveEvmChain,
38
+ resolveEvmChainEntry,
33
39
  resolvePasskeyMetadata,
40
+ resolveRpcUrl,
41
+ resolveSolanaChainEntry,
42
+ resolveWsUrl,
34
43
  sanitizeUserData,
35
44
  serializeEthereumTransaction,
36
- setChainRpcUrl,
37
45
  toB64url,
38
46
  validateChainConfig,
39
47
  verifyIdToken
40
- } from "../chunk-T3ESQWEP.mjs";
48
+ } from "../chunk-W6BYGZ2F.mjs";
41
49
  import "../chunk-GQKIA37O.mjs";
42
50
  export {
43
51
  DEFAULT_AUTH_URL,
@@ -49,16 +57,17 @@ export {
49
57
  broadcastEthereumRaw,
50
58
  broadcastSolanaSigned,
51
59
  bs58ToBytes,
60
+ buildChainIndex,
52
61
  buildUrl,
53
62
  canonicalAlgorithmFor,
54
63
  createManualSiweMessage,
64
+ entryCaip2,
55
65
  estimateEvmGas,
56
66
  estimateEvmGasReserve,
57
67
  formatPasskeyLabel,
58
68
  fromB64url,
59
69
  generatePasskeyUserId,
60
70
  getChainById,
61
- getDefaultChain,
62
71
  getEvmGasPrice,
63
72
  getEvmMaxPriorityFeePerGas,
64
73
  getEvmNonce,
@@ -67,14 +76,21 @@ export {
67
76
  getSOLTransfersFromInstructions,
68
77
  getTransferInfoFromInstructions,
69
78
  isChainSupported,
79
+ isEvmEntry,
80
+ normalizeChainItem,
81
+ normalizeChainKey,
70
82
  parseDerivationPath,
71
83
  prepareEvmSendTransaction,
72
84
  prepareEvmSignTransaction,
85
+ resolveChainEntry,
73
86
  resolveEvmChain,
87
+ resolveEvmChainEntry,
74
88
  resolvePasskeyMetadata,
89
+ resolveRpcUrl,
90
+ resolveSolanaChainEntry,
91
+ resolveWsUrl,
75
92
  sanitizeUserData,
76
93
  serializeEthereumTransaction,
77
- setChainRpcUrl,
78
94
  toB64url,
79
95
  validateChainConfig,
80
96
  verifyIdToken
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  formatPasskeyLabel,
3
3
  resolvePasskeyMetadata
4
- } from "../chunk-T3ESQWEP.mjs";
4
+ } from "../chunk-W6BYGZ2F.mjs";
5
5
  import "../chunk-GQKIA37O.mjs";
6
6
  import {
7
7
  SDKProvider,
@@ -10,7 +10,7 @@ import {
10
10
  bs58ToBytes,
11
11
  serializeEthereumTransaction,
12
12
  toB64url
13
- } from "../chunk-T3ESQWEP.mjs";
13
+ } from "../chunk-W6BYGZ2F.mjs";
14
14
  import "../chunk-GQKIA37O.mjs";
15
15
 
16
16
  // src/sdk/interfaces.ts