88 IProvider ,
99 IRequestArguments ,
1010} from './interface/provider'
11- import { ConsoleLike } from './utils/types'
12- import { callIframe } from './utils/common'
11+ import { ConsoleLike , IAuthResult } from './utils/types'
12+ import { callIframe , readCache , setCache } from './utils/common'
1313import config from '../package.json'
1414import { AddressType , getAddressType } from './utils/address'
1515
@@ -22,27 +22,19 @@ import { AddressType, getAddressType } from './utils/address'
2222 * const provider = new Provider()
2323 */
2424export class Provider implements IProvider {
25- protected logger : ConsoleLike
26- protected appId : string
27- protected address : string [ ] = [ ]
25+ logger : ConsoleLike
26+ public readonly appId : string
27+ address : string [ ] = [ ]
28+ networkId = - 1
29+ chainId = - 1
2830
2931 constructor ( { logger, appId } : BaseProviderOptions ) {
3032 if ( ! logger ) {
3133 logger = console
3234 }
3335 this . logger = logger
3436 this . appId = appId
35-
36- try {
37- this . address =
38- JSON . parse (
39- ( window . localStorage &&
40- window . localStorage . getItem ( 'anyweb_address' ) ) ||
41- '[]'
42- ) || [ ]
43- } catch ( e ) {
44- this . logger . error ( e )
45- }
37+ readCache ( this )
4638
4739 // bind functions (to prevent consumers from making unbound calls)
4840 this . request = this . request . bind ( this )
@@ -125,26 +117,28 @@ export class Provider implements IProvider {
125117 ) : Promise < unknown > {
126118 switch ( method ) {
127119 case 'cfx_netVersion' :
128- return 1
120+ if ( this . networkId === - 1 ) {
121+ return 1
122+ }
123+ return this . networkId
129124 case 'cfx_chainId' :
130- return 1
125+ if ( this . chainId === - 1 ) {
126+ return 1
127+ }
128+ return this . chainId
131129 case 'cfx_requestAccounts' :
132130 return this . rawRequest ( 'cfx_accounts' )
133131 case 'cfx_accounts' :
134132 if ( this . address . length > 0 ) {
135133 return this . address
136134 }
137- this . address = ( await callIframe ( 'pages/dapp/auth' , {
135+ const result = ( await callIframe ( 'pages/dapp/auth' , {
138136 appId : this . appId ,
139137 params : params ? JSON . stringify ( params ) : '' ,
140138 chainId : ( await this . request ( { method : 'cfx_chainId' } ) ) as string ,
141139 authType : 'account' ,
142- } ) ) as string [ ]
143- window . localStorage &&
144- window . localStorage . setItem (
145- 'anyweb_address' ,
146- JSON . stringify ( this . address )
147- )
140+ } ) ) as IAuthResult
141+ setCache ( result , this )
148142 return this . address
149143 case 'cfx_sendTransaction' :
150144 const paramsObj = params
0 commit comments