@@ -193,62 +193,62 @@ export class Provider implements IProvider {
193193 "Provider is not ready, please use on('ready', callback) to listen to ready event"
194194 )
195195 }
196- switch ( method ) {
197- case 'cfx_requestAccounts' :
198- return this . rawRequest ( 'cfx_accounts' )
199- case 'cfx_accounts' :
200- this . logger ?. debug ( '[AnyWeb]' , { params } )
201- const scopes = params [ 0 ] . scopes
202- let result : IAuthResult
203- try {
204- result = ( await callIframe (
205- 'pages/dapp/auth' ,
206- {
207- appId : this . appId ,
208- params : params ? JSON . stringify ( params [ 0 ] ) : '' ,
209- chainId : this . chainId ,
210- authType : 'check_auth' ,
211- scopes : scopes ,
212- silence : true ,
213- } ,
214- this
215- ) ) as IAuthResult
216- this . logger ?. debug ( '[AnyWeb]' , 'silent auth result' , result )
217- } catch ( e ) {
218- this . logger ?. debug ( '[AnyWeb]' , 'need to auth' , e )
219- result = ( await callIframe (
220- 'pages/dapp/auth' ,
221- {
222- appId : this . appId ,
223- params : params ? JSON . stringify ( params [ 0 ] ) : '' ,
224- chainId : this . chainId ,
225- authType : 'account' ,
226- scopes : scopes ,
227- } ,
228- this
229- ) ) as IAuthResult
230- }
196+ try {
197+ switch ( method ) {
198+ case 'cfx_requestAccounts' :
199+ return this . rawRequest ( 'cfx_accounts' )
200+ case 'cfx_accounts' :
201+ this . logger ?. debug ( '[AnyWeb]' , { params } )
202+ const scopes = params [ 0 ] . scopes
203+ let result : IAuthResult
204+ try {
205+ result = ( await callIframe (
206+ 'pages/dapp/auth' ,
207+ {
208+ appId : this . appId ,
209+ params : params ? JSON . stringify ( params [ 0 ] ) : '' ,
210+ chainId : this . chainId ,
211+ authType : 'check_auth' ,
212+ scopes : scopes ,
213+ silence : true ,
214+ } ,
215+ this
216+ ) ) as IAuthResult
217+ this . logger ?. debug ( '[AnyWeb]' , 'silent auth result' , result )
218+ } catch ( e ) {
219+ this . logger ?. debug ( '[AnyWeb]' , 'need to auth' , e )
220+ result = ( await callIframe (
221+ 'pages/dapp/auth' ,
222+ {
223+ appId : this . appId ,
224+ params : params ? JSON . stringify ( params [ 0 ] ) : '' ,
225+ chainId : this . chainId ,
226+ authType : 'account' ,
227+ scopes : scopes ,
228+ } ,
229+ this
230+ ) ) as IAuthResult
231+ }
231232
232- result . scopes = scopes
233- this . events . onAccountsChanged &&
234- this . events . onAccountsChanged ( result . address )
235- this . events . onChainChanged &&
236- this . events . onChainChanged ( String ( result . chainId ) )
237- this . events . onNetworkChanged &&
238- this . events . onNetworkChanged ( String ( result . networkId ) )
239- if ( scopes . length > 0 ) {
240- return {
241- address : result . address ,
242- code : result . code ,
243- scopes : scopes ,
244- chainId : result . chainId ,
245- networkId : result . networkId ,
233+ result . scopes = scopes
234+ this . events . onAccountsChanged &&
235+ this . events . onAccountsChanged ( result . address )
236+ this . events . onChainChanged &&
237+ this . events . onChainChanged ( String ( result . chainId ) )
238+ this . events . onNetworkChanged &&
239+ this . events . onNetworkChanged ( String ( result . networkId ) )
240+ if ( scopes . length > 0 ) {
241+ return {
242+ address : result . address ,
243+ code : result . code ,
244+ scopes : scopes ,
245+ chainId : result . chainId ,
246+ networkId : result . networkId ,
247+ }
248+ } else {
249+ return false
246250 }
247- } else {
248- return false
249- }
250- case 'cfx_sendTransaction' :
251- try {
251+ case 'cfx_sendTransaction' :
252252 let authType : IIframeOptions [ 'authType' ]
253253 const payload = params [ 0 ]
254254 const to = payload . to
@@ -261,7 +261,6 @@ export class Provider implements IProvider {
261261 authType = 'createContract'
262262 }
263263
264- // createContract
265264 return await callIframe (
266265 'pages/dapp/auth' ,
267266 {
@@ -277,15 +276,7 @@ export class Provider implements IProvider {
277276 } ,
278277 this
279278 )
280- } catch ( e : any ) {
281- throw new ProviderRpcError (
282- ProviderErrorCode . SendTransactionError ,
283- 'Error to sendTransaction: ' + e . message ,
284- e . data
285- )
286- }
287- case 'anyweb_importAccount' :
288- try {
279+ case 'anyweb_importAccount' :
289280 return await callIframe (
290281 'pages/dapp/auth' ,
291282 {
@@ -296,107 +287,113 @@ export class Provider implements IProvider {
296287 } ,
297288 this
298289 )
299- } catch ( e ) {
300- throw new ProviderRpcError (
301- ProviderErrorCode . ImportAddressError ,
302- 'Error to import Address: ' + e
303- )
304- }
305- case 'anyweb_version' :
306- return config . version
307- case 'anyweb_home' :
308- return await callIframe (
309- 'pages/index/home' ,
310- {
311- appId : this . appId ,
312- chainId : this . chainId ,
313- params : params ? JSON . stringify ( params ) : '' ,
314- waitResult : false ,
315- } ,
316- this
317- )
318- case 'exit_accounts' :
319- return this . rawRequest ( 'anyweb_revoke' , params )
320- case 'anyweb_revoke' :
321- return await callIframe (
322- 'pages/dapp/auth' ,
323- {
324- appId : this . appId ,
325- chainId : this . chainId ,
326- params : params ? JSON . stringify ( params ) : '' ,
327- authType : 'exit_accounts' ,
328- silence : true ,
329- } ,
330- this
331- )
332- case 'anyweb_identify' :
333- let identifyResult
334- try {
335- identifyResult = await callIframe (
336- 'pages/user/identify' ,
290+ case 'anyweb_version' :
291+ return config . version
292+ case 'anyweb_home' :
293+ return await callIframe (
294+ 'pages/index/home' ,
337295 {
338296 appId : this . appId ,
339297 chainId : this . chainId ,
340298 params : params ? JSON . stringify ( params ) : '' ,
341- authType : 'check_identify' ,
342- silence : true ,
299+ waitResult : false ,
343300 } ,
344301 this
345302 )
346- this . logger ?. debug (
347- '[AnyWeb]' ,
348- 'Check identify result' ,
349- identifyResult
350- )
351- } catch ( e ) {
352- this . logger ?. debug ( '[AnyWeb]' , 'need to identify' , e )
353- identifyResult = await callIframe (
354- 'pages/user/identify' ,
303+ case 'exit_accounts' :
304+ return this . rawRequest ( 'anyweb_revoke' , params )
305+ case 'anyweb_revoke' :
306+ return await callIframe (
307+ 'pages/dapp/auth' ,
355308 {
356309 appId : this . appId ,
357310 chainId : this . chainId ,
358311 params : params ? JSON . stringify ( params ) : '' ,
359- authType : 'identify' ,
312+ authType : 'exit_accounts' ,
313+ silence : true ,
360314 } ,
361315 this
362316 )
363- }
364- return identifyResult
365- case 'anyweb_logout' :
366- // Logout the account of AnyWeb
367- return await callIframe (
368- 'pages/dapp/auth' ,
369- {
370- appId : this . appId ,
371- chainId : this . chainId ,
372- params : params ? JSON . stringify ( params ) : '' ,
373- authType : 'logout' ,
374- silence : true ,
375- } ,
376- this
377- )
378- case 'anyweb_loginstate' :
379- try {
317+ case 'anyweb_identify' :
318+ let identifyResult
319+ try {
320+ identifyResult = await callIframe (
321+ 'pages/user/identify' ,
322+ {
323+ appId : this . appId ,
324+ chainId : this . chainId ,
325+ params : params ? JSON . stringify ( params ) : '' ,
326+ authType : 'check_identify' ,
327+ silence : true ,
328+ } ,
329+ this
330+ )
331+ this . logger ?. debug (
332+ '[AnyWeb]' ,
333+ 'Check identify result' ,
334+ identifyResult
335+ )
336+ } catch ( e ) {
337+ this . logger ?. debug ( '[AnyWeb]' , 'need to identify' , e )
338+ identifyResult = await callIframe (
339+ 'pages/user/identify' ,
340+ {
341+ appId : this . appId ,
342+ chainId : this . chainId ,
343+ params : params ? JSON . stringify ( params ) : '' ,
344+ authType : 'identify' ,
345+ } ,
346+ this
347+ )
348+ }
349+ return identifyResult
350+ case 'anyweb_logout' :
351+ // Logout the account of AnyWeb
380352 return await callIframe (
381353 'pages/dapp/auth' ,
382354 {
383355 appId : this . appId ,
384- params : '' ,
385356 chainId : this . chainId ,
386- authType : 'check_login' ,
357+ params : params ? JSON . stringify ( params ) : '' ,
358+ authType : 'logout' ,
387359 silence : true ,
388360 } ,
389361 this
390362 )
391- } catch ( e ) {
392- this . logger ?. debug ( '[AnyWeb]' , 'need to login' , e )
393- return false
394- }
395- default :
396- throw new ProviderRpcError (
397- ProviderErrorCode . UnsupportedMethod ,
398- 'Unsupported Method: ' + method
399- )
363+ case 'anyweb_loginstate' :
364+ try {
365+ return await callIframe (
366+ 'pages/dapp/auth' ,
367+ {
368+ appId : this . appId ,
369+ params : '' ,
370+ chainId : this . chainId ,
371+ authType : 'check_login' ,
372+ silence : true ,
373+ } ,
374+ this
375+ )
376+ } catch ( e ) {
377+ this . logger ?. debug ( '[AnyWeb]' , 'need to login' , e )
378+ return false
379+ }
380+ default :
381+ throw new ProviderRpcError (
382+ ProviderErrorCode . UnsupportedMethod ,
383+ 'Unsupported Method: ' + method
384+ )
385+ }
386+ } catch ( e : any ) {
387+ // const codeList = {
388+ // cfx_sendTransaction: ProviderErrorCode.SendTransactionError,
389+ // anyweb_importAccount: ProviderErrorCode.ImportAddressError,
390+ // }
391+ this . logger ?. info ( `Error when handler request '${ method } '!` )
392+ throw new ProviderRpcError (
393+ ProviderErrorCode . RequestError ,
394+ isObject ( e ) && 'message' in e ? e . message : e ,
395+ isObject ( e ) && 'data' in e ? e . data : { }
396+ )
400397 }
401398 }
402399
0 commit comments