@meshconnect/uwc-bridge-parent 1.8.0 → 1.9.0-snapshot.06cdfe5

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.
@@ -56,6 +56,20 @@ export function toBip122WireAccount(account) {
56
56
  ...(a.purpose ? { purpose: a.purpose } : {})
57
57
  };
58
58
  }
59
+ /**
60
+ * The sats-connect methods a framed child may call: connect, silent account
61
+ * read, sign, revoke. Everything else — `sendTransfer`, ordinals/runes/Stacks
62
+ * methods — is refused, so a framed page can't reach wallet features UWC
63
+ * doesn't use.
64
+ */
65
+ export const SATS_CONNECT_RELAYED_METHODS = [
66
+ 'wallet_connect',
67
+ 'wallet_getAccount',
68
+ 'signMessage',
69
+ 'signPsbt',
70
+ 'wallet_disconnect',
71
+ 'wallet_renouncePermissions'
72
+ ];
59
73
  export class BridgeParent {
60
74
  iframe;
61
75
  parentAPI = null;
@@ -66,6 +80,8 @@ export class BridgeParent {
66
80
  // bare listener won't match a capture-phase registration.
67
81
  exposeListeners = [];
68
82
  destroyed = false;
83
+ /** Stops following the Wallet Standard registry; see `followLateWallets`. */
84
+ stopFollowingRegistry = null;
69
85
  // Captured so subscribeEip6963ProviderEvent can await the one-shot discovery
70
86
  // window before its uuid/rdns lookup, instead of racing an empty
71
87
  // eip6963Wallets when a subscribe call arrives before discovery resolves.
@@ -104,6 +120,8 @@ export class BridgeParent {
104
120
  bip122WalletsReady: true,
105
121
  bip122LegacyWallets: [],
106
122
  bip122LegacyWalletsReady: false,
123
+ bip122SatsConnectWallets: [],
124
+ bip122SatsConnectWalletsReady: false,
107
125
  tronWallets: [],
108
126
  tronWalletsReady: false,
109
127
  tonWallets: [],
@@ -130,6 +148,13 @@ export class BridgeParent {
130
148
  this.parentAPI.bip122LegacyWalletsReady = true;
131
149
  }
132
150
  },
151
+ discoverBip122SatsConnectWallets: (injectedIds) => {
152
+ if (this.parentAPI) {
153
+ this.parentAPI.bip122SatsConnectWallets =
154
+ this.getBip122SatsConnectWallets(injectedIds);
155
+ this.parentAPI.bip122SatsConnectWalletsReady = true;
156
+ }
157
+ },
133
158
  subscribeEip6963ProviderEvent: async (uuid, eventName, callback, rdns) => {
134
159
  // Discovery is one-shot with a fixed window (see
135
160
  // discovereip6963Wallets) — a subscribe call can legitimately arrive
@@ -386,6 +411,27 @@ export class BridgeParent {
386
411
  // Unsubscribe must never throw across the bridge.
387
412
  }
388
413
  });
414
+ },
415
+ subscribeBip122SatsConnectProviderEvent: (injectedId, eventName, callback) => {
416
+ const provider = this.resolveBip122SatsConnectProvider(injectedId);
417
+ if (typeof provider?.addListener !== 'function') {
418
+ throw new Error(`subscribeBip122SatsConnectProviderEvent: no provider with addListener at injectedId "${injectedId}"`);
419
+ }
420
+ // The wallet's event detail can carry fields that don't clone; only
421
+ // plain account data crosses.
422
+ const off = provider.addListener({
423
+ eventName,
424
+ cb: (event) => callback(toRelayedSatsConnectEvent(event))
425
+ });
426
+ return Comlink.proxy(() => {
427
+ try {
428
+ if (typeof off === 'function')
429
+ off();
430
+ }
431
+ catch {
432
+ // Unsubscribe must never throw across the bridge.
433
+ }
434
+ });
389
435
  }
390
436
  };
391
437
  // Use Comlink's windowEndpoint with proper cross-origin support
@@ -437,6 +483,7 @@ export class BridgeParent {
437
483
  };
438
484
  // Restrict inbound RPC to the mesh iframe origin (comlink defaults to ["*"]).
439
485
  Comlink.expose(this.parentAPI, endpoint, [iframeOrigin]);
486
+ this.followLateWallets();
440
487
  // Populate EIP-6963 results in the background. The child polls the
441
488
  // ready flag; it will see them on the next read after we flip it.
442
489
  eip6963Promise.then(eip6963Wallets => {
@@ -495,157 +542,7 @@ export class BridgeParent {
495
542
  for (const wallet of wallets) {
496
543
  // Check if this is a Solana wallet
497
544
  if (this.isSolanaWallet(wallet)) {
498
- let adapter;
499
- if (isWalletAdapterCompatibleStandardWallet(wallet)) {
500
- adapter = new StandardWalletAdapter({ wallet });
501
- }
502
- solanaWallets.push({
503
- uuid: this.generateWalletId(wallet),
504
- name: wallet.name,
505
- chains: (wallet.chains || []),
506
- features: Object.keys(wallet.features),
507
- // Expect a TS error, as not all adapter properties are implemented
508
- // @ts-expect-error
509
- adapter: adapter
510
- ? {
511
- name: adapter.name,
512
- url: adapter.url,
513
- icon: adapter.icon,
514
- readyState: adapter.readyState,
515
- publicKey: Comlink.proxy({
516
- get value() {
517
- return adapter.publicKey;
518
- },
519
- toBase58: async () => {
520
- return adapter.publicKey?.toBase58();
521
- },
522
- toJSON: async () => {
523
- return adapter.publicKey?.toJSON();
524
- },
525
- toBytes: async () => {
526
- return adapter.publicKey?.toBytes();
527
- },
528
- toBuffer: async () => {
529
- return adapter.publicKey?.toBuffer();
530
- },
531
- toString: async () => {
532
- return adapter.publicKey?.toString();
533
- }
534
- }),
535
- connecting: adapter.connecting,
536
- connected: adapter.connected,
537
- supportedTransactionVersions: adapter.supportedTransactionVersions,
538
- wallet: adapter.wallet,
539
- standard: adapter.standard,
540
- destroy: async () => await adapter.destroy(),
541
- autoConnect: async () => await adapter.autoConnect(),
542
- connect: async () => await adapter.connect(),
543
- disconnect: async () => await adapter.disconnect(),
544
- sendTransaction: async (transaction, connection, options) => await adapter.sendTransaction(transaction, connection, options),
545
- signTransaction: async (transaction) => {
546
- return adapter.signTransaction !== undefined
547
- ? await adapter.signTransaction(transaction)
548
- : new Error('Adapter does not support signTransaction');
549
- },
550
- signAllTransactions: async (transactions) => {
551
- return adapter.signAllTransactions !== undefined
552
- ? await adapter.signAllTransactions(transactions)
553
- : new Error('Adapter does not support signAllTransactions');
554
- },
555
- signMessage: async (message) => {
556
- return adapter.signMessage !== undefined
557
- ? await adapter.signMessage(message)
558
- : new Error('Adapter does not support signMessage');
559
- },
560
- signIn: async (input) => {
561
- return adapter.signIn !== undefined
562
- ? await adapter.signIn(input)
563
- : new Error('Adapter does not support signIn');
564
- },
565
- customFunctions: [
566
- 'sendSerializedTransaction',
567
- 'signAllSerializedTransactions',
568
- 'signSerializedTransaction',
569
- 'signSolanaTransactionBytes'
570
- ],
571
- // Sign on the parent (where the wallet is) so only bytes cross
572
- // Comlink — a Transaction object loses its prototype via
573
- // structured clone. connectedAddress (from the child) wins over
574
- // the parent publicKey, which may have drifted.
575
- signSolanaTransactionBytes: async (transaction, connectedAddress) => {
576
- return signSolanaBytes(adapter, new Uint8Array(transaction), connectedAddress);
577
- },
578
- sendSerializedTransaction: async (transaction, rpcUrl) => {
579
- const connection = new Connection(rpcUrl);
580
- const uint8Array = new Uint8Array(transaction);
581
- // Try to deserialize as VersionedTransaction first
582
- let deserializedTx;
583
- try {
584
- deserializedTx =
585
- VersionedTransaction.deserialize(uint8Array);
586
- }
587
- catch {
588
- // If that fails, try as legacy Transaction
589
- try {
590
- deserializedTx = Transaction.from(uint8Array);
591
- }
592
- catch {
593
- throw new Error('Failed to deserialize transaction as either versioned or legacy format');
594
- }
595
- }
596
- return await adapter.sendTransaction(deserializedTx, connection);
597
- },
598
- signSerializedTransaction: async (transaction) => {
599
- if (adapter.signTransaction === undefined) {
600
- return new Error('Adapter does not support signTransaction');
601
- }
602
- const uint8Array = new Uint8Array(transaction);
603
- // Try to deserialize as VersionedTransaction first
604
- let deserializedTx;
605
- try {
606
- deserializedTx =
607
- VersionedTransaction.deserialize(uint8Array);
608
- }
609
- catch {
610
- // If that fails, try as legacy Transaction
611
- try {
612
- deserializedTx = Transaction.from(uint8Array);
613
- }
614
- catch {
615
- return new Error('Failed to deserialize transaction as either versioned or legacy format');
616
- }
617
- }
618
- return await adapter.signTransaction(deserializedTx);
619
- },
620
- signAllSerializedTransactions: async (transactions) => {
621
- if (adapter.signAllTransactions === undefined) {
622
- return new Error('Adapter does not support signAllTransactions');
623
- }
624
- const deserializedTransactions = [];
625
- for (const transaction of transactions) {
626
- const uint8Array = new Uint8Array(transaction);
627
- // Try to deserialize as VersionedTransaction first
628
- let deserializedTx;
629
- try {
630
- deserializedTx =
631
- VersionedTransaction.deserialize(uint8Array);
632
- }
633
- catch {
634
- // If that fails, try as legacy Transaction
635
- try {
636
- deserializedTx = Transaction.from(uint8Array);
637
- }
638
- catch {
639
- return new Error('Failed to deserialize one or more transactions');
640
- }
641
- }
642
- deserializedTransactions.push(deserializedTx);
643
- }
644
- return await adapter.signAllTransactions(deserializedTransactions);
645
- }
646
- }
647
- : undefined
648
- });
545
+ solanaWallets.push(this.toSolanaWalletInfo(wallet));
649
546
  walletNames.add(wallet.name);
650
547
  }
651
548
  }
@@ -794,6 +691,157 @@ export class BridgeParent {
794
691
  }
795
692
  return solanaWallets;
796
693
  }
694
+ /** One registry wallet as `walletStandardWallets` publishes it. */
695
+ toSolanaWalletInfo(wallet) {
696
+ let adapter;
697
+ if (isWalletAdapterCompatibleStandardWallet(wallet)) {
698
+ adapter = new StandardWalletAdapter({ wallet });
699
+ }
700
+ return {
701
+ uuid: this.generateWalletId(wallet),
702
+ name: wallet.name,
703
+ chains: (wallet.chains || []),
704
+ features: Object.keys(wallet.features),
705
+ // Expect a TS error, as not all adapter properties are implemented
706
+ // @ts-expect-error
707
+ adapter: adapter
708
+ ? {
709
+ name: adapter.name,
710
+ url: adapter.url,
711
+ icon: adapter.icon,
712
+ readyState: adapter.readyState,
713
+ publicKey: Comlink.proxy({
714
+ get value() {
715
+ return adapter.publicKey;
716
+ },
717
+ toBase58: async () => {
718
+ return adapter.publicKey?.toBase58();
719
+ },
720
+ toJSON: async () => {
721
+ return adapter.publicKey?.toJSON();
722
+ },
723
+ toBytes: async () => {
724
+ return adapter.publicKey?.toBytes();
725
+ },
726
+ toBuffer: async () => {
727
+ return adapter.publicKey?.toBuffer();
728
+ },
729
+ toString: async () => {
730
+ return adapter.publicKey?.toString();
731
+ }
732
+ }),
733
+ connecting: adapter.connecting,
734
+ connected: adapter.connected,
735
+ supportedTransactionVersions: adapter.supportedTransactionVersions,
736
+ wallet: adapter.wallet,
737
+ standard: adapter.standard,
738
+ destroy: async () => await adapter.destroy(),
739
+ autoConnect: async () => await adapter.autoConnect(),
740
+ connect: async () => await adapter.connect(),
741
+ disconnect: async () => await adapter.disconnect(),
742
+ sendTransaction: async (transaction, connection, options) => await adapter.sendTransaction(transaction, connection, options),
743
+ signTransaction: async (transaction) => {
744
+ return adapter.signTransaction !== undefined
745
+ ? await adapter.signTransaction(transaction)
746
+ : new Error('Adapter does not support signTransaction');
747
+ },
748
+ signAllTransactions: async (transactions) => {
749
+ return adapter.signAllTransactions !== undefined
750
+ ? await adapter.signAllTransactions(transactions)
751
+ : new Error('Adapter does not support signAllTransactions');
752
+ },
753
+ signMessage: async (message) => {
754
+ return adapter.signMessage !== undefined
755
+ ? await adapter.signMessage(message)
756
+ : new Error('Adapter does not support signMessage');
757
+ },
758
+ signIn: async (input) => {
759
+ return adapter.signIn !== undefined
760
+ ? await adapter.signIn(input)
761
+ : new Error('Adapter does not support signIn');
762
+ },
763
+ customFunctions: [
764
+ 'sendSerializedTransaction',
765
+ 'signAllSerializedTransactions',
766
+ 'signSerializedTransaction',
767
+ 'signSolanaTransactionBytes'
768
+ ],
769
+ // Sign on the parent (where the wallet is) so only bytes cross
770
+ // Comlink — a Transaction object loses its prototype via
771
+ // structured clone. connectedAddress (from the child) wins over
772
+ // the parent publicKey, which may have drifted.
773
+ signSolanaTransactionBytes: async (transaction, connectedAddress) => {
774
+ return signSolanaBytes(adapter, new Uint8Array(transaction), connectedAddress);
775
+ },
776
+ sendSerializedTransaction: async (transaction, rpcUrl) => {
777
+ const connection = new Connection(rpcUrl);
778
+ const uint8Array = new Uint8Array(transaction);
779
+ // Try to deserialize as VersionedTransaction first
780
+ let deserializedTx;
781
+ try {
782
+ deserializedTx = VersionedTransaction.deserialize(uint8Array);
783
+ }
784
+ catch {
785
+ // If that fails, try as legacy Transaction
786
+ try {
787
+ deserializedTx = Transaction.from(uint8Array);
788
+ }
789
+ catch {
790
+ throw new Error('Failed to deserialize transaction as either versioned or legacy format');
791
+ }
792
+ }
793
+ return await adapter.sendTransaction(deserializedTx, connection);
794
+ },
795
+ signSerializedTransaction: async (transaction) => {
796
+ if (adapter.signTransaction === undefined) {
797
+ return new Error('Adapter does not support signTransaction');
798
+ }
799
+ const uint8Array = new Uint8Array(transaction);
800
+ // Try to deserialize as VersionedTransaction first
801
+ let deserializedTx;
802
+ try {
803
+ deserializedTx = VersionedTransaction.deserialize(uint8Array);
804
+ }
805
+ catch {
806
+ // If that fails, try as legacy Transaction
807
+ try {
808
+ deserializedTx = Transaction.from(uint8Array);
809
+ }
810
+ catch {
811
+ return new Error('Failed to deserialize transaction as either versioned or legacy format');
812
+ }
813
+ }
814
+ return await adapter.signTransaction(deserializedTx);
815
+ },
816
+ signAllSerializedTransactions: async (transactions) => {
817
+ if (adapter.signAllTransactions === undefined) {
818
+ return new Error('Adapter does not support signAllTransactions');
819
+ }
820
+ const deserializedTransactions = [];
821
+ for (const transaction of transactions) {
822
+ const uint8Array = new Uint8Array(transaction);
823
+ // Try to deserialize as VersionedTransaction first
824
+ let deserializedTx;
825
+ try {
826
+ deserializedTx = VersionedTransaction.deserialize(uint8Array);
827
+ }
828
+ catch {
829
+ // If that fails, try as legacy Transaction
830
+ try {
831
+ deserializedTx = Transaction.from(uint8Array);
832
+ }
833
+ catch {
834
+ return new Error('Failed to deserialize one or more transactions');
835
+ }
836
+ }
837
+ deserializedTransactions.push(deserializedTx);
838
+ }
839
+ return await adapter.signAllTransactions(deserializedTransactions);
840
+ }
841
+ }
842
+ : undefined
843
+ };
844
+ }
797
845
  isSolanaWallet(wallet) {
798
846
  const chains = wallet.chains || [];
799
847
  const hasSolanaChain = chains.some(chain => chain.startsWith('solana:'));
@@ -814,64 +862,103 @@ export class BridgeParent {
814
862
  */
815
863
  getBitcoinWallets() {
816
864
  const { get } = getWallets();
817
- const wallets = get();
818
- const bitcoinWallets = [];
819
- for (const wallet of wallets) {
820
- if (!this.isBitcoinWallet(wallet))
821
- continue;
822
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
823
- const features = wallet.features;
824
- const connectFeature = features['bitcoin:connect'];
825
- const disconnectFeature = features['bitcoin:disconnect'];
826
- const signMessageFeature = features['bitcoin:signMessage'];
827
- const signTransactionFeature = features['bitcoin:signTransaction'];
828
- bitcoinWallets.push({
829
- uuid: this.generateBip122WalletId(wallet),
865
+ return get()
866
+ .filter(wallet => this.isBitcoinWallet(wallet))
867
+ .map(wallet => this.toBip122WalletInfo(wallet));
868
+ }
869
+ /**
870
+ * Follow the Wallet Standard registry after the bridge starts (ONC-3456). A
871
+ * Solana or Bitcoin wallet that registers later (a slow extension, or one
872
+ * enabled after the page loaded) is appended to `walletStandardWallets` or
873
+ * `bip122Wallets`. The child copies those lists when Link creates its
874
+ * connector, which is well after the bridge starts, so a wallet that
875
+ * registered in between is offered. Any child reads the same arrays, so an
876
+ * older one benefits too.
877
+ *
878
+ * Append only: the child maps its copies to parent entries by index, so an
879
+ * entry never moves. A wallet already published is skipped: Bitcoin by uuid,
880
+ * as the child keeps the first entry with a uuid anyway; Solana by name, as
881
+ * `getSolanaWallets` skips the Base adapter when a wallet of that name is
882
+ * listed. An unregistered wallet stays
883
+ * listed; its calls fail as a removed extension's do.
884
+ */
885
+ followLateWallets() {
886
+ const { on } = getWallets();
887
+ if (typeof on !== 'function')
888
+ return;
889
+ this.stopFollowingRegistry = on('register', (...registered) => {
890
+ const api = this.parentAPI;
891
+ if (!api)
892
+ return;
893
+ const solanaNames = new Set(api.walletStandardWallets.map(entry => entry.name));
894
+ const bitcoinUuids = new Set(api.bip122Wallets.map(entry => entry.uuid));
895
+ for (const wallet of registered) {
896
+ if (this.isSolanaWallet(wallet) && !solanaNames.has(wallet.name)) {
897
+ solanaNames.add(wallet.name);
898
+ api.walletStandardWallets.push(this.toSolanaWalletInfo(wallet));
899
+ }
900
+ if (this.isBitcoinWallet(wallet)) {
901
+ const info = this.toBip122WalletInfo(wallet);
902
+ if (bitcoinUuids.has(info.uuid))
903
+ continue;
904
+ bitcoinUuids.add(info.uuid);
905
+ api.bip122Wallets.push(info);
906
+ }
907
+ }
908
+ });
909
+ }
910
+ toBip122WalletInfo(wallet) {
911
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
912
+ const features = wallet.features;
913
+ const connectFeature = features['bitcoin:connect'];
914
+ const disconnectFeature = features['bitcoin:disconnect'];
915
+ const signMessageFeature = features['bitcoin:signMessage'];
916
+ const signTransactionFeature = features['bitcoin:signTransaction'];
917
+ return {
918
+ uuid: this.generateBip122WalletId(wallet),
919
+ name: wallet.name,
920
+ features: Object.keys(wallet.features),
921
+ wallet: {
830
922
  name: wallet.name,
831
- features: Object.keys(wallet.features),
832
- wallet: {
833
- name: wallet.name,
834
- // A getter, not a snapshot: this list is built once, when the bridge
835
- // starts, and a grant usually lands later. An EVM or Solana connect
836
- // to MetaMask grants its Bitcoin account too, and a framed child
837
- // re-reads this to pick it up (Comlink runs the getter per read).
838
- get accounts() {
839
- return (wallet.accounts ?? []).map(toBip122WireAccount);
840
- },
841
- features: {
842
- 'bitcoin:connect': connectFeature
843
- ? Comlink.proxy({
844
- // Reshape the RESULT too, not just the snapshot above — the
845
- // accounts a connect returns are the same prototype-accessor
846
- // instances and would otherwise cross as `{}`.
847
- connect: async (input) => {
848
- const result = (await connectFeature.connect(input));
849
- return {
850
- accounts: (result?.accounts ?? []).map(toBip122WireAccount)
851
- };
852
- }
853
- })
854
- : undefined,
855
- 'bitcoin:disconnect': disconnectFeature
856
- ? Comlink.proxy({
857
- disconnect: async () => await disconnectFeature.disconnect()
858
- })
859
- : undefined,
860
- 'bitcoin:signMessage': signMessageFeature
861
- ? Comlink.proxy({
862
- signMessage: async (input) => await signMessageFeature.signMessage(input)
863
- })
864
- : undefined,
865
- 'bitcoin:signTransaction': signTransactionFeature
866
- ? Comlink.proxy({
867
- signTransaction: async (input) => await signTransactionFeature.signTransaction(input)
868
- })
869
- : undefined
870
- }
923
+ // A getter, not a snapshot: this list is built once, when the bridge
924
+ // starts, and a grant usually lands later. An EVM or Solana connect
925
+ // to MetaMask grants its Bitcoin account too, and a framed child
926
+ // re-reads this to pick it up (Comlink runs the getter per read).
927
+ get accounts() {
928
+ return (wallet.accounts ?? []).map(toBip122WireAccount);
929
+ },
930
+ features: {
931
+ 'bitcoin:connect': connectFeature
932
+ ? Comlink.proxy({
933
+ // Reshape the RESULT too, not just the snapshot above — the
934
+ // accounts a connect returns are the same prototype-accessor
935
+ // instances and would otherwise cross as `{}`.
936
+ connect: async (input) => {
937
+ const result = (await connectFeature.connect(input));
938
+ return {
939
+ accounts: (result?.accounts ?? []).map(toBip122WireAccount)
940
+ };
941
+ }
942
+ })
943
+ : undefined,
944
+ 'bitcoin:disconnect': disconnectFeature
945
+ ? Comlink.proxy({
946
+ disconnect: async () => await disconnectFeature.disconnect()
947
+ })
948
+ : undefined,
949
+ 'bitcoin:signMessage': signMessageFeature
950
+ ? Comlink.proxy({
951
+ signMessage: async (input) => await signMessageFeature.signMessage(input)
952
+ })
953
+ : undefined,
954
+ 'bitcoin:signTransaction': signTransactionFeature
955
+ ? Comlink.proxy({
956
+ signTransaction: async (input) => await signTransactionFeature.signTransaction(input)
957
+ })
958
+ : undefined
871
959
  }
872
- });
873
- }
874
- return bitcoinWallets;
960
+ }
961
+ };
875
962
  }
876
963
  generateWalletId(wallet) {
877
964
  const chain = wallet.chains?.[0] || 'unknown';
@@ -1074,17 +1161,7 @@ export class BridgeParent {
1074
1161
  */
1075
1162
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
1076
1163
  resolveBip122LegacyProvider(injectedId) {
1077
- if (typeof window === 'undefined' || !injectedId)
1078
- return undefined;
1079
- const parts = injectedId.split('.');
1080
- if (parts.some(part => !part ||
1081
- part === '__proto__' ||
1082
- part === 'prototype' ||
1083
- part === 'constructor')) {
1084
- return undefined;
1085
- }
1086
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
1087
- const provider = parts.reduce((acc, part) => acc?.[part], window);
1164
+ const provider = resolveBip122InjectedPath(injectedId);
1088
1165
  const surface = [
1089
1166
  'requestAccounts',
1090
1167
  'getAccounts',
@@ -1130,6 +1207,45 @@ export class BridgeParent {
1130
1207
  }
1131
1208
  return wallets;
1132
1209
  }
1210
+ /**
1211
+ * A sats-connect provider at a catalog `injectedId`: has `request()` and is
1212
+ * NOT UniSat-shaped (one of those goes to `bip122LegacyWallets` instead, so
1213
+ * a provider exposing both isn't relayed twice).
1214
+ */
1215
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
1216
+ resolveBip122SatsConnectProvider(injectedId) {
1217
+ const provider = resolveBip122InjectedPath(injectedId);
1218
+ if (!provider || typeof provider.request !== 'function')
1219
+ return undefined;
1220
+ return this.resolveBip122LegacyProvider(injectedId) ? undefined : provider;
1221
+ }
1222
+ getBip122SatsConnectWallets(injectedIds) {
1223
+ const wallets = [];
1224
+ const seen = new Set();
1225
+ for (const injectedId of injectedIds ?? []) {
1226
+ if (typeof injectedId !== 'string' || seen.has(injectedId))
1227
+ continue;
1228
+ if (!this.resolveBip122SatsConnectProvider(injectedId))
1229
+ continue;
1230
+ seen.add(injectedId);
1231
+ wallets.push({
1232
+ injectedId,
1233
+ provider: Comlink.proxy({
1234
+ // Re-resolved per call so a wallet that re-injects is still reached.
1235
+ request: async (method, params) => {
1236
+ assertRelayableSatsConnectCall(method, params);
1237
+ const provider = this.resolveBip122SatsConnectProvider(injectedId);
1238
+ if (!provider) {
1239
+ throw new Error(`No bitcoin provider at "${injectedId}"`);
1240
+ }
1241
+ // The reply is the wallet's JSON-RPC envelope; keep only JSON.
1242
+ return toPlainJson(await provider.request(method, params));
1243
+ }
1244
+ })
1245
+ });
1246
+ }
1247
+ return wallets;
1248
+ }
1133
1249
  getTronWallets(injectedIds) {
1134
1250
  if (typeof window === 'undefined' || injectedIds.length === 0) {
1135
1251
  return [];
@@ -1236,6 +1352,13 @@ export class BridgeParent {
1236
1352
  }
1237
1353
  destroy() {
1238
1354
  this.destroyed = true;
1355
+ try {
1356
+ this.stopFollowingRegistry?.();
1357
+ }
1358
+ catch {
1359
+ // Teardown must never throw.
1360
+ }
1361
+ this.stopFollowingRegistry = null;
1239
1362
  // Detach the 'message' listener(s) registered by expose(). Comlink only
1240
1363
  // removes them on a child RELEASE, so without this a closed session leaves a
1241
1364
  // stale listener that wins the next session's discovery reads (registration
@@ -1266,4 +1389,64 @@ export class BridgeParent {
1266
1389
  this.iframe = null;
1267
1390
  }
1268
1391
  }
1392
+ /**
1393
+ * The value at a dotted catalog `injectedId` on this (top) window, or
1394
+ * undefined. Prototype-walking segments are refused: the id comes from the
1395
+ * child.
1396
+ */
1397
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
1398
+ function resolveBip122InjectedPath(injectedId) {
1399
+ if (typeof window === 'undefined' || !injectedId)
1400
+ return undefined;
1401
+ const parts = injectedId.split('.');
1402
+ if (parts.some(part => !part ||
1403
+ part === '__proto__' ||
1404
+ part === 'prototype' ||
1405
+ part === 'constructor')) {
1406
+ return undefined;
1407
+ }
1408
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
1409
+ return parts.reduce((acc, part) => acc?.[part], window);
1410
+ }
1411
+ /**
1412
+ * Refuse a relayed sats-connect call outside the allowlist, and any
1413
+ * `signPsbt` asking the wallet to broadcast: Mesh broadcasts server-side after
1414
+ * checking the transaction against its quote, and a wallet broadcast would
1415
+ * skip that check.
1416
+ */
1417
+ function assertRelayableSatsConnectCall(method, params) {
1418
+ if (!SATS_CONNECT_RELAYED_METHODS.includes(method)) {
1419
+ throw new Error(`sats-connect method "${method}" is not relayed`);
1420
+ }
1421
+ if (method === 'signPsbt' &&
1422
+ params?.broadcast !== false) {
1423
+ throw new Error('sats-connect signPsbt is relayed only with broadcast: false');
1424
+ }
1425
+ }
1426
+ function toPlainJson(value) {
1427
+ return value === undefined ? undefined : JSON.parse(JSON.stringify(value));
1428
+ }
1429
+ function toRelayedSatsConnectEvent(event) {
1430
+ const { type, addresses, network } = (event ?? {});
1431
+ const relayed = {
1432
+ type: typeof type === 'string' ? type : 'unknown'
1433
+ };
1434
+ const networkName = network?.bitcoin?.name;
1435
+ if (typeof networkName === 'string') {
1436
+ relayed.network = { bitcoin: { name: networkName } };
1437
+ }
1438
+ if (Array.isArray(addresses)) {
1439
+ relayed.addresses = addresses
1440
+ .filter((entry) => !!entry && typeof entry.address === 'string')
1441
+ .map(entry => ({
1442
+ address: entry.address,
1443
+ publicKey: typeof entry.publicKey === 'string' ? entry.publicKey : '',
1444
+ purpose: typeof entry.purpose === 'string' ? entry.purpose : '',
1445
+ ...(typeof entry.addressType === 'string'
1446
+ ? { addressType: entry.addressType }
1447
+ : {})
1448
+ }));
1449
+ }
1450
+ return relayed;
1451
+ }
1269
1452
  //# sourceMappingURL=BridgeParent.js.map