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