@@ -2,12 +2,14 @@ import type { Subscriber, SubscriberCleanUp } from '@hub3js/core';
22import type { UtxoActions } from '@rango-dev/wallets-core/namespaces/utxo' ;
33import type { SignClientTypes } from '@walletconnect/types' ;
44
5+ import { ChangeAccountSubscriberBuilder } from '@hub3js/std/hooks' ;
56import {
67 CAIP_BITCOIN_CHAIN_ID ,
78 utils ,
89} from '@rango-dev/wallets-core/namespaces/utxo' ;
910import { AccountId } from 'caip' ;
1011
12+ import { type WalletConnectAdapter } from '../../adapter/adapter.js' ;
1113import { getAdapter } from '../../adapter/registry.js' ;
1214import {
1315 type Bip122AddressEntry ,
@@ -74,54 +76,56 @@ export function sessionEventSubscriber(): [
7476 Subscriber < UtxoActions > ,
7577 SubscriberCleanUp < UtxoActions >
7678] {
77- let handler : ( args : SignClientTypes . EventArguments [ 'session_event' ] ) => void ;
78-
79- return [
80- async ( context ) => {
81- const adapter = getAdapter ( ) ;
82- const client = await adapter . getClient ( ) ;
83- const [ , setState ] = context . state ( ) ;
84-
85- handler = ( args ) => {
86- const session = adapter . getSession ( 'utxo' ) ;
87- if ( ! session || args . topic !== session . topic ) {
88- return ;
89- }
90-
91- if ( args . params . event . name !== BitcoinEvents . ADDRESSES_CHANGED ) {
92- return ;
93- }
94-
95- const data = args . params . event . data as Bip122AddressEntry [ ] ;
96- if ( ! data ?. length ) {
97- void context . action ( 'disconnect' ) ;
98- return ;
99- }
100-
101- const paymentAddress = pickPaymentAddress ( data ) ;
102- if ( ! paymentAddress ) {
103- void context . action ( 'disconnect' ) ;
104- return ;
105- }
106-
107- setState (
108- 'accounts' ,
109- utils . formatAccountsToCAIP ( [ paymentAddress ] , CAIP_BITCOIN_CHAIN_ID )
110- ) ;
111- } ;
112-
113- client . on ( 'session_event' , handler ) ;
114- } ,
115- ( _ , err ) => {
79+ type SessionEvent = SignClientTypes . EventArguments [ 'session_event' ] ;
80+
81+ return new ChangeAccountSubscriberBuilder <
82+ SessionEvent ,
83+ WalletConnectAdapter ,
84+ UtxoActions
85+ > ( )
86+ . getInstance ( getAdapter )
87+ . onSwitchAccount ( ( event , context ) => {
88+ const args = event . payload ;
11689 const adapter = getAdapter ( ) ;
90+ const session = adapter . getSession ( 'utxo' ) ;
91+
92+ if ( ! session || args . topic !== session . topic ) {
93+ event . preventDefault ( ) ;
94+ return ;
95+ }
96+
97+ if ( args . params . event . name !== BitcoinEvents . ADDRESSES_CHANGED ) {
98+ event . preventDefault ( ) ;
99+ return ;
100+ }
101+
102+ const data = args . params . event . data as Bip122AddressEntry [ ] ;
103+ if ( ! data ?. length || ! pickPaymentAddress ( data ) ) {
104+ void context . action ( 'disconnect' ) ;
105+ event . preventDefault ( ) ;
106+ return ;
107+ }
108+
109+ // Let the default flow publish `accounts` via `format`.
110+ } )
111+ . format ( async ( _adapter , args ) => {
112+ const data = args . params . event . data as Bip122AddressEntry [ ] ;
113+ const paymentAddress = pickPaymentAddress ( data ) ;
114+ return paymentAddress
115+ ? utils . formatAccountsToCAIP ( [ paymentAddress ] , CAIP_BITCOIN_CHAIN_ID )
116+ : [ ] ;
117+ } )
118+ . addEventListener ( ( adapter , callback ) => {
117119 void adapter . getClient ( ) . then ( ( client ) => {
118- if ( handler ) {
119- client . off ( 'session_event' , handler ) ;
120- }
120+ client . on ( 'session_event' , callback ) ;
121121 } ) ;
122- return err ;
123- } ,
124- ] ;
122+ } )
123+ . removeEventListener ( ( adapter , callback ) => {
124+ void adapter . getClient ( ) . then ( ( client ) => {
125+ client . off ( 'session_event' , callback ) ;
126+ } ) ;
127+ } )
128+ . build ( ) ;
125129}
126130
127131/**
0 commit comments