@@ -3,34 +3,63 @@ import type { CaipAccountId } from '@metamask/utils';
33import type { ExtendedTransport , RPCAPI , Scope , SessionData } from 'src/domain' ;
44import { addValidAccounts , getOptionalScopes , getValidAccounts } from 'src/multichain/utils' ;
55
6+ const DEFAULT_REQUEST_TIMEOUT = 60 * 1000 ;
7+
68export class DefaultTransport implements ExtendedTransport {
9+ #notificationCallbacks: Set < ( data : unknown ) => void > = new Set ( ) ;
710 #requestId = 0 ;
811 #transport: Transport = getDefaultTransport ( ) ;
12+ #defaultRequestOptions = {
13+ timeout : DEFAULT_REQUEST_TIMEOUT ,
14+ } ;
15+
16+ #notifyCallbacks( data : unknown ) {
17+ for ( const cb of this . #notificationCallbacks) {
18+ try {
19+ cb ( data ) ;
20+ } catch ( err ) {
21+ console . log ( '[WindowPostMessageTransport] notifyCallbacks error:' , err ) ;
22+ }
23+ }
24+ }
925
1026 async connect ( options ?: { scopes : Scope [ ] ; caipAccountIds : CaipAccountId [ ] } ) : Promise < void > {
1127 await this . #transport. connect ( ) ;
28+
1229 //Get wallet session
13- const sessionRequest = await this . request ( { method : 'wallet_getSession' } ) ;
30+ const sessionRequest = await this . request ( { method : 'wallet_getSession' } , this . #defaultRequestOptions ) ;
1431 let walletSession = sessionRequest . result as SessionData ;
1532 if ( walletSession && options ) {
1633 const currentScopes = Object . keys ( walletSession ?. sessionScopes ?? { } ) as Scope [ ] ;
1734 const proposedScopes = options ?. scopes ?? [ ] ;
1835 const isSameScopes = currentScopes . every ( ( scope ) => proposedScopes . includes ( scope ) ) && proposedScopes . every ( ( scope ) => currentScopes . includes ( scope ) ) ;
1936 if ( ! isSameScopes ) {
20- await this . request ( { method : 'wallet_revokeSession' , params : walletSession } ) ;
37+ await this . request ( { method : 'wallet_revokeSession' , params : walletSession } , this . #defaultRequestOptions ) ;
2138 const optionalScopes = addValidAccounts ( getOptionalScopes ( options ?. scopes ?? [ ] ) , getValidAccounts ( options ?. caipAccountIds ?? [ ] ) ) ;
2239 const sessionRequest : CreateSessionParams < RPCAPI > = { optionalScopes } ;
23- const response = await this . request ( { method : 'wallet_createSession' , params : sessionRequest } ) ;
40+ const response = await this . request ( { method : 'wallet_createSession' , params : sessionRequest } , this . #defaultRequestOptions) ;
41+ if ( response . error ) {
42+ throw new Error ( response . error . message ) ;
43+ }
2444 walletSession = response . result as SessionData ;
2545 }
26- } else {
46+ } else if ( ! walletSession ) {
2747 const optionalScopes = addValidAccounts ( getOptionalScopes ( options ?. scopes ?? [ ] ) , getValidAccounts ( options ?. caipAccountIds ?? [ ] ) ) ;
2848 const sessionRequest : CreateSessionParams < RPCAPI > = { optionalScopes } ;
29- await this . request ( { method : 'wallet_createSession' , params : sessionRequest } ) ;
49+ const response = await this . request ( { method : 'wallet_createSession' , params : sessionRequest } , this . #defaultRequestOptions) ;
50+ if ( response . error ) {
51+ throw new Error ( response . error . message ) ;
52+ }
53+ walletSession = response . result as SessionData ;
3054 }
55+ this . #notifyCallbacks( {
56+ method : 'wallet_sessionChanged' ,
57+ params : walletSession ,
58+ } ) ;
3159 }
3260
3361 async disconnect ( ) : Promise < void > {
62+ this . #notificationCallbacks. clear ( ) ;
3463 return this . #transport. disconnect ( ) ;
3564 }
3665
@@ -39,15 +68,14 @@ export class DefaultTransport implements ExtendedTransport {
3968 }
4069
4170 async request < TRequest extends TransportRequest , TResponse extends TransportResponse > ( request : TRequest , options ?: { timeout ?: number } ) {
42- const requestWithId = {
43- ...request ,
44- jsonrpc : '2.0' ,
45- id : `${ this . #requestId++ } ` ,
46- } ;
47- return this . #transport. request ( requestWithId , options ) as Promise < TResponse > ;
71+ return this . #transport. request ( request , options ) as Promise < TResponse > ;
4872 }
4973
5074 onNotification ( callback : ( data : unknown ) => void ) {
51- return this . #transport. onNotification ( callback ) ;
75+ this . #transport. onNotification ( callback ) ;
76+ this . #notificationCallbacks. add ( callback ) ;
77+ return ( ) => {
78+ this . #notificationCallbacks. delete ( callback ) ;
79+ } ;
5280 }
5381}
0 commit comments