1- import type { ProviderOptions , ProviderInterface , AccountInterface } from "starknet"
2- import { constants } from "starknet" ;
3-
4- import type { RequestFnCall , RpcMessage , RpcTypeToMessageMap , StarknetWindowObject , TypedData } from "@starknet-io/types-js"
5-
6- import Controller , { type ControllerOptions } from "@cartridge/controller" ;
1+ import type {
2+ ProviderOptions ,
3+ ProviderInterface ,
4+ AccountInterface ,
5+ } from "starknet"
6+ import { constants } from "starknet"
7+
8+ import type {
9+ RequestFnCall ,
10+ RpcMessage ,
11+ RpcTypeToMessageMap ,
12+ StarknetWindowObject ,
13+ TypedData ,
14+ } from "@starknet-io/types-js"
15+
16+ import Controller , { type ControllerOptions } from "@cartridge/controller"
717import { Connector , type ConnectArgs , type ConnectorData } from "../connector"
8- import { ConnectorNotConnectedError , UserNotConnectedError , UserRejectedRequestError } from "../../errors"
18+ import {
19+ ConnectorNotConnectedError ,
20+ UserNotConnectedError ,
21+ UserRejectedRequestError ,
22+ } from "../../errors"
923
10- import { CONTROLLER_ICON , RPC_URLS } from "./constants" ;
24+ import { CONTROLLER_ICON , RPC_URLS } from "./constants"
1125
1226export class ControllerConnector extends Connector {
13- private controller : Controller | null ;
27+ private controller : Controller | null
1428
1529 constructor ( options : Partial < ControllerOptions > = { } ) {
16- super ( ) ;
30+ super ( )
1731
1832 // Can pass in defaultChainId = SN_SEPOLIA to connect to Sepolia testnet
19- options . defaultChainId = options . defaultChainId || constants . StarknetChainId . SN_MAIN ;
20- options . chains = options . chains || [ { rpcUrl : RPC_URLS [ options . defaultChainId as keyof typeof RPC_URLS ] } ] ;
33+ options . defaultChainId =
34+ options . defaultChainId || constants . StarknetChainId . SN_MAIN
35+ options . chains = options . chains || [
36+ { rpcUrl : RPC_URLS [ options . defaultChainId as keyof typeof RPC_URLS ] } ,
37+ ]
2138
2239 this . controller = this . available ( )
2340 ? new Controller ( options as ControllerOptions )
24- : null ;
41+ : null
2542 }
2643
27- get id ( ) { return "cartridgeController" }
28- get name ( ) { return "Cartridge Controller" }
29- get icon ( ) { return { light : CONTROLLER_ICON , dark : CONTROLLER_ICON } }
44+ get id ( ) {
45+ return "cartridgeController"
46+ }
47+ get name ( ) {
48+ return "Cartridge Controller"
49+ }
50+ get icon ( ) {
51+ return { light : CONTROLLER_ICON , dark : CONTROLLER_ICON }
52+ }
3053
3154 available ( ) {
32- return typeof window !== "undefined" ;
55+ return typeof window !== "undefined"
3356 }
3457
3558 async ready ( ) {
3659 if ( ! this . controller ) {
37- return false ;
60+ return false
3861 }
3962
40- const account = await this . controller . probe ( ) ;
41- return account !== null ;
63+ const account = await this . controller . probe ( )
64+ return account !== null
4265 }
4366
4467 async connect ( _args ?: ConnectArgs ) : Promise < ConnectorData > {
45- if ( ! this . controller ) { throw new ConnectorNotConnectedError ( ) ; }
68+ if ( ! this . controller ) {
69+ throw new ConnectorNotConnectedError ( )
70+ }
4671
47- const account = await this . controller . connect ( ) ;
72+ const account = await this . controller . connect ( )
4873
49- if ( ! account ) { throw new UserNotConnectedError ( ) ; }
74+ if ( ! account ) {
75+ throw new UserNotConnectedError ( )
76+ }
5077
51- const chainId = await this . chainId ( ) ;
78+ const chainId = await this . chainId ( )
5279
5380 /**
5481 * @dev This emit ensures compatibility with starknet-react
@@ -57,67 +84,83 @@ export class ControllerConnector extends Connector {
5784
5885 return {
5986 account : account . address ,
60- chainId : chainId
61- } ;
87+ chainId : chainId ,
88+ }
6289 }
6390
6491 async disconnect ( ) : Promise < void > {
65- if ( ! this . controller ) { throw new ConnectorNotConnectedError ( ) ; }
92+ if ( ! this . controller ) {
93+ throw new ConnectorNotConnectedError ( )
94+ }
6695
6796 /**
6897 * @dev This emit ensures compatibility with starknet-react
6998 */
7099 this . emit ( "disconnect" )
71100
72- return this . controller . disconnect ( ) ;
101+ return this . controller . disconnect ( )
73102 }
74103
75- async account ( _provider : ProviderOptions | ProviderInterface ) : Promise < AccountInterface > {
76- if ( ! this . controller ) { throw new ConnectorNotConnectedError ( ) ; }
104+ async account (
105+ _provider : ProviderOptions | ProviderInterface ,
106+ ) : Promise < AccountInterface > {
107+ if ( ! this . controller ) {
108+ throw new ConnectorNotConnectedError ( )
109+ }
77110
78- const account = await this . controller . probe ( ) ;
111+ const account = await this . controller . probe ( )
79112
80- if ( ! account ) { throw new UserNotConnectedError ( ) ; }
113+ if ( ! account ) {
114+ throw new UserNotConnectedError ( )
115+ }
81116
82- return account ;
117+ return account
83118 }
84119
85120 async chainId ( ) : Promise < bigint > {
86- if ( ! this . controller ) { throw new ConnectorNotConnectedError ( ) ; }
121+ if ( ! this . controller ) {
122+ throw new ConnectorNotConnectedError ( )
123+ }
87124
88- const account = await this . controller . probe ( ) ;
125+ const account = await this . controller . probe ( )
89126
90- if ( ! account ) { throw new UserNotConnectedError ( ) ; }
127+ if ( ! account ) {
128+ throw new UserNotConnectedError ( )
129+ }
91130
92- return BigInt ( await account . getChainId ( ) ) ;
131+ return BigInt ( await account . getChainId ( ) )
93132 }
94133
95134 async request < T extends RpcMessage [ "type" ] > (
96- call : RequestFnCall < T >
135+ call : RequestFnCall < T > ,
97136 ) : Promise < RpcTypeToMessageMap [ T ] [ "result" ] > {
98- if ( ! this . controller ) { throw new ConnectorNotConnectedError ( ) ; }
137+ if ( ! this . controller ) {
138+ throw new ConnectorNotConnectedError ( )
139+ }
99140
100141 // Handle SNIP-12 compliance for signTypedData requests
101142 if ( call . type === "wallet_signTypedData" ) {
102- const params = call . params as TypedData ;
143+ const params = call . params as TypedData
103144 if ( ! params . types . StarknetDomain ) {
104- throw new Error (
105- `Controller requires a SNIP-12 version 1 domain separator. ` +
106- `See: https://github.com/starknet-io/SNIPs/blob/main/SNIPS/snip-12.md#domain-separator`
107- ) ;
108- }
145+ throw new Error (
146+ `Controller requires a SNIP-12 version 1 domain separator. ` +
147+ `See: https://github.com/starknet-io/SNIPs/blob/main/SNIPS/snip-12.md#domain-separator` ,
148+ )
149+ }
109150 }
110151
111152 try {
112- return await this . controller . request ( call ) ;
153+ return await this . controller . request ( call )
113154 } catch {
114- throw new UserRejectedRequestError ( ) ;
155+ throw new UserRejectedRequestError ( )
115156 }
116157 }
117158
118159 get wallet ( ) : StarknetWindowObject {
119- if ( ! this . controller ) { throw new ConnectorNotConnectedError ( ) ; }
160+ if ( ! this . controller ) {
161+ throw new ConnectorNotConnectedError ( )
162+ }
120163
121- return this . controller ;
164+ return this . controller
122165 }
123166}
0 commit comments