@lydianpay/lydianconnect 1.1.4 → 1.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.cjs CHANGED
@@ -36,18 +36,23 @@ var ConnectManager = class {
36
36
  }
37
37
  }
38
38
  /**
39
- * SDK connector if one claims this walletId AND serves the requested
40
- * namespace, else the fallback. So an EVM-only SDK connector is bypassed to
41
- * WalletConnect for a non-EVM chain. The host app is oblivious.
39
+ * The connector claiming this walletId if it serves the requested namespace
40
+ * AND is currently available, else the fallback. So an EVM-only SDK connector
41
+ * is bypassed to WalletConnect for a non-EVM chain, and the injected connector
42
+ * is bypassed when the wallet's extension hasn't announced a provider. The host
43
+ * app is oblivious. SDK connectors report `isAvailable` synchronously as `true`,
44
+ * so this only awaits for connectors that actually probe (e.g. injected).
42
45
  */
43
- resolve(walletId, namespaceName) {
46
+ async resolve(walletId, namespaceName) {
44
47
  const claimed = this.byWallet.get(walletId);
45
- if (claimed && claimed.servesNamespace(namespaceName)) return claimed;
48
+ if (claimed && claimed.servesNamespace(namespaceName) && await claimed.isAvailable(walletId))
49
+ return claimed;
46
50
  return this.fallback;
47
51
  }
48
52
  async connect(req) {
49
53
  if (!this.started) await this.init();
50
- const connection = await this.resolve(req.walletId, req.namespace.name).connect(req);
54
+ const connector = await this.resolve(req.walletId, req.namespace.name);
55
+ const connection = await connector.connect(req);
51
56
  this.connections.set(req.walletId, connection);
52
57
  this.emit({ type: "connect", walletId: req.walletId, connection });
53
58
  return connection;
@@ -132,6 +137,100 @@ function buildNamespace(chainId) {
132
137
  };
133
138
  }
134
139
 
140
+ // node_modules/@lydianpay/lydianconnect-catalog/dist/index.js
141
+ var ICON_BASE = "https://wallet-assets.lydian.com/wallets";
142
+ var WALLETS = [
143
+ { id: "metamask", name: "MetaMask", namespaces: ["eip155"] },
144
+ { id: "trust", name: "Trust", namespaces: ["eip155", "solana"] },
145
+ { id: "rainbow", name: "Rainbow", namespaces: ["eip155"] },
146
+ { id: "coinbase", name: "Coinbase", namespaces: ["eip155", "solana"] },
147
+ { id: "zerion", name: "Zerion", namespaces: ["eip155"] },
148
+ { id: "tokenpocket", name: "Token Pocket", namespaces: ["eip155", "solana"] },
149
+ { id: "blockchaincom", name: "Blockchain.com", namespaces: ["bip122", "eip155", "solana"] },
150
+ { id: "exodus", name: "Exodus", namespaces: ["eip155", "solana"] },
151
+ { id: "phantom", name: "Phantom", namespaces: ["bip122", "eip155", "solana"] },
152
+ { id: "okx", name: "OKX", namespaces: ["bip122", "eip155", "solana"] },
153
+ { id: "cryptocom", name: "Crypto.com", namespaces: ["bip122", "eip155", "solana"] },
154
+ { id: "argent", name: "Argent", namespaces: ["eip155"] },
155
+ { id: "safepal", name: "SafePal", namespaces: ["eip155", "solana", "ton"] },
156
+ { id: "imtoken", name: "imToken", namespaces: ["eip155"] },
157
+ { id: "ronin", name: "Ronin", namespaces: ["eip155"] },
158
+ { id: "coin98", name: "Coin98", namespaces: ["eip155", "solana"] },
159
+ { id: "bitkeep", name: "Bitget Wallet", namespaces: ["eip155", "solana"] },
160
+ { id: "1inch", name: "1inch", namespaces: ["eip155", "solana"] },
161
+ { id: "ledgerlive", name: "Ledger Live", namespaces: ["bip122", "eip155", "solana"] },
162
+ { id: "atomic", name: "Atomic", namespaces: ["bip122", "eip155", "solana"] },
163
+ { id: "alpha", name: "Alpha", namespaces: ["eip155"] },
164
+ { id: "math", name: "Math", namespaces: ["eip155", "solana"] },
165
+ { id: "bitpay", name: "BitPay", namespaces: ["eip155"] },
166
+ { id: "rabby", name: "Rabby", namespaces: ["eip155"] },
167
+ { id: "bybit", name: "Bybit", namespaces: ["eip155", "solana"] },
168
+ { id: "binance", name: "Binance", namespaces: ["eip155", "solana"] },
169
+ { id: "gate", name: "Gate Wallet", namespaces: ["eip155"] },
170
+ { id: "core", name: "Core", namespaces: ["eip155", "solana"] },
171
+ { id: "backpack", name: "Backpack", namespaces: ["eip155", "solana"] },
172
+ { id: "frontier", name: "Frontier", namespaces: ["eip155"] },
173
+ { id: "onekey", name: "OneKey", namespaces: ["eip155"] },
174
+ { id: "kraken", name: "Kraken", namespaces: ["eip155", "solana"] },
175
+ { id: "uniswap", name: "Uniswap Wallet", namespaces: ["eip155"] },
176
+ { id: "omni", name: "Omni", namespaces: ["eip155"] },
177
+ { id: "mew", name: "MEW Wallet", namespaces: ["eip155"] },
178
+ { id: "foxwallet", name: "FoxWallet", namespaces: ["eip155"] },
179
+ { id: "nova", name: "Nova Wallet", namespaces: ["eip155"] },
180
+ { id: "subwallet", name: "SubWallet", namespaces: ["eip155"] },
181
+ { id: "safe", name: "Safe", namespaces: ["eip155"] },
182
+ { id: "robinhood", name: "Robinhood Wallet", namespaces: ["eip155"] },
183
+ { id: "tangem", name: "Tangem", namespaces: ["eip155", "solana"] },
184
+ { id: "trezor", name: "Trezor", namespaces: ["bip122", "eip155", "solana"] },
185
+ { id: "kucoin", name: "KuCoin", namespaces: ["eip155", "solana"] },
186
+ { id: "jupiter", name: "Jupiter", namespaces: ["solana"] },
187
+ { id: "zengo", name: "Zengo", namespaces: ["eip155"] },
188
+ { id: "bitcoincom", name: "Bitcoin.com", namespaces: ["eip155"] },
189
+ { id: "keplr", name: "Keplr", namespaces: ["eip155"] },
190
+ { id: "cake", name: "Cake Wallet", namespaces: ["eip155", "solana"] },
191
+ { id: "unstoppable", name: "Unstoppable", namespaces: ["eip155"] },
192
+ { id: "bifrost", name: "Bifrost", namespaces: ["bip122", "eip155"] },
193
+ { id: "pintu", name: "Pintu", namespaces: ["eip155"] },
194
+ { id: "hot", name: "HOT Wallet", namespaces: ["eip155", "solana"] },
195
+ { id: "arculus", name: "Arculus", namespaces: ["bip122", "eip155", "solana"] }
196
+ ];
197
+ var EXTENSIONS = {
198
+ metamask: {
199
+ rdns: "io.metamask",
200
+ extensionUrl: "https://chromewebstore.google.com/detail/metamask/nkbihfbeogaeaoehlefnkodbefgpgknn"
201
+ },
202
+ trust: {
203
+ rdns: "com.trustwallet.app",
204
+ extensionUrl: "https://chromewebstore.google.com/detail/trust-wallet/egjidjbpglichdcondbcbdnbeeppgdph"
205
+ },
206
+ rabby: {
207
+ rdns: "io.rabby",
208
+ extensionUrl: "https://chromewebstore.google.com/detail/rabby-wallet/acmacodkjbdgmoleebolmdjonilkdbch"
209
+ },
210
+ okx: {
211
+ // Legacy "okex" brand retained in the extension's rdns after the 2022 rebrand.
212
+ rdns: "com.okex.wallet",
213
+ extensionUrl: "https://chromewebstore.google.com/detail/okx-wallet/mcohilncbfahbmgdjkbpemcciiolgcge"
214
+ },
215
+ phantom: {
216
+ rdns: "app.phantom",
217
+ extensionUrl: "https://chromewebstore.google.com/detail/phantom/bfnaelmomeimhlpmgjnjophhpkkoljpa"
218
+ },
219
+ zerion: {
220
+ rdns: "io.zerion",
221
+ extensionUrl: "https://chromewebstore.google.com/detail/zerion-wallet-crypto-defi/klghhnkeealcohjjanjjdaeeggmfmlpl"
222
+ },
223
+ rainbow: {
224
+ rdns: "me.rainbow",
225
+ extensionUrl: "https://chromewebstore.google.com/detail/rainbow/opfgelmcmbiajamepnmloijbpoleiama"
226
+ }
227
+ };
228
+ var WALLET_CATALOG = WALLETS.map((w) => ({
229
+ ...w,
230
+ iconUrl: `${ICON_BASE}/${w.id}.png`,
231
+ ...EXTENSIONS[w.id] ?? {}
232
+ }));
233
+
135
234
  // src/core/connector.ts
136
235
  var BaseConnector = class {
137
236
  constructor() {
@@ -388,6 +487,159 @@ function evmChainIds(req) {
388
487
  return Number.isFinite(n) && n > 0 ? [n] : void 0;
389
488
  }
390
489
 
490
+ // src/connectors/injected/connector.ts
491
+ var DISCOVERY_WAIT_MS = 100;
492
+ var InjectedConnector = class extends BaseConnector {
493
+ constructor(rdnsByWallet) {
494
+ super();
495
+ this.rdnsByWallet = rdnsByWallet;
496
+ this.type = "injected";
497
+ /** rdns -> the provider it announced. Populated by EIP-6963 discovery. */
498
+ this.discovered = /* @__PURE__ */ new Map();
499
+ /**
500
+ * Providers we've already attached listeners to. Bind once and keep for the
501
+ * page's life (never remove) so a reconnect doesn't stack duplicate listeners.
502
+ * EIP-6963 announces exactly one provider per rdns, so a given provider maps to
503
+ * a single walletId — closing over walletId in bindEvents is safe.
504
+ */
505
+ this.bound = /* @__PURE__ */ new WeakSet();
506
+ this.listening = false;
507
+ this.walletIds = Object.keys(rdnsByWallet);
508
+ this.startDiscovery();
509
+ }
510
+ /** Listen for provider announcements, then ask installed extensions to announce. */
511
+ startDiscovery() {
512
+ if (this.listening || typeof window === "undefined") return;
513
+ this.listening = true;
514
+ window.addEventListener("eip6963:announceProvider", (event) => {
515
+ const detail = event.detail;
516
+ if (detail?.info?.rdns && detail.provider) this.discovered.set(detail.info.rdns, detail);
517
+ });
518
+ window.dispatchEvent(new Event("eip6963:requestProvider"));
519
+ }
520
+ servesNamespace(name) {
521
+ return name === "eip155";
522
+ }
523
+ /** Available iff the wallet's extension has announced an injected provider. */
524
+ async isAvailable(walletId) {
525
+ if (typeof window === "undefined") return false;
526
+ const rdns = this.rdnsByWallet[walletId];
527
+ if (!rdns) return false;
528
+ if (this.discovered.has(rdns)) return true;
529
+ window.dispatchEvent(new Event("eip6963:requestProvider"));
530
+ await new Promise((resolve) => setTimeout(resolve, DISCOVERY_WAIT_MS));
531
+ return this.discovered.has(rdns);
532
+ }
533
+ async connect(req) {
534
+ const provider = this.providerFor(req.walletId);
535
+ if (!provider) throw new Error(`No injected provider announced for "${req.walletId}"`);
536
+ const accounts = await withSignal(
537
+ provider.request({ method: "eth_requestAccounts" }),
538
+ // triggers the extension popup
539
+ req.signal
540
+ );
541
+ const account = accounts?.[0];
542
+ if (!account) throw new Error(`${req.walletId} extension returned no account`);
543
+ await this.ensureChain(provider, req);
544
+ const chainId = toCaip3(await provider.request({ method: "eth_chainId" }));
545
+ this.bindEvents(provider, req.walletId);
546
+ return this.toConnection(provider, req.walletId, account, chainId);
547
+ }
548
+ async restore() {
549
+ if (typeof window === "undefined") return [];
550
+ if (this.discovered.size === 0) {
551
+ window.dispatchEvent(new Event("eip6963:requestProvider"));
552
+ await new Promise((resolve) => setTimeout(resolve, DISCOVERY_WAIT_MS));
553
+ }
554
+ const out = [];
555
+ for (const walletId of this.walletIds) {
556
+ const provider = this.providerFor(walletId);
557
+ if (!provider) continue;
558
+ const accounts = await provider.request({ method: "eth_accounts" }).catch(() => []);
559
+ const account = accounts?.[0];
560
+ if (!account) continue;
561
+ const chainId = toCaip3(
562
+ await provider.request({ method: "eth_chainId" }).catch(() => "0x1")
563
+ );
564
+ this.bindEvents(provider, walletId);
565
+ out.push(this.toConnection(provider, walletId, account, chainId));
566
+ }
567
+ return out;
568
+ }
569
+ providerFor(walletId) {
570
+ const rdns = this.rdnsByWallet[walletId];
571
+ return rdns ? this.discovered.get(rdns)?.provider : void 0;
572
+ }
573
+ bindEvents(provider, walletId) {
574
+ if (this.bound.has(provider)) return;
575
+ this.bound.add(provider);
576
+ provider.on("chainChanged", (hex) => {
577
+ this.emit({ type: "chainChanged", walletId, chainId: toCaip3(String(hex)) });
578
+ });
579
+ provider.on("accountsChanged", (accounts) => {
580
+ const account = Array.isArray(accounts) ? accounts[0] : void 0;
581
+ if (account) this.emit({ type: "accountsChanged", walletId, account });
582
+ else this.emit({ type: "disconnect", walletId });
583
+ });
584
+ provider.on("disconnect", () => this.emit({ type: "disconnect", walletId }));
585
+ }
586
+ /** Best-effort switch to the requested chain; leaves the wallet as-is if it's unknown to it. */
587
+ async ensureChain(provider, req) {
588
+ const wanted = req.namespace.value.chains?.[0];
589
+ const wantedNum = wanted ? Number(wanted.split(":")[1]) : NaN;
590
+ if (!Number.isFinite(wantedNum) || wantedNum <= 0) return;
591
+ const current = parseInt(
592
+ await provider.request({ method: "eth_chainId" }).catch(() => "0x0") ?? "0x0",
593
+ 16
594
+ );
595
+ if (current === wantedNum) return;
596
+ await provider.request({
597
+ method: "wallet_switchEthereumChain",
598
+ params: [{ chainId: `0x${wantedNum.toString(16)}` }]
599
+ }).catch(() => {
600
+ });
601
+ }
602
+ toConnection(provider, walletId, account, chainId) {
603
+ return {
604
+ walletId,
605
+ account,
606
+ chainId,
607
+ request: async (args) => await provider.request({ method: args.method, params: args.params }),
608
+ openWallet: () => {
609
+ },
610
+ // the extension foregrounds its own popup on request
611
+ disconnect: async () => {
612
+ await provider.request({ method: "wallet_revokePermissions", params: [{ eth_accounts: {} }] }).catch(() => {
613
+ });
614
+ this.emit({ type: "disconnect", walletId });
615
+ }
616
+ };
617
+ }
618
+ };
619
+ function toCaip3(hexChainId) {
620
+ if (!hexChainId) return "eip155:1";
621
+ const num = hexChainId.startsWith("0x") ? parseInt(hexChainId, 16) : Number(hexChainId);
622
+ return `eip155:${Number.isFinite(num) ? num : 1}`;
623
+ }
624
+ function withSignal(promise, signal) {
625
+ if (!signal) return promise;
626
+ if (signal.aborted) return Promise.reject(new DOMException("Aborted", "AbortError"));
627
+ return new Promise((resolve, reject) => {
628
+ const onAbort = () => reject(new DOMException("Aborted", "AbortError"));
629
+ signal.addEventListener("abort", onAbort, { once: true });
630
+ promise.then(
631
+ (value) => {
632
+ signal.removeEventListener("abort", onAbort);
633
+ resolve(value);
634
+ },
635
+ (error) => {
636
+ signal.removeEventListener("abort", onAbort);
637
+ reject(error);
638
+ }
639
+ );
640
+ });
641
+ }
642
+
391
643
  // src/connectors/walletconnect/wallet-links.ts
392
644
  var WALLET_LINKS = {
393
645
  metamask: { "native": "metamask://", "scheme": "metamask://wc", "universal": "https://metamask.app.link/wc", "store": { "ios": "https://apps.apple.com/us/app/metamask/id1438144202", "android": "https://play.google.com/store/apps/details?id=io.metamask" } },
@@ -586,7 +838,7 @@ var WalletConnectConnector = class extends BaseConnector {
586
838
  }
587
839
  }
588
840
  const pairingTopic = utils.parseUri(uri).topic;
589
- const session = await withSignal(approval(), req.signal, () => {
841
+ const session = await withSignal2(approval(), req.signal, () => {
590
842
  void client.core.pairing.disconnect({ topic: pairingTopic }).catch(() => {
591
843
  });
592
844
  });
@@ -694,7 +946,7 @@ function firstNamespace(session) {
694
946
  if (!v) return null;
695
947
  return { name, value: { chains: v.chains, methods: v.methods, events: v.events } };
696
948
  }
697
- function withSignal(promise, signal, onAbort) {
949
+ function withSignal2(promise, signal, onAbort) {
698
950
  if (!signal) return promise;
699
951
  if (signal.aborted) {
700
952
  onAbort();
@@ -720,15 +972,24 @@ function withSignal(promise, signal, onAbort) {
720
972
  }
721
973
 
722
974
  // src/core/lydian-connect.ts
975
+ function injectedRdnsMap(sdkServed) {
976
+ const map = {};
977
+ for (const w of WALLET_CATALOG) {
978
+ if (w.rdns && w.namespaces.includes("eip155") && !sdkServed.has(w.id)) map[w.id] = w.rdns;
979
+ }
980
+ return map;
981
+ }
723
982
  function createConnectors(config) {
724
983
  const app = {
725
984
  appName: config.appName,
726
985
  icon: config.icon,
727
986
  description: config.description
728
987
  };
988
+ const sdk = [new MetaMaskConnector(app), new CoinbaseConnector(app)];
989
+ const sdkServed = new Set(sdk.flatMap((c) => c.walletIds));
729
990
  return [
730
- new MetaMaskConnector(app),
731
- new CoinbaseConnector(app),
991
+ ...sdk,
992
+ new InjectedConnector(injectedRdnsMap(sdkServed)),
732
993
  new WalletConnectConnector(app, { projectId: config.walletConnectProjectId })
733
994
  ];
734
995
  }