Skip to content

Commit 608b5de

Browse files
committed
feat(Available Cache): add New Cache and NetworkId / ChainId
1 parent f4eb613 commit 608b5de

File tree

3 files changed

+65
-25
lines changed

3 files changed

+65
-25
lines changed

src/provider.ts

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import {
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'
1313
import config from '../package.json'
1414
import { AddressType, getAddressType } from './utils/address'
1515

@@ -22,27 +22,19 @@ import { AddressType, getAddressType } from './utils/address'
2222
* const provider = new Provider()
2323
*/
2424
export 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

src/utils/common.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import * as forge from 'node-forge'
66
import axios from 'axios'
77
import { API_BASE_URL, BASE_URL } from '../config'
8+
import { IAuthResult } from './types'
9+
import { Provider } from '../provider'
810

911
/**
1012
* Check the window width to decide whether to show in full screen
@@ -105,3 +107,41 @@ export const callIframe = async (
105107
}
106108
return 'ok'
107109
}
110+
111+
export const readCache = (provider: Provider) => {
112+
try {
113+
const result = JSON.parse(
114+
(window.localStorage && window.localStorage.getItem('anyweb_info')) ||
115+
'{}'
116+
)
117+
if (
118+
Object.keys(result).length > 0 &&
119+
Object.keys(result).includes('address') &&
120+
Object.keys(result).includes('networkId') &&
121+
Object.keys(result).includes('chainId') &&
122+
Object.keys(result).includes('expires') &&
123+
result.expires > new Date().getTime()
124+
) {
125+
provider.address = result.address
126+
provider.networkId = result.networkId
127+
provider.chainId = result.chainId
128+
}
129+
} catch (e) {
130+
provider.logger.error(e)
131+
}
132+
}
133+
134+
export const setCache = (data: IAuthResult, provider: Provider) => {
135+
window.localStorage &&
136+
window.localStorage.setItem(
137+
'anyweb_info',
138+
JSON.stringify({
139+
...data,
140+
expires: 10 * 60 * 1000 + new Date().getTime(),
141+
})
142+
)
143+
provider.address = data.address
144+
provider.networkId = data.networkId
145+
provider.chainId = data.chainId
146+
return data
147+
}

src/utils/types.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,9 @@ export type ConsoleLike = Pick<
66
Console,
77
'log' | 'warn' | 'error' | 'debug' | 'info' | 'trace'
88
>
9+
10+
export interface IAuthResult {
11+
chainId: number
12+
networkId: number
13+
address: string[]
14+
}

0 commit comments

Comments
 (0)