@@ -29,16 +29,13 @@ const address_1 = require("./utils/address");
2929 * const provider = new Provider()
3030 */
3131class Provider {
32- constructor ( { logger, appId } ) {
32+ constructor ( { logger = console , appId } ) {
3333 this . chainId = 1 ;
3434 this . events = { } ;
3535 if ( Provider . instance ) {
3636 return Provider . instance ;
3737 }
3838 Provider . instance = this ;
39- if ( ! logger ) {
40- logger = console ;
41- }
4239 this . logger = logger ;
4340 this . appId = appId ;
4441 // bind functions (to prevent consumers from making unbound calls)
@@ -52,6 +49,7 @@ class Provider {
5249 window . anyweb = this ;
5350 }
5451 const messageHandler = ( event ) => {
52+ var _a ;
5553 if ( event . data &&
5654 ( 0 , common_1 . isObject ) ( event . data ) &&
5755 'type' in event . data &&
@@ -60,25 +58,25 @@ class Provider {
6058 if ( IframeData . type == 'event' &&
6159 IframeData . data == 'ready' &&
6260 IframeData . success ) {
63- console . debug ( '[AnyWeb] SDK初始化完成' ) ;
61+ ( _a = this . logger ) === null || _a === void 0 ? void 0 : _a . debug ( '[AnyWeb] SDK初始化完成' ) ;
6462 Provider . ready = true ;
6563 this . events . onReady && this . events . onReady ( ) ;
6664 window . removeEventListener ( 'message' , messageHandler ) ;
6765 }
6866 }
6967 } ;
7068 window . addEventListener ( 'message' , messageHandler ) ;
71- ( 0 , common_1 . createIframe ) ( 'pages/index/home' )
69+ ( 0 , common_1 . createIframe ) ( 'pages/index/home' , this . logger )
7270 . then ( )
73- . catch ( ( e ) => console . error ( '[AnyWeb] createIframe error' , e ) ) ;
71+ . catch ( ( e ) => { var _a ; return ( _a = this . logger ) === null || _a === void 0 ? void 0 : _a . error ( '[AnyWeb] createIframe error' , e ) ; } ) ;
7472 }
7573 static getInstance ( params ) {
7674 if ( ! Provider . instance ) {
7775 if ( params ) {
7876 Provider . instance = new Provider ( params ) ;
7977 }
8078 else {
81- throw new Error ( '[AnyWeb] Provider is not initialized') ;
79+ throw new common_1 . ProviderRpcError ( common_1 . ProviderErrorCode . SDKNotReady , ' Provider is not initialized') ;
8280 }
8381 }
8482 return Provider . instance ;
@@ -88,21 +86,23 @@ class Provider {
8886 * @param arg
8987 */
9088 send ( ...arg ) {
89+ var _a ;
9190 return __awaiter ( this , void 0 , void 0 , function * ( ) {
92- console . info ( '[AnyWeb] `send` is deprecated, use `request` instead' ) ;
91+ ( _a = this . logger ) === null || _a === void 0 ? void 0 : _a . info ( '[AnyWeb] `send` is deprecated, use `request` instead' ) ;
9392 if ( arg . length > 1 ) {
9493 return yield this . request ( { method : arg [ 0 ] , params : arg [ 1 ] } ) ;
9594 }
96- throw new Error ( 'Invalid arguments' ) ;
95+ throw new common_1 . ProviderRpcError ( common_1 . ProviderErrorCode . ParamsError , 'Invalid arguments' ) ;
9796 } ) ;
9897 }
9998 /**
10099 * Deprecated: use `request` instead
101100 * @param arg
102101 */
103102 call ( ...arg ) {
103+ var _a ;
104104 return __awaiter ( this , void 0 , void 0 , function * ( ) {
105- console . info ( '[AnyWeb] `call` is deprecated, use `request` instead' , arg ) ;
105+ ( _a = this . logger ) === null || _a === void 0 ? void 0 : _a . info ( '[AnyWeb] `call` is deprecated, use `request` instead' , arg ) ;
106106 if ( arg . length > 1 ) {
107107 return yield this . request ( { method : arg [ 0 ] , params : arg [ 1 ] } ) ;
108108 }
@@ -119,17 +119,18 @@ class Provider {
119119 * const result = await provider.request({ method: 'cfx_sendTransaction', params})
120120 */
121121 request ( args ) {
122+ var _a , _b ;
122123 return __awaiter ( this , void 0 , void 0 , function * ( ) {
123124 if ( ! args || typeof args !== 'object' || Array . isArray ( args ) ) {
124- throw new Error ( 'Invalid request arguments' ) ;
125+ throw new common_1 . ProviderRpcError ( common_1 . ProviderErrorCode . ParamsError , 'Invalid request arguments' ) ;
125126 }
126127 const { method, params } = args ;
127128 if ( ! method || method . trim ( ) . length === 0 ) {
128- throw new Error ( ' Method is required') ;
129+ throw new common_1 . ProviderRpcError ( common_1 . ProviderErrorCode . ParamsError , 'Invalid request arguments: Method is required') ;
129130 }
130- console . debug ( `[AnyWeb] request ${ method } with` , params ) ;
131+ ( _a = this . logger ) === null || _a === void 0 ? void 0 : _a . debug ( `[AnyWeb] request ${ method } with` , params ) ;
131132 const result = yield this . rawRequest ( method , params ) ;
132- console . debug ( `[AnyWeb] request(${ method } ):` , result ) ;
133+ ( _b = this . logger ) === null || _b === void 0 ? void 0 : _b . debug ( `[AnyWeb] request(${ method } ):` , result ) ;
133134 return result ;
134135 } ) ;
135136 }
@@ -149,16 +150,18 @@ class Provider {
149150 * @param params
150151 * @protected
151152 */
153+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
152154 rawRequest ( method , params ) {
155+ var _a , _b , _c , _d , _e , _f ;
153156 return __awaiter ( this , void 0 , void 0 , function * ( ) {
154157 if ( ! Provider . ready ) {
155- throw new Error ( "[AnyWeb] Provider is not ready, please use on('ready', callback) to listen to ready event") ;
158+ throw new common_1 . ProviderRpcError ( common_1 . ProviderErrorCode . SDKNotReady , " Provider is not ready, please use on('ready', callback) to listen to ready event") ;
156159 }
157160 switch ( method ) {
158161 case 'cfx_requestAccounts' :
159162 return this . rawRequest ( 'cfx_accounts' ) ;
160163 case 'cfx_accounts' :
161- console . debug ( '[AnyWeb]' , { params } ) ;
164+ ( _a = this . logger ) === null || _a === void 0 ? void 0 : _a . debug ( '[AnyWeb]' , { params } ) ;
162165 const scopes = params [ 0 ] . scopes ;
163166 let result ;
164167 try {
@@ -170,10 +173,10 @@ class Provider {
170173 scopes : scopes ,
171174 silence : true ,
172175 } , this ) ) ;
173- console . debug ( '[AnyWeb]' , 'silent auth result' , result ) ;
176+ ( _b = this . logger ) === null || _b === void 0 ? void 0 : _b . debug ( '[AnyWeb]' , 'silent auth result' , result ) ;
174177 }
175178 catch ( e ) {
176- console . debug ( '[AnyWeb]' , 'need to auth' , e ) ;
179+ ( _c = this . logger ) === null || _c === void 0 ? void 0 : _c . debug ( '[AnyWeb]' , 'need to auth' , e ) ;
177180 result = ( yield ( 0 , common_1 . callIframe ) ( 'pages/dapp/auth' , {
178181 appId : this . appId ,
179182 params : params ? JSON . stringify ( params [ 0 ] ) : '' ,
@@ -208,7 +211,7 @@ class Provider {
208211 const to = payload . to ;
209212 if ( to ) {
210213 authType =
211- ( 0 , address_1 . getAddressType ) ( to ) === address_1 . AddressType . CONTRACT
214+ ( 0 , address_1 . getAddressType ) ( to , this . logger ) === address_1 . AddressType . CONTRACT
212215 ? 'callContract'
213216 : 'createTransaction' ;
214217 }
@@ -229,8 +232,7 @@ class Provider {
229232 } , this ) ;
230233 }
231234 catch ( e ) {
232- console . error ( '[AnyWeb] Error to sendTransaction' , e ) ;
233- return e ;
235+ throw new common_1 . ProviderRpcError ( common_1 . ProviderErrorCode . SendTransactionError , 'Error to sendTransaction: ' + e ) ;
234236 }
235237 case 'anyweb_importAccount' :
236238 try {
@@ -242,8 +244,7 @@ class Provider {
242244 } , this ) ;
243245 }
244246 catch ( e ) {
245- console . error ( '[AnyWeb] Error to import Address' , e ) ;
246- return e ;
247+ throw new common_1 . ProviderRpcError ( common_1 . ProviderErrorCode . ImportAddressError , 'Error to import Address: ' + e ) ;
247248 }
248249 case 'anyweb_version' :
249250 return package_json_1 . default . version ;
@@ -272,10 +273,10 @@ class Provider {
272273 authType : 'check_identify' ,
273274 silence : true ,
274275 } , this ) ;
275- console . debug ( '[AnyWeb]' , 'Check identify result' , identifyResult ) ;
276+ ( _d = this . logger ) === null || _d === void 0 ? void 0 : _d . debug ( '[AnyWeb]' , 'Check identify result' , identifyResult ) ;
276277 }
277278 catch ( e ) {
278- console . debug ( '[AnyWeb]' , 'need to identify' , e ) ;
279+ ( _e = this . logger ) === null || _e === void 0 ? void 0 : _e . debug ( '[AnyWeb]' , 'need to identify' , e ) ;
279280 identifyResult = yield ( 0 , common_1 . callIframe ) ( 'pages/user/identify' , {
280281 appId : this . appId ,
281282 chainId : this . chainId ,
@@ -304,11 +305,11 @@ class Provider {
304305 } , this ) ;
305306 }
306307 catch ( e ) {
307- console . debug ( '[AnyWeb]' , 'need to login' , e ) ;
308+ ( _f = this . logger ) === null || _f === void 0 ? void 0 : _f . debug ( '[AnyWeb]' , 'need to login' , e ) ;
308309 return false ;
309310 }
310311 default :
311- return 'Unsupported method' ;
312+ throw new common_1 . ProviderRpcError ( common_1 . ProviderErrorCode . UnsupportedMethod , 'Unsupported Method: ' + method ) ;
312313 }
313314 } ) ;
314315 }
@@ -320,7 +321,8 @@ class Provider {
320321 * provider.on('connected', listener)
321322 */
322323 on ( type , listener ) {
323- console . debug ( '[AnyWeb] on' , {
324+ var _a ;
325+ ( _a = this . logger ) === null || _a === void 0 ? void 0 : _a . debug ( '[AnyWeb] on' , {
324326 type,
325327 listener,
326328 } ) ;
0 commit comments